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