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