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