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 //var $_pathsep, $_path;
21 * @param $path array A list of directories in which to search for files.
23 function FileFinder ($path = false) {
24 $this->_pathsep = $this->_get_syspath_separator();
25 if (!isset($this->_path) and $path === false)
26 $path = $this->_get_include_path();
33 * @param $file string File to search for.
34 * @return string The filename (including path), if found, otherwise false.
36 function findFile ($file, $missing_okay = false) {
37 if ($this->_is_abs($file)) {
38 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) {
53 return $this->forcePathSlashes($path, $this->_pathsep);
57 * Force using '/' as path seperator.
59 function forcePathSlashes ($path, $sep='/') {
60 if (is_array($path)) {
62 foreach ($path as $dir) { $result[] = $this->forcePathSlashes($dir,$sep); }
65 if (isWindows() or $this->_isOtherPathsep()) {
66 if (isMac()) $from = ":";
67 elseif (isWindows()) $from = "\\";
69 // PHP is stupid enough to use \\ instead of \
71 if (substr($path,0,2) != '\\\\')
72 $path = str_replace('\\\\','\\',$path);
74 $path = '\\\\' . str_replace('\\\\','\\',substr($path,2));
76 return strtr($path, $from, $sep);
83 * Try to include file.
85 * If file is found in the path, then the files directory is added
86 * to PHP's include_path (if it's not already there.) Then the
87 * file is include_once()'d.
89 * @param $file string File to include.
90 * @return bool True if file was successfully included.
92 function includeOnce ($file) {
93 if ( ($ret = @include_once($file)) )
96 if (!$this->_is_abs($file)) {
97 if ( ($dir = $this->_search_path($file)) && is_file($dir . $this->_pathsep . $file)) {
98 $this->_append_to_include_path($dir);
99 return include_once($file);
102 return $this->_not_found($file);
105 function _isOtherPathsep() {
106 return $this->_pathsep != '/';
110 * The system-dependent path-separator character.
111 * UNIX,WindowsNT,MacOSX: /
116 * @return string path_separator.
118 function _get_syspath_separator () {
119 if (!empty($this->_pathsep)) return $this->_pathsep;
120 elseif (isWindowsNT()) return "/"; // we can safely use '/'
121 elseif (isWindows()) return "\\"; // FAT might use '\'
122 elseif (isMac()) return ':'; // MacOsX is /
123 // VMS or LispM is really weird, we ignore it.
128 * The path-separator character of the given path.
129 * Windows accepts "/" also, but gets confused with mixed path_separators,
130 * e.g "C:\Apache\phpwiki/locale/button"
131 * > dir "C:\Apache\phpwiki/locale/button" =>
132 * Parameterformat nicht korrekt - "locale"
133 * So if there's any '\' in the path, either fix them to '/' (not in Win95 or FAT?)
134 * or use '\' for ours.
137 * @return string path_separator.
139 function _use_path_separator ($path) {
141 if (empty($path)) return "\\";
142 else return (strchr($path,"\\")) ? "\\" : '/';
144 if (empty($path)) return ":";
145 else return (strchr($path,":")) ? ":" : '/';
147 return $this->_get_syspath_separator();
152 * Determine if path is absolute.
155 * @param $path string Path.
156 * @return bool True if path is absolute.
158 function _is_abs($path) {
159 if (ereg('^/', $path)) return true;
160 elseif (isWindows() and (eregi('^[a-z]:[/\\]', $path))) return true;
165 * Strip ending '/' or '\' from path.
168 * @param $path string Path.
169 * @return bool New path (destructive)
171 function _strip_last_pathchar(&$path) {
173 if (substr($path,-1) == ':' or substr($path,-1) == "/")
174 $path = substr($path,0,-1);
176 if (substr($path,-1) == '/' or substr($path,-1) == "\\")
177 $path = substr($path,0,-1);
183 * Report a "file not found" error.
186 * @param $file string Name of missing file.
187 * @return bool false.
189 function _not_found($file) {
190 if (function_exists("_"))
191 trigger_error(sprintf(_("%s: file not found"), $file), E_USER_ERROR);
193 trigger_error(sprintf("%s: file not found", $file), E_USER_ERROR);
199 * Search our path for a file.
202 * @param $file string File to find.
203 * @return string Directory which contains $file, or false.
206 function _search_path ($file) {
207 foreach ($this->_path as $dir) {
208 // ensure we use the same pathsep
209 if ($this->_isOtherPathsep()) {
210 $dir = $this->slashifyPath($dir);
211 $file = $this->slashifyPath($file);
212 if (file_exists($dir . $this->_pathsep . $file))
214 } elseif (@file_exists($dir . $this->_pathsep . $file))
221 * The system-dependent path-separator character. On UNIX systems,
222 * this character is ':'; on Win32 systems it is ';'.
224 * On Mac it cannot be : because this is the seperator there!
227 * @return string path_separator.
229 function _get_ini_separator () {
230 return isWindows() ? ';' : ':';
231 // return preg_match('/^Windows/', php_uname())
235 * Get the value of PHP's include_path.
238 * @return array Include path.
240 function _get_include_path() {
241 if (defined("INCLUDE_PATH"))
242 $path = INCLUDE_PATH;
244 $path = @get_cfg_var('include_path'); // FIXME: report warning
245 if (empty($path)) $path = @ini_get('include_path');
249 return explode($this->_get_ini_separator(), $this->slashifyPath($path));
253 * Add a directory to the end of PHP's include_path.
255 * The directory is appended only if it is not already listed in
259 * @param $dir string Directory to add.
261 function _append_to_include_path ($dir) {
262 $dir = $this->slashifyPath($dir);
263 if (!in_array($dir, $this->_path)) {
264 $this->_path[] = $dir;
267 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
268 * sometimes don't seem to heed their include_path.
269 * I.e. sometimes a file is not found even though it seems to
270 * be in the current include_path. A simple
271 * ini_set('include_path', ini_get('include_path')) seems to
272 * be enough to fix the problem
274 * This following line should be in the above if-block, but we
275 * put it here, as it seems to work-around the bug.
277 $GLOBALS['INCLUDE_PATH'] = implode($this->_get_ini_separator(), $this->_path);
278 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
282 * Add a directory to the front of PHP's include_path.
284 * The directory is prepended, and removed from the tail if already existing.
287 * @param $dir string Directory to add.
289 function _prepend_to_include_path ($dir) {
290 $dir = $this->slashifyPath($dir);
292 if ($i = array_search($dir, $this->_path) !== false) {
293 array_splice($this->_path, $i, 1);
295 array_unshift($this->_path, $dir);
296 $GLOBALS['INCLUDE_PATH'] = implode($this->_path, $this->_get_ini_separator());
297 @ini_set('include_path', $GLOBALS['INCLUDE_PATH']);
300 // Return all the possible shortened locale specifiers for the given locale.
301 // Most specific first.
302 // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
303 // This code might needed somewhere else also.
304 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.
318 *@return string The value of $LANG.
320 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
364 * @param $path array Where to look for PEAR library code.
365 * A good set of defaults is provided, so you can probably leave
366 * this parameter blank.
368 function PearFileFinder ($path = array()) {
369 $this->FileFinder(array_merge(
371 array('/usr/share/php4',
375 '/usr/local/share/php4',
376 '/usr/local/share/php',
377 '/usr/local/lib/php4',
378 '/usr/local/lib/php',
379 '/System/Library/PHP',
380 '/Apache/pear' // Windows
386 * Find PhpWiki localized files.
388 * This is a subclass of FileFinder which searches PHP's include_path
389 * for files. It looks first for "locale/$LANG/$file", then for
392 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
393 * also search under various less specific variations like
394 * "de_DE.iso8859-1", "de_DE" and "de".
396 class LocalizedFileFinder
402 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 () {
441 $this->_pathsep = $this->_get_syspath_separator();
442 $include_path = $this->_get_include_path();
445 $lang = $this->_get_lang();
446 assert(!empty($lang));
447 assert(!empty($WikiTheme));
449 if (is_object($WikiTheme)) {
450 $langs = $this->locale_versions($lang);
451 foreach ($langs as $lang) {
452 if ($lang == 'C') $lang = 'en';
453 foreach ($include_path as $dir) {
454 $path[] = $this->slashifyPath($WikiTheme->file("buttons/$lang"));
459 $this->FileFinder(array_merge($path, $include_path));
463 // Search PHP's include_path to find file or directory.
464 function FindFile ($file, $missing_okay = false, $slashify = false)
467 if (!isset($finder)) {
468 $finder = new FileFinder;
469 // remove "/lib" from dirname(__FILE__)
470 $wikidir = preg_replace('/.lib$/','',dirname(__FILE__));
471 // let the system favor its local pear?
472 $finder->_append_to_include_path(dirname(__FILE__)."/pear");
473 $finder->_prepend_to_include_path($wikidir);
474 // Don't override existing INCLUDE_PATH config.
475 if (!defined("INCLUDE_PATH"))
476 define("INCLUDE_PATH", implode($finder->_get_ini_separator(), $finder->_path));
478 $s = $finder->findFile($file, $missing_okay);
480 $s = $finder->slashifyPath($s);
484 // Search PHP's include_path to find file or directory.
485 // Searches for "locale/$LANG/$file", then for "$file".
486 function FindLocalizedFile ($file, $missing_okay = false, $re_init = false)
489 if ($re_init or !isset($finder))
490 $finder = new LocalizedFileFinder;
491 return $finder->findFile($file, $missing_okay);
494 function FindLocalizedButtonFile ($file, $missing_okay = false, $re_init = false)
496 static $buttonfinder;
497 if ($re_init or !isset($buttonfinder))
498 $buttonfinder = new LocalizedButtonFinder;
499 return $buttonfinder->findFile($file, $missing_okay);
503 * Prefixes with PHPWIKI_DIR and slashify.
504 * For example to unify with
505 * require_once dirname(__FILE__).'/lib/file.php'
506 * require_once 'lib/file.php' loading style.
507 * Doesn't expand "~" or symlinks yet. truename would be perfect.
509 * NormalizeLocalFileName("lib/config.php") => /home/user/phpwiki/lib/config.php
511 function NormalizeLocalFileName($file) {
513 if (!isset($finder)) {
514 $finder = new FileFinder;
516 // remove "/lib" from dirname(__FILE__)
517 if ($finder->_is_abs($file))
518 return $finder->slashifyPath($file);
520 if (defined("PHPWIKI_DIR")) $wikidir = PHPWIKI_DIR;
521 else $wikidir = preg_replace('/.lib$/','',dirname(__FILE__));
522 $wikidir = $finder->_strip_last_pathchar($wikidir);
523 $pathsep = $finder->_use_path_separator($wikidir);
524 return $finder->slashifyPath($wikidir . $pathsep . $file);
525 // return PHPWIKI_DIR . "/" . $file;
530 * Prefixes with DATA_PATH and slashify
532 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);
549 function isWindows() {
551 if (isset($win)) return $win;
552 //return preg_match('/^Windows/', php_uname());
553 $win = (substr(PHP_OS,0,3) == 'WIN');
557 function isWindows95() {
559 if (isset($win95)) return $win95;
560 $win95 = isWindows() and !isWindowsNT();
564 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.
577 * This is for the OLD Macintosh OS, NOT MacOSX or Darwin!
578 * This has really ugly pathname semantics.
579 * ":path" is relative, "Desktop:path" (I think) is absolute.
580 * FIXME: Please fix this someone. So far not supported.
583 return (substr(PHP_OS,0,9) == 'Macintosh'); // not tested!
586 // probably not needed, same behaviour as on unix.
587 function isCygwin() {
588 return (substr(PHP_OS,0,6) == 'CYGWIN');
595 // c-hanging-comment-ender-p: nil
596 // indent-tabs-mode: nil