xref: /dokuwiki/inc/confutils.php (revision e3776c06c37cc197709dac60892604dfea894ac2)
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
118b9ac8716Schris
11936f2d7c1SGina Haeussgefunction getSchemes() {
12049eb6e38SAndreas Gohr    static $schemes = null;
12136f2d7c1SGina Haeussge    if ( !$schemes ) {
122cb043f52SChris Smith        $schemes = retrieveConfig('scheme','file');
12336f2d7c1SGina Haeussge    }
12436f2d7c1SGina Haeussge    $schemes = array_map('trim', $schemes);
12536f2d7c1SGina Haeussge    $schemes = preg_replace('/^#.*/', '', $schemes);
12636f2d7c1SGina Haeussge    $schemes = array_filter($schemes);
12736f2d7c1SGina Haeussge    return $schemes;
12836f2d7c1SGina Haeussge}
12936f2d7c1SGina Haeussge
130b9ac8716Schris/**
131edcb01e5SGina Haeussge * Builds a hash from an array of lines
132b625487dSandi *
1333fd0b676Sandi * If $lower is set to true all hash keys are converted to
1343fd0b676Sandi * lower case.
1353fd0b676Sandi *
136b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1373fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
138edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
139b625487dSandi */
140edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) {
141e5fc893fSAndreas Gohr    $conf = array();
142b625487dSandi    foreach ( $lines as $line ) {
14303ff8795SAndreas Gohr        //ignore comments (except escaped ones)
14403ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
14503ff8795SAndreas Gohr        $line = str_replace('\\#','#',$line);
146b625487dSandi        $line = trim($line);
147b625487dSandi        if(empty($line)) continue;
148b625487dSandi        $line = preg_split('/\s+/',$line,2);
149b625487dSandi        // Build the associative array
15027a2b085Sandi        if($lower){
15127a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
15227a2b085Sandi        }else{
153b625487dSandi            $conf[$line[0]] = $line[1];
154b625487dSandi        }
15527a2b085Sandi    }
156b625487dSandi
157b625487dSandi    return $conf;
158b625487dSandi}
159b625487dSandi
160409d7af7SAndreas Gohr/**
161edcb01e5SGina Haeussge * Builds a hash from a configfile
162edcb01e5SGina Haeussge *
163edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
164edcb01e5SGina Haeussge * lower case.
165edcb01e5SGina Haeussge *
166edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
167edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
168edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
169edcb01e5SGina Haeussge */
170edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) {
171edcb01e5SGina Haeussge    $conf = array();
172edcb01e5SGina Haeussge    $lines = @file( $file );
173edcb01e5SGina Haeussge    if ( !$lines ) return $conf;
174edcb01e5SGina Haeussge
175edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
176edcb01e5SGina Haeussge}
177edcb01e5SGina Haeussge
178edcb01e5SGina Haeussge/**
179cb043f52SChris Smith * Retrieve the requested configuration information
180cb043f52SChris Smith *
181cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
182cb043f52SChris Smith *
183cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
184cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
1854c6a5eccSAndreas Gohr * @param  array    $param    optional additional params to pass to the callback
186cb043f52SChris Smith * @return array    configuration values
187cb043f52SChris Smith */
1884c6a5eccSAndreas Gohrfunction retrieveConfig($type,$fn,$params=null) {
189cb043f52SChris Smith    global $config_cascade;
190cb043f52SChris Smith
1914c6a5eccSAndreas Gohr    if(!is_array($params)) $params = array();
1924c6a5eccSAndreas Gohr
193cb043f52SChris Smith    $combined = array();
194cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
195b303b92cSChris Smith    foreach (array('default','local','protected') as $config_group) {
196b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
197b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
198cb043f52SChris Smith            if (@file_exists($file)) {
1994c6a5eccSAndreas Gohr                $config = call_user_func_array($fn,array_merge(array($file),$params));
200cb043f52SChris Smith                $combined = array_merge($combined, $config);
201cb043f52SChris Smith            }
202cb043f52SChris Smith        }
203b303b92cSChris Smith    }
204cb043f52SChris Smith
205cb043f52SChris Smith    return $combined;
206cb043f52SChris Smith}
207cb043f52SChris Smith
208cb043f52SChris Smith/**
209f8121585SChris Smith * Include the requested configuration information
210f8121585SChris Smith *
211f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
212f8121585SChris Smith *
213f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
214f8121585SChris Smith * @return array              list of files, default before local before protected
215f8121585SChris Smith */
216f8121585SChris Smithfunction getConfigFiles($type) {
217f8121585SChris Smith    global $config_cascade;
218f8121585SChris Smith    $files = array();
219f8121585SChris Smith
220f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
221f8121585SChris Smith    foreach (array('default','local','protected') as $config_group) {
222f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
223f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
224f8121585SChris Smith    }
225f8121585SChris Smith
226f8121585SChris Smith    return $files;
227f8121585SChris Smith}
228f8121585SChris Smith
229f8121585SChris Smith/**
230409d7af7SAndreas Gohr * check if the given action was disabled in config
231409d7af7SAndreas Gohr *
232409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
233409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
234409d7af7SAndreas Gohr */
235409d7af7SAndreas Gohrfunction actionOK($action){
236409d7af7SAndreas Gohr    static $disabled = null;
237409d7af7SAndreas Gohr    if(is_null($disabled)){
238409d7af7SAndreas Gohr        global $conf;
239de4d479aSAdrian Lang        global $auth;
240409d7af7SAndreas Gohr
241409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
242409d7af7SAndreas Gohr        $disabled = explode(',',$conf['disableactions']);
243409d7af7SAndreas Gohr        $disabled = array_map('trim',$disabled);
244409d7af7SAndreas Gohr        if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
245409d7af7SAndreas Gohr        if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
2468c286148SMichael Klier        if(isset($conf['subscribers']) && !$conf['subscribers']) {
2478c286148SMichael Klier            $disabled[] = 'subscribe';
2488c286148SMichael Klier        }
249de4d479aSAdrian Lang        if (is_null($auth) || !$auth->canDo('addUser')) {
250de4d479aSAdrian Lang            $disabled[] = 'register';
251de4d479aSAdrian Lang        }
252de4d479aSAdrian Lang        if (is_null($auth) || !$auth->canDo('modPass')) {
253de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
254de4d479aSAdrian Lang        }
255409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
256409d7af7SAndreas Gohr    }
257409d7af7SAndreas Gohr
258409d7af7SAndreas Gohr    return !in_array($action,$disabled);
259409d7af7SAndreas Gohr}
260409d7af7SAndreas Gohr
261fe9ec250SChris Smith/**
262fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
263fe9ec250SChris Smith *
264fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
265fe9ec250SChris Smith *
266fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
267fe9ec250SChris Smith *                                                      navigation applies to all other links
268fe9ec250SChris Smith * @returns boolean             true if headings should be used for $linktype, false otherwise
269fe9ec250SChris Smith */
270fe9ec250SChris Smithfunction useHeading($linktype) {
271fe9ec250SChris Smith    static $useHeading = null;
272fe9ec250SChris Smith
273fe9ec250SChris Smith    if (is_null($useHeading)) {
274fe9ec250SChris Smith        global $conf;
275fe9ec250SChris Smith
276fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
277fe9ec250SChris Smith            switch ($conf['useheading']) {
27849eb6e38SAndreas Gohr                case 'content':
27949eb6e38SAndreas Gohr                    $useHeading['content'] = true;
28049eb6e38SAndreas Gohr                    break;
28149eb6e38SAndreas Gohr
28249eb6e38SAndreas Gohr                case 'navigation':
28349eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
28449eb6e38SAndreas Gohr                    break;
285fe9ec250SChris Smith                default:
286fe9ec250SChris Smith                    $useHeading['content'] = true;
287fe9ec250SChris Smith                    $useHeading['navigation'] = true;
288fe9ec250SChris Smith            }
289fe9ec250SChris Smith        } else {
290fe9ec250SChris Smith            $useHeading = array();
291fe9ec250SChris Smith        }
292fe9ec250SChris Smith    }
293fe9ec250SChris Smith
294fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
295fe9ec250SChris Smith}
296fe9ec250SChris Smith
2973994772aSChris Smith/**
2983994772aSChris Smith * obscure config data so information isn't plain text
2993994772aSChris Smith *
3003994772aSChris Smith * @param string       $str     data to be encoded
3013994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
3023994772aSChris Smith * @return string               the encoded value
3033994772aSChris Smith */
3043994772aSChris Smithfunction conf_encodeString($str,$code) {
3053994772aSChris Smith    switch ($code) {
3063994772aSChris Smith        case 'base64'   : return '<b>'.base64_encode($str);
3073994772aSChris Smith        case 'uuencode' : return '<u>'.convert_uuencode($str);
3083994772aSChris Smith        case 'plain':
3093994772aSChris Smith        default:
3103994772aSChris Smith                          return $str;
3113994772aSChris Smith    }
3123994772aSChris Smith}
3133994772aSChris Smith/**
3143994772aSChris Smith * return obscured data as plain text
3153994772aSChris Smith *
3163994772aSChris Smith * @param  string      $str   encoded data
3173994772aSChris Smith * @return string             plain text
3183994772aSChris Smith */
3193994772aSChris Smithfunction conf_decodeString($str) {
3203994772aSChris Smith    switch (substr($str,0,3)) {
3213994772aSChris Smith        case '<b>' : return base64_decode(substr($str,3));
3223994772aSChris Smith        case '<u>' : return convert_uudecode(substr($str,3));
3233994772aSChris Smith        default:  // not encode (or unknown)
3243994772aSChris Smith                     return $str;
3253994772aSChris Smith    }
3263994772aSChris Smith}
327*e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
328