PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/system/application/views/admin/static/js/ckeditor/ckfinder/ckfinder/core/connector/php/php5/Utils/FileSystem.php

https://github.com/isS/Microweber
PHP | 473 lines | 430 code | 5 blank | 38 comment | 25 complexity | 5e8b49e733185677d7e5deb044c17b67 MD5 | raw file
  1. <?php
  2. /*
  3. * CKFinder
  4. * ========
  5. * http://ckfinder.com
  6. * Copyright (C) 2007-2009, CKSource - Frederico Knabben. All rights reserved.
  7. *
  8. * The software, this file and its contents are subject to the CKFinder
  9. * License. Please read the license.txt file before using, installing, copying,
  10. * modifying or distribute this file or part of its contents. The contents of
  11. * this file is part of the Source Code of CKFinder.
  12. */
  13. /**
  14. * @package CKFinder
  15. * @subpackage Utils
  16. * @copyright CKSource - Frederico Knabben
  17. */
  18. /**
  19. * @package CKFinder
  20. * @subpackage Utils
  21. * @copyright CKSource - Frederico Knabben
  22. */
  23. class CKFinder_Connector_Utils_FileSystem
  24. {
  25. /**
  26. * This function behaves similar to System.IO.Path.Combine in C#, the only diffrenece is that it also accepts null values and treat them as empty string
  27. *
  28. * @static
  29. * @access public
  30. * @param string $path1 first path
  31. * @param string $path2 scecond path
  32. * @return string
  33. */
  34. public static function combinePaths($path1, $path2)
  35. {
  36. if (is_null($path1)) {
  37. $path1 = "";
  38. }
  39. if (is_null($path2)) {
  40. $path2 = "";
  41. }
  42. if (!strlen($path2)) {
  43. if (strlen($path1)) {
  44. $_lastCharP1 = substr($path1, -1, 1);
  45. if ($_lastCharP1 != "/" && $_lastCharP1 != "\\") {
  46. $path1 .= DIRECTORY_SEPARATOR;
  47. }
  48. }
  49. }
  50. else {
  51. $_firstCharP2 = substr($path2, 0, 1);
  52. if (strlen($path1)) {
  53. if (strpos($path2, $path1)===0) {
  54. return $path2;
  55. }
  56. $_lastCharP1 = substr($path1, -1, 1);
  57. if ($_lastCharP1 != "/" && $_lastCharP1 != "\\" && $_firstCharP2 != "/" && $_firstCharP2 != "\\") {
  58. $path1 .= DIRECTORY_SEPARATOR;
  59. }
  60. }
  61. else {
  62. return $path2;
  63. }
  64. }
  65. return $path1 . $path2;
  66. }
  67. /**
  68. * Check whether $fileName is a valid file name, return true on success
  69. *
  70. * @static
  71. * @access public
  72. * @param string $fileName
  73. * @return boolean
  74. */
  75. public static function checkFileName($fileName)
  76. {
  77. if (is_null($fileName) || !strlen($fileName) || substr($fileName,-1,1)=="." || false!==strpos($fileName, "..")) {
  78. return false;
  79. }
  80. if (preg_match(",[[:cntrl:]]|[/\\:\*\?\"\<\>\|],", $fileName)) {
  81. return false;
  82. }
  83. return true;
  84. }
  85. /**
  86. * Unlink file/folder
  87. *
  88. * @static
  89. * @access public
  90. * @param string $path
  91. * @return boolean
  92. */
  93. public static function unlink($path)
  94. {
  95. /* make sure the path exists */
  96. if(!file_exists($path)) {
  97. return false;
  98. }
  99. /* If it is a file or link, just delete it */
  100. if(is_file($path) || is_link($path)) {
  101. return @unlink($path);
  102. }
  103. /* Scan the dir and recursively unlink */
  104. $files = scandir($path);
  105. if ($files) {
  106. foreach($files as $filename)
  107. {
  108. if ($filename == '.' || $filename == '..') {
  109. continue;
  110. }
  111. $file = str_replace('//','/',$path.'/'.$filename);
  112. CKFinder_Connector_Utils_FileSystem::unlink($file);
  113. }
  114. }
  115. /* Remove the parent dir */
  116. if(!@rmdir($path)) {
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Return file name without extension (without dot & last part after dot)
  123. *
  124. * @static
  125. * @access public
  126. * @param string $fileName
  127. * @return string
  128. */
  129. public static function getFileNameWithoutExtension($fileName)
  130. {
  131. $dotPos = strrpos( $fileName, '.' );
  132. if (false === $dotPos) {
  133. return $fileName;
  134. }
  135. return substr($fileName, 0, $dotPos);
  136. }
  137. /**
  138. * Get file extension (only last part - e.g. extension of file.foo.bar.jpg = jpg)
  139. *
  140. * @static
  141. * @access public
  142. * @param string $fileName
  143. * @return string
  144. */
  145. public static function getExtension( $fileName )
  146. {
  147. $dotPos = strrpos( $fileName, '.' );
  148. if (false === $dotPos) {
  149. return "";
  150. }
  151. return substr( $fileName, strrpos( $fileName, '.' ) +1 ) ;
  152. }
  153. /**
  154. * Read file, split it into small chunks and send it to the browser
  155. *
  156. * @static
  157. * @access public
  158. * @param string $filename
  159. * @return boolean
  160. */
  161. public static function readfileChunked($filename)
  162. {
  163. $chunksize = 1024 * 10; // how many bytes per chunk
  164. $handle = fopen($filename, 'rb');
  165. if ($handle === false) {
  166. return false;
  167. }
  168. while (!feof($handle)) {
  169. echo fread($handle, $chunksize);
  170. @ob_flush();
  171. flush();
  172. }
  173. fclose($handle);
  174. return true;
  175. }
  176. /**
  177. * Convert file name from UTF-8 to system encoding
  178. *
  179. * @static
  180. * @access public
  181. * @param string $fileName
  182. * @return string
  183. */
  184. public static function convertToFilesystemEncoding($fileName)
  185. {
  186. $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  187. $encoding = $_config->getFilesystemEncoding();
  188. if (is_null($encoding) || strcasecmp($encoding, "UTF-8") == 0 || strcasecmp($encoding, "UTF8") == 0) {
  189. return $fileName;
  190. }
  191. if (!function_exists("iconv")) {
  192. if (strcasecmp($encoding, "ISO-8859-1") == 0 || strcasecmp($encoding, "ISO8859-1") == 0 || strcasecmp($encoding, "Latin1") == 0) {
  193. return str_replace("\0", "_", utf8_decode($fileName));
  194. } else if (function_exists('mb_convert_encoding')) {
  195. /**
  196. * @todo check whether charset is supported - mb_list_encodings
  197. */
  198. $encoded = @mb_convert_encoding($fileName, $encoding, 'UTF-8');
  199. if (@mb_strlen($fileName, "UTF-8") != @mb_strlen($encoded, $encoding)) {
  200. return str_replace("\0", "_", preg_replace("/[^[:ascii:]]/u","_",$fileName));
  201. }
  202. else {
  203. return str_replace("\0", "_", $encoded);
  204. }
  205. } else {
  206. return str_replace("\0", "_", preg_replace("/[^[:ascii:]]/u","_",$fileName));
  207. }
  208. }
  209. $converted = @iconv("UTF-8", $encoding . "//IGNORE//TRANSLIT", $fileName);
  210. if ($converted === false) {
  211. return str_replace("\0", "_", preg_replace("/[^[:ascii:]]/u","_",$fileName));
  212. }
  213. return $converted;
  214. }
  215. /**
  216. * Convert file name from system encoding into UTF-8
  217. *
  218. * @static
  219. * @access public
  220. * @param string $fileName
  221. * @return string
  222. */
  223. public static function convertToConnectorEncoding($fileName)
  224. {
  225. $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  226. $encoding = $_config->getFilesystemEncoding();
  227. if (is_null($encoding) || strcasecmp($encoding, "UTF-8") == 0 || strcasecmp($encoding, "UTF8") == 0) {
  228. return $fileName;
  229. }
  230. if (!function_exists("iconv")) {
  231. if (strcasecmp($encoding, "ISO-8859-1") == 0 || strcasecmp($encoding, "ISO8859-1") == 0 || strcasecmp($encoding, "Latin1") == 0) {
  232. return utf8_encode($fileName);
  233. } else {
  234. return $fileName;
  235. }
  236. }
  237. $converted = @iconv($encoding, "UTF-8", $fileName);
  238. if ($converted === false) {
  239. return $fileName;
  240. }
  241. return $converted;
  242. }
  243. /**
  244. * Find document root
  245. *
  246. * @return string
  247. * @access public
  248. */
  249. public function getDocumentRootPath()
  250. {
  251. /**
  252. * The absolute pathname of the currently executing script.
  253. * Notatka: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php,
  254. * $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
  255. */
  256. if (isset($_SERVER['SCRIPT_FILENAME'])) {
  257. $sRealPath = dirname($_SERVER['SCRIPT_FILENAME']);
  258. }
  259. else {
  260. /**
  261. * realpath — Returns canonicalized absolute pathname
  262. */
  263. $sRealPath = realpath( './' ) ;
  264. }
  265. /**
  266. * The filename of the currently executing script, relative to the document root.
  267. * For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar
  268. * would be /test.php/foo.bar.
  269. */
  270. $sSelfPath = dirname($_SERVER['PHP_SELF']);
  271. return substr($sRealPath, 0, strlen($sRealPath) - strlen($sSelfPath));
  272. }
  273. /**
  274. * Create directory recursively
  275. *
  276. * @access public
  277. * @static
  278. * @param string $dir
  279. * @return boolean
  280. */
  281. public static function createDirectoryRecursively($dir)
  282. {
  283. if (DIRECTORY_SEPARATOR === "\\") {
  284. $dir = str_replace("/", "\\", $dir);
  285. }
  286. else if (DIRECTORY_SEPARATOR === "/") {
  287. $dir = str_replace("\\", "/", $dir);
  288. }
  289. $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  290. if ($perms = $_config->getChmodFolders()) {
  291. $oldUmask = umask(0);
  292. $bCreated = @mkdir($dir, $perms, true);
  293. umask($oldUmask);
  294. }
  295. else {
  296. $bCreated = @mkdir($dir, 0777, true);
  297. }
  298. return $bCreated;
  299. }
  300. /**
  301. * Detect HTML in the first KB to prevent against potential security issue with
  302. * IE/Safari/Opera file type auto detection bug.
  303. * Returns true if file contain insecure HTML code at the beginning.
  304. *
  305. * @static
  306. * @access public
  307. * @param string $filePath absolute path to file
  308. * @return boolean
  309. */
  310. public static function detectHtml($filePath)
  311. {
  312. $fp = @fopen($filePath, 'rb');
  313. if ( $fp === false || !flock( $fp, LOCK_SH ) ) {
  314. return -1 ;
  315. }
  316. $chunk = fread($fp, 1024);
  317. flock( $fp, LOCK_UN ) ;
  318. fclose($fp);
  319. $chunk = strtolower($chunk);
  320. if (!$chunk) {
  321. return false;
  322. }
  323. $chunk = trim($chunk);
  324. if (preg_match("/<!DOCTYPE\W*X?HTML/sim", $chunk)) {
  325. return true;
  326. }
  327. $tags = array('<body', '<head', '<html', '<img', '<pre', '<script', '<table', '<title');
  328. foreach( $tags as $tag ) {
  329. if(false !== strpos($chunk, $tag)) {
  330. return true ;
  331. }
  332. }
  333. //type = javascript
  334. if (preg_match('!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk)) {
  335. return true ;
  336. }
  337. //href = javascript
  338. //src = javascript
  339. //data = javascript
  340. if (preg_match('!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim',$chunk)) {
  341. return true ;
  342. }
  343. //url(javascript
  344. if (preg_match('!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk)) {
  345. return true ;
  346. }
  347. return false ;
  348. }
  349. /**
  350. * Check file content.
  351. * Currently this function validates only image files.
  352. * Returns false if file is invalid.
  353. *
  354. * @static
  355. * @access public
  356. * @param string $filePath absolute path to file
  357. * @param string $extension file extension
  358. * @param integer $detectionLevel 0 = none, 1 = use getimagesize for images, 2 = use DetectHtml for images
  359. * @return boolean
  360. */
  361. public static function isImageValid($filePath, $extension)
  362. {
  363. if (!@is_readable($filePath)) {
  364. return -1;
  365. }
  366. $imageCheckExtensions = array('gif', 'jpeg', 'jpg', 'png', 'psd', 'bmp', 'tiff');
  367. // version_compare is available since PHP4 >= 4.0.7
  368. if ( function_exists( 'version_compare' ) ) {
  369. $sCurrentVersion = phpversion();
  370. if ( version_compare( $sCurrentVersion, "4.2.0" ) >= 0 ) {
  371. $imageCheckExtensions[] = "tiff";
  372. $imageCheckExtensions[] = "tif";
  373. }
  374. if ( version_compare( $sCurrentVersion, "4.3.0" ) >= 0 ) {
  375. $imageCheckExtensions[] = "swc";
  376. }
  377. if ( version_compare( $sCurrentVersion, "4.3.2" ) >= 0 ) {
  378. $imageCheckExtensions[] = "jpc";
  379. $imageCheckExtensions[] = "jp2";
  380. $imageCheckExtensions[] = "jpx";
  381. $imageCheckExtensions[] = "jb2";
  382. $imageCheckExtensions[] = "xbm";
  383. $imageCheckExtensions[] = "wbmp";
  384. }
  385. }
  386. if ( !in_array( $extension, $imageCheckExtensions ) ) {
  387. return true;
  388. }
  389. if ( @getimagesize( $filePath ) === false ) {
  390. return false ;
  391. }
  392. return true;
  393. }
  394. /**
  395. * Returns true if directory is not empty
  396. *
  397. * @access public
  398. * @static
  399. * @param string $serverPath
  400. * @return boolean
  401. */
  402. public static function hasChildren($serverPath)
  403. {
  404. if (!is_dir($serverPath) || (false === $fh = @opendir($serverPath))) {
  405. return false;
  406. }
  407. $hasChildren = false;
  408. while (false !== ($filename = readdir($fh))) {
  409. if ($filename == '.' || $filename == '..') {
  410. continue;
  411. } else if (is_dir($serverPath . DIRECTORY_SEPARATOR . $filename)) {
  412. //we have found valid directory
  413. $hasChildren = true;
  414. break;
  415. }
  416. }
  417. closedir($fh);
  418. return $hasChildren;
  419. }
  420. }