Find.php 14.8 KB
Newer Older
Simo Leone's avatar
Simo Leone committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2005 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author: Sterling Hughes <sterling@php.net>                           |
// +----------------------------------------------------------------------+
//
// $Id: Find.php,v 1.27 2006/06/30 14:06:16 techtonik Exp $
//

require_once 'PEAR.php';

define('FILE_FIND_VERSION', '@package_version@');

// to debug uncomment this string
// define('FILE_FIND_DEBUG', '');

/**
*  Commonly needed functions searching directory trees
*
* @access public
* @version $Id: Find.php,v 1.27 2006/06/30 14:06:16 techtonik Exp $
* @package File
* @author Sterling Hughes <sterling@php.net>
*/
class File_Find
{
    /**
     * internal dir-list
     * @var array
     */
    var $_dirs = array();

    /**
     * directory separator
     * @var string
     */
    var $dirsep = "/";

    /**
     * found files
     * @var array
     */
    var $files = array();

    /**
     * found dirs
     * @var array
     */
    var $directories = array();

    /**
     * Search specified directory to find matches for specified pattern
     *
     * @param string $pattern a string containing the pattern to search
     * the directory for.
     *
     * @param string $dirpath a string containing the directory path
     * to search.
     *
     * @param string $pattern_type a string containing the type of
     * pattern matching functions to use (can either be 'php',
     * 'perl' or 'shell').
     *
     * @return array containing all of the files and directories
     * matching the pattern or null if no matches
     *
     * @author Sterling Hughes <sterling@php.net>
     * @access public
     * @static
     */
    function &glob($pattern, $dirpath, $pattern_type = 'php')
    {
        $dh = @opendir($dirpath);

        if (!$dh) {
            $pe = PEAR::raiseError("Cannot open directory $dirpath");
            return $pe;
        }

        $match_function = File_Find::_determineRegex($pattern, $pattern_type);
        $matches = array();

        // empty string cannot be specified for 'php' and 'perl' pattern
        if ($pattern || ($pattern_type != 'php' && $pattern_type != 'perl')) {
            while (false !== ($entry = @readdir($dh))) {
                if ($match_function($pattern, $entry) &&
                    $entry != '.' && $entry != '..') {
                    $matches[] = $entry;
                }
            }
        }

        @closedir($dh);

        if (0 == count($matches)) {
            $matches = null;
        }

        return $matches ;
    }

    /**
     * Map the directory tree given by the directory_path parameter.
     *
     * @param string $directory contains the directory path that you
     * want to map.
     *
     * @return array a two element array, the first element containing a list
     * of all the directories, the second element containing a list of all the
     * files.
     *
     * @author Sterling Hughes <sterling@php.net>
     * @access public
     */
    function &maptree($directory)
    {

        /* if called statically */
        if (!isset($this)  || !is_a($this, "File_Find")) {
            $obj = &new File_Find();
            return $obj->maptree($directory);
        }

        /* clear the results just in case */
        $this->files       = array();
        $this->directories = array();

        /* strip out trailing slashes */
        $directory = preg_replace('![\\\\/]+$!', '', $directory);

        $this->_dirs = array($directory);

        while (count($this->_dirs)) {
            $dir = array_pop($this->_dirs);
            File_Find::_build($dir, $this->dirsep);
            array_push($this->directories, $dir);
        }

        $retval = array($this->directories, $this->files);
        return $retval;

    }

    /**
     * Map the directory tree given by the directory parameter.
     *
     * @param string $directory contains the directory path that you
     * want to map.
     * @param integer $maxrecursion maximun number of folders to recursive
     * map
     *
     * @return array a multidimensional array containing all subdirectories
     * and their files. For example:
     *
     * Array
     * (
     *    [0] => file_1.php
     *    [1] => file_2.php
     *    [subdirname] => Array
     *       (
     *          [0] => file_1.php
     *       )
     * )
     *
     * @author Mika Tuupola <tuupola@appelsiini.net>
     * @access public
     * @static
     */
    function &mapTreeMultiple($directory, $maxrecursion = 0, $count = 0)
    {
        $retval = array();

        $count++;

        /* strip trailing slashes */
        $directory = preg_replace('![\\\\/]+$!', '', $directory);

        if (is_readable($directory)) {
            $dh = opendir($directory);
            while (false !== ($entry = @readdir($dh))) {
                if ($entry != '.' && $entry != '..') {
                     array_push($retval, $entry);
                }
            }
            closedir($dh);
        }

        while (list($key, $val) = each($retval)) {
            $path = $directory . "/" . $val;

            if (!is_array($val) && is_dir($path)) {
                unset($retval[$key]);
                if ($maxrecursion == 0 || $count < $maxrecursion) {
                    $retval[$val] = &File_Find::mapTreeMultiple($path,
                                    $maxrecursion, $count);
                }
            }
        }

        return $retval;
    }

