Back to index

moin  1.9.0~rc2
io.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 function CombinePaths( $sBasePath, $sFolder )
00025 {
00026        return RemoveFromEnd( $sBasePath, '/' ) . '/' . RemoveFromStart( $sFolder, '/' ) ;
00027 }
00028 function GetResourceTypePath( $resourceType, $sCommand )
00029 {
00030        global $Config ;
00031 
00032        if ( $sCommand == "QuickUpload")
00033               return $Config['QuickUploadPath'][$resourceType] ;
00034        else
00035               return $Config['FileTypesPath'][$resourceType] ;
00036 }
00037 
00038 function GetResourceTypeDirectory( $resourceType, $sCommand )
00039 {
00040        global $Config ;
00041        if ( $sCommand == "QuickUpload")
00042        {
00043               if ( strlen( $Config['QuickUploadAbsolutePath'][$resourceType] ) > 0 )
00044                      return $Config['QuickUploadAbsolutePath'][$resourceType] ;
00045 
00046               // Map the "UserFiles" path to a local directory.
00047               return Server_MapPath( $Config['QuickUploadPath'][$resourceType] ) ;
00048        }
00049        else
00050        {
00051               if ( strlen( $Config['FileTypesAbsolutePath'][$resourceType] ) > 0 )
00052                      return $Config['FileTypesAbsolutePath'][$resourceType] ;
00053 
00054               // Map the "UserFiles" path to a local directory.
00055               return Server_MapPath( $Config['FileTypesPath'][$resourceType] ) ;
00056        }
00057 }
00058 
00059 function GetUrlFromPath( $resourceType, $folderPath, $sCommand )
00060 {
00061        return CombinePaths( GetResourceTypePath( $resourceType, $sCommand ), $folderPath ) ;
00062 }
00063 
00064 function RemoveExtension( $fileName )
00065 {
00066        return substr( $fileName, 0, strrpos( $fileName, '.' ) ) ;
00067 }
00068 
00069 function ServerMapFolder( $resourceType, $folderPath, $sCommand )
00070 {
00071        // Get the resource type directory.
00072        $sResourceTypePath = GetResourceTypeDirectory( $resourceType, $sCommand ) ;
00073 
00074        // Ensure that the directory exists.
00075        $sErrorMsg = CreateServerFolder( $sResourceTypePath ) ;
00076        if ( $sErrorMsg != '' )
00077               SendError( 1, "Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})" ) ;
00078 
00079        // Return the resource type directory combined with the required path.
00080        return CombinePaths( $sResourceTypePath , $folderPath ) ;
00081 }
00082 
00083 function GetParentFolder( $folderPath )
00084 {
00085        $sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-" ;
00086        return preg_replace( $sPattern, '', $folderPath ) ;
00087 }
00088 
00089 function CreateServerFolder( $folderPath, $lastFolder = null )
00090 {
00091        global $Config ;
00092        $sParent = GetParentFolder( $folderPath ) ;
00093 
00094        // Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms
00095        while ( strpos($folderPath, '//') !== false )
00096        {
00097               $folderPath = str_replace( '//', '/', $folderPath ) ;
00098        }
00099 
00100        // Check if the parent exists, or create it.
00101        if ( !file_exists( $sParent ) )
00102        {
00103               //prevents agains infinite loop when we can't create root folder
00104               if ( !is_null( $lastFolder ) && $lastFolder === $sParent) {
00105                      return "Can't create $folderPath directory" ;
00106               }
00107 
00108               $sErrorMsg = CreateServerFolder( $sParent, $folderPath ) ;
00109               if ( $sErrorMsg != '' )
00110                      return $sErrorMsg ;
00111        }
00112 
00113        if ( !file_exists( $folderPath ) )
00114        {
00115               // Turn off all error reporting.
00116               error_reporting( 0 ) ;
00117 
00118               $php_errormsg = '' ;
00119               // Enable error tracking to catch the error.
00120               ini_set( 'track_errors', '1' ) ;
00121 
00122               if ( isset( $Config['ChmodOnFolderCreate'] ) && !$Config['ChmodOnFolderCreate'] )
00123               {
00124                      mkdir( $folderPath ) ;
00125               }
00126               else
00127               {
00128                      $permissions = 0777 ;
00129                      if ( isset( $Config['ChmodOnFolderCreate'] ) )
00130                      {
00131                             $permissions = $Config['ChmodOnFolderCreate'] ;
00132                      }
00133                      // To create the folder with 0777 permissions, we need to set umask to zero.
00134                      $oldumask = umask(0) ;
00135                      mkdir( $folderPath, $permissions ) ;
00136                      umask( $oldumask ) ;
00137               }
00138 
00139               $sErrorMsg = $php_errormsg ;
00140 
00141               // Restore the configurations.
00142               ini_restore( 'track_errors' ) ;
00143               ini_restore( 'error_reporting' ) ;
00144 
00145               return $sErrorMsg ;
00146        }
00147        else
00148               return '' ;
00149 }
00150 
00151 function GetRootPath()
00152 {
00153        if (!isset($_SERVER)) {
00154               global $_SERVER;
00155        }
00156        $sRealPath = realpath( './' ) ;
00157        // #2124 ensure that no slash is at the end
00158        $sRealPath = rtrim($sRealPath,"\\/");
00159 
00160        $sSelfPath = $_SERVER['PHP_SELF'] ;
00161        $sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;
00162 
00163        $sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ;
00164 
00165        $position = strpos( $sRealPath, $sSelfPath ) ;
00166 
00167        // This can check only that this script isn't run from a virtual dir
00168        // But it avoids the problems that arise if it isn't checked
00169        if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) )
00170               SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ;
00171 
00172        return substr( $sRealPath, 0, $position ) ;
00173 }
00174 
00175 // Emulate the asp Server.mapPath function.
00176 // given an url path return the physical directory that it corresponds to
00177 function Server_MapPath( $path )
00178 {
00179        // This function is available only for Apache
00180        if ( function_exists( 'apache_lookup_uri' ) )
00181        {
00182               $info = apache_lookup_uri( $path ) ;
00183               return $info->filename . $info->path_info ;
00184        }
00185 
00186        // This isn't correct but for the moment there's no other solution
00187        // If this script is under a virtual directory or symlink it will detect the problem and stop
00188        return GetRootPath() . $path ;
00189 }
00190 
00191 function IsAllowedExt( $sExtension, $resourceType )
00192 {
00193        global $Config ;
00194        // Get the allowed and denied extensions arrays.
00195        $arAllowed    = $Config['AllowedExtensions'][$resourceType] ;
00196        $arDenied     = $Config['DeniedExtensions'][$resourceType] ;
00197 
00198        if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )
00199               return false ;
00200 
00201        if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )
00202               return false ;
00203 
00204        return true ;
00205 }
00206 
00207 function IsAllowedType( $resourceType )
00208 {
00209        global $Config ;
00210        if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) )
00211               return false ;
00212 
00213        return true ;
00214 }
00215 
00216 function IsAllowedCommand( $sCommand )
00217 {
00218        global $Config ;
00219 
00220        if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) )
00221               return false ;
00222 
00223        return true ;
00224 }
00225 
00226 function GetCurrentFolder()
00227 {
00228        if (!isset($_GET)) {
00229               global $_GET;
00230        }
00231        $sCurrentFolder      = isset( $_GET['CurrentFolder'] ) ? $_GET['CurrentFolder'] : '/' ;
00232 
00233        // Check the current folder syntax (must begin and start with a slash).
00234        if ( !preg_match( '|/$|', $sCurrentFolder ) )
00235               $sCurrentFolder .= '/' ;
00236        if ( strpos( $sCurrentFolder, '/' ) !== 0 )
00237               $sCurrentFolder = '/' . $sCurrentFolder ;
00238 
00239        // Ensure the folder path has no double-slashes
00240        while ( strpos ($sCurrentFolder, '//') !== false ) {
00241               $sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ;
00242        }
00243 
00244        // Check for invalid folder paths (..)
00245        if ( strpos( $sCurrentFolder, '..' ) || strpos( $sCurrentFolder, "\\" ))
00246               SendError( 102, '' ) ;
00247 
00248        if ( preg_match(",(/\.)|[[:cntrl:]]|(//)|(\\\\)|([\:\*\?\"<>\|]),", $sCurrentFolder))
00249               SendError( 102, '' ) ;
00250 
00251        return $sCurrentFolder ;
00252 }
00253 
00254 // Do a cleanup of the folder name to avoid possible problems
00255 function SanitizeFolderName( $sNewFolderName )
00256 {
00257        $sNewFolderName = stripslashes( $sNewFolderName ) ;
00258 
00259        // Remove . \ / | : ? * " < >
00260        $sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFolderName ) ;
00261 
00262        return $sNewFolderName ;
00263 }
00264 
00265 // Do a cleanup of the file name to avoid possible problems
00266 function SanitizeFileName( $sNewFileName )
00267 {
00268        global $Config ;
00269 
00270        $sNewFileName = stripslashes( $sNewFileName ) ;
00271 
00272        // Replace dots in the name with underscores (only one dot can be there... security issue).
00273        if ( $Config['ForceSingleExtension'] )
00274               $sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
00275 
00276        // Remove \ / | : ? * " < >
00277        $sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ;
00278 
00279        return $sNewFileName ;
00280 }
00281 
00282 // This is the function that sends the results of the uploading process.
00283 function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' )
00284 {
00285        // Minified version of the document.domain automatic fix script (#1919).
00286        // The original script can be found at _dev/domain_fix_template.js
00287        echo <<<EOF
00288 <script type="text/javascript">
00289 (function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();
00290 EOF;
00291 
00292        if ($errorNumber && $errorNumber != 201) {
00293               $fileUrl = "";
00294               $fileName = "";
00295        }
00296 
00297        $rpl = array( '\\' => '\\\\', '"' => '\\"' ) ;
00298        echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ;
00299        echo '</script>' ;
00300        exit ;
00301 }
00302 
00303 ?>