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;
19 * @param $path array A list of directories in which to search for files.
21 function __construct($path = array())
23 $this->_pathsep = $this->_get_syspath_separator();
24 if (!isset($this->_path) and $path === false)
25 $path = $this->_get_include_path();
32 * @param $file string File to search for.
33 * @param bool $missing_okay
34 * @return string The filename (including path), if found, otherwise false.
36 function findFile($file, $missing_okay = false)
38 if ($this->_is_abs($file)) {
39 if (file_exists($file))
41 } elseif (($dir = $this->_search_path($file))) {
42 return $dir . $this->_use_path_separator($dir) . $file;
44 return $missing_okay ? false : $this->_not_found($file);
48 * Unify used pathsep character.
49 * Accepts array of paths also.
50 * This might not work on Windows95 or FAT volumes. (not tested)
52 function slashifyPath($path)
54 return $this->forcePathSlashes($path, $this->_pathsep);
58 * Force using '/' as path seperator.
60 function forcePathSlashes($path, $sep = '/')
62 if (is_array($path)) {
64 foreach ($path as $dir) {
65 $result[] = $this->forcePathSlashes($dir, $sep);
69 if (isWindows() or $this->_isOtherPathsep()) {
70 if (isWindows()) $from = "\\";
72 // PHP is stupid enough to use \\ instead of \
74 if (substr($path, 0, 2) != '\\\\')
75 $path = str_replace('\\\\', '\\', $path);
77 $path = '\\\\' . str_replace('\\\\', '\\', substr($path, 2));
79 return strtr($path, $from, $sep);
86 * Try to include file.
88 * If file is found in the path, then the files directory is added
89 * to PHP's include_path (if it's not already there.) Then the
90 * file is include_once()'d.
92 * @param $file string File to include.
93 * @return bool True if file was successfully included.
95 function includeOnce($file)
97 if (($ret = @include_once($file)))
100 if (!$this->_is_abs($file)) {
101 if (($dir = $this->_search_path($file)) && is_file($dir . $this->_pathsep . $file)) {
102 $this->_append_to_include_path($dir);
103 return include_once($file);
106 return $this->_not_found($file);
109 function _isOtherPathsep()
111 return $this->_pathsep != '/';
115 * The system-dependent path-separator character.
116 * UNIX,WindowsNT,MacOSX: /
120 * @return string path_separator.
122 private function _get_syspath_separator()
124 if (!empty($this->_pathsep)) return $this->_pathsep;
125 elseif (isWindowsNT()) return "/"; // we can safely use '/'
126 elseif (isWindows()) return "\\"; // FAT might use '\'
127 // VMS or LispM is really weird, we ignore it.
132 * The path-separator character of the given path.
133 * Windows accepts "/" also, but gets confused with mixed path_separators,
134 * e.g "C:\Apache\phpwiki/locale/button"
135 * > dir "C:\Apache\phpwiki/locale/button" =>
136 * Parameterformat nicht korrekt - "locale"
137 * So if there's any '\' in the path, either fix them to '/' (not in Win95 or FAT?)
138 * or use '\' for ours.
140 * @param string $path
141 * @return string path_separator.
143 public function _use_path_separator($path)
146 if (empty($path)) return "\\";
147 else return (strchr($path, "\\")) ? "\\" : '/';
149 return $this->_get_syspath_separator();
154 * Determine if path is absolute.
156 * @param $path string Path.
157 * @return bool True if path is absolute.
159 public function _is_abs($path)
161 if (substr($path, 0, 1) == '/') {
163 } elseif (isWindows() and preg_match("/^[a-z]:/i", $path)
164 and (substr($path, 2, 1) == "/" or substr($path, 2, 1) == "\\")
173 * Strip ending '/' or '\' from path.
175 * @param $path string Path.
176 * @return bool New path (destructive)
178 public function _strip_last_pathchar(&$path)
180 if (substr($path, -1) == '/' or substr($path, -1) == "\\")
181 $path = substr($path, 0, -1);
186 * Report a "file not found" error.
188 * @param $file string Name of missing file.
189 * @return bool false.
191 private function _not_found($file)
193 trigger_error(sprintf(_("%s: file not found"), $file), E_USER_ERROR);
198 * Search our path for a file.
200 * @param $file string File to find.
201 * @return string Directory which contains $file, or false.
204 private function _search_path($file)
206 foreach ($this->_path as $dir) {
207 // ensure we use the same pathsep
208 if ($this->_isOtherPathsep()) {
209 $dir = $this->slashifyPath($dir);
210 $file = $this->slashifyPath($file);
211 if (file_exists($dir . $this->_pathsep . $file))
213 } elseif (@file_exists($dir . $this->_pathsep . $file))
220 * The system-dependent path-separator character. On UNIX systems,
221 * this character is ':'; on Win32 systems it is ';'.
223 * On Mac it cannot be : because this is the seperator there!
225 * @return string path_separator.
227 public function _get_ini_separator()
229 return isWindows() ? ';' : ':';
230 // return preg_match('/^Windows/', php_uname())
234 * Get the value of PHP's include_path.
236 * @return array Include path.
238 private function _get_include_path()
240 if (defined("INCLUDE_PATH"))
241 $path = INCLUDE_PATH;
243 $path = @get_cfg_var('include_path'); // FIXME: report warning
244 if (empty($path)) $path = @ini_get('include_path');
248 return explode($this->_get_ini_separator(), $this->slashifyPath($path));
252 * Add a directory to the end of PHP's include_path.
254 * The directory is appended only if it is not already listed in
257 * @param $dir string Directory to add.
259 public function _append_to_include_path($dir)
261 $dir = $this->slashifyPath($dir);
262 if (!in_array($dir, $this->_path)) {
263 $this->_path[] = $dir;
266 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
267 * sometimes don't seem to heed their include_path.
268 * I.e. sometimes a file is not found even though it seems to
269 * be in the current include_path. A simple
270 * ini_set('include_path', ini_get('include_path')) seems to
271 * be enough to fix the problem
273 * This following line should be in the above if-block, but we
274 * put it here, as it seems to work-around the bug.
276 $GLOBALS['INCLUDE_PATH'] = implode($this->_get_ini_separator(), $this->_path);
277 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
281 * Add a directory to the front of PHP's include_path.
283 * The directory is prepended, and removed from the tail if already existing.
285 * @param $dir string Directory to add.
287 public function _prepend_to_include_path($dir)
289 $dir = $this->slashifyPath($dir);
291 if ($i = array_search($dir, $this->_path) !== false) {
292 array_splice($this->_path, $i, 1);
294 array_unshift($this->_path, $dir);
295 $GLOBALS['INCLUDE_PATH'] = implode($this->_path, $this->_get_ini_separator());
296 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
299 // Return all the possible shortened locale specifiers for the given locale.
300 // Most specific first.
301 // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
302 // This code might needed somewhere else also.
303 function locale_versions($lang)
305 // Try less specific versions of the locale
307 foreach (array('@', '.', '_') as $sep) {
308 if (($tail = strchr($lang, $sep)))
309 $langs[] = substr($lang, 0, -strlen($tail));
315 * Try to figure out the appropriate value for $LANG.
317 * @return string The value of $LANG.
319 private function _get_lang()
321 if (!empty($GLOBALS['LANG']))
322 return $GLOBALS['LANG'];
324 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES') as $var) {
325 $lang = setlocale(constant($var), 0);
330 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
331 $lang = getenv($var);
341 * A class for finding PEAR code.
343 * This is a subclass of FileFinder which searches a standard list of
344 * directories where PEAR code is likely to be installed.
349 * $pearFinder = new PearFileFinder;
350 * $pearFinder->includeOnce('DB.php');
353 * The above code will look for 'DB.php', if found, the directory in
354 * which it was found will be added to PHP's include_path, and the
355 * file will be included. (If the file is not found, and E_USER_ERROR
362 * @param $path array Where to look for PEAR library code.
363 * A good set of defaults is provided, so you can probably leave
364 * this parameter blank.
366 function __construct($path = array())
368 parent::__construct(array_merge(
370 array('/usr/share/php',
372 '/usr/local/share/php',
373 '/usr/local/lib/php',
374 '/System/Library/PHP',
375 '/Apache/pear' // Windows
381 * Find PhpWiki localized files.
383 * This is a subclass of FileFinder which searches PHP's include_path
384 * for files. It looks first for "locale/$LANG/$file", then for
387 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
388 * also search under various less specific variations like
389 * "de_DE.iso8859-1", "de_DE" and "de".
391 class LocalizedFileFinder
394 function __construct()
396 $this->_pathsep = $this->_get_syspath_separator();
397 $include_path = $this->_get_include_path();
400 $lang = $this->_get_lang();
401 assert(!empty($lang));
403 if ($locales = $this->locale_versions($lang)) {
404 foreach ($locales as $lang) {
405 if ($lang == 'C') $lang = 'en';
406 foreach ($include_path as $dir) {
407 $path[] = $this->slashifyPath($dir . "/locale/$lang");
411 $this->FileFinder(array_merge($path, $include_path));
416 * Find PhpWiki localized theme buttons.
418 * This is a subclass of FileFinder which searches PHP's include_path
419 * for files. It looks first for "buttons/$LANG/$file", then for
422 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
423 * also search under various less specific variations like
424 * "de_DE.iso8859-1", "de_DE" and "de".
426 class LocalizedButtonFinder
429 function __construct()
432 $this->_pathsep = $this->_get_syspath_separator();
433 $include_path = $this->_get_include_path();
436 $lang = $this->_get_lang();
437 assert(!empty($lang));
438 assert(!empty($WikiTheme));
440 if (is_object($WikiTheme)) {
441 $langs = $this->locale_versions($lang);
442 foreach ($langs as $lang) {
443 if ($lang == 'C') $lang = 'en';
444 foreach ($include_path as $dir) {
445 $path[] = $this->slashifyPath($WikiTheme->file("buttons/$lang"));
450 $this->FileFinder(array_merge($path, $include_path));
454 // Search PHP's include_path to find file or directory.
455 function FindFile($file, $missing_okay = false, $slashify = false)
458 if (!isset($finder)) {
459 $finder = new FileFinder;
460 // remove "/lib" from dirname(__FILE__)
461 $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
462 // let the system favor its local pear?
463 $finder->_append_to_include_path(dirname(__FILE__) . "/pear");
464 $finder->_prepend_to_include_path($wikidir);
465 // Don't override existing INCLUDE_PATH config.
466 if (!defined("INCLUDE_PATH"))
467 define("INCLUDE_PATH", implode($finder->_get_ini_separator(), $finder->_path));
469 $s = $finder->findFile($file, $missing_okay);
471 $s = $finder->slashifyPath($s);
475 // Search PHP's include_path to find file or directory.
476 // Searches for "locale/$LANG/$file", then for "$file".
477 function FindLocalizedFile($file, $missing_okay = false, $re_init = false)
480 if ($re_init or !isset($finder))
481 $finder = new LocalizedFileFinder;
482 return $finder->findFile($file, $missing_okay);
485 function FindLocalizedButtonFile($file, $missing_okay = false, $re_init = false)
487 static $buttonfinder;
488 if ($re_init or !isset($buttonfinder))
489 $buttonfinder = new LocalizedButtonFinder;
490 return $buttonfinder->findFile($file, $missing_okay);
494 * Prefixes with PHPWIKI_DIR and slashify.
495 * For example to unify with
496 * require_once dirname(__FILE__).'/lib/file.php'
497 * require_once 'lib/file.php' loading style.
498 * Doesn't expand "~" or symlinks yet. truename would be perfect.
500 * NormalizeLocalFileName("lib/config.php") => /home/user/phpwiki/lib/config.php
502 function NormalizeLocalFileName($file)
505 if (!isset($finder)) {
506 $finder = new FileFinder;
508 // remove "/lib" from dirname(__FILE__)
509 if ($finder->_is_abs($file))
510 return $finder->slashifyPath($file);
512 if (defined("PHPWIKI_DIR")) $wikidir = PHPWIKI_DIR;
513 else $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
514 $wikidir = $finder->_strip_last_pathchar($wikidir);
515 $pathsep = $finder->_use_path_separator($wikidir);
516 return $finder->slashifyPath($wikidir . $pathsep . $file);
517 // return PHPWIKI_DIR . "/" . $file;
522 * Prefixes with DATA_PATH and slashify
524 function NormalizeWebFileName($file)
527 if (!isset($finder)) {
528 $finder = new FileFinder;
530 if (defined("DATA_PATH")) {
531 $wikipath = DATA_PATH;
532 $wikipath = $finder->_strip_last_pathchar($wikipath);
534 return $finder->forcePathSlashes($wikipath);
536 return $finder->forcePathSlashes($wikipath . '/' . $file);
538 return $finder->forcePathSlashes($file);
545 if (isset($win)) return $win;
546 //return preg_match('/^Windows/', php_uname());
547 $win = (substr(PHP_OS, 0, 3) == 'WIN');
551 function isWindows95()
554 if (isset($win95)) return $win95;
555 $win95 = isWindows() and !isWindowsNT();
559 function isWindowsNT()
562 if (isset($winnt)) return $winnt;
563 // FIXME: Do this using PHP_OS instead of php_uname().
564 // $winnt = (PHP_OS == "WINNT"); // example from http://www.php.net/manual/en/ref.readline.php
565 if (function_usable('php_uname'))
566 $winnt = preg_match('/^Windows NT/', php_uname());
568 $winnt = false; // FIXME: punt.
576 // c-hanging-comment-ender-p: nil
577 // indent-tabs-mode: nil