Posted By

sk8rjess on 05/12/08


Tagged


Versions (?)

commands


 / Published in: PHP
 

  1. <?php
  2. /*
  3.  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  4.  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  5.  *
  6.  * == BEGIN LICENSE ==
  7.  *
  8.  * Licensed under the terms of any of the following licenses at your
  9.  * choice:
  10.  *
  11.  * - GNU General Public License Version 2 or later (the "GPL")
  12.  * http://www.gnu.org/licenses/gpl.html
  13.  *
  14.  * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  15.  * http://www.gnu.org/licenses/lgpl.html
  16.  *
  17.  * - Mozilla Public License Version 1.1 or later (the "MPL")
  18.  * http://www.mozilla.org/MPL/MPL-1.1.html
  19.  *
  20.  * == END LICENSE ==
  21.  *
  22.  * This is the File Manager Connector for PHP.
  23.  */
  24.  
  25. function GetFolders( $resourceType, $currentFolder )
  26. {
  27. // Map the virtual path to the local server path.
  28. $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFolders' ) ;
  29.  
  30. // Array that will hold the folders names.
  31. $aFolders = array() ;
  32.  
  33. $oCurrentFolder = opendir( $sServerDir ) ;
  34.  
  35. while ( $sFile = readdir( $oCurrentFolder ) )
  36. {
  37. if ( $sFile != '.' && $sFile != '..' && is_dir( $sServerDir . $sFile ) )
  38. $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
  39. }
  40.  
  41. closedir( $oCurrentFolder ) ;
  42.  
  43. // Open the "Folders" node.
  44. echo "<Folders>" ;
  45.  
  46. natcasesort( $aFolders ) ;
  47. foreach ( $aFolders as $sFolder )
  48. echo $sFolder ;
  49.  
  50. // Close the "Folders" node.
  51. echo "</Folders>" ;
  52. }
  53.  
  54. function GetFoldersAndFiles( $resourceType, $currentFolder )
  55. {
  56. // Map the virtual path to the local server path.
  57. $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFoldersAndFiles' ) ;
  58.  
  59. // Arrays that will hold the folders and files names.
  60. $aFolders = array() ;
  61. $aFiles = array() ;
  62.  
  63. $oCurrentFolder = opendir( $sServerDir ) ;
  64.  
  65. while ( $sFile = readdir( $oCurrentFolder ) )
  66. {
  67. if ( $sFile != '.' && $sFile != '..' )
  68. {
  69. if ( is_dir( $sServerDir . $sFile ) )
  70. $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
  71. else
  72. {
  73. $iFileSize = @filesize( $sServerDir . $sFile ) ;
  74. if ( !$iFileSize ) {
  75. $iFileSize = 0 ;
  76. }
  77. if ( $iFileSize > 0 )
  78. {
  79. $iFileSize = round( $iFileSize / 1024 ) ;
  80. if ( $iFileSize < 1 ) $iFileSize = 1 ;
  81. }
  82.  
  83. $aFiles[] = '<File name="' . ConvertToXmlAttribute( $sFile ) . '" size="' . $iFileSize . '" />' ;
  84. }
  85. }
  86. }
  87.  
  88. // Send the folders
  89. natcasesort( $aFolders ) ;
  90. echo '<Folders>' ;
  91.  
  92. foreach ( $aFolders as $sFolder )
  93. echo $sFolder ;
  94.  
  95. echo '</Folders>' ;
  96.  
  97. // Send the files
  98. natcasesort( $aFiles ) ;
  99. echo '<Files>' ;
  100.  
  101. foreach ( $aFiles as $sFiles )
  102. echo $sFiles ;
  103.  
  104. echo '</Files>' ;
  105. }
  106.  
  107. function CreateFolder( $resourceType, $currentFolder )
  108. {
  109. if (!isset($_GET)) {
  110. global $_GET;
  111. }
  112. $sErrorNumber = '0' ;
  113. $sErrorMsg = '' ;
  114.  
  115. if ( isset( $_GET['NewFolderName'] ) )
  116. {
  117. $sNewFolderName = $_GET['NewFolderName'] ;
  118. $sNewFolderName = SanitizeFolderName( $sNewFolderName ) ;
  119.  
  120. if ( strpos( $sNewFolderName, '..' ) !== FALSE )
  121. $sErrorNumber = '102' ; // Invalid folder name.
  122. else
  123. {
  124. // Map the virtual path to the local server path of the current folder.
  125. $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'CreateFolder' ) ;
  126.  
  127. if ( is_writable( $sServerDir ) )
  128. {
  129. $sServerDir .= $sNewFolderName ;
  130.  
  131. $sErrorMsg = CreateServerFolder( $sServerDir ) ;
  132.  
  133. switch ( $sErrorMsg )
  134. {
  135. case '' :
  136. $sErrorNumber = '0' ;
  137. break ;
  138. case 'Invalid argument' :
  139. case 'No such file or directory' :
  140. $sErrorNumber = '102' ; // Path too long.
  141. break ;
  142. default :
  143. $sErrorNumber = '110' ;
  144. break ;
  145. }
  146. }
  147. else
  148. $sErrorNumber = '103' ;
  149. }
  150. }
  151. else
  152. $sErrorNumber = '102' ;
  153.  
  154. // Create the "Error" node.
  155. echo '<Error number="' . $sErrorNumber . '" originalDescription="' . ConvertToXmlAttribute( $sErrorMsg ) . '" />' ;
  156. }
  157.  
  158. function DeleteFile($resourceType, $currentFolder , $deleteFileName )
  159. {
  160. $sErrorNumber = '0' ;
  161. $sErrorMsg = '' ;
  162.  
  163. if ( isset( $deleteFileName ) )
  164. {
  165. $sServerDir = ServerMapFolder( $resourceType, $currentFolder ) ;
  166.  
  167. $sServerFile = $sServerDir . $deleteFileName;
  168.  
  169. $sErrorMsg = DeleteServerFile( $sServerFile ) ;
  170.  
  171. switch ( $sErrorMsg )
  172. {
  173. case '' :
  174. $sErrorNumber = '0' ;
  175. break ;
  176. case 'Could not delete the file - Check permissions' :
  177. $sErrorNumber = '102' ; // Path too long.
  178. break ;
  179. default :
  180. $sErrorNumber = '110' ;
  181. break ;
  182. }
  183. }
  184. else
  185. $sErrorNumber = '103' ;
  186.  
  187. // Create the "Error" node.
  188. echo '<Error number="' . $sErrorNumber . '" originalDescription="' . ConvertToXmlAttribute( $sErrorMsg ) . '" />' ;
  189. }
  190.  
  191. function FileUpload( $resourceType, $currentFolder, $sCommand )
  192. {
  193. if (!isset($_FILES)) {
  194. global $_FILES;
  195. }
  196. $sErrorNumber = '0' ;
  197. $sFileName = '' ;
  198.  
  199. if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) )
  200. {
  201. global $Config ;
  202.  
  203. $oFile = $_FILES['NewFile'] ;
  204.  
  205. // Map the virtual path to the local server path.
  206. $sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ;
  207.  
  208. // Get the uploaded file name.
  209. $sFileName = $oFile['name'] ;
  210. $sFileName = SanitizeFileName( $sFileName ) ;
  211.  
  212. $sOriginalFileName = $sFileName ;
  213.  
  214. // Get the extension.
  215. $sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
  216. $sExtension = strtolower( $sExtension ) ;
  217.  
  218. if ( isset( $Config['SecureImageUploads'] ) )
  219. {
  220. if ( ( $isImageValid = IsImageValid( $oFile['tmp_name'], $sExtension ) ) === false )
  221. {
  222. $sErrorNumber = '202' ;
  223. }
  224. }
  225.  
  226. if ( isset( $Config['HtmlExtensions'] ) )
  227. {
  228. if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) &&
  229. ( $detectHtml = DetectHtml( $oFile['tmp_name'] ) ) === true )
  230. {
  231. $sErrorNumber = '202' ;
  232. }
  233. }
  234.  
  235. // Check if it is an allowed extension.
  236. if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) )
  237. {
  238. $iCounter = 0 ;
  239.  
  240. while ( true )
  241. {
  242. $sFilePath = $sServerDir . $sFileName ;
  243.  
  244. if ( is_file( $sFilePath ) )
  245. {
  246. $iCounter++ ;
  247. $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
  248. $sErrorNumber = '201' ;
  249. }
  250. else
  251. {
  252. move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
  253.  
  254. if ( is_file( $sFilePath ) )
  255. {
  256. if ( isset( $Config['ChmodOnUpload'] ) && !$Config['ChmodOnUpload'] )
  257. {
  258. break ;
  259. }
  260.  
  261. $permissions = 0777;
  262.  
  263. if ( isset( $Config['ChmodOnUpload'] ) && $Config['ChmodOnUpload'] )
  264. {
  265. $permissions = $Config['ChmodOnUpload'] ;
  266. }
  267.  
  268. $oldumask = umask(0) ;
  269. chmod( $sFilePath, $permissions ) ;
  270. umask( $oldumask ) ;
  271. }
  272.  
  273. break ;
  274. }
  275. }
  276.  
  277. if ( file_exists( $sFilePath ) )
  278. {
  279. //previous checks failed, try once again
  280. if ( isset( $isImageValid ) && $isImageValid === -1 && IsImageValid( $sFilePath, $sExtension ) === false )
  281. {
  282. @unlink( $sFilePath ) ;
  283. $sErrorNumber = '202' ;
  284. }
  285. else if ( isset( $detectHtml ) && $detectHtml === -1 && DetectHtml( $sFilePath ) === true )
  286. {
  287. @unlink( $sFilePath ) ;
  288. $sErrorNumber = '202' ;
  289. }
  290. }
  291. }
  292. else
  293. $sErrorNumber = '202' ;
  294. }
  295. else
  296. $sErrorNumber = '202' ;
  297.  
  298.  
  299. $sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ;
  300. $sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ;
  301.  
  302. SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ;
  303.  
  304. exit ;
  305. }
  306. ?>

Report this snippet  

You need to login to post a comment.