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