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 seperator.
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 * On Mac it cannot be : because this is the seperator there!
232 * @return string path_separator.
234 public function _get_ini_separator()
236 return isWindows() ? ';' : ':';
237 // return preg_match('/^Windows/', php_uname())
241 * Get the value of PHP's include_path.
243 * @return array Include path.
245 public function _get_include_path()
247 if (defined("INCLUDE_PATH"))
248 $path = INCLUDE_PATH;
250 $path = @get_cfg_var('include_path'); // FIXME: report warning
251 if (empty($path)) $path = @ini_get('include_path');
255 return explode($this->_get_ini_separator(), $this->slashifyPath($path));
259 * Add a directory to the end of PHP's include_path.
261 * The directory is appended only if it is not already listed in
264 * @param $dir string Directory to add.
266 public function _append_to_include_path($dir)
268 $dir = $this->slashifyPath($dir);
269 if (!in_array($dir, $this->_path)) {
270 $this->_path[] = $dir;
273 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
274 * sometimes don't seem to heed their include_path.
275 * I.e. sometimes a file is not found even though it seems to
276 * be in the current include_path. A simple
277 * ini_set('include_path', ini_get('include_path')) seems to
278 * be enough to fix the problem
280 * This following line should be in the above if-block, but we
281 * put it here, as it seems to work-around the bug.
283 $GLOBALS['INCLUDE_PATH'] = implode($this->_get_ini_separator(), $this->_path);
284 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
288 * Add a directory to the front of PHP's include_path.
290 * The directory is prepended, and removed from the tail if already existing.
292 * @param $dir string Directory to add.
294 public function _prepend_to_include_path($dir)
296 $dir = $this->slashifyPath($dir);
298 if ($i = array_search($dir, $this->_path) !== false) {
299 array_splice($this->_path, $i, 1);
301 array_unshift($this->_path, $dir);
302 $GLOBALS['INCLUDE_PATH'] = implode($this->_path, $this->_get_ini_separator());
303 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
306 // Return all the possible shortened locale specifiers for the given locale.
307 // Most specific first.
308 // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
309 // This code might needed somewhere else also.
310 function locale_versions($lang)
312 // Try less specific versions of the locale
314 foreach (array('@', '.', '_') as $sep) {
315 if (($tail = strchr($lang, $sep)))
316 $langs[] = substr($lang, 0, -strlen($tail));
322 * Try to figure out the appropriate value for $LANG.
324 * @return string The value of $LANG.
326 public static function _get_lang()
328 if (!empty($GLOBALS['LANG']))
329 return $GLOBALS['LANG'];
331 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES') as $var) {
332 $lang = setlocale(constant($var), 0);
337 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
338 $lang = getenv($var);
348 * A class for finding PEAR code.
350 * This is a subclass of FileFinder which searches a standard list of
351 * directories where PEAR code is likely to be installed.
356 * $pearFinder = new PearFileFinder;
357 * $pearFinder->includeOnce('DB.php');
360 * The above code will look for 'DB.php', if found, the directory in
361 * which it was found will be added to PHP's include_path, and the
362 * file will be included. (If the file is not found, and E_USER_ERROR
369 * @param $path array Where to look for PEAR library code.
370 * A good set of defaults is provided, so you can probably leave
371 * this parameter blank.
373 function __construct($path = array())
375 parent::__construct(array_merge(
377 array('/usr/share/php',
379 '/usr/local/share/php',
380 '/usr/local/lib/php',
381 '/System/Library/PHP',
382 '/Apache/pear' // Windows
388 * Find PhpWiki localized files.
390 * This is a subclass of FileFinder which searches PHP's include_path
391 * for files. It looks first for "locale/$LANG/$file", then for
394 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
395 * also search under various less specific variations like
396 * "de_DE.iso8859-1", "de_DE" and "de".
398 class LocalizedFileFinder
401 function __construct()
403 $this->_pathsep = $this->_get_syspath_separator();
404 $include_path = $this->_get_include_path();
407 $lang = $this->_get_lang();
408 assert(!empty($lang));
410 if ($locales = $this->locale_versions($lang)) {
411 foreach ($locales as $lang) {
412 if ($lang == 'C') $lang = 'en';
413 foreach ($include_path as $dir) {
414 $path[] = $this->slashifyPath($dir . "/locale/$lang");
418 parent::__construct(array_merge($path, $include_path));
423 * Find PhpWiki localized theme buttons.
425 * This is a subclass of FileFinder which searches PHP's include_path
426 * for files. It looks first for "buttons/$LANG/$file", then for
429 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
430 * also search under various less specific variations like
431 * "de_DE.iso8859-1", "de_DE" and "de".
433 class LocalizedButtonFinder
436 function __construct()
439 $this->_pathsep = $this->_get_syspath_separator();
440 $include_path = $this->_get_include_path();
443 $lang = $this->_get_lang();
444 assert(!empty($lang));
445 assert(!empty($WikiTheme));
447 if (is_object($WikiTheme)) {
448 $langs = $this->locale_versions($lang);
449 foreach ($langs as $lang) {
450 if ($lang == 'C') $lang = 'en';
451 foreach ($include_path as $dir) {
452 $path[] = $this->slashifyPath($WikiTheme->file("buttons/$lang"));
457 parent::__construct(array_merge($path, $include_path));
461 // Search PHP's include_path to find file or directory.
462 function FindFile($file, $missing_okay = false, $slashify = false)
465 if (!isset($finder)) {
466 $finder = new FileFinder;
467 // remove "/lib" from dirname(__FILE__)
468 $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
469 // let the system favor its local pear?
470 $finder->_append_to_include_path(dirname(__FILE__) . "/pear");
471 $finder->_prepend_to_include_path($wikidir);
472 // Don't override existing INCLUDE_PATH config.
473 if (!defined("INCLUDE_PATH"))
474 define("INCLUDE_PATH", implode($finder->_get_ini_separator(), $finder->_path));
476 $s = $finder->findFile($file, $missing_okay);
478 $s = $finder->slashifyPath($s);
482 // Search PHP's include_path to find file or directory.
483 // Searches for "locale/$LANG/$file", then for "$file".
484 function FindLocalizedFile($file, $missing_okay = false, $re_init = false)
487 if ($re_init or !isset($finder))
488 $finder = new LocalizedFileFinder;
489 return $finder->findFile($file, $missing_okay);
492 function FindLocalizedButtonFile($file, $missing_okay = false, $re_init = false)
494 static $buttonfinder;
495 if ($re_init or !isset($buttonfinder))
496 $buttonfinder = new LocalizedButtonFinder;
497 return $buttonfinder->findFile($file, $missing_okay);
501 * Prefixes with PHPWIKI_DIR and slashify.
502 * For example to unify with
503 * require_once dirname(__FILE__).'/lib/file.php'
504 * require_once 'lib/file.php' loading style.
505 * Doesn't expand "~" or symlinks yet. truename would be perfect.
507 * NormalizeLocalFileName("lib/config.php") => /home/user/phpwiki/lib/config.php
509 function NormalizeLocalFileName($file)
512 if (!isset($finder)) {
513 $finder = new FileFinder;
515 // remove "/lib" from dirname(__FILE__)
516 if ($finder->_is_abs($file))
517 return $finder->slashifyPath($file);
519 if (defined("PHPWIKI_DIR")) $wikidir = PHPWIKI_DIR;
520 else $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
521 $wikidir = $finder->_strip_last_pathchar($wikidir);
522 $pathsep = $finder->_use_path_separator($wikidir);
523 return $finder->slashifyPath($wikidir . $pathsep . $file);
524 // return PHPWIKI_DIR . "/" . $file;
529 * Prefixes with DATA_PATH and slashify
531 function NormalizeWebFileName($file)
534 if (!isset($finder)) {
535 $finder = new FileFinder;
537 if (defined("DATA_PATH")) {
538 $wikipath = DATA_PATH;
539 $wikipath = $finder->_strip_last_pathchar($wikipath);
541 return $finder->forcePathSlashes($wikipath);
543 return $finder->forcePathSlashes($wikipath . '/' . $file);
545 return $finder->forcePathSlashes($file);
552 if (isset($win)) return $win;
553 //return preg_match('/^Windows/', php_uname());
554 $win = (substr(PHP_OS, 0, 3) == 'WIN');
558 function isWindows95()
561 if (isset($win95)) return $win95;
562 $win95 = isWindows() and !isWindowsNT();
566 function isWindowsNT()
569 if (isset($winnt)) return $winnt;
570 // FIXME: Do this using PHP_OS instead of php_uname().
571 // $winnt = (PHP_OS == "WINNT"); // example from http://www.php.net/manual/en/ref.readline.php
572 if (function_usable('php_uname'))
573 $winnt = preg_match('/^Windows NT/', php_uname());
575 $winnt = false; // FIXME: punt.
583 // c-hanging-comment-ender-p: nil
584 // indent-tabs-mode: nil