Browse Source

[svn r21598] FS#2867 - The FCKEditor: Reworking the upload connector to be close to the 2.6.4 source original.

Ivan Tcholakov 15 years ago
parent
commit
199f61f1f7

+ 4 - 1
main/inc/lib/fckeditor/editor/filemanager/connectors/php/config.php

@@ -22,7 +22,10 @@
  * Configuration file for the File Manager Connector for PHP.
  */
 
-// Modifications by Ivan Tcholakov, JAN-2009.
+// Modifications by Ivan Tcholakov, JUN-2009.
+
+// Some language variables are needed.
+$language_file = array('create_course');
 
 // Loading the global initialization file, Dokeos LMS.
 require_once '../../../../../../global.inc.php';

+ 2 - 3
main/inc/lib/fckeditor/editor/filemanager/connectors/php/connector.php

@@ -29,7 +29,7 @@ require('./util.php') ;
 require('./io.php') ;
 require('./basexml.php') ;
 require('./commands.php') ;
-require('./phpcompat.php') ;
+//require('./phpcompat.php') ;
 
 if ( !$Config['Enabled'] )
 	SendError( 1, 'This connector is disabled. Please check the "editor/filemanager/connectors/php/config.php" file' ) ;
@@ -68,8 +68,7 @@ else
 {
 	// Map the "UserFiles" path to a local directory.
 	$GLOBALS["UserFilesDirectory"] = GetRootPath() . $GLOBALS["UserFilesPath"] ;	
-} 
-
+}
 
 DoResponse() ;
 

+ 25 - 13
main/inc/lib/fckeditor/editor/filemanager/connectors/php/io.php

@@ -22,7 +22,7 @@
  * This is the File Manager Connector for PHP.
  */
 
-// Modifications by Ivan Tcholakov, JAN-2009.
+// Modifications by Ivan Tcholakov, JUN-2009.
 
 function CombinePaths( $sBasePath, $sFolder )
 {
@@ -60,11 +60,11 @@ function GetResourceTypeDirectory( $resourceType, $sCommand )
 }
 
 /*
- * this is the function that is call to change the URL in the fckeditor source view
+ * This function is called to change the URL in the fckeditor source view.
 */
 function GetUrlFromPath( $resourceType, $folderPath, $sCommand )
 {
-	$resourceType =strtolower($resourceType); 
+	$resourceType = strtolower($resourceType); 
 	return $resourceType . $folderPath;
 }
 
@@ -76,10 +76,7 @@ function RemoveExtension( $fileName )
 function ServerMapFolder( $resourceType, $folderPath, $sCommand )
 {
 	// Get the resource type directory.
-	//this should be fixed
 	$resourceType = strtolower($resourceType);
-
-	// Get the resource type directory.
 	if ( $resourceType != 'file' )
 	{
 		$sResourceTypePath = $GLOBALS["UserFilesDirectory"] . $resourceType . '/' ;
@@ -90,8 +87,6 @@ function ServerMapFolder( $resourceType, $folderPath, $sCommand )
 	}
 	
 	// Ensure that the directory exists.
-	// Modified by Ivan Tcholakov.
-	//CreateServerFolder( $sResourceTypePath ) ;
 	if ( $resourceType != 'file' )
 	{
 		CreateServerFolder( $sResourceTypePath ) ;
@@ -191,10 +186,26 @@ function CreateServerFolder( $folderPath, $lastFolder = null )
 
 function GetRootPath()
 {
+	if (!isset($_SERVER)) {
+		global $_SERVER;
+	}
 	$sRealPath = realpath( './' ) ;
-	$sSelfPath = htmlentities($_SERVER['PHP_SELF']);
-	$sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;	
-	return substr( $sRealPath, 0, strlen( $sRealPath ) - strlen( $sSelfPath ) ) ;
+	// #2124 ensure that no slash is at the end
+	$sRealPath = rtrim($sRealPath,"\\/");
+
+	$sSelfPath = $_SERVER['PHP_SELF'] ;
+	$sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;
+
+	$sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ;
+
+	$position = strpos( $sRealPath, $sSelfPath ) ;
+
+	// 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
+	if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) )
+		SendError( 1, 'Sorry, the physical root path of the file repository can not be resolved (the function GetRootPath() failed).' ) ;
+
+	return substr( $sRealPath, 0, $position ) ;
 }
 
 // Emulate the asp Server.mapPath function.