    /**
     * Search the specified directory tree with the specified pattern.  Return
     * an array containing all matching files (no directories included).
     *
     * @param string $pattern the pattern to match every file with.
     *
     * @param string $directory the directory tree to search in.
     *
     * @param string $type the type of regular expression support to use, either
     * 'php', 'perl' or 'shell'.
     *
     * @param bool $fullpath whether the regex should be matched against the
     * full path or only against the filename
     *
     * @param string $match can be either 'files', 'dirs' or 'both' to specify
     * the kind of list to return
     *
     * @return array a list of files matching the pattern parameter in the the
     * directory path specified by the directory parameter
     *
     * @author Sterling Hughes <sterling@php.net>
     * @access public
     * @static
     */
    function &search($pattern, $directory, $type = 'php', $fullpath = true, $match = 'files')
    {

        $matches = array();
        list ($directories,$files)  = File_Find::maptree($directory);
        switch($match) {
            case 'directories':
                $data = $directories;
                break;
            case 'both':
                $data = array_merge($directories, $files);
                break;
            case 'files':
            default:
                $data = $files;
        }
        unset($files, $directories);

        $match_function = File_Find::_determineRegex($pattern, $type);

        reset($data);
        // check if empty string given (ok for 'shell' method, but bad for others)
        if ($pattern || ($type != 'php' && $type != 'perl')) {
            while (list(,$entry) = each($data)) {
                if ($match_function($pattern,
                                    $fullpath ? $entry : basename($entry))) {
                    $matches[] = $entry;
                }
            }
        }

        return $matches;
    }

    /**
     * Determine whether or not a variable is a PEAR error
     *
     * @param object PEAR_Error $var the variable to test.
     *
     * @return boolean returns true if the variable is a PEAR error, otherwise
     * it returns false.
     * @access public
     */
    function isError(&$var)
    {
        return PEAR::isError($var);
    }

    /**
     * internal function to build singular directory trees, used by
     * File_Find::maptree()
     *
     * @param string $directory name of the directory to read
     * @param string $separator directory separator
     * @return void
     */
    function _build($directory, $separator = "/")
    {

        $dh = @opendir($directory);

        if (!$dh) {
            $pe = PEAR::raiseError("Cannot open directory");
            return $pe;
        }

        while (false !== ($entry = @readdir($dh))) {
            if ($entry != '.' && $entry != '..') {

                $entry = $directory.$separator.$entry;

                if (is_dir($entry)) {
                    array_push($this->_dirs, $entry);
                } else {
                    array_push($this->files, $entry);
                }
            }
        }

        @closedir($dh);
    }

    /**
     * internal function to determine the type of regular expression to
     * use, implemented by File_Find::glob() and File_Find::search()
     *
     * @param string $type given RegExp type
     * @return string kind of function ( "eregi", "ereg" or "preg_match") ;
     *
     */
    function _determineRegex($pattern, $type)
    {
        if (!strcasecmp($type, 'shell')) {
            $match_function = 'File_Find_match_shell';
        } else if (!strcasecmp($type, 'perl')) {
            $match_function = 'preg_match';
        } else if (!strcasecmp(substr($pattern, -2), '/i')) {
            $match_function = 'eregi';
        } else {
            $match_function = 'ereg';
        }
        return $match_function;
    }

}

