PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/js/tiny_mce/plugins/kcfinder/lib/helper_dir.php

https://gitlab.com/ilya.webcity/anna
PHP | 161 lines | 107 code | 23 blank | 31 comment | 39 complexity | 7e3afdb32938a81ef45329c01de00a67 MD5 | raw file
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc Directory helper class
  5. * @package KCFinder
  6. * @version 2.52-dev
  7. * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
  8. * @copyright 2010, 2011 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. /** Recursively delete the given directory. Returns TRUE on success.
  32. * If $firstFailExit parameter is true (default), the method returns the
  33. * path to the first failed file or directory which cannot be deleted.
  34. * If $firstFailExit is false, the method returns an array with failed
  35. * files and directories which cannot be deleted. The third parameter
  36. * $failed is used for internal use only.
  37. * @param string $dir
  38. * @param bool $firstFailExit
  39. * @param array $failed
  40. * @return mixed */
  41. static function prune($dir, $firstFailExit=true, array $failed=null) {
  42. if ($failed === null) $failed = array();
  43. $files = self::content($dir);
  44. if ($files === false) {
  45. if ($firstFailExit)
  46. return $dir;
  47. $failed[] = $dir;
  48. return $failed;
  49. }
  50. foreach ($files as $file) {
  51. if (is_dir($file)) {
  52. $failed_in = self::prune($file, $firstFailExit, $failed);
  53. if ($failed_in !== true) {
  54. if ($firstFailExit)
  55. return $failed_in;
  56. if (is_array($failed_in))
  57. $failed = array_merge($failed, $failed_in);
  58. else
  59. $failed[] = $failed_in;
  60. }
  61. } elseif (!@unlink($file)) {
  62. if ($firstFailExit)
  63. return $file;
  64. $failed[] = $file;
  65. }
  66. }
  67. if (!@rmdir($dir)) {
  68. if ($firstFailExit)
  69. return $dir;
  70. $failed[] = $dir;
  71. }
  72. return count($failed) ? $failed : true;
  73. }
  74. /** Get the content of the given directory. Returns an array with filenames
  75. * or FALSE on failure
  76. * @param string $dir
  77. * @param array $options
  78. * @return mixed */
  79. static function content($dir, array $options=null) {
  80. $defaultOptions = array(
  81. 'types' => "all", // Allowed: "all" or possible return values
  82. // of filetype(), or an array with them
  83. 'addPath' => true, // Whether to add directory path to filenames
  84. 'pattern' => '/./', // Regular expression pattern for filename
  85. 'followLinks' => true
  86. );
  87. if (!is_dir($dir) || !is_readable($dir))
  88. return false;
  89. if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN")
  90. $dir = str_replace("\\", "/", $dir);
  91. $dir = rtrim($dir, "/");
  92. $dh = @opendir($dir);
  93. if ($dh === false)
  94. return false;
  95. if ($options === null)
  96. $options = $defaultOptions;
  97. foreach ($defaultOptions as $key => $val)
  98. if (!isset($options[$key]))
  99. $options[$key] = $val;
  100. $files = array();
  101. while (($file = @readdir($dh)) !== false) {
  102. $type = filetype("$dir/$file");
  103. if ($options['followLinks'] && ($type === "link")) {
  104. $lfile = "$dir/$file";
  105. do {
  106. $ldir = dirname($lfile);
  107. $lfile = @readlink($lfile);
  108. if (substr($lfile, 0, 1) != "/")
  109. $lfile = "$ldir/$lfile";
  110. $type = filetype($lfile);
  111. } while ($type == "link");
  112. }
  113. if ((($type === "dir") && (($file == ".") || ($file == ".."))) ||
  114. !preg_match($options['pattern'], $file)
  115. )
  116. continue;
  117. if (($options['types'] === "all") || ($type === $options['types']) ||
  118. ((is_array($options['types'])) && in_array($type, $options['types']))
  119. )
  120. $files[] = $options['addPath'] ? "$dir/$file" : $file;
  121. }
  122. closedir($dh);
  123. usort($files, array("dir", "fileSort"));
  124. return $files;
  125. }
  126. static function fileSort($a, $b) {
  127. if (function_exists("mb_strtolower")) {
  128. $a = mb_strtolower($a);
  129. $b = mb_strtolower($b);
  130. } else {
  131. $a = strtolower($a);
  132. $b = strtolower($b);
  133. }
  134. if ($a == $b) return 0;
  135. return ($a < $b) ? -1 : 1;
  136. }
  137. }
  138. ?>