Find.php 9.5 KB

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