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