]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/FileFinder.php
Adjustments to english instructions for conflicting edits.
[SourceForge/phpwiki.git] / lib / FileFinder.php
1 <?php rcs_id('$Id: FileFinder.php,v 1.7 2002-01-22 03:12:59 carstenklapp Exp $');
2
3 // FIXME: make this work with non-unix (e.g. DOS) filenames.
4
5 /**
6  * A class for finding files.
7  */
8 class FileFinder
9 {
10     /**
11      * Constructor.
12      *
13      * @param $path array A list of directories in which to search for files.
14      */
15     function FileFinder ($path = false) {
16         if ($path === false)
17             $path = $this->_get_include_path();
18         $this->_path = $path;
19     }
20
21     /**
22      * Find file.
23      *
24      * @param $file string File to search for.
25      * @return string The filename (including path), if found, otherwise false.
26      */
27     function findFile ($file, $missing_okay = false) {
28         if ($this->_is_abs($file)) {
29             if (file_exists($file))
30                 return $file;
31         }
32         elseif ( ($dir = $this->_search_path($file)) ) {
33             return "$dir/$file";
34         }
35
36         return $missing_okay ? false : $this->_not_found($file);
37     }
38
39     /**
40      * Try to include file.
41      *
42      * If file is found in the path, then the files directory is added
43      * to PHP's include_path (if it's not already there.) Then the
44      * file is include_once()'d.
45      *
46      * @param $file string File to include.
47      * @return bool True if file was successfully included.
48      */
49     function includeOnce ($file) {
50         if ( ($ret = @include_once($file)) )
51             return $ret;
52
53         if (!$this->_is_abs($file)) {
54             if ( ($dir = $this->_search_path($file)) && is_file("$dir/$file")) {
55                 $this->_append_to_include_path($dir);
56                 return include_once($file);
57             }
58         }
59
60         return $this->_not_found($file);
61     }
62
63     /**
64      * Determine if path is absolute.
65      *
66      * @access private
67      * @param $path string Path.
68      * @return bool True iff path is absolute. 
69      */
70     function _is_abs($path) {
71         return ereg('^/', $path);
72     }
73
74     /**
75      * Report a "file not found" error.
76      *
77      * @access private
78      * @param $file string Name of missing file.
79      * @return bool false.
80      */
81     function _not_found($file) {
82         trigger_error(sprintf(_("%s: file not found"),$file), E_USER_ERROR);
83         return false;
84     }
85
86
87     /**
88      * Search our path for a file.
89      *
90      * @access private
91      * @param $file string File to find.
92      * @return string Directory which contains $file, or false.
93      */
94     function _search_path ($file) {
95         foreach ($this->_path as $dir) {
96             if (file_exists("$dir/$file"))
97                 return $dir;
98         }
99         return false;
100     }
101
102     /**
103      * The system-dependent path-separator character. On UNIX systems,
104      * this character is ':'; on Win32 systems it is ';'.
105      *
106      * @access private
107      * @return string path_separator.
108      */
109     function _get_path_separator () {
110         return preg_match('/^Windows/', php_uname()) ? ';' : ':';
111     }
112
113     /**
114      * Get the value of PHP's include_path.
115      *
116      * @access private
117      * @return array Include path.
118      */
119     function _get_include_path() {
120         $path = ini_get('include_path');
121         if (empty($path))
122             $path = '.';
123         return explode($this->_get_path_separator(), $path);
124     }
125
126     /**
127      * Add a directory to the end of PHP's include_path.
128      *
129      * The directory is appended only if it is not already listed in
130      * the include_path.
131      *
132      * @access private
133      * @param $dir string Directory to add.
134      */
135     function _append_to_include_path ($dir) {
136         $path = $this->_get_include_path();
137         if (!in_array($dir, $path)) {
138             $path[] = $dir;
139             //ini_set('include_path', implode(':', $path));
140         }
141         /*
142          * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
143          * sometimes don't seem to heed their include_path.
144          * I.e. sometimes a file is not found even though it seems to
145          * be in the current include_path. A simple
146          * ini_set('include_path', ini_get('include_path')) seems to
147          * be enough to fix the problem
148          *
149          * This following line should be in the above if-block, but we
150          * put it here, as it seems to work-around the bug.
151          */
152         ini_set('include_path', implode($this->_get_path_separator(), $path));
153     }
154 }
155
156 /**
157  * A class for finding PEAR code.
158  *
159  * This is a subclass of FileFinder which searches a standard list of
160  * directories where PEAR code is likely to be installed.
161  *
162  * Example usage:
163  *
164  * <pre>
165  *   $pearFinder = new PearFileFinder;
166  *   $pearFinder->includeOnce('DB.php');
167  * </pre>
168  *
169  * The above code will look for 'DB.php', if found, the directory in
170  * which it was found will be added to PHP's include_path, and the
171  * file will be included. (If the file is not found, and E_USER_ERROR
172  * will be thrown.)
173  */
174 class PearFileFinder
175     extends FileFinder
176 {
177     /**
178      * Constructor.
179      *
180      * @param $path array Where to look for PEAR library code.
181      * A good set of defaults is provided, so you can probably leave
182      * this parameter blank.
183      */
184     function PearFileFinder ($path = false) {
185         $this->FileFinder(array('/usr/share/php4',
186                                 '/usr/share/php',
187                                 '/usr/lib/php4',
188                                 '/usr/lib/php',
189                                 '/usr/local/share/php4',
190                                 '/usr/local/share/php',
191                                 '/usr/local/lib/php4',
192                                 '/usr/local/lib/php',
193                                 '/System/Library/PHP'));
194     }
195 }
196
197 /**
198  * Find PhpWiki localized files.
199  *
200  * This is a subclass of FileFinder which searches PHP's include_path
201  * for files. It looks first for "locale/$LANG/$file", then for
202  * "$file".
203  *
204  * If $LANG is something like "de_DE.iso8859-1@euro", this class will
205  * also search under various less specific variations like
206  * "de_DE.iso8859-1", "de_DE" and "de".
207  */
208 class LocalizedFileFinder
209     extends FileFinder
210 {
211     /**
212      * Constructor.
213      */
214     function LocalizedFileFinder () {
215         $include_path = $this->_get_include_path();
216         $path = array();
217
218         $lang = $this->_get_lang();
219         assert(!empty($lang));
220
221         // A locale can be, e.g. de_DE.iso8859-1@euro.
222         // Try less specific versions of the locale: 
223         $langs[] = $lang;
224         foreach (array('@', '.', '_') as $sep) {
225             if ( ($tail = strchr($lang, $sep)) )
226                 $langs[] = substr($lang, 0, -strlen($tail));
227         }
228
229         foreach ($langs as $lang) {
230             foreach ($include_path as $dir) {
231                 $path[] = "$dir/locale/$lang";
232             }
233         }
234
235         $this->FileFinder(array_merge($path, $include_path));
236     }
237
238     /**
239      * Try to figure out the appropriate value for $LANG.
240      *
241      *@access private
242      *@return string The value of $LANG.
243      */
244     function _get_lang() {
245         if (!empty($GLOBALS['LANG']))
246             return $GLOBALS['LANG'];
247
248         foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
249             $lang = getenv($var);
250             if (!empty($lang))
251                 return $lang;
252         }
253
254         return "C";
255     }
256
257 }
258
259 /**
260  * Find PhpWiki localized theme buttons.
261  *
262  * This is a subclass of FileFinder which searches PHP's include_path
263  * for files. It looks first for "buttons/$LANG/$file", then for
264  * "$file".
265  *
266  * If $LANG is something like "de_DE.iso8859-1@euro", this class will
267  * also search under various less specific variations like
268  * "de_DE.iso8859-1", "de_DE" and "de".
269  */
270 class LocalizedButtonFinder
271     extends FileFinder
272 {
273     /**
274      * Constructor.
275      */
276     function LocalizedButtonFinder () {
277         $include_path = $this->_get_include_path();
278         $path = array();
279
280         $lang = $this->_get_lang();
281         assert(!empty($lang));
282
283         // A locale can be, e.g. de_DE.iso8859-1@euro.
284         // Try less specific versions of the locale: 
285         $langs[] = $lang;
286         foreach (array('@', '.', '_') as $sep) {
287             if ( ($tail = strchr($lang, $sep)) )
288                 $langs[] = substr($lang, 0, -strlen($tail));
289         }
290
291         foreach ($langs as $lang) {
292             foreach ($include_path as $dir) {
293                 // FIXME: sorry I know this is ugly but don't know yet what else to do
294                 if ($lang=='C')
295                     $lang='en';
296                 $path[] = "$dir/themes/".THEME."/buttons/$lang";
297
298             }
299         }
300
301         $this->FileFinder(array_merge($path, $include_path));
302     }
303
304     /**
305      * Try to figure out the appropriate value for $LANG.
306      *
307      *@access private
308      *@return string The value of $LANG.
309      */
310     function _get_lang() {
311         if (!empty($GLOBALS['LANG']))
312             return $GLOBALS['LANG'];
313
314         foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
315             $lang = getenv($var);
316             if (!empty($lang))
317                 return $lang;
318         }
319
320         return "en";
321     }
322
323 }
324
325 // Local Variables:
326 // mode: php
327 // tab-width: 8
328 // c-basic-offset: 4
329 // c-hanging-comment-ender-p: nil
330 // indent-tabs-mode: nil
331 // End:
332 ?>