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