@@ -311,8 +322,9 @@ function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $custom
 (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;}}})();
 EOF;
 
-	$rpl = array( '\\' => '\\\\', '"' => '\\"' ) ;
-	echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ;
+	$search = array( '\\', '"' );
+	$replace = array( '\\\\', '\\"' );
+	echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . str_replace( $search, $replace, $fileUrl ) . '","' . str_replace( $search, $replace, $fileName ) . '", "' . str_replace( $search, $replace, $customMsg ) . '") ;' ;
 	echo '</script>' ;
 	exit ;
 }

+ 241 - 0
main/inc/lib/fckeditor/editor/filemanager/connectors/php/upload.php

@@ -0,0 +1,241 @@
+<?php 
+/*
+ * FCKeditor - The text editor for Internet - http://www.fckeditor.net
+ * Copyright (C) 2003-2009 Frederico Caldeira Knabben
+ *
+ * == BEGIN LICENSE ==
+ *
+ * Licensed under the terms of any of the following licenses at your
+ * choice:
+ *
+ *  - GNU General Public License Version 2 or later (the "GPL")
+ *    http://www.gnu.org/licenses/gpl.html
+ *
+ *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
+ *    http://www.gnu.org/licenses/lgpl.html
+ *
+ *  - Mozilla Public License Version 1.1 or later (the "MPL")
+ *    http://www.mozilla.org/MPL/MPL-1.1.html
+ *
+ * == END LICENSE ==
+ *
+ * This is the "File Uploader" for PHP.
+ */
+
+require('./config.php') ;
+require_once api_get_path(INCLUDE_PATH).'lib/fckeditor/repositories_config.php';
+require('./util.php') ;
+require('./io.php') ;
+require('./commands.php') ;
+//require('./phpcompat.php') ;
+
+function SendError( $number, $text )
+{
+	SendUploadResults( $number, '', '', $text ) ;
+}
+
+function check_and_create_resource_directory($repository_path, $resource_directory, $resource_directory_name)
+{
+	global $permissions_for_new_directories;
+
+	$resource_directory_full_path = substr($repository_path, 0, strlen($repository_path) - 1) . $resource_directory . '/';
+
+	if (!is_dir($resource_directory_full_path))
+	{
+		if (@mkdir($resource_directory_full_path, $permissions_for_new_directories))
+		{
+			// While we are in a course: Registering the newly created folder in the course's database.
+			if (api_is_in_course())
+			{
+				global $_course, $_user;
+				global $group_properties, $to_group_id;
+				$group_directory = !empty($group_properties['directory']) ? $group_properties['directory'] : '';
+
+				$doc_id = add_document($_course, $group_directory.$resource_directory, 'folder', 0, $resource_directory_name);
+				api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id'], $to_group_id);
+			}
+			return true;
+		}
+		return false;
+	}
+	return true;
+}
+
+// Check if this uploader has been enabled.
+if ( !$Config['Enabled'] )
+	SendUploadResults( '1', '', '', 'This file uploader is disabled. Please check the "editor/filemanager/connectors/php/config.php" file' ) ;
+
+// Check if the file has been correctly uploaded.
+if ( !isset( $_FILES['NewFile'] ) || is_null( $_FILES['NewFile']['tmp_name'] ) || $_FILES['NewFile']['name'] == '' )
+	SendUploadResults( '202' ) ;
+
+// Get the posted file.
+$oFile = $_FILES['NewFile'] ;
+
+// Get the uploaded file name and extension.
+$sFileName = $oFile['name'] ;
+$sOriginalFileName = $sFileName ;
+$sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
+$sExtension = strtolower( $sExtension ) ;
+
+// The file type (from the QueryString, by default 'File').
+$sType = isset( $_GET['Type'] ) ? $_GET['Type'] : 'File' ;
+
+// Get the allowed and denied extensions arrays.
+$arAllowed	= $Config['AllowedExtensions'][$sType] ;
+$arDenied	= $Config['DeniedExtensions'][$sType] ;
+
+// Check if it is an allowed extension.
+if ( ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )  || ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )){
+	SendUploadResults( '202' ) ;
+}
+
+$sErrorNumber	= '0' ;
+$sFileUrl		= '' ;
+
+// Initializes the counter used to rename the file, if another one with the same name already exists.
+$iCounter = 0 ;
+
+$sType=strtolower($sType);
+
+// Choosing the repository to be used.
+if (api_is_in_course())
+{
+	if (!api_is_in_group())
+	{
+		// 1. We are inside a course and not in a group.
+		if (api_is_allowed_to_edit())
+		{
+			// 1.1. Teacher
+			$sServerDir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document/';
+			$sserverWebath = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/';
+		}
+		else
+		{
+			// 1.2. Student
+			$sServerDir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/';
+			$sserverWebath = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/shared_folder/sf_user_'.api_get_user_id().'/';
+		}
+	}
+	else
+	{
+		// 2. Inside a course and inside a group.
+		$sServerDir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/';
+		$sserverWebath = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/';
+	}
+}
+else
+{
+	if (api_is_platform_admin() && $_SESSION['this_section'] == 'platform_admin')
+	{
+		// 3. Platform administration activities.
+		$sServerDir = $_configuration['root_sys'].'home/default_platform_document/';
+		$sserverWebath = $_configuration['root_web'].'home/default_platform_document/';
+	}
+	else
+	{
+		// 4. The user is outside courses.
+		$sServerDir = $_configuration['root_sys'].'main/upload/users/'.api_get_user_id().'/my_files/';
+		$sserverWebath = $_configuration['root_web'].'main/upload/users/'.api_get_user_id().'/my_files/';
+	}
+}
+
+// Set the upload path according to the resource type.
+if ($sType == 'audio')
+{
+	check_and_create_resource_directory($sServerDir, '/audio', get_lang('Audio'));
+	$sServerDir = $sServerDir.'audio/';
+	$sserverWebath = $sserverWebath.'audio/';
+	$path = '/audio/';
+}
+elseif ($sType == 'mp3')
+{
+	$sType = 'audio';
+	check_and_create_resource_directory($sServerDir, '/audio', get_lang('Audio'));
+	$sServerDir = $sServerDir.'audio/';
+	$sserverWebath = $sserverWebath.'audio/';
+	$path = '/audio/';
+}
+elseif ($sType == 'flash')
+{
+	check_and_create_resource_directory($sServerDir, '/flash', get_lang('Flash'));
+	$sServerDir = $sServerDir.'flash/';
+	$sserverWebath = $sserverWebath.'flash/';
+	$path = '/flash/';
+}
+elseif ($sType == 'images')
+{
+	check_and_create_resource_directory($sServerDir, '/images', get_lang('Images'));
+	$sServerDir = $sServerDir.'images/';
+	$sserverWebath = $sserverWebath.'images/';
+	$path = '/images/';
+}
+elseif ($sType == 'video')
+{
+	check_and_create_resource_directory($sServerDir, '/video', get_lang('Video'));
+	$sServerDir = $sServerDir.'video/';
+	$sserverWebath = $sserverWebath.'video/';
+	$path = '/video/';
+}
+elseif ($sType == 'video/flv')
+{
+	check_and_create_resource_directory($sServerDir, '/video', get_lang('Video'));
+	check_and_create_resource_directory($sServerDir, '/video/flv', 'flv');
+	$sServerDir = $sServerDir.'video/flv/';
+	$sserverWebath = $sserverWebath.'video/flv/';
+	$path = '/video/flv/';
+}
+else
+{
+	$path = '/';
+}
+
+// Try to add an extension to the file if it has'nt one
+$sFileName = add_ext_on_mime(stripslashes($oFile['name']),$oFile['type']);
+
+// Replace dangerous characters
+$sFileName = replace_dangerous_char($sFileName,'strict');
+
+// Transform any .php file in .phps for security
+$sFileName = php2phps($sFileName);
+
+if ( is_file( $sServerDir.$sFileName ) )
+{
+	$dotIndex = strrpos($sFileName, '.');
+	$ext = '';
+	if(is_int($dotIndex)) 
+	{
+		$ext = substr($sFileName, $dotIndex);
+		$base = substr($sFileName, 0, $dotIndex);
+	}
+	$counter = 0;
+	while(is_file($sServerDir.$sFileName)) 
+	{
+		$counter++;
+		$sFileName = $base.'_'.$counter.$ext;
+	}
+}
+
+if (!move_uploaded_file( $oFile['tmp_name'], $sServerDir.$sFileName )) $sErrorNumber = '203' ; // Check php.ini setting.
+	
+if ( is_file( $sServerDir.$sFileName ) )
+{
+	$oldumask = umask(0) ;
+	chmod( $sServerDir.$sFileName, $permissions_for_new_files ) ;
+	umask( $oldumask ) ;
+
+	// If we are in a course we record the uploaded file in database.
+	if (api_is_in_course())
+	{
+		$document_name = $sFileName;
+		$document_size=$oFile["size"];
+		$group_directory = !empty($group_properties['directory']) ? $group_properties['directory'] : '';
+	
+		$doc_id = add_document($_course, $group_directory.$path.$document_name, 'file', $document_size, $document_name);
+		api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentCreated', $_user['user_id'], $to_group_id);
+	}
+}
+
+SendUploadResults( $sErrorNumber, $sserverWebath.$sFileName, $sFileName ) ;
+
+?>

