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