xref: /dokuwiki/inc/confutils.php (revision e5e61eb0b5e7f947cb80e305215246ff894cbcd9)
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 * check if the given action was disabled in config
186 *
187 * @author Andreas Gohr <andi@splitbrain.org>
188 * @returns boolean true if enabled, false if disabled
189 */
190function actionOK($action){
191  static $disabled = null;
192  if(is_null($disabled)){
193    global $conf;
194
195    // prepare disabled actions array and handle legacy options
196    $disabled = explode(',',$conf['disableactions']);
197    $disabled = array_map('trim',$disabled);
198    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
199    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
200    if(isset($conf['subscribers']) && !$conf['subscribers']) {
201        $disabled[] = 'subscribe';
202        $disabled[] = 'subscribens';
203    }
204    $disabled = array_unique($disabled);
205  }
206
207  return !in_array($action,$disabled);
208}
209
210/**
211 * check if headings should be used as link text for the specified link type
212 *
213 * @author Chris Smith <chris@jalakai.co.uk>
214 *
215 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
216 *                                                      navigation applies to all other links
217 * @returns boolean             true if headings should be used for $linktype, false otherwise
218 */
219function useHeading($linktype) {
220  static $useHeading = null;
221
222  if (is_null($useHeading)) {
223    global $conf;
224
225    if (!empty($conf['useheading'])) {
226      switch ($conf['useheading']) {
227        case 'content'    : $useHeading['content'] = true; break;
228        case 'navigation' : $useHeading['navigation'] = true; break;
229        default:
230          $useHeading['content'] = true;
231          $useHeading['navigation'] = true;
232      }
233    } else {
234      $useHeading = array();
235    }
236  }
237
238  return (!empty($useHeading[$linktype]));
239}
240
241/**
242 * obscure config data so information isn't plain text
243 *
244 * @param string       $str     data to be encoded
245 * @param string       $code    encoding method, values: plain, base64, uuencode.
246 * @return string               the encoded value
247 */
248function conf_encodeString($str,$code) {
249  switch ($code) {
250  	case 'base64'   : return '<b>'.base64_encode($str);
251  	case 'uuencode' : return '<u>'.convert_uuencode($str);
252  	case 'plain':
253  	default:
254  	  return $str;
255  }
256}
257/**
258 * return obscured data as plain text
259 *
260 * @param  string      $str   encoded data
261 * @return string             plain text
262 */
263function conf_decodeString($str) {
264	switch (substr($str,0,3)) {
265	  case '<b>' : return base64_decode(substr($str,3));
266	  case '<u>' : return convert_uudecode(substr($str,3));
267	  default:  // not encode (or unknown)
268	  	return $str;
269	}
270}
271//Setup VIM: ex: et ts=2 enc=utf-8 :
272