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