xref: /dokuwiki/inc/confutils.php (revision 24d494984899eca69df2a5e50d941007500ba545)
1<?php
2/**
3 * Utilities for collecting data from config files
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Harry Fuecks <hfuecks@gmail.com>
7 */
8
9
10/**
11 * Returns the (known) extension and mimetype of a given filename
12 *
13 * If $knownonly is true (the default), then only known extensions
14 * are returned.
15 *
16 * @author Andreas Gohr <andi@splitbrain.org>
17 */
18function mimetype($file, $knownonly=true){
19    $mtypes = getMimeTypes();     // known mimetypes
20    $ext    = strrpos($file, '.');
21    if ($ext === false) {
22        return array(false, false, false);
23    }
24    $ext = strtolower(substr($file, $ext + 1));
25    if (!isset($mtypes[$ext])){
26        if ($knownonly) {
27            return array(false, false, false);
28        } else {
29            return array($ext, 'application/octet-stream', true);
30        }
31    }
32    if($mtypes[$ext][0] == '!'){
33        return array($ext, substr($mtypes[$ext],1), true);
34    }else{
35        return array($ext, $mtypes[$ext], false);
36    }
37}
38
39/**
40 * returns a hash of mimetypes
41 *
42 * @author Andreas Gohr <andi@splitbrain.org>
43 */
44function getMimeTypes() {
45    static $mime = null;
46    if ( !$mime ) {
47        $mime = retrieveConfig('mime','confToHash');
48    }
49    return $mime;
50}
51
52/**
53 * returns a hash of acronyms
54 *
55 * @author Harry Fuecks <hfuecks@gmail.com>
56 */
57function getAcronyms() {
58    static $acronyms = null;
59    if ( !$acronyms ) {
60        $acronyms = retrieveConfig('acronyms','confToHash');
61    }
62    return $acronyms;
63}
64
65/**
66 * returns a hash of smileys
67 *
68 * @author Harry Fuecks <hfuecks@gmail.com>
69 */
70function getSmileys() {
71    static $smileys = null;
72    if ( !$smileys ) {
73        $smileys = retrieveConfig('smileys','confToHash');
74    }
75    return $smileys;
76}
77
78/**
79 * returns a hash of entities
80 *
81 * @author Harry Fuecks <hfuecks@gmail.com>
82 */
83function getEntities() {
84    static $entities = null;
85    if ( !$entities ) {
86        $entities = retrieveConfig('entities','confToHash');
87    }
88    return $entities;
89}
90
91/**
92 * returns a hash of interwikilinks
93 *
94 * @author Harry Fuecks <hfuecks@gmail.com>
95 */
96function getInterwiki() {
97    static $wikis = null;
98    if ( !$wikis ) {
99        $wikis = retrieveConfig('interwiki','confToHash',array(true));
100    }
101    //add sepecial case 'this'
102    $wikis['this'] = DOKU_URL.'{NAME}';
103    return $wikis;
104}
105
106/**
107 * returns array of wordblock patterns
108 *
109 */
110function getWordblocks() {
111    static $wordblocks = null;
112    if ( !$wordblocks ) {
113        $wordblocks = retrieveConfig('wordblock','file');
114    }
115    return $wordblocks;
116}
117
118/**
119 * Gets the list of configured schemes
120 *
121 * @return array the schemes
122 */
123function getSchemes() {
124    static $schemes = null;
125    if ( !$schemes ) {
126        $schemes = retrieveConfig('scheme','file');
127    }
128    $schemes = array_map('trim', $schemes);
129    $schemes = preg_replace('/^#.*/', '', $schemes);
130    $schemes = array_filter($schemes);
131    return $schemes;
132}
133
134/**
135 * Builds a hash from an array of lines
136 *
137 * If $lower is set to true all hash keys are converted to
138 * lower case.
139 *
140 * @author Harry Fuecks <hfuecks@gmail.com>
141 * @author Andreas Gohr <andi@splitbrain.org>
142 * @author Gina Haeussge <gina@foosel.net>
143 */
144function linesToHash($lines, $lower=false) {
145    $conf = array();
146    foreach ( $lines as $line ) {
147        //ignore comments (except escaped ones)
148        $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
149        $line = str_replace('\\#','#',$line);
150        $line = trim($line);
151        if(empty($line)) continue;
152        $line = preg_split('/\s+/',$line,2);
153        // Build the associative array
154        if($lower){
155            $conf[strtolower($line[0])] = $line[1];
156        }else{
157            $conf[$line[0]] = $line[1];
158        }
159    }
160
161    return $conf;
162}
163
164/**
165 * Builds a hash from a configfile
166 *
167 * If $lower is set to true all hash keys are converted to
168 * lower case.
169 *
170 * @author Harry Fuecks <hfuecks@gmail.com>
171 * @author Andreas Gohr <andi@splitbrain.org>
172 * @author Gina Haeussge <gina@foosel.net>
173 */
174function confToHash($file,$lower=false) {
175    $conf = array();
176    $lines = @file( $file );
177    if ( !$lines ) return $conf;
178
179    return linesToHash($lines, $lower);
180}
181
182/**
183 * Retrieve the requested configuration information
184 *
185 * @author Chris Smith <chris@jalakai.co.uk>
186 *
187 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
188 * @param  callback $fn       the function used to process the configuration file into an array
189 * @param  array    $params   optional additional params to pass to the callback
190 * @return array    configuration values
191 */
192function retrieveConfig($type,$fn,$params=null) {
193    global $config_cascade;
194
195    if(!is_array($params)) $params = array();
196
197    $combined = array();
198    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
199    foreach (array('default','local','protected') as $config_group) {
200        if (empty($config_cascade[$type][$config_group])) continue;
201        foreach ($config_cascade[$type][$config_group] as $file) {
202            if (@file_exists($file)) {
203                $config = call_user_func_array($fn,array_merge(array($file),$params));
204                $combined = array_merge($combined, $config);
205            }
206        }
207    }
208
209    return $combined;
210}
211
212/**
213 * Include the requested configuration information
214 *
215 * @author Chris Smith <chris@jalakai.co.uk>
216 *
217 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
218 * @return array              list of files, default before local before protected
219 */
220function getConfigFiles($type) {
221    global $config_cascade;
222    $files = array();
223
224    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
225    foreach (array('default','local','protected') as $config_group) {
226        if (empty($config_cascade[$type][$config_group])) continue;
227        $files = array_merge($files, $config_cascade[$type][$config_group]);
228    }
229
230    return $files;
231}
232
233/**
234 * check if the given action was disabled in config
235 *
236 * @author Andreas Gohr <andi@splitbrain.org>
237 * @returns boolean true if enabled, false if disabled
238 */
239function actionOK($action){
240    static $disabled = null;
241    if(is_null($disabled)){
242        global $conf;
243        /** @var auth_basic $auth */
244        global $auth;
245
246        // prepare disabled actions array and handle legacy options
247        $disabled = explode(',',$conf['disableactions']);
248        $disabled = array_map('trim',$disabled);
249        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
250            $disabled[] = 'register';
251        }
252        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
253            $disabled[] = 'resendpwd';
254        }
255        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
256            $disabled[] = 'subscribe';
257        }
258        if (is_null($auth) || !$auth->canDo('Profile')) {
259            $disabled[] = 'profile';
260        }
261        if (is_null($auth)) {
262            $disabled[] = 'login';
263        }
264        if (is_null($auth) || !$auth->canDo('logout')) {
265            $disabled[] = 'logout';
266        }
267        $disabled = array_unique($disabled);
268    }
269
270    return !in_array($action,$disabled);
271}
272
273/**
274 * check if headings should be used as link text for the specified link type
275 *
276 * @author Chris Smith <chris@jalakai.co.uk>
277 *
278 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
279 *                                                      navigation applies to all other links
280 * @return  boolean             true if headings should be used for $linktype, false otherwise
281 */
282function useHeading($linktype) {
283    static $useHeading = null;
284
285    if (is_null($useHeading)) {
286        global $conf;
287
288        if (!empty($conf['useheading'])) {
289            switch ($conf['useheading']) {
290                case 'content':
291                    $useHeading['content'] = true;
292                    break;
293
294                case 'navigation':
295                    $useHeading['navigation'] = true;
296                    break;
297                default:
298                    $useHeading['content'] = true;
299                    $useHeading['navigation'] = true;
300            }
301        } else {
302            $useHeading = array();
303        }
304    }
305
306    return (!empty($useHeading[$linktype]));
307}
308
309/**
310 * obscure config data so information isn't plain text
311 *
312 * @param string       $str     data to be encoded
313 * @param string       $code    encoding method, values: plain, base64, uuencode.
314 * @return string               the encoded value
315 */
316function conf_encodeString($str,$code) {
317    switch ($code) {
318        case 'base64'   : return '<b>'.base64_encode($str);
319        case 'uuencode' : return '<u>'.convert_uuencode($str);
320        case 'plain':
321        default:
322                          return $str;
323    }
324}
325/**
326 * return obscured data as plain text
327 *
328 * @param  string      $str   encoded data
329 * @return string             plain text
330 */
331function conf_decodeString($str) {
332    switch (substr($str,0,3)) {
333        case '<b>' : return base64_decode(substr($str,3));
334        case '<u>' : return convert_uudecode(substr($str,3));
335        default:  // not encode (or unknown)
336                     return $str;
337    }
338}
339//Setup VIM: ex: et ts=4 :
340