"remove index.{html|php} added by apache, which causes the upload/browsing in some apache configs to fail"

This commit is contained in:
Ralf Becker 2008-04-15 08:02:13 +00:00
parent e8ab381986
commit 30c265a5c7

View File

@ -1,272 +1,273 @@
<?php <?php
/* /*
* FCKeditor - The text editor for Internet - http://www.fckeditor.net * FCKeditor - The text editor for Internet - http://www.fckeditor.net
* Copyright (C) 2003-2007 Frederico Caldeira Knabben * Copyright (C) 2003-2007 Frederico Caldeira Knabben
* *
* == BEGIN LICENSE == * == BEGIN LICENSE ==
* *
* Licensed under the terms of any of the following licenses at your * Licensed under the terms of any of the following licenses at your
* choice: * choice:
* *
* - GNU General Public License Version 2 or later (the "GPL") * - GNU General Public License Version 2 or later (the "GPL")
* http://www.gnu.org/licenses/gpl.html * http://www.gnu.org/licenses/gpl.html
* *
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL") * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
* http://www.gnu.org/licenses/lgpl.html * http://www.gnu.org/licenses/lgpl.html
* *
* - Mozilla Public License Version 1.1 or later (the "MPL") * - Mozilla Public License Version 1.1 or later (the "MPL")
* http://www.mozilla.org/MPL/MPL-1.1.html * http://www.mozilla.org/MPL/MPL-1.1.html
* *
* == END LICENSE == * == END LICENSE ==
* *
* This is the File Manager Connector for PHP. * This is the File Manager Connector for PHP.
*/ */
function CombinePaths( $sBasePath, $sFolder ) function CombinePaths( $sBasePath, $sFolder )
{ {
return RemoveFromEnd( $sBasePath, '/' ) . '/' . RemoveFromStart( $sFolder, '/' ) ; return RemoveFromEnd( $sBasePath, '/' ) . '/' . RemoveFromStart( $sFolder, '/' ) ;
} }
function GetResourceTypePath( $resourceType, $sCommand ) function GetResourceTypePath( $resourceType, $sCommand )
{ {
global $Config ; global $Config ;
if ( $sCommand == "QuickUpload") if ( $sCommand == "QuickUpload")
return $Config['QuickUploadPath'][$resourceType] ; return $Config['QuickUploadPath'][$resourceType] ;
else else
return $Config['FileTypesPath'][$resourceType] ; return $Config['FileTypesPath'][$resourceType] ;
} }
function GetResourceTypeDirectory( $resourceType, $sCommand ) function GetResourceTypeDirectory( $resourceType, $sCommand )
{ {
global $Config ; global $Config ;
if ( $sCommand == "QuickUpload") if ( $sCommand == "QuickUpload")
{ {
if ( strlen( $Config['QuickUploadAbsolutePath'][$resourceType] ) > 0 ) if ( strlen( $Config['QuickUploadAbsolutePath'][$resourceType] ) > 0 )
return $Config['QuickUploadAbsolutePath'][$resourceType] ; return $Config['QuickUploadAbsolutePath'][$resourceType] ;
// Map the "UserFiles" path to a local directory. // Map the "UserFiles" path to a local directory.
return Server_MapPath( $Config['QuickUploadPath'][$resourceType] ) ; return Server_MapPath( $Config['QuickUploadPath'][$resourceType] ) ;
} }
else else
{ {
if ( strlen( $Config['FileTypesAbsolutePath'][$resourceType] ) > 0 ) if ( strlen( $Config['FileTypesAbsolutePath'][$resourceType] ) > 0 )
return $Config['FileTypesAbsolutePath'][$resourceType] ; return $Config['FileTypesAbsolutePath'][$resourceType] ;
// Map the "UserFiles" path to a local directory. // Map the "UserFiles" path to a local directory.
return Server_MapPath( $Config['FileTypesPath'][$resourceType] ) ; return Server_MapPath( $Config['FileTypesPath'][$resourceType] ) ;
} }
} }
function GetUrlFromPath( $resourceType, $folderPath, $sCommand ) function GetUrlFromPath( $resourceType, $folderPath, $sCommand )
{ {
return CombinePaths( GetResourceTypePath( $resourceType, $sCommand ), $folderPath ) ; return CombinePaths( GetResourceTypePath( $resourceType, $sCommand ), $folderPath ) ;
} }
function RemoveExtension( $fileName ) function RemoveExtension( $fileName )
{ {
return substr( $fileName, 0, strrpos( $fileName, '.' ) ) ; return substr( $fileName, 0, strrpos( $fileName, '.' ) ) ;
} }
function ServerMapFolder( $resourceType, $folderPath, $sCommand ) function ServerMapFolder( $resourceType, $folderPath, $sCommand )
{ {
// Get the resource type directory. // Get the resource type directory.
$sResourceTypePath = GetResourceTypeDirectory( $resourceType, $sCommand ) ; $sResourceTypePath = GetResourceTypeDirectory( $resourceType, $sCommand ) ;
// Ensure that the directory exists. // Ensure that the directory exists.
$sErrorMsg = CreateServerFolder( $sResourceTypePath ) ; $sErrorMsg = CreateServerFolder( $sResourceTypePath ) ;
if ( $sErrorMsg != '' ) if ( $sErrorMsg != '' )
SendError( 1, "Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})" ) ; SendError( 1, "Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})" ) ;
// Return the resource type directory combined with the required path. // Return the resource type directory combined with the required path.
return CombinePaths( $sResourceTypePath , $folderPath ) ; return CombinePaths( $sResourceTypePath , $folderPath ) ;
} }
function GetParentFolder( $folderPath ) function GetParentFolder( $folderPath )
{ {
$sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-" ; $sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-" ;
return preg_replace( $sPattern, '', $folderPath ) ; return preg_replace( $sPattern, '', $folderPath ) ;
} }
function CreateServerFolder( $folderPath, $lastFolder = null ) function CreateServerFolder( $folderPath, $lastFolder = null )
{ {
$sParent = GetParentFolder( $folderPath ) ; $sParent = GetParentFolder( $folderPath ) ;
// Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms // Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms
while ( strpos($folderPath, '//') !== false ) while ( strpos($folderPath, '//') !== false )
{ {
$folderPath = str_replace( '//', '/', $folderPath ) ; $folderPath = str_replace( '//', '/', $folderPath ) ;
} }
// Check if the parent exists, or create it. // Check if the parent exists, or create it.
if ( !file_exists( $sParent ) ) if ( !file_exists( $sParent ) )
{ {
//prevents agains infinite loop when we can't create root folder //prevents agains infinite loop when we can't create root folder
if ( !is_null( $lastFolder ) && $lastFolder === $sParent) { if ( !is_null( $lastFolder ) && $lastFolder === $sParent) {
return "Can't create $folderPath directory" ; return "Can't create $folderPath directory" ;
} }
$sErrorMsg = CreateServerFolder( $sParent, $folderPath ) ; $sErrorMsg = CreateServerFolder( $sParent, $folderPath ) ;
if ( $sErrorMsg != '' ) if ( $sErrorMsg != '' )
return $sErrorMsg ; return $sErrorMsg ;
} }
if ( !file_exists( $folderPath ) ) if ( !file_exists( $folderPath ) )
{ {
// Turn off all error reporting. // Turn off all error reporting.
error_reporting( 0 ) ; error_reporting( 0 ) ;
$php_errormsg = '' ; $php_errormsg = '' ;
// Enable error tracking to catch the error. // Enable error tracking to catch the error.
ini_set( 'track_errors', '1' ) ; ini_set( 'track_errors', '1' ) ;
// To create the folder with 0777 permissions, we need to set umask to zero. // To create the folder with 0777 permissions, we need to set umask to zero.
$oldumask = umask(0) ; $oldumask = umask(0) ;
mkdir( $folderPath, 0777 ) ; mkdir( $folderPath, 0777 ) ;
umask( $oldumask ) ; umask( $oldumask ) ;
$sErrorMsg = $php_errormsg ; $sErrorMsg = $php_errormsg ;
// Restore the configurations. // Restore the configurations.
ini_restore( 'track_errors' ) ; ini_restore( 'track_errors' ) ;
ini_restore( 'error_reporting' ) ; ini_restore( 'error_reporting' ) ;
return $sErrorMsg ; return $sErrorMsg ;
} }
else else
return '' ; return '' ;
} }
function GetRootPath() function GetRootPath()
{ {
if (!isset($_SERVER)) { if (!isset($_SERVER)) {
global $_SERVER; global $_SERVER;
} }
$sRealPath = realpath( './' ) ; $sRealPath = realpath( './' ) ;
$sSelfPath = $_SERVER['PHP_SELF'] ; $sSelfPath = $_SERVER['PHP_SELF'] ;
$sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ; $sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;
$sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ; $sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ;
$position = strpos( $sRealPath, $sSelfPath ) ; $position = strpos( $sRealPath, $sSelfPath ) ;
// This can check only that this script isn't run from a virtual dir // This can check only that this script isn't run from a virtual dir
// But it avoids the problems that arise if it isn't checked // But it avoids the problems that arise if it isn't checked
if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) ) if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) )
SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ; SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ;
return substr( $sRealPath, 0, $position ) ; return substr( $sRealPath, 0, $position ) ;
} }
// Emulate the asp Server.mapPath function. // Emulate the asp Server.mapPath function.
// given an url path return the physical directory that it corresponds to // given an url path return the physical directory that it corresponds to
function Server_MapPath( $path ) function Server_MapPath( $path )
{ {
// This function is available only for Apache // This function is available only for Apache
if ( function_exists( 'apache_lookup_uri' ) ) if ( function_exists( 'apache_lookup_uri' ) )
{ {
$info = apache_lookup_uri( $path ) ; $info = apache_lookup_uri( $path ) ;
return $info->filename . $info->path_info ; // RalfBecker 2008/04/15: remove index.{html|php} added by apache
} return str_replace(array('/index.html','/index.php'),'',$info->filename) . $info->path_info ;
}
// This isn't correct but for the moment there's no other solution
// If this script is under a virtual directory or symlink it will detect the problem and stop // This isn't correct but for the moment there's no other solution
return GetRootPath() . $path ; // If this script is under a virtual directory or symlink it will detect the problem and stop
} return GetRootPath() . $path ;
}
function IsAllowedExt( $sExtension, $resourceType )
{ function IsAllowedExt( $sExtension, $resourceType )
global $Config ; {
// Get the allowed and denied extensions arrays. global $Config ;
$arAllowed = $Config['AllowedExtensions'][$resourceType] ; // Get the allowed and denied extensions arrays.
$arDenied = $Config['DeniedExtensions'][$resourceType] ; $arAllowed = $Config['AllowedExtensions'][$resourceType] ;
$arDenied = $Config['DeniedExtensions'][$resourceType] ;
if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )
return false ; if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )
return false ;
if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )
return false ; if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )
return false ;
return true ;
} return true ;
}
function IsAllowedType( $resourceType )
{ function IsAllowedType( $resourceType )
global $Config ; {
if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) ) global $Config ;
return false ; if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) )
return false ;
return true ;
} return true ;
}
function IsAllowedCommand( $sCommand )
{ function IsAllowedCommand( $sCommand )
global $Config ; {
global $Config ;
if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) )
return false ; if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) )
return false ;
return true ;
} return true ;
}
function GetCurrentFolder()
{ function GetCurrentFolder()
if (!isset($_GET)) { {
global $_GET; if (!isset($_GET)) {
} global $_GET;
$sCurrentFolder = isset( $_GET['CurrentFolder'] ) ? $_GET['CurrentFolder'] : '/' ; }
$sCurrentFolder = isset( $_GET['CurrentFolder'] ) ? $_GET['CurrentFolder'] : '/' ;
// Check the current folder syntax (must begin and start with a slash).
if ( ! ereg( '/$', $sCurrentFolder ) ) $sCurrentFolder .= '/' ; // Check the current folder syntax (must begin and start with a slash).
if ( strpos( $sCurrentFolder, '/' ) !== 0 ) $sCurrentFolder = '/' . $sCurrentFolder ; if ( ! ereg( '/$', $sCurrentFolder ) ) $sCurrentFolder .= '/' ;
if ( strpos( $sCurrentFolder, '/' ) !== 0 ) $sCurrentFolder = '/' . $sCurrentFolder ;
// Ensure the folder path has no double-slashes
while ( strpos ($sCurrentFolder, '//') !== false ) { // Ensure the folder path has no double-slashes
$sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ; while ( strpos ($sCurrentFolder, '//') !== false ) {
} $sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ;
}
// Check for invalid folder paths (..)
if ( strpos( $sCurrentFolder, '..' ) ) // Check for invalid folder paths (..)
SendError( 102, '' ) ; if ( strpos( $sCurrentFolder, '..' ) )
SendError( 102, '' ) ;
return $sCurrentFolder ;
} return $sCurrentFolder ;
}
// Do a cleanup of the folder name to avoid possible problems
function SanitizeFolderName( $sNewFolderName ) // Do a cleanup of the folder name to avoid possible problems
{ function SanitizeFolderName( $sNewFolderName )
$sNewFolderName = stripslashes( $sNewFolderName ) ; {
$sNewFolderName = stripslashes( $sNewFolderName ) ;
// Remove . \ / | : ? * " < >
$sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>/', '_', $sNewFolderName ) ; // Remove . \ / | : ? * " < >
$sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>/', '_', $sNewFolderName ) ;
return $sNewFolderName ;
} return $sNewFolderName ;
}
// Do a cleanup of the file name to avoid possible problems
function SanitizeFileName( $sNewFileName ) // Do a cleanup of the file name to avoid possible problems
{ function SanitizeFileName( $sNewFileName )
global $Config ; {
global $Config ;
$sNewFileName = stripslashes( $sNewFileName ) ;
$sNewFileName = stripslashes( $sNewFileName ) ;
// Replace dots in the name with underscores (only one dot can be there... security issue).
if ( $Config['ForceSingleExtension'] ) // Replace dots in the name with underscores (only one dot can be there... security issue).
$sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ; if ( $Config['ForceSingleExtension'] )
$sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
// Remove \ / | : ? * " < >
$sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>/', '_', $sNewFileName ) ; // Remove \ / | : ? * " < >
$sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>/', '_', $sNewFileName ) ;
return $sNewFileName ;
} return $sNewFileName ;
}
// This is the function that sends the results of the uploading process.
function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' ) // This is the function that sends the results of the uploading process.
{ function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' )
echo '<script type="text/javascript">' ; {
$rpl = array( '\\' => '\\\\', '"' => '\\"' ) ; echo '<script type="text/javascript">' ;
echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ; $rpl = array( '\\' => '\\\\', '"' => '\\"' ) ;
echo '</script>' ; echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ;
exit ; echo '</script>' ;
} exit ;
}
?> ?>