xref: /dokuwiki/inc/confutils.php (revision ecebf3a8627f527cbd184b7907dd91f65764617b)
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
900976812SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
10b625487dSandi
11b625487dSandi/**
12b625487dSandi * Returns the (known) extension and mimetype of a given filename
13b625487dSandi *
14b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
15b625487dSandi */
16b625487dSandifunction mimetype($file){
17*ecebf3a8SAndreas Gohr  $ret    = array(false,false,false); // return array
18b625487dSandi  $mtypes = getMimeTypes();     // known mimetypes
19b625487dSandi  $exts   = join('|',array_keys($mtypes));  // known extensions (regexp)
20b625487dSandi  if(preg_match('#\.('.$exts.')$#i',$file,$matches)){
21b625487dSandi    $ext = strtolower($matches[1]);
22b625487dSandi  }
23b625487dSandi
24b625487dSandi  if($ext && $mtypes[$ext]){
25*ecebf3a8SAndreas Gohr    if($mtypes[$ext][0] == '!'){
26*ecebf3a8SAndreas Gohr        $ret = array($ext, substr($mtypes[$ext],1), true);
27*ecebf3a8SAndreas Gohr    }else{
28*ecebf3a8SAndreas Gohr        $ret = array($ext, $mtypes[$ext], false);
29*ecebf3a8SAndreas Gohr    }
30b625487dSandi  }
31b625487dSandi
32b625487dSandi  return $ret;
33b625487dSandi}
34b625487dSandi
35b625487dSandi/**
36b625487dSandi * returns a hash of mimetypes
37b625487dSandi *
38b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
39b625487dSandi */
40b625487dSandifunction getMimeTypes() {
41b625487dSandi  static $mime = NULL;
42b625487dSandi  if ( !$mime ) {
43e7cb32dcSAndreas Gohr    $mime = confToHash(DOKU_CONF.'mime.conf');
44e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'mime.local.conf')) {
45e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'mime.local.conf');
464a9a52beSAndreas Gohr      $mime = array_merge($mime, $local);
474a9a52beSAndreas Gohr    }
48b625487dSandi  }
49b625487dSandi  return $mime;
50b625487dSandi}
51b625487dSandi
52b625487dSandi/**
53b625487dSandi * returns a hash of acronyms
54b625487dSandi *
55b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
56b625487dSandi */
57b625487dSandifunction getAcronyms() {
58b625487dSandi  static $acronyms = NULL;
59b625487dSandi  if ( !$acronyms ) {
60e7cb32dcSAndreas Gohr    $acronyms = confToHash(DOKU_CONF.'acronyms.conf');
61e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'acronyms.local.conf')) {
62e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'acronyms.local.conf');
63d2ee49ceSSteven Danz      $acronyms = array_merge($acronyms, $local);
64d2ee49ceSSteven Danz    }
65b625487dSandi  }
66b625487dSandi  return $acronyms;
67b625487dSandi}
68b625487dSandi
69b625487dSandi/**
70b625487dSandi * returns a hash of smileys
71b625487dSandi *
72b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
73b625487dSandi */
74b625487dSandifunction getSmileys() {
75b625487dSandi  static $smileys = NULL;
76b625487dSandi  if ( !$smileys ) {
77e7cb32dcSAndreas Gohr    $smileys = confToHash(DOKU_CONF.'smileys.conf');
78e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'smileys.local.conf')) {
79e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'smileys.local.conf');
804a9a52beSAndreas Gohr      $smileys = array_merge($smileys, $local);
814a9a52beSAndreas Gohr    }
82b625487dSandi  }
83b625487dSandi  return $smileys;
84b625487dSandi}
85b625487dSandi
86b625487dSandi/**
87b625487dSandi * returns a hash of entities
88b625487dSandi *
89b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
90b625487dSandi */
91b625487dSandifunction getEntities() {
92b625487dSandi  static $entities = NULL;
93b625487dSandi  if ( !$entities ) {
94e7cb32dcSAndreas Gohr    $entities = confToHash(DOKU_CONF.'entities.conf');
95e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'entities.local.conf')) {
96e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'entities.local.conf');
974a9a52beSAndreas Gohr      $entities = array_merge($entities, $local);
984a9a52beSAndreas Gohr    }
99b625487dSandi  }
100b625487dSandi  return $entities;
101b625487dSandi}
102b625487dSandi
103b625487dSandi/**
104b625487dSandi * returns a hash of interwikilinks
105b625487dSandi *
106b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
107b625487dSandi */
108b625487dSandifunction getInterwiki() {
109b625487dSandi  static $wikis = NULL;
110b625487dSandi  if ( !$wikis ) {
111e7cb32dcSAndreas Gohr    $wikis = confToHash(DOKU_CONF.'interwiki.conf',true);
112e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'interwiki.local.conf')) {
113e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'interwiki.local.conf');
1144a9a52beSAndreas Gohr      $wikis = array_merge($wikis, $local);
1154a9a52beSAndreas Gohr    }
116b625487dSandi  }
11797a3e4e3Sandi  //add sepecial case 'this'
11827a2b085Sandi  $wikis['this'] = DOKU_URL.'{NAME}';
119b625487dSandi  return $wikis;
120b625487dSandi}
121b625487dSandi
122b625487dSandi/**
123b9ac8716Schris * returns array of wordblock patterns
124b9ac8716Schris *
125b9ac8716Schris */
126b9ac8716Schrisfunction getWordblocks() {
127b9ac8716Schris  static $wordblocks = NULL;
128b9ac8716Schris  if ( !$wordblocks ) {
129b9ac8716Schris    $wordblocks = file(DOKU_CONF.'wordblock.conf');
130b9ac8716Schris    if (@file_exists(DOKU_CONF.'wordblock.local.conf')) {
131b9ac8716Schris      $local = file(DOKU_CONF.'wordblock.local.conf');
132b9ac8716Schris      $wordblocks = array_merge($wordblocks, $local);
133b9ac8716Schris    }
134b9ac8716Schris  }
135b9ac8716Schris  return $wordblocks;
136b9ac8716Schris}
137b9ac8716Schris
138b9ac8716Schris
13936f2d7c1SGina Haeussgefunction getSchemes() {
14036f2d7c1SGina Haeussge  static $schemes = NULL;
14136f2d7c1SGina Haeussge  if ( !$schemes ) {
14236f2d7c1SGina Haeussge    $schemes = file(DOKU_CONF.'scheme.conf');
14336f2d7c1SGina Haeussge    if (@file_exists(DOKU_CONF.'scheme.local.conf')) {
14436f2d7c1SGina Haeussge      $local = file(DOKU_CONF.'scheme.local.conf');
14536f2d7c1SGina Haeussge      $schemes = array_merge($schemes, $local);
14636f2d7c1SGina Haeussge    }
14736f2d7c1SGina Haeussge  }
14836f2d7c1SGina Haeussge  $schemes = array_map('trim', $schemes);
14936f2d7c1SGina Haeussge  $schemes = preg_replace('/^#.*/', '', $schemes);
15036f2d7c1SGina Haeussge  $schemes = array_filter($schemes);
15136f2d7c1SGina Haeussge  return $schemes;
15236f2d7c1SGina Haeussge}
15336f2d7c1SGina Haeussge
154b9ac8716Schris/**
155b625487dSandi * Builds a hash from a configfile
156b625487dSandi *
1573fd0b676Sandi * If $lower is set to true all hash keys are converted to
1583fd0b676Sandi * lower case.
1593fd0b676Sandi *
160b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1613fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
162b625487dSandi */
16327a2b085Sandifunction confToHash($file,$lower=false) {
164b625487dSandi  $conf = array();
165b625487dSandi  $lines = @file( $file );
166b625487dSandi  if ( !$lines ) return $conf;
167b625487dSandi
168b625487dSandi  foreach ( $lines as $line ) {
16903ff8795SAndreas Gohr    //ignore comments (except escaped ones)
17003ff8795SAndreas Gohr    $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
17103ff8795SAndreas Gohr    $line = str_replace('\\#','#',$line);
172b625487dSandi    $line = trim($line);
173b625487dSandi    if(empty($line)) continue;
174b625487dSandi    $line = preg_split('/\s+/',$line,2);
175b625487dSandi    // Build the associative array
17627a2b085Sandi    if($lower){
17727a2b085Sandi      $conf[strtolower($line[0])] = $line[1];
17827a2b085Sandi    }else{
179b625487dSandi      $conf[$line[0]] = $line[1];
180b625487dSandi    }
18127a2b085Sandi  }
182b625487dSandi
183b625487dSandi  return $conf;
184b625487dSandi}
185b625487dSandi
186409d7af7SAndreas Gohr/**
187409d7af7SAndreas Gohr * check if the given action was disabled in config
188409d7af7SAndreas Gohr *
189409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
190409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
191409d7af7SAndreas Gohr */
192409d7af7SAndreas Gohrfunction actionOK($action){
193409d7af7SAndreas Gohr  static $disabled = null;
194409d7af7SAndreas Gohr  if(is_null($disabled)){
195409d7af7SAndreas Gohr    global $conf;
196409d7af7SAndreas Gohr
197409d7af7SAndreas Gohr    // prepare disabled actions array and handle legacy options
198409d7af7SAndreas Gohr    $disabled = explode(',',$conf['disableactions']);
199409d7af7SAndreas Gohr    $disabled = array_map('trim',$disabled);
200409d7af7SAndreas Gohr    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
201409d7af7SAndreas Gohr    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
2028c286148SMichael Klier    if(isset($conf['subscribers']) && !$conf['subscribers']) {
2038c286148SMichael Klier        $disabled[] = 'subscribe';
2048c286148SMichael Klier        $disabled[] = 'subscribens';
2058c286148SMichael Klier    }
206409d7af7SAndreas Gohr    $disabled = array_unique($disabled);
207409d7af7SAndreas Gohr  }
208409d7af7SAndreas Gohr
209409d7af7SAndreas Gohr  return !in_array($action,$disabled);
210409d7af7SAndreas Gohr}
211409d7af7SAndreas Gohr
212fe9ec250SChris Smith/**
213fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
214fe9ec250SChris Smith *
215fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
216fe9ec250SChris Smith *
217fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
218fe9ec250SChris Smith *                                                      navigation applies to all other links
219fe9ec250SChris Smith * @returns boolean             true if headings should be used for $linktype, false otherwise
220fe9ec250SChris Smith */
221fe9ec250SChris Smithfunction useHeading($linktype) {
222fe9ec250SChris Smith  static $useHeading = null;
223fe9ec250SChris Smith
224fe9ec250SChris Smith  if (is_null($useHeading)) {
225fe9ec250SChris Smith    global $conf;
226fe9ec250SChris Smith
227fe9ec250SChris Smith    if (!empty($conf['useheading'])) {
228fe9ec250SChris Smith      switch ($conf['useheading']) {
229fe9ec250SChris Smith        case 'content'    : $useHeading['content'] = true; break;
230fe9ec250SChris Smith        case 'navigation' : $useHeading['navigation'] = true; break;
231fe9ec250SChris Smith        default:
232fe9ec250SChris Smith          $useHeading['content'] = true;
233fe9ec250SChris Smith          $useHeading['navigation'] = true;
234fe9ec250SChris Smith      }
235fe9ec250SChris Smith    } else {
236fe9ec250SChris Smith      $useHeading = array();
237fe9ec250SChris Smith    }
238fe9ec250SChris Smith  }
239fe9ec250SChris Smith
240fe9ec250SChris Smith  return (!empty($useHeading[$linktype]));
241fe9ec250SChris Smith}
242fe9ec250SChris Smith
243b625487dSandi
244b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
245