PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/plugins/wiki/www/lib/config.php

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