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 * returns array of wordblock patterns
120 *
121 */
122function getWordblocks() {
123  static $wordblocks = NULL;
124  if ( !$wordblocks ) {
125    $wordblocks = file(DOKU_CONF.'wordblock.conf');
126    if (@file_exists(DOKU_CONF.'wordblock.local.conf')) {
127      $local = file(DOKU_CONF.'wordblock.local.conf');
128      $wordblocks = array_merge($wordblocks, $local);
129    }
130  }
131  return $wordblocks;
132}
133
134
135/**
136 * Builds a hash from a configfile
137 *
138 * If $lower is set to true all hash keys are converted to
139 * lower case.
140 *
141 * @author Harry Fuecks <hfuecks@gmail.com>
142 * @author Andreas Gohr <andi@splitbrain.org>
143 */
144function confToHash($file,$lower=false) {
145  $conf = array();
146  $lines = @file( $file );
147  if ( !$lines ) return $conf;
148
149  foreach ( $lines as $line ) {
150    //ignore comments
151    $line = preg_replace('/(?<!&)#.*$/','',$line);
152    $line = trim($line);
153    if(empty($line)) continue;
154    $line = preg_split('/\s+/',$line,2);
155    // Build the associative array
156    if($lower){
157      $conf[strtolower($line[0])] = $line[1];
158    }else{
159      $conf[$line[0]] = $line[1];
160    }
161  }
162
163  return $conf;
164}
165
166/**
167 * check if the given action was disabled in config
168 *
169 * @author Andreas Gohr <andi@splitbrain.org>
170 * @returns boolean true if enabled, false if disabled
171 */
172function actionOK($action){
173  static $disabled = null;
174  if(is_null($disabled)){
175    global $conf;
176
177    // prepare disabled actions array and handle legacy options
178    $disabled = explode(',',$conf['disableactions']);
179    $disabled = array_map('trim',$disabled);
180    if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
181    if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
182    $disabled = array_unique($disabled);
183  }
184
185  return !in_array($action,$disabled);
186}
187
188
189//Setup VIM: ex: et ts=2 enc=utf-8 :
190