3 * Utilities for collecting data from config files
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Harry Fuecks <hfuecks@gmail.com>
11 * Returns the (known) extension and mimetype of a given filename
13 * If $knownonly is true (the default), then only known extensions
16 * @author Andreas Gohr <andi@splitbrain.org>
18 function mimetype($file, $knownonly=true){
19 $mtypes = getMimeTypes(); // known mimetypes
20 $ext = strrpos($file, '.');
22 return array(false, false, false);
24 $ext = strtolower(substr($file, $ext + 1));
25 if (!isset($mtypes[$ext])){
27 return array(false, false, false);
29 return array($ext, 'application/octet-stream', true);
32 if($mtypes[$ext][0] == '!'){
33 return array($ext, substr($mtypes[$ext],1), true);
35 return array($ext, $mtypes[$ext], false);
40 * returns a hash of mimetypes
42 * @author Andreas Gohr <andi@splitbrain.org>
44 function getMimeTypes() {
47 $mime = retrieveConfig('mime','confToHash');
53 * returns a hash of acronyms
55 * @author Harry Fuecks <hfuecks@gmail.com>
57 function getAcronyms() {
58 static $acronyms = null;
60 $acronyms = retrieveConfig('acronyms','confToHash');
66 * returns a hash of smileys
68 * @author Harry Fuecks <hfuecks@gmail.com>
70 function getSmileys() {
71 static $smileys = null;
73 $smileys = retrieveConfig('smileys','confToHash');
79 * returns a hash of entities
81 * @author Harry Fuecks <hfuecks@gmail.com>
83 function getEntities() {
84 static $entities = null;
86 $entities = retrieveConfig('entities','confToHash');
92 * returns a hash of interwikilinks
94 * @author Harry Fuecks <hfuecks@gmail.com>
96 function getInterwiki() {
99 $wikis = retrieveConfig('interwiki','confToHash',array(true));
101 //add sepecial case 'this'
102 $wikis['this'] = DOKU_URL.'{NAME}';
107 * returns array of wordblock patterns
110 function getWordblocks() {
111 static $wordblocks = null;
112 if ( !$wordblocks ) {
113 $wordblocks = retrieveConfig('wordblock','file');
119 * Gets the list of configured schemes
121 * @return array the schemes
123 function getSchemes() {
124 static $schemes = null;
126 $schemes = retrieveConfig('scheme','file');
128 $schemes = array_map('trim', $schemes);
129 $schemes = preg_replace('/^#.*/', '', $schemes);
130 $schemes = array_filter($schemes);
135 * Builds a hash from an array of lines
137 * If $lower is set to true all hash keys are converted to
140 * @author Harry Fuecks <hfuecks@gmail.com>
141 * @author Andreas Gohr <andi@splitbrain.org>
142 * @author Gina Haeussge <gina@foosel.net>
144 function linesToHash($lines, $lower=false) {
147 if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf))
148 $lines[0] = substr($lines[0],3);
149 foreach ( $lines as $line ) {
150 //ignore comments (except escaped ones)
151 $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
152 $line = str_replace('\\#','#',$line);
154 if(empty($line)) continue;
155 $line = preg_split('/\s+/',$line,2);
156 // Build the associative array
158 $conf[strtolower($line[0])] = $line[1];
160 $conf[$line[0]] = $line[1];
168 * Builds a hash from a configfile
170 * If $lower is set to true all hash keys are converted to
173 * @author Harry Fuecks <hfuecks@gmail.com>
174 * @author Andreas Gohr <andi@splitbrain.org>
175 * @author Gina Haeussge <gina@foosel.net>
177 function confToHash($file,$lower=false) {
179 $lines = @file( $file );
180 if ( !$lines ) return $conf;
182 return linesToHash($lines, $lower);
186 * Retrieve the requested configuration information
188 * @author Chris Smith <chris@jalakai.co.uk>
190 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
191 * @param callback $fn the function used to process the configuration file into an array
192 * @param array $params optional additional params to pass to the callback
193 * @return array configuration values
195 function retrieveConfig($type,$fn,$params=null) {
196 global $config_cascade;
198 if(!is_array($params)) $params = array();
201 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
202 foreach (array('default','local','protected') as $config_group) {
203 if (empty($config_cascade[$type][$config_group])) continue;
204 foreach ($config_cascade[$type][$config_group] as $file) {
205 if (@file_exists($file)) {
206 $config = call_user_func_array($fn,array_merge(array($file),$params));
207 $combined = array_merge($combined, $config);
216 * Include the requested configuration information
218 * @author Chris Smith <chris@jalakai.co.uk>
220 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
221 * @return array list of files, default before local before protected
223 function getConfigFiles($type) {
224 global $config_cascade;
227 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
228 foreach (array('default','local','protected') as $config_group) {
229 if (empty($config_cascade[$type][$config_group])) continue;
230 $files = array_merge($files, $config_cascade[$type][$config_group]);
237 * check if the given action was disabled in config
239 * @author Andreas Gohr <andi@splitbrain.org>
240 * @returns boolean true if enabled, false if disabled
242 function actionOK($action){
243 static $disabled = null;
244 if(is_null($disabled)){
246 /** @var auth_basic $auth */
249 // prepare disabled actions array and handle legacy options
250 $disabled = explode(',',$conf['disableactions']);
251 $disabled = array_map('trim',$disabled);
252 if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
253 $disabled[] = 'register';
255 if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
256 $disabled[] = 'resendpwd';
258 if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
259 $disabled[] = 'subscribe';
261 if (is_null($auth) || !$auth->canDo('Profile')) {
262 $disabled[] = 'profile';
264 if (is_null($auth)) {
265 $disabled[] = 'login';
267 if (is_null($auth) || !$auth->canDo('logout')) {
268 $disabled[] = 'logout';
270 $disabled = array_unique($disabled);
273 return !in_array($action,$disabled);
277 * check if headings should be used as link text for the specified link type
279 * @author Chris Smith <chris@jalakai.co.uk>
281 * @param string $linktype 'content'|'navigation', content applies to links in wiki text
282 * navigation applies to all other links
283 * @return boolean true if headings should be used for $linktype, false otherwise
285 function useHeading($linktype) {
286 static $useHeading = null;
288 if (is_null($useHeading)) {
291 if (!empty($conf['useheading'])) {
292 switch ($conf['useheading']) {
294 $useHeading['content'] = true;
298 $useHeading['navigation'] = true;
301 $useHeading['content'] = true;
302 $useHeading['navigation'] = true;
305 $useHeading = array();
309 return (!empty($useHeading[$linktype]));
313 * obscure config data so information isn't plain text
315 * @param string $str data to be encoded
316 * @param string $code encoding method, values: plain, base64, uuencode.
317 * @return string the encoded value
319 function conf_encodeString($str,$code) {
321 case 'base64' : return '<b>'.base64_encode($str);
322 case 'uuencode' : return '<u>'.convert_uuencode($str);
329 * return obscured data as plain text
331 * @param string $str encoded data
332 * @return string plain text
334 function conf_decodeString($str) {
335 switch (substr($str,0,3)) {
336 case '<b>' : return base64_decode(substr($str,3));
337 case '<u>' : return convert_uudecode(substr($str,3));
338 default: // not encode (or unknown)
342 //Setup VIM: ex: et ts=4 :