Chris@18
|
1 <?php
|
Chris@18
|
2 /**
|
Chris@18
|
3 * File/Directory manipulation
|
Chris@18
|
4 *
|
Chris@18
|
5 * PHP versions 4 and 5
|
Chris@18
|
6 *
|
Chris@18
|
7 * @category pear
|
Chris@18
|
8 * @package System
|
Chris@18
|
9 * @author Tomas V.V.Cox <cox@idecnet.com>
|
Chris@18
|
10 * @copyright 1997-2009 The Authors
|
Chris@18
|
11 * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
Chris@18
|
12 * @link http://pear.php.net/package/PEAR
|
Chris@18
|
13 * @since File available since Release 0.1
|
Chris@18
|
14 */
|
Chris@18
|
15
|
Chris@18
|
16 /**
|
Chris@18
|
17 * base class
|
Chris@18
|
18 */
|
Chris@18
|
19 require_once 'PEAR.php';
|
Chris@18
|
20 require_once 'Console/Getopt.php';
|
Chris@18
|
21
|
Chris@18
|
22 $GLOBALS['_System_temp_files'] = array();
|
Chris@18
|
23
|
Chris@18
|
24 /**
|
Chris@18
|
25 * System offers cross platform compatible system functions
|
Chris@18
|
26 *
|
Chris@18
|
27 * Static functions for different operations. Should work under
|
Chris@18
|
28 * Unix and Windows. The names and usage has been taken from its respectively
|
Chris@18
|
29 * GNU commands. The functions will return (bool) false on error and will
|
Chris@18
|
30 * trigger the error with the PHP trigger_error() function (you can silence
|
Chris@18
|
31 * the error by prefixing a '@' sign after the function call, but this
|
Chris@18
|
32 * is not recommended practice. Instead use an error handler with
|
Chris@18
|
33 * {@link set_error_handler()}).
|
Chris@18
|
34 *
|
Chris@18
|
35 * Documentation on this class you can find in:
|
Chris@18
|
36 * http://pear.php.net/manual/
|
Chris@18
|
37 *
|
Chris@18
|
38 * Example usage:
|
Chris@18
|
39 * if (!@System::rm('-r file1 dir1')) {
|
Chris@18
|
40 * print "could not delete file1 or dir1";
|
Chris@18
|
41 * }
|
Chris@18
|
42 *
|
Chris@18
|
43 * In case you need to to pass file names with spaces,
|
Chris@18
|
44 * pass the params as an array:
|
Chris@18
|
45 *
|
Chris@18
|
46 * System::rm(array('-r', $file1, $dir1));
|
Chris@18
|
47 *
|
Chris@18
|
48 * @category pear
|
Chris@18
|
49 * @package System
|
Chris@18
|
50 * @author Tomas V.V. Cox <cox@idecnet.com>
|
Chris@18
|
51 * @copyright 1997-2006 The PHP Group
|
Chris@18
|
52 * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
Chris@18
|
53 * @version Release: @package_version@
|
Chris@18
|
54 * @link http://pear.php.net/package/PEAR
|
Chris@18
|
55 * @since Class available since Release 0.1
|
Chris@18
|
56 * @static
|
Chris@18
|
57 */
|
Chris@18
|
58 class System
|
Chris@18
|
59 {
|
Chris@18
|
60 /**
|
Chris@18
|
61 * returns the commandline arguments of a function
|
Chris@18
|
62 *
|
Chris@18
|
63 * @param string $argv the commandline
|
Chris@18
|
64 * @param string $short_options the allowed option short-tags
|
Chris@18
|
65 * @param string $long_options the allowed option long-tags
|
Chris@18
|
66 * @return array the given options and there values
|
Chris@18
|
67 */
|
Chris@18
|
68 public static function _parseArgs($argv, $short_options, $long_options = null)
|
Chris@18
|
69 {
|
Chris@18
|
70 if (!is_array($argv) && $argv !== null) {
|
Chris@18
|
71 /*
|
Chris@18
|
72 // Quote all items that are a short option
|
Chris@18
|
73 $av = preg_split('/(\A| )--?[a-z0-9]+[ =]?((?<!\\\\)((,\s*)|((?<!,)\s+))?)/i', $argv, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
|
Chris@18
|
74 $offset = 0;
|
Chris@18
|
75 foreach ($av as $a) {
|
Chris@18
|
76 $b = trim($a[0]);
|
Chris@18
|
77 if ($b{0} == '"' || $b{0} == "'") {
|
Chris@18
|
78 continue;
|
Chris@18
|
79 }
|
Chris@18
|
80
|
Chris@18
|
81 $escape = escapeshellarg($b);
|
Chris@18
|
82 $pos = $a[1] + $offset;
|
Chris@18
|
83 $argv = substr_replace($argv, $escape, $pos, strlen($b));
|
Chris@18
|
84 $offset += 2;
|
Chris@18
|
85 }
|
Chris@18
|
86 */
|
Chris@18
|
87
|
Chris@18
|
88 // Find all items, quoted or otherwise
|
Chris@18
|
89 preg_match_all("/(?:[\"'])(.*?)(?:['\"])|([^\s]+)/", $argv, $av);
|
Chris@18
|
90 $argv = $av[1];
|
Chris@18
|
91 foreach ($av[2] as $k => $a) {
|
Chris@18
|
92 if (empty($a)) {
|
Chris@18
|
93 continue;
|
Chris@18
|
94 }
|
Chris@18
|
95 $argv[$k] = trim($a) ;
|
Chris@18
|
96 }
|
Chris@18
|
97 }
|
Chris@18
|
98
|
Chris@18
|
99 return Console_Getopt::getopt2($argv, $short_options, $long_options);
|
Chris@18
|
100 }
|
Chris@18
|
101
|
Chris@18
|
102 /**
|
Chris@18
|
103 * Output errors with PHP trigger_error(). You can silence the errors
|
Chris@18
|
104 * with prefixing a "@" sign to the function call: @System::mkdir(..);
|
Chris@18
|
105 *
|
Chris@18
|
106 * @param mixed $error a PEAR error or a string with the error message
|
Chris@18
|
107 * @return bool false
|
Chris@18
|
108 */
|
Chris@18
|
109 protected static function raiseError($error)
|
Chris@18
|
110 {
|
Chris@18
|
111 if (PEAR::isError($error)) {
|
Chris@18
|
112 $error = $error->getMessage();
|
Chris@18
|
113 }
|
Chris@18
|
114 trigger_error($error, E_USER_WARNING);
|
Chris@18
|
115 return false;
|
Chris@18
|
116 }
|
Chris@18
|
117
|
Chris@18
|
118 /**
|
Chris@18
|
119 * Creates a nested array representing the structure of a directory
|
Chris@18
|
120 *
|
Chris@18
|
121 * System::_dirToStruct('dir1', 0) =>
|
Chris@18
|
122 * Array
|
Chris@18
|
123 * (
|
Chris@18
|
124 * [dirs] => Array
|
Chris@18
|
125 * (
|
Chris@18
|
126 * [0] => dir1
|
Chris@18
|
127 * )
|
Chris@18
|
128 *
|
Chris@18
|
129 * [files] => Array
|
Chris@18
|
130 * (
|
Chris@18
|
131 * [0] => dir1/file2
|
Chris@18
|
132 * [1] => dir1/file3
|
Chris@18
|
133 * )
|
Chris@18
|
134 * )
|
Chris@18
|
135 * @param string $sPath Name of the directory
|
Chris@18
|
136 * @param integer $maxinst max. deep of the lookup
|
Chris@18
|
137 * @param integer $aktinst starting deep of the lookup
|
Chris@18
|
138 * @param bool $silent if true, do not emit errors.
|
Chris@18
|
139 * @return array the structure of the dir
|
Chris@18
|
140 */
|
Chris@18
|
141 protected static function _dirToStruct($sPath, $maxinst, $aktinst = 0, $silent = false)
|
Chris@18
|
142 {
|
Chris@18
|
143 $struct = array('dirs' => array(), 'files' => array());
|
Chris@18
|
144 if (($dir = @opendir($sPath)) === false) {
|
Chris@18
|
145 if (!$silent) {
|
Chris@18
|
146 System::raiseError("Could not open dir $sPath");
|
Chris@18
|
147 }
|
Chris@18
|
148 return $struct; // XXX could not open error
|
Chris@18
|
149 }
|
Chris@18
|
150
|
Chris@18
|
151 $struct['dirs'][] = $sPath = realpath($sPath); // XXX don't add if '.' or '..' ?
|
Chris@18
|
152 $list = array();
|
Chris@18
|
153 while (false !== ($file = readdir($dir))) {
|
Chris@18
|
154 if ($file != '.' && $file != '..') {
|
Chris@18
|
155 $list[] = $file;
|
Chris@18
|
156 }
|
Chris@18
|
157 }
|
Chris@18
|
158
|
Chris@18
|
159 closedir($dir);
|
Chris@18
|
160 natsort($list);
|
Chris@18
|
161 if ($aktinst < $maxinst || $maxinst == 0) {
|
Chris@18
|
162 foreach ($list as $val) {
|
Chris@18
|
163 $path = $sPath . DIRECTORY_SEPARATOR . $val;
|
Chris@18
|
164 if (is_dir($path) && !is_link($path)) {
|
Chris@18
|
165 $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1, $silent);
|
Chris@18
|
166 $struct = array_merge_recursive($struct, $tmp);
|
Chris@18
|
167 } else {
|
Chris@18
|
168 $struct['files'][] = $path;
|
Chris@18
|
169 }
|
Chris@18
|
170 }
|
Chris@18
|
171 }
|
Chris@18
|
172
|
Chris@18
|
173 return $struct;
|
Chris@18
|
174 }
|
Chris@18
|
175
|
Chris@18
|
176 /**
|
Chris@18
|
177 * Creates a nested array representing the structure of a directory and files
|
Chris@18
|
178 *
|
Chris@18
|
179 * @param array $files Array listing files and dirs
|
Chris@18
|
180 * @return array
|
Chris@18
|
181 * @static
|
Chris@18
|
182 * @see System::_dirToStruct()
|
Chris@18
|
183 */
|
Chris@18
|
184 protected static function _multipleToStruct($files)
|
Chris@18
|
185 {
|
Chris@18
|
186 $struct = array('dirs' => array(), 'files' => array());
|
Chris@18
|
187 settype($files, 'array');
|
Chris@18
|
188 foreach ($files as $file) {
|
Chris@18
|
189 if (is_dir($file) && !is_link($file)) {
|
Chris@18
|
190 $tmp = System::_dirToStruct($file, 0);
|
Chris@18
|
191 $struct = array_merge_recursive($tmp, $struct);
|
Chris@18
|
192 } else {
|
Chris@18
|
193 if (!in_array($file, $struct['files'])) {
|
Chris@18
|
194 $struct['files'][] = $file;
|
Chris@18
|
195 }
|
Chris@18
|
196 }
|
Chris@18
|
197 }
|
Chris@18
|
198 return $struct;
|
Chris@18
|
199 }
|
Chris@18
|
200
|
Chris@18
|
201 /**
|
Chris@18
|
202 * The rm command for removing files.
|
Chris@18
|
203 * Supports multiple files and dirs and also recursive deletes
|
Chris@18
|
204 *
|
Chris@18
|
205 * @param string $args the arguments for rm
|
Chris@18
|
206 * @return mixed PEAR_Error or true for success
|
Chris@18
|
207 * @static
|
Chris@18
|
208 * @access public
|
Chris@18
|
209 */
|
Chris@18
|
210 public static function rm($args)
|
Chris@18
|
211 {
|
Chris@18
|
212 $opts = System::_parseArgs($args, 'rf'); // "f" does nothing but I like it :-)
|
Chris@18
|
213 if (PEAR::isError($opts)) {
|
Chris@18
|
214 return System::raiseError($opts);
|
Chris@18
|
215 }
|
Chris@18
|
216 foreach ($opts[0] as $opt) {
|
Chris@18
|
217 if ($opt[0] == 'r') {
|
Chris@18
|
218 $do_recursive = true;
|
Chris@18
|
219 }
|
Chris@18
|
220 }
|
Chris@18
|
221 $ret = true;
|
Chris@18
|
222 if (isset($do_recursive)) {
|
Chris@18
|
223 $struct = System::_multipleToStruct($opts[1]);
|
Chris@18
|
224 foreach ($struct['files'] as $file) {
|
Chris@18
|
225 if (!@unlink($file)) {
|
Chris@18
|
226 $ret = false;
|
Chris@18
|
227 }
|
Chris@18
|
228 }
|
Chris@18
|
229
|
Chris@18
|
230 rsort($struct['dirs']);
|
Chris@18
|
231 foreach ($struct['dirs'] as $dir) {
|
Chris@18
|
232 if (!@rmdir($dir)) {
|
Chris@18
|
233 $ret = false;
|
Chris@18
|
234 }
|
Chris@18
|
235 }
|
Chris@18
|
236 } else {
|
Chris@18
|
237 foreach ($opts[1] as $file) {
|
Chris@18
|
238 $delete = (is_dir($file)) ? 'rmdir' : 'unlink';
|
Chris@18
|
239 if (!@$delete($file)) {
|
Chris@18
|
240 $ret = false;
|
Chris@18
|
241 }
|
Chris@18
|
242 }
|
Chris@18
|
243 }
|
Chris@18
|
244 return $ret;
|
Chris@18
|
245 }
|
Chris@18
|
246
|
Chris@18
|
247 /**
|
Chris@18
|
248 * Make directories.
|
Chris@18
|
249 *
|
Chris@18
|
250 * The -p option will create parent directories
|
Chris@18
|
251 * @param string $args the name of the director(y|ies) to create
|
Chris@18
|
252 * @return bool True for success
|
Chris@18
|
253 */
|
Chris@18
|
254 public static function mkDir($args)
|
Chris@18
|
255 {
|
Chris@18
|
256 $opts = System::_parseArgs($args, 'pm:');
|
Chris@18
|
257 if (PEAR::isError($opts)) {
|
Chris@18
|
258 return System::raiseError($opts);
|
Chris@18
|
259 }
|
Chris@18
|
260
|
Chris@18
|
261 $mode = 0777; // default mode
|
Chris@18
|
262 foreach ($opts[0] as $opt) {
|
Chris@18
|
263 if ($opt[0] == 'p') {
|
Chris@18
|
264 $create_parents = true;
|
Chris@18
|
265 } elseif ($opt[0] == 'm') {
|
Chris@18
|
266 // if the mode is clearly an octal number (starts with 0)
|
Chris@18
|
267 // convert it to decimal
|
Chris@18
|
268 if (strlen($opt[1]) && $opt[1]{0} == '0') {
|
Chris@18
|
269 $opt[1] = octdec($opt[1]);
|
Chris@18
|
270 } else {
|
Chris@18
|
271 // convert to int
|
Chris@18
|
272 $opt[1] += 0;
|
Chris@18
|
273 }
|
Chris@18
|
274 $mode = $opt[1];
|
Chris@18
|
275 }
|
Chris@18
|
276 }
|
Chris@18
|
277
|
Chris@18
|
278 $ret = true;
|
Chris@18
|
279 if (isset($create_parents)) {
|
Chris@18
|
280 foreach ($opts[1] as $dir) {
|
Chris@18
|
281 $dirstack = array();
|
Chris@18
|
282 while ((!file_exists($dir) || !is_dir($dir)) &&
|
Chris@18
|
283 $dir != DIRECTORY_SEPARATOR) {
|
Chris@18
|
284 array_unshift($dirstack, $dir);
|
Chris@18
|
285 $dir = dirname($dir);
|
Chris@18
|
286 }
|
Chris@18
|
287
|
Chris@18
|
288 while ($newdir = array_shift($dirstack)) {
|
Chris@18
|
289 if (!is_writeable(dirname($newdir))) {
|
Chris@18
|
290 $ret = false;
|
Chris@18
|
291 break;
|
Chris@18
|
292 }
|
Chris@18
|
293
|
Chris@18
|
294 if (!mkdir($newdir, $mode)) {
|
Chris@18
|
295 $ret = false;
|
Chris@18
|
296 }
|
Chris@18
|
297 }
|
Chris@18
|
298 }
|
Chris@18
|
299 } else {
|
Chris@18
|
300 foreach($opts[1] as $dir) {
|
Chris@18
|
301 if ((@file_exists($dir) || !is_dir($dir)) && !mkdir($dir, $mode)) {
|
Chris@18
|
302 $ret = false;
|
Chris@18
|
303 }
|
Chris@18
|
304 }
|
Chris@18
|
305 }
|
Chris@18
|
306
|
Chris@18
|
307 return $ret;
|
Chris@18
|
308 }
|
Chris@18
|
309
|
Chris@18
|
310 /**
|
Chris@18
|
311 * Concatenate files
|
Chris@18
|
312 *
|
Chris@18
|
313 * Usage:
|
Chris@18
|
314 * 1) $var = System::cat('sample.txt test.txt');
|
Chris@18
|
315 * 2) System::cat('sample.txt test.txt > final.txt');
|
Chris@18
|
316 * 3) System::cat('sample.txt test.txt >> final.txt');
|
Chris@18
|
317 *
|
Chris@18
|
318 * Note: as the class use fopen, urls should work also
|
Chris@18
|
319 *
|
Chris@18
|
320 * @param string $args the arguments
|
Chris@18
|
321 * @return boolean true on success
|
Chris@18
|
322 */
|
Chris@18
|
323 public static function &cat($args)
|
Chris@18
|
324 {
|
Chris@18
|
325 $ret = null;
|
Chris@18
|
326 $files = array();
|
Chris@18
|
327 if (!is_array($args)) {
|
Chris@18
|
328 $args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
|
Chris@18
|
329 }
|
Chris@18
|
330
|
Chris@18
|
331 $count_args = count($args);
|
Chris@18
|
332 for ($i = 0; $i < $count_args; $i++) {
|
Chris@18
|
333 if ($args[$i] == '>') {
|
Chris@18
|
334 $mode = 'wb';
|
Chris@18
|
335 $outputfile = $args[$i+1];
|
Chris@18
|
336 break;
|
Chris@18
|
337 } elseif ($args[$i] == '>>') {
|
Chris@18
|
338 $mode = 'ab+';
|
Chris@18
|
339 $outputfile = $args[$i+1];
|
Chris@18
|
340 break;
|
Chris@18
|
341 } else {
|
Chris@18
|
342 $files[] = $args[$i];
|
Chris@18
|
343 }
|
Chris@18
|
344 }
|
Chris@18
|
345 $outputfd = false;
|
Chris@18
|
346 if (isset($mode)) {
|
Chris@18
|
347 if (!$outputfd = fopen($outputfile, $mode)) {
|
Chris@18
|
348 $err = System::raiseError("Could not open $outputfile");
|
Chris@18
|
349 return $err;
|
Chris@18
|
350 }
|
Chris@18
|
351 $ret = true;
|
Chris@18
|
352 }
|
Chris@18
|
353 foreach ($files as $file) {
|
Chris@18
|
354 if (!$fd = fopen($file, 'r')) {
|
Chris@18
|
355 System::raiseError("Could not open $file");
|
Chris@18
|
356 continue;
|
Chris@18
|
357 }
|
Chris@18
|
358 while ($cont = fread($fd, 2048)) {
|
Chris@18
|
359 if (is_resource($outputfd)) {
|
Chris@18
|
360 fwrite($outputfd, $cont);
|
Chris@18
|
361 } else {
|
Chris@18
|
362 $ret .= $cont;
|
Chris@18
|
363 }
|
Chris@18
|
364 }
|
Chris@18
|
365 fclose($fd);
|
Chris@18
|
366 }
|
Chris@18
|
367 if (is_resource($outputfd)) {
|
Chris@18
|
368 fclose($outputfd);
|
Chris@18
|
369 }
|
Chris@18
|
370 return $ret;
|
Chris@18
|
371 }
|
Chris@18
|
372
|
Chris@18
|
373 /**
|
Chris@18
|
374 * Creates temporary files or directories. This function will remove
|
Chris@18
|
375 * the created files when the scripts finish its execution.
|
Chris@18
|
376 *
|
Chris@18
|
377 * Usage:
|
Chris@18
|
378 * 1) $tempfile = System::mktemp("prefix");
|
Chris@18
|
379 * 2) $tempdir = System::mktemp("-d prefix");
|
Chris@18
|
380 * 3) $tempfile = System::mktemp();
|
Chris@18
|
381 * 4) $tempfile = System::mktemp("-t /var/tmp prefix");
|
Chris@18
|
382 *
|
Chris@18
|
383 * prefix -> The string that will be prepended to the temp name
|
Chris@18
|
384 * (defaults to "tmp").
|
Chris@18
|
385 * -d -> A temporary dir will be created instead of a file.
|
Chris@18
|
386 * -t -> The target dir where the temporary (file|dir) will be created. If
|
Chris@18
|
387 * this param is missing by default the env vars TMP on Windows or
|
Chris@18
|
388 * TMPDIR in Unix will be used. If these vars are also missing
|
Chris@18
|
389 * c:\windows\temp or /tmp will be used.
|
Chris@18
|
390 *
|
Chris@18
|
391 * @param string $args The arguments
|
Chris@18
|
392 * @return mixed the full path of the created (file|dir) or false
|
Chris@18
|
393 * @see System::tmpdir()
|
Chris@18
|
394 */
|
Chris@18
|
395 public static function mktemp($args = null)
|
Chris@18
|
396 {
|
Chris@18
|
397 static $first_time = true;
|
Chris@18
|
398 $opts = System::_parseArgs($args, 't:d');
|
Chris@18
|
399 if (PEAR::isError($opts)) {
|
Chris@18
|
400 return System::raiseError($opts);
|
Chris@18
|
401 }
|
Chris@18
|
402
|
Chris@18
|
403 foreach ($opts[0] as $opt) {
|
Chris@18
|
404 if ($opt[0] == 'd') {
|
Chris@18
|
405 $tmp_is_dir = true;
|
Chris@18
|
406 } elseif ($opt[0] == 't') {
|
Chris@18
|
407 $tmpdir = $opt[1];
|
Chris@18
|
408 }
|
Chris@18
|
409 }
|
Chris@18
|
410
|
Chris@18
|
411 $prefix = (isset($opts[1][0])) ? $opts[1][0] : 'tmp';
|
Chris@18
|
412 if (!isset($tmpdir)) {
|
Chris@18
|
413 $tmpdir = System::tmpdir();
|
Chris@18
|
414 }
|
Chris@18
|
415
|
Chris@18
|
416 if (!System::mkDir(array('-p', $tmpdir))) {
|
Chris@18
|
417 return false;
|
Chris@18
|
418 }
|
Chris@18
|
419
|
Chris@18
|
420 $tmp = tempnam($tmpdir, $prefix);
|
Chris@18
|
421 if (isset($tmp_is_dir)) {
|
Chris@18
|
422 unlink($tmp); // be careful possible race condition here
|
Chris@18
|
423 if (!mkdir($tmp, 0700)) {
|
Chris@18
|
424 return System::raiseError("Unable to create temporary directory $tmpdir");
|
Chris@18
|
425 }
|
Chris@18
|
426 }
|
Chris@18
|
427
|
Chris@18
|
428 $GLOBALS['_System_temp_files'][] = $tmp;
|
Chris@18
|
429 if (isset($tmp_is_dir)) {
|
Chris@18
|
430 //$GLOBALS['_System_temp_files'][] = dirname($tmp);
|
Chris@18
|
431 }
|
Chris@18
|
432
|
Chris@18
|
433 if ($first_time) {
|
Chris@18
|
434 PEAR::registerShutdownFunc(array('System', '_removeTmpFiles'));
|
Chris@18
|
435 $first_time = false;
|
Chris@18
|
436 }
|
Chris@18
|
437
|
Chris@18
|
438 return $tmp;
|
Chris@18
|
439 }
|
Chris@18
|
440
|
Chris@18
|
441 /**
|
Chris@18
|
442 * Remove temporary files created my mkTemp. This function is executed
|
Chris@18
|
443 * at script shutdown time
|
Chris@18
|
444 */
|
Chris@18
|
445 public static function _removeTmpFiles()
|
Chris@18
|
446 {
|
Chris@18
|
447 if (count($GLOBALS['_System_temp_files'])) {
|
Chris@18
|
448 $delete = $GLOBALS['_System_temp_files'];
|
Chris@18
|
449 array_unshift($delete, '-r');
|
Chris@18
|
450 System::rm($delete);
|
Chris@18
|
451 $GLOBALS['_System_temp_files'] = array();
|
Chris@18
|
452 }
|
Chris@18
|
453 }
|
Chris@18
|
454
|
Chris@18
|
455 /**
|
Chris@18
|
456 * Get the path of the temporal directory set in the system
|
Chris@18
|
457 * by looking in its environments variables.
|
Chris@18
|
458 * Note: php.ini-recommended removes the "E" from the variables_order setting,
|
Chris@18
|
459 * making unavaible the $_ENV array, that s why we do tests with _ENV
|
Chris@18
|
460 *
|
Chris@18
|
461 * @return string The temporary directory on the system
|
Chris@18
|
462 */
|
Chris@18
|
463 public static function tmpdir()
|
Chris@18
|
464 {
|
Chris@18
|
465 if (OS_WINDOWS) {
|
Chris@18
|
466 if ($var = isset($_ENV['TMP']) ? $_ENV['TMP'] : getenv('TMP')) {
|
Chris@18
|
467 return $var;
|
Chris@18
|
468 }
|
Chris@18
|
469 if ($var = isset($_ENV['TEMP']) ? $_ENV['TEMP'] : getenv('TEMP')) {
|
Chris@18
|
470 return $var;
|
Chris@18
|
471 }
|
Chris@18
|
472 if ($var = isset($_ENV['USERPROFILE']) ? $_ENV['USERPROFILE'] : getenv('USERPROFILE')) {
|
Chris@18
|
473 return $var;
|
Chris@18
|
474 }
|
Chris@18
|
475 if ($var = isset($_ENV['windir']) ? $_ENV['windir'] : getenv('windir')) {
|
Chris@18
|
476 return $var;
|
Chris@18
|
477 }
|
Chris@18
|
478 return getenv('SystemRoot') . '\temp';
|
Chris@18
|
479 }
|
Chris@18
|
480 if ($var = isset($_ENV['TMPDIR']) ? $_ENV['TMPDIR'] : getenv('TMPDIR')) {
|
Chris@18
|
481 return $var;
|
Chris@18
|
482 }
|
Chris@18
|
483 return realpath('/tmp');
|
Chris@18
|
484 }
|
Chris@18
|
485
|
Chris@18
|
486 /**
|
Chris@18
|
487 * The "which" command (show the full path of a command)
|
Chris@18
|
488 *
|
Chris@18
|
489 * @param string $program The command to search for
|
Chris@18
|
490 * @param mixed $fallback Value to return if $program is not found
|
Chris@18
|
491 *
|
Chris@18
|
492 * @return mixed A string with the full path or false if not found
|
Chris@18
|
493 * @author Stig Bakken <ssb@php.net>
|
Chris@18
|
494 */
|
Chris@18
|
495 public static function which($program, $fallback = false)
|
Chris@18
|
496 {
|
Chris@18
|
497 // enforce API
|
Chris@18
|
498 if (!is_string($program) || '' == $program) {
|
Chris@18
|
499 return $fallback;
|
Chris@18
|
500 }
|
Chris@18
|
501
|
Chris@18
|
502 // full path given
|
Chris@18
|
503 if (basename($program) != $program) {
|
Chris@18
|
504 $path_elements[] = dirname($program);
|
Chris@18
|
505 $program = basename($program);
|
Chris@18
|
506 } else {
|
Chris@18
|
507 $path = getenv('PATH');
|
Chris@18
|
508 if (!$path) {
|
Chris@18
|
509 $path = getenv('Path'); // some OSes are just stupid enough to do this
|
Chris@18
|
510 }
|
Chris@18
|
511
|
Chris@18
|
512 $path_elements = explode(PATH_SEPARATOR, $path);
|
Chris@18
|
513 }
|
Chris@18
|
514
|
Chris@18
|
515 if (OS_WINDOWS) {
|
Chris@18
|
516 $exe_suffixes = getenv('PATHEXT')
|
Chris@18
|
517 ? explode(PATH_SEPARATOR, getenv('PATHEXT'))
|
Chris@18
|
518 : array('.exe','.bat','.cmd','.com');
|
Chris@18
|
519 // allow passing a command.exe param
|
Chris@18
|
520 if (strpos($program, '.') !== false) {
|
Chris@18
|
521 array_unshift($exe_suffixes, '');
|
Chris@18
|
522 }
|
Chris@18
|
523 } else {
|
Chris@18
|
524 $exe_suffixes = array('');
|
Chris@18
|
525 }
|
Chris@18
|
526
|
Chris@18
|
527 foreach ($exe_suffixes as $suff) {
|
Chris@18
|
528 foreach ($path_elements as $dir) {
|
Chris@18
|
529 $file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
|
Chris@18
|
530 // It's possible to run a .bat on Windows that is_executable
|
Chris@18
|
531 // would return false for. The is_executable check is meaningless...
|
Chris@18
|
532 if (OS_WINDOWS) {
|
Chris@18
|
533 return $file;
|
Chris@18
|
534 } else {
|
Chris@18
|
535 if (is_executable($file)) {
|
Chris@18
|
536 return $file;
|
Chris@18
|
537 }
|
Chris@18
|
538 }
|
Chris@18
|
539 }
|
Chris@18
|
540 }
|
Chris@18
|
541 return $fallback;
|
Chris@18
|
542 }
|
Chris@18
|
543
|
Chris@18
|
544 /**
|
Chris@18
|
545 * The "find" command
|
Chris@18
|
546 *
|
Chris@18
|
547 * Usage:
|
Chris@18
|
548 *
|
Chris@18
|
549 * System::find($dir);
|
Chris@18
|
550 * System::find("$dir -type d");
|
Chris@18
|
551 * System::find("$dir -type f");
|
Chris@18
|
552 * System::find("$dir -name *.php");
|
Chris@18
|
553 * System::find("$dir -name *.php -name *.htm*");
|
Chris@18
|
554 * System::find("$dir -maxdepth 1");
|
Chris@18
|
555 *
|
Chris@18
|
556 * Params implemented:
|
Chris@18
|
557 * $dir -> Start the search at this directory
|
Chris@18
|
558 * -type d -> return only directories
|
Chris@18
|
559 * -type f -> return only files
|
Chris@18
|
560 * -maxdepth <n> -> max depth of recursion
|
Chris@18
|
561 * -name <pattern> -> search pattern (bash style). Multiple -name param allowed
|
Chris@18
|
562 *
|
Chris@18
|
563 * @param mixed Either array or string with the command line
|
Chris@18
|
564 * @return array Array of found files
|
Chris@18
|
565 */
|
Chris@18
|
566 public static function find($args)
|
Chris@18
|
567 {
|
Chris@18
|
568 if (!is_array($args)) {
|
Chris@18
|
569 $args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
|
Chris@18
|
570 }
|
Chris@18
|
571 $dir = realpath(array_shift($args));
|
Chris@18
|
572 if (!$dir) {
|
Chris@18
|
573 return array();
|
Chris@18
|
574 }
|
Chris@18
|
575 $patterns = array();
|
Chris@18
|
576 $depth = 0;
|
Chris@18
|
577 $do_files = $do_dirs = true;
|
Chris@18
|
578 $args_count = count($args);
|
Chris@18
|
579 for ($i = 0; $i < $args_count; $i++) {
|
Chris@18
|
580 switch ($args[$i]) {
|
Chris@18
|
581 case '-type':
|
Chris@18
|
582 if (in_array($args[$i+1], array('d', 'f'))) {
|
Chris@18
|
583 if ($args[$i+1] == 'd') {
|
Chris@18
|
584 $do_files = false;
|
Chris@18
|
585 } else {
|
Chris@18
|
586 $do_dirs = false;
|
Chris@18
|
587 }
|
Chris@18
|
588 }
|
Chris@18
|
589 $i++;
|
Chris@18
|
590 break;
|
Chris@18
|
591 case '-name':
|
Chris@18
|
592 $name = preg_quote($args[$i+1], '#');
|
Chris@18
|
593 // our magic characters ? and * have just been escaped,
|
Chris@18
|
594 // so now we change the escaped versions to PCRE operators
|
Chris@18
|
595 $name = strtr($name, array('\?' => '.', '\*' => '.*'));
|
Chris@18
|
596 $patterns[] = '('.$name.')';
|
Chris@18
|
597 $i++;
|
Chris@18
|
598 break;
|
Chris@18
|
599 case '-maxdepth':
|
Chris@18
|
600 $depth = $args[$i+1];
|
Chris@18
|
601 break;
|
Chris@18
|
602 }
|
Chris@18
|
603 }
|
Chris@18
|
604 $path = System::_dirToStruct($dir, $depth, 0, true);
|
Chris@18
|
605 if ($do_files && $do_dirs) {
|
Chris@18
|
606 $files = array_merge($path['files'], $path['dirs']);
|
Chris@18
|
607 } elseif ($do_dirs) {
|
Chris@18
|
608 $files = $path['dirs'];
|
Chris@18
|
609 } else {
|
Chris@18
|
610 $files = $path['files'];
|
Chris@18
|
611 }
|
Chris@18
|
612 if (count($patterns)) {
|
Chris@18
|
613 $dsq = preg_quote(DIRECTORY_SEPARATOR, '#');
|
Chris@18
|
614 $pattern = '#(^|'.$dsq.')'.implode('|', $patterns).'($|'.$dsq.')#';
|
Chris@18
|
615 $ret = array();
|
Chris@18
|
616 $files_count = count($files);
|
Chris@18
|
617 for ($i = 0; $i < $files_count; $i++) {
|
Chris@18
|
618 // only search in the part of the file below the current directory
|
Chris@18
|
619 $filepart = basename($files[$i]);
|
Chris@18
|
620 if (preg_match($pattern, $filepart)) {
|
Chris@18
|
621 $ret[] = $files[$i];
|
Chris@18
|
622 }
|
Chris@18
|
623 }
|
Chris@18
|
624 return $ret;
|
Chris@18
|
625 }
|
Chris@18
|
626 return $files;
|
Chris@18
|
627 }
|
Chris@18
|
628 }
|