xref: /dokuwiki/inc/confutils.php (revision 61537d473082f0edeb704a391f412ea4040b3226)
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
910b38f10SChristopher Smith/*
1010b38f10SChristopher Smith * line prefix used to negate single value config items
1110b38f10SChristopher Smith * (scheme.conf & stopwords.conf), e.g.
1210b38f10SChristopher Smith * !gopher
1310b38f10SChristopher Smith */
1410b38f10SChristopher 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/**
123*61537d47SAndreas Gohr * Returns the jquery script versions defined in lib/scripts/jquery/versions
124*61537d47SAndreas Gohr *
125*61537d47SAndreas Gohr * @return array
126*61537d47SAndreas Gohr */
127*61537d47SAndreas Gohrfunction getJqueryVersions() {
128*61537d47SAndreas Gohr    $versions = array();
129*61537d47SAndreas Gohr    $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
130*61537d47SAndreas Gohr    foreach($lines as $line ) {
131*61537d47SAndreas Gohr        $line = preg_replace('/#.*$/', '', $line);
132*61537d47SAndreas Gohr        list($key, $val) = explode('=', $line, 2);
133*61537d47SAndreas Gohr        $key = trim($key);
134*61537d47SAndreas Gohr        $val = trim($val);
135*61537d47SAndreas Gohr        $versions[$key] = $val;
136*61537d47SAndreas Gohr    }
137*61537d47SAndreas Gohr    return $versions;
138*61537d47SAndreas Gohr}
139*61537d47SAndreas Gohr
140*61537d47SAndreas Gohr/**
141b9ac8716Schris * returns array of wordblock patterns
142b9ac8716Schris *
143b9ac8716Schris */
144b9ac8716Schrisfunction getWordblocks() {
14549eb6e38SAndreas Gohr    static $wordblocks = null;
146b9ac8716Schris    if ( !$wordblocks ) {
1474c353447SChristopher Smith        $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal');
148b9ac8716Schris    }
149b9ac8716Schris    return $wordblocks;
150b9ac8716Schris}
151b9ac8716Schris
152e3ab6fc5SMichael Hamann/**
153e3ab6fc5SMichael Hamann * Gets the list of configured schemes
154e3ab6fc5SMichael Hamann *
155e3ab6fc5SMichael Hamann * @return array the schemes
156e3ab6fc5SMichael Hamann */
15736f2d7c1SGina Haeussgefunction getSchemes() {
15849eb6e38SAndreas Gohr    static $schemes = null;
15936f2d7c1SGina Haeussge    if ( !$schemes ) {
1604c353447SChristopher Smith        $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal');
16136f2d7c1SGina Haeussge        $schemes = array_map('trim', $schemes);
16236f2d7c1SGina Haeussge        $schemes = preg_replace('/^#.*/', '', $schemes);
16336f2d7c1SGina Haeussge        $schemes = array_filter($schemes);
1644c353447SChristopher Smith    }
16536f2d7c1SGina Haeussge    return $schemes;
16636f2d7c1SGina Haeussge}
16736f2d7c1SGina Haeussge
168b9ac8716Schris/**
169edcb01e5SGina Haeussge * Builds a hash from an array of lines
170b625487dSandi *
1713fd0b676Sandi * If $lower is set to true all hash keys are converted to
1723fd0b676Sandi * lower case.
1733fd0b676Sandi *
174b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1753fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
176edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
177b625487dSandi */
178edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) {
179e5fc893fSAndreas Gohr    $conf = array();
180dd74fecfSMichael Hamann    // remove BOM
181dd74fecfSMichael Hamann    if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf))
182dd74fecfSMichael Hamann        $lines[0] = substr($lines[0],3);
183b625487dSandi    foreach ( $lines as $line ) {
18403ff8795SAndreas Gohr        //ignore comments (except escaped ones)
18503ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
18603ff8795SAndreas Gohr        $line = str_replace('\\#','#',$line);
187b625487dSandi        $line = trim($line);
188b625487dSandi        if(empty($line)) continue;
189b625487dSandi        $line = preg_split('/\s+/',$line,2);
190b625487dSandi        // Build the associative array
19127a2b085Sandi        if($lower){
19227a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
19327a2b085Sandi        }else{
194b625487dSandi            $conf[$line[0]] = $line[1];
195b625487dSandi        }
19627a2b085Sandi    }
197b625487dSandi
198b625487dSandi    return $conf;
199b625487dSandi}
200b625487dSandi
201409d7af7SAndreas Gohr/**
202edcb01e5SGina Haeussge * Builds a hash from a configfile
203edcb01e5SGina Haeussge *
204edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
205edcb01e5SGina Haeussge * lower case.
206edcb01e5SGina Haeussge *
207edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
208edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
209edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
210edcb01e5SGina Haeussge */
211edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) {
212edcb01e5SGina Haeussge    $conf = array();
213edcb01e5SGina Haeussge    $lines = @file( $file );
214edcb01e5SGina Haeussge    if ( !$lines ) return $conf;
215edcb01e5SGina Haeussge
216edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
217edcb01e5SGina Haeussge}
218edcb01e5SGina Haeussge
219edcb01e5SGina Haeussge/**
220cb043f52SChris Smith * Retrieve the requested configuration information
221cb043f52SChris Smith *
222cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
223cb043f52SChris Smith *
224cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
225cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
226e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
2274286c64eSChristopher Smith * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
2284286c64eSChristopher Smith *                            the function takes two parameters,
2294286c64eSChristopher Smith *                               $combined - the already read & merged configuration values
2304286c64eSChristopher Smith *                               $new - array of config values from the config cascade file being currently processed
2314286c64eSChristopher Smith *                            and returns an array of the merged configuration values.
232cb043f52SChris Smith * @return array    configuration values
233cb043f52SChris Smith */
2344c353447SChristopher Smithfunction retrieveConfig($type,$fn,$params=null,$combine='array_merge') {
235cb043f52SChris Smith    global $config_cascade;
236cb043f52SChris Smith
2374c6a5eccSAndreas Gohr    if(!is_array($params)) $params = array();
2384c6a5eccSAndreas Gohr
239cb043f52SChris Smith    $combined = array();
240cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
241b303b92cSChris Smith    foreach (array('default','local','protected') as $config_group) {
242b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
243b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
24479e79377SAndreas Gohr            if (file_exists($file)) {
2454c6a5eccSAndreas Gohr                $config = call_user_func_array($fn,array_merge(array($file),$params));
2464c353447SChristopher Smith                $combined = $combine($combined, $config);
247cb043f52SChris Smith            }
248cb043f52SChris Smith        }
249b303b92cSChris Smith    }
250cb043f52SChris Smith
251cb043f52SChris Smith    return $combined;
252cb043f52SChris Smith}
253cb043f52SChris Smith
254cb043f52SChris Smith/**
255f8121585SChris Smith * Include the requested configuration information
256f8121585SChris Smith *
257f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
258f8121585SChris Smith *
259f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
260f8121585SChris Smith * @return array              list of files, default before local before protected
261f8121585SChris Smith */
262f8121585SChris Smithfunction getConfigFiles($type) {
263f8121585SChris Smith    global $config_cascade;
264f8121585SChris Smith    $files = array();
265f8121585SChris Smith
266f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
267f8121585SChris Smith    foreach (array('default','local','protected') as $config_group) {
268f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
269f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
270f8121585SChris Smith    }
271f8121585SChris Smith
272f8121585SChris Smith    return $files;
273f8121585SChris Smith}
274f8121585SChris Smith
275f8121585SChris Smith/**
276409d7af7SAndreas Gohr * check if the given action was disabled in config
277409d7af7SAndreas Gohr *
278409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2790e2431b7SGerrit Uitslag * @param string $action
280409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
281409d7af7SAndreas Gohr */
282409d7af7SAndreas Gohrfunction actionOK($action){
283409d7af7SAndreas Gohr    static $disabled = null;
284020ea9e1SChristopher Smith    if(is_null($disabled) || defined('SIMPLE_TEST')){
285409d7af7SAndreas Gohr        global $conf;
2860e2431b7SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
287de4d479aSAdrian Lang        global $auth;
288409d7af7SAndreas Gohr
289409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
290409d7af7SAndreas Gohr        $disabled = explode(',',$conf['disableactions']);
291409d7af7SAndreas Gohr        $disabled = array_map('trim',$disabled);
292e4eda66bSAndreas Gohr        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
293de4d479aSAdrian Lang            $disabled[] = 'register';
294de4d479aSAdrian Lang        }
295e4eda66bSAndreas Gohr        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
296de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
297de4d479aSAdrian Lang        }
298e4eda66bSAndreas Gohr        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
2993a48618aSAnika Henke            $disabled[] = 'subscribe';
3003a48618aSAnika Henke        }
3013a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('Profile')) {
3023a48618aSAnika Henke            $disabled[] = 'profile';
3033a48618aSAnika Henke        }
3042a7abf2dSChristopher Smith        if (is_null($auth) || !$auth->canDo('delUser')) {
3052a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
3062a7abf2dSChristopher Smith        }
3073a48618aSAnika Henke        if (is_null($auth)) {
3083a48618aSAnika Henke            $disabled[] = 'login';
3093a48618aSAnika Henke        }
3103a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('logout')) {
3113a48618aSAnika Henke            $disabled[] = 'logout';
3123a48618aSAnika Henke        }
313409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
314409d7af7SAndreas Gohr    }
315409d7af7SAndreas Gohr
316409d7af7SAndreas Gohr    return !in_array($action,$disabled);
317409d7af7SAndreas Gohr}
318409d7af7SAndreas Gohr
319fe9ec250SChris Smith/**
320fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
321fe9ec250SChris Smith *
322fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
323fe9ec250SChris Smith *
324fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
325fe9ec250SChris Smith *                                                      navigation applies to all other links
326e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
327fe9ec250SChris Smith */
328fe9ec250SChris Smithfunction useHeading($linktype) {
329fe9ec250SChris Smith    static $useHeading = null;
330fe9ec250SChris Smith
331fe9ec250SChris Smith    if (is_null($useHeading)) {
332fe9ec250SChris Smith        global $conf;
333fe9ec250SChris Smith
334fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
335fe9ec250SChris Smith            switch ($conf['useheading']) {
33649eb6e38SAndreas Gohr                case 'content':
33749eb6e38SAndreas Gohr                    $useHeading['content'] = true;
33849eb6e38SAndreas Gohr                    break;
33949eb6e38SAndreas Gohr
34049eb6e38SAndreas Gohr                case 'navigation':
34149eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
34249eb6e38SAndreas Gohr                    break;
343fe9ec250SChris Smith                default:
344fe9ec250SChris Smith                    $useHeading['content'] = true;
345fe9ec250SChris Smith                    $useHeading['navigation'] = true;
346fe9ec250SChris Smith            }
347fe9ec250SChris Smith        } else {
348fe9ec250SChris Smith            $useHeading = array();
349fe9ec250SChris Smith        }
350fe9ec250SChris Smith    }
351fe9ec250SChris Smith
352fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
353fe9ec250SChris Smith}
354fe9ec250SChris Smith
3553994772aSChris Smith/**
3563994772aSChris Smith * obscure config data so information isn't plain text
3573994772aSChris Smith *
3583994772aSChris Smith * @param string       $str     data to be encoded
3593994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
3603994772aSChris Smith * @return string               the encoded value
3613994772aSChris Smith */
3623994772aSChris Smithfunction conf_encodeString($str,$code) {
3633994772aSChris Smith    switch ($code) {
3643994772aSChris Smith        case 'base64'   : return '<b>'.base64_encode($str);
3653994772aSChris Smith        case 'uuencode' : return '<u>'.convert_uuencode($str);
3663994772aSChris Smith        case 'plain':
3673994772aSChris Smith        default:
3683994772aSChris Smith                          return $str;
3693994772aSChris Smith    }
3703994772aSChris Smith}
3713994772aSChris Smith/**
3723994772aSChris Smith * return obscured data as plain text
3733994772aSChris Smith *
3743994772aSChris Smith * @param  string      $str   encoded data
3753994772aSChris Smith * @return string             plain text
3763994772aSChris Smith */
3773994772aSChris Smithfunction conf_decodeString($str) {
3783994772aSChris Smith    switch (substr($str,0,3)) {
3793994772aSChris Smith        case '<b>' : return base64_decode(substr($str,3));
3803994772aSChris Smith        case '<u>' : return convert_uudecode(substr($str,3));
381b96ff25bSElan Ruusamäe        default:  // not encoded (or unknown)
3823994772aSChris Smith                     return $str;
3833994772aSChris Smith    }
3843994772aSChris Smith}
3854c353447SChristopher Smith
3864c353447SChristopher Smith/**
3874c353447SChristopher Smith * array combination function to remove negated values (prefixed by !)
3884c353447SChristopher Smith *
3894c353447SChristopher Smith * @param  array $current
3904c353447SChristopher Smith * @param  array $new
3914c353447SChristopher Smith *
3924c353447SChristopher Smith * @return array the combined array, numeric keys reset
3934c353447SChristopher Smith */
3944c353447SChristopher Smithfunction array_merge_with_removal($current, $new) {
3954c353447SChristopher Smith    foreach ($new as $val) {
39610b38f10SChristopher Smith        if (substr($val,0,1) == DOKU_CONF_NEGATION) {
3973a7669bdSChristopher Smith            $idx = array_search(trim(substr($val,1)),$current);
3984c353447SChristopher Smith            if ($idx !== false) {
3994c353447SChristopher Smith                unset($current[$idx]);
4004c353447SChristopher Smith            }
4014c353447SChristopher Smith        } else {
4023a7669bdSChristopher Smith            $current[] = trim($val);
4034c353447SChristopher Smith        }
4044c353447SChristopher Smith    }
4054c353447SChristopher Smith
4064c353447SChristopher Smith    return array_slice($current,0);
4074c353447SChristopher Smith}
408e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
409