+ 12 - 12
main/inc/lib/fckeditor/fckeditor.php

@@ -378,44 +378,44 @@ class FCKeditor
 			// URLs for opening the file browser for different resource types (file types):
 
 			// for images
-			$this->Config['ImageBrowserURL'] = $this->BasePath."editor/filemanager/browser/default/browser.html?Type=Images&Connector=".$this->BasePath."editor/filemanager/connectors/php/connector.php&ServerPath=$upload_path";
+			$this->Config['ImageBrowserURL'] = $this->BasePath.'editor/filemanager/browser/default/browser.html?Type=Images&Connector='.$this->BasePath.'editor/filemanager/connectors/php/connector.php&ServerPath='.$upload_path;
 
 			// for flash
-			$this->Config['FlashBrowserURL'] = $this->BasePath."editor/filemanager/browser/default/browser.html?Type=Flash&Connector=".$this->BasePath."editor/filemanager/connectors/php/connector.php&ServerPath=$upload_path";
+			$this->Config['FlashBrowserURL'] = $this->BasePath.'editor/filemanager/browser/default/browser.html?Type=Flash&Connector='.$this->BasePath.'editor/filemanager/connectors/php/connector.php&ServerPath='.$upload_path;
 	
 			// for audio files (mp3)
-			$this->Config['MP3BrowserURL'] = $this->BasePath."editor/filemanager/browser/default/browser.html?Type=MP3&Connector=".$this->BasePath."editor/filemanager/connectors/php/connector.php&ServerPath=$upload_path";
+			$this->Config['MP3BrowserURL'] = $this->BasePath.'editor/filemanager/browser/default/browser.html?Type=MP3&Connector='.$this->BasePath.'editor/filemanager/connectors/php/connector.php&ServerPath='.$upload_path;
 
 			// for videos
