Back to index

moin  1.9.0~rc2
commands.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
00004  * Copyright (C) 2003-2009 Frederico Caldeira Knabben
00005  *
00006  * == BEGIN LICENSE ==
00007  *
00008  * Licensed under the terms of any of the following licenses at your
00009  * choice:
00010  *
00011  *  - GNU General Public License Version 2 or later (the "GPL")
00012  *    http://www.gnu.org/licenses/gpl.html
00013  *
00014  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
00015  *    http://www.gnu.org/licenses/lgpl.html
00016  *
00017  *  - Mozilla Public License Version 1.1 or later (the "MPL")
00018  *    http://www.mozilla.org/MPL/MPL-1.1.html
00019  *
00020  * == END LICENSE ==
00021  *
00022  * This is the File Manager Connector for PHP.
00023  */
00024 
00025 function GetFolders( $resourceType, $currentFolder )
00026 {
00027        // Map the virtual path to the local server path.
00028        $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFolders' ) ;
00029 
00030        // Array that will hold the folders names.
00031        $aFolders     = array() ;
00032 
00033        $oCurrentFolder = @opendir( $sServerDir ) ;
00034 
00035        if ($oCurrentFolder !== false)
00036        {
00037               while ( $sFile = readdir( $oCurrentFolder ) )
00038               {
00039                      if ( $sFile != '.' && $sFile != '..' && is_dir( $sServerDir . $sFile ) )
00040                             $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
00041               }
00042               closedir( $oCurrentFolder ) ;
00043        }
00044 
00045        // Open the "Folders" node.
00046        echo "<Folders>" ;
00047 
00048        natcasesort( $aFolders ) ;
00049        foreach ( $aFolders as $sFolder )
00050               echo $sFolder ;
00051 
00052        // Close the "Folders" node.
00053        echo "</Folders>" ;
00054 }
00055 
00056 function GetFoldersAndFiles( $resourceType, $currentFolder )
00057 {
00058        // Map the virtual path to the local server path.
00059        $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFoldersAndFiles' ) ;
00060 
00061        // Arrays that will hold the folders and files names.
00062        $aFolders     = array() ;
00063        $aFiles              = array() ;
00064 
00065        $oCurrentFolder = @opendir( $sServerDir ) ;
00066 
00067        if ($oCurrentFolder !== false)
00068        {
00069               while ( $sFile = readdir( $oCurrentFolder ) )
00070               {
00071                      if ( $sFile != '.' && $sFile != '..' )
00072                      {
00073                             if ( is_dir( $sServerDir . $sFile ) )
00074                                    $aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
00075                             else
00076                             {
00077                                    $iFileSize = @filesize( $sServerDir . $sFile ) ;
00078                                    if ( !$iFileSize ) {
00079                                           $iFileSize = 0 ;
00080                                    }
00081                                    if ( $iFileSize > 0 )
00082                                    {
00083                                           $iFileSize = round( $iFileSize / 1024 ) ;
00084                                           if ( $iFileSize < 1 )
00085                                                  $iFileSize = 1 ;
00086                                    }
00087 
00088                                    $aFiles[] = '<File name="' . ConvertToXmlAttribute( $sFile ) . '" size="' . $iFileSize . '" />' ;
00089                             }
00090                      }
00091               }
00092               closedir( $oCurrentFolder ) ;
00093        }
00094 
00095        // Send the folders
00096        natcasesort( $aFolders ) ;
00097        echo '<Folders>' ;
00098 
00099        foreach ( $aFolders as $sFolder )
00100               echo $sFolder ;
00101 
00102        echo '</Folders>' ;
00103 
00104        // Send the files
00105        natcasesort( $aFiles ) ;
00106        echo '<Files>' ;
00107 
00108        foreach ( $aFiles as $sFiles )
00109               echo $sFiles ;
00110 
00111        echo '</Files>' ;
00112 }
00113 
00114 function CreateFolder( $resourceType, $currentFolder )
00115 {
00116        if (!isset($_GET)) {
00117               global $_GET;
00118        }
00119        $sErrorNumber = '0' ;
00120        $sErrorMsg           = '' ;
00121 
00122        if ( isset( $_GET['NewFolderName'] ) )
00123        {
00124               $sNewFolderName = $_GET['NewFolderName'] ;
00125               $sNewFolderName = SanitizeFolderName( $sNewFolderName ) ;
00126 
00127               if ( strpos( $sNewFolderName, '..' ) !== FALSE )
00128                      $sErrorNumber = '102' ;            // Invalid folder name.
00129               else
00130               {
00131                      // Map the virtual path to the local server path of the current folder.
00132                      $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'CreateFolder' ) ;
00133 
00134                      if ( is_writable( $sServerDir ) )
00135                      {
00136                             $sServerDir .= $sNewFolderName ;
00137 
00138                             $sErrorMsg = CreateServerFolder( $sServerDir ) ;
00139 
00140                             switch ( $sErrorMsg )
00141                             {
00142                                    case '' :
00143                                           $sErrorNumber = '0' ;
00144                                           break ;
00145                                    case 'Invalid argument' :
00146                                    case 'No such file or directory' :
00147                                           $sErrorNumber = '102' ;            // Path too long.
00148                                           break ;
00149                                    default :
00150                                           $sErrorNumber = '110' ;
00151                                           break ;
00152                             }
00153                      }
00154                      else
00155                             $sErrorNumber = '103' ;
00156               }
00157        }
00158        else
00159               $sErrorNumber = '102' ;
00160 
00161        // Create the "Error" node.
00162        echo '<Error number="' . $sErrorNumber . '" />' ;
00163 }
00164 
00165 function FileUpload( $resourceType, $currentFolder, $sCommand )
00166 {
00167        if (!isset($_FILES)) {
00168               global $_FILES;
00169        }
00170        $sErrorNumber = '0' ;
00171        $sFileName = '' ;
00172 
00173        if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) )
00174        {
00175               global $Config ;
00176 
00177               $oFile = $_FILES['NewFile'] ;
00178 
00179               // Map the virtual path to the local server path.
00180               $sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ;
00181 
00182               // Get the uploaded file name.
00183               $sFileName = $oFile['name'] ;
00184               $sFileName = SanitizeFileName( $sFileName ) ;
00185 
00186               $sOriginalFileName = $sFileName ;
00187 
00188               // Get the extension.
00189               $sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
00190               $sExtension = strtolower( $sExtension ) ;
00191 
00192               if ( isset( $Config['SecureImageUploads'] ) )
00193               {
00194                      if ( ( $isImageValid = IsImageValid( $oFile['tmp_name'], $sExtension ) ) === false )
00195                      {
00196                             $sErrorNumber = '202' ;
00197                      }
00198               }
00199 
00200               if ( isset( $Config['HtmlExtensions'] ) )
00201               {
00202                      if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) &&
00203                             ( $detectHtml = DetectHtml( $oFile['tmp_name'] ) ) === true )
00204                      {
00205                             $sErrorNumber = '202' ;
00206                      }
00207               }
00208 
00209               // Check if it is an allowed extension.
00210               if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) )
00211               {
00212                      $iCounter = 0 ;
00213 
00214                      while ( true )
00215                      {
00216                             $sFilePath = $sServerDir . $sFileName ;
00217 
00218                             if ( is_file( $sFilePath ) )
00219                             {
00220                                    $iCounter++ ;
00221                                    $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
00222                                    $sErrorNumber = '201' ;
00223                             }
00224                             else
00225                             {
00226                                    move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
00227 
00228                                    if ( is_file( $sFilePath ) )
00229                                    {
00230                                           if ( isset( $Config['ChmodOnUpload'] ) && !$Config['ChmodOnUpload'] )
00231                                           {
00232                                                  break ;
00233                                           }
00234 
00235                                           $permissions = 0777;
00236 
00237                                           if ( isset( $Config['ChmodOnUpload'] ) && $Config['ChmodOnUpload'] )
00238                                           {
00239                                                  $permissions = $Config['ChmodOnUpload'] ;
00240                                           }
00241 
00242                                           $oldumask = umask(0) ;
00243                                           chmod( $sFilePath, $permissions ) ;
00244                                           umask( $oldumask ) ;
00245                                    }
00246 
00247                                    break ;
00248                             }
00249                      }
00250 
00251                      if ( file_exists( $sFilePath ) )
00252                      {
00253                             //previous checks failed, try once again
00254                             if ( isset( $isImageValid ) && $isImageValid === -1 && IsImageValid( $sFilePath, $sExtension ) === false )
00255                             {
00256                                    @unlink( $sFilePath ) ;
00257                                    $sErrorNumber = '202' ;
00258                             }
00259                             else if ( isset( $detectHtml ) && $detectHtml === -1 && DetectHtml( $sFilePath ) === true )
00260                             {
00261                                    @unlink( $sFilePath ) ;
00262                                    $sErrorNumber = '202' ;
00263                             }
00264                      }
00265               }
00266               else
00267                      $sErrorNumber = '202' ;
00268        }
00269        else
00270               $sErrorNumber = '202' ;
00271 
00272 
00273        $sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ;
00274        $sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ;
00275 
00276        SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ;
00277 
00278        exit ;
00279 }
00280 ?>