xref: /dokuwiki/inc/confutils.php (revision e7cb32dcb57d3ae03544db66367d6aaa02f7ddf8)
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
9b625487dSandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(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 ) {
39*e7cb32dcSAndreas Gohr    $mime = confToHash(DOKU_CONF.'mime.conf');
40*e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'mime.local.conf')) {
41*e7cb32dcSAndreas 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 ) {
56*e7cb32dcSAndreas Gohr    $acronyms = confToHash(DOKU_CONF.'acronyms.conf');
57*e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'acronyms.local.conf')) {
58*e7cb32dcSAndreas 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 ) {
73*e7cb32dcSAndreas Gohr    $smileys = confToHash(DOKU_CONF.'smileys.conf');
74*e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'smileys.local.conf')) {
75*e7cb32dcSAndreas 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 ) {
90*e7cb32dcSAndreas Gohr    $entities = confToHash(DOKU_CONF.'entities.conf');
91*e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'entities.local.conf')) {
92*e7cb32dcSAndreas 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 ) {
107*e7cb32dcSAndreas Gohr    $wikis = confToHash(DOKU_CONF.'interwiki.conf',true);
108*e7cb32dcSAndreas Gohr    if (@file_exists(DOKU_CONF.'interwiki.local.conf')) {
109*e7cb32dcSAndreas 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/**
119b625487dSandi * Builds a hash from a configfile
120b625487dSandi *
1213fd0b676Sandi * If $lower is set to true all hash keys are converted to
1223fd0b676Sandi * lower case.
1233fd0b676Sandi *
124b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
1253fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
126b625487dSandi */
12727a2b085Sandifunction confToHash($file,$lower=false) {
128b625487dSandi  $conf = array();
129b625487dSandi  $lines = @file( $file );
130b625487dSandi  if ( !$lines ) return $conf;
131b625487dSandi
132b625487dSandi  foreach ( $lines as $line ) {
133b625487dSandi    //ignore comments
134b625487dSandi    $line = preg_replace('/[^&]?#.*$/','',$line);
135b625487dSandi    $line = trim($line);
136b625487dSandi    if(empty($line)) continue;
137b625487dSandi    $line = preg_split('/\s+/',$line,2);
138b625487dSandi    // Build the associative array
13927a2b085Sandi    if($lower){
14027a2b085Sandi      $conf[strtolower($line[0])] = $line[1];
14127a2b085Sandi    }else{
142b625487dSandi      $conf[$line[0]] = $line[1];
143b625487dSandi    }
14427a2b085Sandi  }
145b625487dSandi
146b625487dSandi  return $conf;
147b625487dSandi}
148b625487dSandi
149b625487dSandi
150b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
151