1 <?php
2 /**
3 * Retrieve the files from a directory listing
4 * @package PEAR_PackageFileManager
5 */
6 /**
7 * Retrieve the files from a directory listing
8 * @package PEAR_PackageFileManager
9 */
10 class PEAR_PackageFileManager_File {
11 /**
12 * @var array
13 * @access private
14 */
15 var $_options =
16 array(
17 );
18
19 /**
20 * @access private
21 * @var PEAR_PackageFileManager
22 */
23 var $_parent;
24
25 /**
26 * @access private
27 * @var array|false
28 */
29 var $_ignore = false;
30
31 /**
32 * @param PEAR_PackageFileManager
33 * @param array
34 */
35 function PEAR_PackageFileManager_File(&$parent, $options)
36 {
37 $this->_parent = &$parent;
38 $this->_options = array_merge($this->_options, $options);
39 }
40
41 /**
42 * Generate the <filelist></filelist> section
43 * of the package file.
44 *
45 * This function performs the backend generation of the array
46 * containing all files in this package
47 * @return array
48 */
49 function getFileList()
50 {
51 $package_directory = $this->_options['packagedirectory'];
52 $ignore = $this->_options['ignore'];
53 $allfiles = $this->dirList(substr($package_directory, 0, strlen($package_directory) - 1));
54 if (PEAR::isError($allfiles)) {
55 return $allfiles;
56 }
57 if (!count($allfiles)) {
58 return PEAR_PackageFileManager::raiseError(PEAR_PACKAGEFILEMANAGER_NO_FILES,
59 substr($package_directory, 0, strlen($package_directory) - 1));
60 }
61 $struc = array();
62 foreach($allfiles as $file) {
63 if ($this->_checkIgnore(basename($file), dirname($file), $ignore, false)) {
64 // print 'Ignoring '.$file."<br>\n";
65 continue;
66 }
67 $path = substr(dirname($file), strlen(str_replace(DIRECTORY_SEPARATOR,
68 '/',
69 realpath($package_directory))) + 1);
70 if (!$path) {
71 $path = '/';
72 }
73 $ext = array_pop(explode('.', $file));
74 if (strlen($ext) == strlen($file)) {
75 $ext = '';
76 }
77 $struc[$path][] = array('file' => basename($file),
78 'ext' => $ext,
79 'path' => (($path == '/') ? basename($file) : $path . '/' . basename($file)),
80 'fullpath' => $file);
81 }
82 if (!count($struc)) {
83 $newig = '';
84 foreach($this->_options['ignore'] as $ig) {
85 if (!empty($newig)) {
86 $newig .= ', ';
87 }
88 $newig .= $ig;
89 }
90 return PEAR_PackageFileManager::raiseError(PEAR_PACKAGEFILEMANAGER_IGNORED_EVERYTHING,
91 substr($package_directory, 0, strlen($package_directory) - 1), $newig);
92 }
93 uksort($struc,'strnatcasecmp');
94 foreach($struc as $key => $ind) {
95 usort($ind, array($this, 'sortfiles'));
96 $struc[$key] = $ind;
97 }
98
99 $tempstruc = $struc;
100 $struc = array('/' => $tempstruc['/']);
101 $bv = 0;
102 foreach($tempstruc as $key => $ind) {
103 $save = $key;
104 if ($key != '/')
105 {
106 $struc['/'] = $this->_setupDirs($struc['/'], explode('/',$key), $tempstruc[$key]);
107 }
108 }
109 uksort($struc['/'], array($this, 'mystrucsort'));
110
111 return $struc;
112 }
113
114 /**
115 * @return array list of files in a directory
116 * @param string $directory full path to the directory you want the list of
117 */
118 function dirList($directory)
119 {
120 $ret = false;
121 if (@is_dir($directory)) {
122 $ret = array();
123 $d = @dir($directory); // thanks to Jason E Sweat (jsweat@users.sourceforge.net) for fix
124 while($d && $entry=$d->read()) {
125 if ($entry{0} != '.') {
126 if (is_file($directory . '/' . $entry)) {
127 $ret[] = $directory . '/' . $entry;
128 }
129 if (is_dir($directory . '/' . $entry)) {
130 $tmp = $this->dirList($directory . '/' . $entry);
131 if (is_array($tmp)) {
132 foreach($tmp as $ent) {
133 $ret[] = $ent;
134 }
135 }
136 }
137 }
138 }
139 if ($d) {
140 $d->close();
141 }
142 } else {
143 return PEAR_PackageFileManager::raiseError(PEAR_PACKAGEFILEMANAGER_DIR_DOESNT_EXIST, $directory);
144 }
145 return $ret;
146 }
147
148 /**
149 * Tell whether to ignore a file or a directory
150 * allows * and ? wildcards
151 *
152 * @param string $file just the file name of the file or directory,
153 * in the case of directories this is the last dir
154 * @param string $path the full path
155 * @param array $ignore
156 * @return bool true if $path should be ignored, false if it should not
157 * @access private
158 */
159 function _checkIgnore($file, $path, $ignore, $ignore_no_ext = false)
160 {
161 $path = realpath($path);
162 if (!count($ignore)) {
163 return false;
164 }
165 if ($ignore_no_ext && strtoupper($file) != 'README' && strtoupper($file) != 'INSTALL'
166 && strtoupper($file) != 'CHANGELOG' && strtoupper($file) != 'FAQ'
167 && strtoupper($file) != 'NEWS') {
168 if (!is_numeric(strpos($file,'.'))) return true;
169 }
170 if (!isset($this->ignore) || !$this->ignore) {
171 $this->_setupIgnore($ignore);
172 if (!$this->ignore) {
173 return false;
174 }
175 }
176 if (is_array($this->ignore)) {
177 foreach($this->ignore as $match) {
178 // match is an array if the ignore parameter was a /path/to/pattern
179 if (is_array($match)) {
180 // check to see if the path matches with a path delimiter appended
181 preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path) . '/',$find);
182 if (!count($find)) {
183 // check to see if it matches without an appended path delimiter
184 preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path), $find);
185 }
186 if (count($find)) {
187 // check to see if the file matches the file portion of the regex string
188 preg_match('/^' . strtoupper($match[1]).'$/', strtoupper($file), $find);
189 if (count($find)) {
190 return true;
191 }
192 }
193 // check to see if the full path matches the regex
194 preg_match('/^' . strtoupper($match[0]).'$/',
195 strtoupper($path . DIRECTORY_SEPARATOR . $file), $find);
196 if (count($find)) {
197 return true;
198 }
199 } else {
200 // ignore parameter was just a pattern with no path delimiters
201 // check it against the path
202 preg_match('/^' . strtoupper($match).'$/', strtoupper($path), $find);
203 if (count($find)) {
204 return true;
205 }
206 // check it against the file only
207 preg_match('/^' . strtoupper($match).'$/', strtoupper($file), $find);
208 if (count($find)) {
209 return true;
210 }
211 }
212 }
213 }
214 return false;
215 }
216
217 /**
218 * Construct the {@link $ignore} array
219 * @param array strings of files/paths/wildcards to ignore
220 * @access private
221 */
222 function _setupIgnore($ignore)
223 {
224 $ig = array();
225 if (is_array($ignore)) {
226 for($i=0; $i<count($ignore);$i++) {
227 $ignore[$i] = strtr($ignore[$i], "\\", "/");
228 $ignore[$i] = str_replace('//','/',$ignore[$i]);
229
230 if (!empty($ignore[$i])) {
231 if (!is_numeric(strpos($ignore[$i], '/'))) {
232 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
233 } else {
234 if (basename($ignore[$i]) . '/' == $ignore[$i]) {
235 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
236 } else {
237 $ig[] = array($this->_getRegExpableSearchString($ignore[$i]),
238 $this->_getRegExpableSearchString(basename($ignore[$i])));
239 }
240 }
241 }
242 }
243 if (count($ig)) {
244 $this->ignore = $ig;
245 }
246 } else $this->ignore = false;
247 }
248
249 /**
250 * Converts $s into a string that can be used with preg_match
251 * @param string $s string with wildcards ? and *
252 * @return string converts * to .*, ? to ., etc.
253 * @access private
254 */
255 function _getRegExpableSearchString($s)
256 {
257 $y = '\/';
258 if (DIRECTORY_SEPARATOR == '\\') {
259 $y = '\\\\';
260 }
261 $s = str_replace('/', DIRECTORY_SEPARATOR, $s);
262 $x = strtr($s, array('?' => '.','*' => '.*','.' => '\\.','\\' => '\\\\','/' => '\\/',
263 '[' => '\\[',']' => '\\]','-' => '\\-'));
264 if (strpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) {
265 $x = "(?:.*$y$x?.*|$x.*)";
266 }
267 return $x;
268 }
269
270 /**
271 * Recursively move contents of $struc into associative array
272 *
273 * The contents of $struc have many indexes like 'dir/subdir/subdir2'.
274 * This function converts them to
275 * array('dir' => array('subdir' => array('subdir2')))
276 * @param array struc is array('dir' => array of files in dir,
277 * 'dir/subdir' => array of files in dir/subdir,...)
278 * @param array array form of 'dir/subdir/subdir2' array('dir','subdir','subdir2')
279 * @return array same as struc but with array('dir' =>
280 * array(file1,file2,'subdir' => array(file1,...)))
281 * @access private
282 */
283 function _setupDirs($struc, $dir, $contents)
284 {
285 if (!count($dir)) {
286 foreach($contents as $dir => $files) {
287 if (is_string($dir)) {
288 if (strpos($dir, '/')) {
289 $test = true;
290 $a = $contents[$dir];
291 unset($contents[$dir]);
292 $b = explode('/', $dir);
293 $c = array_shift($b);
294 if (isset($contents[$c])) {
295 $contents[$c] = $this->_setDir($contents[$c], $this->_setupDirs(array(), $b, $a));
296 } else {
297 $contents[$c] = $this->_setupDirs(array(), $b, $a);
298 }
299 }
300 }
301 }
302 return $contents;
303 }
304 $me = array_shift($dir);
305 if (!isset($struc[$me])) {
306 $struc[$me] = array();
307 }
308 $struc[$me] = $this->_setupDirs($struc[$me], $dir, $contents);
309 return $struc;
310 }
311
312
313 /**
314 * Recursively add all the subdirectories of $contents to $dir without erasing anything in
315 * $dir
316 * @param array
317 * @param array
318 * @return array processed $dir
319 * @access private
320 */
321 function _setDir($dir, $contents)
322 {
323 while(list($one,$two) = each($contents)) {
324 if (isset($dir[$one])) {
325 $dir[$one] = $this->_setDir($dir[$one], $contents[$one]);
326 } else {
327 $dir[$one] = $two;
328 }
329 }
330 return $dir;
331 }
332
333
334 /**#@+
335 * Sorting functions for the file list
336 * @param string
337 * @param string
338 * @access private
339 */
340 function sortfiles($a, $b)
341 {
342 return strnatcasecmp($a['file'],$b['file']);
343 }
344
345 function mystrucsort($a, $b)
346 {
347 if (is_numeric($a) && is_string($b)) return 1;
348 if (is_numeric($b) && is_string($a)) return -1;
349 if (is_numeric($a) && is_numeric($b))
350 {
351 if ($a > $b) return 1;
352 if ($a < $b) return -1;
353 if ($a == $b) return 0;
354 }
355 return strnatcasecmp($a,$b);
356 }
357 /**#@-*/
358 }
359 ?>