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