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