-			$this->Config['VideoBrowserURL'] = $this->BasePath."editor/filemanager/browser/default/browser.html?Type=Video&Connector=".$this->BasePath."editor/filemanager/connectors/php/connector.php&ServerPath=$upload_path";
+			$this->Config['VideoBrowserURL'] = $this->BasePath.'editor/filemanager/browser/default/browser.html?Type=Video&Connector='.$this->BasePath.'editor/filemanager/connectors/php/connector.php&ServerPath='.$upload_path;
 
 			// for videos (flv)
-			$this->Config['MediaBrowserURL'] = $this->BasePath."editor/filemanager/browser/default/browser.html?Type=Video/flv&Connector=".$this->BasePath."editor/filemanager/connectors/php/connector.php&ServerPath=$upload_path";
+			$this->Config['MediaBrowserURL'] = $this->BasePath.'editor/filemanager/browser/default/browser.html?Type=Video/flv&Connector='.$this->BasePath.'editor/filemanager/connectors/php/connector.php&ServerPath='.$upload_path;
 
 			// for links (any resource type)
-			$this->Config['LinkBrowserURL'] = $this->BasePath."editor/filemanager/browser/default/browser.html?Type=File&Connector=".$this->BasePath."editor/filemanager/connectors/php/connector.php&ServerPath=$upload_path";
+			$this->Config['LinkBrowserURL'] = $this->BasePath.'editor/filemanager/browser/default/browser.html?Type=File&Connector='.$this->BasePath.'editor/filemanager/connectors/php/connector.php&ServerPath='.$upload_path;
 		}
 
 		// URLs for making quick uplods for different resource types (file types).
 		// These URLs are used by the dialogs' quick upload tabs:
 
 		// for images