/**
* Package method to match via 'shell' pattern. Provided in global
* scope, because it should be called like 'preg_match' and 'eregi'
* and can be easily copied into other packages
*
* @author techtonik <techtonik@php.net>
* @return mixed bool on success and PEAR_Error on failure
*/
function File_Find_match_shell($pattern, $filename)
{
    // {{{ convert pattern to positive and negative regexps
        $positive = $pattern;
        $negation = substr_count($pattern, "|");

        if ($negation > 1) {
            PEAR::raiseError("Mask string contains errors!");
            return FALSE;
        } elseif ($negation) {
            list($positive, $negative) = explode("|", $pattern);
            if (strlen($negative) == 0) {
                PEAR::raiseError("File-mask string contains errors!");
                return FALSE;
            }
        }

       $positive = _File_Find_match_shell_get_pattern($positive);
       if ($negation) {
           $negative = _File_Find_match_shell_get_pattern($negative);
       }
    // }}} convert end


    if (defined("FILE_FIND_DEBUG")) {
        print("Method: $type\nPattern: $pattern\n Converted pattern:");
        print_r($positive);
        if (isset($negative)) print_r($negative);
    }

    if (!preg_match($positive, $filename)) {
        return FALSE;
    } else {
        if (isset($negative)
              && preg_match($negative, $filename)) {
            return FALSE;
        } else {
            return TRUE;
        }
    }
}

/**
* function used by File_Find_match_shell to convert 'shell' mask
* into pcre regexp. Some of the rules (see testcases for more):
*  escaping all special chars and replacing
*    . with \.
*    * with .*
*    ? with .{1}
*    also adding ^ and $ as the pattern matches whole filename
*
* @author techtonik <techtonik@php.net>
* @return string pcre regexp for preg_match
*/
function _File_Find_match_shell_get_pattern($mask) {
    // get array of several masks (if any) delimited by comma
    // do not touch commas in char class
    $premasks = preg_split("|(\[[^\]]+\])|", $mask, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
    if (defined("FILE_FIND_DEBUG")) {
        print("\nPremask: ");
        print_r($premasks);
    }
    $pi = 0;
    foreach($premasks as $pm) {
        if (!isset($masks[$pi])) $masks[$pi] = "";
        if ($pm{0} == '[' && $pm{strlen($pm)-1} == ']') {
            // strip commas from character class
            $masks[$pi] .= str_replace(",", "", $pm);
        } else {
            $tarr = explode(",", $pm);
            if (sizeof($tarr) == 1) {
                $masks[$pi] .= $pm;
            } else {
                foreach ($tarr as $te) {
                    $masks[$pi++] .= $te;
                    $masks[$pi] = "";
                }
                unset($masks[$pi--]);
            }
        }
    }

    // if empty string given return *.* pattern
    if (strlen($mask) == 0) return "!^.*$!";

    // convert to preg regexp
    $regexmask = implode("|", $masks);
    if (defined("FILE_FIND_DEBUG")) {
        print("regexMask step one(implode): $regexmask");
    }
    $regexmask = addcslashes($regexmask, '^$}!{)(\/.+');
    if (defined("FILE_FIND_DEBUG")) {
        print("\nregexMask step two(addcslashes): $regexmask");
    }
    $regexmask = preg_replace("!(\*|\?)!", ".$1", $regexmask);
    if (defined("FILE_FIND_DEBUG")) {
        print("\nregexMask step three(* ? -> .* .?): $regexmask");
    }
    // a special case '*.' at the end means that there is no extension
    $regexmask = preg_replace("!\.\*\\\.(\||$)!", "[^\.]*$1", $regexmask);
    // it is impossible to have dot at the end of filename
    $regexmask = preg_replace("!\\\.(\||$)!", "$1", $regexmask);
    // and .* at the end also means that there could be nothing at all
    //   (i.e. no dot at the end also)
    $regexmask = preg_replace("!\\\.\.\*(\||$)!", "(\\\\..*)?$1", $regexmask);
    if (defined("FILE_FIND_DEBUG")) {
        print("\nregexMask step two and half(*.$ \\..*$ .$ -> [^.]*$ .?.* $): $regexmask");
    }
    // if no extension supplied - add .* to match partially from filename start
    if (strpos($regexmask, "\\.") === FALSE) $regexmask .= ".*";

    // file mask match whole name - adding restrictions
    $regexmask = preg_replace("!(\|)!", '^'."$1".'$', $regexmask);
    $regexmask = '^'.$regexmask.'$';
    if (defined("FILE_FIND_DEBUG")) {
        print("\nregexMask step three(^ and $ to match whole name): $regexmask");
    }
    // wrap regex into ! since all ! are already escaped
    $regexmask = "!$regexmask!i";
    if (defined("FILE_FIND_DEBUG")) {
        print("\nWrapped regex: $regexmask\n");
    }
    return $regexmask;
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */

?>