xref: /dokuwiki/inc/confutils.php (revision 10b38f10e6ce99d01c6ca30bcd9992699d6ee049)
1b625487dSandi<?php
2b625487dSandi/**
3b625487dSandi * Utilities for collecting data from config files
4b625487dSandi *
5b625487dSandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6b625487dSandi * @author     Harry Fuecks <hfuecks@gmail.com>
7b625487dSandi */
8b625487dSandi
9*10b38f10SChristopher Smith/*
10*10b38f10SChristopher Smith * line prefix used to negate single value config items
11*10b38f10SChristopher Smith * (scheme.conf & stopwords.conf), e.g.
12*10b38f10SChristopher Smith * !gopher
13*10b38f10SChristopher Smith */
14*10b38f10SChristopher Smithconst DOKU_CONF_NEGATION = '!';
15b625487dSandi
16b625487dSandi/**
17b625487dSandi * Returns the (known) extension and mimetype of a given filename
18b625487dSandi *
1927bf7924STom N Harris * If $knownonly is true (the default), then only known extensions
2027bf7924STom N Harris * are returned.
2127bf7924STom N Harris *
22b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
2342ea7f44SGerrit Uitslag *
2442ea7f44SGerrit Uitslag * @param string $file file name
2542ea7f44SGerrit Uitslag * @param bool   $knownonly
2642ea7f44SGerrit Uitslag * @return array with extension, mimetype and if it should be downloaded
27b625487dSandi */
2827bf7924STom N Harrisfunction mimetype($file, $knownonly=true){
29b625487dSandi    $mtypes = getMimeTypes();     // known mimetypes
30ad74fe66SAdrian Lang    $ext    = strrpos($file, '.');
31ad74fe66SAdrian Lang    if ($ext === false) {
32ad74fe66SAdrian Lang        return array(false, false, false);
3327bf7924STom N Harris    }
34ad74fe66SAdrian Lang    $ext = strtolower(substr($file, $ext + 1));
35ad74fe66SAdrian Lang    if (!isset($mtypes[$ext])){
36ad74fe66SAdrian Lang        if ($knownonly) {
37ad74fe66SAdrian Lang            return array(false, false, false);
38ecebf3a8SAndreas Gohr        } else {
39ad74fe66SAdrian Lang            return array($ext, 'application/octet-stream', true);
4027bf7924STom N Harris        }
41b625487dSandi    }
42ad74fe66SAdrian Lang    if($mtypes[$ext][0] == '!'){
43ad74fe66SAdrian Lang        return array($ext, substr($mtypes[$ext],1), true);
44ad74fe66SAdrian Lang    }else{
45ad74fe66SAdrian Lang        return array($ext, $mtypes[$ext], false);
46ad74fe66SAdrian Lang    }
47b625487dSandi}
48b625487dSandi
49b625487dSandi/**
50b625487dSandi * returns a hash of mimetypes
51b625487dSandi *
52b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
53b625487dSandi */
54b625487dSandifunction getMimeTypes() {
5549eb6e38SAndreas Gohr    static $mime = null;
56b625487dSandi    if ( !$mime ) {
57cb043f52SChris Smith        $mime = retrieveConfig('mime','confToHash');
5845ae4bb8SChristopher Smith        $mime = array_filter($mime);
59b625487dSandi    }
60b625487dSandi    return $mime;
61b625487dSandi}
62b625487dSandi
63b625487dSandi/**
64b625487dSandi * returns a hash of acronyms
65b625487dSandi *
66b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
67b625487dSandi */
68b625487dSandifunction getAcronyms() {
6949eb6e38SAndreas Gohr    static $acronyms = null;
70b625487dSandi    if ( !$acronyms ) {
71cb043f52SChris Smith        $acronyms = retrieveConfig('acronyms','confToHash');
72f266a919SChristopher Smith        $acronyms = array_filter($acronyms, 'strlen');
73b625487dSandi    }
74b625487dSandi    return $acronyms;
75b625487dSandi}
76b625487dSandi
77b625487dSandi/**
78b625487dSandi * returns a hash of smileys
79b625487dSandi *
80b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
81b625487dSandi */
82b625487dSandifunction getSmileys() {
8349eb6e38SAndreas Gohr    static $smileys = null;
84b625487dSandi    if ( !$smileys ) {
85cb043f52SChris Smith        $smileys = retrieveConfig('smileys','confToHash');
86f266a919SChristopher Smith        $smileys = array_filter($smileys, 'strlen');
87b625487dSandi    }
88b625487dSandi    return $smileys;
89b625487dSandi}
90b625487dSandi
91b625487dSandi/**
92b625487dSandi * returns a hash of entities
93b625487dSandi *
94b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
95b625487dSandi */
96b625487dSandifunction getEntities() {
9749eb6e38SAndreas Gohr    static $entities = null;
98b625487dSandi    if ( !$entities ) {
99cb043f52SChris Smith        $entities = retrieveConfig('entities','confToHash');
100f266a919SChristopher Smith        $entities = array_filter($entities, 'strlen');
101b625487dSandi    }
102b625487dSandi    return $entities;
103b625487dSandi}
104b625487dSandi
105b625487dSandi/**
106b625487dSandi * returns a hash of interwikilinks
107b625487dSandi *
108b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
109b625487dSandi */
110b625487dSandifunction getInterwiki() {
11149eb6e38SAndreas Gohr    static $wikis = null;
112b625487dSandi    if ( !$wikis ) {
1134c6a5eccSAndreas Gohr        $wikis = retrieveConfig('interwiki','confToHash',array(true));
114f266a919SChristopher Smith        $wikis = array_filter($wikis, 'strlen');
11545ae4bb8SChristopher Smith
11697a3e4e3Sandi        //add sepecial case 'this'
11727a2b085Sandi        $wikis['this'] = DOKU_URL.'{NAME}';
11845ae4bb8SChristopher Smith    }
119b625487dSandi    return $wikis;
120b625487dSandi}
121b625487dSandi
122b625487dSandi/**
123b9ac8716Schris * returns array of wordblock patterns
124b9ac8716Schris *
125b9ac8716Schris */
126b9ac8716Schrisfunction getWordblocks() {
12749eb6e38SAndreas Gohr    static $wordblocks = null;
128b9ac8716Schris    if ( !$wordblocks ) {
1294c353447SChristopher Smith        $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal');
130b9ac8716Schris    }
131b9ac8716Schris    return $wordblocks;
132b9ac8716Schris}
133b9ac8716Schris
134e3ab6fc5SMichael Hamann/**
135e3ab6fc5SMichael Hamann * Gets the list of configured schemes
136e3ab6fc5SMichael Hamann *
137e3ab6fc5SMichael Hamann * @return array the schemes
138e3ab6fc5SMichael Hamann */
13936f2d7c1SGina Haeussgefunction getSchemes() {
14049eb6e38SAndreas Gohr    static $schemes = null;
14136f2d7c1SGina Haeussge    if ( !$schemes ) {
1424c353447SChristopher Smith        $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal');
14336f2d7c1SGina Haeussge        $schemes = array_map('trim', $schemes);
14436f2d7c1SGina Haeussge        $schemes = preg_replace('/^#.*/', '', $schemes);
14536f2d7c1SGina Haeussge        $schemes = array_filter($schemes);
1464c353447SChristopher Smith    }
14736f2d7c1SGina Haeussge    return $schemes;
14836f2d7c1SGina Haeussge}
14936f2d7c1SGina Haeussge
150b9ac8716Schris/**
151edcb01e5SGina Haeussge * Builds a hash from an array of lines
152b625487dSandi *
1533fd0b676Sandi * If $lower is set to true all hash keys are converted to
1543fd0b676Sandi * lower case.
1553fd0b676Sandi *
156b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1573fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
158edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
159b625487dSandi */
160edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) {
161e5fc893fSAndreas Gohr    $conf = array();
162dd74fecfSMichael Hamann    // remove BOM
163dd74fecfSMichael Hamann    if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf))
164dd74fecfSMichael Hamann        $lines[0] = substr($lines[0],3);
165b625487dSandi    foreach ( $lines as $line ) {
16603ff8795SAndreas Gohr        //ignore comments (except escaped ones)
16703ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
16803ff8795SAndreas Gohr        $line = str_replace('\\#','#',$line);
169b625487dSandi        $line = trim($line);
170b625487dSandi        if(empty($line)) continue;
171b625487dSandi        $line = preg_split('/\s+/',$line,2);
172b625487dSandi        // Build the associative array
17327a2b085Sandi        if($lower){
17427a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
17527a2b085Sandi        }else{
176b625487dSandi            $conf[$line[0]] = $line[1];
177b625487dSandi        }
17827a2b085Sandi    }
179b625487dSandi
180b625487dSandi    return $conf;
181b625487dSandi}
182b625487dSandi
183409d7af7SAndreas Gohr/**
184edcb01e5SGina Haeussge * Builds a hash from a configfile
185edcb01e5SGina Haeussge *
186edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
187edcb01e5SGina Haeussge * lower case.
188edcb01e5SGina Haeussge *
189edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
190edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
191edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
192edcb01e5SGina Haeussge */
193edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) {
194edcb01e5SGina Haeussge    $conf = array();
195edcb01e5SGina Haeussge    $lines = @file( $file );
196edcb01e5SGina Haeussge    if ( !$lines ) return $conf;
197edcb01e5SGina Haeussge
198edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
199edcb01e5SGina Haeussge}
200edcb01e5SGina Haeussge
201edcb01e5SGina Haeussge/**
202cb043f52SChris Smith * Retrieve the requested configuration information
203cb043f52SChris Smith *
204cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
205cb043f52SChris Smith *
206cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
207cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
208e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
209cb043f52SChris Smith * @return array    configuration values
210cb043f52SChris Smith */
2114c353447SChristopher Smithfunction retrieveConfig($type,$fn,$params=null,$combine='array_merge') {
212cb043f52SChris Smith    global $config_cascade;
213cb043f52SChris Smith
2144c6a5eccSAndreas Gohr    if(!is_array($params)) $params = array();
2154c6a5eccSAndreas Gohr
216cb043f52SChris Smith    $combined = array();
217cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
218b303b92cSChris Smith    foreach (array('default','local','protected') as $config_group) {
219b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
220b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
22179e79377SAndreas Gohr            if (file_exists($file)) {
2224c6a5eccSAndreas Gohr                $config = call_user_func_array($fn,array_merge(array($file),$params));
2234c353447SChristopher Smith                $combined = $combine($combined, $config);
224cb043f52SChris Smith            }
225cb043f52SChris Smith        }
226b303b92cSChris Smith    }
227cb043f52SChris Smith
228cb043f52SChris Smith    return $combined;
229cb043f52SChris Smith}
230cb043f52SChris Smith
231cb043f52SChris Smith/**
232f8121585SChris Smith * Include the requested configuration information
233f8121585SChris Smith *
234f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
235f8121585SChris Smith *
236f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
237f8121585SChris Smith * @return array              list of files, default before local before protected
238f8121585SChris Smith */
239f8121585SChris Smithfunction getConfigFiles($type) {
240f8121585SChris Smith    global $config_cascade;
241f8121585SChris Smith    $files = array();
242f8121585SChris Smith
243f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
244f8121585SChris Smith    foreach (array('default','local','protected') as $config_group) {
245f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
246f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
247f8121585SChris Smith    }
248f8121585SChris Smith
249f8121585SChris Smith    return $files;
250f8121585SChris Smith}
251f8121585SChris Smith
252f8121585SChris Smith/**
253409d7af7SAndreas Gohr * check if the given action was disabled in config
254409d7af7SAndreas Gohr *
255409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2560e2431b7SGerrit Uitslag * @param string $action
257409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
258409d7af7SAndreas Gohr */
259409d7af7SAndreas Gohrfunction actionOK($action){
260409d7af7SAndreas Gohr    static $disabled = null;
261020ea9e1SChristopher Smith    if(is_null($disabled) || defined('SIMPLE_TEST')){
262409d7af7SAndreas Gohr        global $conf;
2630e2431b7SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
264de4d479aSAdrian Lang        global $auth;
265409d7af7SAndreas Gohr
266409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
267409d7af7SAndreas Gohr        $disabled = explode(',',$conf['disableactions']);
268409d7af7SAndreas Gohr        $disabled = array_map('trim',$disabled);
269e4eda66bSAndreas Gohr        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
270de4d479aSAdrian Lang            $disabled[] = 'register';
271de4d479aSAdrian Lang        }
272e4eda66bSAndreas Gohr        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
273de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
274de4d479aSAdrian Lang        }
275e4eda66bSAndreas Gohr        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
2763a48618aSAnika Henke            $disabled[] = 'subscribe';
2773a48618aSAnika Henke        }
2783a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('Profile')) {
2793a48618aSAnika Henke            $disabled[] = 'profile';
2803a48618aSAnika Henke        }
2812a7abf2dSChristopher Smith        if (is_null($auth) || !$auth->canDo('delUser')) {
2822a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
2832a7abf2dSChristopher Smith        }
2843a48618aSAnika Henke        if (is_null($auth)) {
2853a48618aSAnika Henke            $disabled[] = 'login';
2863a48618aSAnika Henke        }
2873a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('logout')) {
2883a48618aSAnika Henke            $disabled[] = 'logout';
2893a48618aSAnika Henke        }
290409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
291409d7af7SAndreas Gohr    }
292409d7af7SAndreas Gohr
293409d7af7SAndreas Gohr    return !in_array($action,$disabled);
294409d7af7SAndreas Gohr}
295409d7af7SAndreas Gohr
296fe9ec250SChris Smith/**
297fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
298fe9ec250SChris Smith *
299fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
300fe9ec250SChris Smith *
301fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
302fe9ec250SChris Smith *                                                      navigation applies to all other links
303e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
304fe9ec250SChris Smith */
305fe9ec250SChris Smithfunction useHeading($linktype) {
306fe9ec250SChris Smith    static $useHeading = null;
307fe9ec250SChris Smith
308fe9ec250SChris Smith    if (is_null($useHeading)) {
309fe9ec250SChris Smith        global $conf;
310fe9ec250SChris Smith
311fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
312fe9ec250SChris Smith            switch ($conf['useheading']) {
31349eb6e38SAndreas Gohr                case 'content':
31449eb6e38SAndreas Gohr                    $useHeading['content'] = true;
31549eb6e38SAndreas Gohr                    break;
31649eb6e38SAndreas Gohr
31749eb6e38SAndreas Gohr                case 'navigation':
31849eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
31949eb6e38SAndreas Gohr                    break;
320fe9ec250SChris Smith                default:
321fe9ec250SChris Smith                    $useHeading['content'] = true;
322fe9ec250SChris Smith                    $useHeading['navigation'] = true;
323fe9ec250SChris Smith            }
324fe9ec250SChris Smith        } else {
325fe9ec250SChris Smith            $useHeading = array();
326fe9ec250SChris Smith        }
327fe9ec250SChris Smith    }
328fe9ec250SChris Smith
329fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
330fe9ec250SChris Smith}
331fe9ec250SChris Smith
3323994772aSChris Smith/**
3333994772aSChris Smith * obscure config data so information isn't plain text
3343994772aSChris Smith *
3353994772aSChris Smith * @param string       $str     data to be encoded
3363994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
3373994772aSChris Smith * @return string               the encoded value
3383994772aSChris Smith */
3393994772aSChris Smithfunction conf_encodeString($str,$code) {
3403994772aSChris Smith    switch ($code) {
3413994772aSChris Smith        case 'base64'   : return '<b>'.base64_encode($str);
3423994772aSChris Smith        case 'uuencode' : return '<u>'.convert_uuencode($str);
3433994772aSChris Smith        case 'plain':
3443994772aSChris Smith        default:
3453994772aSChris Smith                          return $str;
3463994772aSChris Smith    }
3473994772aSChris Smith}
3483994772aSChris Smith/**
3493994772aSChris Smith * return obscured data as plain text
3503994772aSChris Smith *
3513994772aSChris Smith * @param  string      $str   encoded data
3523994772aSChris Smith * @return string             plain text
3533994772aSChris Smith */
3543994772aSChris Smithfunction conf_decodeString($str) {
3553994772aSChris Smith    switch (substr($str,0,3)) {
3563994772aSChris Smith        case '<b>' : return base64_decode(substr($str,3));
3573994772aSChris Smith        case '<u>' : return convert_uudecode(substr($str,3));
3583994772aSChris Smith        default:  // not encode (or unknown)
3593994772aSChris Smith                     return $str;
3603994772aSChris Smith    }
3613994772aSChris Smith}
3624c353447SChristopher Smith
3634c353447SChristopher Smith/**
3644c353447SChristopher Smith * array combination function to remove negated values (prefixed by !)
3654c353447SChristopher Smith *
3664c353447SChristopher Smith * @param  array $current
3674c353447SChristopher Smith * @param  array $new
3684c353447SChristopher Smith *
3694c353447SChristopher Smith * @return array the combined array, numeric keys reset
3704c353447SChristopher Smith */
3714c353447SChristopher Smithfunction array_merge_with_removal($current, $new) {
3724c353447SChristopher Smith    foreach ($new as $val) {
373*10b38f10SChristopher Smith        if (substr($val,0,1) == DOKU_CONF_NEGATION) {
3743a7669bdSChristopher Smith            $idx = array_search(trim(substr($val,1)),$current);
3754c353447SChristopher Smith            if ($idx !== false) {
3764c353447SChristopher Smith                unset($current[$idx]);
3774c353447SChristopher Smith            }
3784c353447SChristopher Smith        } else {
3793a7669bdSChristopher Smith            $current[] = trim($val);
3804c353447SChristopher Smith        }
3814c353447SChristopher Smith    }
3824c353447SChristopher Smith
3834c353447SChristopher Smith    return array_slice($current,0);
3844c353447SChristopher Smith}
385e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
386