xref: /dokuwiki/inc/confutils.php (revision 36df6fa3069d55db3c35724c4f465bde9c50b53a)
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',realpath(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); // 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    $ret = array($ext, $mtypes[$ext]);
26  }
27
28  return $ret;
29}
30
31/**
32 * returns a hash of mimetypes
33 *
34 * @author Andreas Gohr <andi@splitbrain.org>
35 */
36function getMimeTypes() {
37  static $mime = NULL;
38  if ( !$mime ) {
39    $mime = confToHash(DOKU_CONF.'mime.conf');
40    if (@file_exists(DOKU_CONF.'mime.local.conf')) {
41      $local = confToHash(DOKU_CONF.'mime.local.conf');
42      $mime = array_merge($mime, $local);
43    }
44  }
45  return $mime;
46}
47
48/**
49 * returns a hash of acronyms
50 *
51 * @author Harry Fuecks <hfuecks@gmail.com>
52 */
53function getAcronyms() {
54  static $acronyms = NULL;
55  if ( !$acronyms ) {
56    $acronyms = confToHash(DOKU_CONF.'acronyms.conf');
57    if (@file_exists(DOKU_CONF.'acronyms.local.conf')) {
58      $local = confToHash(DOKU_CONF.'acronyms.local.conf');
59      $acronyms = array_merge($acronyms, $local);
60    }
61  }
62  return $acronyms;
63}
64
65/**
66 * returns a hash of smileys
67 *
68 * @author Harry Fuecks <hfuecks@gmail.com>
69 */
70function getSmileys() {
71  static $smileys = NULL;
72  if ( !$smileys ) {
73    $smileys = confToHash(DOKU_CONF.'smileys.conf');
74    if (@file_exists(DOKU_CONF.'smileys.local.conf')) {
75      $local = confToHash(DOKU_CONF.'smileys.local.conf');
76      $smileys = array_merge($smileys, $local);
77    }
78  }
79  return $smileys;
80}
81
82/**
83 * returns a hash of entities
84 *
85 * @author Harry Fuecks <hfuecks@gmail.com>
86 */
87function getEntities() {
88  static $entities = NULL;
89  if ( !$entities ) {
90    $entities = confToHash(DOKU_CONF.'entities.conf');
91    if (@file_exists(DOKU_CONF.'entities.local.conf')) {
92      $local = confToHash(DOKU_CONF.'entities.local.conf');
93      $entities = array_merge($entities, $local);
94    }
95  }
96  return $entities;
97}
98
99/**
100 * returns a hash of interwikilinks
101 *
102 * @author Harry Fuecks <hfuecks@gmail.com>
103 */
104function getInterwiki() {
105  static $wikis = NULL;
106  if ( !$wikis ) {
107    $wikis = confToHash(DOKU_CONF.'interwiki.conf',true);
108    if (@file_exists(DOKU_CONF.'interwiki.local.conf')) {
109      $local = confToHash(DOKU_CONF.'interwiki.local.conf');
110      $wikis = array_merge($wikis, $local);
111    }
112  }
113  //add sepecial case 'this'
114  $wikis['this'] = DOKU_URL.'{NAME}';
115  return $wikis;
116}
117
118/**
119 * Builds a hash from a configfile
120 *
121 * If $lower is set to true all hash keys are converted to
122 * lower case.
123 *
124 * @author Harry Fuecks <hfuecks@gmail.com>
125 * @author Andreas Gohr <andi@splitbrain.org>
126 */
127function confToHash($file,$lower=false) {
128  $conf = array();
129  $lines = @file( $file );
130  if ( !$lines ) return $conf;
131
132  foreach ( $lines as $line ) {
133    //ignore comments
134    $line = preg_replace('/[^&]?#.*$/','',$line);
135    $line = trim($line);
136    if(empty($line)) continue;
137    $line = preg_split('/\s+/',$line,2);
138    // Build the associative array
139    if($lower){
140      $conf[strtolower($line[0])] = $line[1];
141    }else{
142      $conf[$line[0]] = $line[1];
143    }
144  }
145
146  return $conf;
147}
148
149
150//Setup VIM: ex: et ts=2 enc=utf-8 :
151