]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/config.php
pcre_fix_posix_classes does nothing in Gforge
[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     // Backward compatibility (for PHP < 4.0.5)
11     if (!check_php_version(4,0,5)) {
12         define("LC_ALL",   "LC_ALL");
13         define("LC_CTYPE", "LC_CTYPE");
14     } else {
15         define("LC_ALL",   0);
16         define("LC_CTYPE", 2);
17     }
18 }
19 // debug flags: 
20 define ('_DEBUG_VERBOSE',   1); // verbose msgs and add validator links on footer
21 define ('_DEBUG_PAGELINKS', 2); // list the extraced pagelinks at the top of each pages
22 define ('_DEBUG_PARSER',    4); // verbose parsing steps
23 define ('_DEBUG_TRACE',     8); // test php memory usage, prints php debug backtraces
24 define ('_DEBUG_INFO',     16);
25 define ('_DEBUG_APD',      32); // APD tracing/profiling
26 define ('_DEBUG_LOGIN',    64); // verbose login debug-msg (settings and reason for failure)
27 define ('_DEBUG_SQL',     128); // force check db, force optimize, print some debugging logs
28 define ('_DEBUG_REMOTE',  256); // remote debug into subrequests (xmlrpc, ajax, wikiwyg, ...) 
29                                 // or test local SearchHighlight.
30                                 // internal links have persistent ?start_debug=1 
31
32 function isCGI() {
33     return (substr(php_sapi_name(),0,3) == 'cgi' and 
34             isset($GLOBALS['HTTP_ENV_VARS']['GATEWAY_INTERFACE']) and
35             @preg_match('/CGI/',$GLOBALS['HTTP_ENV_VARS']['GATEWAY_INTERFACE']));
36 }
37
38 /*
39 // copy some $_ENV vars to $_SERVER for CGI compatibility. php does it automatically since when?
40 if (isCGI()) {
41     foreach (explode(':','SERVER_SOFTWARE:SERVER_NAME:GATEWAY_INTERFACE:SERVER_PROTOCOL:SERVER_PORT:REQUEST_METHOD:HTTP_ACCEPT:PATH_INFO:PATH_TRANSLATED:SCRIPT_NAME:QUERY_STRING:REMOTE_HOST:REMOTE_ADDR:REMOTE_USER:AUTH_TYPE:CONTENT_TYPE:CONTENT_LENGTH') as $key) {
42         $GLOBALS['HTTP_SERVER_VARS'][$key] = &$GLOBALS['HTTP_ENV_VARS'][$key];
43     }
44 }
45 */
46
47 // essential internal stuff
48 set_magic_quotes_runtime(0);
49
50 /** 
51  * Browser Detection Functions
52  *
53  * Current Issues:
54  *  NS/IE < 4.0 doesn't accept < ? xml version="1.0" ? >
55  *  NS/IE < 4.0 cannot display PNG
56  *  NS/IE < 4.0 cannot display all XHTML tags
57  *  NS < 5.0 needs textarea wrap=virtual
58  *  IE55 has problems with transparent PNG's
59  * @author: ReiniUrban
60  */
61 function browserAgent() {
62     static $HTTP_USER_AGENT = false;
63     if ($HTTP_USER_AGENT !== false) return $HTTP_USER_AGENT;
64     if (!$HTTP_USER_AGENT)
65         $HTTP_USER_AGENT = @$GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
66     if (!$HTTP_USER_AGENT) // CGI
67         $HTTP_USER_AGENT = @$GLOBALS['HTTP_ENV_VARS']['HTTP_USER_AGENT'];
68     if (!$HTTP_USER_AGENT) // local CGI testing
69         $HTTP_USER_AGENT = 'none';
70     return $HTTP_USER_AGENT;
71 }
72 function browserDetect($match) {
73     return (strpos(strtolower(browserAgent()), strtolower($match)) !== false);
74 }
75 // returns a similar number for Netscape/Mozilla (gecko=5.0)/IE/Opera features.
76 function browserVersion() {
77     $agent = browserAgent();
78     if (strstr($agent, "Mozilla/4.0 (compatible; MSIE"))
79         return (float) substr($agent, 30);
80     elseif (strstr($agent, "Mozilla/5.0 (compatible; Konqueror/"))
81         return (float) substr($agent, 36);
82     else
83         return (float) substr($agent, 8);
84 }
85 function isBrowserIE() {
86     return (browserDetect('Mozilla/') and 
87             browserDetect('MSIE'));
88 }
89 // problem with transparent PNG's
90 function isBrowserIE55() {
91     return (isBrowserIE() and 
92             browserVersion() > 5.1 and browserVersion() < 6.0);
93 }
94 // old Netscape prior to Mozilla
95 function isBrowserNetscape($version = false) {
96     $agent = (browserDetect('Mozilla/') and 
97             ! browserDetect('Gecko/') and
98             ! browserDetect('MSIE'));
99     if ($version) return $agent and browserVersion() >= $version; 
100     else return $agent;
101 }
102 // NS3 or less
103 function isBrowserNS3() {
104     return (isBrowserNetscape() and browserVersion() < 4.0);
105 }
106 // NS4 or less
107 function isBrowserNS4() {
108     return (isBrowserNetscape() and browserVersion() < 5.0);
109 }
110 // must omit display alternate stylesheets: konqueror 3.1.4
111 // http://sourceforge.net/tracker/index.php?func=detail&aid=945154&group_id=6121&atid=106121
112 function isBrowserKonqueror($version = false) {
113     if ($version) return browserDetect('Konqueror/') and browserVersion() >= $version; 
114     return browserDetect('Konqueror/');
115 }
116 // MacOSX Safari has certain limitations. Need detection and patches.
117 // * no <object>, only <embed>
118 function isBrowserSafari($version = false) {
119     $found = browserDetect('Spoofer/');
120     $found = browserDetect('AppleWebKit/') or $found;
121     if ($version) return $found and browserVersion() >= $version; 
122     return $found;
123 }
124 function isBrowserOpera($version = false) {
125     if ($version) return browserDetect('Opera/') and browserVersion() >= $version; 
126     return browserDetect('Opera/');
127 }
128
129
130 /**
131  * If $LANG is undefined:
132  * Smart client language detection, based on our supported languages
133  * HTTP_ACCEPT_LANGUAGE="de-at,en;q=0.5"
134  *   => "de"
135  * We should really check additionally if the i18n HomePage version is defined.
136  * So must defer this to the request loop.
137  */
138 function guessing_lang ($languages=false) {
139     if (!$languages) {
140         // make this faster
141         $languages = array("en","de","es","fr","it","ja","zh","nl","sv");
142         // ignore possible "_<territory>" and codeset "ja.utf8"
143         /*
144         require_once("lib/WikiTheme.php");
145         $languages = listAvailableLanguages();
146         if (defined('DEFAULT_LANGUAGE') and in_array(DEFAULT_LANGUAGE, $languages))
147         {
148             // remove duplicates
149             if ($i = array_search(DEFAULT_LANGUAGE, $languages) !== false) {
150                 array_splice($languages, $i, 1);
151             }
152             array_unshift($languages, DEFAULT_LANGUAGE);
153             foreach ($languages as $lang) {
154                 $arr = FileFinder::locale_versions($lang);
155                 $languages = array_merge($languages, $arr);
156             }
157         }
158         */
159     }
160
161     $accept = false; 
162     if (isset($GLOBALS['request'])) // in fixup-dynamic-config there's no request yet
163         $accept = $GLOBALS['request']->get('HTTP_ACCEPT_LANGUAGE');
164     elseif (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
165         $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
166
167     if ($accept) {
168         $lang_list = array();
169         $list = explode(",", $accept);
170         for ($i=0; $i<count($list); $i++) {
171             $pos = strchr($list[$i], ";") ;
172             if ($pos === false) {
173                 // No Q it is only a locale...
174                 $lang_list[$list[$i]] = 100;
175             } else {
176                 // Has a Q rating        
177                 $q = explode(";",$list[$i]) ;
178                 $loc = $q[0] ;
179                 $q = explode("=",$q[1]) ;
180                 $lang_list[$loc] = $q[1]*100 ;
181             }
182         }
183
184         // sort by q desc
185         arsort($lang_list);
186
187         // compare with languages, ignoring sublang and charset
188         foreach ($lang_list as $lang => $q) {
189             if (in_array($lang, $languages))
190                 return $lang;
191             // de_DE.iso8859-1@euro => de_DE.iso8859-1, de_DE, de
192             // de-DE => de-DE, de
193             foreach (array('@', '.', '_') as $sep) {
194                 if ( ($tail = strchr($lang, $sep)) ) {
195                     $lang_short = substr($lang, 0, -strlen($tail));
196                     if (in_array($lang_short, $languages))
197                         return $lang_short;
198                 }
199             }
200             if ($pos = strchr($lang, "-") and in_array(substr($lang, 0, $pos), $languages))
201                 return substr($lang, 0, $pos);
202         }
203     }
204     return $languages[0];
205 }
206
207 /**
208  * Smart setlocale().
209  *
210  * This is a version of the builtin setlocale() which is
211  * smart enough to try some alternatives...
212  *
213  * @param mixed $category
214  * @param string $locale
215  * @return string The new locale, or <code>false</code> if unable
216  *  to set the requested locale.
217  * @see setlocale
218  * [56ms]
219  */
220 function guessing_setlocale ($category, $locale) {
221     $alt = array('en' => array('C', 'en_US', 'en_GB', 'en_AU', 'en_CA', 'english'),
222                  'de' => array('de_DE', 'de_DE', 'de_DE@euro', 
223                                'de_AT@euro', 'de_AT', 'German_Austria.1252', 'deutsch', 
224                                'german', 'ge'),
225                  'es' => array('es_ES', 'es_MX', 'es_AR', 'spanish'),
226                  'nl' => array('nl_NL', 'dutch'),
227                  'fr' => array('fr_FR', 'français', 'french'),
228                  'it' => array('it_IT'),
229                  'sv' => array('sv_SE'),
230                  'ja.utf-8'  => array('ja_JP','ja_JP.utf-8','japanese'),
231                  'ja.euc-jp' => array('ja_JP','ja_JP.eucJP','japanese.euc'),
232                  'zh' => array('zh_TW', 'zh_CN'),
233                  );
234     if (!$locale or $locale=='C') { 
235         // do the reverse: return the detected locale collapsed to our LANG
236         $locale = setlocale($category, '');
237         if ($locale) {
238             if (strstr($locale, '_')) list ($lang) = split('_', $locale);
239             else $lang = $locale;
240             if (strlen($lang) > 2) { 
241                 foreach ($alt as $try => $locs) {
242                     if (in_array($locale, $locs) or in_array($lang, $locs)) {
243                         //if (empty($GLOBALS['LANG'])) $GLOBALS['LANG'] = $try;
244                         return $try;
245                     }
246                 }
247             }
248         }
249     }
250     if (strlen($locale) == 2)
251         $lang = $locale;
252     else 
253         list ($lang) = split('_', $locale);
254     if (!isset($alt[$lang]))
255         return false;
256         
257     foreach ($alt[$lang] as $try) {
258         if ($res = setlocale($category, $try))
259             return $res;
260         // Try with charset appended...
261         $try = $try . '.' . $GLOBALS['charset'];
262         if ($res = setlocale($category, $try))
263             return $res;
264         foreach (array(".", '@', '_') as $sep) {
265             if ($i = strpos($try, $sep)) {
266                 $try = substr($try, 0, $i);
267                 if (($res = setlocale($category, $try)))
268                     return $res;
269             }
270         }
271     }
272     return false;
273     // A standard locale name is typically of  the  form
274     // language[_territory][.codeset][@modifier],  where  language is
275     // an ISO 639 language code, territory is an ISO 3166 country code,
276     // and codeset  is  a  character  set or encoding identifier like
277     // ISO-8859-1 or UTF-8.
278 }
279
280 // [99ms]
281 function update_locale($loc) {
282     // $LANG or DEFAULT_LANGUAGE is too less information, at least on unix for
283     // setlocale(), for bindtextdomain() to succeed.
284     $setlocale = guessing_setlocale(LC_ALL, $loc); // [56ms]
285     if (!$setlocale) { // system has no locale for this language, so gettext might fail
286         $setlocale = FileFinder::_get_lang();
287         list ($setlocale,) = split('_', $setlocale, 2);
288         $setlocale = guessing_setlocale(LC_ALL, $setlocale); // try again
289         if (!$setlocale) $setlocale = $loc;
290     }
291     // Try to put new locale into environment (so any
292     // programs we run will get the right locale.)
293     if (!function_exists('bindtextdomain'))  {
294         // Reinitialize translation array.
295         global $locale;
296         $locale = array();
297         // do reinit to purge PHP's static cache [43ms]
298         if ( ($lcfile = FindLocalizedFile("LC_MESSAGES/phpwiki.php", 'missing_ok', 'reinit')) ) {
299             include($lcfile);
300         }
301     } else {
302         // If PHP is in safe mode, this is not allowed,
303         // so hide errors...
304         @putenv("LC_ALL=$setlocale");
305         @putenv("LANG=$loc");
306         @putenv("LANGUAGE=$loc");
307     }
308
309     // To get the POSIX character classes in the PCRE's (e.g.
310     // [[:upper:]]) to match extended characters (e.g. GrüßGott), we have
311     // to set the locale, using setlocale().
312     //
313     // The problem is which locale to set?  We would like to recognize all
314     // upper-case characters in the iso-8859-1 character set as upper-case
315     // characters --- not just the ones which are in the current $LANG.
316     //
317     // As it turns out, at least on my system (Linux/glibc-2.2) as long as
318     // you setlocale() to anything but "C" it works fine.  (I'm not sure
319     // whether this is how it's supposed to be, or whether this is a bug
320     // in the libc...)
321     //
322     // We don't currently use the locale setting for anything else, so for
323     // now, just set the locale to US English.
324     //
325     // FIXME: Not all environments may support en_US?  We should probably
326     // have a list of locales to try.
327     if (setlocale(LC_CTYPE, 0) == 'C') {
328         $x = setlocale(LC_CTYPE, 'en_US.' . $GLOBALS['charset']);
329     } else {
330         $x = setlocale(LC_CTYPE, $setlocale);
331     }
332
333     return $loc;
334 }
335
336 /** string pcre_fix_posix_classes (string $regexp)
337 *
338 * Older version (pre 3.x?) of the PCRE library do not support
339 * POSIX named character classes (e.g. [[:alnum:]]).
340 *
341 * This is a helper function which can be used to convert a regexp
342 * which contains POSIX named character classes to one that doesn't.
343 *
344 * All instances of strings like '[:<class>:]' are replaced by the equivalent
345 * enumerated character class.
346 *
347 * Implementation Notes:
348 *
349 * Currently we use hard-coded values which are valid only for
350 * ISO-8859-1.  Also, currently on the classes [:alpha:], [:alnum:],
351 * [:upper:] and [:lower:] are implemented.  (The missing classes:
352 * [:blank:], [:cntrl:], [:digit:], [:graph:], [:print:], [:punct:],
353 * [:space:], and [:xdigit:] could easily be added if needed.)
354 *
355 * This is a hack.  I tried to generate these classes automatically
356 * using ereg(), but discovered that in my PHP, at least, ereg() is
357 * slightly broken w.r.t. POSIX character classes.  (It includes
358 * "\xaa" and "\xba" in [:alpha:].)
359 *
360 * So for now, this will do.  --Jeff <dairiki@dairiki.org> 14 Mar, 2001
361 */
362 function pcre_fix_posix_classes ($regexp) {
363     global $charset;
364
365     if (defined('GFORGE') and GFORGE) {
366         return $regexp;
367     }
368
369     if (!isset($charset))
370         $charset = CHARSET; // get rid of constant. pref is dynamic and language specific
371     if (in_array($GLOBALS['LANG'], array('zh')))
372         $charset = 'utf-8';
373     if (strstr($GLOBALS['LANG'],'.utf-8'))
374         $charset = 'utf-8';
375     elseif (strstr($GLOBALS['LANG'],'.euc-jp'))
376         $charset = 'euc-jp';
377     elseif (strstr($GLOBALS['LANG'], 'ja'))
378         $charset = 'euc-jp';
379
380     if (strtolower($charset) == 'utf-8') { // thanks to John McPherson
381         // until posix class names/pcre work with utf-8
382         if (preg_match('/[[:upper:]]/', '\xc4\x80'))
383             return $regexp;    
384         // utf-8 non-ascii chars: most common (eg western) latin chars are 0xc380-0xc3bf
385         // we currently ignore other less common non-ascii characters
386         // (eg central/east european) latin chars are 0xc432-0xcdbf and 0xc580-0xc5be
387         // and indian/cyrillic/asian languages
388         
389         // this replaces [[:lower:]] with utf-8 match (Latin only)
390         $regexp = preg_replace('/\[\[\:lower\:\]\]/','(?:[a-z]|\xc3[\x9f-\xbf]|\xc4[\x81\x83\x85\x87])',
391                                $regexp);
392         // this replaces [[:upper:]] with utf-8 match (Latin only)
393         $regexp = preg_replace('/\[\[\:upper\:\]\]/','(?:[A-Z]|\xc3[\x80-\x9e]|\xc4[\x80\x82\x84\x86])',
394                                $regexp);
395     } elseif (preg_match('/[[:upper:]]/', 'Ä')) {
396         // First check to see if our PCRE lib supports POSIX character
397         // classes.  If it does, there's nothing to do.
398         return $regexp;
399     }
400     static $classes = array(
401                             'alnum' => "0-9A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
402                             'alpha' => "A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff",
403                             'upper' => "A-Z\xc0-\xd6\xd8-\xde",
404                             'lower' => "a-z\xdf-\xf6\xf8-\xff"
405                             );
406     $keys = join('|', array_keys($classes));
407     return preg_replace("/\[:($keys):]/e", '$classes["\1"]', $regexp);
408 }
409
410 function deduce_script_name() {
411     $s = &$GLOBALS['HTTP_SERVER_VARS'];
412     $script = @$s['SCRIPT_NAME'];
413     if (empty($script) or $script[0] != '/') {
414         // Some places (e.g. Lycos) only supply a relative name in
415         // SCRIPT_NAME, but give what we really want in SCRIPT_URL.
416         if (!empty($s['SCRIPT_URL']))
417             $script = $s['SCRIPT_URL'];
418     }
419     return $script;
420 }
421
422 function IsProbablyRedirectToIndex () {
423     // This might be a redirect to the DirectoryIndex,
424     // e.g. REQUEST_URI = /dir/?some_action got redirected
425     // to SCRIPT_NAME = /dir/index.php
426
427     // In this case, the proper virtual path is still
428     // $SCRIPT_NAME, since pages appear at
429     // e.g. /dir/index.php/HomePage.
430
431     $requri = preg_replace('/\?.*$/','',$GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']);
432     $requri = preg_quote($requri, '%');
433     return preg_match("%^${requri}[^/]*$%", $GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
434 }
435
436 // >= php-4.1.0
437 if (!function_exists('array_key_exists')) { // lib/IniConfig.php, sqlite, adodb, ...
438     function array_key_exists($item, $array) {
439         return isset($array[$item]);
440     }
441 }
442
443 // => php-4.0.5
444 if (!function_exists('is_scalar')) { // lib/stdlib.php:wikihash()
445     function is_scalar($x) {
446         return is_numeric($x) or is_string($x) or is_float($x) or is_bool($x); 
447     }
448 }
449
450 // => php-4.2.0. pear wants to break old php's! DB uses it now.
451 if (!function_exists('is_a')) {
452     function is_a($item,$class) {
453         return isa($item,$class); 
454     }
455 }
456
457 // needed < php5
458 // by bradhuizenga at softhome dot net from the php docs
459 if (!function_exists('str_ireplace')) {
460   function str_ireplace($find, $replace, $string) {
461       if (!is_array($find)) $find = array($find);
462       if (!is_array($replace)) {
463           if (!is_array($find)) 
464               $replace = array($replace);
465           else {
466               // this will duplicate the string into an array the size of $find
467               $c = count($find);
468               $rString = $replace;
469               unset($replace);
470               for ($i = 0; $i < $c; $i++) {
471                   $replace[$i] = $rString;
472               }
473           }
474       }
475       foreach ($find as $fKey => $fItem) {
476           $between = explode(strtolower($fItem),strtolower($string));
477           $pos = 0;
478           foreach ($between as $bKey => $bItem) {
479               $between[$bKey] = substr($string,$pos,strlen($bItem));
480               $pos += strlen($bItem) + strlen($fItem);
481           }
482           $string = implode($replace[$fKey], $between);
483       }
484       return($string);
485   }
486 }
487
488 /**
489  * safe php4 definition for clone.
490  * php5 copies objects by reference, but we need to clone "deep copy" in some places.
491  * (BlockParser)
492  * We need to eval it as workaround for the php5 parser.
493  * See http://www.acko.net/node/54
494  */
495 if (!check_php_version(5)) {
496     eval('
497     function clone($object) {
498       return $object;
499     }
500     ');
501 }
502
503 /**
504  * array_diff_assoc() returns an array containing all the values from array1 that are not
505  * present in any of the other arguments. Note that the keys are used in the comparison 
506  * unlike array_diff(). In core since php-4.3.0
507  * Our fallback here supports only hashes and two args.
508  * $array1 = array("a" => "green", "b" => "brown", "c" => "blue");
509  * $array2 = array("a" => "green", "y" => "yellow", "r" => "red");
510  * => b => brown, c => blue
511  */
512 if (!function_exists('array_diff_assoc')) {
513     function array_diff_assoc($a1, $a2) {
514         $result = array();
515         foreach ($a1 as $k => $v) {
516             if (!isset($a2[$k]) or !$a2[$k])
517                 $result[$k] = $v;       
518         }
519         return $result;
520     }
521 }
522
523 /** 
524  * wordwrap() might crash between 4.1.2 and php-4.3.0RC2, fixed in 4.3.0
525  * See http://bugs.php.net/bug.php?id=20927 and 
526  * http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2002-1396
527  * Improved version of wordwrap2() in the comments at http://www.php.net/wordwrap
528  */
529 function safe_wordwrap($str, $width=80, $break="\n", $cut=false) {
530     if (check_php_version(4,3))
531         return wordwrap($str, $width, $break, $cut);
532     elseif (!check_php_version(4,1,2))
533         return wordwrap($str, $width, $break, $cut);
534     else {
535         $len = strlen($str);
536         $tag = 0; $result = ''; $wordlen = 0;
537         for ($i = 0; $i < $len; $i++) {
538             $chr = $str[$i];
539             // don't break inside xml tags
540             if ($chr == '<') {
541                 $tag++;
542             } elseif ($chr == '>') {
543                 $tag--;
544             } elseif (!$tag) {
545                 if (!function_exists('ctype_space')) {
546                     if (preg_match('/^\s$/', $chr))
547                         $wordlen = 0;
548                     else
549                         $wordlen++;
550                 }
551                 elseif (ctype_space($chr)) {
552                     $wordlen = 0;
553                 } else {
554                     $wordlen++;
555                 }
556             }
557             if ((!$tag) && ($wordlen) && (!($wordlen % $width))) {
558                 $chr .= $break;
559             }
560             $result .= $chr;
561         }
562         return $result;
563         /*
564         if (isset($str) && isset($width)) {
565             $ex = explode(" ", $str); // wrong: must use preg_split \s+
566             $rp = array();
567             for ($i=0; $i<count($ex); $i++) {
568                 // $word_array = preg_split('//', $ex[$i], -1, PREG_SPLIT_NO_EMPTY);
569                 // delete #&& !is_numeric($ex[$i])# if you want force it anyway
570                 if (strlen($ex[$i]) > $width && !is_numeric($ex[$i])) {
571                     $where = 0;
572                     $rp[$i] = "";
573                     for($b=0; $b < (ceil(strlen($ex[$i]) / $width)); $b++) {
574                         $rp[$i] .= substr($ex[$i], $where, $width).$break;
575                         $where += $width;
576                     }
577                 } else {
578                     $rp[$i] = $ex[$i];
579                 }
580             }
581             return implode(" ",$rp);
582         }
583         return $text;
584         */
585     }
586 }
587
588 function getUploadFilePath() {
589
590     if (defined('UPLOAD_FILE_PATH')) {
591         // Force creation of the returned directory if it does not exist.
592         if (!file_exists(UPLOAD_FILE_PATH)) {
593             mkdir(UPLOAD_FILE_PATH, 0775);
594         }
595         if (string_ends_with(UPLOAD_FILE_PATH, "/") 
596             or string_ends_with(UPLOAD_FILE_PATH, "\\")) {
597             return UPLOAD_FILE_PATH;
598         } else {
599             return UPLOAD_FILE_PATH."/";
600         }
601     }
602     return defined('PHPWIKI_DIR') 
603         ? PHPWIKI_DIR . "/uploads/" 
604         : realpath(dirname(__FILE__) . "/../uploads/");
605 }
606 function getUploadDataPath() {
607     if (defined('UPLOAD_DATA_PATH')) {
608         return string_ends_with(UPLOAD_DATA_PATH, "/") 
609             ? UPLOAD_DATA_PATH : UPLOAD_DATA_PATH."/";
610     }
611     return SERVER_URL . (string_ends_with(DATA_PATH, "/") ? '' : "/") 
612          . DATA_PATH . '/uploads/';
613 }
614
615 /**
616  * htmlspecialchars doesn't support some special 8bit charsets, which we do want to support.
617  * Well it just prints a warning which we could circumvent.
618  * Note: unused, since php htmlspecialchars does the same, just prints a warning which we silence
619  */
620 /*
621 function htmlspecialchars_workaround($str, $quote=ENT_COMPAT, $charset='iso-8859-1') {
622     if (in_array(strtolower($charset), 
623                  array('iso-8859-2', 'iso8859-2', 'latin-2', 'latin2'))) 
624     {
625         if (! ($quote & ENT_NOQUOTES)) {
626             $str = str_replace("\"", "&quot;",
627                                $str);
628         }
629         if ($quote & ENT_QUOTES) {
630             $str = str_replace("\'", "&#039;",
631                                $str);
632         }
633         return str_replace(array("<", ">", "&"),
634                            array("&lt;", "&gt;", "&amp;"), $str);
635     }
636     else {
637         return htmlspecialchars($str, $quote, $charset);
638     }
639 }
640 */
641
642 /**
643  * htmlspecialchars doesn't support some special 8bit charsets, which we do want to support.
644  * Well it just prints a warning which we could circumvent.
645  * Note: unused, since php htmlspecialchars does the same, just prints a warning which we silence
646  */
647 /*
648 function htmlspecialchars_workaround($str, $quote=ENT_COMPAT, $charset='iso-8859-1') {
649     if (in_array(strtolower($charset), 
650                  array('iso-8859-2', 'iso8859-2', 'latin-2', 'latin2'))) 
651     {
652         if (! ($quote & ENT_NOQUOTES)) {
653             $str = str_replace("\"", "&quot;",
654                                $str);
655         }
656         if ($quote & ENT_QUOTES) {
657             $str = str_replace("\'", "&#039;",
658                                $str);
659         }
660         return str_replace(array("<", ">", "&"),
661                            array("&lt;", "&gt;", "&amp;"), $str);
662     }
663     else {
664         return htmlspecialchars($str, $quote, $charset);
665     }
666 }
667 */
668
669 // For emacs users
670 // Local Variables:
671 // mode: php
672 // tab-width: 8
673 // c-basic-offset: 4
674 // c-hanging-comment-ender-p: nil
675 // indent-tabs-mode: nil
676 // End:
677 ?>