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