]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/config.php
*** empty log message ***
[SourceForge/phpwiki.git] / lib / config.php
1 <?php
2 rcs_id('$Id: config.php,v 1.63 2002-09-09 13:30:41 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 // I think this putenv is unnecessary, and it causes trouble
71 // if PHP's safe_mode is enabled:
72 //putenv("LC_ALL=$LANG");
73
74 if (!function_exists ('bindtextdomain')) {
75     $locale = array();
76
77     function gettext ($text) { 
78         global $locale;
79         if (!empty ($locale[$text]))
80             return $locale[$text];
81         return $text;
82     }
83
84     function _ ($text) {
85         return gettext($text);
86     }
87
88     if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok')) ) {
89         include($lcfile);
90     }
91 }
92
93 // Setup localisation
94 // This is currently broken, after trying to enable dynamic UserPreferences 
95 // on the language.
96 function update_locale ($language) {
97     global $locale, $LC_ALL, $language_locales;
98     if (!$language and $GLOBALS['default_language']) {
99         return $GLOBALS['default_language'];
100     }
101
102     // shortterm LANG fix. We really should define LC_ALL as "C" and LANG as "en"
103     if ($language == 'C') {
104         $language = 'en'; $LC_ALL = 'C';
105     }
106     if (empty($LC_ALL)) {
107         if (empty($language_locales[$language]))
108             $LC_ALL = $language;
109         else
110             $LC_ALL = $language_locales[$language];
111     }
112     if (empty($LC_ALL))
113        $LC_ALL = $language;
114
115     // Fixme: Currently we just check the dirs under locale for all 
116     // available languages, but with setlocale we must use the long form, 
117     // like 'de_DE','nl_NL', 'es_MX', 'es_AR', 'fr_FR'. For Windows maybe even 'german'.
118     $result = setlocale(LC_ALL, $LC_ALL);
119     if (!$result and !($result = setlocale(LC_ALL, substr($LC_ALL,0,2)))) {
120         putenv("LANG=$LC_ALL");
121         // The system supported locale. E.g. my Windows returns "German_Austria.1252"
122         $result = setlocale(LC_ALL,'');
123     }
124     if ($result) {
125         $LC_ALL = $result;
126         putenv("LC_ALL=$LC_ALL");
127     }
128     putenv("LANG=$LC_ALL");
129             
130     if (!function_exists ('bindtextdomain')) {
131         if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok')) ) {
132             include($lcfile);
133         }
134     } else {
135         if (empty($language_locales[$language])) {
136             trigger_error(sprintf(_("No default locale for this language '%s'"), $language), E_USER_NOTICE);
137         }
138         // Setup localisation
139         $f = FindFile("locale", false, true);
140         
141         bindtextdomain ("phpwiki", $f);
142         textdomain ("phpwiki");
143     }
144     $GLOBALS['LANG'] = $language;
145 }
146 $default_language = $LANG;
147 update_locale ($LANG);
148
149 // To get the POSIX character classes in the PCRE's (e.g.
150 // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
151 // to set the locale, using setlocale().
152 //
153 // The problem is which locale to set?  We would like to recognize all
154 // upper-case characters in the iso-8859-1 character set as upper-case
155 // characters --- not just the ones which are in the current $LANG.
156 //
157 // As it turns out, at least on my system (Linux/glibc-2.2) as long as
158 // you setlocale() to anything but "C" it works fine.  (I'm not sure
159 // whether this is how it's supposed to be, or whether this is a bug
160 // in the libc...)
161 //
162 // We don't currently use the locale setting for anything else, so for
163 // now, just set the locale to US English.
164 //
165 // FIXME: Not all environments may support en_US?  We should probably
166 // have a list of locales to try.
167 if (setlocale(LC_CTYPE, 0) == 'C')
168      setlocale(LC_CTYPE, 'en_US.' . CHARSET );
169
170 /** string pcre_fix_posix_classes (string $regexp)
171 *
172 * Older version (pre 3.x?) of the PCRE library do not support
173 * POSIX named character classes (e.g. [[:alnum:]]).
174 *
175 * This is a helper function which can be used to convert a regexp
176 * which contains POSIX named character classes to one that doesn't.
177 *
178 * All instances of strings like '[:<class>:]' are replaced by the equivalent
179 * enumerated character class.
180 *
181 * Implementation Notes:
182 *
183 * Currently we use hard-coded values which are valid only for
184 * ISO-8859-1.  Also, currently on the classes [:alpha:], [:alnum:],
185 * [:upper:] and [:lower:] are implemented.  (The missing classes:
186 * [:blank:], [:cntrl:], [:digit:], [:graph:], [:print:], [:punct:],
187 * [:space:], and [:xdigit:] could easily be added if needed.)
188 *
189 * This is a hack.  I tried to generate these classes automatically
190 * using ereg(), but discovered that in my PHP, at least, ereg() is
191 * slightly broken w.r.t. POSIX character classes.  (It includes
192 * "\xaa" and "\xba" in [:alpha:].)
193 *
194 * So for now, this will do.  --Jeff <dairiki@dairiki.org> 14 Mar, 2001
195 */
196 function pcre_fix_posix_classes ($regexp) {
197     // First check to see if our PCRE lib supports POSIX character
198     // classes.  If it does, there's nothing to do.
199     if (preg_match('/[[:upper:]]/', 'Ä'))
200         return $regexp;
201
202     static $classes = array(
203                             'alnum' => "0-9A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
204                             'alpha' => "A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
205                             'upper' => "A-Z\xc0-\xd6\xd8-\xde",
206                             'lower' => "a-z\xdf-\xf6\xf8-\xff"
207                             );
208
209     $keys = join('|', array_keys($classes));
210
211     return preg_replace("/\[:($keys):]/e", '$classes["\1"]', $regexp);
212 }
213
214 $WikiNameRegexp = pcre_fix_posix_classes($WikiNameRegexp);
215
216 //////////////////////////////////////////////////////////////////
217 // Autodetect URL settings:
218 //
219 if (!defined('SERVER_NAME')) define('SERVER_NAME', $HTTP_SERVER_VARS['SERVER_NAME']);
220 if (!defined('SERVER_PORT')) define('SERVER_PORT', $HTTP_SERVER_VARS['SERVER_PORT']);
221 if (!defined('SERVER_PROTOCOL')) {
222     if (empty($HTTP_SERVER_VARS['HTTPS']) || $HTTP_SERVER_VARS['HTTPS'] == 'off')
223         define('SERVER_PROTOCOL', 'http');
224     else
225         define('SERVER_PROTOCOL', 'https');
226 }
227
228 if (!defined('SCRIPT_NAME')) define('SCRIPT_NAME', $HTTP_SERVER_VARS['SCRIPT_NAME']);
229
230 if (!defined('DATA_PATH'))   define('DATA_PATH', dirname(SCRIPT_NAME));
231
232 if (!defined('USE_PATH_INFO'))
233 {
234     /*
235      * If SCRIPT_NAME does not look like php source file,
236      * or user cgi we assume that php is getting run by an
237      * action handler in /cgi-bin.  In this case,
238      * I think there is no way to get Apache to pass
239      * useful PATH_INFO to the php script (PATH_INFO
240      * is used to the the php interpreter where the
241      * php script is...)
242      */
243     if (php_sapi_name() == 'apache')
244         define('USE_PATH_INFO', true);
245     else
246         define('USE_PATH_INFO', ereg('\.(php3?|cgi)$', $SCRIPT_NAME));
247 }
248
249
250 function IsProbablyRedirectToIndex () {
251     // This might be a redirect to the DirectoryIndex,
252     // e.g. REQUEST_URI = /dir/  got redirected
253     // to SCRIPT_NAME = /dir/index.php
254
255     // In this case, the proper virtual path is still
256     // $SCRIPT_NAME, since pages appear at
257     // e.g. /dir/index.php/HomePage.
258
259     $requri = preg_quote($GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'], '%');
260     return preg_match("%^${requri}[^/]*$%", $GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
261 }
262
263
264 if (!defined('VIRTUAL_PATH')) {
265     // We'd like to auto-detect when the cases where apaches
266     // 'Action' directive (or similar means) is used to
267     // redirect page requests to a cgi-handler.
268     //
269     // In cases like this, requests for e.g. /wiki/HomePage
270     // get redirected to a cgi-script called, say,
271     // /path/to/wiki/index.php.  The script gets all
272     // of /wiki/HomePage as it's PATH_INFO.
273     //
274     // The problem is:
275     //   How to detect when this has happened reliably?
276     //   How to pick out the "virtual path" (in this case '/wiki')?
277     //
278     // (Another time an redirect might occur is to a DirectoryIndex
279     // -- the requested URI is '/wikidir/', the request gets
280     // passed to '/wikidir/index.php'.  In this case, the
281     // proper VIRTUAL_PATH is '/wikidir/index.php', since the
282     // pages will appear at e.g. '/wikidir/index.php/HomePage'.
283     //
284
285     $REDIRECT_URL = &$HTTP_SERVER_VARS['REDIRECT_URL'];
286     if (USE_PATH_INFO and isset($REDIRECT_URL)
287         and ! IsProbablyRedirectToIndex()) {
288         // FIXME: This is a hack, and won't work if the requested
289         // pagename has a slash in it.
290         define('VIRTUAL_PATH', dirname($REDIRECT_URL . 'x'));
291     } else {
292         define('VIRTUAL_PATH', SCRIPT_NAME);
293     }
294 }
295
296 if (SERVER_PORT
297     && SERVER_PORT != (SERVER_PROTOCOL == 'https' ? 443 : 80)) {
298     define('SERVER_URL',
299            SERVER_PROTOCOL . '://' . SERVER_NAME . ':' . SERVER_PORT);
300 }
301 else {
302     define('SERVER_URL',
303            SERVER_PROTOCOL . '://' . SERVER_NAME);
304 }
305
306 if (VIRTUAL_PATH != SCRIPT_NAME) {
307     // Apache action handlers are used.
308     define('PATH_INFO_PREFIX', VIRTUAL_PATH . '/');
309 }
310 else
311     define('PATH_INFO_PREFIX', '/');
312
313
314 define('BASE_URL',
315        SERVER_URL . (USE_PATH_INFO ? VIRTUAL_PATH . '/' : SCRIPT_NAME));
316
317 //////////////////////////////////////////////////////////////////
318 // Select database
319 //
320 if (empty($DBParams['dbtype']))
321     $DBParams['dbtype'] = 'dba';
322
323 if (!defined('WIKI_NAME'))
324     define('WIKI_NAME', _("An unnamed PhpWiki"));
325
326 if (!defined('THEME'))
327     define('THEME', 'default');
328
329 if (!defined('HOME_PAGE'))
330     define('HOME_PAGE', _("HomePage"));
331
332
333 // FIXME: delete
334 // Access log
335 if (!defined('ACCESS_LOG'))
336      define('ACCESS_LOG', '');
337
338 // FIXME: delete
339 // Get remote host name, if apache hasn't done it for us
340 if (empty($HTTP_SERVER_VARS['REMOTE_HOST']) && ENABLE_REVERSE_DNS)
341      $HTTP_SERVER_VARS['REMOTE_HOST'] = gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR']);
342
343 // check whether the crypt() function is needed and present
344 if (defined('ENCRYPTED_PASSWD') && !function_exists('crypt')) {
345     $error = sprintf(_("Encrypted passwords cannot be used: %s."),
346                      "'function crypt()' not available in this version of php");
347     trigger_error($error);
348 }
349
350 if (!defined('ADMIN_PASSWD') or ADMIN_PASSWD == '')
351     trigger_error(_("The admin password cannot be empty. Please update your /index.php"));
352
353 // For emacs users
354 // Local Variables:
355 // mode: php
356 // tab-width: 8
357 // c-basic-offset: 4
358 // c-hanging-comment-ender-p: nil
359 // indent-tabs-mode: nil
360 // End:
361 ?>