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