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