PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wysiwyg/shared/kcfinder/lib/helper_dir.php

https://github.com/syfisher/limb
PHP | 174 lines | 113 code | 26 blank | 35 comment | 41 complexity | 7f9c69c03847b3db91b0e6e03b963e02 MD5 | raw file
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc Directory helper class
  5. * @package KCFinder
  6. * @version {version}
  7. * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
  8. * @copyright 2010 KCFinder Project
  9. * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
  10. * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. class dir {
  14. /** Checks if the given directory is really writable. The standard PHP
  15. * function is_writable() does not work properly on Windows servers
  16. * @param string $dir
  17. * @return bool */
  18. static function isWritable($dir) {
  19. $dir = path::normalize($dir);
  20. if (!is_dir($dir))
  21. return false;
  22. $i = 0;
  23. do {
  24. $file = "$dir/is_writable_" . md5($i++);
  25. } while (file_exists($file));
  26. if (!@touch($file))
  27. return false;
  28. unlink($file);
  29. return true;
  30. }
  31. /** Reqursively create all directories in given path if they are not exists.
  32. * Returns TRUE on success or false on fail.
  33. * @param string $dir
  34. * @param integer $mode
  35. * @return bool */
  36. static function rmkdir($dir, $mode=0755) {
  37. $dir = dirname(preg_replace('/\/{2,}/s', "/", $dir));
  38. if (!is_dir($dir) && !mkdir($dir, $mode, true))
  39. return false;
  40. return true;
  41. }
  42. /** Recursively delete the given directory. Returns TRUE on success.
  43. * If $firstFailExit parameter is true (default), the method returns the
  44. * path to the first failed file or directory which cannot be deleted.
  45. * If $firstFailExit is false, the method returns an array with failed
  46. * files and directories which cannot be deleted. The third parameter
  47. * $failed is used for internal use only.
  48. * @param string $dir
  49. * @param bool $firstFailExit
  50. * @param array $failed
  51. * @return mixed */
  52. static function prune($dir, $firstFailExit=true, array $failed=null) {
  53. if ($failed === null) $failed = array();
  54. $files = self::content($dir);
  55. if ($files === false) {
  56. if ($firstFailExit)
  57. return $dir;
  58. $failed[] = $dir;
  59. return $failed;
  60. }
  61. foreach ($files as $file) {
  62. if (is_dir($file)) {
  63. $failed_in = self::prune($file, $firstFailExit, $failed);
  64. if ($failed_in !== true) {
  65. if ($firstFailExit)
  66. return $failed_in;
  67. if (is_array($failed_in))
  68. $failed = array_merge($failed, $failed_in);
  69. else
  70. $failed[] = $failed_in;
  71. }
  72. } elseif (!@unlink($file)) {
  73. if ($firstFailExit)
  74. return $file;
  75. $failed[] = $file;
  76. }
  77. }
  78. if (!@rmdir($dir)) {
  79. if ($firstFailExit)
  80. return $dir;
  81. $failed[] = $dir;
  82. }
  83. return count($failed) ? $failed : true;
  84. }
  85. /** Get the content of the given directory. Returns an array with filenames
  86. * or FALSE on failure
  87. * @param string $dir
  88. * @param array $options */
  89. static function content($dir, array $options=null) {
  90. $defaultOptions = array(
  91. 'types' => "all", // Allowed: "all" or possible return values
  92. // of filetype(), or an array with them
  93. 'addPath' => true, // Whether to add directory path to filenames
  94. 'pattern' => '/./', // Regular expression pattern for filename
  95. 'followLinks' => true
  96. );
  97. if (!is_dir($dir) || !is_readable($dir))
  98. return false;
  99. if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN")
  100. $dir = str_replace("\\", "/", $dir);
  101. $dir = rtrim($dir, "/");
  102. $dh = @opendir($dir);
  103. if ($dh === false)
  104. return false;
  105. if ($options === null)
  106. $options = $defaultOptions;
  107. foreach ($defaultOptions as $key => $val)
  108. if (!isset($options[$key]))
  109. $options[$key] = $val;
  110. $files = array();
  111. while (($file = @readdir($dh)) !== false) {
  112. $type = filetype("$dir/$file");
  113. if ($options['followLinks'] && ($type === "link")) {
  114. $lfile = "$dir/$file";
  115. do {
  116. $ldir = dirname($lfile);
  117. $lfile = @readlink($lfile);
  118. if (substr($lfile, 0, 1) != "/")
  119. $lfile = "$ldir/$lfile";
  120. $type = filetype($lfile);
  121. } while ($type == "link");
  122. }
  123. if ((($type === "dir") && (($file == ".") || ($file == ".."))) ||
  124. !preg_match($options['pattern'], $file)
  125. )
  126. continue;
  127. if (($options['types'] === "all") || ($type === $options['types']) ||
  128. ((is_array($options['types'])) && in_array($type, $options['types']))
  129. )
  130. $files[] = $options['addPath'] ? "$dir/$file" : $file;
  131. }
  132. closedir($dh);
  133. usort($files, "dir::fileSort");
  134. return $files;
  135. }
  136. static function fileSort($a, $b) {
  137. if (function_exists("mb_strtolower")) {
  138. $a = mb_strtolower($a);
  139. $b = mb_strtolower($b);
  140. } else {
  141. $a = strtolower($a);
  142. $b = strtolower($b);
  143. }
  144. if ($a == $b) return 0;
  145. return ($a < $b) ? -1 : 1;
  146. }
  147. }
  148. ?>