PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/FHTML/filemanager/connectors/php/io.php

https://github.com/reshadf/Library
PHP | 311 lines | 216 code | 44 blank | 51 comment | 33 complexity | b929d8b49cd29750481901845ef5d0f9 MD5 | raw file
Possible License(s): LGPL-2.1
  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. if ( isset( $Config['ChmodOnFolderCreate'] ) )
  113. {
  114. $permissions = $Config['ChmodOnFolderCreate'] ;
  115. }
  116. // To create the folder with 0777 permissions, we need to set umask to zero.
  117. $oldumask = umask(0) ;
  118. mkdir( $folderPath, $permissions ) ;
  119. umask( $oldumask ) ;
  120. }
  121. $sErrorMsg = $php_errormsg ;
  122. // Restore the configurations.
  123. ini_restore( 'track_errors' ) ;
  124. ini_restore( 'error_reporting' ) ;
  125. return $sErrorMsg ;
  126. }
  127. else
  128. return '' ;
  129. }
  130. function GetRootPath()
  131. {
  132. if (!isset($_SERVER)) {
  133. global $_SERVER;
  134. }
  135. $sRealPath = realpath( './' ) ;
  136. // #2124 ensure that no slash is at the end
  137. $sRealPath = rtrim($sRealPath,"\\/");
  138. $sSelfPath = $_SERVER['PHP_SELF'] ;
  139. $sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;
  140. $sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ;
  141. $position = strpos( $sRealPath, $sSelfPath ) ;
  142. // This can check only that this script isn't run from a virtual dir
  143. // But it avoids the problems that arise if it isn't checked
  144. if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) )
  145. SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ;
  146. return substr( $sRealPath, 0, $position ) ;
  147. }
  148. // Emulate the asp Server.mapPath function.
  149. // given an url path return the physical directory that it corresponds to
  150. function Server_MapPath( $path )
  151. {
  152. // This function is available only for Apache
  153. if ( function_exists( 'apache_lookup_uri' ) )
  154. {
  155. $info = apache_lookup_uri( $path ) ;
  156. return $info->filename . $info->path_info ;
  157. }
  158. // This isn't correct but for the moment there's no other solution
  159. // If this script is under a virtual directory or symlink it will detect the problem and stop
  160. return GetRootPath() . $path ;
  161. }
  162. function IsAllowedExt( $sExtension, $resourceType )
  163. {
  164. global $Config ;
  165. // Get the allowed and denied extensions arrays.
  166. $arAllowed = $Config['AllowedExtensions'][$resourceType] ;
  167. $arDenied = $Config['DeniedExtensions'][$resourceType] ;
  168. if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )
  169. return false ;
  170. if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )
  171. return false ;
  172. return true ;
  173. }
  174. function IsAllowedType( $resourceType )
  175. {
  176. global $Config ;
  177. if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) )
  178. return false ;
  179. return true ;
  180. }
  181. function IsAllowedCommand( $sCommand )
  182. {
  183. global $Config ;
  184. if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) )
  185. return false ;
  186. return true ;
  187. }
  188. function GetCurrentFolder()
  189. {
  190. if (!isset($_GET)) {
  191. global $_GET;
  192. }
  193. $sCurrentFolder = isset( $_GET['CurrentFolder'] ) ? $_GET['CurrentFolder'] : '/' ;
  194. // Check the current folder syntax (must begin and start with a slash).
  195. if ( !preg_match( '|/$|', $sCurrentFolder ) )
  196. $sCurrentFolder .= '/' ;
  197. if ( strpos( $sCurrentFolder, '/' ) !== 0 )
  198. $sCurrentFolder = '/' . $sCurrentFolder ;
  199. // Ensure the folder path has no double-slashes
  200. while ( strpos ($sCurrentFolder, '//') !== false ) {
  201. $sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ;
  202. }
  203. // Check for invalid folder paths (..)
  204. if ( strpos( $sCurrentFolder, '..' ) || strpos( $sCurrentFolder, "\\" ))
  205. SendError( 102, '' ) ;
  206. if ( preg_match(",(/\.)|[[:cntrl:]]|(//)|(\\\\)|([\:\*\?\"\<\>\|]),", $sCurrentFolder))
  207. SendError( 102, '' ) ;
  208. return $sCurrentFolder ;
  209. }
  210. // Do a cleanup of the folder name to avoid possible problems
  211. function SanitizeFolderName( $sNewFolderName )
  212. {
  213. $sNewFolderName = stripslashes( $sNewFolderName ) ;
  214. // Remove . \ / | : ? * " < >
  215. $sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFolderName ) ;
  216. return $sNewFolderName ;
  217. }
  218. // Do a cleanup of the file name to avoid possible problems
  219. function SanitizeFileName( $sNewFileName )
  220. {
  221. global $Config ;
  222. $sNewFileName = stripslashes( $sNewFileName ) ;
  223. // Replace dots in the name with underscores (only one dot can be there... security issue).
  224. if ( $Config['ForceSingleExtension'] )
  225. $sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
  226. // Remove \ / | : ? * " < >
  227. $sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ;
  228. return $sNewFileName ;
  229. }
  230. // This is the function that sends the results of the uploading process.
  231. function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' )
  232. {
  233. // Minified version of the document.domain automatic fix script (#1919).
  234. // The original script can be found at _dev/domain_fix_template.js
  235. echo <<<EOF
  236. <script type="text/javascript">
  237. (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;}}})();
  238. EOF;
  239. if ($errorNumber && $errorNumber != 201) {
  240. $fileUrl = "";
  241. $fileName = "";
  242. }
  243. $rpl = array( '\\' => '\\\\', '"' => '\\"' ) ;
  244. echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ;
  245. echo '</script>' ;
  246. exit ;
  247. }
  248. // This is the function that sends the results of the uploading process to CKEditor.
  249. function SendCKEditorResults ($errorNumber, $CKECallback, $fileUrl, $fileName, $customMsg ='')
  250. {
  251. // Minified version of the document.domain automatic fix script (#1919).
  252. // The original script can be found at _dev/domain_fix_template.js
  253. echo '<<<script type="text/javascript"><!--mce:0--></script>';
  254. }
  255. ?>