1<?php 2/* 3 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 4 * Copyright (C) 2003-2009 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 */ 24require_once 'input_utils.php'; 25 26function CombinePaths( $sBasePath, $sFolder ) 27{ 28 return RemoveFromEnd( $sBasePath, '/' ) . '/' . RemoveFromStart( $sFolder, '/' ) ; 29} 30function GetResourceTypePath( $resourceType, $sCommand ) 31{ 32 global $Config ; 33 34 if ( $sCommand == "QuickUpload") 35 return $Config['QuickUploadPath'][$resourceType] ; 36 else 37 return $Config['FileTypesPath'][$resourceType] ; 38} 39 40function GetResourceTypeDirectory( $resourceType, $sCommand ) 41{ 42 global $Config ; 43 if ( $sCommand == "QuickUpload") 44 { 45 if ( strlen( $Config['QuickUploadAbsolutePath'][$resourceType] ) > 0 ) 46 return $Config['QuickUploadAbsolutePath'][$resourceType] ; 47 48 // Map the "UserFiles" path to a local directory. 49 return Server_MapPath( $Config['QuickUploadPath'][$resourceType] ) ; 50 } 51 else 52 { 53 if ( strlen( $Config['FileTypesAbsolutePath'][$resourceType] ) > 0 ) 54 return $Config['FileTypesAbsolutePath'][$resourceType] ; 55 56 // Map the "UserFiles" path to a local directory. 57 return Server_MapPath( $Config['FileTypesPath'][$resourceType] ) ; 58 } 59} 60 61function GetUrlFromPath( $resourceType, $folderPath, $sCommand ) 62{ 63 return CombinePaths( GetResourceTypePath( $resourceType, $sCommand ), $folderPath ) ; 64} 65 66function RemoveExtension( $fileName ) 67{ 68 return substr( $fileName, 0, strrpos( $fileName, '.' ) ) ; 69} 70 71function ServerMapFolder( $resourceType, $folderPath, $sCommand ) 72{ 73 // Get the resource type directory. 74 $sResourceTypePath = GetResourceTypeDirectory( $resourceType, $sCommand ) ; 75 76 // Ensure that the directory exists. 77 $sErrorMsg = CreateServerFolder( $sResourceTypePath ) ; 78 if ( $sErrorMsg != '' ) 79 SendError( 1, "Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})" ) ; 80 81 // Return the resource type directory combined with the required path. 82 return CombinePaths( $sResourceTypePath , $folderPath ) ; 83} 84 85function GetParentFolder( $folderPath ) 86{ 87 $sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-" ; 88 return preg_replace( $sPattern, '', $folderPath ) ; 89} 90 91function CreateServerFolder( $folderPath, $lastFolder = null ) 92{ 93 global $Config ; 94 $sParent = GetParentFolder( $folderPath ) ; 95 96 // Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms 97 while ( strpos($folderPath, '//') !== false ) 98 { 99 $folderPath = str_replace( '//', '/', $folderPath ) ; 100 $sParent = str_replace( '//', '/', $sParent ) ; 101 } 102 103 // Check if the parent exists, or create it. 104 if ( !file_exists( $sParent ) ) 105 { 106 //prevents agains infinite loop when we can't create root folder 107 if ( !is_null( $lastFolder ) && $lastFolder === $sParent) { 108 return "Can't create $folderPath directory" ; 109 } 110 111 $sErrorMsg = CreateServerFolder( $sParent, $folderPath ) ; 112 if ( $sErrorMsg != '' ) 113 return $sErrorMsg ; 114 } 115 116 if ( !file_exists( $folderPath ) ) 117 { 118 // Turn off all error reporting. 119 error_reporting( 0 ) ; 120 121 $php_errormsg = '' ; 122 // Enable error tracking to catch the error. 123 ini_set( 'track_errors', '1' ) ; 124 125 if ( isset( $Config['ChmodOnFolderCreate'] ) && !$Config['ChmodOnFolderCreate'] ) 126 { 127 mkdir( $folderPath ) ; 128 } 129 else 130 { 131 $permissions = 0777 ; 132 if ( isset( $Config['ChmodOnFolderCreate'] ) ) 133 { 134 $permissions = $Config['ChmodOnFolderCreate'] ; 135 } 136 // To create the folder with 0777 permissions, we need to set umask to zero. 137 $oldumask = umask(0) ; 138 mkdir( $folderPath, $permissions ) ; 139 umask( $oldumask ) ; 140 } 141 142 $sErrorMsg = $php_errormsg ; 143 144 // Restore the configurations. 145 ini_restore( 'track_errors' ) ; 146 ini_restore( 'error_reporting' ) ; 147 148 return $sErrorMsg ; 149 } 150 else 151 return '' ; 152} 153 154function GetRootPath() 155{ 156 if (!isset($_SERVER)) { 157 global $_SERVER; 158 } 159 $sRealPath = realpath( './' ) ; 160 // #2124 ensure that no slash is at the end 161 $sRealPath = rtrim($sRealPath,"\\/"); 162 163 $sSelfPath = $_SERVER['PHP_SELF'] ; 164 $sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ; 165 166 $sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ; 167 168 $position = strpos( $sRealPath, $sSelfPath ) ; 169 170 // This can check only that this script isn't run from a virtual dir 171 // But it avoids the problems that arise if it isn't checked 172 if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) ) 173 SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ; 174 175 return substr( $sRealPath, 0, $position ) ; 176} 177 178// Emulate the asp Server.mapPath function. 179// given an url path return the physical directory that it corresponds to 180function Server_MapPath( $path ) 181{ 182 // This function is available only for Apache 183 if ( function_exists( 'apache_lookup_uri' ) ) 184 { 185 $info = apache_lookup_uri( $path ) ; 186 return $info->filename . $info->path_info ; 187 } 188 189 // This isn't correct but for the moment there's no other solution 190 // If this script is under a virtual directory or symlink it will detect the problem and stop 191 return GetRootPath() . $path ; 192} 193 194function IsAllowedExt( $sExtension, $resourceType ) 195{ 196 global $Config ; 197 // Get the allowed and denied extensions arrays. 198 $arAllowed = $Config['AllowedExtensions'][$resourceType] ; 199 $arDenied = $Config['DeniedExtensions'][$resourceType] ; 200 201 if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) ) 202 return false ; 203 204 if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) ) 205 return false ; 206 207 return true ; 208} 209 210function IsAllowedType( $resourceType ) 211{ 212 global $Config ; 213 if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) ) 214 return false ; 215 216 return true ; 217} 218 219function IsAllowedCommand( $sCommand ) 220{ 221 global $Config ; 222 223 if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) ) 224 return false ; 225 226 return true ; 227} 228 229function GetCurrentFolder() 230{ 231 232 $sCurrentFolder = input_strval('CurrentFolder'); 233 if(!$sCurrentFolder) $sCurrentFolder = '/'; 234 235 // Check the current folder syntax (must begin and start with a slash). 236 if ( !preg_match( '|/$|', $sCurrentFolder ) ) 237 $sCurrentFolder .= '/' ; 238 if ( strpos( $sCurrentFolder, '/' ) !== 0 ) 239 $sCurrentFolder = '/' . $sCurrentFolder ; 240 241 // Ensure the folder path has no double-slashes 242 while ( strpos ($sCurrentFolder, '//') !== false ) { 243 $sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ; 244 } 245 246 // Check for invalid folder paths (..) 247 // if ( $sCurrentFolder == '..' ) SendError( 102, '' ) ; 248 if ( preg_match(",(/\.)|(//)|(\\\\)|([\:\*\?\"\<\>\|]),", $sCurrentFolder)) 249 SendError( 102, '' ) ; 250 251 return $sCurrentFolder ; 252 253 254 255} 256 257// Do a cleanup of the folder name to avoid possible problems 258function SanitizeFolderName( $sNewFolderName ) 259{ 260 $sNewFolderName = stripslashes( $sNewFolderName ) ; 261 262 // Remove . \ / | : ? * " < > 263 $sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFolderName ) ; 264 265 return $sNewFolderName ; 266} 267 268// Do a cleanup of the file name to avoid possible problems 269function SanitizeFileName( $sNewFileName ) 270{ 271 global $Config ; 272 273 $sNewFileName = stripslashes( $sNewFileName ) ; 274 275 // Replace dots in the name with underscores (only one dot can be there... security issue). 276 if ( $Config['ForceSingleExtension'] ) 277 $sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ; 278 279 // Remove \ / | : ? * " < > 280 $sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ; 281 282 return $sNewFileName ; 283} 284 285// This is the function that sends the results of the uploading process. 286function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' ) 287{ 288 // Minified version of the document.domain automatic fix script (#1919). 289 // The original script can be found at _dev/domain_fix_template.js 290 echo <<<EOF 291<script type="text/javascript"> 292(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;}}})(); 293EOF; 294 295 if ($errorNumber && $errorNumber != 201) { 296 $fileUrl = ""; 297 $fileName = ""; 298 } 299 300 $rpl = array( '\\' => '\\\\', '"' => '\\"' ) ; 301 echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ; 302 echo '</script>' ; 303 exit ; 304} 305 306?> 307