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: /
122 * @return string path_separator.
124 private function _get_syspath_separator()
126 if (!empty($this->_pathsep)) return $this->_pathsep;
127 elseif (isWindowsNT()) return "/"; // we can safely use '/'
128 elseif (isWindows()) return "\\"; // FAT might use '\'
129 // VMS or LispM is really weird, we ignore it.
134 * The path-separator character of the given path.
135 * Windows accepts "/" also, but gets confused with mixed path_separators,
136 * e.g "C:\Apache\phpwiki/locale/button"
137 * > dir "C:\Apache\phpwiki/locale/button" =>
138 * Parameterformat nicht korrekt - "locale"
139 * So if there's any '\' in the path, either fix them to '/' (not in Win95 or FAT?)
140 * or use '\' for ours.
142 * @param string $path
143 * @return string path_separator.
145 public function _use_path_separator($path)
148 if (empty($path)) return "\\";
149 else return (strchr($path, "\\")) ? "\\" : '/';
151 return $this->_get_syspath_separator();
156 * Determine if path is absolute.
158 * @param $path string Path.
159 * @return bool True if path is absolute.
161 public function _is_abs($path)
163 if (substr($path, 0, 1) == '/') {
165 } elseif (isWindows() and preg_match("/^[a-z]:/i", $path)
166 and (substr($path, 2, 1) == "/" or substr($path, 2, 1) == "\\")
175 * Strip ending '/' or '\' from path.
177 * @param $path string Path.
178 * @return bool New path (destructive)
180 public function _strip_last_pathchar(&$path)
182 if (substr($path, -1) == '/' or substr($path, -1) == "\\")
183 $path = substr($path, 0, -1);
188 * Report a "file not found" error.
190 * @param $file string Name of missing file.
191 * @return bool false.
193 private function _not_found($file)
195 trigger_error(sprintf(_("%s: file not found"), $file), E_USER_ERROR);
200 * Search our path for a file.
202 * @param $file string File to find.
203 * @return string Directory which contains $file, or false.
206 private function _search_path($file)
208 foreach ($this->_path as $dir) {
209 // ensure we use the same pathsep
210 if ($this->_isOtherPathsep()) {
211 $dir = $this->slashifyPath($dir);
212 $file = $this->slashifyPath($file);
213 if (file_exists($dir . $this->_pathsep . $file))
215 } elseif (@file_exists($dir . $this->_pathsep . $file))
222 * The system-dependent path-separator character. On UNIX systems,
223 * this character is ':'; on Win32 systems it is ';'.
225 * On Mac it cannot be : because this is the seperator there!
227 * @return string path_separator.
229 public function _get_ini_separator()
231 return isWindows() ? ';' : ':';
232 // return preg_match('/^Windows/', php_uname())
236 * Get the value of PHP's include_path.
238 * @return array Include path.
240 private function _get_include_path()
242 if (defined("INCLUDE_PATH"))
243 $path = INCLUDE_PATH;
245 $path = @get_cfg_var('include_path'); // FIXME: report warning
246 if (empty($path)) $path = @ini_get('include_path');
250 return explode($this->_get_ini_separator(), $this->slashifyPath($path));
254 * Add a directory to the end of PHP's include_path.
256 * The directory is appended only if it is not already listed in
259 * @param $dir string Directory to add.
261 public function _append_to_include_path($dir)
263 $dir = $this->slashifyPath($dir);
264 if (!in_array($dir, $this->_path)) {
265 $this->_path[] = $dir;
268 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
269 * sometimes don't seem to heed their include_path.
270 * I.e. sometimes a file is not found even though it seems to
271 * be in the current include_path. A simple
272 * ini_set('include_path', ini_get('include_path')) seems to
273 * be enough to fix the problem
275 * This following line should be in the above if-block, but we
276 * put it here, as it seems to work-around the bug.
278 $GLOBALS['INCLUDE_PATH'] = implode($this->_get_ini_separator(), $this->_path);
279 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
283 * Add a directory to the front of PHP's include_path.
285 * The directory is prepended, and removed from the tail if already existing.
287 * @param $dir string Directory to add.
289 public function _prepend_to_include_path($dir)
291 $dir = $this->slashifyPath($dir);
293 if ($i = array_search($dir, $this->_path) !== false) {
294 array_splice($this->_path, $i, 1);
296 array_unshift($this->_path, $dir);
297 $GLOBALS['INCLUDE_PATH'] = implode($this->_path, $this->_get_ini_separator());
298 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
301 // Return all the possible shortened locale specifiers for the given locale.
302 // Most specific first.
303 // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
304 // This code might needed somewhere else also.
305 function locale_versions($lang)
307 // Try less specific versions of the locale
309 foreach (array('@', '.', '_') as $sep) {
310 if (($tail = strchr($lang, $sep)))
311 $langs[] = substr($lang, 0, -strlen($tail));
317 * Try to figure out the appropriate value for $LANG.
319 * @return string The value of $LANG.
321 private function _get_lang()
323 if (!empty($GLOBALS['LANG']))
324 return $GLOBALS['LANG'];
326 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES') as $var) {
327 $lang = setlocale(constant($var), 0);
332 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
333 $lang = getenv($var);
343 * A class for finding PEAR code.
345 * This is a subclass of FileFinder which searches a standard list of
346 * directories where PEAR code is likely to be installed.
351 * $pearFinder = new PearFileFinder;
352 * $pearFinder->includeOnce('DB.php');
355 * The above code will look for 'DB.php', if found, the directory in
356 * which it was found will be added to PHP's include_path, and the
357 * 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 PearFileFinder($path = array())
372 $this->FileFinder(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
401 function LocalizedFileFinder()
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 $this->FileFinder(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
439 function LocalizedButtonFinder()
442 $this->_pathsep = $this->_get_syspath_separator();
443 $include_path = $this->_get_include_path();
446 $lang = $this->_get_lang();
447 assert(!empty($lang));
448 assert(!empty($WikiTheme));
450 if (is_object($WikiTheme)) {
451 $langs = $this->locale_versions($lang);
452 foreach ($langs as $lang) {
453 if ($lang == 'C') $lang = 'en';
454 foreach ($include_path as $dir) {
455 $path[] = $this->slashifyPath($WikiTheme->file("buttons/$lang"));
460 $this->FileFinder(array_merge($path, $include_path));
464 // Search PHP's include_path to find file or directory.
465 function FindFile($file, $missing_okay = false, $slashify = false)
468 if (!isset($finder)) {
469 $finder = new FileFinder;
470 // remove "/lib" from dirname(__FILE__)
471 $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
472 // let the system favor its local pear?
473 $finder->_append_to_include_path(dirname(__FILE__) . "/pear");
474 $finder->_prepend_to_include_path($wikidir);
475 // Don't override existing INCLUDE_PATH config.
476 if (!defined("INCLUDE_PATH"))
477 define("INCLUDE_PATH", implode($finder->_get_ini_separator(), $finder->_path));
479 $s = $finder->findFile($file, $missing_okay);
481 $s = $finder->slashifyPath($s);
485 // Search PHP's include_path to find file or directory.
486 // Searches for "locale/$LANG/$file", then for "$file".
487 function FindLocalizedFile($file, $missing_okay = false, $re_init = false)
490 if ($re_init or !isset($finder))
491 $finder = new LocalizedFileFinder;
492 return $finder->findFile($file, $missing_okay);
495 function FindLocalizedButtonFile($file, $missing_okay = false, $re_init = false)
497 static $buttonfinder;
498 if ($re_init or !isset($buttonfinder))
499 $buttonfinder = new LocalizedButtonFinder;
500 return $buttonfinder->findFile($file, $missing_okay);
504 * Prefixes with PHPWIKI_DIR and slashify.
505 * For example to unify with
506 * require_once dirname(__FILE__).'/lib/file.php'
507 * require_once 'lib/file.php' loading style.
508 * Doesn't expand "~" or symlinks yet. truename would be perfect.
510 * NormalizeLocalFileName("lib/config.php") => /home/user/phpwiki/lib/config.php
512 function NormalizeLocalFileName($file)
515 if (!isset($finder)) {
516 $finder = new FileFinder;
518 // remove "/lib" from dirname(__FILE__)
519 if ($finder->_is_abs($file))
520 return $finder->slashifyPath($file);
522 if (defined("PHPWIKI_DIR")) $wikidir = PHPWIKI_DIR;
523 else $wikidir = preg_replace('/.lib$/', '', dirname(__FILE__));
524 $wikidir = $finder->_strip_last_pathchar($wikidir);
525 $pathsep = $finder->_use_path_separator($wikidir);
526 return $finder->slashifyPath($wikidir . $pathsep . $file);
527 // return PHPWIKI_DIR . "/" . $file;
532 * Prefixes with DATA_PATH and slashify
534 function NormalizeWebFileName($file)
537 if (!isset($finder)) {
538 $finder = new FileFinder;
540 if (defined("DATA_PATH")) {
541 $wikipath = DATA_PATH;
542 $wikipath = $finder->_strip_last_pathchar($wikipath);
544 return $finder->forcePathSlashes($wikipath);
546 return $finder->forcePathSlashes($wikipath . '/' . $file);
548 return $finder->forcePathSlashes($file);
555 if (isset($win)) return $win;
556 //return preg_match('/^Windows/', php_uname());
557 $win = (substr(PHP_OS, 0, 3) == 'WIN');
561 function isWindows95()
564 if (isset($win95)) return $win95;
565 $win95 = isWindows() and !isWindowsNT();
569 function isWindowsNT()
572 if (isset($winnt)) return $winnt;
573 // FIXME: Do this using PHP_OS instead of php_uname().
574 // $winnt = (PHP_OS == "WINNT"); // example from http://www.php.net/manual/en/ref.readline.php
575 if (function_usable('php_uname'))
576 $winnt = preg_match('/^Windows NT/', php_uname());
578 $winnt = false; // FIXME: punt.
586 // c-hanging-comment-ender-p: nil
587 // indent-tabs-mode: nil