3
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
4
* Copyright (C) 2003-2009 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.
24
function CombinePaths( $sBasePath, $sFolder )
26
return RemoveFromEnd( $sBasePath, '/' ) . '/' . RemoveFromStart( $sFolder, '/' ) ;
28
function GetResourceTypePath( $resourceType, $sCommand )
32
if ( $sCommand == "QuickUpload")
33
return $Config['QuickUploadPath'][$resourceType] ;
35
return $Config['FileTypesPath'][$resourceType] ;
38
function GetResourceTypeDirectory( $resourceType, $sCommand )
41
if ( $sCommand == "QuickUpload")
43
if ( strlen( $Config['QuickUploadAbsolutePath'][$resourceType] ) > 0 )
44
return $Config['QuickUploadAbsolutePath'][$resourceType] ;
46
// Map the "UserFiles" path to a local directory.
47
return Server_MapPath( $Config['QuickUploadPath'][$resourceType] ) ;
51
if ( strlen( $Config['FileTypesAbsolutePath'][$resourceType] ) > 0 )
52
return $Config['FileTypesAbsolutePath'][$resourceType] ;
54
// Map the "UserFiles" path to a local directory.
55
return Server_MapPath( $Config['FileTypesPath'][$resourceType] ) ;
59
function GetUrlFromPath( $resourceType, $folderPath, $sCommand )
61
return CombinePaths( GetResourceTypePath( $resourceType, $sCommand ), $folderPath ) ;
64
function RemoveExtension( $fileName )
66
return substr( $fileName, 0, strrpos( $fileName, '.' ) ) ;
69
function ServerMapFolder( $resourceType, $folderPath, $sCommand )
71
// Get the resource type directory.
72
$sResourceTypePath = GetResourceTypeDirectory( $resourceType, $sCommand ) ;
74
// Ensure that the directory exists.
75
$sErrorMsg = CreateServerFolder( $sResourceTypePath ) ;
76
if ( $sErrorMsg != '' )
77
SendError( 1, "Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})" ) ;
79
// Return the resource type directory combined with the required path.
80
return CombinePaths( $sResourceTypePath , $folderPath ) ;
83
function GetParentFolder( $folderPath )
85
$sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-" ;
86
return preg_replace( $sPattern, '', $folderPath ) ;
89
function CreateServerFolder( $folderPath, $lastFolder = null )
92
$sParent = GetParentFolder( $folderPath ) ;
94
// Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms
95
while ( strpos($folderPath, '//') !== false )
97
$folderPath = str_replace( '//', '/', $folderPath ) ;
100
// Check if the parent exists, or create it.
101
if ( !file_exists( $sParent ) )
103
//prevents agains infinite loop when we can't create root folder
104
if ( !is_null( $lastFolder ) && $lastFolder === $sParent) {
105
return "Can't create $folderPath directory" ;
108
$sErrorMsg = CreateServerFolder( $sParent, $folderPath ) ;
109
if ( $sErrorMsg != '' )
113
if ( !file_exists( $folderPath ) )
115
// Turn off all error reporting.
116
error_reporting( 0 ) ;
119
// Enable error tracking to catch the error.
120
ini_set( 'track_errors', '1' ) ;
122
if ( isset( $Config['ChmodOnFolderCreate'] ) && !$Config['ChmodOnFolderCreate'] )
124
mkdir( $folderPath ) ;
128
$permissions = 0777 ;
129
if ( isset( $Config['ChmodOnFolderCreate'] ) )
131
$permissions = $Config['ChmodOnFolderCreate'] ;
133
// To create the folder with 0777 permissions, we need to set umask to zero.
134
$oldumask = umask(0) ;
135
mkdir( $folderPath, $permissions ) ;
139
$sErrorMsg = $php_errormsg ;
141
// Restore the configurations.
142
ini_restore( 'track_errors' ) ;
143
ini_restore( 'error_reporting' ) ;
151
function GetRootPath()
153
if (!isset($_SERVER)) {
156
$sRealPath = realpath( './' ) ;
157
// #2124 ensure that no slash is at the end
158
$sRealPath = rtrim($sRealPath,"\\/");
160
$sSelfPath = $_SERVER['PHP_SELF'] ;
161
$sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;
163
$sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ;
165
$position = strpos( $sRealPath, $sSelfPath ) ;
167
// This can check only that this script isn't run from a virtual dir
168
// But it avoids the problems that arise if it isn't checked
169
if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) )
170
SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ;
172
return substr( $sRealPath, 0, $position ) ;
175
// Emulate the asp Server.mapPath function.
176
// given an url path return the physical directory that it corresponds to
177
function Server_MapPath( $path )
179
// This function is available only for Apache
180
if ( function_exists( 'apache_lookup_uri' ) )
182
$info = apache_lookup_uri( $path ) ;
183
return $info->filename . $info->path_info ;
186
// This isn't correct but for the moment there's no other solution
187
// If this script is under a virtual directory or symlink it will detect the problem and stop
188
return GetRootPath() . $path ;
191
function IsAllowedExt( $sExtension, $resourceType )
194
// Get the allowed and denied extensions arrays.
195
$arAllowed = $Config['AllowedExtensions'][$resourceType] ;
196
$arDenied = $Config['DeniedExtensions'][$resourceType] ;
198
if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )
201
if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )
207
function IsAllowedType( $resourceType )
210
if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) )
216
function IsAllowedCommand( $sCommand )
220
if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) )
226
function GetCurrentFolder()
231
$sCurrentFolder = isset( $_GET['CurrentFolder'] ) ? $_GET['CurrentFolder'] : '/' ;
233
// Check the current folder syntax (must begin and start with a slash).
234
if ( !preg_match( '|/$|', $sCurrentFolder ) )
235
$sCurrentFolder .= '/' ;
236
if ( strpos( $sCurrentFolder, '/' ) !== 0 )
237
$sCurrentFolder = '/' . $sCurrentFolder ;
239
// Ensure the folder path has no double-slashes
240
while ( strpos ($sCurrentFolder, '//') !== false ) {
241
$sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ;
244
// Check for invalid folder paths (..)
245
if ( strpos( $sCurrentFolder, '..' ) || strpos( $sCurrentFolder, "\\" ))
246
SendError( 102, '' ) ;
248
if ( preg_match(",(/\.)|[[:cntrl:]]|(//)|(\\\\)|([\:\*\?\"\<\>\|]),", $sCurrentFolder))
249
SendError( 102, '' ) ;
251
return $sCurrentFolder ;
254
// Do a cleanup of the folder name to avoid possible problems
255
function SanitizeFolderName( $sNewFolderName )
257
$sNewFolderName = stripslashes( $sNewFolderName ) ;
259
// Remove . \ / | : ? * " < >
260
$sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFolderName ) ;
262
return $sNewFolderName ;
265
// Do a cleanup of the file name to avoid possible problems
266
function SanitizeFileName( $sNewFileName )
270
$sNewFileName = stripslashes( $sNewFileName ) ;
272
// Replace dots in the name with underscores (only one dot can be there... security issue).
273
if ( $Config['ForceSingleExtension'] )
274
$sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
276
// Remove \ / | : ? * " < >
277
$sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ;
279
return $sNewFileName ;
282
// This is the function that sends the results of the uploading process.
283
function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' )
285
// Minified version of the document.domain automatic fix script (#1919).
286
// The original script can be found at _dev/domain_fix_template.js
288
<script type="text/javascript">
289
(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;}}})();
292
if ($errorNumber && $errorNumber != 201) {
297
$rpl = array( '\\' => '\\\\', '"' => '\\"' ) ;
298
echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ;