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