]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/config.php
check default config values
[SourceForge/phpwiki.git] / lib / config.php
1 <?php
2 rcs_id('$Id: config.php,v 1.100 2004-04-26 12:15:01 rurban Exp $');
3 /*
4  * NOTE: The settings here should probably not need to be changed.
5  * The user-configurable settings have been moved to IniConfig.php
6  * The run-time code have been moved to lib/IniConfig.php:fix_configs()
7  */
8  
9 if (!defined("LC_ALL")) {
10     // Backward compatibility (for PHP < 4.0.5)
11     define("LC_ALL",   0);
12     define("LC_CTYPE", 2);
13 }
14
15 function isCGI() {
16     return @preg_match('/CGI/',$GLOBALS['HTTP_ENV_VARS']['GATEWAY_INTERFACE']);
17 }
18
19 /*
20 // copy some $_ENV vars to $_SERVER for CGI compatibility. php does it automatically since when?
21 if (isCGI()) {
22     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) {
23         $GLOBALS['HTTP_SERVER_VARS'][$key] = &$GLOBALS['HTTP_ENV_VARS'][$key];
24     }
25 }
26 */
27
28 // essential internal stuff
29 set_magic_quotes_runtime(0);
30
31 /** 
32  * Browser Detection Functions
33  *
34  * Current Issues:
35  *  NS/IE < 4.0 doesn't accept < ? xml version="1.0" ? >
36  *  NS/IE < 4.0 cannot display PNG
37  *  NS/IE < 4.0 cannot display all XHTML tags
38  *  NS < 5.0 needs textarea wrap=virtual
39  *  IE55 has problems with transparent PNG's
40  * @author: ReiniUrban
41  */
42 function browserAgent() {
43     static $HTTP_USER_AGENT = false;
44     if (!$HTTP_USER_AGENT)
45         $HTTP_USER_AGENT = @$GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
46     if (!$HTTP_USER_AGENT) // CGI
47         $HTTP_USER_AGENT = $GLOBALS['HTTP_ENV_VARS']['HTTP_USER_AGENT'];
48     return $HTTP_USER_AGENT;
49 }
50 function browserDetect($match) {
51     return strstr(browserAgent(), $match);
52 }
53 // returns a similar number for Netscape/Mozilla (gecko=5.0)/IE/Opera features.
54 function browserVersion() {
55     if (strstr(browserAgent(),"Mozilla/4.0 (compatible; MSIE"))
56         return (float) substr(browserAgent(),30);
57     else
58         return (float) substr(browserAgent(),8);
59 }
60 function isBrowserIE() {
61     return (browserDetect('Mozilla/') and 
62             browserDetect('MSIE'));
63 }
64 // problem with transparent PNG's
65 function isBrowserIE55() {
66     return (isBrowserIE() and 
67             browserVersion() > 5.1 and browserVersion() < 6.0);
68 }
69 function isBrowserNetscape() {
70     return (browserDetect('Mozilla/') and 
71             ! browserDetect('Gecko/') and
72             ! browserDetect('MSIE'));
73 }
74 // NS3 or less
75 function isBrowserNS3() {
76     return (isBrowserNetscape() and browserVersion() < 4.0);
77 }
78
79 /**
80  * Smart? setlocale().
81  *
82  * This is a version of the builtin setlocale() which is
83  * smart enough to try some alternatives...
84  *
85  * @param mixed $category
86  * @param string $locale
87  * @return string The new locale, or <code>false</code> if unable
88  *  to set the requested locale.
89  * @see setlocale
90  */
91 function guessing_setlocale ($category, $locale) {
92     if ($res = setlocale($category, $locale))
93         return $res;
94     $alt = array('en' => array('C', 'en_US', 'en_GB', 'en_AU', 'en_CA', 'english'),
95                  'de' => array('de_DE', 'de_DE', 'de_DE@euro', 
96                                'de_AT@euro', 'de_AT', 'German_Austria.1252', 'deutsch', 
97                                'german', 'ge'),
98                  'es' => array('es_ES', 'es_MX', 'es_AR', 'spanish'),
99                  'nl' => array('nl_NL', 'dutch'),
100                  'fr' => array('fr_FR', 'français', 'french'),
101                  'it' => array('it_IT'),
102                  'sv' => array('sv_SE'),
103                  'ja' => array('ja_JP','ja_JP.eucJP','japanese.euc'),
104                  //'zh' => array('zh_TW', 'zh_CN')
105                  );
106     if (strlen($locale) == 2)
107         $lang = $locale;
108     else 
109         list ($lang) = split('_', $locale);
110     if (!isset($alt[$lang]))
111         return false;
112         
113     foreach ($alt[$lang] as $try) {
114         if ($res = setlocale($category, $try))
115             return $res;
116         // Try with charset appended...
117         $try = $try . '.' . $GLOBALS['charset'];
118         if ($res = setlocale($category, $try))
119             return $res;
120         foreach (array('@', ".", '_') as $sep) {
121             list ($try) = split($sep, $try);
122             if ($res = setlocale($category, $try))
123                 return $res;
124         }
125     }
126     return false;
127
128     // A standard locale name is typically of  the  form
129     // language[_territory][.codeset][@modifier],  where  language is
130     // an ISO 639 language code, territory is an ISO 3166 country code,
131     // and codeset  is  a  character  set or encoding identifier like
132     // ISO-8859-1 or UTF-8.
133 }
134
135 function update_locale($loc) {
136     require_once(dirname(__FILE__)."/FileFinder.php");
137     $newlocale = guessing_setlocale(LC_ALL, $loc);
138     if (!$newlocale) {
139         //trigger_error(sprintf(_("Can't setlocale(LC_ALL,'%s')"), $loc), E_USER_NOTICE);
140         // => LC_COLLATE=C;LC_CTYPE=German_Austria.1252;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C
141         //$loc = setlocale(LC_CTYPE, '');  // pull locale from environment.
142         $newlocale = FileFinder::_get_lang();
143         list ($newlocale,) = split('_', $newlocale, 2);
144         //$GLOBALS['LANG'] = $loc;
145         //$newlocale = $loc;
146         //return false;
147     }
148     //if (substr($newlocale,0,2) == $loc) // don't update with C or failing setlocale
149     $GLOBALS['LANG'] = $loc;
150     // Try to put new locale into environment (so any
151     // programs we run will get the right locale.)
152     //
153     // If PHP is in safe mode, this is not allowed,
154     // so hide errors...
155     @putenv("LC_ALL=$newlocale");
156     @putenv("LANG=$newlocale");
157     @putenv("LANGUAGE=$newlocale");
158     
159     if (!function_exists ('bindtextdomain'))  {
160         // Reinitialize translation array.
161         global $locale;
162         $locale = array();
163         // do reinit to purge PHP's static cache
164         if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok','reinit')) ) {
165             include($lcfile);
166         }
167     }
168
169     // To get the POSIX character classes in the PCRE's (e.g.
170     // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
171     // to set the locale, using setlocale().
172     //
173     // The problem is which locale to set?  We would like to recognize all
174     // upper-case characters in the iso-8859-1 character set as upper-case
175     // characters --- not just the ones which are in the current $LANG.
176     //
177     // As it turns out, at least on my system (Linux/glibc-2.2) as long as
178     // you setlocale() to anything but "C" it works fine.  (I'm not sure
179     // whether this is how it's supposed to be, or whether this is a bug
180     // in the libc...)
181     //
182     // We don't currently use the locale setting for anything else, so for
183     // now, just set the locale to US English.
184     //
185     // FIXME: Not all environments may support en_US?  We should probably
186     // have a list of locales to try.
187     if (setlocale(LC_CTYPE, 0) == 'C') {
188         $x = setlocale(LC_CTYPE, 'en_US.' . $GLOBALS['charset']);
189     } else {
190         $x = setlocale(LC_CTYPE, $newlocale);
191     }
192
193     return $newlocale;
194 }
195
196 /** string pcre_fix_posix_classes (string $regexp)
197 *
198 * Older version (pre 3.x?) of the PCRE library do not support
199 * POSIX named character classes (e.g. [[:alnum:]]).
200 *
201 * This is a helper function which can be used to convert a regexp
202 * which contains POSIX named character classes to one that doesn't.
203 *
204 * All instances of strings like '[:<class>:]' are replaced by the equivalent
205 * enumerated character class.
206 *
207 * Implementation Notes:
208 *
209 * Currently we use hard-coded values which are valid only for
210 * ISO-8859-1.  Also, currently on the classes [:alpha:], [:alnum:],
211 * [:upper:] and [:lower:] are implemented.  (The missing classes:
212 * [:blank:], [:cntrl:], [:digit:], [:graph:], [:print:], [:punct:],
213 * [:space:], and [:xdigit:] could easily be added if needed.)
214 *
215 * This is a hack.  I tried to generate these classes automatically
216 * using ereg(), but discovered that in my PHP, at least, ereg() is
217 * slightly broken w.r.t. POSIX character classes.  (It includes
218 * "\xaa" and "\xba" in [:alpha:].)
219 *
220 * So for now, this will do.  --Jeff <dairiki@dairiki.org> 14 Mar, 2001
221 */
222 function pcre_fix_posix_classes ($regexp) {
223     global $charset;
224     // First check to see if our PCRE lib supports POSIX character
225     // classes.  If it does, there's nothing to do.
226     if (preg_match('/[[:upper:]]/', 'Ä'))
227         return $regexp;
228
229     static $classes = array(
230                             'alnum' => "0-9A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
231                             'alpha' => "A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
232                             'upper' => "A-Z\xc0-\xd6\xd8-\xde",
233                             'lower' => "a-z\xdf-\xf6\xf8-\xff"
234                             );
235     if (!isset($charset))
236         $charset = CHARSET; // FIXME: get rid of constant. pref is dynamic and language specific
237     if (in_array($GLOBALS['LANG'],array('ja','zh')))
238         $charset = 'utf-8';
239     if (strtolower($charset) == 'utf-8') { // thanks to John McPherson
240         // until posix class names/pcre work with utf-8
241         // utf-8 non-ascii chars: most common (eg western) latin chars are 0xc380-0xc3bf
242         // we currently ignore other less common non-ascii characters
243         // (eg central/east european) latin chars are 0xc432-0xcdbf and 0xc580-0xc5be
244         // and indian/cyrillic/asian languages
245         
246         // this replaces [[:lower:]] with utf-8 match (Latin only)
247         $regexp = preg_replace('/\[\[\:lower\:\]\]/','(?:[a-z]|\xc3[\x9f-\xbf]|\xc4[\x81\x83\x85\x87])',
248                                $regexp);
249         // this replaces [[:upper:]] with utf-8 match (Latin only)
250         $regexp = preg_replace('/\[\[\:upper\:\]\]/','(?:[A-Z]|\xc3[\x80-\x9e]|\xc4[\x80\x82\x84\x86])',
251                                $regexp);
252     }
253    
254     $keys = join('|', array_keys($classes));
255
256     return preg_replace("/\[:($keys):]/e", '$classes["\1"]', $regexp);
257 }
258
259 function deduce_script_name() {
260     $s = &$GLOBALS['HTTP_SERVER_VARS'];
261     $script = @$s['SCRIPT_NAME'];
262     if (empty($script) or $script[0] != '/') {
263         // Some places (e.g. Lycos) only supply a relative name in
264         // SCRIPT_NAME, but give what we really want in SCRIPT_URL.
265         if (!empty($s['SCRIPT_URL']))
266             $script = $s['SCRIPT_URL'];
267     }
268     return $script;
269 }
270
271 function IsProbablyRedirectToIndex () {
272     // This might be a redirect to the DirectoryIndex,
273     // e.g. REQUEST_URI = /dir/  got redirected
274     // to SCRIPT_NAME = /dir/index.php
275
276     // In this case, the proper virtual path is still
277     // $SCRIPT_NAME, since pages appear at
278     // e.g. /dir/index.php/HomePage.
279
280     $requri = preg_quote($GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'], '%');
281     return preg_match("%^${requri}[^/]*$%", $GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
282 }
283
284 // $Log: not supported by cvs2svn $
285 // Revision 1.99  2004/04/21 14:04:24  zorloc
286 // 'Require lib/FileFinder.php' necessary to allow for call to FindLocalizedFile().
287 //
288 // Revision 1.98  2004/04/20 18:10:28  rurban
289 // config refactoring:
290 //   FileFinder is needed for WikiFarm scripts calling index.php
291 //   config run-time calls moved to lib/IniConfig.php:fix_configs()
292 //   added PHPWIKI_DIR smart-detection code (Theme finder)
293 //   moved FileFind to lib/FileFinder.php
294 //   cleaned lib/config.php
295 //
296 // Revision 1.97  2004/04/18 01:11:52  rurban
297 // more numeric pagename fixes.
298 // fixed action=upload with merge conflict warnings.
299 // charset changed from constant to global (dynamic utf-8 switching)
300 //
301
302 // For emacs users
303 // Local Variables:
304 // mode: php
305 // tab-width: 8
306 // c-basic-offset: 4
307 // c-hanging-comment-ender-p: nil
308 // indent-tabs-mode: nil
309 // End:
310 ?>