xref: /dokuwiki/inc/confutils.php (revision f8121585ae97890245b1969cb62fbef583462b7d)
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 ) {
94cb043f52SChris Smith    $wikis = retrieveConfig('interwiki','confToHash');
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
164cb043f52SChris Smith * @return array    configuration values
165cb043f52SChris Smith */
166cb043f52SChris Smithfunction retrieveConfig($type,$fn) {
167cb043f52SChris Smith  global $config_cascade;
168cb043f52SChris Smith
169cb043f52SChris Smith  $combined = array();
170cb043f52SChris Smith  if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
171b303b92cSChris Smith  foreach (array('default','local','protected') as $config_group) {
172b303b92cSChris Smith    if (empty($config_cascade[$type][$config_group])) continue;
173b303b92cSChris Smith    foreach ($config_cascade[$type][$config_group] as $file) {
174cb043f52SChris Smith      if (@file_exists($file)) {
175cb043f52SChris Smith        $config = $fn($file);
176cb043f52SChris Smith        $combined = array_merge($combined, $config);
177cb043f52SChris Smith      }
178cb043f52SChris Smith    }
179b303b92cSChris Smith  }
180cb043f52SChris Smith
181cb043f52SChris Smith  return $combined;
182cb043f52SChris Smith}
183cb043f52SChris Smith
184cb043f52SChris Smith/**
185*f8121585SChris Smith * Include the requested configuration information
186*f8121585SChris Smith *
187*f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
188*f8121585SChris Smith *
189*f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
190*f8121585SChris Smith * @return array              list of files, default before local before protected
191*f8121585SChris Smith */
192*f8121585SChris Smithfunction getConfigFiles($type) {
193*f8121585SChris Smith  global $config_cascade;
194*f8121585SChris Smith  $files = array();
195*f8121585SChris Smith
196*f8121585SChris Smith  if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
197*f8121585SChris Smith  foreach (array('default','local','protected') as $config_group) {
198*f8121585SChris Smith    if (empty($config_cascade[$type][$config_group])) continue;
199*f8121585SChris Smith  	$files = array_merge($files, $config_cascade[$type][$config_group]);
200*f8121585SChris Smith  }
201*f8121585SChris Smith
202*f8121585SChris Smith  return $files;
203*f8121585SChris Smith}
204*f8121585SChris Smith
205*f8121585SChris Smith/**
206409d7af7SAndreas Gohr * check if the given action was disabled in config
207409d7af7SAndreas Gohr *
208409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
209409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
210409d7af7SAndreas Gohr */
211409d7af7SAndreas Gohrfunction actionOK($action){
212409d7af7SAndreas Gohr  static $disabled = null;
213409d7af7SAndreas Gohr  if(is_null($disabled)){
214409d7af7SAndreas Gohr    global $conf;
215409d7af7SAndreas Gohr
216409d7af7SAndreas Gohr    // prepare disabled actions array and handle legacy options
217409d7af7SAndreas Gohr    $disabled = explode(',',$conf['disableactions']);
218409d7af7SAndreas Gohr    $disabled = array_map('trim',$disabled);
219409d7af7SAndreas Gohr    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
220409d7af7SAndreas Gohr    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
2218c286148SMichael Klier    if(isset($conf['subscribers']) && !$conf['subscribers']) {
2228c286148SMichael Klier        $disabled[] = 'subscribe';
2238c286148SMichael Klier        $disabled[] = 'subscribens';
2248c286148SMichael Klier    }
225409d7af7SAndreas Gohr    $disabled = array_unique($disabled);
226409d7af7SAndreas Gohr  }
227409d7af7SAndreas Gohr
228409d7af7SAndreas Gohr  return !in_array($action,$disabled);
229409d7af7SAndreas Gohr}
230409d7af7SAndreas Gohr
231fe9ec250SChris Smith/**
232fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
233fe9ec250SChris Smith *
234fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
235fe9ec250SChris Smith *
236fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
237fe9ec250SChris Smith *                                                      navigation applies to all other links
238fe9ec250SChris Smith * @returns boolean             true if headings should be used for $linktype, false otherwise
239fe9ec250SChris Smith */
240fe9ec250SChris Smithfunction useHeading($linktype) {
241fe9ec250SChris Smith  static $useHeading = null;
242fe9ec250SChris Smith
243fe9ec250SChris Smith  if (is_null($useHeading)) {
244fe9ec250SChris Smith    global $conf;
245fe9ec250SChris Smith
246fe9ec250SChris Smith    if (!empty($conf['useheading'])) {
247fe9ec250SChris Smith      switch ($conf['useheading']) {
248fe9ec250SChris Smith        case 'content'    : $useHeading['content'] = true; break;
249fe9ec250SChris Smith        case 'navigation' : $useHeading['navigation'] = true; break;
250fe9ec250SChris Smith        default:
251fe9ec250SChris Smith          $useHeading['content'] = true;
252fe9ec250SChris Smith          $useHeading['navigation'] = true;
253fe9ec250SChris Smith      }
254fe9ec250SChris Smith    } else {
255fe9ec250SChris Smith      $useHeading = array();
256fe9ec250SChris Smith    }
257fe9ec250SChris Smith  }
258fe9ec250SChris Smith
259fe9ec250SChris Smith  return (!empty($useHeading[$linktype]));
260fe9ec250SChris Smith}
261fe9ec250SChris Smith
2623994772aSChris Smith/**
2633994772aSChris Smith * obscure config data so information isn't plain text
2643994772aSChris Smith *
2653994772aSChris Smith * @param string       $str     data to be encoded
2663994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
2673994772aSChris Smith * @return string               the encoded value
2683994772aSChris Smith */
2693994772aSChris Smithfunction conf_encodeString($str,$code) {
2703994772aSChris Smith  switch ($code) {
2713994772aSChris Smith  	case 'base64'   : return '<b>'.base64_encode($str);
2723994772aSChris Smith  	case 'uuencode' : return '<u>'.convert_uuencode($str);
2733994772aSChris Smith  	case 'plain':
2743994772aSChris Smith  	default:
2753994772aSChris Smith  	  return $str;
2763994772aSChris Smith  }
2773994772aSChris Smith}
2783994772aSChris Smith/**
2793994772aSChris Smith * return obscured data as plain text
2803994772aSChris Smith *
2813994772aSChris Smith * @param  string      $str   encoded data
2823994772aSChris Smith * @return string             plain text
2833994772aSChris Smith */
2843994772aSChris Smithfunction conf_decodeString($str) {
2853994772aSChris Smith	switch (substr($str,0,3)) {
2863994772aSChris Smith	  case '<b>' : return base64_decode(substr($str,3));
2873994772aSChris Smith	  case '<u>' : return convert_uudecode(substr($str,3));
2883994772aSChris Smith	  default:  // not encode (or unknown)
2893994772aSChris Smith	  	return $str;
2903994772aSChris Smith	}
2913994772aSChris Smith}
292b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
293