]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/config.php
Remove LC_MESSAGES/phpwiki.php
[SourceForge/phpwiki.git] / lib / config.php
1 <?php
2
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 has been moved to lib/IniConfig.php:fix_configs()
7  */
8
9 if (!defined("LC_ALL")) {
10     define("LC_ALL", 0);
11     define("LC_CTYPE", 2);
12 }
13 // debug flags:
14 define ('_DEBUG_VERBOSE', 1); // verbose msgs and add validator links on footer
15 define ('_DEBUG_PAGELINKS', 2); // list the extraced pagelinks at the top of each pages
16 define ('_DEBUG_PARSER', 4); // verbose parsing steps
17 define ('_DEBUG_TRACE', 8); // test php memory usage, prints php debug backtraces
18 define ('_DEBUG_INFO', 16);
19 define ('_DEBUG_APD', 32); // APD tracing/profiling
20 define ('_DEBUG_LOGIN', 64); // verbose login debug-msg (settings and reason for failure)
21 define ('_DEBUG_SQL', 128); // force check db, force optimize, print some debugging logs
22 define ('_DEBUG_REMOTE', 256); // remote debug into subrequests (xmlrpc, ajax, wikiwyg, ...)
23 // or test local SearchHighlight.
24 // internal links have persistent ?start_debug=1
25
26 function isCGI()
27 {
28     return (substr(php_sapi_name(), 0, 3) == 'cgi' and
29         isset($GLOBALS['HTTP_ENV_VARS']['GATEWAY_INTERFACE']) and
30             @preg_match('/CGI/', $GLOBALS['HTTP_ENV_VARS']['GATEWAY_INTERFACE']));
31 }
32
33 // essential internal stuff
34 if (!check_php_version(5, 3)) {
35     set_magic_quotes_runtime(0);
36 }
37
38 /**
39  * Browser Detection Functions
40  *
41  * @author: ReiniUrban
42  */
43 function browserAgent()
44 {
45     static $HTTP_USER_AGENT = false;
46     if ($HTTP_USER_AGENT !== false) return $HTTP_USER_AGENT;
47     if (!$HTTP_USER_AGENT)
48         $HTTP_USER_AGENT = @$GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
49     if (!$HTTP_USER_AGENT) // CGI
50         $HTTP_USER_AGENT = @$GLOBALS['HTTP_ENV_VARS']['HTTP_USER_AGENT'];
51     if (!$HTTP_USER_AGENT) // local CGI testing
52         $HTTP_USER_AGENT = 'none';
53     return $HTTP_USER_AGENT;
54 }
55
56 function browserDetect($match)
57 {
58     return (strpos(strtolower(browserAgent()), strtolower($match)) !== false);
59 }
60
61 // returns a similar number for Netscape/Mozilla (gecko=5.0)/IE/Opera features.
62 function browserVersion()
63 {
64     $agent = browserAgent();
65     if (strstr($agent, "Mozilla/4.0 (compatible; MSIE"))
66         return (float)substr($agent, 30);
67     elseif (strstr($agent, "Mozilla/5.0 (compatible; Konqueror/"))
68         return (float)substr($agent, 36); elseif (strstr($agent, "AppleWebKit/"))
69         return (float)substr($agent, strpos($agent, "AppleWebKit/") + 12); else
70         return (float)substr($agent, 8);
71 }
72
73 function isBrowserIE()
74 {
75     return (browserDetect('Mozilla/') and
76         browserDetect('MSIE'));
77 }
78
79 // must omit display alternate stylesheets: konqueror 3.1.4
80 // http://sourceforge.net/tracker/index.php?func=detail&aid=945154&group_id=6121&atid=106121
81 function isBrowserKonqueror($version = false)
82 {
83     if ($version) return browserDetect('Konqueror/') and browserVersion() >= $version;
84     return browserDetect('Konqueror/');
85 }
86
87 // MacOSX Safari has certain limitations. Need detection and patches.
88 // * no <object>, only <embed>
89 function isBrowserSafari($version = false)
90 {
91     $found = browserDetect('Spoofer/');
92     $found = browserDetect('AppleWebKit/') or $found;
93     if ($version) return $found and browserVersion() >= $version;
94     return $found;
95 }
96
97 function isBrowserOpera($version = false)
98 {
99     if ($version) return browserDetect('Opera/') and browserVersion() >= $version;
100     return browserDetect('Opera/');
101 }
102
103 /**
104  * If $LANG is undefined:
105  * Smart client language detection, based on our supported languages
106  * HTTP_ACCEPT_LANGUAGE="de-at,en;q=0.5"
107  *   => "de"
108  * We should really check additionally if the i18n HomePage version is defined.
109  * So must defer this to the request loop.
110  */
111 function guessing_lang($languages = false)
112 {
113     if (!$languages) {
114         // make this faster
115         $languages = array("en", "de", "es", "fr", "it", "ja", "zh", "nl", "sv");
116     }
117
118     $accept = false;
119     if (isset($GLOBALS['request'])) // in fixup-dynamic-config there's no request yet
120         $accept = $GLOBALS['request']->get('HTTP_ACCEPT_LANGUAGE');
121     elseif (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
122         $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
123
124     if ($accept) {
125         $lang_list = array();
126         $list = explode(",", $accept);
127         for ($i = 0; $i < count($list); $i++) {
128             $pos = strchr($list[$i], ";");
129             if ($pos === false) {
130                 // No Q it is only a locale...
131                 $lang_list[$list[$i]] = 100;
132             } else {
133                 // Has a Q rating
134                 $q = explode(";", $list[$i]);
135                 $loc = $q[0];
136                 $q = explode("=", $q[1]);
137                 $lang_list[$loc] = $q[1] * 100;
138             }
139         }
140
141         // sort by q desc
142         arsort($lang_list);
143
144         // compare with languages, ignoring sublang and charset
145         foreach ($lang_list as $lang => $q) {
146             if (in_array($lang, $languages))
147                 return $lang;
148             // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
149             // de-DE => de-DE, de
150             foreach (array('@', '.', '_') as $sep) {
151                 if (($tail = strchr($lang, $sep))) {
152                     $lang_short = substr($lang, 0, -strlen($tail));
153                     if (in_array($lang_short, $languages))
154                         return $lang_short;
155                 }
156             }
157             if ($pos = strpos($lang, "-") and in_array(substr($lang, 0, $pos), $languages))
158                 return substr($lang, 0, $pos);
159         }
160     }
161     return $languages[0];
162 }
163
164 /**
165  * Smart setlocale().
166  *
167  * This is a version of the builtin setlocale() which is
168  * smart enough to try some alternatives...
169  *
170  * @param mixed $category
171  * @param string $locale
172  * @return string The new locale, or <code>false</code> if unable
173  *  to set the requested locale.
174  * @see setlocale
175  * [56ms]
176  */
177 function guessing_setlocale($category, $locale)
178 {
179     $alt = array('en' => array('C', 'en_US', 'en_GB', 'en_AU', 'en_CA', 'english'),
180         'de' => array('de_DE', 'de_DE', 'de_DE@euro',
181             'de_AT@euro', 'de_AT', 'German_Austria.1252', 'deutsch',
182             'german', 'ge'),
183         'es' => array('es_ES', 'es_MX', 'es_AR', 'spanish'),
184         'nl' => array('nl_NL', 'dutch'),
185         'fr' => array('fr_FR', 'français', 'french'),
186         'it' => array('it_IT'),
187         'sv' => array('sv_SE'),
188         'ja.utf-8' => array('ja_JP', 'ja_JP.utf-8', 'japanese'),
189         'ja.euc-jp' => array('ja_JP', 'ja_JP.eucJP', 'japanese.euc'),
190         'zh' => array('zh_TW', 'zh_CN'),
191     );
192     if (!$locale or $locale == 'C') {
193         // do the reverse: return the detected locale collapsed to our LANG
194         $locale = setlocale($category, '');
195         if ($locale) {
196             if (strstr($locale, '_')) list ($lang) = explode('_', $locale);
197             else $lang = $locale;
198             if (strlen($lang) > 2) {
199                 foreach ($alt as $try => $locs) {
200                     if (in_array($locale, $locs) or in_array($lang, $locs)) {
201                         //if (empty($GLOBALS['LANG'])) $GLOBALS['LANG'] = $try;
202                         return $try;
203                     }
204                 }
205             }
206         }
207     }
208     if (strlen($locale) == 2)
209         $lang = $locale;
210     else
211         list ($lang) = explode('_', $locale);
212     if (!isset($alt[$lang]))
213         return false;
214
215     foreach ($alt[$lang] as $try) {
216         if ($res = setlocale($category, $try))
217             return $res;
218         // Try with charset appended...
219         $try = $try . '.' . 'UTF-8';
220         if ($res = setlocale($category, $try))
221             return $res;
222         foreach (array(".", '@', '_') as $sep) {
223             if ($i = strpos($try, $sep)) {
224                 $try = substr($try, 0, $i);
225                 if (($res = setlocale($category, $try)))
226                     return $res;
227             }
228         }
229     }
230     return false;
231     // A standard locale name is typically of  the  form
232     // language[_territory][.codeset][@modifier],  where  language is
233     // an ISO 639 language code, territory is an ISO 3166 country code,
234     // and codeset  is  a  character  set or encoding identifier like
235     // ISO-8859-1 or UTF-8.
236 }
237
238 // [99ms]
239 function update_locale($loc)
240 {
241     if ($loc == 'C' or $loc == 'en') {
242         return '';
243     }
244     // $LANG or DEFAULT_LANGUAGE is too less information, at least on unix for
245     // setlocale(), for bindtextdomain() to succeed.
246     $setlocale = guessing_setlocale(LC_ALL, $loc); // [56ms]
247     if (!$setlocale) { // system has no locale for this language, so gettext might fail
248         $setlocale = FileFinder::_get_lang();
249         list ($setlocale,) = explode('_', $setlocale, 2);
250         $setlocale = guessing_setlocale(LC_ALL, $setlocale); // try again
251         if (!$setlocale) $setlocale = $loc;
252     }
253     // Try to put new locale into environment (so any
254     // programs we run will get the right locale.)
255     if (function_exists('bindtextdomain')) {
256         // If PHP is in safe mode, this is not allowed,
257         // so hide errors...
258         @putenv("LC_ALL=$setlocale");
259         @putenv("LANG=$loc");
260         @putenv("LANGUAGE=$loc");
261     }
262
263     // To get the POSIX character classes in the PCRE's (e.g.
264     // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
265     // to set the locale, using setlocale().
266     //
267     // The problem is which locale to set?  We would like to recognize all
268     // upper-case characters in the iso-8859-1 character set as upper-case
269     // characters --- not just the ones which are in the current $LANG.
270     //
271     // As it turns out, at least on my system (Linux/glibc-2.2) as long as
272     // you setlocale() to anything but "C" it works fine.  (I'm not sure
273     // whether this is how it's supposed to be, or whether this is a bug
274     // in the libc...)
275     //
276     // We don't currently use the locale setting for anything else, so for
277     // now, just set the locale to US English.
278     //
279     // FIXME: Not all environments may support en_US?  We should probably
280     // have a list of locales to try.
281     if (setlocale(LC_CTYPE, 0) == 'C') {
282         $x = setlocale(LC_CTYPE, 'en_US.UTF-8');
283     } else {
284         $x = setlocale(LC_CTYPE, $setlocale);
285     }
286
287     return $loc;
288 }
289
290 function deduce_script_name()
291 {
292     $s = &$_SERVER;
293     $script = @$s['SCRIPT_NAME'];
294     if (empty($script) or $script[0] != '/') {
295         // Some places (e.g. Lycos) only supply a relative name in
296         // SCRIPT_NAME, but give what we really want in SCRIPT_URL.
297         if (!empty($s['SCRIPT_URL']))
298             $script = $s['SCRIPT_URL'];
299     }
300     return $script;
301 }
302
303 function IsProbablyRedirectToIndex()
304 {
305     // This might be a redirect to the DirectoryIndex,
306     // e.g. REQUEST_URI = /dir/?some_action got redirected
307     // to SCRIPT_NAME = /dir/index.php
308
309     // In this case, the proper virtual path is still
310     // $SCRIPT_NAME, since pages appear at
311     // e.g. /dir/index.php/HomePage.
312
313     $requri = preg_replace('/\?.*$/', '', $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']);
314     $requri = preg_quote($requri, '%');
315     return preg_match("%^${requri}[^/]*$%", $GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
316 }
317
318 // needed < php5
319 // by bradhuizenga at softhome dot net from the php docs
320 if (!function_exists('str_ireplace')) {
321     function str_ireplace($find, $replace, $string)
322     {
323         if (!is_array($find)) $find = array($find);
324         if (!is_array($replace)) {
325             if (!is_array($find))
326                 $replace = array($replace);
327             else {
328                 // this will duplicate the string into an array the size of $find
329                 $c = count($find);
330                 $rString = $replace;
331                 unset($replace);
332                 for ($i = 0; $i < $c; $i++) {
333                     $replace[$i] = $rString;
334                 }
335             }
336         }
337         foreach ($find as $fKey => $fItem) {
338             $between = explode(strtolower($fItem), strtolower($string));
339             $pos = 0;
340             foreach ($between as $bKey => $bItem) {
341                 $between[$bKey] = substr($string, $pos, strlen($bItem));
342                 $pos += strlen($bItem) + strlen($fItem);
343             }
344             $string = implode($replace[$fKey], $between);
345         }
346         return ($string);
347     }
348 }
349
350 // htmlspecialchars_decode exists for PHP >= 5.1
351 if (!function_exists('htmlspecialchars_decode')) {
352
353     function htmlspecialchars_decode($text)
354     {
355         return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
356     }
357
358 }
359
360 /**
361  * safe php4 definition for clone.
362  * php5 copies objects by reference, but we need to clone "deep copy" in some places.
363  * (BlockParser)
364  * We need to eval it as workaround for the php5 parser.
365  * See http://www.acko.net/node/54
366  */
367 if (!check_php_version(5)) {
368     eval('
369     function clone($object) {
370       return $object;
371     }
372     ');
373 }
374
375 function getUploadFilePath()
376 {
377
378     if (defined('UPLOAD_FILE_PATH')) {
379         // Force creation of the returned directory if it does not exist.
380         if (!file_exists(UPLOAD_FILE_PATH)) {
381             mkdir(UPLOAD_FILE_PATH, 0775, true);
382         }
383         if (string_ends_with(UPLOAD_FILE_PATH, "/")
384             or string_ends_with(UPLOAD_FILE_PATH, "\\")
385         ) {
386             return UPLOAD_FILE_PATH;
387         } else {
388             return UPLOAD_FILE_PATH . "/";
389         }
390     }
391     return defined('PHPWIKI_DIR')
392         ? PHPWIKI_DIR . "/uploads/"
393         : realpath(dirname(__FILE__) . "/../uploads/");
394 }
395
396 function getUploadDataPath()
397 {
398     if (defined('UPLOAD_DATA_PATH')) {
399         return string_ends_with(UPLOAD_DATA_PATH, "/")
400             ? UPLOAD_DATA_PATH : UPLOAD_DATA_PATH . "/";
401     }
402     return SERVER_URL . (string_ends_with(DATA_PATH, "/") ? '' : "/")
403         . DATA_PATH . '/uploads/';
404 }
405
406 // Local Variables:
407 // mode: php
408 // tab-width: 8
409 // c-basic-offset: 4
410 // c-hanging-comment-ender-p: nil
411 // indent-tabs-mode: nil
412 // End: