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 $struc = array();
55 foreach($allfiles as $file) {
56 if ($this->_checkIgnore(basename($file), dirname($file), $ignore, false)) {
57 // print 'Ignoring '.$file."<br>\n";
58 continue;
59 }
60 $path = substr(dirname($file), strlen(str_replace(DIRECTORY_SEPARATOR,
61 '/',
62 realpath($package_directory))) + 1);
63 if (!$path) {
64 $path = '/';
65 }
66 $ext = array_pop(explode('.', $file));
67 if (strlen($ext) == strlen($file)) {
68 $ext = '';
69 }
70 $struc[$path][] = array('file' => basename($file),
71 'ext' => $ext,
72 'path' => (($path == '/') ? basename($file) : $path . '/' . basename($file)),
73 'fullpath' => $file);
74 }
75 uksort($struc,'strnatcasecmp');
76 foreach($struc as $key => $ind) {
77 usort($ind, array($this, 'sortfiles'));
78 $struc[$key] = $ind;
79 }
80
81 $tempstruc = $struc;
82 $struc = array('/' => $tempstruc['/']);
83 $bv = 0;
84 foreach($tempstruc as $key => $ind) {
85 $save = $key;
86 if ($key != '/')
87 {
88 $struc['/'] = $this->_setupDirs($struc['/'], explode('/',$key), $tempstruc[$key]);
89 }
90 }
91 uksort($struc['/'], array($this, 'mystrucsort'));
92
93 return $struc;
94 }
95
96 /**
97 * @return array list of files in a directory
98 * @param string $directory full path to the directory you want the list of
99 */
100 function dirList($directory)
101 {
102 $ret = false;
103 if (@is_dir($directory)) {
104 $ret = array();
105 $d = @dir($directory); // thanks to Jason E Sweat (jsweat@users.sourceforge.net) for fix
106 while($d && $entry=$d->read()) {
107 if ($entry{0} != '.') {
108 if (is_file($directory . '/' . $entry)) {
109 $ret[] = $directory . '/' . $entry;
110 }
111 if (is_dir($directory . '/' . $entry)) {
112 $tmp = $this->dirList($directory . '/' . $entry);
113 if (is_array($tmp)) {
114 foreach($tmp as $ent) {
115 $ret[] = $ent;
116 }
117 }
118 }
119 }
120 }
121 if ($d) {
122 $d->close();
123 }
124 } else {
125 return PEAR_PackageFileManager::raiseError(PEAR_PACKAGEFILEMANAGER_DIR_DOESNT_EXIST, $directory);
126 }
127 return $ret;
128 }
129
130 /**
131 * Tell whether to ignore a file or a directory
132 * allows * and ? wildcards
133 *
134 * @param string $file just the file name of the file or directory,
135 * in the case of directories this is the last dir
136 * @param string $path the full path
137 * @param array $ignore
138 * @return bool true if $path should be ignored, false if it should not
139 * @access private
140 */
141 function _checkIgnore($file, $path, $ignore, $ignore_no_ext = false)
142 {
143 $path = realpath($path);
144 if (!count($ignore)) {
145 return false;
146 }
147 if ($ignore_no_ext && strtoupper($file) != 'README' && strtoupper($file) != 'INSTALL'
148 && strtoupper($file) != 'CHANGELOG' && strtoupper($file) != 'FAQ'
149 && strtoupper($file) != 'NEWS') {
150 if (!is_numeric(strpos($file,'.'))) return true;
151 }
152 $this->_setupIgnore($ignore);
153 if (!$this->ignore) {
154 return false;
155 }
156 if (is_array($this->ignore)) {
157 foreach($this->ignore as $match) {
158 if (is_array($match)) {
159 preg_match('/^'.strtoupper($match[0]).'$/',strtoupper($path) . PATH_DELIMITER,$find);
160 if (!count($find)) {
161 preg_match('/^'.strtoupper($match[0]).'$/',strtoupper($path),$find);
162 }
163 if (count($find)) {
164 preg_match('/^'.strtoupper($match[1]).'$/',strtoupper($file),$find);
165 if (count($find)) return true;
166 }
167 } else {
168 preg_match('/^'.strtoupper($match).'$/',strtoupper($path),$find);
169 if (count($find)) return true;
170 preg_match('/^'.strtoupper($match).'$/',strtoupper($file),$find);
171 if (count($find)) return true;
172 }
173 }
174 }
175 return false;
176 }
177
178 /**
179 * Construct the {@link $ignore} array
180 * @param array strings of files/paths/wildcards to ignore
181 * @access private
182 */
183 function _setupIgnore($ignore)
184 {
185 $ig = array();
186 if (is_array($ignore)) {
187 for($i=0; $i<count($ignore);$i++) {
188 $ignore[$i] = strtr($ignore[$i], "\\", "/");
189 $ignore[$i] = str_replace('//','/',$ignore[$i]);
190
191 if (!empty($ignore[$i])) {
192 if (!is_numeric(strpos($ignore[$i], '/'))) {
193 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
194 } else {
195 if (basename($ignore[$i]) . '/' == $ignore[$i]) {
196 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
197 } else {
198 $ig[] = array($this->_getRegExpableSearchString($ignore[$i]),
199 $this->_getRegExpableSearchString(basename($ignore[$i])));
200 }
201 }
202 }
203 }
204 if (count($ig)) {
205 $this->ignore = $ig;
206 }
207 } else $this->ignore = false;
208 }
209
210 /**
211 * Converts $s into a string that can be used with preg_match
212 * @param string $s string with wildcards ? and *
213 * @return string converts * to .*, ? to ., etc.
214 * @access private
215 */
216 function _getRegExpableSearchString($s)
217 {
218 $y = '\/';
219 if (DIRECTORY_SEPARATOR == '\\') {
220 $y = '\\\\';
221 }
222 if (strpos($s,'/') === strlen($s) - 1) {
223 $s = str_replace('/', DIRECTORY_SEPARATOR, $s);
224 }
225 $x = strtr($s, array('?' => '.','*' => '.*','.' => '\\.','\\' => '\\\\','/' => '\\/',
226 '[' => '\\[',']' => '\\]','-' => '\\-'));
227 if (strpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) {
228 $x = "(?:.*$y$x?.*|$x.*)";
229 }
230 return $x;
231 }
232
233 /**
234 * Recursively move contents of $struc into associative array
235 *
236 * The contents of $struc have many indexes like 'dir/subdir/subdir2'.
237 * This function converts them to
238 * array('dir' => array('subdir' => array('subdir2')))
239 * @param array struc is array('dir' => array of files in dir,
240 * 'dir/subdir' => array of files in dir/subdir,...)
241 * @param array array form of 'dir/subdir/subdir2' array('dir','subdir','subdir2')
242 * @return array same as struc but with array('dir' =>
243 * array(file1,file2,'subdir' => array(file1,...)))
244 * @access private
245 */
246 function _setupDirs($struc, $dir, $contents)
247 {
248 if (!count($dir)) {
249 foreach($contents as $dir => $files) {
250 if (is_string($dir)) {
251 if (strpos($dir, '/')) {
252 $test = true;
253 $a = $contents[$dir];
254 unset($contents[$dir]);
255 $b = explode('/', $dir);
256 $c = array_shift($b);
257 if (isset($contents[$c])) {
258 $contents[$c] = $this->_setDir($contents[$c], $this->_setupDirs(array(), $b, $a));
259 } else {
260 $contents[$c] = $this->_setupDirs(array(), $b, $a);
261 }
262 }
263 }
264 }
265 return $contents;
266 }
267 $me = array_shift($dir);
268 if (!isset($struc[$me])) {
269 $struc[$me] = array();
270 }
271 $struc[$me] = $this->_setupDirs($struc[$me], $dir, $contents);
272 return $struc;
273 }
274
275
276 /**
277 * Recursively add all the subdirectories of $contents to $dir without erasing anything in
278 * $dir
279 * @param array
280 * @param array
281 * @return array processed $dir
282 * @access private
283 */
284 function _setDir($dir, $contents)
285 {
286 while(list($one,$two) = each($contents)) {
287 if (isset($dir[$one])) {
288 $dir[$one] = $this->_setDir($dir[$one], $contents[$one]);
289 } else {
290 $dir[$one] = $two;
291 }
292 }
293 return $dir;
294 }
295
296
297 /**#@+
298 * Sorting functions for the file list
299 * @param string
300 * @param string
301 * @access private
302 */
303 function sortfiles($a, $b)
304 {
305 return strnatcasecmp($a['file'],$b['file']);
306 }
307
308 function mystrucsort($a, $b)
309 {
310 if (is_numeric($a) && is_string($b)) return 1;
311 if (is_numeric($b) && is_string($a)) return -1;
312 if (is_numeric($a) && is_numeric($b))
313 {
314 if ($a > $b) return 1;
315 if ($a < $b) return -1;
316 if ($a == $b) return 0;
317 }
318 return strnatcasecmp($a,$b);
319 }
320 /**#@-*/
321 }
322 ?>