xref: /dokuwiki/inc/confutils.php (revision b625487d2258a6f1f875813206adc9a5857dab24)
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');
92  }
93  return $wikis;
94}
95
96/**
97 * Builds a hash from a configfile
98 *
99 * @author Harry Fuecks <hfuecks@gmail.com>
100 */
101function confToHash($file) {
102  $conf = array();
103  $lines = @file( $file );
104  if ( !$lines ) return $conf;
105
106  foreach ( $lines as $line ) {
107    //ignore comments
108    $line = preg_replace('/[^&]?#.*$/','',$line);
109    $line = trim($line);
110    if(empty($line)) continue;
111    $line = preg_split('/\s+/',$line,2);
112    // Build the associative array
113    $conf[$line[0]] = $line[1];
114  }
115
116  return $conf;
117}
118
119
120//Setup VIM: ex: et ts=2 enc=utf-8 :
121