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