home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / File / Find.php next >
PHP Script  |  2001-11-13  |  8KB  |  270 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@php.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Find.php,v 1.14.2.3 2001/11/13 01:26:45 ssb Exp $
  20. //
  21.  
  22. require_once 'PEAR.php';
  23.  
  24. /**
  25. *  Commonly needed functions searching directory trees
  26. *
  27. * @access public
  28. * @version $Id: Find.php,v 1.14.2.3 2001/11/13 01:26:45 ssb Exp $
  29. * @package File
  30. * @author Sterling Hughes <sterling@php.net>
  31. */
  32. class File_Find
  33. {
  34.     /**
  35.     * internal dir-list
  36.     * @var array
  37.     */
  38.     var $_dirs       = array ();
  39.     /**
  40.     * founded files
  41.     * @var array
  42.     */
  43.     var $files       = array ();
  44.     /**
  45.     * founded dirs
  46.     * @var array
  47.     */
  48.     var $directories = array ();
  49.  
  50.     /**
  51.      * Search the current directory to find matches for the
  52.      * the specified pattern.
  53.      *
  54.      * @param string $pattern a string containing the pattern to search
  55.      * the directory for.
  56.      *
  57.      * @param string $direct_path a string containing the directory path
  58.      * to search.
  59.      *
  60.      * @param string $pattern_type a string containing the type of
  61.      * pattern matching functions to use (can either be 'php' or
  62.      * 'perl').
  63.      *
  64.      * @return array containing all of the files and directories
  65.      * matching the pattern or null if no matches
  66.      *
  67.      * @author Sterling Hughes <sterling@php.net>
  68.      * @access public
  69.      */
  70.     function &glob ($pattern, $dirpath, $pattern_type='php')
  71.     {
  72.         $dh = @opendir ($dirpath);
  73.  
  74.         if (!$dh) {
  75.             $pe = new FileFindException("Cannot open directory");
  76.             return ($pe);
  77.         }
  78.  
  79.         $match_function = File_Find::_determineRegex($pattern, $pattern_type);
  80.         $matches = array();
  81.         while ($entry = @readdir ($dh)) {
  82.             if ($match_function($pattern, $entry) &&
  83.                 $entry != '.'                     &&
  84.                 $entry != '..') {
  85.                 $matches[] = $entry;
  86.             }
  87.         }
  88.  
  89.         @closedir ($dh);
  90.         return count($matches) > 0 ? $matches : null;
  91.     }
  92.  
  93.     /**
  94.      * Map the directory tree given by the directory_path parameter.
  95.      *
  96.      * @param string $directory_path contains the directory path that you
  97.      * want to map.
  98.      *
  99.      * @return array a two element array, the first element containing a list
  100.      * of all the directories, the second element containing a list of all the
  101.      * files.
  102.      *
  103.      * @author Sterling Hughes <sterling@php.net>
  104.      * @access public
  105.      */
  106.     function &maptree ($directory)
  107.     {
  108.         $this->_dirs = array($directory);
  109.  
  110.         while (count($this->_dirs)) {
  111.             $dir = array_pop($this->_dirs);
  112.             File_Find::_build($dir);
  113.             array_push($this->directories, $dir);
  114.         }
  115.  
  116.         return array($this->directories, $this->files);
  117.     }
  118.  
  119.     /**
  120.      * Search the specified directory tree with the specified pattern.  Return an
  121.      * array containing all matching files (no directories included).
  122.      *
  123.      * @param string $pattern the pattern to match every file with.
  124.      *
  125.      * @param string $directory the directory tree to search in.
  126.      *
  127.      * @param string $regex_type the type of regular expression support to use, either
  128.      * 'php' or 'perl'.
  129.      *
  130.      * @return array a list of files matching the pattern parameter in the the directory
  131.      * path specified by the directory parameter
  132.      *
  133.      * @author Sterling Hughes <sterling@php.net>
  134.      * @access public
  135.      */
  136.     function &search ($pattern, $directory, $type='php') {
  137.         list (,$files)  = File_Find::maptree($directory);
  138.         $match_function = File_Find::_determineRegex($pattern, $type);
  139.  
  140.         reset($files);
  141.         while (list(,$entry) = each($files)) {
  142.             if ($match_function($pattern, $entry))
  143.                 $matches[] = $entry;
  144.         }
  145.  
  146.         return ($matches);
  147.     }
  148.     /**
  149.      * Determine whether or not a variable is a PEAR exception
  150.      *
  151.      * @param object PEAR_Error $var the variable to test.
  152.      *
  153.      * @return boolean returns true if the variable is a PEAR error, otherwise
  154.      * it returns false.
  155.      * @access public
  156.      */
  157.     function isError (&$var)
  158.     {
  159.         return PEAR::isError($var);
  160.     }
  161.  
  162.     /**
  163.      * Fetch the current File_Find version
  164.      *
  165.      * @return string the current File_Find version.
  166.      * @access public
  167.      */
  168.     function File_Find_version()
  169.     {
  170.          return 1.1;
  171.     }
  172.     /**
  173.      * internal function to build singular directory trees, used by
  174.      * File_Find::maptree()
  175.      *
  176.      * @param string $directory name of the directory to read
  177.      * @return void
  178.      */
  179.     function _build ($directory)
  180.     {
  181.         $dh = @opendir ($directory);
  182.  
  183.         if (!$dh) {
  184.             $pe = new FileFindException("Cannot open directory");
  185.             return $pe;
  186.         }
  187.  
  188.         while ($entry = @readdir($dh)) {
  189.             if ($entry != '.' &&
  190.                 $entry != '..') {
  191.  
  192.                 $entry = "$directory/$entry";
  193.  
  194.                 if (is_dir($entry))
  195.                     array_push($this->_dirs, $entry);
  196.                 else
  197.                     array_push($this->files, $entry);
  198.  
  199.             }
  200.  
  201.         }
  202.  
  203.         @closedir($dh);
  204.     }
  205.  
  206.     /**
  207.      * internal function to determine the type of regular expression to
  208.      * use, implemented by File_Find::glob() and File_Find::search()
  209.      *
  210.      * @param string $type given RegExp type
  211.      * @return string kind of function ( "eregi", "ereg" or "preg_match") ;
  212.      *
  213.      */
  214.     function _determineRegex ($pattern, $type)
  215.     {
  216.         if (! strcasecmp($type, 'perl')) {
  217.             $match_function = 'preg_match';
  218.         } else if (! strcasecmp(substr($pattern, -2), '/i')) {
  219.             $match_function = 'eregi';
  220.         } else {
  221.             $match_function = 'ereg';
  222.         }
  223.  
  224.         return $match_function;
  225.     }
  226.  
  227. //End Class
  228. }
  229. /**
  230. * Exception Class for Errorhandling of File_Find
  231. * @access public
  232. */
  233. class FileFindException extends PEAR_Error
  234. {
  235.     /**
  236.     * classname
  237.     * @var string
  238.     */
  239.     var $classname             = 'FileFindException';
  240.     /**
  241.     * Message in front of the error message
  242.     * @var string
  243.     */
  244.     var $error_message_prepend = 'Error in File_Find';
  245.     /**
  246.     * Creates a PEAR_Error object
  247.     *
  248.     * @param string $message    Error message
  249.     * @param int    $mode       Error mode
  250.     * @param int    $level      Error level
  251.     *
  252.     * @return object PEAR_Error
  253.     * @access public
  254.     */
  255.     function FileFindException ($message, $mode = PEAR_ERROR_RETURN, $level = E_USER_NOTICE)
  256.     {
  257.         $this->PEAR_Error($message, $mode, $level);
  258.     }
  259. }
  260.  
  261.  
  262. /*
  263.  * Local variables:
  264.  * tab-width: 4
  265.  * c-basic-offset: 4
  266.  * End:
  267.  */
  268.  
  269. ?>
  270.