PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/fckeditor/editor/filemanager/connectors/php/io.php

https://gitlab.com/a.loskutnikov/sitimobile
PHP | 272 lines | 186 code | 40 blank | 46 comment | 27 complexity | b6bf5c3a0fbb039d4c6b7e4db71f2f81 MD5 | raw file
  1. <?php
  2. /*
  3. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  4. * Copyright (C) 2003-2007 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. $sParent = GetParentFolder( $folderPath ) ;
  81. // Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms
  82. while ( strpos($folderPath, '//') !== false )
  83. {
  84. $folderPath = str_replace( '//', '/', $folderPath ) ;
  85. }
  86. // Check if the parent exists, or create it.
  87. if ( !file_exists( $sParent ) )
  88. {
  89. //prevents agains infinite loop when we can't create root folder
  90. if ( !is_null( $lastFolder ) && $lastFolder === $sParent) {
  91. return "Can't create $folderPath directory" ;
  92. }
  93. $sErrorMsg = CreateServerFolder( $sParent, $folderPath ) ;
  94. if ( $sErrorMsg != '' )
  95. return $sErrorMsg ;
  96. }
  97. if ( !file_exists( $folderPath ) )
  98. {
  99. // Turn off all error reporting.
  100. error_reporting( 0 ) ;
  101. $php_errormsg = '' ;
  102. // Enable error tracking to catch the error.
  103. ini_set( 'track_errors', '1' ) ;
  104. // To create the folder with 0777 permissions, we need to set umask to zero.
  105. $oldumask = umask(0) ;
  106. mkdir( $folderPath, 0777 ) ;
  107. umask( $oldumask ) ;
  108. $sErrorMsg = $php_errormsg ;
  109. // Restore the configurations.
  110. ini_restore( 'track_errors' ) ;
  111. ini_restore( 'error_reporting' ) ;
  112. return $sErrorMsg ;
  113. }
  114. else
  115. return '' ;
  116. }
  117. function GetRootPath()
  118. {
  119. if (!isset($_SERVER)) {
  120. global $_SERVER;
  121. }
  122. $sRealPath = realpath( './' ) ;
  123. $sSelfPath = $_SERVER['PHP_SELF'] ;
  124. $sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;
  125. $sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ;
  126. $position = strpos( $sRealPath, $sSelfPath ) ;
  127. // This can check only that this script isn't run from a virtual dir
  128. // But it avoids the problems that arise if it isn't checked
  129. if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) )
  130. SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ;
  131. return substr( $sRealPath, 0, $position ) ;
  132. }
  133. // Emulate the asp Server.mapPath function.
  134. // given an url path return the physical directory that it corresponds to
  135. function Server_MapPath( $path )
  136. {
  137. // This function is available only for Apache
  138. if ( function_exists( 'apache_lookup_uri' ) )
  139. {
  140. $info = apache_lookup_uri( $path ) ;
  141. return $info->filename . $info->path_info ;
  142. }
  143. // This isn't correct but for the moment there's no other solution
  144. // If this script is under a virtual directory or symlink it will detect the problem and stop
  145. return GetRootPath() . $path ;
  146. }
  147. function IsAllowedExt( $sExtension, $resourceType )
  148. {
  149. global $Config ;
  150. // Get the allowed and denied extensions arrays.
  151. $arAllowed = $Config['AllowedExtensions'][$resourceType] ;
  152. $arDenied = $Config['DeniedExtensions'][$resourceType] ;
  153. if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )
  154. return false ;
  155. if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )
  156. return false ;
  157. return true ;
  158. }
  159. function IsAllowedType( $resourceType )
  160. {
  161. global $Config ;
  162. if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) )
  163. return false ;
  164. return true ;
  165. }
  166. function IsAllowedCommand( $sCommand )
  167. {
  168. global $Config ;
  169. if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) )
  170. return false ;
  171. return true ;
  172. }
  173. function GetCurrentFolder()
  174. {
  175. if (!isset($_GET)) {
  176. global $_GET;
  177. }
  178. $sCurrentFolder = isset( $_GET['CurrentFolder'] ) ? $_GET['CurrentFolder'] : '/' ;
  179. // Check the current folder syntax (must begin and start with a slash).
  180. if ( ! ereg( '/$', $sCurrentFolder ) ) $sCurrentFolder .= '/' ;
  181. if ( strpos( $sCurrentFolder, '/' ) !== 0 ) $sCurrentFolder = '/' . $sCurrentFolder ;
  182. // Ensure the folder path has no double-slashes
  183. while ( strpos ($sCurrentFolder, '//') !== false ) {
  184. $sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ;
  185. }
  186. // Check for invalid folder paths (..)
  187. if ( strpos( $sCurrentFolder, '..' ) )
  188. SendError( 102, '' ) ;
  189. return $sCurrentFolder ;
  190. }
  191. // Do a cleanup of the folder name to avoid possible problems
  192. function SanitizeFolderName( $sNewFolderName )
  193. {
  194. $sNewFolderName = stripslashes( $sNewFolderName ) ;
  195. // Remove . \ / | : ? * " < >
  196. $sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>/', '_', $sNewFolderName ) ;
  197. return $sNewFolderName ;
  198. }
  199. // Do a cleanup of the file name to avoid possible problems
  200. function SanitizeFileName( $sNewFileName )
  201. {
  202. global $Config ;
  203. $sNewFileName = stripslashes( $sNewFileName ) ;
  204. // Replace dots in the name with underscores (only one dot can be there... security issue).
  205. if ( $Config['ForceSingleExtension'] )
  206. $sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
  207. // Remove \ / | : ? * " < >
  208. $sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>/', '_', $sNewFileName ) ;
  209. return $sNewFileName ;
  210. }
  211. // This is the function that sends the results of the uploading process.
  212. function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' )
  213. {
  214. echo '<script type="text/javascript">' ;
  215. $rpl = array( '\\' => '\\\\', '"' => '\\"' ) ;
  216. echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ;
  217. echo '</script>' ;
  218. exit ;
  219. }
  220. ?>