xref: /dokuwiki/inc/confutils.php (revision edcb01e585240a0977dded101c5e715aa365e9df)
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 *
13b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
14b625487dSandi */
15b625487dSandifunction mimetype($file){
16ecebf3a8SAndreas Gohr  $ret    = array(false,false,false); // return array
17b625487dSandi  $mtypes = getMimeTypes();     // known mimetypes
18b625487dSandi  $exts   = join('|',array_keys($mtypes));  // known extensions (regexp)
19b625487dSandi  if(preg_match('#\.('.$exts.')$#i',$file,$matches)){
20b625487dSandi    $ext = strtolower($matches[1]);
21b625487dSandi  }
22b625487dSandi
23b625487dSandi  if($ext && $mtypes[$ext]){
24ecebf3a8SAndreas Gohr    if($mtypes[$ext][0] == '!'){
25ecebf3a8SAndreas Gohr        $ret = array($ext, substr($mtypes[$ext],1), true);
26ecebf3a8SAndreas Gohr    }else{
27ecebf3a8SAndreas Gohr        $ret = array($ext, $mtypes[$ext], false);
28ecebf3a8SAndreas Gohr    }
29b625487dSandi  }
30b625487dSandi
31b625487dSandi  return $ret;
32b625487dSandi}
33b625487dSandi
34b625487dSandi/**
35b625487dSandi * returns a hash of mimetypes
36b625487dSandi *
37b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
38b625487dSandi */
39b625487dSandifunction getMimeTypes() {
40b625487dSandi  static $mime = NULL;
41b625487dSandi  if ( !$mime ) {
42cb043f52SChris Smith    $mime = retrieveConfig('mime','confToHash');
43b625487dSandi  }
44b625487dSandi  return $mime;
45b625487dSandi}
46b625487dSandi
47b625487dSandi/**
48b625487dSandi * returns a hash of acronyms
49b625487dSandi *
50b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
51b625487dSandi */
52b625487dSandifunction getAcronyms() {
53b625487dSandi  static $acronyms = NULL;
54b625487dSandi  if ( !$acronyms ) {
55cb043f52SChris Smith    $acronyms = retrieveConfig('acronyms','confToHash');
56b625487dSandi  }
57b625487dSandi  return $acronyms;
58b625487dSandi}
59b625487dSandi
60b625487dSandi/**
61b625487dSandi * returns a hash of smileys
62b625487dSandi *
63b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
64b625487dSandi */
65b625487dSandifunction getSmileys() {
66b625487dSandi  static $smileys = NULL;
67b625487dSandi  if ( !$smileys ) {
68cb043f52SChris Smith    $smileys = retrieveConfig('smileys','confToHash');
69b625487dSandi  }
70b625487dSandi  return $smileys;
71b625487dSandi}
72b625487dSandi
73b625487dSandi/**
74b625487dSandi * returns a hash of entities
75b625487dSandi *
76b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
77b625487dSandi */
78b625487dSandifunction getEntities() {
79b625487dSandi  static $entities = NULL;
80b625487dSandi  if ( !$entities ) {
81cb043f52SChris Smith    $entities = retrieveConfig('entities','confToHash');
82b625487dSandi  }
83b625487dSandi  return $entities;
84b625487dSandi}
85b625487dSandi
86b625487dSandi/**
87b625487dSandi * returns a hash of interwikilinks
88b625487dSandi *
89b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
90b625487dSandi */
91b625487dSandifunction getInterwiki() {
92b625487dSandi  static $wikis = NULL;
93b625487dSandi  if ( !$wikis ) {
944c6a5eccSAndreas Gohr    $wikis = retrieveConfig('interwiki','confToHash',array(true));
95b625487dSandi  }
9697a3e4e3Sandi  //add sepecial case 'this'
9727a2b085Sandi  $wikis['this'] = DOKU_URL.'{NAME}';
98b625487dSandi  return $wikis;
99b625487dSandi}
100b625487dSandi
101b625487dSandi/**
102b9ac8716Schris * returns array of wordblock patterns
103b9ac8716Schris *
104b9ac8716Schris */
105b9ac8716Schrisfunction getWordblocks() {
106b9ac8716Schris  static $wordblocks = NULL;
107b9ac8716Schris  if ( !$wordblocks ) {
108cb043f52SChris Smith    $wordblocks = retrieveConfig('wordblock','file');
109b9ac8716Schris  }
110b9ac8716Schris  return $wordblocks;
111b9ac8716Schris}
112b9ac8716Schris
113b9ac8716Schris
11436f2d7c1SGina Haeussgefunction getSchemes() {
11536f2d7c1SGina Haeussge  static $schemes = NULL;
11636f2d7c1SGina Haeussge  if ( !$schemes ) {
117cb043f52SChris Smith    $schemes = retrieveConfig('scheme','file');
11836f2d7c1SGina Haeussge  }
11936f2d7c1SGina Haeussge  $schemes = array_map('trim', $schemes);
12036f2d7c1SGina Haeussge  $schemes = preg_replace('/^#.*/', '', $schemes);
12136f2d7c1SGina Haeussge  $schemes = array_filter($schemes);
12236f2d7c1SGina Haeussge  return $schemes;
12336f2d7c1SGina Haeussge}
12436f2d7c1SGina Haeussge
125b9ac8716Schris/**
126*edcb01e5SGina Haeussge * Builds a hash from an array of lines
127b625487dSandi *
1283fd0b676Sandi * If $lower is set to true all hash keys are converted to
1293fd0b676Sandi * lower case.
1303fd0b676Sandi *
131b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1323fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
133*edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
134b625487dSandi */
135*edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) {
136b625487dSandi  foreach ( $lines as $line ) {
13703ff8795SAndreas Gohr    //ignore comments (except escaped ones)
13803ff8795SAndreas Gohr    $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
13903ff8795SAndreas Gohr    $line = str_replace('\\#','#',$line);
140b625487dSandi    $line = trim($line);
141b625487dSandi    if(empty($line)) continue;
142b625487dSandi    $line = preg_split('/\s+/',$line,2);
143b625487dSandi    // Build the associative array
14427a2b085Sandi    if($lower){
14527a2b085Sandi      $conf[strtolower($line[0])] = $line[1];
14627a2b085Sandi    }else{
147b625487dSandi      $conf[$line[0]] = $line[1];
148b625487dSandi    }
14927a2b085Sandi  }
150b625487dSandi
151b625487dSandi  return $conf;
152b625487dSandi}
153b625487dSandi
154409d7af7SAndreas Gohr/**
155*edcb01e5SGina Haeussge * Builds a hash from a configfile
156*edcb01e5SGina Haeussge *
157*edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
158*edcb01e5SGina Haeussge * lower case.
159*edcb01e5SGina Haeussge *
160*edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
161*edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
162*edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
163*edcb01e5SGina Haeussge */
164*edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) {
165*edcb01e5SGina Haeussge  $conf = array();
166*edcb01e5SGina Haeussge  $lines = @file( $file );
167*edcb01e5SGina Haeussge  if ( !$lines ) return $conf;
168*edcb01e5SGina Haeussge
169*edcb01e5SGina Haeussge  return linesToHash($lines, $lower);
170*edcb01e5SGina Haeussge}
171*edcb01e5SGina Haeussge
172*edcb01e5SGina Haeussge/**
173cb043f52SChris Smith * Retrieve the requested configuration information
174cb043f52SChris Smith *
175cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
176cb043f52SChris Smith *
177cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
178cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
1794c6a5eccSAndreas Gohr * @param  array    $param    optional additional params to pass to the callback
180cb043f52SChris Smith * @return array    configuration values
181cb043f52SChris Smith */
1824c6a5eccSAndreas Gohrfunction retrieveConfig($type,$fn,$params=null) {
183cb043f52SChris Smith  global $config_cascade;
184cb043f52SChris Smith
1854c6a5eccSAndreas Gohr  if(!is_array($params)) $params = array();
1864c6a5eccSAndreas Gohr
187cb043f52SChris Smith  $combined = array();
188cb043f52SChris Smith  if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
189b303b92cSChris Smith  foreach (array('default','local','protected') as $config_group) {
190b303b92cSChris Smith    if (empty($config_cascade[$type][$config_group])) continue;
191b303b92cSChris Smith    foreach ($config_cascade[$type][$config_group] as $file) {
192cb043f52SChris Smith      if (@file_exists($file)) {
1934c6a5eccSAndreas Gohr        $config = call_user_func_array($fn,array_merge(array($file),$params));
194cb043f52SChris Smith        $combined = array_merge($combined, $config);
195cb043f52SChris Smith      }
196cb043f52SChris Smith    }
197b303b92cSChris Smith  }
198cb043f52SChris Smith
199cb043f52SChris Smith  return $combined;
200cb043f52SChris Smith}
201cb043f52SChris Smith
202cb043f52SChris Smith/**
203f8121585SChris Smith * Include the requested configuration information
204f8121585SChris Smith *
205f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
206f8121585SChris Smith *
207f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
208f8121585SChris Smith * @return array              list of files, default before local before protected
209f8121585SChris Smith */
210f8121585SChris Smithfunction getConfigFiles($type) {
211f8121585SChris Smith  global $config_cascade;
212f8121585SChris Smith  $files = array();
213f8121585SChris Smith
214f8121585SChris Smith  if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
215f8121585SChris Smith  foreach (array('default','local','protected') as $config_group) {
216f8121585SChris Smith    if (empty($config_cascade[$type][$config_group])) continue;
217f8121585SChris Smith    $files = array_merge($files, $config_cascade[$type][$config_group]);
218f8121585SChris Smith  }
219f8121585SChris Smith
220f8121585SChris Smith  return $files;
221f8121585SChris Smith}
222f8121585SChris Smith
223f8121585SChris Smith/**
224409d7af7SAndreas Gohr * check if the given action was disabled in config
225409d7af7SAndreas Gohr *
226409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
227409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
228409d7af7SAndreas Gohr */
229409d7af7SAndreas Gohrfunction actionOK($action){
230409d7af7SAndreas Gohr  static $disabled = null;
231409d7af7SAndreas Gohr  if(is_null($disabled)){
232409d7af7SAndreas Gohr    global $conf;
233409d7af7SAndreas Gohr
234409d7af7SAndreas Gohr    // prepare disabled actions array and handle legacy options
235409d7af7SAndreas Gohr    $disabled = explode(',',$conf['disableactions']);
236409d7af7SAndreas Gohr    $disabled = array_map('trim',$disabled);
237409d7af7SAndreas Gohr    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
238409d7af7SAndreas Gohr    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
2398c286148SMichael Klier    if(isset($conf['subscribers']) && !$conf['subscribers']) {
2408c286148SMichael Klier        $disabled[] = 'subscribe';
2418c286148SMichael Klier        $disabled[] = 'subscribens';
2428c286148SMichael Klier    }
243409d7af7SAndreas Gohr    $disabled = array_unique($disabled);
244409d7af7SAndreas Gohr  }
245409d7af7SAndreas Gohr
246409d7af7SAndreas Gohr  return !in_array($action,$disabled);
247409d7af7SAndreas Gohr}
248409d7af7SAndreas Gohr
249fe9ec250SChris Smith/**
250fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
251fe9ec250SChris Smith *
252fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
253fe9ec250SChris Smith *
254fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
255fe9ec250SChris Smith *                                                      navigation applies to all other links
256fe9ec250SChris Smith * @returns boolean             true if headings should be used for $linktype, false otherwise
257fe9ec250SChris Smith */
258fe9ec250SChris Smithfunction useHeading($linktype) {
259fe9ec250SChris Smith  static $useHeading = null;
260fe9ec250SChris Smith
261fe9ec250SChris Smith  if (is_null($useHeading)) {
262fe9ec250SChris Smith    global $conf;
263fe9ec250SChris Smith
264fe9ec250SChris Smith    if (!empty($conf['useheading'])) {
265fe9ec250SChris Smith      switch ($conf['useheading']) {
266fe9ec250SChris Smith        case 'content'    : $useHeading['content'] = true; break;
267fe9ec250SChris Smith        case 'navigation' : $useHeading['navigation'] = true; break;
268fe9ec250SChris Smith        default:
269fe9ec250SChris Smith          $useHeading['content'] = true;
270fe9ec250SChris Smith          $useHeading['navigation'] = true;
271fe9ec250SChris Smith      }
272fe9ec250SChris Smith    } else {
273fe9ec250SChris Smith      $useHeading = array();
274fe9ec250SChris Smith    }
275fe9ec250SChris Smith  }
276fe9ec250SChris Smith
277fe9ec250SChris Smith  return (!empty($useHeading[$linktype]));
278fe9ec250SChris Smith}
279fe9ec250SChris Smith
2803994772aSChris Smith/**
2813994772aSChris Smith * obscure config data so information isn't plain text
2823994772aSChris Smith *
2833994772aSChris Smith * @param string       $str     data to be encoded
2843994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
2853994772aSChris Smith * @return string               the encoded value
2863994772aSChris Smith */
2873994772aSChris Smithfunction conf_encodeString($str,$code) {
2883994772aSChris Smith  switch ($code) {
2893994772aSChris Smith    case 'base64'   : return '<b>'.base64_encode($str);
2903994772aSChris Smith    case 'uuencode' : return '<u>'.convert_uuencode($str);
2913994772aSChris Smith    case 'plain':
2923994772aSChris Smith    default:
2933994772aSChris Smith      return $str;
2943994772aSChris Smith  }
2953994772aSChris Smith}
2963994772aSChris Smith/**
2973994772aSChris Smith * return obscured data as plain text
2983994772aSChris Smith *
2993994772aSChris Smith * @param  string      $str   encoded data
3003994772aSChris Smith * @return string             plain text
3013994772aSChris Smith */
3023994772aSChris Smithfunction conf_decodeString($str) {
3033994772aSChris Smith  switch (substr($str,0,3)) {
3043994772aSChris Smith    case '<b>' : return base64_decode(substr($str,3));
3053994772aSChris Smith    case '<u>' : return convert_uudecode(substr($str,3));
3063994772aSChris Smith    default:  // not encode (or unknown)
3073994772aSChris Smith      return $str;
3083994772aSChris Smith  }
3093994772aSChris Smith}
310b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
311