PageRenderTime 77ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/php/main/inc/lib/fckeditor/editor/filemanager/connectors/php/io.php

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