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