xref: /dokuwiki/inc/confutils.php (revision 6106ad89147dec3e180931e3cee6c3973aac8150)
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 ($config_cascade[$type] as $file) {
172    if (@file_exists($file)) {
173      $config = $fn($file);
174      $combined = array_merge($combined, $config);
175    }
176  }
177
178  return $combined;
179}
180
181/**
182 * check if the given action was disabled in config
183 *
184 * @author Andreas Gohr <andi@splitbrain.org>
185 * @returns boolean true if enabled, false if disabled
186 */
187function actionOK($action){
188  static $disabled = null;
189  if(is_null($disabled)){
190    global $conf;
191
192    // prepare disabled actions array and handle legacy options
193    $disabled = explode(',',$conf['disableactions']);
194    $disabled = array_map('trim',$disabled);
195    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
196    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
197    if(isset($conf['subscribers']) && !$conf['subscribers']) {
198        $disabled[] = 'subscribe';
199        $disabled[] = 'subscribens';
200    }
201    $disabled = array_unique($disabled);
202  }
203
204  return !in_array($action,$disabled);
205}
206
207/**
208 * check if headings should be used as link text for the specified link type
209 *
210 * @author Chris Smith <chris@jalakai.co.uk>
211 *
212 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
213 *                                                      navigation applies to all other links
214 * @returns boolean             true if headings should be used for $linktype, false otherwise
215 */
216function useHeading($linktype) {
217  static $useHeading = null;
218
219  if (is_null($useHeading)) {
220    global $conf;
221
222    if (!empty($conf['useheading'])) {
223      switch ($conf['useheading']) {
224        case 'content'    : $useHeading['content'] = true; break;
225        case 'navigation' : $useHeading['navigation'] = true; break;
226        default:
227          $useHeading['content'] = true;
228          $useHeading['navigation'] = true;
229      }
230    } else {
231      $useHeading = array();
232    }
233  }
234
235  return (!empty($useHeading[$linktype]));
236}
237
238
239//Setup VIM: ex: et ts=2 enc=utf-8 :
240