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