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()
9 if (!defined("LC_ALL")) {
11 define("LC_CTYPE", 2);
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
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']));
32 // essential internal stuff
33 if (!check_php_version(5,3)) {
34 set_magic_quotes_runtime(0);
38 * Browser Detection Functions
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;
53 function browserDetect($match) {
54 return (strpos(strtolower(browserAgent()), strtolower($match)) !== false);
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);
66 return (float)substr($agent, 8);
68 function isBrowserIE() {
69 return (browserDetect('Mozilla/') and
70 browserDetect('MSIE'));
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/');
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;
86 function isBrowserOpera($version = false) {
87 if ($version) return browserDetect('Opera/') and browserVersion() >= $version;
88 return browserDetect('Opera/');
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"
96 * We should really check additionally if the i18n HomePage version is defined.
97 * So must defer this to the request loop.
99 function guessing_lang ($languages=false) {
102 $languages = array("en","de","es","fr","it","ja","zh","nl","sv");
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'];
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;
121 $q = explode(";",$list[$i]) ;
123 $q = explode("=",$q[1]) ;
124 $lang_list[$loc] = $q[1]*100 ;
131 // compare with languages, ignoring sublang and charset
132 foreach ($lang_list as $lang => $q) {
133 if (in_array($lang, $languages))
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))
144 if ($pos = strchr($lang, "-") and in_array(substr($lang, 0, $pos), $languages))
145 return substr($lang, 0, $pos);
148 return $languages[0];
154 * This is a version of the builtin setlocale() which is
155 * smart enough to try some alternatives...
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.
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',
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'),
178 if (!$locale or $locale=='C') {
179 // do the reverse: return the detected locale collapsed to our LANG
180 $locale = setlocale($category, '');
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;
194 if (strlen($locale) == 2)
197 list ($lang) = explode('_', $locale);
198 if (!isset($alt[$lang]))
201 foreach ($alt[$lang] as $try) {
202 if ($res = setlocale($category, $try))
204 // Try with charset appended...
205 $try = $try . '.' . $GLOBALS['charset'];
206 if ($res = setlocale($category, $try))
208 foreach (array(".", '@', '_') as $sep) {
209 if ($i = strpos($try, $sep)) {
210 $try = substr($try, 0, $i);
211 if (($res = setlocale($category, $try)))
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.
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;
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.
242 // do reinit to purge PHP's static cache [43ms]
243 if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok', 'reinit')) ) {
247 // If PHP is in safe mode, this is not allowed,
249 @putenv("LC_ALL=$setlocale");
250 @putenv("LANG=$loc");
251 @putenv("LANGUAGE=$loc");
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().
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.
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
267 // We don't currently use the locale setting for anything else, so for
268 // now, just set the locale to US English.
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']);
275 $x = setlocale(LC_CTYPE, $setlocale);
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'];
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
298 // In this case, the proper virtual path is still
299 // $SCRIPT_NAME, since pages appear at
300 // e.g. /dir/index.php/HomePage.
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']);
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);
316 // this will duplicate the string into an array the size of $find
320 for ($i = 0; $i < $c; $i++) {
321 $replace[$i] = $rString;
325 foreach ($find as $fKey => $fItem) {
326 $between = explode(strtolower($fItem),strtolower($string));
328 foreach ($between as $bKey => $bItem) {
329 $between[$bKey] = substr($string,$pos,strlen($bItem));
330 $pos += strlen($bItem) + strlen($fItem);
332 $string = implode($replace[$fKey], $between);
338 // htmlspecialchars_decode exists for PHP >= 5.1
339 if (!function_exists('htmlspecialchars_decode')) {
341 function htmlspecialchars_decode($text) {
342 return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
348 * safe php4 definition for clone.
349 * php5 copies objects by reference, but we need to clone "deep copy" in some places.
351 * We need to eval it as workaround for the php5 parser.
352 * See http://www.acko.net/node/54
354 if (!check_php_version(5)) {
356 function clone($object) {
362 function getUploadFilePath() {
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);
369 if (string_ends_with(UPLOAD_FILE_PATH, "/")
370 or string_ends_with(UPLOAD_FILE_PATH, "\\")) {
371 return UPLOAD_FILE_PATH;
373 return UPLOAD_FILE_PATH."/";
376 return defined('PHPWIKI_DIR')
377 ? PHPWIKI_DIR . "/uploads/"
378 : realpath(dirname(__FILE__) . "/../uploads/");
380 function getUploadDataPath() {
381 if (defined('UPLOAD_DATA_PATH')) {
382 return string_ends_with(UPLOAD_DATA_PATH, "/")
383 ? UPLOAD_DATA_PATH : UPLOAD_DATA_PATH."/";
385 return SERVER_URL . (string_ends_with(DATA_PATH, "/") ? '' : "/")
386 . DATA_PATH . '/uploads/';
393 // c-hanging-comment-ender-p: nil
394 // indent-tabs-mode: nil