2 rcs_id('$Id: IniConfig.php,v 1.15 2004-05-01 15:59:29 rurban Exp $');
5 * A configurator intended to read it's config from a PHP-style INI file,
6 * instead of a PHP file.
8 * Pass a filename to the IniConfig() function and it will read all it's
9 * definitions from there, all by itself, and proceed to do a mass-define
10 * of all valid PHPWiki config items. In this way, we can hopefully be
11 * totally backwards-compatible with the old index.php method, while still
12 * providing a much tastier on-going experience.
14 * @author: Joby Walker, Reini Urban, Matthew Palmer
17 * Copyright 2004 $ThePhpWikiProgrammingTeam
19 * This file is part of PhpWiki.
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.
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.
31 * You should have received a copy of the GNU General Public License
32 * along with PhpWiki; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 * - Convert the value lists to provide defaults, so that every "if
38 * (defined())" and "if (!defined())" can fuck off to the dismal hole
41 * - Resurrect the larger "config object" code (in config/) so it'll aid the
42 * GUI config writers, and allow us to do proper validation and default
45 * - Get rid of WikiNameRegexp and KeywordLinkRegexp as globals by finding
46 * everywhere that uses them as variables and modify the code to use
47 * them as constants. Will involve hacking around
48 * pcre_fix_posix_classes (probably with redefines()).
51 include_once (dirname(__FILE__)."/config.php");
52 include_once (dirname(__FILE__)."/FileFinder.php");
54 function IniConfig($file) {
56 //FindFile("pear/Config.php");
57 //require_once("Config.php");
59 // List of all valid config options to be define()d which take "values" (not
60 // booleans). Needs to be categorised, and generally made a lot tidier.
61 $_IC_VALID_VALUE = array
62 ('DEBUG', 'WIKI_NAME', 'ADMIN_USER', 'ADMIN_PASSWD',
63 'HTML_DUMP_SUFFIX', 'MAX_UPLOAD_SIZE', 'MINOR_EDIT_TIMEOUT',
64 'ACCESS_LOG', 'CACHE_CONTROL', 'CACHE_CONTROL_MAX_AGE',
65 'PASSWORD_LENGTH_MINIMUM', 'USER_AUTH_POLICY', 'LDAP_AUTH_HOST',
66 'LDAP_BASE_DN', 'LDAP_AUTH_USER', 'LDAP_AUTH_PASSWORD',
67 'LDAP_SEARCH_FIELD', 'IMAP_AUTH_HOST', 'POP3_AUTH_HOST',
68 'AUTH_USER_FILE', 'AUTH_SESS_USER', 'AUTH_SESS_LEVEL', 'GROUP_METHOD',
69 'AUTH_GROUP_FILE', 'EDITING_POLICY', 'THEME', 'CHARSET',
70 'DEFAULT_LANGUAGE', 'WIKI_PGSRC', 'DEFAULT_WIKI_PGSRC',
71 'ALLOWED_PROTOCOLS', 'INLINE_IMAGES', 'SUBPAGE_SEPARATOR',
72 'INTERWIKI_MAP_FILE', 'COPYRIGHTPAGE_TITLE', 'COPYRIGHTPAGE_URL',
73 'AUTHORPAGE_TITLE', 'AUTHORPAGE_URL', 'SERVER_NAME', 'SERVER_PORT',
74 'SCRIPT_NAME', 'DATA_PATH', 'PHPWIKI_DIR', 'VIRTUAL_PATH');
76 // List of all valid config options to be define()d which take booleans.
77 $_IC_VALID_BOOL = array
78 ('ENABLE_USER_NEW', 'ENABLE_EDIT_TOOLBAR', 'JS_SEARCHREPLACE',
79 'ENABLE_REVERSE_DNS', 'ENCRYPTED_PASSWD', 'ZIPDUMP_AUTH',
80 'ENABLE_RAW_HTML', 'STRICT_MAILABLE_PAGEDUMPS', 'COMPRESS_OUTPUT',
81 'WIKIDB_NOCACHE_MARKUP', 'ALLOW_ANON_USER', 'ALLOW_ANON_EDIT',
82 'ALLOW_BOGO_LOGIN', 'ALLOW_USER_PASSWORDS',
83 'AUTH_USER_FILE_STORABLE', 'ALLOW_HTTP_AUTH_LOGIN',
84 'ALLOW_USER_LOGIN', 'ALLOW_LDAP_LOGIN', 'ALLOW_IMAP_LOGIN',
85 'WARN_NONPUBLIC_INTERWIKIMAP', 'USE_PATH_INFO',
86 'DISABLE_HTTP_REDIRECT');
88 if(!file_exists($file)){
89 trigger_error("Datasource file '$file' does not exist", E_USER_ERROR);
93 $rs = @parse_ini_file($file);
94 $rsdef = @parse_ini_file(dirname(__FILE__)."/../config/config-default.ini");
96 foreach ($_IC_VALID_VALUE as $item) {
97 if (defined($item)) continue;
98 if (array_key_exists($item, $rs)) {
99 define($item, $rs[$item]);
100 } elseif (array_key_exists($item, $rsdef)) {
101 define($item, $rsdef[$item]);
102 // calculate them later:
103 } elseif (in_array($item,array('DATABASE_PREFIX', 'SERVER_NAME', 'SERVER_PORT',
104 'SCRIPT_NAME', 'DATA_PATH', 'PHPWIKI_DIR', 'VIRTUAL_PATH'))) {
107 trigger_error(sprintf("missing config setting for %s",$item));
111 // Boolean options are slightly special - if they're set to any of
112 // 'false', '0', or 'no' (all case-insensitive) then the value will
113 // be a boolean false, otherwise if there is anything set it'll
115 foreach ($_IC_VALID_BOOL as $item) {
116 if (defined($item)) continue;
117 if (array_key_exists($item, $rs)) {
119 } elseif (array_key_exists($item, $rsdef)) {
120 $val = $rsdef[$item];
122 ; //trigger_error(sprintf("missing boolean config setting for %s",$item));
125 // calculate them later: old or dynamic constants
126 if (!$val and in_array($item,array('USE_PATH_INFO','USE_DB_SESSION',
127 'ALLOW_HTTP_AUTH_LOGIN','ALLOW_LDAP_LOGIN',
128 'ALLOW_IMAP_LOGIN','ALLOW_USER_LOGIN',
129 'REQUIRE_SIGNIN_BEFORE_EDIT',
130 'WIKIDB_NOCACHE_MARKUP')))
135 define($item, false);
137 elseif (strtolower($val) == 'false' ||
138 strtolower($val) == 'no' ||
140 define($item, false);
147 // Special handling for some config options
148 if ($val = @$rs['INCLUDE_PATH']) {
149 ini_set('include_path', $val);
152 if ($val = @$rs['SESSION_SAVE_PATH']) {
153 ini_set('session.save_path', $val);
158 $DBParams['dbtype'] = @$rs['DATABASE_TYPE'];
159 if (isset($rs['DATABASE_DSN']))
160 $DBParams['dsn'] = $rs['DATABASE_DSN'];
161 if (isset($rs['DATABASE_PREFIX']))
162 $DBParams['prefix'] = $rs['DATABASE_PREFIX'];
163 $DBParams['db_session_table'] = @$rs['DATABASE_SESSION_TABLE'];
164 $DBParams['dba_handler'] = @$rs['DATABASE_DBA_HANDLER'];
165 $DBParams['directory'] = @$rs['DATABASE_DIRECTORY'];
166 $DBParams['timeout'] = @$rs['DATABASE_TIMEOUT'];
167 if (!defined('USE_DB_SESSION') and $DBParams['db_session_table'] and
168 in_array($DBParams['dbtype'],array('SQL','ADODB','dba'))) {
169 define('USE_DB_SESSION', true);
173 global $ExpireParams;
175 $ExpireParams['major'] = array(
176 'max_age' => @$rs['MAJOR_MAX_AGE'],
177 'min_age' => @$rs['MAJOR_MIN_AGE'],
178 'min_keep' => @$rs['MAJOR_MIN_KEEP'],
179 'keep' => @$rs['MAJOR_KEEP'],
180 'max_keep' => @$rs['MAJOR_MAX_KEEP']
182 $ExpireParams['minor'] = array(
183 'max_age' => @$rs['MINOR_MAX_AGE'],
184 'min_age' => @$rs['MINOR_MIN_AGE'],
185 'min_keep' => @$rs['MINOR_MIN_KEEP'],
186 'keep' => @$rs['MINOR_KEEP'],
187 'max_keep' => @$rs['MINOR_MAX_KEEP']
189 $ExpireParams['author'] = array(
190 'max_age' => @$rs['AUTHOR_MAX_AGE'],
191 'min_age' => @$rs['AUTHOR_MIN_AGE'],
192 'min_keep' => @$rs['AUTHOR_MIN_KEEP'],
193 'keep' => @$rs['AUTHOR_KEEP'],
194 'max_keep' => @$rs['AUTHOR_MAX_KEEP']
197 // User authentication
198 global $USER_AUTH_ORDER;
199 $USER_AUTH_ORDER = preg_split('/\s*:\s*/', @$rs['USER_AUTH_ORDER']);
202 global $LDAP_SET_OPTION;
203 $optlist = preg_split('/\s*:\s*/', @$rs['LDAP_SET_OPTION']);
204 foreach ($optlist as $opt) {
205 $bits = preg_split('/\s*=\s*/', $opt, 2);
206 if (count($bits) == 2) {
207 $LDAP_SET_OPTION[$bits[0]] = $bits[1];
210 // Possibly throw some sort of error?
214 // Now it's the external DB authentication stuff's turn
215 if (in_array('Db', $USER_AUTH_ORDER) && empty($rs['DBAUTH_AUTH_DSN'])) {
216 $rs['DBAUTH_AUTH_DSN'] = $DBParams['dsn'];
219 global $DBAuthParams;
220 $DBAP_MAP = array('DBAUTH_AUTH_DSN' => 'auth_dsn',
221 'DBAUTH_AUTH_CHECK' => 'auth_check',
222 'DBAUTH_AUTH_USER_EXISTS' => 'auth_user_exists',
223 'DBAUTH_AUTH_CRYPT_METHOD' => 'auth_crypt_method',
224 'DBAUTH_AUTH_UPDATE' => 'auth_update',
225 'DBAUTH_AUTH_CREATE' => 'auth_create',
226 'DBAUTH_PREF_SELECT' => 'pref_select',
227 'DBAUTH_PREF_UPDATE' => 'pref_update',
228 'DBAUTH_IS_MEMBER' => 'is_member',
229 'DBAUTH_GROUP_MEMBERS' => 'group_members',
230 'DBAUTH_USER_GROUPS' => 'user_groups'
233 foreach ($DBAP_MAP as $rskey => $apkey) {
234 if (isset($rs[$rskey])) {
235 $DBAuthParams[$apkey] = $rs[$rskey];
239 // Default Wiki pages
240 global $GenericPages;
241 if (!isset($rs['DEFAULT_WIKI_PAGES']))
242 $rs['DEFAULT_WIKI_PAGES'] = "ReleaseNotes:SteveWainstead:TestPage";
243 $GenericPages = preg_split('/\s*:\s*/', @$rs['DEFAULT_WIKI_PAGES']);
245 // Wiki name regexp. Should be a define(), but too many places want
246 // to use it as a variable for me to be bothered changing them all.
247 // Major TODO item, there.
248 global $WikiNameRegexp;
249 $WikiNameRegexp = @$rs['WIKI_NAME_REGEXP'];
251 // Another "too-tricky" redefine
252 global $KeywordLinkRegexp;
253 if (!isset($rs['KEYWORDS']))
254 $rs['KEYWORDS'] = "Category:Topic";
255 $keywords = preg_split('/\s*:\s*/', $rs['KEYWORDS']);
256 $KeywordLinkRegexp = '(?<=' . implode('|^', $keywords) . ')[[:upper:]].*$';
258 global $DisabledActions;
259 $DisabledActions = preg_split('/\s*:\s*/', @$rs['DISABLED_ACTIONS']);
264 // moved from lib/config.php
265 function fix_configs() {
266 global $FieldSeparator, $charset, $WikiNameRegexp, $KeywordLinkRegexp;
267 global $DisabledActions, $HTTP_SERVER_VARS, $DBParams, $LANG;
269 // "\x80"-"\x9f" (and "\x00" - "\x1f") are non-printing control
270 // chars in iso-8859-*
271 // $FieldSeparator = "\263"; // this is a superscript 3 in ISO-8859-1.
272 // $FieldSeparator = "\xFF"; // this byte should never appear in utf-8
273 // FIXME: get rid of constant. pref is dynamic and language specific
275 if (isset($LANG) and in_array($LANG,array('ja','zh')))
277 if (strtolower($charset) == 'utf-8')
278 $FieldSeparator = "\xFF";
280 $FieldSeparator = "\x81";
282 if (!defined('DEFAULT_LANGUAGE'))
283 define('DEFAULT_LANGUAGE', 'en');
284 update_locale(isset($LANG) ? $LANG : DEFAULT_LANGUAGE);
286 // Set up (possibly fake) gettext()
288 if (!function_exists ('bindtextdomain')) {
291 function gettext ($text) {
293 if (!empty ($locale[$text]))
294 return $locale[$text];
299 return gettext($text);
303 // Working around really weird gettext problems: (4.3.2, 4.3.6 win)
304 // bindtextdomain() returns the current domain path.
305 // 1. If the script is not index.php but something like "de", on a different path
306 // then bindtextdomain() fails, but after chdir to the correct path it will work okay.
307 // 2. But the weird error "Undefined variable: bindtextdomain" is generated then.
308 $bindtextdomain_path = FindFile("locale", false, true);
310 $bindtextdomain_path = str_replace("/","\\",$bindtextdomain_path);
311 $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain);
312 if ($bindtextdomain_real != $bindtextdomain_path) {
313 // this will happen with virtual_paths. chdir and try again.
314 chdir($bindtextdomain_path);
315 $bindtextdomain_real = @bindtextdomain("phpwiki", $bindtextdomain);
317 textdomain("phpwiki");
318 if ($bindtextdomain_real != $bindtextdomain_path) { // change back
319 chdir($bindtextdomain_real . (isWindows() ? "\\.." : "/.."));
323 $WikiNameRegexp = pcre_fix_posix_classes($WikiNameRegexp);
324 $KeywordLinkRegexp = pcre_fix_posix_classes($KeywordLinkRegexp);
326 //////////////////////////////////////////////////////////////////
327 // Autodetect URL settings:
329 if (!defined('SERVER_NAME')) define('SERVER_NAME', $HTTP_SERVER_VARS['SERVER_NAME']);
330 if (!defined('SERVER_PORT')) define('SERVER_PORT', $HTTP_SERVER_VARS['SERVER_PORT']);
331 if (!defined('SERVER_PROTOCOL')) {
332 if (empty($HTTP_SERVER_VARS['HTTPS']) || $HTTP_SERVER_VARS['HTTPS'] == 'off')
333 define('SERVER_PROTOCOL', 'http');
335 define('SERVER_PROTOCOL', 'https');
338 if (!defined('SCRIPT_NAME'))
339 define('SCRIPT_NAME', deduce_script_name());
341 if (!defined('USE_PATH_INFO')) {
343 * If SCRIPT_NAME does not look like php source file,
344 * or user cgi we assume that php is getting run by an
345 * action handler in /cgi-bin. In this case,
346 * I think there is no way to get Apache to pass
347 * useful PATH_INFO to the php script (PATH_INFO
348 * is used to the the php interpreter where the
351 switch (php_sapi_name()) {
353 case 'apache2handler':
354 define('USE_PATH_INFO', true);
357 case 'apache2filter':
358 define('USE_PATH_INFO', false);
361 define('USE_PATH_INFO', ereg('\.(php3?|cgi)$', SCRIPT_NAME));
366 // If user has not defined DATA_PATH, we want to use relative URLs.
367 if (!defined('DATA_PATH') && USE_PATH_INFO)
368 define('DATA_PATH', '..');
370 // If user has not defined PHPWIKI_DIR, and we need it
371 if (!defined('PHPWIKI_DIR') and !file_exists("themes/default")) {
372 $themes_dir = FindFile("themes");
373 define('PHPWIKI_DIR', dirname($themes_dir));
376 if (!defined('VIRTUAL_PATH')) {
377 // We'd like to auto-detect when the cases where apaches
378 // 'Action' directive (or similar means) is used to
379 // redirect page requests to a cgi-handler.
381 // In cases like this, requests for e.g. /wiki/HomePage
382 // get redirected to a cgi-script called, say,
383 // /path/to/wiki/index.php. The script gets all
384 // of /wiki/HomePage as it's PATH_INFO.
387 // How to detect when this has happened reliably?
388 // How to pick out the "virtual path" (in this case '/wiki')?
390 // (Another time an redirect might occur is to a DirectoryIndex
391 // -- the requested URI is '/wikidir/', the request gets
392 // passed to '/wikidir/index.php'. In this case, the
393 // proper VIRTUAL_PATH is '/wikidir/index.php', since the
394 // pages will appear at e.g. '/wikidir/index.php/HomePage'.
397 $REDIRECT_URL = &$HTTP_SERVER_VARS['REDIRECT_URL'];
398 if (USE_PATH_INFO and isset($REDIRECT_URL)
399 and ! IsProbablyRedirectToIndex()) {
400 // FIXME: This is a hack, and won't work if the requested
401 // pagename has a slash in it.
402 define('VIRTUAL_PATH', dirname($REDIRECT_URL . 'x'));
404 define('VIRTUAL_PATH', SCRIPT_NAME);
409 && SERVER_PORT != (SERVER_PROTOCOL == 'https' ? 443 : 80)) {
411 SERVER_PROTOCOL . '://' . SERVER_NAME . ':' . SERVER_PORT);
415 SERVER_PROTOCOL . '://' . SERVER_NAME);
418 if (VIRTUAL_PATH != SCRIPT_NAME) {
419 // Apache action handlers are used.
420 define('PATH_INFO_PREFIX', VIRTUAL_PATH . '/');
423 define('PATH_INFO_PREFIX', '/');
426 define('PHPWIKI_BASE_URL',
427 SERVER_URL . (USE_PATH_INFO ? VIRTUAL_PATH . '/' : SCRIPT_NAME));
429 //////////////////////////////////////////////////////////////////
432 if (empty($DBParams['dbtype']))
433 $DBParams['dbtype'] = 'dba';
435 if (!defined('THEME'))
436 define('THEME', 'default');
438 if (!defined('WIKI_NAME'))
439 define('WIKI_NAME', _("An unnamed PhpWiki"));
441 if (!defined('HOME_PAGE'))
442 define('HOME_PAGE', _("HomePage"));
446 if (!defined('ACCESS_LOG'))
447 define('ACCESS_LOG', '');
450 // Get remote host name, if apache hasn't done it for us
451 if (empty($HTTP_SERVER_VARS['REMOTE_HOST']) && ENABLE_REVERSE_DNS)
452 $HTTP_SERVER_VARS['REMOTE_HOST'] = gethostbyaddr($HTTP_SERVER_VARS['REMOTE_ADDR']);
454 // check whether the crypt() function is needed and present
455 if (defined('ENCRYPTED_PASSWD') && !function_exists('crypt')) {
456 $error = sprintf(_("Encrypted passwords cannot be used: %s."),
457 "'function crypt()' not available in this version of php");
458 trigger_error($error);
461 if (!defined('ADMIN_PASSWD') or ADMIN_PASSWD == '')
462 trigger_error(_("The admin password cannot be empty. Please update your /index.php"));
464 if (defined('USE_DB_SESSION') and USE_DB_SESSION) {
465 if (! $DBParams['db_session_table'] ) {
466 trigger_error(_("Empty db_session_table. Turn USE_DB_SESSION off or define the table name."),
468 // this is flawed. constants cannot be changed.
469 define('USE_DB_SESSION',false);
470 $DBParams['db_session_table'] = @$DBParams['prefix'] . 'session';
473 // default: true (since v1.3.8)
474 if (!defined('USE_DB_SESSION'))
475 define('USE_DB_SESSION',true);
478 if (!defined('ENABLE_USER_NEW')) define('ENABLE_USER_NEW',true);
479 if (!defined('ALLOW_USER_LOGIN'))
480 define('ALLOW_USER_LOGIN', defined('ALLOW_USER_PASSWORDS') && ALLOW_USER_PASSWORDS);
481 if (!defined('ALLOW_ANON_USER')) define('ALLOW_ANON_USER', true);
482 if (!defined('ALLOW_ANON_EDIT')) define('ALLOW_ANON_EDIT', false);
483 if (!defined('REQUIRE_SIGNIN_BEFORE_EDIT')) define('REQUIRE_SIGNIN_BEFORE_EDIT', ! ALLOW_ANON_EDIT);
484 if (!defined('ALLOW_BOGO_LOGIN')) define('ALLOW_BOGO_LOGIN', true);
486 if (ALLOW_USER_LOGIN and !empty($DBAuthParams) and empty($DBAuthParams['auth_dsn'])) {
487 if (isset($DBParams['dsn']))
488 $DBAuthParams['auth_dsn'] = $DBParams['dsn'];
492 // $Log: not supported by cvs2svn $
493 // Revision 1.14 2004/04/29 23:25:12 rurban
494 // re-ordered locale init (as in 1.3.9)
495 // fixed loadfile with subpages, and merge/restore anyway
496 // (sf.net bug #844188)
498 // Revision 1.13 2004/04/29 21:54:05 rurban
501 // Revision 1.12 2004/04/27 16:16:27 rurban
502 // more subtle config problems with defaults
504 // Revision 1.11 2004/04/26 20:44:34 rurban
505 // locking table specific for better databases
507 // Revision 1.10 2004/04/26 13:22:32 rurban
508 // calculate bool old or dynamic constants later
510 // Revision 1.9 2004/04/26 12:15:01 rurban
511 // check default config values
513 // Revision 1.8 2004/04/23 16:55:59 zorloc
514 // If using Db auth and DBAUTH_AUTH_DSN is empty set DBAUTH_AUTH_DSN to $DBParams['dsn']
516 // Revision 1.7 2004/04/20 22:26:27 zorloc
517 // Removed Pear_Config for parse_ini_file().
519 // Revision 1.6 2004/04/20 18:10:27 rurban
520 // config refactoring:
521 // FileFinder is needed for WikiFarm scripts calling index.php
522 // config run-time calls moved to lib/IniConfig.php:fix_configs()
523 // added PHPWIKI_DIR smart-detection code (Theme finder)
524 // moved FileFind to lib/FileFinder.php
525 // cleaned lib/config.php
527 // Revision 1.5 2004/04/20 17:21:57 rurban
528 // WikiFarm code: honor predefined constants
530 // Revision 1.4 2004/04/20 17:08:19 rurban
531 // Some IniConfig fixes: prepend our private lib/pear dir
532 // switch from " to ' in the auth statements
533 // use error handling.
534 // WikiUserNew changes for the new "'$variable'" syntax
536 // TODO: optimization to put config vars into the session.
539 // (c-file-style: "gnu")
544 // c-hanging-comment-ender-p: nil
545 // indent-tabs-mode: nil