xref: /dokuwiki/inc/confutils.php (revision a27c9d6f6c03a5b93871dd414021317c2bb9eac8)
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    // remove BOM
147    if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf))
148        $lines[0] = substr($lines[0],3);
149    foreach ( $lines as $line ) {
150        //ignore comments (except escaped ones)
151        $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
152        $line = str_replace('\\#','#',$line);
153        $line = trim($line);
154        if(empty($line)) continue;
155        $line = preg_split('/\s+/',$line,2);
156        // Build the associative array
157        if($lower){
158            $conf[strtolower($line[0])] = $line[1];
159        }else{
160            $conf[$line[0]] = $line[1];
161        }
162    }
163
164    return $conf;
165}
166
167/**
168 * Builds a hash from a configfile
169 *
170 * If $lower is set to true all hash keys are converted to
171 * lower case.
172 *
173 * @author Harry Fuecks <hfuecks@gmail.com>
174 * @author Andreas Gohr <andi@splitbrain.org>
175 * @author Gina Haeussge <gina@foosel.net>
176 */
177function confToHash($file,$lower=false) {
178    $conf = array();
179    $lines = @file( $file );
180    if ( !$lines ) return $conf;
181
182    return linesToHash($lines, $lower);
183}
184
185/**
186 * Retrieve the requested configuration information
187 *
188 * @author Chris Smith <chris@jalakai.co.uk>
189 *
190 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
191 * @param  callback $fn       the function used to process the configuration file into an array
192 * @param  array    $params   optional additional params to pass to the callback
193 * @return array    configuration values
194 */
195function retrieveConfig($type,$fn,$params=null) {
196    global $config_cascade;
197
198    if(!is_array($params)) $params = array();
199
200    $combined = array();
201    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
202    foreach (array('default','local','protected') as $config_group) {
203        if (empty($config_cascade[$type][$config_group])) continue;
204        foreach ($config_cascade[$type][$config_group] as $file) {
205            if (@file_exists($file)) {
206                $config = call_user_func_array($fn,array_merge(array($file),$params));
207                $combined = array_merge($combined, $config);
208            }
209        }
210    }
211
212    return $combined;
213}
214
215/**
216 * Include the requested configuration information
217 *
218 * @author Chris Smith <chris@jalakai.co.uk>
219 *
220 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
221 * @return array              list of files, default before local before protected
222 */
223function getConfigFiles($type) {
224    global $config_cascade;
225    $files = array();
226
227    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
228    foreach (array('default','local','protected') as $config_group) {
229        if (empty($config_cascade[$type][$config_group])) continue;
230        $files = array_merge($files, $config_cascade[$type][$config_group]);
231    }
232
233    return $files;
234}
235
236/**
237 * check if the given action was disabled in config
238 *
239 * @author Andreas Gohr <andi@splitbrain.org>
240 * @returns boolean true if enabled, false if disabled
241 */
242function actionOK($action){
243    static $disabled = null;
244    if(is_null($disabled) || defined('SIMPLE_TEST')){
245        global $conf;
246        /** @var auth_basic $auth */
247        global $auth;
248
249        // prepare disabled actions array and handle legacy options
250        $disabled = explode(',',$conf['disableactions']);
251        $disabled = array_map('trim',$disabled);
252        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
253            $disabled[] = 'register';
254        }
255        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
256            $disabled[] = 'resendpwd';
257        }
258        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
259            $disabled[] = 'subscribe';
260        }
261        if (is_null($auth) || !$auth->canDo('Profile')) {
262            $disabled[] = 'profile';
263        }
264        if (is_null($auth) || !$auth->canDo('delUser')) {
265            $disabled[] = 'profile_delete';
266        }
267        if (is_null($auth)) {
268            $disabled[] = 'login';
269        }
270        if (is_null($auth) || !$auth->canDo('logout')) {
271            $disabled[] = 'logout';
272        }
273        $disabled = array_unique($disabled);
274    }
275
276    return !in_array($action,$disabled);
277}
278
279/**
280 * check if headings should be used as link text for the specified link type
281 *
282 * @author Chris Smith <chris@jalakai.co.uk>
283 *
284 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
285 *                                                      navigation applies to all other links
286 * @return  boolean             true if headings should be used for $linktype, false otherwise
287 */
288function useHeading($linktype) {
289    static $useHeading = null;
290
291    if (is_null($useHeading)) {
292        global $conf;
293
294        if (!empty($conf['useheading'])) {
295            switch ($conf['useheading']) {
296                case 'content':
297                    $useHeading['content'] = true;
298                    break;
299
300                case 'navigation':
301                    $useHeading['navigation'] = true;
302                    break;
303                default:
304                    $useHeading['content'] = true;
305                    $useHeading['navigation'] = true;
306            }
307        } else {
308            $useHeading = array();
309        }
310    }
311
312    return (!empty($useHeading[$linktype]));
313}
314
315/**
316 * obscure config data so information isn't plain text
317 *
318 * @param string       $str     data to be encoded
319 * @param string       $code    encoding method, values: plain, base64, uuencode.
320 * @return string               the encoded value
321 */
322function conf_encodeString($str,$code) {
323    switch ($code) {
324        case 'base64'   : return '<b>'.base64_encode($str);
325        case 'uuencode' : return '<u>'.convert_uuencode($str);
326        case 'plain':
327        default:
328                          return $str;
329    }
330}
331/**
332 * return obscured data as plain text
333 *
334 * @param  string      $str   encoded data
335 * @return string             plain text
336 */
337function conf_decodeString($str) {
338    switch (substr($str,0,3)) {
339        case '<b>' : return base64_decode(substr($str,3));
340        case '<u>' : return convert_uudecode(substr($str,3));
341        default:  // not encode (or unknown)
342                     return $str;
343    }
344}
345//Setup VIM: ex: et ts=4 :
346