xref: /dokuwiki/inc/confutils.php (revision 4c6a5ecc2b270cdbcf9d23697f99fb13f101ffbd)
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 ) {
94*4c6a5eccSAndreas 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/**
126b625487dSandi * Builds a hash from a configfile
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>
133b625487dSandi */
13427a2b085Sandifunction confToHash($file,$lower=false) {
135b625487dSandi  $conf = array();
136b625487dSandi  $lines = @file( $file );
137b625487dSandi  if ( !$lines ) return $conf;
138b625487dSandi
139b625487dSandi  foreach ( $lines as $line ) {
14003ff8795SAndreas Gohr    //ignore comments (except escaped ones)
14103ff8795SAndreas Gohr    $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
14203ff8795SAndreas Gohr    $line = str_replace('\\#','#',$line);
143b625487dSandi    $line = trim($line);
144b625487dSandi    if(empty($line)) continue;
145b625487dSandi    $line = preg_split('/\s+/',$line,2);
146b625487dSandi    // Build the associative array
14727a2b085Sandi    if($lower){
14827a2b085Sandi      $conf[strtolower($line[0])] = $line[1];
14927a2b085Sandi    }else{
150b625487dSandi      $conf[$line[0]] = $line[1];
151b625487dSandi    }
15227a2b085Sandi  }
153b625487dSandi
154b625487dSandi  return $conf;
155b625487dSandi}
156b625487dSandi
157409d7af7SAndreas Gohr/**
158cb043f52SChris Smith * Retrieve the requested configuration information
159cb043f52SChris Smith *
160cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
161cb043f52SChris Smith *
162cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
163cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
164*4c6a5eccSAndreas Gohr * @param  array    $param    optional additional params to pass to the callback
165cb043f52SChris Smith * @return array    configuration values
166cb043f52SChris Smith */
167*4c6a5eccSAndreas Gohrfunction retrieveConfig($type,$fn,$params=null) {
168cb043f52SChris Smith  global $config_cascade;
169cb043f52SChris Smith
170*4c6a5eccSAndreas Gohr  if(!is_array($params)) $params = array();
171*4c6a5eccSAndreas Gohr
172cb043f52SChris Smith  $combined = array();
173cb043f52SChris Smith  if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
174b303b92cSChris Smith  foreach (array('default','local','protected') as $config_group) {
175b303b92cSChris Smith    if (empty($config_cascade[$type][$config_group])) continue;
176b303b92cSChris Smith    foreach ($config_cascade[$type][$config_group] as $file) {
177cb043f52SChris Smith      if (@file_exists($file)) {
178*4c6a5eccSAndreas Gohr        $config = call_user_func_array($fn,array_merge(array($file),$params));
179cb043f52SChris Smith        $combined = array_merge($combined, $config);
180cb043f52SChris Smith      }
181cb043f52SChris Smith    }
182b303b92cSChris Smith  }
183cb043f52SChris Smith
184cb043f52SChris Smith  return $combined;
185cb043f52SChris Smith}
186cb043f52SChris Smith
187cb043f52SChris Smith/**
188f8121585SChris Smith * Include the requested configuration information
189f8121585SChris Smith *
190f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
191f8121585SChris Smith *
192f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
193f8121585SChris Smith * @return array              list of files, default before local before protected
194f8121585SChris Smith */
195f8121585SChris Smithfunction getConfigFiles($type) {
196f8121585SChris Smith  global $config_cascade;
197f8121585SChris Smith  $files = array();
198f8121585SChris Smith
199f8121585SChris Smith  if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
200f8121585SChris Smith  foreach (array('default','local','protected') as $config_group) {
201f8121585SChris Smith    if (empty($config_cascade[$type][$config_group])) continue;
202f8121585SChris Smith    $files = array_merge($files, $config_cascade[$type][$config_group]);
203f8121585SChris Smith  }
204f8121585SChris Smith
205f8121585SChris Smith  return $files;
206f8121585SChris Smith}
207f8121585SChris Smith
208f8121585SChris Smith/**
209409d7af7SAndreas Gohr * check if the given action was disabled in config
210409d7af7SAndreas Gohr *
211409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
212409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
213409d7af7SAndreas Gohr */
214409d7af7SAndreas Gohrfunction actionOK($action){
215409d7af7SAndreas Gohr  static $disabled = null;
216409d7af7SAndreas Gohr  if(is_null($disabled)){
217409d7af7SAndreas Gohr    global $conf;
218409d7af7SAndreas Gohr
219409d7af7SAndreas Gohr    // prepare disabled actions array and handle legacy options
220409d7af7SAndreas Gohr    $disabled = explode(',',$conf['disableactions']);
221409d7af7SAndreas Gohr    $disabled = array_map('trim',$disabled);
222409d7af7SAndreas Gohr    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
223409d7af7SAndreas Gohr    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
2248c286148SMichael Klier    if(isset($conf['subscribers']) && !$conf['subscribers']) {
2258c286148SMichael Klier        $disabled[] = 'subscribe';
2268c286148SMichael Klier        $disabled[] = 'subscribens';
2278c286148SMichael Klier    }
228409d7af7SAndreas Gohr    $disabled = array_unique($disabled);
229409d7af7SAndreas Gohr  }
230409d7af7SAndreas Gohr
231409d7af7SAndreas Gohr  return !in_array($action,$disabled);
232409d7af7SAndreas Gohr}
233409d7af7SAndreas Gohr
234fe9ec250SChris Smith/**
235fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
236fe9ec250SChris Smith *
237fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
238fe9ec250SChris Smith *
239fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
240fe9ec250SChris Smith *                                                      navigation applies to all other links
241fe9ec250SChris Smith * @returns boolean             true if headings should be used for $linktype, false otherwise
242fe9ec250SChris Smith */
243fe9ec250SChris Smithfunction useHeading($linktype) {
244fe9ec250SChris Smith  static $useHeading = null;
245fe9ec250SChris Smith
246fe9ec250SChris Smith  if (is_null($useHeading)) {
247fe9ec250SChris Smith    global $conf;
248fe9ec250SChris Smith
249fe9ec250SChris Smith    if (!empty($conf['useheading'])) {
250fe9ec250SChris Smith      switch ($conf['useheading']) {
251fe9ec250SChris Smith        case 'content'    : $useHeading['content'] = true; break;
252fe9ec250SChris Smith        case 'navigation' : $useHeading['navigation'] = true; break;
253fe9ec250SChris Smith        default:
254fe9ec250SChris Smith          $useHeading['content'] = true;
255fe9ec250SChris Smith          $useHeading['navigation'] = true;
256fe9ec250SChris Smith      }
257fe9ec250SChris Smith    } else {
258fe9ec250SChris Smith      $useHeading = array();
259fe9ec250SChris Smith    }
260fe9ec250SChris Smith  }
261fe9ec250SChris Smith
262fe9ec250SChris Smith  return (!empty($useHeading[$linktype]));
263fe9ec250SChris Smith}
264fe9ec250SChris Smith
2653994772aSChris Smith/**
2663994772aSChris Smith * obscure config data so information isn't plain text
2673994772aSChris Smith *
2683994772aSChris Smith * @param string       $str     data to be encoded
2693994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
2703994772aSChris Smith * @return string               the encoded value
2713994772aSChris Smith */
2723994772aSChris Smithfunction conf_encodeString($str,$code) {
2733994772aSChris Smith  switch ($code) {
2743994772aSChris Smith    case 'base64'   : return '<b>'.base64_encode($str);
2753994772aSChris Smith    case 'uuencode' : return '<u>'.convert_uuencode($str);
2763994772aSChris Smith    case 'plain':
2773994772aSChris Smith    default:
2783994772aSChris Smith      return $str;
2793994772aSChris Smith  }
2803994772aSChris Smith}
2813994772aSChris Smith/**
2823994772aSChris Smith * return obscured data as plain text
2833994772aSChris Smith *
2843994772aSChris Smith * @param  string      $str   encoded data
2853994772aSChris Smith * @return string             plain text
2863994772aSChris Smith */
2873994772aSChris Smithfunction conf_decodeString($str) {
2883994772aSChris Smith  switch (substr($str,0,3)) {
2893994772aSChris Smith    case '<b>' : return base64_decode(substr($str,3));
2903994772aSChris Smith    case '<u>' : return convert_uudecode(substr($str,3));
2913994772aSChris Smith    default:  // not encode (or unknown)
2923994772aSChris Smith      return $str;
2933994772aSChris Smith  }
2943994772aSChris Smith}
295b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
296