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