xref: /dokuwiki/inc/confutils.php (revision 0e2431b761b5c24b59109867ec74d7647d16131f)
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
9b625487dSandi
10b625487dSandi/**
11b625487dSandi * Returns the (known) extension and mimetype of a given filename
12b625487dSandi *
1327bf7924STom N Harris * If $knownonly is true (the default), then only known extensions
1427bf7924STom N Harris * are returned.
1527bf7924STom N Harris *
16b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
17b625487dSandi */
1827bf7924STom N Harrisfunction mimetype($file, $knownonly=true){
19b625487dSandi    $mtypes = getMimeTypes();     // known mimetypes
20ad74fe66SAdrian Lang    $ext    = strrpos($file, '.');
21ad74fe66SAdrian Lang    if ($ext === false) {
22ad74fe66SAdrian Lang        return array(false, false, false);
2327bf7924STom N Harris    }
24ad74fe66SAdrian Lang    $ext = strtolower(substr($file, $ext + 1));
25ad74fe66SAdrian Lang    if (!isset($mtypes[$ext])){
26ad74fe66SAdrian Lang        if ($knownonly) {
27ad74fe66SAdrian Lang            return array(false, false, false);
28ecebf3a8SAndreas Gohr        } else {
29ad74fe66SAdrian Lang            return array($ext, 'application/octet-stream', true);
3027bf7924STom N Harris        }
31b625487dSandi    }
32ad74fe66SAdrian Lang    if($mtypes[$ext][0] == '!'){
33ad74fe66SAdrian Lang        return array($ext, substr($mtypes[$ext],1), true);
34ad74fe66SAdrian Lang    }else{
35ad74fe66SAdrian Lang        return array($ext, $mtypes[$ext], false);
36ad74fe66SAdrian Lang    }
37b625487dSandi}
38b625487dSandi
39b625487dSandi/**
40b625487dSandi * returns a hash of mimetypes
41b625487dSandi *
42b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
43b625487dSandi */
44b625487dSandifunction getMimeTypes() {
4549eb6e38SAndreas Gohr    static $mime = null;
46b625487dSandi    if ( !$mime ) {
47cb043f52SChris Smith        $mime = retrieveConfig('mime','confToHash');
48b625487dSandi    }
49b625487dSandi    return $mime;
50b625487dSandi}
51b625487dSandi
52b625487dSandi/**
53b625487dSandi * returns a hash of acronyms
54b625487dSandi *
55b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
56b625487dSandi */
57b625487dSandifunction getAcronyms() {
5849eb6e38SAndreas Gohr    static $acronyms = null;
59b625487dSandi    if ( !$acronyms ) {
60cb043f52SChris Smith        $acronyms = retrieveConfig('acronyms','confToHash');
61b625487dSandi    }
62b625487dSandi    return $acronyms;
63b625487dSandi}
64b625487dSandi
65b625487dSandi/**
66b625487dSandi * returns a hash of smileys
67b625487dSandi *
68b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
69b625487dSandi */
70b625487dSandifunction getSmileys() {
7149eb6e38SAndreas Gohr    static $smileys = null;
72b625487dSandi    if ( !$smileys ) {
73cb043f52SChris Smith        $smileys = retrieveConfig('smileys','confToHash');
74b625487dSandi    }
75b625487dSandi    return $smileys;
76b625487dSandi}
77b625487dSandi
78b625487dSandi/**
79b625487dSandi * returns a hash of entities
80b625487dSandi *
81b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
82b625487dSandi */
83b625487dSandifunction getEntities() {
8449eb6e38SAndreas Gohr    static $entities = null;
85b625487dSandi    if ( !$entities ) {
86cb043f52SChris Smith        $entities = retrieveConfig('entities','confToHash');
87b625487dSandi    }
88b625487dSandi    return $entities;
89b625487dSandi}
90b625487dSandi
91b625487dSandi/**
92b625487dSandi * returns a hash of interwikilinks
93b625487dSandi *
94b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
95b625487dSandi */
96b625487dSandifunction getInterwiki() {
9749eb6e38SAndreas Gohr    static $wikis = null;
98b625487dSandi    if ( !$wikis ) {
994c6a5eccSAndreas Gohr        $wikis = retrieveConfig('interwiki','confToHash',array(true));
100b625487dSandi    }
10197a3e4e3Sandi    //add sepecial case 'this'
10227a2b085Sandi    $wikis['this'] = DOKU_URL.'{NAME}';
103b625487dSandi    return $wikis;
104b625487dSandi}
105b625487dSandi
106b625487dSandi/**
107b9ac8716Schris * returns array of wordblock patterns
108b9ac8716Schris *
109b9ac8716Schris */
110b9ac8716Schrisfunction getWordblocks() {
11149eb6e38SAndreas Gohr    static $wordblocks = null;
112b9ac8716Schris    if ( !$wordblocks ) {
113cb043f52SChris Smith        $wordblocks = retrieveConfig('wordblock','file');
114b9ac8716Schris    }
115b9ac8716Schris    return $wordblocks;
116b9ac8716Schris}
117b9ac8716Schris
118e3ab6fc5SMichael Hamann/**
119e3ab6fc5SMichael Hamann * Gets the list of configured schemes
120e3ab6fc5SMichael Hamann *
121e3ab6fc5SMichael Hamann * @return array the schemes
122e3ab6fc5SMichael Hamann */
12336f2d7c1SGina Haeussgefunction getSchemes() {
12449eb6e38SAndreas Gohr    static $schemes = null;
12536f2d7c1SGina Haeussge    if ( !$schemes ) {
126cb043f52SChris Smith        $schemes = retrieveConfig('scheme','file');
12736f2d7c1SGina Haeussge    }
12836f2d7c1SGina Haeussge    $schemes = array_map('trim', $schemes);
12936f2d7c1SGina Haeussge    $schemes = preg_replace('/^#.*/', '', $schemes);
13036f2d7c1SGina Haeussge    $schemes = array_filter($schemes);
13136f2d7c1SGina Haeussge    return $schemes;
13236f2d7c1SGina Haeussge}
13336f2d7c1SGina Haeussge
134b9ac8716Schris/**
135edcb01e5SGina Haeussge * Builds a hash from an array of lines
136b625487dSandi *
1373fd0b676Sandi * If $lower is set to true all hash keys are converted to
1383fd0b676Sandi * lower case.
1393fd0b676Sandi *
140b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1413fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
142edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
143b625487dSandi */
144edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) {
145e5fc893fSAndreas Gohr    $conf = array();
146dd74fecfSMichael Hamann    // remove BOM
147dd74fecfSMichael Hamann    if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf))
148dd74fecfSMichael Hamann        $lines[0] = substr($lines[0],3);
149b625487dSandi    foreach ( $lines as $line ) {
15003ff8795SAndreas Gohr        //ignore comments (except escaped ones)
15103ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
15203ff8795SAndreas Gohr        $line = str_replace('\\#','#',$line);
153b625487dSandi        $line = trim($line);
154b625487dSandi        if(empty($line)) continue;
155b625487dSandi        $line = preg_split('/\s+/',$line,2);
156b625487dSandi        // Build the associative array
15727a2b085Sandi        if($lower){
15827a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
15927a2b085Sandi        }else{
160b625487dSandi            $conf[$line[0]] = $line[1];
161b625487dSandi        }
16227a2b085Sandi    }
163b625487dSandi
164b625487dSandi    return $conf;
165b625487dSandi}
166b625487dSandi
167409d7af7SAndreas Gohr/**
168edcb01e5SGina Haeussge * Builds a hash from a configfile
169edcb01e5SGina Haeussge *
170edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
171edcb01e5SGina Haeussge * lower case.
172edcb01e5SGina Haeussge *
173edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
174edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
175edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
176edcb01e5SGina Haeussge */
177edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) {
178edcb01e5SGina Haeussge    $conf = array();
179edcb01e5SGina Haeussge    $lines = @file( $file );
180edcb01e5SGina Haeussge    if ( !$lines ) return $conf;
181edcb01e5SGina Haeussge
182edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
183edcb01e5SGina Haeussge}
184edcb01e5SGina Haeussge
185edcb01e5SGina Haeussge/**
186cb043f52SChris Smith * Retrieve the requested configuration information
187cb043f52SChris Smith *
188cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
189cb043f52SChris Smith *
190cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
191cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
192e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
193cb043f52SChris Smith * @return array    configuration values
194cb043f52SChris Smith */
1954c6a5eccSAndreas Gohrfunction retrieveConfig($type,$fn,$params=null) {
196cb043f52SChris Smith    global $config_cascade;
197cb043f52SChris Smith
1984c6a5eccSAndreas Gohr    if(!is_array($params)) $params = array();
1994c6a5eccSAndreas Gohr
200cb043f52SChris Smith    $combined = array();
201cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
202b303b92cSChris Smith    foreach (array('default','local','protected') as $config_group) {
203b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
204b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
205cb043f52SChris Smith            if (@file_exists($file)) {
2064c6a5eccSAndreas Gohr                $config = call_user_func_array($fn,array_merge(array($file),$params));
207cb043f52SChris Smith                $combined = array_merge($combined, $config);
208cb043f52SChris Smith            }
209cb043f52SChris Smith        }
210b303b92cSChris Smith    }
211cb043f52SChris Smith
212cb043f52SChris Smith    return $combined;
213cb043f52SChris Smith}
214cb043f52SChris Smith
215cb043f52SChris Smith/**
216f8121585SChris Smith * Include the requested configuration information
217f8121585SChris Smith *
218f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
219f8121585SChris Smith *
220f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
221f8121585SChris Smith * @return array              list of files, default before local before protected
222f8121585SChris Smith */
223f8121585SChris Smithfunction getConfigFiles($type) {
224f8121585SChris Smith    global $config_cascade;
225f8121585SChris Smith    $files = array();
226f8121585SChris Smith
227f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
228f8121585SChris Smith    foreach (array('default','local','protected') as $config_group) {
229f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
230f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
231f8121585SChris Smith    }
232f8121585SChris Smith
233f8121585SChris Smith    return $files;
234f8121585SChris Smith}
235f8121585SChris Smith
236f8121585SChris Smith/**
237409d7af7SAndreas Gohr * check if the given action was disabled in config
238409d7af7SAndreas Gohr *
239409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
240*0e2431b7SGerrit Uitslag * @param string $action
241409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
242409d7af7SAndreas Gohr */
243409d7af7SAndreas Gohrfunction actionOK($action){
244409d7af7SAndreas Gohr    static $disabled = null;
245020ea9e1SChristopher Smith    if(is_null($disabled) || defined('SIMPLE_TEST')){
246409d7af7SAndreas Gohr        global $conf;
247*0e2431b7SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
248de4d479aSAdrian Lang        global $auth;
249409d7af7SAndreas Gohr
250409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
251409d7af7SAndreas Gohr        $disabled = explode(',',$conf['disableactions']);
252409d7af7SAndreas Gohr        $disabled = array_map('trim',$disabled);
253e4eda66bSAndreas Gohr        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
254de4d479aSAdrian Lang            $disabled[] = 'register';
255de4d479aSAdrian Lang        }
256e4eda66bSAndreas Gohr        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
257de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
258de4d479aSAdrian Lang        }
259e4eda66bSAndreas Gohr        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
2603a48618aSAnika Henke            $disabled[] = 'subscribe';
2613a48618aSAnika Henke        }
2623a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('Profile')) {
2633a48618aSAnika Henke            $disabled[] = 'profile';
2643a48618aSAnika Henke        }
2652a7abf2dSChristopher Smith        if (is_null($auth) || !$auth->canDo('delUser')) {
2662a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
2672a7abf2dSChristopher Smith        }
2683a48618aSAnika Henke        if (is_null($auth)) {
2693a48618aSAnika Henke            $disabled[] = 'login';
2703a48618aSAnika Henke        }
2713a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('logout')) {
2723a48618aSAnika Henke            $disabled[] = 'logout';
2733a48618aSAnika Henke        }
274409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
275409d7af7SAndreas Gohr    }
276409d7af7SAndreas Gohr
277409d7af7SAndreas Gohr    return !in_array($action,$disabled);
278409d7af7SAndreas Gohr}
279409d7af7SAndreas Gohr
280fe9ec250SChris Smith/**
281fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
282fe9ec250SChris Smith *
283fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
284fe9ec250SChris Smith *
285fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
286fe9ec250SChris Smith *                                                      navigation applies to all other links
287e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
288fe9ec250SChris Smith */
289fe9ec250SChris Smithfunction useHeading($linktype) {
290fe9ec250SChris Smith    static $useHeading = null;
291fe9ec250SChris Smith
292fe9ec250SChris Smith    if (is_null($useHeading)) {
293fe9ec250SChris Smith        global $conf;
294fe9ec250SChris Smith
295fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
296fe9ec250SChris Smith            switch ($conf['useheading']) {
29749eb6e38SAndreas Gohr                case 'content':
29849eb6e38SAndreas Gohr                    $useHeading['content'] = true;
29949eb6e38SAndreas Gohr                    break;
30049eb6e38SAndreas Gohr
30149eb6e38SAndreas Gohr                case 'navigation':
30249eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
30349eb6e38SAndreas Gohr                    break;
304fe9ec250SChris Smith                default:
305fe9ec250SChris Smith                    $useHeading['content'] = true;
306fe9ec250SChris Smith                    $useHeading['navigation'] = true;
307fe9ec250SChris Smith            }
308fe9ec250SChris Smith        } else {
309fe9ec250SChris Smith            $useHeading = array();
310fe9ec250SChris Smith        }
311fe9ec250SChris Smith    }
312fe9ec250SChris Smith
313fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
314fe9ec250SChris Smith}
315fe9ec250SChris Smith
3163994772aSChris Smith/**
3173994772aSChris Smith * obscure config data so information isn't plain text
3183994772aSChris Smith *
3193994772aSChris Smith * @param string       $str     data to be encoded
3203994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
3213994772aSChris Smith * @return string               the encoded value
3223994772aSChris Smith */
3233994772aSChris Smithfunction conf_encodeString($str,$code) {
3243994772aSChris Smith    switch ($code) {
3253994772aSChris Smith        case 'base64'   : return '<b>'.base64_encode($str);
3263994772aSChris Smith        case 'uuencode' : return '<u>'.convert_uuencode($str);
3273994772aSChris Smith        case 'plain':
3283994772aSChris Smith        default:
3293994772aSChris Smith                          return $str;
3303994772aSChris Smith    }
3313994772aSChris Smith}
3323994772aSChris Smith/**
3333994772aSChris Smith * return obscured data as plain text
3343994772aSChris Smith *
3353994772aSChris Smith * @param  string      $str   encoded data
3363994772aSChris Smith * @return string             plain text
3373994772aSChris Smith */
3383994772aSChris Smithfunction conf_decodeString($str) {
3393994772aSChris Smith    switch (substr($str,0,3)) {
3403994772aSChris Smith        case '<b>' : return base64_decode(substr($str,3));
3413994772aSChris Smith        case '<u>' : return convert_uudecode(substr($str,3));
3423994772aSChris Smith        default:  // not encode (or unknown)
3433994772aSChris Smith                     return $str;
3443994772aSChris Smith    }
3453994772aSChris Smith}
346e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
347