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)
53 * @return mixed|string
55 function slashifyPath($path)
57 return $this->forcePathSlashes($path, $this->_pathsep);
61 * Force using '/' as path separator.
65 * @return mixed|string
67 function forcePathSlashes($path, $sep = '/')
69 if (is_array($path)) {
71 foreach ($path as $dir) {
72 $result[] = $this->forcePathSlashes($dir, $sep);
76 if (isWindows() or $this->_isOtherPathsep()) {
77 if (isWindows()) $from = "\\";
79 // PHP is stupid enough to use \\ instead of \
81 if (substr($path, 0, 2) != '\\\\')
82 $path = str_replace('\\\\', '\\', $path);
84 $path = '\\\\' . str_replace('\\\\', '\\', substr($path, 2));
86 return strtr($path, $from, $sep);
93 * Try to include file.
95 * If file is found in the path, then the files directory is added
96 * to PHP's include_path (if it's not already there.) Then the
97 * file is include_once()'d.
99 * @param string $file File to include.
100 * @return bool True if file was successfully included.
102 function includeOnce($file)
104 if (($ret = @include_once($file)))
107 if (!$this->_is_abs($file)) {
108 if (($dir = $this->_search_path($file)) && is_file($dir . $this->_pathsep . $file)) {
109 $this->_append_to_include_path($dir);
110 return include_once($file);
113 return $this->_not_found($file);
116 function _isOtherPathsep()
118 return $this->_pathsep != '/';
122 * The system-dependent path-separator character.
123 * UNIX,WindowsNT,MacOSX: /
127 * @return string path_separator.
129 public function _get_syspath_separator()
131 if (!empty($this->_pathsep)) return $this->_pathsep;
132 elseif (isWindowsNT()) return "/"; // we can safely use '/'
133 elseif (isWindows()) return "\\"; // FAT might use '\'
134 // VMS or LispM is really weird, we ignore it.
139 * The path-separator character of the given path.
140 * Windows accepts "/" also, but gets confused with mixed path_separators,
141 * e.g "C:\Apache\phpwiki/locale/button"
142 * > dir "C:\Apache\phpwiki/locale/button" =>
143 * Parameterformat nicht korrekt - "locale"
144 * So if there's any '\' in the path, either fix them to '/' (not in Win95 or FAT?)
145 * or use '\' for ours.
147 * @param string $path
148 * @return string path_separator.
150 public function _use_path_separator($path)
153 if (empty($path)) return "\\";
154 else return (strchr($path, "\\")) ? "\\" : '/';
156 return $this->_get_syspath_separator();
161 * Determine if path is absolute.
163 * @param $path string Path.
164 * @return bool True if path is absolute.
166 public function _is_abs($path)
168 if (substr($path, 0, 1) == '/') {
170 } elseif (isWindows() and preg_match("/^[a-z]:/i", $path)
171 and (substr($path, 2, 1) == "/" or substr($path, 2, 1) == "\\")
180 * Strip ending '/' or '\' from path.
182 * @param $path string Path.
183 * @return bool New path (destructive)
185 public function _strip_last_pathchar(&$path)
187 if (substr($path, -1) == '/' or substr($path, -1) == "\\")
188 $path = substr($path, 0, -1);
193 * Report a "file not found" error.
195 * @param $file string Name of missing file.
196 * @return bool false.
198 private function _not_found($file)
200 trigger_error(sprintf(_("File ā%sā not found."), $file), E_USER_ERROR);
205 * Search our path for a file.
207 * @param $file string File to find.
208 * @return string Directory which contains $file, or false.
211 private function _search_path($file)
213 foreach ($this->_path as $dir) {
214 // ensure we use the same pathsep
215 if ($this->_isOtherPathsep()) {
216 $dir = $this->slashifyPath($dir);
217 $file = $this->slashifyPath($file);
218 if (file_exists($dir . $this->_pathsep . $file))
220 } elseif (@file_exists($dir . $this->_pathsep . $file))
227 * The system-dependent path-separator character. On UNIX systems,
228 * this character is ':'; on Win32 systems it is ';'.
230 * @return string path_separator.
232 public function _get_ini_separator()
234 return isWindows() ? ';' : ':';
238 * Get the value of PHP's include_path.
240 * @return array Include path.
242 public function _get_include_path()
244 if (defined("INCLUDE_PATH"))
245 $path = INCLUDE_PATH;
247 $path = @get_cfg_var('include_path'); // FIXME: report warning
248 if (empty($path)) $path = @ini_get('include_path');
252 return explode($this->_get_ini_separator(), $this->slashifyPath($path));
256 * Add a directory to the end of PHP's include_path.
258 * The directory is appended only if it is not already listed in
261 * @param $dir string Directory to add.
263 public function _append_to_include_path($dir)
265 $dir = $this->slashifyPath($dir);
266 if (!in_array($dir, $this->_path)) {
267 $this->_path[] = $dir;
270 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
271 * sometimes don't seem to heed their include_path.
272 * I.e. sometimes a file is not found even though it seems to
273 * be in the current include_path. A simple
274 * ini_set('include_path', ini_get('include_path')) seems to
275 * be enough to fix the problem
277 * This following line should be in the above if-block, but we
278 * put it here, as it seems to work-around the bug.
280 $GLOBALS['INCLUDE_PATH'] = implode($this->_get_ini_separator(), $this->_path);
281 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
285 * Add a directory to the front of PHP's include_path.
287 * The directory is prepended, and removed from the tail if already existing.
289 * @param $dir string Directory to add.
291 public function _prepend_to_include_path($dir)
293 $dir = $this->slashifyPath($dir);
295 if ($i = array_search($dir, $this->_path) !== false) {
296 array_splice($this->_path, $i, 1);
298 array_unshift($this->_path, $dir);
299 $GLOBALS['INCLUDE_PATH'] = implode($this->_path, $this->_get_ini_separator());
300 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
303 // Return all the possible shortened locale specifiers for the given locale.
304 // Most specific first.
305 // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
306 // This code might needed somewhere else also.
307 function locale_versions($lang)
309 // Try less specific versions of the locale
311 foreach (array('@', '.', '_') as $sep) {
312 if (($tail = strchr($lang, $sep)))
313 $langs[] = substr($lang, 0, -strlen($tail));
319 * Try to figure out the appropriate value for $LANG.
321 * @return string The value of $LANG.
323 public static function _get_lang()
325 if (!empty($GLOBALS['LANG']))
326 return $GLOBALS['LANG'];
328 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES') as $var) {
329 $lang = setlocale(constant($var), 0);
334 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
335 $lang = getenv($var);
345 * A class for finding PEAR code.
347 * This is a subclass of FileFinder which searches a standard list of
348 * directories where PEAR code is likely to be installed.
353 * $pearFinder = new PearFileFinder;
354 * $pearFinder->includeOnce('DB.php');
357 * The above code will look for 'DB.php', if found, the directory in
358 * which it was found will be added to PHP's include_path, and the
359 * file will be included. (If the file is not found, and E_USER_ERROR
366 * @param $path array Where to look for PEAR library code.
367 * A good set of defaults is provided, so you can probably leave
368 * this parameter blank.
370 function __construct($path = array())
372 parent::__construct(array_merge(
374 array('/usr/share/php',
376 '/usr/local/share/php',
377 '/usr/local/lib/php',
378 '/System/Library/PHP',
379 '/Apache/pear' // Windows
385 * Find PhpWiki localized files.
387 * This is a subclass of FileFinder which searches PHP's include_path
388 * for files. It looks first for "locale/$LANG/$file", then for
391 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
392 * also search under various less specific variations like
393 * "de_DE.iso8859-1", "de_DE" and "de".
395 class LocalizedFileFinder
398 function __construct()
400 $this->_pathsep = $this->_get_syspath_separator();
401 $include_path = $this->_get_include_path();
404 $lang = $this->_get_lang();
405 assert(!empty($lang));
407 if ($locales = $this->locale_versions($lang)) {
408 foreach ($locales as $lang) {
409 if ($lang == 'C') $lang = 'en';
410 foreach ($include_path as $dir) {
411 $path[] = $this->slashifyPath($dir . "/locale/$lang");
415 parent::__construct(array_merge($path, $include_path));
420 * Find PhpWiki localized theme buttons.
422 * This is a subclass of FileFinder which searches PHP's include_path
423 * for files. It looks first for "buttons/$LANG/$file", then for
426 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
427 * also search under various less specific variations like
428 * "de_DE.iso8859-1", "de_DE" and "de".
430 class LocalizedButtonFinder
433 function __construct()
436 $this->_pathsep = $this->_get_syspath_separator();
437 $include_path = $this->_get_include_path();
440 $lang = $this->_get_lang();
441 assert(!empty($lang));
442 assert(!empty($WikiTheme));
444 if (is_object($WikiTheme)) {
445 $langs = $this->locale_versions($lang);
446 foreach ($langs as $lang) {
447 if ($lang == 'C') $lang = 'en';
448 foreach ($include_path as $dir) {
449 $path[] = $this->slashifyPath($WikiTheme->file("buttons/$lang"));
454 parent::__construct(array_merge($path, $include_path));
458 // Search PHP's include_path to find file or directory.
459 function FindFile($file, $missing_okay = false, $slashify = false)
462 if (!isset($finder)) {
463 $finder = new FileFinder;
464 // remove "/lib" from dirname(__FILE__)
465 $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
466 // let the system favor its local pear?
467 $finder->_append_to_include_path(dirname(__FILE__) . "/pear");
468 $finder->_prepend_to_include_path($wikidir);
469 // Don't override existing INCLUDE_PATH config.
470 if (!defined("INCLUDE_PATH"))
471 define("INCLUDE_PATH", implode($finder->_get_ini_separator(), $finder->_path));
473 $s = $finder->findFile($file, $missing_okay);
475 $s = $finder->slashifyPath($s);
479 // Search PHP's include_path to find file or directory.
480 // Searches for "locale/$LANG/$file", then for "$file".
481 function FindLocalizedFile($file, $missing_okay = false, $re_init = false)
484 if ($re_init or !isset($finder))
485 $finder = new LocalizedFileFinder;
486 return $finder->findFile($file, $missing_okay);
489 function FindLocalizedButtonFile($file, $missing_okay = false, $re_init = false)
491 static $buttonfinder;
492 if ($re_init or !isset($buttonfinder))
493 $buttonfinder = new LocalizedButtonFinder;
494 return $buttonfinder->findFile($file, $missing_okay);
498 * Prefixes with PHPWIKI_DIR and slashify.
499 * For example to unify with
500 * require_once dirname(__FILE__).'/lib/file.php'
501 * require_once 'lib/file.php' loading style.
502 * Doesn't expand "~" or symlinks yet. truename would be perfect.
504 * NormalizeLocalFileName("lib/config.php") => /home/user/phpwiki/lib/config.php
506 function NormalizeLocalFileName($file)
509 if (!isset($finder)) {
510 $finder = new FileFinder;
512 // remove "/lib" from dirname(__FILE__)
513 if ($finder->_is_abs($file))
514 return $finder->slashifyPath($file);
516 if (defined("PHPWIKI_DIR")) $wikidir = PHPWIKI_DIR;
517 else $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
518 $wikidir = $finder->_strip_last_pathchar($wikidir);
519 $pathsep = $finder->_use_path_separator($wikidir);
520 return $finder->slashifyPath($wikidir . $pathsep . $file);
521 // return PHPWIKI_DIR . "/" . $file;
526 * Prefixes with DATA_PATH and slashify
528 function NormalizeWebFileName($file)
531 if (!isset($finder)) {
532 $finder = new FileFinder;
534 if (defined("DATA_PATH")) {
535 $wikipath = DATA_PATH;
536 $wikipath = $finder->_strip_last_pathchar($wikipath);
538 return $finder->forcePathSlashes($wikipath);
540 return $finder->forcePathSlashes($wikipath . '/' . $file);
542 return $finder->forcePathSlashes($file);
549 if (isset($win)) return $win;
550 //return preg_match('/^Windows/', php_uname());
551 $win = (substr(PHP_OS, 0, 3) == 'WIN');
555 function isWindows95()
558 if (isset($win95)) return $win95;
559 $win95 = isWindows() and !isWindowsNT();
563 function isWindowsNT()
566 if (isset($winnt)) return $winnt;
567 // FIXME: Do this using PHP_OS instead of php_uname().
568 // $winnt = (PHP_OS == "WINNT"); // example from http://www.php.net/manual/en/ref.readline.php
569 if (function_usable('php_uname'))
570 $winnt = preg_match('/^Windows NT/', php_uname());
572 $winnt = false; // FIXME: punt.
580 // c-hanging-comment-ender-p: nil
581 // indent-tabs-mode: nil