-		$this->Config['ImageUploadURL'] = $this->BasePath . "editor/filemanager/upload/php/upload.php?Type=Images&ServerPath=$upload_path" ;
+		$this->Config['ImageUploadURL'] = $this->BasePath.'editor/filemanager/connectors/php/upload.php?Type=Images&ServerPath='.$upload_path;
 
 		// for flash
-		$this->Config['FlashUploadURL'] = $this->BasePath . "editor/filemanager/upload/php/upload.php?Type=Flash&ServerPath=$upload_path" ;
+		$this->Config['FlashUploadURL'] = $this->BasePath.'editor/filemanager/connectors/php/upload.php?Type=Flash&ServerPath='.$upload_path;
 	
 		// for audio files (mp3)
-		$this->Config['MP3UploadURL'] = $this->BasePath . "editor/filemanager/upload/php/upload.php?Type=MP3&ServerPath=$upload_path" ;
+		$this->Config['MP3UploadURL'] = $this->BasePath.'editor/filemanager/connectors/php/upload.php?Type=MP3&ServerPath='.$upload_path;
 
 		// for videos
-		$this->Config['VideoUploadURL'] = $this->BasePath . "editor/filemanager/upload/php/upload.php?Type=Video&ServerPath=$upload_path" ;
+		$this->Config['VideoUploadURL'] = $this->BasePath.'editor/filemanager/connectors/php/upload.php?Type=Video&ServerPath='.$upload_path;
 
 		// for videos (flv)
-		$this->Config['MediaUploadURL'] = $this->BasePath . "editor/filemanager/upload/php/upload.php?Type=Video/flv&ServerPath=$upload_path" ;
+		$this->Config['MediaUploadURL'] = $this->BasePath.'editor/filemanager/connectors/php/upload.php?Type=Video/flv&ServerPath='.$upload_path;
 
 		// for links (any resource type)
-		$this->Config['LinkUploadURL'] = $this->BasePath . "editor/filemanager/upload/php/upload.php?Type=File&ServerPath=$upload_path" ;
+		$this->Config['LinkUploadURL'] = $this->BasePath.'editor/filemanager/connectors/php/upload.php?Type=File&ServerPath='.$upload_path;
 
 		// Passing user status related data to the editor.
 		$this->Config['UserIsCourseAdmin'] = api_is_allowed_to_edit() ? true : false;