]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/config.php
init global FileFinder to add proper include paths at startup
[SourceForge/phpwiki.git] / lib / config.php
1 <?php
2 rcs_id('$Id: config.php,v 1.112 2004-06-02 18:01:46 rurban Exp $');
3 /*
4  * NOTE: The settings here should probably not need to be changed.
5  * The user-configurable settings have been moved to IniConfig.php
6  * The run-time code has been moved to lib/IniConfig.php:fix_configs()
7  */
8  
9 /** 
10  * Returns true if current php version is at mimimum a.b.c 
11  * Called: check_php_version(4,1)
12  */
13 function check_php_version ($a = '0', $b = '0', $c = '0') {
14     static $PHP_VERSION;
15     if (!isset($PHP_VERSION))
16         $PHP_VERSION = substr( str_pad( preg_replace('/\D/','', PHP_VERSION), 3, '0'), 0, 3);
17     return ($PHP_VERSION >= ($a.$b.$c));
18 }
19
20 if (!defined("LC_ALL")) {
21     // Backward compatibility (for PHP < 4.0.5)
22     if (!check_php_version(4,0,5)) {
23         define("LC_ALL",   "LC_ALL");
24         define("LC_CTYPE", "LC_CTYPE");
25     } else {
26         define("LC_ALL",   0);
27         define("LC_CTYPE", 2);
28     }
29 }
30
31 function isCGI() {
32     return @preg_match('/CGI/',$GLOBALS['HTTP_ENV_VARS']['GATEWAY_INTERFACE']);
33 }
34
35 /*
36 // copy some $_ENV vars to $_SERVER for CGI compatibility. php does it automatically since when?
37 if (isCGI()) {
38     foreach (explode(':','SERVER_SOFTWARE:SERVER_NAME:GATEWAY_INTERFACE:SERVER_PROTOCOL:SERVER_PORT:REQUEST_METHOD:HTTP_ACCEPT:PATH_INFO:PATH_TRANSLATED:SCRIPT_NAME:QUERY_STRING:REMOTE_HOST:REMOTE_ADDR:REMOTE_USER:AUTH_TYPE:CONTENT_TYPE:CONTENT_LENGTH') as $key) {
39         $GLOBALS['HTTP_SERVER_VARS'][$key] = &$GLOBALS['HTTP_ENV_VARS'][$key];
40     }
41 }
42 */
43
44 // essential internal stuff
45 set_magic_quotes_runtime(0);
46
47 /** 
48  * Browser Detection Functions
49  *
50  * Current Issues:
51  *  NS/IE < 4.0 doesn't accept < ? xml version="1.0" ? >
52  *  NS/IE < 4.0 cannot display PNG
53  *  NS/IE < 4.0 cannot display all XHTML tags
54  *  NS < 5.0 needs textarea wrap=virtual
55  *  IE55 has problems with transparent PNG's
56  * @author: ReiniUrban
57  */
58 function browserAgent() {
59     static $HTTP_USER_AGENT = false;
60     if (!$HTTP_USER_AGENT)
61         $HTTP_USER_AGENT = @$GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
62     if (!$HTTP_USER_AGENT) // CGI
63         $HTTP_USER_AGENT = $GLOBALS['HTTP_ENV_VARS']['HTTP_USER_AGENT'];
64     return $HTTP_USER_AGENT;
65 }
66 function browserDetect($match) {
67     return strstr(browserAgent(), $match);
68 }
69 // returns a similar number for Netscape/Mozilla (gecko=5.0)/IE/Opera features.
70 function browserVersion() {
71     if (strstr(browserAgent(),    "Mozilla/4.0 (compatible; MSIE"))
72         return (float) substr(browserAgent(),30);
73     elseif (strstr(browserAgent(),"Mozilla/5.0 (compatible; Konqueror/"))
74         return (float) substr(browserAgent(),36);
75     else
76         return (float) substr(browserAgent(),8);
77 }
78 function isBrowserIE() {
79     return (browserDetect('Mozilla/') and 
80             browserDetect('MSIE'));
81 }
82 // problem with transparent PNG's
83 function isBrowserIE55() {
84     return (isBrowserIE() and 
85             browserVersion() > 5.1 and browserVersion() < 6.0);
86 }
87 // old Netscape prior to Mozilla
88 function isBrowserNetscape() {
89     return (browserDetect('Mozilla/') and 
90             ! browserDetect('Gecko/') and
91             ! browserDetect('MSIE'));
92 }
93 // NS3 or less
94 function isBrowserNS3() {
95     return (isBrowserNetscape() and browserVersion() < 4.0);
96 }
97 // must omit display alternate stylesheets: konqueror 3.1.4
98 // http://sourceforge.net/tracker/index.php?func=detail&aid=945154&group_id=6121&atid=106121
99 function isBrowserKonqueror($version = false) {
100     if ($version) return browserDetect('Konqueror/') and browserVersion() >= $version; 
101     return browserDetect('Konqueror/');
102 }
103
104 /**
105  * Smart? setlocale().
106  *
107  * This is a version of the builtin setlocale() which is
108  * smart enough to try some alternatives...
109  *
110  * @param mixed $category
111  * @param string $locale
112  * @return string The new locale, or <code>false</code> if unable
113  *  to set the requested locale.
114  * @see setlocale
115  */
116 function guessing_setlocale ($category, $locale) {
117     if ($res = setlocale($category, $locale))
118         return $res;
119     $alt = array('en' => array('C', 'en_US', 'en_GB', 'en_AU', 'en_CA', 'english'),
120                  'de' => array('de_DE', 'de_DE', 'de_DE@euro', 
121                                'de_AT@euro', 'de_AT', 'German_Austria.1252', 'deutsch', 
122                                'german', 'ge'),
123                  'es' => array('es_ES', 'es_MX', 'es_AR', 'spanish'),
124                  'nl' => array('nl_NL', 'dutch'),
125                  'fr' => array('fr_FR', 'français', 'french'),
126                  'it' => array('it_IT'),
127                  'sv' => array('sv_SE'),
128                  'ja' => array('ja_JP','ja_JP.eucJP','japanese.euc'),
129                  'zh' => array('zh_TW', 'zh_CN'),
130                  );
131     if (strlen($locale) == 2)
132         $lang = $locale;
133     else 
134         list ($lang) = split('_', $locale);
135     if (!isset($alt[$lang]))
136         return false;
137         
138     foreach ($alt[$lang] as $try) {
139         if ($res = setlocale($category, $try))
140             return $res;
141         // Try with charset appended...
142         $try = $try . '.' . $GLOBALS['charset'];
143         if ($res = setlocale($category, $try))
144             return $res;
145         foreach (array('@', ".", '_') as $sep) {
146             list ($try) = split($sep, $try);
147             if ($res = setlocale($category, $try))
148                 return $res;
149         }
150     }
151     return false;
152
153     // A standard locale name is typically of  the  form
154     // language[_territory][.codeset][@modifier],  where  language is
155     // an ISO 639 language code, territory is an ISO 3166 country code,
156     // and codeset  is  a  character  set or encoding identifier like
157     // ISO-8859-1 or UTF-8.
158 }
159
160 function update_locale($loc) {
161     //require_once(dirname(__FILE__)."/FileFinder.php");
162     $newlocale = guessing_setlocale(LC_ALL, $loc);
163     if (!$newlocale) {
164         //trigger_error(sprintf(_("Can't setlocale(LC_ALL,'%s')"), $loc), E_USER_NOTICE);
165         // => LC_COLLATE=C;LC_CTYPE=German_Austria.1252;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C
166         //$loc = setlocale(LC_CTYPE, '');  // pull locale from environment.
167         $newlocale = FileFinder::_get_lang();
168         list ($newlocale,) = split('_', $newlocale, 2);
169         //$GLOBALS['LANG'] = $loc;
170         //$newlocale = $loc;
171         //return false;
172     }
173     //if (substr($newlocale,0,2) == $loc) // don't update with C or failing setlocale
174     if (!isset($GLOBALS['LANG'])) $GLOBALS['LANG'] = $loc;
175     // Try to put new locale into environment (so any
176     // programs we run will get the right locale.)
177     //
178     // If PHP is in safe mode, this is not allowed,
179     // so hide errors...
180     @putenv("LC_ALL=$newlocale");
181     @putenv("LANG=$newlocale");
182     @putenv("LANGUAGE=$newlocale");
183     
184     if (!function_exists ('bindtextdomain'))  {
185         // Reinitialize translation array.
186         global $locale;
187         $locale = array();
188         // do reinit to purge PHP's static cache
189         if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok', 'reinit')) ) {
190             include($lcfile);
191         }
192     }
193
194     // To get the POSIX character classes in the PCRE's (e.g.
195     // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
196     // to set the locale, using setlocale().
197     //
198     // The problem is which locale to set?  We would like to recognize all
199     // upper-case characters in the iso-8859-1 character set as upper-case
200     // characters --- not just the ones which are in the current $LANG.
201     //
202     // As it turns out, at least on my system (Linux/glibc-2.2) as long as
203     // you setlocale() to anything but "C" it works fine.  (I'm not sure
204     // whether this is how it's supposed to be, or whether this is a bug
205     // in the libc...)
206     //
207     // We don't currently use the locale setting for anything else, so for
208     // now, just set the locale to US English.
209     //
210     // FIXME: Not all environments may support en_US?  We should probably
211     // have a list of locales to try.
212     if (setlocale(LC_CTYPE, 0) == 'C') {
213         $x = setlocale(LC_CTYPE, 'en_US.' . $GLOBALS['charset']);
214     } else {
215         $x = setlocale(LC_CTYPE, $newlocale);
216     }
217
218     return $newlocale;
219 }
220
221 /** string pcre_fix_posix_classes (string $regexp)
222 *
223 * Older version (pre 3.x?) of the PCRE library do not support
224 * POSIX named character classes (e.g. [[:alnum:]]).
225 *
226 * This is a helper function which can be used to convert a regexp
227 * which contains POSIX named character classes to one that doesn't.
228 *
229 * All instances of strings like '[:<class>:]' are replaced by the equivalent
230 * enumerated character class.
231 *
232 * Implementation Notes:
233 *
234 * Currently we use hard-coded values which are valid only for
235 * ISO-8859-1.  Also, currently on the classes [:alpha:], [:alnum:],
236 * [:upper:] and [:lower:] are implemented.  (The missing classes:
237 * [:blank:], [:cntrl:], [:digit:], [:graph:], [:print:], [:punct:],
238 * [:space:], and [:xdigit:] could easily be added if needed.)
239 *
240 * This is a hack.  I tried to generate these classes automatically
241 * using ereg(), but discovered that in my PHP, at least, ereg() is
242 * slightly broken w.r.t. POSIX character classes.  (It includes
243 * "\xaa" and "\xba" in [:alpha:].)
244 *
245 * So for now, this will do.  --Jeff <dairiki@dairiki.org> 14 Mar, 2001
246 */
247 function pcre_fix_posix_classes ($regexp) {
248     global $charset;
249     if (!isset($charset))
250         $charset = CHARSET; // get rid of constant. pref is dynamic and language specific
251     if (in_array($GLOBALS['LANG'],array('zh')))
252         $charset = 'utf-8';
253     if (in_array($GLOBALS['LANG'],array('ja')))
254         $charset = 'EUC-JP';
255     if (strtolower($charset) == 'utf-8') { // thanks to John McPherson
256         // until posix class names/pcre work with utf-8
257         if (preg_match('/[[:upper:]]/', '\xc4\x80'))
258             return $regexp;    
259         // utf-8 non-ascii chars: most common (eg western) latin chars are 0xc380-0xc3bf
260         // we currently ignore other less common non-ascii characters
261         // (eg central/east european) latin chars are 0xc432-0xcdbf and 0xc580-0xc5be
262         // and indian/cyrillic/asian languages
263         
264         // this replaces [[:lower:]] with utf-8 match (Latin only)
265         $regexp = preg_replace('/\[\[\:lower\:\]\]/','(?:[a-z]|\xc3[\x9f-\xbf]|\xc4[\x81\x83\x85\x87])',
266                                $regexp);
267         // this replaces [[:upper:]] with utf-8 match (Latin only)
268         $regexp = preg_replace('/\[\[\:upper\:\]\]/','(?:[A-Z]|\xc3[\x80-\x9e]|\xc4[\x80\x82\x84\x86])',
269                                $regexp);
270     } elseif (preg_match('/[[:upper:]]/', 'Ä')) {
271         // First check to see if our PCRE lib supports POSIX character
272         // classes.  If it does, there's nothing to do.
273         return $regexp;
274     }
275     static $classes = array(
276                             'alnum' => "0-9A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
277                             'alpha' => "A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
278                             'upper' => "A-Z\xc0-\xd6\xd8-\xde",
279                             'lower' => "a-z\xdf-\xf6\xf8-\xff"
280                             );
281     $keys = join('|', array_keys($classes));
282     return preg_replace("/\[:($keys):]/e", '$classes["\1"]', $regexp);
283 }
284
285 function deduce_script_name() {
286     $s = &$GLOBALS['HTTP_SERVER_VARS'];
287     $script = @$s['SCRIPT_NAME'];
288     if (empty($script) or $script[0] != '/') {
289         // Some places (e.g. Lycos) only supply a relative name in
290         // SCRIPT_NAME, but give what we really want in SCRIPT_URL.
291         if (!empty($s['SCRIPT_URL']))
292             $script = $s['SCRIPT_URL'];
293     }
294     return $script;
295 }
296
297 function IsProbablyRedirectToIndex () {
298     // This might be a redirect to the DirectoryIndex,
299     // e.g. REQUEST_URI = /dir/?some_action got redirected
300     // to SCRIPT_NAME = /dir/index.php
301
302     // In this case, the proper virtual path is still
303     // $SCRIPT_NAME, since pages appear at
304     // e.g. /dir/index.php/HomePage.
305
306     $requri = preg_replace('/\?.*$/','',$GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']);
307     $requri = preg_quote($requri, '%');
308     return preg_match("%^${requri}[^/]*$%", $GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
309 }
310
311 // >= php-4.1.0
312 if (!function_exists('array_key_exists')) { // lib/IniConfig.php, sqlite, adodb, ...
313     function array_key_exists($item, $array) {
314         return isset($array[$item]);
315     }
316 }
317
318 // => php-4.0.5
319 if (!function_exists('is_scalar')) { // lib/stdlib.php:hash()
320     function is_scalar($x) {
321         return is_numeric($x) or is_string($x) or is_float($x) or is_bool($x); 
322     }
323 }
324
325 // $Log: not supported by cvs2svn $
326 // Revision 1.111  2004/05/17 17:43:29  rurban
327 // CGI: no PATH_INFO fix
328 //
329 // Revision 1.110  2004/05/16 23:10:44  rurban
330 // update_locale wrongly resetted LANG, which broke japanese.
331 // japanese now correctly uses EUC_JP, not utf-8.
332 // more charset and lang headers to help the browser.
333 //
334 // Revision 1.109  2004/05/08 14:06:12  rurban
335 // new support for inlined image attributes: [image.jpg size=50x30 align=right]
336 // minor stability and portability fixes
337 //
338 // Revision 1.108  2004/05/08 11:25:16  rurban
339 // php-4.0.4 fixes
340 //
341 // Revision 1.107  2004/05/06 17:30:38  rurban
342 // CategoryGroup: oops, dos2unix eol
343 // improved phpwiki_version:
344 //   pre -= .0001 (1.3.10pre: 1030.099)
345 //   -p1 += .001 (1.3.9-p1: 1030.091)
346 // improved InstallTable for mysql and generic SQL versions and all newer tables so far.
347 // abstracted more ADODB/PearDB methods for action=upgrade stuff:
348 //   backend->backendType(), backend->database(),
349 //   backend->listOfFields(),
350 //   backend->listOfTables(),
351 //
352 // Revision 1.106  2004/05/02 19:12:14  rurban
353 // fix sf.net bug #945154 Konqueror alt css
354 //
355 // Revision 1.105  2004/05/02 15:10:06  rurban
356 // new finally reliable way to detect if /index.php is called directly
357 //   and if to include lib/main.php
358 // new global AllActionPages
359 // SetupWiki now loads all mandatory pages: HOME_PAGE, action pages, and warns if not.
360 // WikiTranslation what=buttons for Carsten to create the missing MacOSX buttons
361 // PageGroupTestOne => subpages
362 // renamed PhpWikiRss to PhpWikiRecentChanges
363 // more docs, default configs, ...
364 //
365 // Revision 1.104  2004/05/01 11:26:37  rurban
366 // php-4.0.x support: array_key_exists (PHP 4 >= 4.1.0)
367 //
368 // Revision 1.103  2004/04/30 00:04:14  rurban
369 // zh (chinese language) support
370 //
371 // Revision 1.102  2004/04/29 23:25:12  rurban
372 // re-ordered locale init (as in 1.3.9)
373 // fixed loadfile with subpages, and merge/restore anyway
374 //   (sf.net bug #844188)
375 //
376 // Revision 1.101  2004/04/26 13:22:32  rurban
377 // calculate bool old or dynamic constants later
378 //
379 // Revision 1.100  2004/04/26 12:15:01  rurban
380 // check default config values
381 //
382 // Revision 1.99  2004/04/21 14:04:24  zorloc
383 // 'Require lib/FileFinder.php' necessary to allow for call to FindLocalizedFile().
384 //
385 // Revision 1.98  2004/04/20 18:10:28  rurban
386 // config refactoring:
387 //   FileFinder is needed for WikiFarm scripts calling index.php
388 //   config run-time calls moved to lib/IniConfig.php:fix_configs()
389 //   added PHPWIKI_DIR smart-detection code (Theme finder)
390 //   moved FileFind to lib/FileFinder.php
391 //   cleaned lib/config.php
392 //
393 // Revision 1.97  2004/04/18 01:11:52  rurban
394 // more numeric pagename fixes.
395 // fixed action=upload with merge conflict warnings.
396 // charset changed from constant to global (dynamic utf-8 switching)
397 //
398
399 // For emacs users
400 // Local Variables:
401 // mode: php
402 // tab-width: 8
403 // c-basic-offset: 4
404 // c-hanging-comment-ender-p: nil
405 // indent-tabs-mode: nil
406 // End:
407 ?>