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