]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/IniConfig.php
Remove CVS backend
[SourceForge/phpwiki.git] / lib / IniConfig.php
1 <?php
2
3 /**
4  * A configurator intended to read its config from a PHP-style INI file,
5  * instead of a PHP file.
6  *
7  * Pass a filename to the IniConfig() function and it will read all its
8  * definitions from there, all by itself, and proceed to do a mass-define
9  * of all valid PHPWiki config items.  In this way, we can hopefully be
10  * totally backwards-compatible with the old index.php method, while still
11  * providing a much tastier on-going experience.
12  *
13  * @author: Joby Walker, Reini Urban, Matthew Palmer
14  */
15 /*
16  * Copyright 2004,2005,2006,2007 $ThePhpWikiProgrammingTeam
17  * Copyright 2008-2010 Marc-Etienne Vargenau, Alcatel-Lucent
18  *
19  * This file is part of PhpWiki.
20  *
21  * PhpWiki is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation; either version 2 of the License, or
24  * (at your option) any later version.
25  *
26  * PhpWiki is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License along
32  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
33  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34  */
35
36 /**
37  * DONE:
38  * - Convert the value lists to provide defaults, so that every "if
39  *      (defined())" and "if (!defined())" can fuck off to the dismal hole
40  *      it belongs in.
41  * - config.ini => config.php dumper for faster startup. (really faster? to time)
42  *
43  * TODO:
44  * - Old-style index.php => config/config.ini converter.
45  *
46  * - Don't use too much globals for easier integration into other projects
47  *   (namespace pollution). (FusionForge, phpnuke, postnuke, phpBB2, carolina, ...)
48  *   Use one global $phpwiki object instead which holds the cfg vars, constants
49  *   and all other globals.
50  *     (global $FieldSeparator, $WikiNameRegexp, $KeywordLinkRegexp;
51  *      global $DisabledActions, $DBParams, $LANG, $AllActionPages)
52  *
53  * - Resurrect the larger "config object" code (in config/) so it'll aid the
54  *   GUI config writers, and allow us to do proper validation and default
55  *   value handling.
56  *
57  * - Get rid of WikiNameRegexp and KeywordLinkRegexp as globals by finding
58  *   everywhere that uses them as variables and modify the code to use
59  *   them as constants.
60  */
61
62 include_once (dirname(__FILE__) . "/config.php");
63 include_once (dirname(__FILE__) . "/FileFinder.php");
64
65 /**
66  * Speed-up iniconfig loading.
67  *
68  * Dump the static parts of the parsed config/config.ini settings to a fast-loadable config.php file.
69  * The dynamic parts are then evaluated as before.
70  * Requires write-permissions to config/config.php
71  *
72  * @param string $file
73  */
74 function save_dump($file)
75 {
76     $vars =& $GLOBALS; // copy + unset not possible
77     $ignore = array();
78     foreach (array("SERVER", "ENV", "GET", "POST", "REQUEST", "COOKIE", "FILES") as $key) {
79         $ignore["HTTP_" . $key . "_VARS"]++;
80         $ignore["_" . $key]++;
81     }
82     foreach (array("HTTP_POST_FILES", "GLOBALS", "RUNTIMER", "ErrorManager", 'LANG',
83                  'HOME_PAGE', 'request', 'SCRIPT_NAME', 'VIRTUAL_PATH', 'SCRIPT_FILENAME') as $key)
84         $ignore[$key]++;
85     $fp = fopen($file, "wb");
86     fwrite($fp, "<?php\n");
87     fwrite($fp, "function wiki_configrestore(){\n");
88     //TODO: optimize this by removing ignore, big serialized array and merge into existing GLOBALS
89     foreach ($vars as $var => $val) {
90         if (!$ignore[$var])
91             fwrite($fp, "\$GLOBALS['" . $var . "']=unserialize(\""
92                 . addslashes(serialize($val)) . "\");\n");
93     }
94     // cannot be optimized, maybe leave away predefined consts somehow
95     foreach (get_defined_constants() as $var => $val) {
96         if (substr($var, 0, 4) != "PHP_" and substr($var, 0, 2) != "E_"
97             and substr($var, 0, 2) != "T_"  and substr($var, 0, 2) != "M_"
98         )
99             fwrite($fp, "if(!defined('" . $var . "')) define('" . $var . "',unserialize(\""
100                 . addslashes(serialize($val)) . "\"));\n");
101     }
102     fwrite($fp, "return 'noerr';}");
103     fwrite($fp, "?>");
104     fclose($fp);
105 }
106
107 function _check_int_constant(&$c)
108 {
109     // if int value == string value, force int type
110     if (sprintf("%d", (int)$c) === $c) { // DEBUG & _DEBUG_bla
111         $c = (int)$c;
112     }
113 }
114
115 function IniConfig($file)
116 {
117
118     // Optionally check config/config.php dump for faster startup
119     $dump = substr($file, 0, -3) . "php";
120     if (isWindows($dump)) $dump = str_replace("/", "\\", $dump);
121     if (file_exists($dump) and is_readable($dump) and filesize($dump) > 0 and sort_file_mtime($dump, $file) < 0) {
122         @include($dump) or die("Error including " . $dump);
123         if (function_exists('wiki_configrestore') and (wiki_configrestore() === 'noerr')) {
124             fixup_dynamic_configs();
125             return;
126         }
127     }
128
129     // First-time installer detection here...
130     // Similar to SetupWiki()
131     if (!file_exists($file)) {
132         // We need to DATA_PATH for configurator, or pass the posted values
133         // somewhow to the script
134         include_once(dirname(__FILE__) . "/install.php");
135         run_install("_part1");
136         if (!defined("_PHPWIKI_INSTALL_RUNNING"))
137             trigger_error("Datasource file '$file' does not exist", E_USER_ERROR);
138         exit();
139     }
140
141     // List of all valid config options to be define()d which take "values" (not
142     // booleans). Needs to be categorised, and generally made a lot tidier.
143     $_IC_VALID_VALUE = array
144     ('WIKI_NAME', 'ADMIN_USER', 'ADMIN_PASSWD',
145         'DEFAULT_DUMP_DIR', 'HTML_DUMP_DIR',
146         'HTML_DUMP_SUFFIX', 'MAX_UPLOAD_SIZE', 'MINOR_EDIT_TIMEOUT',
147         'ACCESS_LOG', 'CACHE_CONTROL', 'CACHE_CONTROL_MAX_AGE',
148         'COOKIE_EXPIRATION_DAYS', 'COOKIE_DOMAIN',
149         'PASSWORD_LENGTH_MINIMUM', 'USER_AUTH_POLICY',
150         'GROUP_METHOD',
151         'EDITING_POLICY', 'THEME',
152         'WIKI_PGSRC', 'DEFAULT_WIKI_PGSRC',
153         'ALLOWED_PROTOCOLS', 'INLINE_IMAGES', /*'KEYWORDS',*/
154         // extra logic:
155         //'DATABASE_PREFIX', 'DATABASE_DSN', 'DATABASE_TYPE', 'DATABASE_DBHANDLER',
156         'DATABASE_OPTIMISE_FREQUENCY',
157         'INTERWIKI_MAP_FILE', 'COPYRIGHTPAGE_TITLE', 'COPYRIGHTPAGE_URL',
158         'AUTHORPAGE_TITLE', 'AUTHORPAGE_URL',
159         'WIKI_NAME_REGEXP',
160         'PLUGIN_CACHED_DATABASE', 'PLUGIN_CACHED_FILENAME_PREFIX',
161         'PLUGIN_CACHED_HIGHWATER', 'PLUGIN_CACHED_LOWWATER', 'PLUGIN_CACHED_MAXLIFETIME',
162         'PLUGIN_CACHED_MAXARGLEN', 'PLUGIN_CACHED_IMGTYPES',
163         'WYSIWYG_BACKEND',
164         // extra logic:
165         'SERVER_NAME', 'SERVER_PORT', 'SCRIPT_NAME', 'DATA_PATH', 'PHPWIKI_DIR', 'VIRTUAL_PATH',
166         'EXTERNAL_HTML2PDF_PAGELIST', 'PLUGIN_CACHED_CACHE_DIR'
167     );
168
169     // Optional values which need to be defined.
170     // These are not defined in config-default.ini and empty if not defined.
171     $_IC_OPTIONAL_VALUE = array
172     (
173         'DEBUG', 'TEMP_DIR', 'DEFAULT_LANGUAGE',
174         'LDAP_AUTH_HOST', 'LDAP_SET_OPTION', 'LDAP_BASE_DN', 'LDAP_AUTH_USER',
175         'LDAP_AUTH_PASSWORD', 'LDAP_SEARCH_FIELD', 'LDAP_OU_GROUP', 'LDAP_OU_USERS',
176         'AUTH_USER_FILE', 'DBAUTH_AUTH_DSN',
177         'IMAP_AUTH_HOST', 'POP3_AUTH_HOST',
178         'AUTH_USER_FILE', 'AUTH_GROUP_FILE', 'AUTH_SESS_USER', 'AUTH_SESS_LEVEL',
179         'GOOGLE_LICENSE_KEY', 'FORTUNE_DIR',
180         'DISABLE_GETIMAGESIZE', 'DBADMIN_USER', 'DBADMIN_PASSWD',
181         'SESSION_SAVE_PATH',
182         'TOOLBAR_PAGELINK_PULLDOWN', 'TOOLBAR_TEMPLATE_PULLDOWN', 'TOOLBAR_IMAGE_PULLDOWN',
183         'EXTERNAL_LINK_TARGET', 'ACCESS_LOG_SQL', 'USE_EXTERNAL_HTML2PDF',
184         'LOGIN_LOG', 'LDAP_SEARCH_FILTER'
185     );
186
187     // List of all valid config options to be define()d which take booleans.
188     $_IC_VALID_BOOL = array
189     ('ENABLE_PAGEPERM', 'ENABLE_EDIT_TOOLBAR', 'JS_SEARCHREPLACE',
190         'ENABLE_XHTML_XML', 'ENABLE_DOUBLECLICKEDIT',
191         'USECACHE', 'WIKIDB_NOCACHE_MARKUP',
192         'ENABLE_REVERSE_DNS', 'ENCRYPTED_PASSWD', 'ZIPDUMP_AUTH',
193         'ENABLE_RAW_HTML', 'ENABLE_RAW_HTML_LOCKEDONLY', 'ENABLE_RAW_HTML_SAFE',
194         'STRICT_MAILABLE_PAGEDUMPS', 'COMPRESS_OUTPUT',
195         'ALLOW_ANON_USER', 'ALLOW_ANON_EDIT',
196         'ALLOW_BOGO_LOGIN', 'ALLOW_USER_PASSWORDS',
197         'AUTH_USER_FILE_STORABLE', 'ALLOW_HTTP_AUTH_LOGIN',
198         'ALLOW_USER_LOGIN', 'ALLOW_LDAP_LOGIN', 'ALLOW_IMAP_LOGIN',
199         'WARN_NONPUBLIC_INTERWIKIMAP', 'USE_PATH_INFO',
200         'DISABLE_HTTP_REDIRECT',
201         'PLUGIN_CACHED_USECACHE', 'PLUGIN_CACHED_FORCE_SYNCMAP',
202         'BLOG_DEFAULT_EMPTY_PREFIX', 'DATABASE_PERSISTENT',
203         'FUSIONFORGE',
204         'ENABLE_DISCUSSION_LINK', 'ENABLE_CAPTCHA',
205         'ENABLE_WYSIWYG', 'WYSIWYG_DEFAULT_PAGETYPE_HTML',
206         'DISABLE_MARKUP_WIKIWORD', 'ENABLE_MARKUP_COLOR', 'ENABLE_MARKUP_TEMPLATE',
207         'ENABLE_MARKUP_MEDIAWIKI_TABLE',
208         'ENABLE_MARKUP_DIVSPAN', 'USE_BYTEA', 'UPLOAD_USERDIR', 'DISABLE_UNITS',
209         'ENABLE_SEARCHHIGHLIGHT', 'DISABLE_UPLOAD_ONLY_ALLOWED_EXTENSIONS',
210         'ENABLE_AUTH_OPENID', 'INSECURE_ACTIONS_LOCALHOST_ONLY',
211         'ENABLE_MAILNOTIFY', 'ENABLE_RECENTCHANGESBOX', 'ENABLE_PAGE_PUBLIC',
212         'ENABLE_AJAX', 'ENABLE_EXTERNAL_PAGES',
213         'READONLY'
214     );
215
216     $rs = @parse_ini_file($file);
217     $rsdef = @parse_ini_file(dirname(__FILE__) . "/../config/config-default.ini");
218     foreach ($rsdef as $k => $v) {
219         if (defined($k)) {
220             $rs[$k] = constant($k);
221         } elseif (!isset($rs[$k])) {
222             $rs[$k] = $v;
223         }
224     }
225     unset($k);
226     unset($v);
227
228     foreach ($_IC_VALID_VALUE as $item) {
229         if (defined($item)) {
230             unset($rs[$item]);
231             continue;
232         }
233         if (array_key_exists($item, $rs)) {
234             _check_int_constant($rs[$item]);
235             define($item, $rs[$item]);
236             unset($rs[$item]);
237             //} elseif (array_key_exists($item, $rsdef)) {
238             //    define($item, $rsdef[$item]);
239             // calculate them later or not at all:
240         } elseif (in_array($item,
241             array('DATABASE_PREFIX', 'SERVER_NAME', 'SERVER_PORT',
242                 'SCRIPT_NAME', 'DATA_PATH', 'PHPWIKI_DIR', 'VIRTUAL_PATH',
243                 'LDAP_AUTH_HOST', 'IMAP_AUTH_HOST', 'POP3_AUTH_HOST',
244                 'PLUGIN_CACHED_CACHE_DIR', 'EXTERNAL_HTML2PDF_PAGELIST'))
245         ) {
246             ;
247         } elseif (!defined("_PHPWIKI_INSTALL_RUNNING")) {
248             trigger_error(sprintf("missing config setting for %s", $item));
249         }
250     }
251     unset($item);
252
253     // Boolean options are slightly special - if they're set to any of
254     // '', 'false', '0', or 'no' (all case-insensitive) then the value will
255     // be a boolean false, otherwise if there is anything set it'll
256     // be true.
257     foreach ($_IC_VALID_BOOL as $item) {
258         if (defined($item)) {
259             unset($rs[$item]);
260             continue;
261         }
262         if (array_key_exists($item, $rs)) {
263             $val = $rs[$item];
264             //} elseif (array_key_exists($item, $rsdef)) {
265             //    $val = $rsdef[$item];
266         } else {
267             $val = false;
268             //trigger_error(sprintf("missing boolean config setting for %s",$item));
269         }
270
271         // calculate them later: old or dynamic constants
272         if (!array_key_exists($item, $rs) and
273             in_array($item, array('USE_PATH_INFO', 'USE_DB_SESSION',
274                 'ALLOW_HTTP_AUTH_LOGIN', 'ALLOW_LDAP_LOGIN',
275                 'ALLOW_IMAP_LOGIN', 'ALLOW_USER_LOGIN',
276                 'REQUIRE_SIGNIN_BEFORE_EDIT',
277                 'WIKIDB_NOCACHE_MARKUP',
278                 'COMPRESS_OUTPUT', 'USE_BYTEA', 'READONLY',
279             ))
280         ) {
281             ;
282         } elseif (!$val) {
283             define($item, false);
284         } elseif (strtolower($val) == 'false' ||
285             strtolower($val) == 'no' ||
286             $val == '' ||
287             $val == false ||
288             $val == '0'
289         ) {
290             define($item, false);
291         } else {
292             define($item, true);
293         }
294         unset($rs[$item]);
295     }
296     unset($item);
297
298     // Database
299     global $DBParams;
300     foreach (array('DATABASE_TYPE' => 'dbtype',
301                  'DATABASE_DSN' => 'dsn',
302                  'DATABASE_SESSION_TABLE' => 'db_session_table',
303                  'DATABASE_DBA_HANDLER' => 'dba_handler',
304                  'DATABASE_DIRECTORY' => 'directory',
305                  'DATABASE_TIMEOUT' => 'timeout',
306                  'DATABASE_PREFIX' => 'prefix')
307              as $item => $k) {
308         if (defined($item)) {
309             $DBParams[$k] = constant($item);
310             unset($rs[$item]);
311         } elseif (array_key_exists($item, $rs)) {
312             $DBParams[$k] = $rs[$item];
313             define($item, $rs[$item]);
314             unset($rs[$item]);
315         } elseif (array_key_exists($item, $rsdef)) {
316             $DBParams[$k] = $rsdef[$item];
317             define($item, $rsdef[$item]);
318             unset($rsdef[$item]);
319         }
320     }
321     $valid_database_types = array('SQL', 'ADODB', 'PDO', 'dba', 'file', 'flatfile');
322     if (!in_array(DATABASE_TYPE, $valid_database_types))
323         trigger_error(sprintf("Invalid DATABASE_TYPE=%s. Choose one of %s",
324                 DATABASE_TYPE, join(",", $valid_database_types)),
325             E_USER_ERROR);
326     unset($valid_database_types);
327     if (DATABASE_TYPE == 'PDO') {
328         // try to load it dynamically (unix only)
329         if (!loadPhpExtension("pdo")) {
330             echo $GLOBALS['php_errormsg'], "<br>\n";
331             trigger_error(sprintf("dl() problem: Required extension “%s” could not be loaded!",
332                     "pdo"),
333                 E_USER_ERROR);
334         }
335     }
336     // Detect readonly database, e.g. system mounted read-only for maintenance
337     // via dbh->readonly later. Unfortunately not possible as constant.
338
339     // USE_DB_SESSION default logic:
340     if (!defined('USE_DB_SESSION')) {
341         if ($DBParams['db_session_table']
342             and in_array($DBParams['dbtype'], array('SQL', 'ADODB', 'PDO', 'dba'))
343         ) {
344             define('USE_DB_SESSION', true);
345         } else {
346             define('USE_DB_SESSION', false);
347         }
348     }
349     unset($item);
350     unset($k);
351
352     // Expiry stuff
353     global $ExpireParams;
354     foreach (array('major', 'minor', 'author') as $major) {
355         foreach (array('max_age', 'min_age', 'min_keep', 'keep', 'max_keep') as $max) {
356             $item = strtoupper($major) . '_' . strtoupper($max);
357             if (defined($item))
358                 $val = constant($item);
359             elseif (array_key_exists($item, $rs))
360                 $val = $rs[$item];
361             elseif (array_key_exists($item, $rsdef))
362                 $val = $rsdef[$item];
363             if (!isset($ExpireParams[$major]))
364                 $ExpireParams[$major] = array();
365             $ExpireParams[$major][$max] = $val;
366             unset($rs[$item]);
367         }
368     }
369     unset($item);
370     unset($major);
371     unset($max);
372
373     // User authentication
374     if (!isset($GLOBALS['USER_AUTH_ORDER'])) {
375         if (isset($rs['USER_AUTH_ORDER']))
376             $GLOBALS['USER_AUTH_ORDER'] = preg_split('/\s*:\s*/',
377                 $rs['USER_AUTH_ORDER']);
378         else
379             $GLOBALS['USER_AUTH_ORDER'] = array("PersonalPage");
380     }
381
382     // Now it's the external DB authentication stuff's turn
383     if (in_array('Db', $GLOBALS['USER_AUTH_ORDER']) && empty($rs['DBAUTH_AUTH_DSN'])) {
384         $rs['DBAUTH_AUTH_DSN'] = $DBParams['dsn'];
385     }
386
387     global $DBAuthParams;
388     $DBAP_MAP = array('DBAUTH_AUTH_DSN' => 'auth_dsn',
389         'DBAUTH_AUTH_CHECK' => 'auth_check',
390         'DBAUTH_AUTH_USER_EXISTS' => 'auth_user_exists',
391         'DBAUTH_AUTH_CRYPT_METHOD' => 'auth_crypt_method',
392         'DBAUTH_AUTH_UPDATE' => 'auth_update',
393         'DBAUTH_AUTH_CREATE' => 'auth_create',
394         'DBAUTH_PREF_SELECT' => 'pref_select',
395         'DBAUTH_PREF_INSERT' => 'pref_insert',
396         'DBAUTH_PREF_UPDATE' => 'pref_update',
397         'DBAUTH_IS_MEMBER' => 'is_member',
398         'DBAUTH_GROUP_MEMBERS' => 'group_members',
399         'DBAUTH_USER_GROUPS' => 'user_groups'
400     );
401     foreach ($DBAP_MAP as $rskey => $apkey) {
402         if (defined($rskey)) {
403             $DBAuthParams[$apkey] = constant($rskey);
404         } elseif (isset($rs[$rskey])) {
405             $DBAuthParams[$apkey] = $rs[$rskey];
406             define($rskey, $rs[$rskey]);
407         } elseif (isset($rsdef[$rskey])) {
408             $DBAuthParams[$apkey] = $rsdef[$rskey];
409             define($rskey, $rsdef[$rskey]);
410         }
411         unset($rs[$rskey]);
412     }
413     unset($rskey);
414     unset($apkey);
415
416     // TODO: Currently unsupported on non-SQL. Nice to have for RhNavPlugin
417     // CHECKME: PDO
418     if (!defined('ACCESS_LOG_SQL')) {
419         if (array_key_exists('ACCESS_LOG_SQL', $rs)) {
420             // WikiDB_backend::isSql() not yet loaded
421             if (!in_array(DATABASE_TYPE, array('SQL', 'ADODB', 'PDO'))) {
422                 // override false config setting on no SQL WikiDB database.
423                 define('ACCESS_LOG_SQL', 0);
424             }
425             // SQL defaults to ACCESS_LOG_SQL = 2
426         } else {
427             define('ACCESS_LOG_SQL',
428             in_array(DATABASE_TYPE, array('SQL', 'ADODB', 'PDO')) ? 2 : 0);
429         }
430     }
431
432     if (empty($rs['TEMP_DIR'])) {
433         $rs['TEMP_DIR'] = "/tmp";
434         if (getenv("TEMP"))
435             $rs['TEMP_DIR'] = getenv("TEMP");
436     }
437     // optional values will be set to '' to simplify the logic.
438     foreach ($_IC_OPTIONAL_VALUE as $item) {
439         if (defined($item)) {
440             unset($rs[$item]);
441             continue;
442         }
443         if (array_key_exists($item, $rs)) {
444             _check_int_constant($rs[$item]);
445             define($item, $rs[$item]);
446             unset($rs[$item]);
447         } else
448             define($item, '');
449     }
450
451     if (USE_EXTERNAL_HTML2PDF) {
452         $item = 'EXTERNAL_HTML2PDF_PAGELIST';
453         if (defined($item)) {
454             unset($rs[$item]);
455         } elseif (array_key_exists($item, $rs)) {
456             define($item, $rs[$item]);
457             unset($rs[$item]);
458         } elseif (array_key_exists($item, $rsdef)) {
459             define($item, $rsdef[$item]);
460         }
461     }
462     unset($item);
463
464     // LDAP bind options
465     global $LDAP_SET_OPTION;
466     if (defined('LDAP_SET_OPTION') and LDAP_SET_OPTION) {
467         $optlist = preg_split('/\s*:\s*/', LDAP_SET_OPTION);
468         foreach ($optlist as $opt) {
469             $bits = preg_split('/\s*=\s*/', $opt, 2);
470             if (count($bits) == 2) {
471                 if (is_string($bits[0]) and defined($bits[0]))
472                     $bits[0] = constant($bits[0]);
473                 $LDAP_SET_OPTION[$bits[0]] = $bits[1];
474             } else {
475                 // Possibly throw some sort of error?
476             }
477         }
478         unset($opt);
479         unset($bits);
480     }
481
482     // Default Wiki pages to force loading from pgsrc
483     global $GenericPages;
484     $GenericPages = preg_split('/\s*:\s*/', @$rs['DEFAULT_WIKI_PAGES']);
485
486     // Wiki name regexp:  Should be a define(), but might needed to be changed at runtime
487     // (different LC_CHAR need different posix classes)
488     global $WikiNameRegexp;
489     $WikiNameRegexp = constant('WIKI_NAME_REGEXP');
490     if (!trim($WikiNameRegexp))
491         $WikiNameRegexp = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
492
493     // Got rid of global $KeywordLinkRegexp by using a TextSearchQuery instead
494     // of "Category:Topic"
495     if (!isset($rs['KEYWORDS'])) $rs['KEYWORDS'] = @$rsdef['KEYWORDS'];
496     if (!isset($rs['KEYWORDS'])) $rs['KEYWORDS'] = "Category* OR Topic*";
497     if ($rs['KEYWORDS'] == 'Category:Topic') $rs['KEYWORDS'] = "Category* OR Topic*";
498     if (!defined('KEYWORDS')) define('KEYWORDS', $rs['KEYWORDS']);
499     //if (empty($keywords)) $keywords = array("Category","Topic");
500     //$KeywordLinkRegexp = '(?<=' . implode('|^', $keywords) . ')[[:upper:]].*$';
501
502     // TODO: can this be a constant?
503     global $DisabledActions;
504     if (!array_key_exists('DISABLED_ACTIONS', $rs)
505         and array_key_exists('DISABLED_ACTIONS', $rsdef)
506     )
507         $rs['DISABLED_ACTIONS'] = @$rsdef['DISABLED_ACTIONS'];
508     if (array_key_exists('DISABLED_ACTIONS', $rs))
509         $DisabledActions = preg_split('/\s*:\s*/', $rs['DISABLED_ACTIONS']);
510
511     global $PLUGIN_CACHED_IMGTYPES;
512     $PLUGIN_CACHED_IMGTYPES = preg_split('/\s*[|:]\s*/', PLUGIN_CACHED_IMGTYPES);
513
514     if (!defined('PLUGIN_CACHED_CACHE_DIR')) {
515         if (empty($rs['PLUGIN_CACHED_CACHE_DIR']) and !empty($rsdef['PLUGIN_CACHED_CACHE_DIR']))
516             $rs['PLUGIN_CACHED_CACHE_DIR'] = $rsdef['PLUGIN_CACHED_CACHE_DIR'];
517         if (empty($rs['PLUGIN_CACHED_CACHE_DIR'])) {
518             if (!empty($rs['INCLUDE_PATH'])) {
519                 @ini_set('include_path', $rs['INCLUDE_PATH']);
520                 $GLOBALS['INCLUDE_PATH'] = $rs['INCLUDE_PATH'];
521             }
522             $rs['PLUGIN_CACHED_CACHE_DIR'] = TEMP_DIR . '/cache';
523             if (!FindFile($rs['PLUGIN_CACHED_CACHE_DIR'], 1)) { // [29ms]
524                 FindFile(TEMP_DIR, false, 1); // TEMP must exist!
525                 mkdir($rs['PLUGIN_CACHED_CACHE_DIR'], 0777);
526             }
527             // will throw an error if not exists.
528             define('PLUGIN_CACHED_CACHE_DIR', FindFile($rs['PLUGIN_CACHED_CACHE_DIR'], false, 1));
529         } else {
530             define('PLUGIN_CACHED_CACHE_DIR', $rs['PLUGIN_CACHED_CACHE_DIR']);
531             // will throw an error if not exists.
532             FindFile(PLUGIN_CACHED_CACHE_DIR);
533         }
534     }
535
536     // process the rest of the config.ini settings:
537     foreach ($rs as $item => $v) {
538         if (defined($item)) {
539             continue;
540         } else {
541             _check_int_constant($v);
542             define($item, $v);
543         }
544     }
545     unset($item);
546     unset($v);
547
548     unset($rs);
549     unset($rsdef);
550
551     fixup_static_configs($file); //[1ms]
552     // Dump all globals and constants
553     // The question is if reading this is faster then doing IniConfig() + fixup_static_configs()
554     if (is_writable($dump)) {
555         save_dump($dump);
556     }
557     // store locale[] in config.php? This is too problematic.
558     fixup_dynamic_configs(); // [100ms]
559 }
560
561 function _ignore_unknown_charset_warning(&$error)
562 {
563     if (preg_match('/^htmlspecialchars\(\): charset \`.+\' not supported, assuming iso-8859-1/',
564         $error->errstr)
565     ) {
566         $error->errno = 0;
567         return true; // Ignore error
568     }
569     return false;
570 }
571
572 // moved from lib/config.php [1ms]
573 function fixup_static_configs($file)
574 {
575     global $FieldSeparator, $AllActionPages;
576     global $DBParams;
577     // init FileFinder to add proper include paths
578     FindFile("lib/interwiki.map", true);
579
580     // $FieldSeparator = "\xFF"; // this byte should never appear in utf-8
581     $FieldSeparator = "\xFF";
582
583     // All pages containing plugins of the same name as the filename
584     $ActionPages = explode(':',
585         'AllPages:AllUsers:AppendText:AuthorHistory:'
586             . 'BackLinks:BlogArchives:BlogJournal:'
587             . 'CreatePage:'
588             . 'FullTextSearch:FuzzyPages:'
589             . 'LikePages:LinkDatabase:LinkSearch:ListRelations:'
590             . 'ModeratedPage:MostPopular:'
591             . 'NewPagesPerUser:'
592             . 'OrphanedPages:'
593             . 'PageDump:PageHistory:PageInfo:PluginManager:'
594             . 'RateIt:' // RateIt works only in wikilens derived themes
595             . 'RandomPage:RecentChanges:RelatedChanges:RecentEdits:'
596             . 'SearchHighlight:SemanticRelations:SemanticSearch:SystemInfo:'
597             . 'TitleSearch:'
598             . 'UpLoad:UserPreferences:'
599             . 'UserRatings:' // UserRatings works only in wikilens derived themes
600             . 'WantedPages:WatchPage:WikiBlog:WhoIsOnline:WikiAdminSelect');
601
602     // The FUSIONFORGE theme omits them
603     if (!(defined('FUSIONFORGE') && FUSIONFORGE)) {
604         // Add some some action pages
605         $ActionPages[] = 'DebugInfo';
606         $ActionPages[] = 'SpellCheck'; // SpellCheck does not work
607         $ActionPages[] = 'EditMetaData';
608         $ActionPages[] = 'InterWikiSearch';
609         $ActionPages[] = 'LdapSearch';
610         $ActionPages[] = 'PasswordReset';
611         $ActionPages[] = 'RecentComments';
612         $ActionPages[] = 'TranslateText';
613         $ActionPages[] = 'UriResolver';
614     }
615
616     global $AllAllowedPlugins;
617     $AllAllowedPlugins = $ActionPages;
618     // Add plugins that have no corresponding action page
619     $AllAllowedPlugins[] = 'AddComment';
620     $AllAllowedPlugins[] = 'AsciiMath';
621     $AllAllowedPlugins[] = 'AsciiSVG';
622     $AllAllowedPlugins[] = 'AtomFeed';
623     $AllAllowedPlugins[] = 'BoxRight';
624     $AllAllowedPlugins[] = 'CalendarList';
625     $AllAllowedPlugins[] = 'Calendar';
626     $AllAllowedPlugins[] = 'CategoryPage';
627     $AllAllowedPlugins[] = 'Chart';
628     $AllAllowedPlugins[] = 'Comment';
629     $AllAllowedPlugins[] = 'CreateBib';
630     $AllAllowedPlugins[] = 'CreateToc';
631     $AllAllowedPlugins[] = 'CurrentTime';
632     $AllAllowedPlugins[] = 'DeadEndPages';
633     $AllAllowedPlugins[] = 'Diff';
634     $AllAllowedPlugins[] = 'DynamicIncludePage';
635     $AllAllowedPlugins[] = 'ExternalSearch';
636     $AllAllowedPlugins[] = 'FacebookLike';
637     $AllAllowedPlugins[] = 'FileInfo';
638     $AllAllowedPlugins[] = 'GoogleMaps';
639     $AllAllowedPlugins[] = 'GooglePlugin';
640     $AllAllowedPlugins[] = 'GoTo';
641     $AllAllowedPlugins[] = 'HelloWorld';
642     $AllAllowedPlugins[] = 'IncludePage';
643     $AllAllowedPlugins[] = 'IncludePages';
644     $AllAllowedPlugins[] = 'IncludeSiteMap';
645     $AllAllowedPlugins[] = 'IncludeTree';
646     $AllAllowedPlugins[] = 'ListPages';
647     $AllAllowedPlugins[] = 'ListSubpages';
648     $AllAllowedPlugins[] = 'MediawikiTable';
649     $AllAllowedPlugins[] = 'NoCache';
650     $AllAllowedPlugins[] = 'OldStyleTable';
651     $AllAllowedPlugins[] = 'PageGroup';
652     $AllAllowedPlugins[] = 'PageTrail';
653     $AllAllowedPlugins[] = 'PhotoAlbum';
654     $AllAllowedPlugins[] = 'PhpHighlight';
655     $AllAllowedPlugins[] = 'PopularTags';
656     $AllAllowedPlugins[] = 'PopUp';
657     $AllAllowedPlugins[] = 'PrevNext';
658     $AllAllowedPlugins[] = 'Processing';
659     $AllAllowedPlugins[] = 'RawHtml';
660     $AllAllowedPlugins[] = 'RecentChangesCached';
661     $AllAllowedPlugins[] = 'RecentReferrers';
662     $AllAllowedPlugins[] = 'RedirectTo';
663     $AllAllowedPlugins[] = 'RichTable';
664     $AllAllowedPlugins[] = 'RssFeed';
665     $AllAllowedPlugins[] = 'SemanticSearchAdvanced';
666     $AllAllowedPlugins[] = 'SiteMap';
667     $AllAllowedPlugins[] = 'SyncWiki';
668     $AllAllowedPlugins[] = 'SyntaxHighlighter';
669     $AllAllowedPlugins[] = 'Template';
670     $AllAllowedPlugins[] = 'Transclude';
671     $AllAllowedPlugins[] = 'UnfoldSubpages';
672     $AllAllowedPlugins[] = 'Video';
673     $AllAllowedPlugins[] = 'WikiAdminChown';
674     $AllAllowedPlugins[] = 'WikiAdminPurge';
675     $AllAllowedPlugins[] = 'WikiAdminRemove';
676     $AllAllowedPlugins[] = 'WikiAdminRename';
677     $AllAllowedPlugins[] = 'WikiAdminSearchReplace';
678     $AllAllowedPlugins[] = 'WikiAdminDeleteAcl';
679     $AllAllowedPlugins[] = 'WikiAdminSetAcl';
680     $AllAllowedPlugins[] = 'WikiAdminSetAclSimple';
681     $AllAllowedPlugins[] = 'WikiAdminUtils';
682     $AllAllowedPlugins[] = 'WikicreoleTable';
683     $AllAllowedPlugins[] = 'WikiForm';
684     $AllAllowedPlugins[] = 'WikiFormRich';
685     $AllAllowedPlugins[] = 'WikiPoll';
686     $AllAllowedPlugins[] = 'YouTube';
687     $AllAllowedPlugins[] = 'DebugGroupInfo';
688     $AllAllowedPlugins[] = 'DebugAuthInfo';
689     $AllAllowedPlugins[] = 'DebugBackendInfo';
690     $AllAllowedPlugins[] = 'DebugRetransform';
691
692     // The FUSIONFORGE theme omits them
693     if (!(defined('FUSIONFORGE') && FUSIONFORGE)) {
694         $AllAllowedPlugins[] = 'AnalyseAccessLogSql';
695         $AllAllowedPlugins[] = 'CacheTest';
696         $AllAllowedPlugins[] = 'CategoryPage';
697         $AllAllowedPlugins[] = 'FoafViewer';
698         $AllAllowedPlugins[] = 'GraphViz';
699         $AllAllowedPlugins[] = 'HtmlConverter';
700         $AllAllowedPlugins[] = 'JabberPresence';
701         $AllAllowedPlugins[] = 'ListPages';
702         $AllAllowedPlugins[] = 'PhpWeather';
703         $AllAllowedPlugins[] = 'Ploticus';
704         $AllAllowedPlugins[] = 'PopularNearby';
705         $AllAllowedPlugins[] = 'PreferenceApp';
706         $AllAllowedPlugins[] = 'PreferencesInfo';
707         $AllAllowedPlugins[] = 'SqlResult';
708         $AllAllowedPlugins[] = 'TeX2png';
709         $AllAllowedPlugins[] = 'text2png';
710         $AllAllowedPlugins[] = 'TexToPng';
711         $AllAllowedPlugins[] = 'VisualWiki';
712         $AllAllowedPlugins[] = 'WantedPagesOld';
713         $AllAllowedPlugins[] = 'WikiForum';
714         $AllAllowedPlugins[] = 'WikiTranslation';
715     }
716
717     // Used by SetupWiki to pull in required pages, if not translated, then in English.
718     // Also used by WikiTranslation. Really important are only those which return pagelists
719     // or contain basic functionality.
720     $AllActionPages = $ActionPages;
721     $AllActionPages[] = 'AllPagesCreatedByMe';
722     $AllActionPages[] = 'AllPagesLastEditedByMe';
723     $AllActionPages[] = 'AllPagesOwnedByMe';
724     $AllActionPages[] = 'AllPagesByAcl';
725     $AllActionPages[] = 'AllUserPages';
726     $AllActionPages[] = 'FullRecentChanges';
727     $AllActionPages[] = 'LeastPopular';
728     $AllActionPages[] = 'LockedPages';
729     $AllActionPages[] = 'MyRatings'; // MyRatings works only in wikilens-derived themes
730     $AllActionPages[] = 'MyRecentEdits';
731     $AllActionPages[] = 'MyRecentChanges';
732     $AllActionPages[] = 'PhpWikiAdministration';
733     $AllActionPages[] = 'PhpWikiAdministration/Chown';
734     $AllActionPages[] = 'PhpWikiAdministration/Purge';
735     $AllActionPages[] = 'PhpWikiAdministration/Remove';
736     $AllActionPages[] = 'PhpWikiAdministration/Rename';
737     $AllActionPages[] = 'PhpWikiAdministration/SearchReplace';
738     $AllActionPages[] = 'PhpWikiAdministration/DeleteAcl';
739     $AllActionPages[] = 'PhpWikiAdministration/SetAcl';
740     $AllActionPages[] = 'PhpWikiAdministration/SetAclSimple';
741     $AllActionPages[] = 'RecentChangesMyPages';
742     $AllActionPages[] = 'RecentEdits';
743     $AllActionPages[] = 'RecentNewPages';
744     $AllActionPages[] = 'SetGlobalAccessRights';
745     $AllActionPages[] = 'SetGlobalAccessRightsSimple';
746     $AllActionPages[] = 'UserContribs';
747
748     if ((defined('FUSIONFORGE') && FUSIONFORGE)) {
749         if (ENABLE_EXTERNAL_PAGES) {
750             $AllAllowedPlugins[] = 'WikiAdminSetExternal';
751             $AllActionPages[] = 'PhpWikiAdministration/SetExternal';
752             $AllActionPages[] = 'ExternalPages';
753         }
754     }
755
756     // If user has not defined PHPWIKI_DIR, and we need it
757     if (!defined('PHPWIKI_DIR') and !file_exists("themes/default")) {
758         $themes_dir = FindFile("themes");
759         define('PHPWIKI_DIR', dirname($themes_dir));
760     }
761
762     // If user has not defined DATA_PATH, we want to use relative URLs.
763     if (!defined('DATA_PATH')) {
764         // fix similar to the one suggested by jkalmbach for
765         // installations in the webrootdir, like "http://phpwiki.fr/HomePage"
766         if (!defined('SCRIPT_NAME'))
767             define('SCRIPT_NAME', deduce_script_name());
768         $temp = dirname(SCRIPT_NAME);
769         if (($temp == '/') || ($temp == '\\'))
770             $temp = '';
771         define('DATA_PATH', $temp);
772         /*
773         if (USE_PATH_INFO)
774             define('DATA_PATH', '..');
775         */
776     }
777
778     //////////////////////////////////////////////////////////////////
779     // Select database
780     //
781     if (empty($DBParams['dbtype']))
782         $DBParams['dbtype'] = 'dba';
783
784     if (!defined('THEME'))
785         define('THEME', 'default');
786
787     // Basic configurator validation
788     if (!defined('ADMIN_USER') or ADMIN_USER == '') {
789         $error = sprintf("%s may not be empty. Please update your configuration.",
790             "ADMIN_USER");
791         // protect against recursion
792         if (!preg_match("/config\-(dist|default)\.ini$/", $file)
793             and !defined("_PHPWIKI_INSTALL_RUNNING")
794         ) {
795             include_once(dirname(__FILE__) . "/install.php");
796             run_install("_part1");
797             trigger_error($error, E_USER_ERROR);
798             exit();
799         } elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
800             $GLOBALS['HTTP_GET_VARS']['show'] = '_part1';
801             trigger_error($error, E_USER_WARNING);
802         }
803     }
804     if (!defined('ADMIN_PASSWD') or ADMIN_PASSWD == '') {
805         $error = sprintf("%s may not be empty. Please update your configuration.",
806             "ADMIN_PASSWD");
807         // protect against recursion
808         if (!preg_match("/config\-(dist|default)\.ini$/", $file)
809             and !defined("_PHPWIKI_INSTALL_RUNNING")
810         ) {
811             include_once(dirname(__FILE__) . "/install.php");
812             run_install("_part1");
813             trigger_error($error, E_USER_ERROR);
814             exit();
815         } elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
816             $GLOBALS['HTTP_GET_VARS']['show'] = '_part1';
817             trigger_error($error, E_USER_WARNING);
818         }
819     }
820
821     if (defined('USE_DB_SESSION') and USE_DB_SESSION) {
822         if (!$DBParams['db_session_table']) {
823             $DBParams['db_session_table'] = @$DBParams['prefix'] . 'session';
824             trigger_error(sprintf("DATABASE_SESSION_TABLE configuration set to %s.",
825                     $DBParams['db_session_table']),
826                 E_USER_ERROR);
827         }
828     }
829     // legacy:
830     if (!defined('ALLOW_USER_LOGIN'))
831         define('ALLOW_USER_LOGIN', defined('ALLOW_USER_PASSWORDS') && ALLOW_USER_PASSWORDS);
832     if (!defined('ALLOW_ANON_USER')) define('ALLOW_ANON_USER', true);
833     if (!defined('ALLOW_ANON_EDIT')) define('ALLOW_ANON_EDIT', false);
834     if (!defined('REQUIRE_SIGNIN_BEFORE_EDIT')) define('REQUIRE_SIGNIN_BEFORE_EDIT', !ALLOW_ANON_EDIT);
835     if (!defined('ALLOW_BOGO_LOGIN')) define('ALLOW_BOGO_LOGIN', true);
836     if (ALLOW_USER_LOGIN and !empty($DBAuthParams) and empty($DBAuthParams['auth_dsn'])) {
837         if (isset($DBParams['dsn']))
838             $DBAuthParams['auth_dsn'] = $DBParams['dsn'];
839     }
840 }
841
842 /**
843  * Define constants which are client or request specific and should not be dumped statically.
844  * Such as the language, and the virtual and server paths, which might be overridden
845  * by startup scripts for wiki farms.
846  */
847 function fixup_dynamic_configs()
848 {
849     global $LANG;
850
851     if (defined('INCLUDE_PATH') and INCLUDE_PATH) {
852         @ini_set('include_path', INCLUDE_PATH);
853         $GLOBALS['INCLUDE_PATH'] = INCLUDE_PATH;
854     }
855     if (defined('SESSION_SAVE_PATH') and SESSION_SAVE_PATH)
856         @ini_set('session.save_path', SESSION_SAVE_PATH);
857     if (!defined('DEFAULT_LANGUAGE')) // not needed anymore
858         define('DEFAULT_LANGUAGE', ''); // detect from client
859
860     // FusionForge hack
861     if (!(defined('FUSIONFORGE') && FUSIONFORGE)) {
862         // Disable update_locale because Zend Debugger crash
863         if (!extension_loaded('Zend Debugger')) {
864             update_locale(isset($LANG) ? $LANG : DEFAULT_LANGUAGE);
865         }
866     }
867
868     if (empty($LANG)) {
869         if (!defined("DEFAULT_LANGUAGE") or !DEFAULT_LANGUAGE) {
870             // TODO: defer this to WikiRequest::initializeLang()
871             $LANG = guessing_lang();
872             guessing_setlocale(LC_ALL, $LANG);
873         } else
874             $LANG = DEFAULT_LANGUAGE;
875     }
876
877     // Set up (possibly fake) gettext()
878     // Todo: this could be moved to fixup_static_configs()
879     // Bug #1381464 with php-5.1.1
880     if (!function_exists('bindtextdomain')
881         and !function_exists('gettext')
882             and !function_exists('_')
883     ) {
884         $locale = array();
885
886         function gettext($text)
887         {
888             global $locale;
889             if (!empty ($locale[$text]))
890                 return $locale[$text];
891             return $text;
892         }
893
894         function _($text)
895         {
896             return gettext($text);
897         }
898     } else {
899         $chback = 0;
900         if ($LANG != 'en') {
901
902             // Working around really weird gettext problems: (4.3.2, 4.3.6 win)
903             // bindtextdomain() in returns the current domain path.
904             // 1. If the script is not index.php but something like "de", on a different path
905             //    then bindtextdomain() fails, but after chdir to the correct path it will work okay.
906             // 2. But the weird error "Undefined variable: bindtextdomain" is generated then.
907             $bindtextdomain_path = FindFile("locale", false, true);
908             if (isWindows())
909                 $bindtextdomain_path = str_replace("/", "\\", $bindtextdomain_path);
910             $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain_path);
911             if (realpath($bindtextdomain_real) != realpath($bindtextdomain_path)) {
912                 // this will happen with virtual_paths. chdir and try again.
913                 chdir($bindtextdomain_path);
914                 $chback = 1;
915                 $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain_path);
916             }
917         }
918         // tell gettext not to use unicode. PHP >= 4.2.0. Thanks to Kai Krakow.
919         if ($LANG != 'en')
920             textdomain("phpwiki");
921         if ($chback) { // change back
922             chdir($bindtextdomain_real . (isWindows() ? "\\.." : "/.."));
923         }
924     }
925
926     // language dependent updates:
927     if (!defined('CATEGORY_GROUP_PAGE'))
928         define('CATEGORY_GROUP_PAGE', _("CategoryGroup"));
929     if (!defined('WIKI_NAME'))
930         define('WIKI_NAME', _("An unnamed PhpWiki"));
931     if (!defined('HOME_PAGE'))
932         define('HOME_PAGE', __("HomePage"));
933
934     //////////////////////////////////////////////////////////////////
935     // Autodetect URL settings:
936     //
937     foreach (array('SERVER_NAME', 'SERVER_PORT') as $var) {
938         //FIXME: for CGI without _SERVER
939         if (!defined($var) and !empty($_SERVER[$var]))
940             // IPV6 fix by matt brown, #1546571
941             // An IPv6 address must be surrounded by square brackets to form a valid server name.
942             if ($var == 'SERVER_NAME' &&
943                 strstr($_SERVER[$var], ':')
944             ) {
945                 define($var, '[' . $_SERVER[$var] . ']');
946             } else {
947                 define($var, $_SERVER[$var]);
948             }
949     }
950     if (!defined('SERVER_NAME')) define('SERVER_NAME', '127.0.0.1');
951     if (!defined('SERVER_PORT')) define('SERVER_PORT', 80);
952     if (!defined('SERVER_PROTOCOL')) {
953         if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off')
954             define('SERVER_PROTOCOL', 'http');
955         else
956             define('SERVER_PROTOCOL', 'https');
957     }
958
959     if (!defined('SCRIPT_NAME'))
960         define('SCRIPT_NAME', deduce_script_name());
961
962     if (!defined('USE_PATH_INFO')) {
963         if (isCGI())
964             define('USE_PATH_INFO', false);
965         else {
966             /*
967              * If SCRIPT_NAME does not look like php source file,
968              * or user cgi we assume that php is getting run by an
969              * action handler in /cgi-bin.  In this case,
970              * I think there is no way to get Apache to pass
971              * useful PATH_INFO to the php script (PATH_INFO
972              * is used to the the php interpreter where the
973              * php script is...)
974              */
975             switch (php_sapi_name()) {
976                 case 'apache':
977                 case 'apache2handler':
978                     define('USE_PATH_INFO', true);
979                     break;
980                 case 'cgi':
981                 case 'apache2filter':
982                     define('USE_PATH_INFO', false);
983                     break;
984                 default:
985                     define('USE_PATH_INFO', ereg('\.(php3?|cgi)$', SCRIPT_NAME));
986                     break;
987             }
988         }
989     }
990
991     if (SERVER_PORT
992         && SERVER_PORT != (SERVER_PROTOCOL == 'https' ? 443 : 80)
993     ) {
994         define('SERVER_URL',
995             SERVER_PROTOCOL . '://' . SERVER_NAME . ':' . SERVER_PORT);
996     } else {
997         define('SERVER_URL',
998             SERVER_PROTOCOL . '://' . SERVER_NAME);
999     }
1000
1001     if (!defined('VIRTUAL_PATH')) {
1002         // We'd like to auto-detect when the cases where apaches
1003         // 'Action' directive (or similar means) is used to
1004         // redirect page requests to a cgi-handler.
1005         //
1006         // In cases like this, requests for e.g. /wiki/HomePage
1007         // get redirected to a cgi-script called, say,
1008         // /path/to/wiki/index.php.  The script gets all
1009         // of /wiki/HomePage as it's PATH_INFO.
1010         //
1011         // The problem is:
1012         //   How to detect when this has happened reliably?
1013         //   How to pick out the "virtual path" (in this case '/wiki')?
1014         //
1015         // (Another time an redirect might occur is to a DirectoryIndex
1016         // -- the requested URI is '/wikidir/', the request gets
1017         // passed to '/wikidir/index.php'.  In this case, the
1018         // proper VIRTUAL_PATH is '/wikidir/index.php', since the
1019         // pages will appear at e.g. '/wikidir/index.php/HomePage'.
1020         //
1021
1022         $REDIRECT_URL = &$_SERVER['REDIRECT_URL'];
1023         if (USE_PATH_INFO and isset($REDIRECT_URL)
1024             and !IsProbablyRedirectToIndex()
1025         ) {
1026             // FIXME: This is a hack, and won't work if the requested
1027             // pagename has a slash in it.
1028             $temp = strtr(dirname($REDIRECT_URL . 'x'), "\\", '/');
1029             if (($temp == '/') || ($temp == '\\'))
1030                 $temp = '';
1031             define('VIRTUAL_PATH', $temp);
1032         } else {
1033             define('VIRTUAL_PATH', SCRIPT_NAME);
1034         }
1035     }
1036
1037     if (!defined('PATH_INFO_PREFIX')) {
1038         if (VIRTUAL_PATH != SCRIPT_NAME) {
1039             // Apache action handlers are used.
1040             define('PATH_INFO_PREFIX', VIRTUAL_PATH . '/');
1041         } else {
1042             define('PATH_INFO_PREFIX', '/');
1043         }
1044     }
1045
1046     define('PHPWIKI_BASE_URL',
1047         SERVER_URL . (USE_PATH_INFO ? VIRTUAL_PATH . '/' : SCRIPT_NAME));
1048
1049     // Detect PrettyWiki setup (not loading index.php directly)
1050     // $SCRIPT_FILENAME should be the same as __FILE__ in index.php
1051     if (!isset($SCRIPT_FILENAME))
1052         $SCRIPT_FILENAME = @$_SERVER['SCRIPT_FILENAME'];
1053     if (!isset($SCRIPT_FILENAME))
1054         $SCRIPT_FILENAME = @$_ENV['SCRIPT_FILENAME'];
1055     if (!isset($SCRIPT_FILENAME))
1056         $SCRIPT_FILENAME = dirname(__FILE__ . '/../') . '/index.php';
1057     if (isWindows())
1058         $SCRIPT_FILENAME = str_replace('\\\\', '\\', strtr($SCRIPT_FILENAME, '/', '\\'));
1059     define('SCRIPT_FILENAME', $SCRIPT_FILENAME);
1060
1061     // Get remote host name, if Apache hasn't done it for us
1062     if (empty($_SERVER['REMOTE_HOST'])
1063         and !empty($_SERVER['REMOTE_ADDR'])
1064             and ENABLE_REVERSE_DNS
1065     )
1066         $_SERVER['REMOTE_HOST'] = gethostbyaddr($_SERVER['REMOTE_ADDR']);
1067
1068 }
1069
1070 /*
1071  * We have to handle three languages:
1072  * 1) English, the language of the source code
1073  * 2) The language in which the wiki was created (DEFAULT_LANGUAGE)
1074  * 3) The language selected by the user, in which the wiki is displayed (LANG)
1075  *
1076  * The function "_" translates from English to LANG.
1077  * The function "__" translates from English to DEFAULT_LANGUAGE.
1078  */
1079
1080 function __($text)
1081 {
1082     return $text;
1083 }
1084
1085 // Local Variables:
1086 // mode: php
1087 // tab-width: 8
1088 // c-basic-offset: 4
1089 // c-hanging-comment-ender-p: nil
1090 // indent-tabs-mode: nil
1091 // End: