xref: /dokuwiki/inc/confutils.php (revision 4dc44bc35045455e2d2d2773e986705ff672c868)
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 = confToHash(DOKU_CONF.'mime.conf');
43    if (@file_exists(DOKU_CONF.'mime.local.conf')) {
44      $local = confToHash(DOKU_CONF.'mime.local.conf');
45      $mime = array_merge($mime, $local);
46    }
47  }
48  return $mime;
49}
50
51/**
52 * returns a hash of acronyms
53 *
54 * @author Harry Fuecks <hfuecks@gmail.com>
55 */
56function getAcronyms() {
57  static $acronyms = NULL;
58  if ( !$acronyms ) {
59    $acronyms = confToHash(DOKU_CONF.'acronyms.conf');
60    if (@file_exists(DOKU_CONF.'acronyms.local.conf')) {
61      $local = confToHash(DOKU_CONF.'acronyms.local.conf');
62      $acronyms = array_merge($acronyms, $local);
63    }
64  }
65  return $acronyms;
66}
67
68/**
69 * returns a hash of smileys
70 *
71 * @author Harry Fuecks <hfuecks@gmail.com>
72 */
73function getSmileys() {
74  static $smileys = NULL;
75  if ( !$smileys ) {
76    $smileys = confToHash(DOKU_CONF.'smileys.conf');
77    if (@file_exists(DOKU_CONF.'smileys.local.conf')) {
78      $local = confToHash(DOKU_CONF.'smileys.local.conf');
79      $smileys = array_merge($smileys, $local);
80    }
81  }
82  return $smileys;
83}
84
85/**
86 * returns a hash of entities
87 *
88 * @author Harry Fuecks <hfuecks@gmail.com>
89 */
90function getEntities() {
91  static $entities = NULL;
92  if ( !$entities ) {
93    $entities = confToHash(DOKU_CONF.'entities.conf');
94    if (@file_exists(DOKU_CONF.'entities.local.conf')) {
95      $local = confToHash(DOKU_CONF.'entities.local.conf');
96      $entities = array_merge($entities, $local);
97    }
98  }
99  return $entities;
100}
101
102/**
103 * returns a hash of interwikilinks
104 *
105 * @author Harry Fuecks <hfuecks@gmail.com>
106 */
107function getInterwiki() {
108  static $wikis = NULL;
109  if ( !$wikis ) {
110    $wikis = confToHash(DOKU_CONF.'interwiki.conf',true);
111    if (@file_exists(DOKU_CONF.'interwiki.local.conf')) {
112      $local = confToHash(DOKU_CONF.'interwiki.local.conf');
113      $wikis = array_merge($wikis, $local);
114    }
115  }
116  //add sepecial case 'this'
117  $wikis['this'] = DOKU_URL.'{NAME}';
118  return $wikis;
119}
120
121/**
122 * returns array of wordblock patterns
123 *
124 */
125function getWordblocks() {
126  static $wordblocks = NULL;
127  if ( !$wordblocks ) {
128    $wordblocks = file(DOKU_CONF.'wordblock.conf');
129    if (@file_exists(DOKU_CONF.'wordblock.local.conf')) {
130      $local = file(DOKU_CONF.'wordblock.local.conf');
131      $wordblocks = array_merge($wordblocks, $local);
132    }
133  }
134  return $wordblocks;
135}
136
137
138function getSchemes() {
139  static $schemes = NULL;
140  if ( !$schemes ) {
141    $schemes = file(DOKU_CONF.'scheme.conf');
142    if (@file_exists(DOKU_CONF.'scheme.local.conf')) {
143      $local = file(DOKU_CONF.'scheme.local.conf');
144      $schemes = array_merge($schemes, $local);
145    }
146  }
147  $schemes = array_map('trim', $schemes);
148  $schemes = preg_replace('/^#.*/', '', $schemes);
149  $schemes = array_filter($schemes);
150  return $schemes;
151}
152
153/**
154 * Builds a hash from a configfile
155 *
156 * If $lower is set to true all hash keys are converted to
157 * lower case.
158 *
159 * @author Harry Fuecks <hfuecks@gmail.com>
160 * @author Andreas Gohr <andi@splitbrain.org>
161 */
162function confToHash($file,$lower=false) {
163  $conf = array();
164  $lines = @file( $file );
165  if ( !$lines ) return $conf;
166
167  foreach ( $lines as $line ) {
168    //ignore comments (except escaped ones)
169    $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
170    $line = str_replace('\\#','#',$line);
171    $line = trim($line);
172    if(empty($line)) continue;
173    $line = preg_split('/\s+/',$line,2);
174    // Build the associative array
175    if($lower){
176      $conf[strtolower($line[0])] = $line[1];
177    }else{
178      $conf[$line[0]] = $line[1];
179    }
180  }
181
182  return $conf;
183}
184
185/**
186 * check if the given action was disabled in config
187 *
188 * @author Andreas Gohr <andi@splitbrain.org>
189 * @returns boolean true if enabled, false if disabled
190 */
191function actionOK($action){
192  static $disabled = null;
193  if(is_null($disabled)){
194    global $conf;
195
196    // prepare disabled actions array and handle legacy options
197    $disabled = explode(',',$conf['disableactions']);
198    $disabled = array_map('trim',$disabled);
199    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
200    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
201    if(isset($conf['subscribers']) && !$conf['subscribers']) {
202        $disabled[] = 'subscribe';
203        $disabled[] = 'subscribens';
204    }
205    $disabled = array_unique($disabled);
206  }
207
208  return !in_array($action,$disabled);
209}
210
211/**
212 * check if headings should be used as link text for the specified link type
213 *
214 * @author Chris Smith <chris@jalakai.co.uk>
215 *
216 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
217 *                                                      navigation applies to all other links
218 * @returns boolean             true if headings should be used for $linktype, false otherwise
219 */
220function useHeading($linktype) {
221  static $useHeading = null;
222
223  if (is_null($useHeading)) {
224    global $conf;
225
226    if (!empty($conf['useheading'])) {
227      switch ($conf['useheading']) {
228        case 'content'    : $useHeading['content'] = true; break;
229        case 'navigation' : $useHeading['navigation'] = true; break;
230        default:
231          $useHeading['content'] = true;
232          $useHeading['navigation'] = true;
233      }
234    } else {
235      $useHeading = array();
236    }
237  }
238
239  return (!empty($useHeading[$linktype]));
240}
241
242
243//Setup VIM: ex: et ts=2 enc=utf-8 :
244