xref: /dokuwiki/inc/confutils.php (revision fe9ec250a6558c8352a35b6537cdc30d6c9f5477)
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){
17b625487dSandi  $ret    = array(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]){
25b625487dSandi    $ret = array($ext, $mtypes[$ext]);
26b625487dSandi  }
27b625487dSandi
28b625487dSandi  return $ret;
29b625487dSandi}
30b625487dSandi
31b625487dSandi/**
32b625487dSandi * returns a hash of mimetypes
33b625487dSandi *
34b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
35b625487dSandi */
36b625487dSandifunction getMimeTypes() {
37b625487dSandi  static $mime = NULL;
38b625487dSandi  if ( !$mime ) {
39e7cb32dcSAndreas Gohr    $mime = confToHash(DOKU_CONF.'mime.conf');
40e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'mime.local.conf')) {
41e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'mime.local.conf');
424a9a52beSAndreas Gohr      $mime = array_merge($mime, $local);
434a9a52beSAndreas Gohr    }
44b625487dSandi  }
45b625487dSandi  return $mime;
46b625487dSandi}
47b625487dSandi
48b625487dSandi/**
49b625487dSandi * returns a hash of acronyms
50b625487dSandi *
51b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
52b625487dSandi */
53b625487dSandifunction getAcronyms() {
54b625487dSandi  static $acronyms = NULL;
55b625487dSandi  if ( !$acronyms ) {
56e7cb32dcSAndreas Gohr    $acronyms = confToHash(DOKU_CONF.'acronyms.conf');
57e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'acronyms.local.conf')) {
58e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'acronyms.local.conf');
59d2ee49ceSSteven Danz      $acronyms = array_merge($acronyms, $local);
60d2ee49ceSSteven Danz    }
61b625487dSandi  }
62b625487dSandi  return $acronyms;
63b625487dSandi}
64b625487dSandi
65b625487dSandi/**
66b625487dSandi * returns a hash of smileys
67b625487dSandi *
68b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
69b625487dSandi */
70b625487dSandifunction getSmileys() {
71b625487dSandi  static $smileys = NULL;
72b625487dSandi  if ( !$smileys ) {
73e7cb32dcSAndreas Gohr    $smileys = confToHash(DOKU_CONF.'smileys.conf');
74e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'smileys.local.conf')) {
75e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'smileys.local.conf');
764a9a52beSAndreas Gohr      $smileys = array_merge($smileys, $local);
774a9a52beSAndreas Gohr    }
78b625487dSandi  }
79b625487dSandi  return $smileys;
80b625487dSandi}
81b625487dSandi
82b625487dSandi/**
83b625487dSandi * returns a hash of entities
84b625487dSandi *
85b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
86b625487dSandi */
87b625487dSandifunction getEntities() {
88b625487dSandi  static $entities = NULL;
89b625487dSandi  if ( !$entities ) {
90e7cb32dcSAndreas Gohr    $entities = confToHash(DOKU_CONF.'entities.conf');
91e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'entities.local.conf')) {
92e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'entities.local.conf');
934a9a52beSAndreas Gohr      $entities = array_merge($entities, $local);
944a9a52beSAndreas Gohr    }
95b625487dSandi  }
96b625487dSandi  return $entities;
97b625487dSandi}
98b625487dSandi
99b625487dSandi/**
100b625487dSandi * returns a hash of interwikilinks
101b625487dSandi *
102b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
103b625487dSandi */
104b625487dSandifunction getInterwiki() {
105b625487dSandi  static $wikis = NULL;
106b625487dSandi  if ( !$wikis ) {
107e7cb32dcSAndreas Gohr    $wikis = confToHash(DOKU_CONF.'interwiki.conf',true);
108e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'interwiki.local.conf')) {
109e7cb32dcSAndreas Gohr      $local = confToHash(DOKU_CONF.'interwiki.local.conf');
1104a9a52beSAndreas Gohr      $wikis = array_merge($wikis, $local);
1114a9a52beSAndreas Gohr    }
112b625487dSandi  }
11397a3e4e3Sandi  //add sepecial case 'this'
11427a2b085Sandi  $wikis['this'] = DOKU_URL.'{NAME}';
115b625487dSandi  return $wikis;
116b625487dSandi}
117b625487dSandi
118b625487dSandi/**
119b9ac8716Schris * returns array of wordblock patterns
120b9ac8716Schris *
121b9ac8716Schris */
122b9ac8716Schrisfunction getWordblocks() {
123b9ac8716Schris  static $wordblocks = NULL;
124b9ac8716Schris  if ( !$wordblocks ) {
125b9ac8716Schris    $wordblocks = file(DOKU_CONF.'wordblock.conf');
126b9ac8716Schris    if (@file_exists(DOKU_CONF.'wordblock.local.conf')) {
127b9ac8716Schris      $local = file(DOKU_CONF.'wordblock.local.conf');
128b9ac8716Schris      $wordblocks = array_merge($wordblocks, $local);
129b9ac8716Schris    }
130b9ac8716Schris  }
131b9ac8716Schris  return $wordblocks;
132b9ac8716Schris}
133b9ac8716Schris
134b9ac8716Schris
13536f2d7c1SGina Haeussgefunction getSchemes() {
13636f2d7c1SGina Haeussge  static $schemes = NULL;
13736f2d7c1SGina Haeussge  if ( !$schemes ) {
13836f2d7c1SGina Haeussge    $schemes = file(DOKU_CONF.'scheme.conf');
13936f2d7c1SGina Haeussge    if (@file_exists(DOKU_CONF.'scheme.local.conf')) {
14036f2d7c1SGina Haeussge      $local = file(DOKU_CONF.'scheme.local.conf');
14136f2d7c1SGina Haeussge      $schemes = array_merge($schemes, $local);
14236f2d7c1SGina Haeussge    }
14336f2d7c1SGina Haeussge  }
14436f2d7c1SGina Haeussge  $schemes = array_map('trim', $schemes);
14536f2d7c1SGina Haeussge  $schemes = preg_replace('/^#.*/', '', $schemes);
14636f2d7c1SGina Haeussge  $schemes = array_filter($schemes);
14736f2d7c1SGina Haeussge  return $schemes;
14836f2d7c1SGina Haeussge}
14936f2d7c1SGina Haeussge
150b9ac8716Schris/**
151b625487dSandi * Builds a hash from a configfile
152b625487dSandi *
1533fd0b676Sandi * If $lower is set to true all hash keys are converted to
1543fd0b676Sandi * lower case.
1553fd0b676Sandi *
156b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1573fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
158b625487dSandi */
15927a2b085Sandifunction confToHash($file,$lower=false) {
160b625487dSandi  $conf = array();
161b625487dSandi  $lines = @file( $file );
162b625487dSandi  if ( !$lines ) return $conf;
163b625487dSandi
164b625487dSandi  foreach ( $lines as $line ) {
16503ff8795SAndreas Gohr    //ignore comments (except escaped ones)
16603ff8795SAndreas Gohr    $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
16703ff8795SAndreas Gohr    $line = str_replace('\\#','#',$line);
168b625487dSandi    $line = trim($line);
169b625487dSandi    if(empty($line)) continue;
170b625487dSandi    $line = preg_split('/\s+/',$line,2);
171b625487dSandi    // Build the associative array
17227a2b085Sandi    if($lower){
17327a2b085Sandi      $conf[strtolower($line[0])] = $line[1];
17427a2b085Sandi    }else{
175b625487dSandi      $conf[$line[0]] = $line[1];
176b625487dSandi    }
17727a2b085Sandi  }
178b625487dSandi
179b625487dSandi  return $conf;
180b625487dSandi}
181b625487dSandi
182409d7af7SAndreas Gohr/**
183409d7af7SAndreas Gohr * check if the given action was disabled in config
184409d7af7SAndreas Gohr *
185409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
186409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
187409d7af7SAndreas Gohr */
188409d7af7SAndreas Gohrfunction actionOK($action){
189409d7af7SAndreas Gohr  static $disabled = null;
190409d7af7SAndreas Gohr  if(is_null($disabled)){
191409d7af7SAndreas Gohr    global $conf;
192409d7af7SAndreas Gohr
193409d7af7SAndreas Gohr    // prepare disabled actions array and handle legacy options
194409d7af7SAndreas Gohr    $disabled = explode(',',$conf['disableactions']);
195409d7af7SAndreas Gohr    $disabled = array_map('trim',$disabled);
196409d7af7SAndreas Gohr    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
197409d7af7SAndreas Gohr    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
1988c286148SMichael Klier    if(isset($conf['subscribers']) && !$conf['subscribers']) {
1998c286148SMichael Klier        $disabled[] = 'subscribe';
2008c286148SMichael Klier        $disabled[] = 'subscribens';
2018c286148SMichael Klier    }
202409d7af7SAndreas Gohr    $disabled = array_unique($disabled);
203409d7af7SAndreas Gohr  }
204409d7af7SAndreas Gohr
205409d7af7SAndreas Gohr  return !in_array($action,$disabled);
206409d7af7SAndreas Gohr}
207409d7af7SAndreas Gohr
208*fe9ec250SChris Smith/**
209*fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
210*fe9ec250SChris Smith *
211*fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
212*fe9ec250SChris Smith *
213*fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
214*fe9ec250SChris Smith *                                                      navigation applies to all other links
215*fe9ec250SChris Smith * @returns boolean             true if headings should be used for $linktype, false otherwise
216*fe9ec250SChris Smith */
217*fe9ec250SChris Smithfunction useHeading($linktype) {
218*fe9ec250SChris Smith  static $useHeading = null;
219*fe9ec250SChris Smith
220*fe9ec250SChris Smith  if (is_null($useHeading)) {
221*fe9ec250SChris Smith    global $conf;
222*fe9ec250SChris Smith
223*fe9ec250SChris Smith    if (!empty($conf['useheading'])) {
224*fe9ec250SChris Smith      switch ($conf['useheading']) {
225*fe9ec250SChris Smith        case 'content'    : $useHeading['content'] = true; break;
226*fe9ec250SChris Smith        case 'navigation' : $useHeading['navigation'] = true; break;
227*fe9ec250SChris Smith        default:
228*fe9ec250SChris Smith          $useHeading['content'] = true;
229*fe9ec250SChris Smith          $useHeading['navigation'] = true;
230*fe9ec250SChris Smith      }
231*fe9ec250SChris Smith    } else {
232*fe9ec250SChris Smith      $useHeading = array();
233*fe9ec250SChris Smith    }
234*fe9ec250SChris Smith  }
235*fe9ec250SChris Smith
236*fe9ec250SChris Smith  return (!empty($useHeading[$linktype]));
237*fe9ec250SChris Smith}
238*fe9ec250SChris Smith
239b625487dSandi
240b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
241