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
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']));
33 // essential internal stuff
34 if (!check_php_version(5, 3)) {
35 set_magic_quotes_runtime(0);
39 * Browser Detection Functions
43 function browserAgent()
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;
56 function browserDetect($match)
58 return (strpos(strtolower(browserAgent()), strtolower($match)) !== false);
61 // returns a similar number for Netscape/Mozilla (gecko=5.0)/IE/Opera features.
62 function browserVersion()
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);
73 function isBrowserIE()
75 return (browserDetect('Mozilla/') and
76 browserDetect('MSIE'));
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)
83 if ($version) return browserDetect('Konqueror/') and browserVersion() >= $version;
84 return browserDetect('Konqueror/');
87 // MacOSX Safari has certain limitations. Need detection and patches.
88 // * no <object>, only <embed>
89 function isBrowserSafari($version = false)
91 $found = browserDetect('Spoofer/');
92 $found = browserDetect('AppleWebKit/') or $found;
93 if ($version) return $found and browserVersion() >= $version;
97 function isBrowserOpera($version = false)
99 if ($version) return browserDetect('Opera/') and browserVersion() >= $version;
100 return browserDetect('Opera/');
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"
108 * We should really check additionally if the i18n HomePage version is defined.
109 * So must defer this to the request loop.
111 function guessing_lang($languages = false)
115 $languages = array("en", "de", "es", "fr", "it", "ja", "zh", "nl", "sv");
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'];
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;
134 $q = explode(";", $list[$i]);
136 $q = explode("=", $q[1]);
137 $lang_list[$loc] = $q[1] * 100;
144 // compare with languages, ignoring sublang and charset
145 foreach ($lang_list as $lang => $q) {
146 if (in_array($lang, $languages))
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))
157 if ($pos = strpos($lang, "-") and in_array(substr($lang, 0, $pos), $languages))
158 return substr($lang, 0, $pos);
161 return $languages[0];
167 * This is a version of the builtin setlocale() which is
168 * smart enough to try some alternatives...
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.
177 function guessing_setlocale($category, $locale)
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',
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'),
192 if (!$locale or $locale == 'C') {
193 // do the reverse: return the detected locale collapsed to our LANG
194 $locale = setlocale($category, '');
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;
208 if (strlen($locale) == 2)
211 list ($lang) = explode('_', $locale);
212 if (!isset($alt[$lang]))
215 foreach ($alt[$lang] as $try) {
216 if ($res = setlocale($category, $try))
218 // Try with charset appended...
219 $try = $try . '.' . $GLOBALS['charset'];
220 if ($res = setlocale($category, $try))
222 foreach (array(".", '@', '_') as $sep) {
223 if ($i = strpos($try, $sep)) {
224 $try = substr($try, 0, $i);
225 if (($res = setlocale($category, $try)))
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.
239 function update_locale($loc)
241 if ($loc == 'C' or $loc == 'en') {
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;
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 // Reinitialize translation array.
259 // do reinit to purge PHP's static cache [43ms]
260 if (($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok', 'reinit'))) {
264 // If PHP is in safe mode, this is not allowed,
266 @putenv("LC_ALL=$setlocale");
267 @putenv("LANG=$loc");
268 @putenv("LANGUAGE=$loc");
271 // To get the POSIX character classes in the PCRE's (e.g.
272 // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
273 // to set the locale, using setlocale().
275 // The problem is which locale to set? We would like to recognize all
276 // upper-case characters in the iso-8859-1 character set as upper-case
277 // characters --- not just the ones which are in the current $LANG.
279 // As it turns out, at least on my system (Linux/glibc-2.2) as long as
280 // you setlocale() to anything but "C" it works fine. (I'm not sure
281 // whether this is how it's supposed to be, or whether this is a bug
284 // We don't currently use the locale setting for anything else, so for
285 // now, just set the locale to US English.
287 // FIXME: Not all environments may support en_US? We should probably
288 // have a list of locales to try.
289 if (setlocale(LC_CTYPE, 0) == 'C') {
290 $x = setlocale(LC_CTYPE, 'en_US.' . $GLOBALS['charset']);
292 $x = setlocale(LC_CTYPE, $setlocale);
298 function deduce_script_name()
301 $script = @$s['SCRIPT_NAME'];
302 if (empty($script) or $script[0] != '/') {
303 // Some places (e.g. Lycos) only supply a relative name in
304 // SCRIPT_NAME, but give what we really want in SCRIPT_URL.
305 if (!empty($s['SCRIPT_URL']))
306 $script = $s['SCRIPT_URL'];
311 function IsProbablyRedirectToIndex()
313 // This might be a redirect to the DirectoryIndex,
314 // e.g. REQUEST_URI = /dir/?some_action got redirected
315 // to SCRIPT_NAME = /dir/index.php
317 // In this case, the proper virtual path is still
318 // $SCRIPT_NAME, since pages appear at
319 // e.g. /dir/index.php/HomePage.
321 $requri = preg_replace('/\?.*$/', '', $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']);
322 $requri = preg_quote($requri, '%');
323 return preg_match("%^${requri}[^/]*$%", $GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
327 // by bradhuizenga at softhome dot net from the php docs
328 if (!function_exists('str_ireplace')) {
329 function str_ireplace($find, $replace, $string)
331 if (!is_array($find)) $find = array($find);
332 if (!is_array($replace)) {
333 if (!is_array($find))
334 $replace = array($replace);
336 // this will duplicate the string into an array the size of $find
340 for ($i = 0; $i < $c; $i++) {
341 $replace[$i] = $rString;
345 foreach ($find as $fKey => $fItem) {
346 $between = explode(strtolower($fItem), strtolower($string));
348 foreach ($between as $bKey => $bItem) {
349 $between[$bKey] = substr($string, $pos, strlen($bItem));
350 $pos += strlen($bItem) + strlen($fItem);
352 $string = implode($replace[$fKey], $between);
358 // htmlspecialchars_decode exists for PHP >= 5.1
359 if (!function_exists('htmlspecialchars_decode')) {
361 function htmlspecialchars_decode($text)
363 return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
369 * safe php4 definition for clone.
370 * php5 copies objects by reference, but we need to clone "deep copy" in some places.
372 * We need to eval it as workaround for the php5 parser.
373 * See http://www.acko.net/node/54
375 if (!check_php_version(5)) {
377 function clone($object) {
383 function getUploadFilePath()
386 if (defined('UPLOAD_FILE_PATH')) {
387 // Force creation of the returned directory if it does not exist.
388 if (!file_exists(UPLOAD_FILE_PATH)) {
389 mkdir(UPLOAD_FILE_PATH, 0775, true);
391 if (string_ends_with(UPLOAD_FILE_PATH, "/")
392 or string_ends_with(UPLOAD_FILE_PATH, "\\")
394 return UPLOAD_FILE_PATH;
396 return UPLOAD_FILE_PATH . "/";
399 return defined('PHPWIKI_DIR')
400 ? PHPWIKI_DIR . "/uploads/"
401 : realpath(dirname(__FILE__) . "/../uploads/");
404 function getUploadDataPath()
406 if (defined('UPLOAD_DATA_PATH')) {
407 return string_ends_with(UPLOAD_DATA_PATH, "/")
408 ? UPLOAD_DATA_PATH : UPLOAD_DATA_PATH . "/";
410 return SERVER_URL . (string_ends_with(DATA_PATH, "/") ? '' : "/")
411 . DATA_PATH . '/uploads/';
418 // c-hanging-comment-ender-p: nil
419 // indent-tabs-mode: nil