3 * File/Directory manipulation
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 313024 2011-07-06 19:51:24Z dufuz $
13 * @link http://pear.php.net/package/PEAR
14 * @since File available since Release 0.1
20 require_once 'PEAR.php';
21 require_once 'Console/Getopt.php';
23 $GLOBALS['_System_temp_files'] = array();
26 * System offers cross plattform compatible system functions
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()}).
36 * Documentation on this class you can find in:
37 * http://pear.php.net/manual/
40 * if (!@System::rm('-r file1 dir1')) {
41 * print "could not delete file1 or dir1";
44 * In case you need to to pass file names with spaces,
45 * pass the params as an array:
47 * System::rm(array('-r', $file1, $dir1));
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.4
55 * @link http://pear.php.net/package/PEAR
56 * @since Class available since Release 0.1
62 * returns the commandline arguments of a function
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
71 function _parseArgs($argv, $short_options, $long_options = null)
73 if (!is_array($argv) && $argv !== null) {
74 // Find all items, quoted or otherwise
75 preg_match_all("/(?:[\"'])(.*?)(?:['\"])|([^\s]+)/", $argv, $av);
77 foreach ($av[2] as $k => $a) {
81 $argv[$k] = trim($a) ;
84 return Console_Getopt::getopt2($argv, $short_options, $long_options);
88 * Output errors with PHP trigger_error(). You can silence the errors
89 * with prefixing a "@" sign to the function call: @System::mkdir(..);
91 * @param mixed $error a PEAR error or a string with the error message
96 function raiseError($error)
98 if (PEAR::isError($error)) {
99 $error = $error->getMessage();
101 trigger_error($error, E_USER_WARNING);
106 * Creates a nested array representing the structure of a directory
108 * System::_dirToStruct('dir1', 0) =>
122 * @param string $sPath Name of the directory
123 * @param integer $maxinst max. deep of the lookup
124 * @param integer $aktinst starting deep of the lookup
125 * @param bool $silent if true, do not emit errors.
126 * @return array the structure of the dir
130 function _dirToStruct($sPath, $maxinst, $aktinst = 0, $silent = false)
132 $struct = array('dirs' => array(), 'files' => array());
133 if (($dir = @opendir($sPath)) === false) {
135 System::raiseError("Could not open dir $sPath");
137 return $struct; // XXX could not open error
140 $struct['dirs'][] = $sPath = realpath($sPath); // XXX don't add if '.' or '..' ?
142 while (false !== ($file = readdir($dir))) {
143 if ($file != '.' && $file != '..') {
150 if ($aktinst < $maxinst || $maxinst == 0) {
151 foreach ($list as $val) {
152 $path = $sPath . DIRECTORY_SEPARATOR . $val;
153 if (is_dir($path) && !is_link($path)) {
154 $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1, $silent);
155 $struct = array_merge_recursive($struct, $tmp);
157 $struct['files'][] = $path;
166 * Creates a nested array representing the structure of a directory and files
168 * @param array $files Array listing files and dirs
171 * @see System::_dirToStruct()
173 function _multipleToStruct($files)
175 $struct = array('dirs' => array(), 'files' => array());
176 settype($files, 'array');
177 foreach ($files as $file) {
178 if (is_dir($file) && !is_link($file)) {
179 $tmp = System::_dirToStruct($file, 0);
180 $struct = array_merge_recursive($tmp, $struct);
182 if (!in_array($file, $struct['files'])) {
183 $struct['files'][] = $file;
191 * The rm command for removing files.
192 * Supports multiple files and dirs and also recursive deletes
194 * @param string $args the arguments for rm
195 * @return mixed PEAR_Error or true for success
201 $opts = System::_parseArgs($args, 'rf'); // "f" does nothing but I like it :-)
202 if (PEAR::isError($opts)) {
203 return System::raiseError($opts);
205 foreach ($opts[0] as $opt) {
206 if ($opt[0] == 'r') {
207 $do_recursive = true;
211 if (isset($do_recursive)) {
212 $struct = System::_multipleToStruct($opts[1]);
213 foreach ($struct['files'] as $file) {
214 if (!@unlink($file)) {
219 rsort($struct['dirs']);
220 foreach ($struct['dirs'] as $dir) {
226 foreach ($opts[1] as $file) {
227 $delete = (is_dir($file)) ? 'rmdir' : 'unlink';
228 if (!@$delete($file)) {
239 * The -p option will create parent directories
240 * @param string $args the name of the director(y|ies) to create
241 * @return bool True for success
245 function mkDir($args)
247 $opts = System::_parseArgs($args, 'pm:');
248 if (PEAR::isError($opts)) {
249 return System::raiseError($opts);
252 $mode = 0777; // default mode
253 foreach ($opts[0] as $opt) {
254 if ($opt[0] == 'p') {
255 $create_parents = true;
256 } elseif ($opt[0] == 'm') {
257 // if the mode is clearly an octal number (starts with 0)
258 // convert it to decimal
259 if (strlen($opt[1]) && $opt[1]{0} == '0') {
260 $opt[1] = octdec($opt[1]);
270 if (isset($create_parents)) {
271 foreach ($opts[1] as $dir) {
273 while ((!file_exists($dir) || !is_dir($dir)) &&
274 $dir != DIRECTORY_SEPARATOR) {
275 array_unshift($dirstack, $dir);
276 $dir = dirname($dir);
279 while ($newdir = array_shift($dirstack)) {
280 if (!is_writeable(dirname($newdir))) {
285 if (!mkdir($newdir, $mode)) {
291 foreach($opts[1] as $dir) {
292 if ((@file_exists($dir) || !is_dir($dir)) && !mkdir($dir, $mode)) {
305 * 1) $var = System::cat('sample.txt test.txt');
306 * 2) System::cat('sample.txt test.txt > final.txt');
307 * 3) System::cat('sample.txt test.txt >> final.txt');
309 * Note: as the class use fopen, urls should work also (test that)
311 * @param string $args the arguments
312 * @return boolean true on success
320 if (!is_array($args)) {
321 $args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
324 $count_args = count($args);
325 for ($i = 0; $i < $count_args; $i++) {
326 if ($args[$i] == '>') {
328 $outputfile = $args[$i+1];
330 } elseif ($args[$i] == '>>') {
332 $outputfile = $args[$i+1];
335 $files[] = $args[$i];
340 if (!$outputfd = fopen($outputfile, $mode)) {
341 $err = System::raiseError("Could not open $outputfile");
346 foreach ($files as $file) {
347 if (!$fd = fopen($file, 'r')) {
348 System::raiseError("Could not open $file");
351 while ($cont = fread($fd, 2048)) {
352 if (is_resource($outputfd)) {
353 fwrite($outputfd, $cont);
360 if (is_resource($outputfd)) {
367 * Creates temporary files or directories. This function will remove
368 * the created files when the scripts finish its execution.
371 * 1) $tempfile = System::mktemp("prefix");
372 * 2) $tempdir = System::mktemp("-d prefix");
373 * 3) $tempfile = System::mktemp();
374 * 4) $tempfile = System::mktemp("-t /var/tmp prefix");
376 * prefix -> The string that will be prepended to the temp name
377 * (defaults to "tmp").
378 * -d -> A temporary dir will be created instead of a file.
379 * -t -> The target dir where the temporary (file|dir) will be created. If
380 * this param is missing by default the env vars TMP on Windows or
381 * TMPDIR in Unix will be used. If these vars are also missing
382 * c:\windows\temp or /tmp will be used.
384 * @param string $args The arguments
385 * @return mixed the full path of the created (file|dir) or false
386 * @see System::tmpdir()
390 function mktemp($args = null)
392 static $first_time = true;
393 $opts = System::_parseArgs($args, 't:d');
394 if (PEAR::isError($opts)) {
395 return System::raiseError($opts);
398 foreach ($opts[0] as $opt) {
399 if ($opt[0] == 'd') {
401 } elseif ($opt[0] == 't') {
406 $prefix = (isset($opts[1][0])) ? $opts[1][0] : 'tmp';
407 if (!isset($tmpdir)) {
408 $tmpdir = System::tmpdir();
411 if (!System::mkDir(array('-p', $tmpdir))) {
415 $tmp = tempnam($tmpdir, $prefix);
416 if (isset($tmp_is_dir)) {
417 unlink($tmp); // be careful possible race condition here
418 if (!mkdir($tmp, 0700)) {
419 return System::raiseError("Unable to create temporary directory $tmpdir");
423 $GLOBALS['_System_temp_files'][] = $tmp;
424 if (isset($tmp_is_dir)) {
425 //$GLOBALS['_System_temp_files'][] = dirname($tmp);
429 PEAR::registerShutdownFunc(array('System', '_removeTmpFiles'));
437 * Remove temporary files created my mkTemp. This function is executed
438 * at script shutdown time
443 function _removeTmpFiles()
445 if (count($GLOBALS['_System_temp_files'])) {
446 $delete = $GLOBALS['_System_temp_files'];
447 array_unshift($delete, '-r');
449 $GLOBALS['_System_temp_files'] = array();
454 * Get the path of the temporal directory set in the system
455 * by looking in its environments variables.
456 * Note: php.ini-recommended removes the "E" from the variables_order setting,
457 * making unavaible the $_ENV array, that s why we do tests with _ENV
460 * @return string The temporary directory on the system
465 if ($var = isset($_ENV['TMP']) ? $_ENV['TMP'] : getenv('TMP')) {
468 if ($var = isset($_ENV['TEMP']) ? $_ENV['TEMP'] : getenv('TEMP')) {
471 if ($var = isset($_ENV['USERPROFILE']) ? $_ENV['USERPROFILE'] : getenv('USERPROFILE')) {
474 if ($var = isset($_ENV['windir']) ? $_ENV['windir'] : getenv('windir')) {
477 return getenv('SystemRoot') . '\temp';
479 if ($var = isset($_ENV['TMPDIR']) ? $_ENV['TMPDIR'] : getenv('TMPDIR')) {
482 return realpath('/tmp');
486 * The "which" command (show the full path of a command)
488 * @param string $program The command to search for
489 * @param mixed $fallback Value to return if $program is not found
491 * @return mixed A string with the full path or false if not found
493 * @author Stig Bakken <ssb@php.net>
495 function which($program, $fallback = false)
498 if (!is_string($program) || '' == $program) {
503 if (basename($program) != $program) {
504 $path_elements[] = dirname($program);
505 $program = basename($program);
508 if (!ini_get('safe_mode') || !$path = ini_get('safe_mode_exec_dir')) {
509 $path = getenv('PATH');
511 $path = getenv('Path'); // some OSes are just stupid enough to do this
514 $path_elements = explode(PATH_SEPARATOR, $path);
518 $exe_suffixes = getenv('PATHEXT')
519 ? explode(PATH_SEPARATOR, getenv('PATHEXT'))
520 : array('.exe','.bat','.cmd','.com');
521 // allow passing a command.exe param
522 if (strpos($program, '.') !== false) {
523 array_unshift($exe_suffixes, '');
525 // is_executable() is not available on windows for PHP4
526 $pear_is_executable = (function_exists('is_executable')) ? 'is_executable' : 'is_file';
528 $exe_suffixes = array('');
529 $pear_is_executable = 'is_executable';
532 foreach ($exe_suffixes as $suff) {
533 foreach ($path_elements as $dir) {
534 $file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
535 if (@$pear_is_executable($file)) {
548 * System::find($dir);
549 * System::find("$dir -type d");
550 * System::find("$dir -type f");
551 * System::find("$dir -name *.php");
552 * System::find("$dir -name *.php -name *.htm*");
553 * System::find("$dir -maxdepth 1");
556 * $dir -> Start the search at this directory
557 * -type d -> return only directories
558 * -type f -> return only files
559 * -maxdepth <n> -> max depth of recursion
560 * -name <pattern> -> search pattern (bash style). Multiple -name param allowed
562 * @param mixed Either array or string with the command line
563 * @return array Array of found files
569 if (!is_array($args)) {
570 $args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY);
572 $dir = realpath(array_shift($args));
578 $do_files = $do_dirs = true;
579 $args_count = count($args);
580 for ($i = 0; $i < $args_count; $i++) {
583 if (in_array($args[$i+1], array('d', 'f'))) {
584 if ($args[$i+1] == 'd') {
593 $name = preg_quote($args[$i+1], '#');
594 // our magic characters ? and * have just been escaped,
595 // so now we change the escaped versions to PCRE operators
596 $name = strtr($name, array('\?' => '.', '\*' => '.*'));
597 $patterns[] = '('.$name.')';
601 $depth = $args[$i+1];
605 $path = System::_dirToStruct($dir, $depth, 0, true);
606 if ($do_files && $do_dirs) {
607 $files = array_merge($path['files'], $path['dirs']);
608 } elseif ($do_dirs) {
609 $files = $path['dirs'];
611 $files = $path['files'];
613 if (count($patterns)) {
614 $dsq = preg_quote(DIRECTORY_SEPARATOR, '#');
615 $pattern = '#(^|'.$dsq.')'.implode('|', $patterns).'($|'.$dsq.')#';
617 $files_count = count($files);
618 for ($i = 0; $i < $files_count; $i++) {
619 // only search in the part of the file below the current directory
620 $filepart = basename($files[$i]);
621 if (preg_match($pattern, $filepart)) {