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