3 require_once(dirname(__FILE__) . '/stdlib.php');
6 * A class for finding files.
8 * This should really provided by pear. We don't want really to mess around
9 * with all the lousy systems. (WindowsNT, Win95, Mac, ...)
10 * But pear has only System and File, which do nothing.
11 * Anyway, in good PHP style we ignore the rest of the world and try to behave
12 * as on unix only. That means we use / as pathsep in all our constants.
16 public $_pathsep, $_path;
21 * @param $path array A list of directories in which to search for files.
23 function FileFinder($path = array())
25 $this->_pathsep = $this->_get_syspath_separator();
26 if (!isset($this->_path) and $path === false)
27 $path = $this->_get_include_path();
34 * @param $file string File to search for.
35 * @param bool $missing_okay
36 * @return string The filename (including path), if found, otherwise false.
38 function findFile($file, $missing_okay = false)
40 if ($this->_is_abs($file)) {
41 if (file_exists($file))
43 } elseif (($dir = $this->_search_path($file))) {
44 return $dir . $this->_use_path_separator($dir) . $file;
46 return $missing_okay ? false : $this->_not_found($file);
50 * Unify used pathsep character.
51 * Accepts array of paths also.
52 * This might not work on Windows95 or FAT volumes. (not tested)
54 function slashifyPath($path)
56 return $this->forcePathSlashes($path, $this->_pathsep);
60 * Force using '/' as path seperator.
62 function forcePathSlashes($path, $sep = '/')
64 if (is_array($path)) {
66 foreach ($path as $dir) {
67 $result[] = $this->forcePathSlashes($dir, $sep);
71 if (isWindows() or $this->_isOtherPathsep()) {
72 if (isWindows()) $from = "\\";
74 // PHP is stupid enough to use \\ instead of \
76 if (substr($path, 0, 2) != '\\\\')
77 $path = str_replace('\\\\', '\\', $path);
79 $path = '\\\\' . str_replace('\\\\', '\\', substr($path, 2));
81 return strtr($path, $from, $sep);
88 * Try to include file.
90 * If file is found in the path, then the files directory is added
91 * to PHP's include_path (if it's not already there.) Then the
92 * file is include_once()'d.
94 * @param $file string File to include.
95 * @return bool True if file was successfully included.
97 function includeOnce($file)
99 if (($ret = @include_once($file)))
102 if (!$this->_is_abs($file)) {
103 if (($dir = $this->_search_path($file)) && is_file($dir . $this->_pathsep . $file)) {
104 $this->_append_to_include_path($dir);
105 return include_once($file);
108 return $this->_not_found($file);
111 function _isOtherPathsep()
113 return $this->_pathsep != '/';
117 * The system-dependent path-separator character.
118 * UNIX,WindowsNT,MacOSX: /
123 * @return string path_separator.
125 function _get_syspath_separator()
127 if (!empty($this->_pathsep)) return $this->_pathsep;
128 elseif (isWindowsNT()) return "/"; // we can safely use '/'
129 elseif (isWindows()) return "\\"; // FAT might use '\'
130 // VMS or LispM is really weird, we ignore it.
135 * The path-separator character of the given path.
136 * Windows accepts "/" also, but gets confused with mixed path_separators,
137 * e.g "C:\Apache\phpwiki/locale/button"
138 * > dir "C:\Apache\phpwiki/locale/button" =>
139 * Parameterformat nicht korrekt - "locale"
140 * So if there's any '\' in the path, either fix them to '/' (not in Win95 or FAT?)
141 * or use '\' for ours.
143 * @param string $path
145 * @return string path_separator.
147 function _use_path_separator($path)
150 if (empty($path)) return "\\";
151 else return (strchr($path, "\\")) ? "\\" : '/';
153 return $this->_get_syspath_separator();
158 * Determine if path is absolute.
161 * @param $path string Path.
162 * @return bool True if path is absolute.
164 function _is_abs($path)
166 if (substr($path, 0, 1) == '/') {
168 } elseif (isWindows() and preg_match("/^[a-z]:/i", $path)
169 and (substr($path, 2, 1) == "/" or substr($path, 2, 1) == "\\")
178 * Strip ending '/' or '\' from path.
181 * @param $path string Path.
182 * @return bool New path (destructive)
184 function _strip_last_pathchar(&$path)
186 if (substr($path, -1) == '/' or substr($path, -1) == "\\")
187 $path = substr($path, 0, -1);
192 * Report a "file not found" error.
195 * @param $file string Name of missing file.
196 * @return bool false.
198 function _not_found($file)
200 trigger_error(sprintf(_("%s: file not found"), $file), E_USER_ERROR);
205 * Search our path for a file.
208 * @param $file string File to find.
209 * @return string Directory which contains $file, or false.
212 function _search_path($file)
214 foreach ($this->_path as $dir) {
215 // ensure we use the same pathsep
216 if ($this->_isOtherPathsep()) {
217 $dir = $this->slashifyPath($dir);
218 $file = $this->slashifyPath($file);
219 if (file_exists($dir . $this->_pathsep . $file))
221 } elseif (@file_exists($dir . $this->_pathsep . $file))
228 * The system-dependent path-separator character. On UNIX systems,
229 * this character is ':'; on Win32 systems it is ';'.
231 * On Mac it cannot be : because this is the seperator there!
234 * @return string path_separator.
236 function _get_ini_separator()
238 return isWindows() ? ';' : ':';
239 // return preg_match('/^Windows/', php_uname())
243 * Get the value of PHP's include_path.
246 * @return array Include path.
248 function _get_include_path()
250 if (defined("INCLUDE_PATH"))
251 $path = INCLUDE_PATH;
253 $path = @get_cfg_var('include_path'); // FIXME: report warning
254 if (empty($path)) $path = @ini_get('include_path');
258 return explode($this->_get_ini_separator(), $this->slashifyPath($path));
262 * Add a directory to the end of PHP's include_path.
264 * The directory is appended only if it is not already listed in
268 * @param $dir string Directory to add.
270 function _append_to_include_path($dir)
272 $dir = $this->slashifyPath($dir);
273 if (!in_array($dir, $this->_path)) {
274 $this->_path[] = $dir;
277 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
278 * sometimes don't seem to heed their include_path.
279 * I.e. sometimes a file is not found even though it seems to
280 * be in the current include_path. A simple
281 * ini_set('include_path', ini_get('include_path')) seems to
282 * be enough to fix the problem
284 * This following line should be in the above if-block, but we
285 * put it here, as it seems to work-around the bug.
287 $GLOBALS['INCLUDE_PATH'] = implode($this->_get_ini_separator(), $this->_path);
288 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
292 * Add a directory to the front of PHP's include_path.
294 * The directory is prepended, and removed from the tail if already existing.
297 * @param $dir string Directory to add.
299 function _prepend_to_include_path($dir)
301 $dir = $this->slashifyPath($dir);
303 if ($i = array_search($dir, $this->_path) !== false) {
304 array_splice($this->_path, $i, 1);
306 array_unshift($this->_path, $dir);
307 $GLOBALS['INCLUDE_PATH'] = implode($this->_path, $this->_get_ini_separator());
308 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
311 // Return all the possible shortened locale specifiers for the given locale.
312 // Most specific first.
313 // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
314 // This code might needed somewhere else also.
315 function locale_versions($lang)
317 // Try less specific versions of the locale
319 foreach (array('@', '.', '_') as $sep) {
320 if (($tail = strchr($lang, $sep)))
321 $langs[] = substr($lang, 0, -strlen($tail));
327 * Try to figure out the appropriate value for $LANG.
330 * @return string The value of $LANG.
334 if (!empty($GLOBALS['LANG']))
335 return $GLOBALS['LANG'];
337 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES') as $var) {
338 $lang = setlocale(constant($var), 0);
343 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
344 $lang = getenv($var);
354 * A class for finding PEAR code.
356 * This is a subclass of FileFinder which searches a standard list of
357 * directories where PEAR code is likely to be installed.
362 * $pearFinder = new PearFileFinder;
363 * $pearFinder->includeOnce('DB.php');
366 * The above code will look for 'DB.php', if found, the directory in
367 * which it was found will be added to PHP's include_path, and the
368 * file will be included. (If the file is not found, and E_USER_ERROR
377 * @param $path array Where to look for PEAR library code.
378 * A good set of defaults is provided, so you can probably leave
379 * this parameter blank.
381 function PearFileFinder($path = array())
383 $this->FileFinder(array_merge(
385 array('/usr/share/php',
387 '/usr/local/share/php',
388 '/usr/local/lib/php',
389 '/System/Library/PHP',
390 '/Apache/pear' // Windows
396 * Find PhpWiki localized files.
398 * This is a subclass of FileFinder which searches PHP's include_path
399 * for files. It looks first for "locale/$LANG/$file", then for
402 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
403 * also search under various less specific variations like
404 * "de_DE.iso8859-1", "de_DE" and "de".
406 class LocalizedFileFinder
412 function LocalizedFileFinder()
414 $this->_pathsep = $this->_get_syspath_separator();
415 $include_path = $this->_get_include_path();
418 $lang = $this->_get_lang();
419 assert(!empty($lang));
421 if ($locales = $this->locale_versions($lang)) {
422 foreach ($locales as $lang) {
423 if ($lang == 'C') $lang = 'en';
424 foreach ($include_path as $dir) {
425 $path[] = $this->slashifyPath($dir . "/locale/$lang");
429 $this->FileFinder(array_merge($path, $include_path));
434 * Find PhpWiki localized theme buttons.
436 * This is a subclass of FileFinder which searches PHP's include_path
437 * for files. It looks first for "buttons/$LANG/$file", then for
440 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
441 * also search under various less specific variations like
442 * "de_DE.iso8859-1", "de_DE" and "de".
444 class LocalizedButtonFinder
450 function LocalizedButtonFinder()
453 $this->_pathsep = $this->_get_syspath_separator();
454 $include_path = $this->_get_include_path();
457 $lang = $this->_get_lang();
458 assert(!empty($lang));
459 assert(!empty($WikiTheme));
461 if (is_object($WikiTheme)) {
462 $langs = $this->locale_versions($lang);
463 foreach ($langs as $lang) {
464 if ($lang == 'C') $lang = 'en';
465 foreach ($include_path as $dir) {
466 $path[] = $this->slashifyPath($WikiTheme->file("buttons/$lang"));
471 $this->FileFinder(array_merge($path, $include_path));
475 // Search PHP's include_path to find file or directory.
476 function FindFile($file, $missing_okay = false, $slashify = false)
479 if (!isset($finder)) {
480 $finder = new FileFinder;
481 // remove "/lib" from dirname(__FILE__)
482 $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
483 // let the system favor its local pear?
484 $finder->_append_to_include_path(dirname(__FILE__) . "/pear");
485 $finder->_prepend_to_include_path($wikidir);
486 // Don't override existing INCLUDE_PATH config.
487 if (!defined("INCLUDE_PATH"))
488 define("INCLUDE_PATH", implode($finder->_get_ini_separator(), $finder->_path));
490 $s = $finder->findFile($file, $missing_okay);
492 $s = $finder->slashifyPath($s);
496 // Search PHP's include_path to find file or directory.
497 // Searches for "locale/$LANG/$file", then for "$file".
498 function FindLocalizedFile($file, $missing_okay = false, $re_init = false)
501 if ($re_init or !isset($finder))
502 $finder = new LocalizedFileFinder;
503 return $finder->findFile($file, $missing_okay);
506 function FindLocalizedButtonFile($file, $missing_okay = false, $re_init = false)
508 static $buttonfinder;
509 if ($re_init or !isset($buttonfinder))
510 $buttonfinder = new LocalizedButtonFinder;
511 return $buttonfinder->findFile($file, $missing_okay);
515 * Prefixes with PHPWIKI_DIR and slashify.
516 * For example to unify with
517 * require_once dirname(__FILE__).'/lib/file.php'
518 * require_once 'lib/file.php' loading style.
519 * Doesn't expand "~" or symlinks yet. truename would be perfect.
521 * NormalizeLocalFileName("lib/config.php") => /home/user/phpwiki/lib/config.php
523 function NormalizeLocalFileName($file)
526 if (!isset($finder)) {
527 $finder = new FileFinder;
529 // remove "/lib" from dirname(__FILE__)
530 if ($finder->_is_abs($file))
531 return $finder->slashifyPath($file);
533 if (defined("PHPWIKI_DIR")) $wikidir = PHPWIKI_DIR;
534 else $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
535 $wikidir = $finder->_strip_last_pathchar($wikidir);
536 $pathsep = $finder->_use_path_separator($wikidir);
537 return $finder->slashifyPath($wikidir . $pathsep . $file);
538 // return PHPWIKI_DIR . "/" . $file;
543 * Prefixes with DATA_PATH and slashify
545 function NormalizeWebFileName($file)
548 if (!isset($finder)) {
549 $finder = new FileFinder;
551 if (defined("DATA_PATH")) {
552 $wikipath = DATA_PATH;
553 $wikipath = $finder->_strip_last_pathchar($wikipath);
555 return $finder->forcePathSlashes($wikipath);
557 return $finder->forcePathSlashes($wikipath . '/' . $file);
559 return $finder->forcePathSlashes($file);
566 if (isset($win)) return $win;
567 //return preg_match('/^Windows/', php_uname());
568 $win = (substr(PHP_OS, 0, 3) == 'WIN');
572 function isWindows95()
575 if (isset($win95)) return $win95;
576 $win95 = isWindows() and !isWindowsNT();
580 function isWindowsNT()
583 if (isset($winnt)) return $winnt;
584 // FIXME: Do this using PHP_OS instead of php_uname().
585 // $winnt = (PHP_OS == "WINNT"); // example from http://www.php.net/manual/en/ref.readline.php
586 if (function_usable('php_uname'))
587 $winnt = preg_match('/^Windows NT/', php_uname());
589 $winnt = false; // FIXME: punt.
597 // c-hanging-comment-ender-p: nil
598 // indent-tabs-mode: nil