1 <?php rcs_id('$Id: FileFinder.php,v 1.7 2002-01-22 03:12:59 carstenklapp Exp $');
3 // FIXME: make this work with non-unix (e.g. DOS) filenames.
6 * A class for finding files.
13 * @param $path array A list of directories in which to search for files.
15 function FileFinder ($path = false) {
17 $path = $this->_get_include_path();
24 * @param $file string File to search for.
25 * @return string The filename (including path), if found, otherwise false.
27 function findFile ($file, $missing_okay = false) {
28 if ($this->_is_abs($file)) {
29 if (file_exists($file))
32 elseif ( ($dir = $this->_search_path($file)) ) {
36 return $missing_okay ? false : $this->_not_found($file);
40 * Try to include file.
42 * If file is found in the path, then the files directory is added
43 * to PHP's include_path (if it's not already there.) Then the
44 * file is include_once()'d.
46 * @param $file string File to include.
47 * @return bool True if file was successfully included.
49 function includeOnce ($file) {
50 if ( ($ret = @include_once($file)) )
53 if (!$this->_is_abs($file)) {
54 if ( ($dir = $this->_search_path($file)) && is_file("$dir/$file")) {
55 $this->_append_to_include_path($dir);
56 return include_once($file);
60 return $this->_not_found($file);
64 * Determine if path is absolute.
67 * @param $path string Path.
68 * @return bool True iff path is absolute.
70 function _is_abs($path) {
71 return ereg('^/', $path);
75 * Report a "file not found" error.
78 * @param $file string Name of missing file.
81 function _not_found($file) {
82 trigger_error(sprintf(_("%s: file not found"),$file), E_USER_ERROR);
88 * Search our path for a file.
91 * @param $file string File to find.
92 * @return string Directory which contains $file, or false.
94 function _search_path ($file) {
95 foreach ($this->_path as $dir) {
96 if (file_exists("$dir/$file"))
103 * The system-dependent path-separator character. On UNIX systems,
104 * this character is ':'; on Win32 systems it is ';'.
107 * @return string path_separator.
109 function _get_path_separator () {
110 return preg_match('/^Windows/', php_uname()) ? ';' : ':';
114 * Get the value of PHP's include_path.
117 * @return array Include path.
119 function _get_include_path() {
120 $path = ini_get('include_path');
123 return explode($this->_get_path_separator(), $path);
127 * Add a directory to the end of PHP's include_path.
129 * The directory is appended only if it is not already listed in
133 * @param $dir string Directory to add.
135 function _append_to_include_path ($dir) {
136 $path = $this->_get_include_path();
137 if (!in_array($dir, $path)) {
139 //ini_set('include_path', implode(':', $path));
142 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
143 * sometimes don't seem to heed their include_path.
144 * I.e. sometimes a file is not found even though it seems to
145 * be in the current include_path. A simple
146 * ini_set('include_path', ini_get('include_path')) seems to
147 * be enough to fix the problem
149 * This following line should be in the above if-block, but we
150 * put it here, as it seems to work-around the bug.
152 ini_set('include_path', implode($this->_get_path_separator(), $path));
157 * A class for finding PEAR code.
159 * This is a subclass of FileFinder which searches a standard list of
160 * directories where PEAR code is likely to be installed.
165 * $pearFinder = new PearFileFinder;
166 * $pearFinder->includeOnce('DB.php');
169 * The above code will look for 'DB.php', if found, the directory in
170 * which it was found will be added to PHP's include_path, and the
171 * file will be included. (If the file is not found, and E_USER_ERROR
180 * @param $path array Where to look for PEAR library code.
181 * A good set of defaults is provided, so you can probably leave
182 * this parameter blank.
184 function PearFileFinder ($path = false) {
185 $this->FileFinder(array('/usr/share/php4',
189 '/usr/local/share/php4',
190 '/usr/local/share/php',
191 '/usr/local/lib/php4',
192 '/usr/local/lib/php',
193 '/System/Library/PHP'));
198 * Find PhpWiki localized files.
200 * This is a subclass of FileFinder which searches PHP's include_path
201 * for files. It looks first for "locale/$LANG/$file", then for
204 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
205 * also search under various less specific variations like
206 * "de_DE.iso8859-1", "de_DE" and "de".
208 class LocalizedFileFinder
214 function LocalizedFileFinder () {
215 $include_path = $this->_get_include_path();
218 $lang = $this->_get_lang();
219 assert(!empty($lang));
221 // A locale can be, e.g. de_DE.iso8859-1@euro.
222 // Try less specific versions of the locale:
224 foreach (array('@', '.', '_') as $sep) {
225 if ( ($tail = strchr($lang, $sep)) )
226 $langs[] = substr($lang, 0, -strlen($tail));
229 foreach ($langs as $lang) {
230 foreach ($include_path as $dir) {
231 $path[] = "$dir/locale/$lang";
235 $this->FileFinder(array_merge($path, $include_path));
239 * Try to figure out the appropriate value for $LANG.
242 *@return string The value of $LANG.
244 function _get_lang() {
245 if (!empty($GLOBALS['LANG']))
246 return $GLOBALS['LANG'];
248 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
249 $lang = getenv($var);
260 * Find PhpWiki localized theme buttons.
262 * This is a subclass of FileFinder which searches PHP's include_path
263 * for files. It looks first for "buttons/$LANG/$file", then for
266 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
267 * also search under various less specific variations like
268 * "de_DE.iso8859-1", "de_DE" and "de".
270 class LocalizedButtonFinder
276 function LocalizedButtonFinder () {
277 $include_path = $this->_get_include_path();
280 $lang = $this->_get_lang();
281 assert(!empty($lang));
283 // A locale can be, e.g. de_DE.iso8859-1@euro.
284 // Try less specific versions of the locale:
286 foreach (array('@', '.', '_') as $sep) {
287 if ( ($tail = strchr($lang, $sep)) )
288 $langs[] = substr($lang, 0, -strlen($tail));
291 foreach ($langs as $lang) {
292 foreach ($include_path as $dir) {
293 // FIXME: sorry I know this is ugly but don't know yet what else to do
296 $path[] = "$dir/themes/".THEME."/buttons/$lang";
301 $this->FileFinder(array_merge($path, $include_path));
305 * Try to figure out the appropriate value for $LANG.
308 *@return string The value of $LANG.
310 function _get_lang() {
311 if (!empty($GLOBALS['LANG']))
312 return $GLOBALS['LANG'];
314 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
315 $lang = getenv($var);
329 // c-hanging-comment-ender-p: nil
330 // indent-tabs-mode: nil