xref: /dokuwiki/inc/confutils.php (revision df3678d8c02fdace2a95a6e72bf0ffb2d3323b85)
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 a configfile
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 */
134function confToHash($file,$lower=false) {
135  $conf = array();
136  $lines = @file( $file );
137  if ( !$lines ) return $conf;
138
139  foreach ( $lines as $line ) {
140    //ignore comments (except escaped ones)
141    $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
142    $line = str_replace('\\#','#',$line);
143    $line = trim($line);
144    if(empty($line)) continue;
145    $line = preg_split('/\s+/',$line,2);
146    // Build the associative array
147    if($lower){
148      $conf[strtolower($line[0])] = $line[1];
149    }else{
150      $conf[$line[0]] = $line[1];
151    }
152  }
153
154  return $conf;
155}
156
157/**
158 * Retrieve the requested configuration information
159 *
160 * @author Chris Smith <chris@jalakai.co.uk>
161 *
162 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
163 * @param  callback $fn       the function used to process the configuration file into an array
164 * @param  array    $param    optional additional params to pass to the callback
165 * @return array    configuration values
166 */
167function retrieveConfig($type,$fn,$params=null) {
168  global $config_cascade;
169
170  if(!is_array($params)) $params = array();
171
172  $combined = array();
173  if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
174  foreach (array('default','local','protected') as $config_group) {
175    if (empty($config_cascade[$type][$config_group])) continue;
176    foreach ($config_cascade[$type][$config_group] as $file) {
177      if (@file_exists($file)) {
178        $config = call_user_func_array($fn,array_merge(array($file),$params));
179        $combined = array_merge($combined, $config);
180      }
181    }
182  }
183
184  return $combined;
185}
186
187/**
188 * Include the requested configuration information
189 *
190 * @author Chris Smith <chris@jalakai.co.uk>
191 *
192 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
193 * @return array              list of files, default before local before protected
194 */
195function getConfigFiles($type) {
196  global $config_cascade;
197  $files = array();
198
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    $files = array_merge($files, $config_cascade[$type][$config_group]);
203  }
204
205  return $files;
206}
207
208/**
209 * check if the given action was disabled in config
210 *
211 * @author Andreas Gohr <andi@splitbrain.org>
212 * @returns boolean true if enabled, false if disabled
213 */
214function actionOK($action){
215  static $disabled = null;
216  if(is_null($disabled)){
217    global $conf;
218
219    // prepare disabled actions array and handle legacy options
220    $disabled = explode(',',$conf['disableactions']);
221    $disabled = array_map('trim',$disabled);
222    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
223    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
224    if(isset($conf['subscribers']) && !$conf['subscribers']) {
225        $disabled[] = 'subscribe';
226        $disabled[] = 'subscribens';
227    }
228    $disabled = array_unique($disabled);
229  }
230
231  return !in_array($action,$disabled);
232}
233
234/**
235 * check if headings should be used as link text for the specified link type
236 *
237 * @author Chris Smith <chris@jalakai.co.uk>
238 *
239 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
240 *                                                      navigation applies to all other links
241 * @returns boolean             true if headings should be used for $linktype, false otherwise
242 */
243function useHeading($linktype) {
244  static $useHeading = null;
245
246  if (is_null($useHeading)) {
247    global $conf;
248
249    if (!empty($conf['useheading'])) {
250      switch ($conf['useheading']) {
251        case 'content'    : $useHeading['content'] = true; break;
252        case 'navigation' : $useHeading['navigation'] = true; break;
253        default:
254          $useHeading['content'] = true;
255          $useHeading['navigation'] = true;
256      }
257    } else {
258      $useHeading = array();
259    }
260  }
261
262  return (!empty($useHeading[$linktype]));
263}
264
265/**
266 * obscure config data so information isn't plain text
267 *
268 * @param string       $str     data to be encoded
269 * @param string       $code    encoding method, values: plain, base64, uuencode.
270 * @return string               the encoded value
271 */
272function conf_encodeString($str,$code) {
273  switch ($code) {
274    case 'base64'   : return '<b>'.base64_encode($str);
275    case 'uuencode' : return '<u>'.convert_uuencode($str);
276    case 'plain':
277    default:
278      return $str;
279  }
280}
281/**
282 * return obscured data as plain text
283 *
284 * @param  string      $str   encoded data
285 * @return string             plain text
286 */
287function conf_decodeString($str) {
288  switch (substr($str,0,3)) {
289    case '<b>' : return base64_decode(substr($str,3));
290    case '<u>' : return convert_uudecode(substr($str,3));
291    default:  // not encode (or unknown)
292      return $str;
293  }
294}
295//Setup VIM: ex: et ts=2 enc=utf-8 :
296