]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/IniConfig.php
Connect the rest of PhpWiki to the IniConfig system. Also the keyword regular expres...
[SourceForge/phpwiki.git] / lib / IniConfig.php
1 <?php
2 rcs_id('$Id: IniConfig.php,v 1.3 2004-04-19 23:13:03 zorloc Exp $');
3
4 /**************************************************************************
5  * A configurator intended to read it's config from a PHP-style INI file,
6  * instead of a PHP file.
7  *
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.
13  *
14  * This file is part of PhpWiki.
15  *
16  * PhpWiki is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  * 
21  * PhpWiki is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * 
26  * You should have received a copy of the GNU General Public License
27  * along with PhpWiki; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  */
30
31 /** TODO
32  * - Convert the value lists to provide defaults, so that every "if
33  *      (defined())" and "if (!defined())" can fuck off to the dismal hole
34  *      it belongs in.
35  *
36  * - Resurrect the larger "config object" code (in config/) so it'll aid the
37  *      GUI config writers, and allow us to do proper validation and default
38  *      value handling.
39  *
40  * - Get rid of WikiNameRegexp and KeywordLinkRegexp as globals by finding
41  *      everywhere that uses them as variables and modify the code to use
42  *      them as constants.  Will involve hacking around
43  *      pcre_fix_posix_classes (probably with redefines()).
44  */
45  
46 // List of all valid config options to be define()d which take "values" (not
47 // booleans). Needs to be categorised, and generally made a lot tidier.
48 $_IC_VALID_VALUE = array('WIKI_NAME', 'ADMIN_USER', 'ADMIN_PASSWD',
49         'HTML_DUMP_SUFFIX', 'MAX_UPLOAD_SIZE', 'MINOR_EDIT_TIMEOUT',
50         'ACCESS_LOG', 'CACHE_CONTROL', 'CACHE_CONTROL_MAX_AGE',
51         'PASSWORD_LENGTH_MINIMUM', 'USER_AUTH_POLICY', 'LDAP_AUTH_HOST',
52         'LDAP_BASE_DN', 'LDAP_AUTH_USER', 'LDAP_AUTH_PASSWORD',
53         'LDAP_SEARCH_FIELD', 'IMAP_AUTH_HOST', 'POP3_AUTH_HOST',
54         'POP3_AUTH_PORT', 'AUTH_USER_FILE', 'AUTH_SESS_USER', 
55         'AUTH_SESS_LEVEL', 'GROUP_METHOD',
56         'AUTH_GROUP_FILE', 'EDITING_POLICY', 'THEME', 'CHARSET',
57         'DEFAULT_LANGUAGE', 'WIKI_PGSRC', 'DEFAULT_WIKI_PGSRC',
58         'ALLOWED_PROTOCOLS', 'INLINE_IMAGES', 'SUBPAGE_SEPARATOR',
59         'INTERWIKI_MAP_FILE', 'COPYRIGHTPAGE_TITLE', 'COPYRIGHTPAGE_URL',
60         'AUTHORPAGE_TITLE', 'AUTHORPAGE_URL', 'SERVER_NAME', 'SERVER_PORT',
61         'SCRIPT_NAME', 'DATA_PATH', 'PHPWIKI_DIR', 'VIRTUAL_PATH');
62
63 // List of all valid config options to be define()d which take booleans.
64 $_IC_VALID_BOOL = array('DEBUG', 'ENABLE_USER_NEW', 'JS_SEARCHREPLACE',
65         'ENABLE_REVERSE_DNS', 'ENCRYPTED_PASSWD', 'ZIPDUMP_AUTH', 
66         'ENABLE_RAW_HTML', 'STRICT_MAILABLE_PAGEDUMPS', 'COMPRESS_OUTPUT',
67         'WIKIDB_NOCACHE_MARKUP', 'ALLOW_ANON_USER', 'ALLOW_ANON_EDIT',
68         'ALLOW_BOGO_LOGIN', 'ALLOW_USER_PASSWORDS',
69         'AUTH_USER_FILE_STORABLE', 'ALLOW_HTTP_AUTH_LOGIN',
70         'ALLOW_USER_LOGIN', 'ALLOW_LDAP_LOGIN', 'ALLOW_IMAP_LOGIN',
71         'WARN_NONPUBLIC_INTERWIKIMAP', 'USE_PATH_INFO',
72         'DISABLE_HTTP_REDIRECT');
73
74 function IniConfig($file)
75 {
76         require_once('lib/pear/Config.php');
77         
78         $config = new Config();
79         $root = &$config->parseConfig($file, 'inicommented');
80         $out = $root->toArray();
81
82         $rs = &$out['root'];
83
84         global $_IC_VALID_VALUE, $_IC_VALID_BOOL;
85
86         foreach ($_IC_VALID_VALUE as $item)
87         {
88                 if (array_key_exists($item, $rs))
89                 {
90                         define($item, $rs[$item]);
91                 }
92         }
93
94         // Boolean options are slightly special - if they're set to any of
95         // 'false', '0', or 'no' (all case-insentitive) then the value will
96         // be a boolean false, otherwise if there is anything set it'll
97         // be true.
98         foreach ($_IC_VALID_BOOL as $item)
99         {
100                 if (array_key_exists($item, $rs))
101                 {
102                         $val = $rs[$item];
103                         if (!$val)
104                         {
105                                 define($item, false);
106                         }
107                         else if (strtolower($val) == 'false' ||
108                                 strtolower($val) == 'no' ||
109                                 $val == '0')
110                         {
111                                 define($item, false);
112                         }
113                         else
114                         {
115                                 define($item, true);
116                         }
117                 }
118         }
119
120         // Special handling for some config options
121         if ($val = @$rs['INCLUDE_PATH'])
122         {
123                 ini_set('include_path', $val);
124         }
125
126         if ($val = @$rs['SESSION_SAVE_PATH'])
127         {
128                 ini_set('session.save_path', $val);
129         }
130
131         // Database
132         global $DBParams;
133         $DBParams['dbtype'] = @$rs['DATABASE_TYPE'];
134         $DBParams['prefix'] = @$rs['DATABASE_PREFIX'];
135         $DBParams['dsn'] = @$rs['DATABASE_DSN'];
136         $DBParams['db_session_table'] = @$rs['DATABASE_SESSION_TABLE'];
137         $DBParams['dba_handler'] = @$rs['DATABASE_DBA_HANDLER'];
138         $DBParams['directory'] = @$rs['DATABASE_DIRECTORY'];
139         $DBParams['timeout'] = @$rs['DATABASE_TIMEOUT'];
140         if ($DBParams['dbtype'] == 'SQL' && $DBParams['db_session_table'])
141         {
142                 define('USE_DB_SESSION', true);
143         }
144
145         // Expiry stuff
146         global $ExpiryParams;
147
148         $ExpiryParams['major'] = array(
149                         'max_age' => @$rs['MAJOR_MAX_AGE'],
150                         'min_age' => @$rs['MAJOR_MIN_AGE'],
151                         'min_keep' => @$rs['MAJOR_MIN_KEEP'],
152                         'keep' => @$rs['MAJOR_KEEP'],
153                         'max_keep' => @$rs['MAJOR_MAX_KEEP']
154                                 );
155
156         $ExpiryParams['minor'] = array(
157                         'max_age' => @$rs['MINOR_MAX_AGE'],
158                         'min_age' => @$rs['MINOR_MIN_AGE'],
159                         'min_keep' => @$rs['MINOR_MIN_KEEP'],
160                         'keep' => @$rs['MINOR_KEEP'],
161                         'max_keep' => @$rs['MINOR_MAX_KEEP']
162                                 );
163
164         $ExpiryParams['author'] = array(
165                         'max_age' => @$rs['AUTHOR_MAX_AGE'],
166                         'min_age' => @$rs['AUTHOR_MIN_AGE'],
167                         'min_keep' => @$rs['AUTHOR_MIN_KEEP'],
168                         'keep' => @$rs['AUTHOR_KEEP'],
169                         'max_keep' => @$rs['AUTHOR_MAX_KEEP']
170                                 );
171
172         // User authentication
173         global $USER_AUTH_ORDER;
174         $USER_AUTH_ORDER = preg_split('/\s*:\s*/', @$rs['USER_AUTH_ORDER']);
175
176         // LDAP bind options
177         global $LDAP_SET_OPTION;
178         $optlist = preg_split('/\s*:\s*/', @$rs['LDAP_SET_OPTION']);
179         foreach ($optlist as $opt)
180         {
181                 $bits = preg_split('/\s*=\s*/', $opt, 2);
182
183                 if (count($bits) == 2)
184                 {
185                         $LDAP_SET_OPTION[$bits[0]] = $bits[1];
186                 }
187                 else
188                 {
189                         // Possibly throw some sort of error?
190                 }
191         }
192
193         // Now it's the external DB authentication stuff's turn
194         global $DBAuthParams;
195         $DBAP_MAP = array('DBAUTH_AUTH_DSN' => 'auth_dsn',
196                         'DBAUTH_AUTH_CHECK' => 'auth_check',
197                         'DBAUTH_AUTH_USER_EXISTS' => 'auth_user_exists',
198                         'DBAUTH_AUTH_CRYPT_METHOD' => 'auth_crypt_method',
199                         'DBAUTH_AUTH_UPDATE' => 'auth_update',
200                         'DBAUTH_AUTH_CREATE' => 'auth_create',
201                         'DBAUTH_PREF_SELECT' => 'pref_select',
202                         'DBAUTH_PREF_UPDATE' => 'pref_update',
203                         'DBAUTH_IS_MEMBER' => 'is_member',
204                         'DBAUTH_GROUP_MEMBERS' => 'group_members',
205                         'DBAUTH_USER_GROUPS' => 'user_groups'
206                 );
207
208         foreach ($DBAP_MAP as $rskey => $apkey)
209         {
210                 $val = @$rs[$rskey];
211                 if ($val)
212                 {
213                         $DBAuthParams[$apkey] = $val;
214                 }
215         }
216
217         // Default Wiki pages
218         global $GenericPages;
219         $GenericPages = preg_split('/\s*:\s*/', @$rs['DEFAULT_WIKI_PAGES']);
220
221         // Wiki name regexp.  Should be a define(), but too many places want
222         // to use it as a variable for me to be bothered changing them all.
223         // Major TODO item, there.
224         global $WikiNameRegexp;
225         $WikiNameRegexp = @$rs['WIKI_NAME_REGEXP'];
226
227         // Another "too-tricky" redefine
228         global $KeywordLinkRegexp;
229         $keywords = preg_split('/\s*:\s*/', @$rs['KEYWORDS']);
230         $KeywordLinkRegexp = '(?<=' . implode('|^', $keywords) . ')[[:upper:]].*$';
231         
232         global $DisabledActions;
233         $DisabledActions = preg_split('/\s*:\s*/', @$rs['DISABLED_ACTIONS']);
234
235 }