3 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
4 * Copyright (C) 2003-2008 Frederico Caldeira Knabben
8 * Licensed under the terms of any of the following licenses at your
11 * - GNU General Public License Version 2 or later (the "GPL")
12 * http://www.gnu.org/licenses/gpl.html
14 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
15 * http://www.gnu.org/licenses/lgpl.html
17 * - Mozilla Public License Version 1.1 or later (the "MPL")
18 * http://www.mozilla.org/MPL/MPL-1.1.html
22 * This is the File Manager Connector for PHP.
25 function GetFolders( $resourceType, $currentFolder )
27 // Map the virtual path to the local server path.
28 $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFolders' ) ;
30 // Array that will hold the folders names.
33 $oCurrentFolder = opendir( $sServerDir ) ;
35 while ( $sFile = readdir( $oCurrentFolder ) )
37 if ( $sFile != '.' && $sFile != '..' && is_dir( $sServerDir . $sFile ) )
38 $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
41 closedir( $oCurrentFolder ) ;
43 // Open the "Folders" node.
46 natcasesort( $aFolders ) ;
47 foreach ( $aFolders as $sFolder )
50 // Close the "Folders" node.
54 function GetFoldersAndFiles( $resourceType, $currentFolder )
56 // Map the virtual path to the local server path.
57 $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFoldersAndFiles' ) ;
59 // Arrays that will hold the folders and files names.
63 $oCurrentFolder = opendir( $sServerDir ) ;
65 while ( $sFile = readdir( $oCurrentFolder ) )
67 if ( $sFile != '.' && $sFile != '..' )
69 if ( is_dir( $sServerDir . $sFile ) )
70 $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
73 $iFileSize = @filesize( $sServerDir . $sFile ) ;
79 $iFileSize = round( $iFileSize / 1024 ) ;
80 if ( $iFileSize < 1 ) $iFileSize = 1 ;
83 $aFiles[] = '<File name="' . ConvertToXmlAttribute( $sFile ) . '" size="' . $iFileSize . '" />' ;
89 natcasesort( $aFolders ) ;
92 foreach ( $aFolders as $sFolder )
98 natcasesort( $aFiles ) ;
101 foreach ( $aFiles as $sFiles )
107 function CreateFolder( $resourceType, $currentFolder )
112 $sErrorNumber = '0' ;
115 if ( isset( $_GET['NewFolderName'] ) )
117 $sNewFolderName = $_GET['NewFolderName'] ;
118 $sNewFolderName = SanitizeFolderName( $sNewFolderName ) ;
120 if ( strpos( $sNewFolderName, '..' ) !== FALSE )
121 $sErrorNumber = '102' ; // Invalid folder name.
124 // Map the virtual path to the local server path of the current folder.
125 $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'CreateFolder' ) ;
127 if ( is_writable( $sServerDir ) )
129 $sServerDir .= $sNewFolderName ;
131 $sErrorMsg = CreateServerFolder( $sServerDir ) ;
133 switch ( $sErrorMsg )
136 $sErrorNumber = '0' ;
138 case 'Invalid argument' :
139 case 'No such file or directory' :
140 $sErrorNumber = '102' ; // Path too long.
143 $sErrorNumber = '110' ;
148 $sErrorNumber = '103' ;
152 $sErrorNumber = '102' ;
154 // Create the "Error" node.
155 echo '<Error number="' . $sErrorNumber . '" originalDescription="' . ConvertToXmlAttribute( $sErrorMsg ) . '" />' ;
158 function FileUpload( $resourceType, $currentFolder, $sCommand )
160 if (!isset($_FILES)) {
163 $sErrorNumber = '0' ;
166 if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) )
170 $oFile = $_FILES['NewFile'] ;
172 // Map the virtual path to the local server path.
173 $sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ;
175 // Get the uploaded file name.
176 $sFileName = $oFile['name'] ;
177 $sFileName = SanitizeFileName( $sFileName ) ;
179 $sOriginalFileName = $sFileName ;
181 // Get the extension.
182 $sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
183 $sExtension = strtolower( $sExtension ) ;
185 if ( isset( $Config['SecureImageUploads'] ) )
187 if ( ( $isImageValid = IsImageValid( $oFile['tmp_name'], $sExtension ) ) === false )
189 $sErrorNumber = '202' ;
193 if ( isset( $Config['HtmlExtensions'] ) )
195 if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) &&
196 ( $detectHtml = DetectHtml( $oFile['tmp_name'] ) ) === true )
198 $sErrorNumber = '202' ;
202 // Check if it is an allowed extension.
203 if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) )
209 $sFilePath = $sServerDir . $sFileName ;
211 if ( is_file( $sFilePath ) )
214 $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
215 $sErrorNumber = '201' ;
219 move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
221 if ( is_file( $sFilePath ) )
223 if ( isset( $Config['ChmodOnUpload'] ) && !$Config['ChmodOnUpload'] )
230 if ( isset( $Config['ChmodOnUpload'] ) && $Config['ChmodOnUpload'] )
232 $permissions = $Config['ChmodOnUpload'] ;
235 $oldumask = umask(0) ;
236 chmod( $sFilePath, $permissions ) ;
244 if ( file_exists( $sFilePath ) )
246 //previous checks failed, try once again
247 if ( isset( $isImageValid ) && $isImageValid === -1 && IsImageValid( $sFilePath, $sExtension ) === false )
249 @unlink( $sFilePath ) ;
250 $sErrorNumber = '202' ;
252 else if ( isset( $detectHtml ) && $detectHtml === -1 && DetectHtml( $sFilePath ) === true )
254 @unlink( $sFilePath ) ;
255 $sErrorNumber = '202' ;
260 $sErrorNumber = '202' ;
263 $sErrorNumber = '202' ;
266 $sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ;
267 $sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ;
269 SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ;