annotate vendor/pear/pear-core-minimal/src/System.php @ 0:1e000243b222

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