]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/config.php
fix broken relative paths
[SourceForge/phpwiki.git] / lib / config.php
1 <?php
2 rcs_id('$Id: config.php,v 1.96 2004-04-12 10:08:34 rurban Exp $');
3 /*
4  * NOTE: the settings here should probably not need to be changed.
5 *
6 *
7 * (The user-configurable settings have been moved to index.php.)
8 */
9
10 if (!defined("LC_ALL")) {
11     // Backward compatibility (for PHP < 4.0.5)
12     define("LC_ALL",   0);
13     define("LC_CTYPE", 2);
14 }
15
16 function isCGI() {
17     return @preg_match('/CGI/',$GLOBALS['HTTP_ENV_VARS']['GATEWAY_INTERFACE']);
18 }
19
20 /*
21 // copy some $_ENV vars to $_SERVER for CGI compatibility. php does it automatically since when?
22 if (isCGI()) {
23     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) {
24         $GLOBALS['HTTP_SERVER_VARS'][$key] = &$GLOBALS['HTTP_ENV_VARS'][$key];
25     }
26 }
27 */
28
29 // essential internal stuff
30 set_magic_quotes_runtime(0);
31
32 // Some constants.
33
34 // "\x80"-"\x9f" (and "\x00" - "\x1f") are non-printing control
35 // chars in iso-8859-*
36 // $FieldSeparator = "\263"; //this is a superscript 3 in ISO-8859-1.
37 // $FieldSeparator = "\xFF"; // this byte should never appear in utf-8
38 if (strtolower(CHARSET) == 'utf-8')
39     $FieldSeparator = "\xFF";
40 else
41     $FieldSeparator = "\x81";
42
43 /** 
44  * Browser Detection Functions
45  *
46  * Current Issues:
47  *  NS/IE < 4.0 doesn't accept < ? xml version="1.0" ? >
48  *  NS/IE < 4.0 cannot display PNG
49  *  NS/IE < 4.0 cannot display all XHTML tags
50  *  NS < 5.0 needs textarea wrap=virtual
51  *  IE55 has problems with transparent PNG's
52  * @author: ReiniUrban
53  */
54 function browserAgent() {
55     static $HTTP_USER_AGENT = false;
56     if (!$HTTP_USER_AGENT)
57         $HTTP_USER_AGENT = @$GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
58     if (!$HTTP_USER_AGENT) // CGI
59         $HTTP_USER_AGENT = $GLOBALS['HTTP_ENV_VARS']['HTTP_USER_AGENT'];
60     return $HTTP_USER_AGENT;
61 }
62 function browserDetect($match) {
63     return strstr(browserAgent(), $match);
64 }
65 // returns a similar number for Netscape/Mozilla (gecko=5.0)/IE/Opera features.
66 function browserVersion() {
67     if (strstr(browserAgent(),"Mozilla/4.0 (compatible; MSIE"))
68         return (float) substr(browserAgent(),30);
69     else
70         return (float) substr(browserAgent(),8);
71 }
72 function isBrowserIE() {
73     return (browserDetect('Mozilla/') and 
74             browserDetect('MSIE'));
75 }
76 // problem with transparent PNG's
77 function isBrowserIE55() {
78     return (isBrowserIE() and 
79             browserVersion() > 5.1 and browserVersion() < 6.0);
80 }
81 function isBrowserNetscape() {
82     return (browserDetect('Mozilla/') and 
83             ! browserDetect('Gecko/') and
84             ! browserDetect('MSIE'));
85 }
86 // NS3 or less
87 function isBrowserNS3() {
88     return (isBrowserNetscape() and browserVersion() < 4.0);
89 }
90
91 require_once('lib/FileFinder.php');
92 // Search PHP's include_path to find file or directory.
93 function FindFile ($file, $missing_okay = false, $slashify = false)
94 {
95     static $finder;
96     if (!isset($finder))
97         $finder = new FileFinder;
98     $s = $finder->findFile($file, $missing_okay);
99     if ($slashify)
100       $s = $finder->slashifyPath($s);
101     return $s;
102 }
103
104 // Search PHP's include_path to find file or directory.
105 // Searches for "locale/$LANG/$file", then for "$file".
106 function FindLocalizedFile ($file, $missing_okay = false, $re_init = false)
107 {
108     static $finder;
109     if ($re_init or !isset($finder))
110         $finder = new LocalizedFileFinder;
111     return $finder->findFile($file, $missing_okay);
112 }
113
114 function FindLocalizedButtonFile ($file, $missing_okay = false, $re_init = false)
115 {
116     static $buttonfinder;
117     if ($re_init or !isset($buttonfinder))
118         $buttonfinder = new LocalizedButtonFinder;
119     return $buttonfinder->findFile($file, $missing_okay);
120 }
121
122
123 /**
124  * Smart? setlocale().
125  *
126  * This is a version of the builtin setlocale() which is
127  * smart enough to try some alternatives...
128  *
129  * @param mixed $category
130  * @param string $locale
131  * @return string The new locale, or <code>false</code> if unable
132  *  to set the requested locale.
133  * @see setlocale
134  */
135 function guessing_setlocale ($category, $locale) {
136     if ($res = setlocale($category, $locale))
137         return $res;
138     $alt = array('en' => array('C', 'en_US', 'en_GB', 'en_AU', 'en_CA', 'english'),
139                  'de' => array('de_DE', 'de_DE', 'de_DE@euro', 
140                                'de_AT@euro', 'de_AT', 'German_Austria.1252', 'deutsch', 
141                                'german', 'ge'),
142                  'es' => array('es_ES', 'es_MX', 'es_AR', 'spanish'),
143                  'nl' => array('nl_NL', 'dutch'),
144                  'fr' => array('fr_FR', 'français', 'french'),
145                  'it' => array('it_IT'),
146                  'sv' => array('sv_SE'),
147                  'ja' => array('ja_JP','ja_JP.eucJP','japanese.euc')
148                  );
149     if (strlen($locale) == 2)
150         $lang = $locale;
151     else 
152         list ($lang) = split('_', $locale);
153     if (!isset($alt[$lang]))
154         return false;
155         
156     foreach ($alt[$lang] as $try) {
157         if ($res = setlocale($category, $try))
158             return $res;
159         // Try with charset appended...
160         $try = $try . '.' . CHARSET;
161         if ($res = setlocale($category, $try))
162             return $res;
163         foreach (array('@', ".", '_') as $sep) {
164             list ($try) = split($sep, $try);
165             if ($res = setlocale($category, $try))
166                 return $res;
167         }
168     }
169     return false;
170
171     // A standard locale name is typically of  the  form
172     // language[_territory][.codeset][@modifier],  where  language is
173     // an ISO 639 language code, territory is an ISO 3166 country code,
174     // and codeset  is  a  character  set or encoding identifier like
175     // ISO-8859-1 or UTF-8.
176 }
177
178 function update_locale($loc) {
179     $newlocale = guessing_setlocale(LC_ALL, $loc);
180     if (!$newlocale) {
181         //trigger_error(sprintf(_("Can't setlocale(LC_ALL,'%s')"), $loc), E_USER_NOTICE);
182         // => LC_COLLATE=C;LC_CTYPE=German_Austria.1252;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C
183         //$loc = setlocale(LC_CTYPE, '');  // pull locale from environment.
184         $newlocale = FileFinder::_get_lang();
185         list ($newlocale,) = split('_', $newlocale, 2);
186         //$GLOBALS['LANG'] = $loc;
187         //$newlocale = $loc;
188         //return false;
189     }
190     //if (substr($newlocale,0,2) == $loc) // don't update with C or failing setlocale
191     $GLOBALS['LANG'] = $loc;
192     // Try to put new locale into environment (so any
193     // programs we run will get the right locale.)
194     //
195     // If PHP is in safe mode, this is not allowed,
196     // so hide errors...
197     @putenv("LC_ALL=$newlocale");
198     @putenv("LANG=$newlocale");
199     @putenv("LANGUAGE=$newlocale");
200     
201     if (!function_exists ('bindtextdomain'))  {
202         // Reinitialize translation array.
203         global $locale;
204         $locale = array();
205         // do reinit to purge PHP's static cache
206         if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok','reinit')) ) {
207             include($lcfile);
208         }
209     }
210
211     // To get the POSIX character classes in the PCRE's (e.g.
212     // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
213     // to set the locale, using setlocale().
214     //
215     // The problem is which locale to set?  We would like to recognize all
216     // upper-case characters in the iso-8859-1 character set as upper-case
217     // characters --- not just the ones which are in the current $LANG.
218     //
219     // As it turns out, at least on my system (Linux/glibc-2.2) as long as
220     // you setlocale() to anything but "C" it works fine.  (I'm not sure
221     // whether this is how it's supposed to be, or whether this is a bug
222     // in the libc...)
223     //
224     // We don't currently use the locale setting for anything else, so for
225     // now, just set the locale to US English.
226     //
227     // FIXME: Not all environments may support en_US?  We should probably
228     // have a list of locales to try.
229     if (setlocale(LC_CTYPE, 0) == 'C') {
230         $x = setlocale(LC_CTYPE, 'en_US.' . CHARSET );
231     } else {
232         $x = setlocale(LC_CTYPE, $newlocale);
233     }
234
235     return $newlocale;
236 }
237
238 if (!defined('DEFAULT_LANGUAGE'))
239      define('DEFAULT_LANGUAGE', 'en');
240
241 //
242 // Set up (possibly fake) gettext()
243 //
244 if (!function_exists ('bindtextdomain')) {
245     $locale = array();
246
247     function gettext ($text) { 
248         global $locale;
249         if (!empty ($locale[$text]))
250             return $locale[$text];
251         return $text;
252     }
253
254     function _ ($text) {
255         return gettext($text);
256     }
257 }
258 else {
259     // Working around really weird gettext problems: (4.3.2, 4.3.6 win)
260     // bindtextdomain() returns the current domain path.
261     // 1. If the script is not index.php but something like "de", on a different path
262     //    then bindtextdomain() fails, but after chdir to the correct path it will work okay.
263     // 2. But the weird error "Undefined variable: bindtextdomain" is generated then.
264     $bindtextdomain_path = FindFile("locale", false, true);
265     if (isWindows())
266         $bindtextdomain_path = str_replace("/","\\",$bindtextdomain_path);
267     $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain);
268     if ($bindtextdomain_real != $bindtextdomain_path) {
269         // this will happen with virtual_paths. chdir and try again.
270         chdir($bindtextdomain_path);
271         $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain);
272     }
273     textdomain("phpwiki");
274     if ($bindtextdomain_real != $bindtextdomain_path) { // change back
275         chdir($bindtextdomain_real . (isWindows() ? "\\.." : "/.."));
276     }
277 }
278
279 /** string pcre_fix_posix_classes (string $regexp)
280 *
281 * Older version (pre 3.x?) of the PCRE library do not support
282 * POSIX named character classes (e.g. [[:alnum:]]).
283 *
284 * This is a helper function which can be used to convert a regexp
285 * which contains POSIX named character classes to one that doesn't.
286 *
287 * All instances of strings like '[:<class>:]' are replaced by the equivalent
288 * enumerated character class.
289 *
290 * Implementation Notes:
291 *
292 * Currently we use hard-coded values which are valid only for
293 * ISO-8859-1.  Also, currently on the classes [:alpha:], [:alnum:],
294 * [:upper:] and [:lower:] are implemented.  (The missing classes:
295 * [:blank:], [:cntrl:], [:digit:], [:graph:], [:print:], [:punct:],
296 * [:space:], and [:xdigit:] could easily be added if needed.)
297 *
298 * This is a hack.  I tried to generate these classes automatically
299 * using ereg(), but discovered that in my PHP, at least, ereg() is
300 * slightly broken w.r.t. POSIX character classes.  (It includes
301 * "\xaa" and "\xba" in [:alpha:].)
302 *
303 * So for now, this will do.  --Jeff <dairiki@dairiki.org> 14 Mar, 2001
304 */
305 function pcre_fix_posix_classes ($regexp) {
306     // First check to see if our PCRE lib supports POSIX character
307     // classes.  If it does, there's nothing to do.
308     if (preg_match('/[[:upper:]]/', 'Ä'))
309         return $regexp;
310
311     static $classes = array(
312                             'alnum' => "0-9A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
313                             'alpha' => "A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
314                             'upper' => "A-Z\xc0-\xd6\xd8-\xde",
315                             'lower' => "a-z\xdf-\xf6\xf8-\xff"
316                             );
317
318     $keys = join('|', array_keys($classes));
319
320     return preg_replace("/\[:($keys):]/e", '$classes["\1"]', $regexp);
321 }
322
323 $WikiNameRegexp = pcre_fix_posix_classes($WikiNameRegexp);
324 $KeywordLinkRegexp = pcre_fix_posix_classes($KeywordLinkRegexp);
325
326 //////////////////////////////////////////////////////////////////
327 // Autodetect URL settings:
328 //
329 if (!defined('SERVER_NAME')) define('SERVER_NAME', $HTTP_SERVER_VARS['SERVER_NAME']);
330 if (!defined('SERVER_PORT')) define('SERVER_PORT', $HTTP_SERVER_VARS['SERVER_PORT']);
331 if (!defined('SERVER_PROTOCOL')) {
332     if (empty($HTTP_SERVER_VARS['HTTPS']) || $HTTP_SERVER_VARS['HTTPS'] == 'off')
333         define('SERVER_PROTOCOL', 'http');
334     else
335         define('SERVER_PROTOCOL', 'https');
336 }
337
338 function deduce_script_name() {
339     $s = &$GLOBALS['HTTP_SERVER_VARS'];
340     $script = @$s['SCRIPT_NAME'];
341     if (empty($script) or $script[0] != '/') {
342         // Some places (e.g. Lycos) only supply a relative name in
343         // SCRIPT_NAME, but give what we really want in SCRIPT_URL.
344         if (!empty($s['SCRIPT_URL']))
345             $script = $s['SCRIPT_URL'];
346     }
347     return $script;
348 }
349
350 if (!defined('SCRIPT_NAME'))
351     define('SCRIPT_NAME', deduce_script_name());
352
353 if (!defined('USE_PATH_INFO'))
354 {
355     /*
356      * If SCRIPT_NAME does not look like php source file,
357      * or user cgi we assume that php is getting run by an
358      * action handler in /cgi-bin.  In this case,
359      * I think there is no way to get Apache to pass
360      * useful PATH_INFO to the php script (PATH_INFO
361      * is used to the the php interpreter where the
362      * php script is...)
363      */
364     switch (php_sapi_name()) {
365     case 'apache':
366     case 'apache2handler':
367         define('USE_PATH_INFO', true);
368         break;
369     case 'cgi':
370     case 'apache2filter':
371         define('USE_PATH_INFO', false);
372         break;
373     default:
374         define('USE_PATH_INFO', ereg('\.(php3?|cgi)$', SCRIPT_NAME));
375         break;
376     }
377 }
378      
379 // If user has not defined DATA_PATH, we want to use relative URLs.
380 if (!defined('DATA_PATH') && USE_PATH_INFO)
381      define('DATA_PATH', '..');
382
383 function IsProbablyRedirectToIndex () {
384     // This might be a redirect to the DirectoryIndex,
385     // e.g. REQUEST_URI = /dir/  got redirected
386     // to SCRIPT_NAME = /dir/index.php
387
388     // In this case, the proper virtual path is still
389     // $SCRIPT_NAME, since pages appear at
390     // e.g. /dir/index.php/HomePage.
391
392     $requri = preg_quote($GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'], '%');
393     return preg_match("%^${requri}[^/]*$%", $GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
394 }
395
396
397 if (!defined('VIRTUAL_PATH')) {
398     // We'd like to auto-detect when the cases where apaches
399     // 'Action' directive (or similar means) is used to
400     // redirect page requests to a cgi-handler.
401     //
402     // In cases like this, requests for e.g. /wiki/HomePage
403     // get redirected to a cgi-script called, say,
404     // /path/to/wiki/index.php.  The script gets all
405     // of /wiki/HomePage as it's PATH_INFO.
406     //
407     // The problem is:
408     //   How to detect when this has happened reliably?
409     //   How to pick out the "virtual path" (in this case '/wiki')?
410     //
411     // (Another time an redirect might occur is to a DirectoryIndex
412     // -- the requested URI is '/wikidir/', the request gets
413     // passed to '/wikidir/index.php'.  In this case, the
414     // proper VIRTUAL_PATH is '/wikidir/index.php', since the
415     // pages will appear at e.g. '/wikidir/index.php/HomePage'.
416     //
417
418     $REDIRECT_URL = &$HTTP_SERVER_VARS['REDIRECT_URL'];
419     if (USE_PATH_INFO and isset($REDIRECT_URL)
420         and ! IsProbablyRedirectToIndex()) {
421         // FIXME: This is a hack, and won't work if the requested
422         // pagename has a slash in it.
423         define('VIRTUAL_PATH', dirname($REDIRECT_URL . 'x'));
424     } else {
425         define('VIRTUAL_PATH', SCRIPT_NAME);
426     }
427 }
428
429 if (SERVER_PORT
430     && SERVER_PORT != (SERVER_PROTOCOL == 'https' ? 443 : 80)) {
431     define('SERVER_URL',
432            SERVER_PROTOCOL . '://' . SERVER_NAME . ':' . SERVER_PORT);
433 }
434 else {
435     define('SERVER_URL',
436            SERVER_PROTOCOL . '://' . SERVER_NAME);
437 }
438
439 if (VIRTUAL_PATH != SCRIPT_NAME) {
440     // Apache action handlers are used.
441     define('PATH_INFO_PREFIX', VIRTUAL_PATH . '/');
442 }
443 else
444     define('PATH_INFO_PREFIX', '/');
445
446
447 define('PHPWIKI_BASE_URL',
448        SERVER_URL . (USE_PATH_INFO ? VIRTUAL_PATH . '/' : SCRIPT_NAME));
449
450 //////////////////////////////////////////////////////////////////
451 // Select database
452 //
453 if (empty($DBParams['dbtype']))
454     $DBParams['dbtype'] = 'dba';
455
456 if (!defined('THEME'))
457     define('THEME', 'default');
458
459 update_locale(isset($LANG) ? $LANG : DEFAULT_LANGUAGE);
460
461 if (!defined('WIKI_NAME'))
462     define('WIKI_NAME', _("An unnamed PhpWiki"));
463
464 if (!defined('HOME_PAGE'))
465     define('HOME_PAGE', _("HomePage"));
466
467 // FIXME: delete
468 // Access log
469 if (!defined('ACCESS_LOG'))
470      define('ACCESS_LOG', '');
471
472 // FIXME: delete
473 // Get remote host name, if apache hasn't done it for us
474 if (empty($HTTP_SERVER_VARS['REMOTE_HOST']) && ENABLE_REVERSE_DNS)
475      $HTTP_SERVER_VARS['REMOTE_HOST'] = gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR']);
476
477 // check whether the crypt() function is needed and present
478 if (defined('ENCRYPTED_PASSWD') && !function_exists('crypt')) {
479     $error = sprintf(_("Encrypted passwords cannot be used: %s."),
480                      "'function crypt()' not available in this version of php");
481     trigger_error($error);
482 }
483
484 if (!defined('ADMIN_PASSWD') or ADMIN_PASSWD == '')
485     trigger_error(_("The admin password cannot be empty. Please update your /index.php"));
486
487 if (defined('USE_DB_SESSION') and USE_DB_SESSION) {
488     if (! $DBParams['db_session_table'] ) {
489         trigger_error(_("Empty db_session_table. Turn USE_DB_SESSION off or define the table name."), 
490                           E_USER_ERROR);
491         // this is flawed. constants cannot be changed.
492         define('USE_DB_SESSION',false);
493     }
494 } else {
495     // default: true (since v1.3.8)
496     if (!defined('USE_DB_SESSION'))
497         define('USE_DB_SESSION',true);
498 }
499 // legacy:
500 if (!defined('ENABLE_USER_NEW')) define('ENABLE_USER_NEW',true);
501 if (!defined('ALLOW_USER_LOGIN'))
502     define('ALLOW_USER_LOGIN', defined('ALLOW_USER_PASSWORDS') && ALLOW_USER_PASSWORDS);
503 if (!defined('ALLOW_ANON_USER')) define('ALLOW_ANON_USER', true); 
504 if (!defined('ALLOW_ANON_EDIT')) define('ALLOW_ANON_EDIT', false); 
505 if (!defined('REQUIRE_SIGNIN_BEFORE_EDIT')) define('REQUIRE_SIGNIN_BEFORE_EDIT', ! ALLOW_ANON_EDIT);
506 if (!defined('ALLOW_BOGO_LOGIN')) define('ALLOW_BOGO_LOGIN', true);
507
508 if (ALLOW_USER_LOGIN and !empty($DBAuthParams) and empty($DBAuthParams['auth_dsn'])) {
509     if (isset($DBParams['dsn']))
510         $DBAuthParams['auth_dsn'] = $DBParams['dsn'];
511 }
512
513 // For emacs users
514 // Local Variables:
515 // mode: php
516 // tab-width: 8
517 // c-basic-offset: 4
518 // c-hanging-comment-ender-p: nil
519 // indent-tabs-mode: nil
520 // End:
521 ?>