summaryrefslogtreecommitdiff
path: root/inc/utils.php
blob: 6610c217a9c6294c735d6c466ce4120c81e85aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
 * Utilities for collecting data from config files
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Harry Fuecks <hfuecks@gmail.com>
 */

  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');

/**
 * Returns the (known) extension of a given filename
 *
 * returns false if not a known extension
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function media_extension($file){
  $exts = join('|',array_keys(getMimeTypes()));
  if(preg_match('#\.('.$exts.')$#i',$file,$matches)){
    return strtolower($matches[1]);
  }
  
  return false;
}


/**
 * returns a hash of mimetypes
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function getMimeTypes() {
  static $mime = NULL;
  if ( !$mime ) {
    $mime = confToHash(DOKU_INC . 'conf/mime.conf');
  }
  return $mime;
}

/**
 * returns a hash of acronyms
 *
 * @author Harry Fuecks <hfuecks@gmail.com>
 */
function getAcronyms() {
  static $acronyms = NULL;
  if ( !$acronyms ) {
    $acronyms = confToHash(DOKU_INC . 'conf/acronyms.conf');
  }
  return $acronyms;
}

/**
 * returns a hash of smileys
 *
 * @author Harry Fuecks <hfuecks@gmail.com>
 */
function getSmileys() {
  static $smileys = NULL;
  if ( !$smileys ) {
    $smileys = confToHash(DOKU_INC . 'conf/smileys.conf');
  }
  return $smileys;
}

/**
 * returns a hash of entities
 *
 * @author Harry Fuecks <hfuecks@gmail.com>
 */
function getEntities() {
  static $entities = NULL;
  if ( !$entities ) {
    $entities = confToHash(DOKU_INC . 'conf/entities.conf');
  }
  return $entities;
}

/**
 * returns a hash of interwikilinks
 *
 * @author Harry Fuecks <hfuecks@gmail.com>
 */
function getInterwiki() {
  static $wikis = NULL;
  if ( !$wikis ) {
    $wikis = confToHash(DOKU_INC . 'conf/interwiki.conf');
  }
  return $wikis;
}

/**
 * Builds a hash from a configfile
 *
 * @author Harry Fuecks <hfuecks@gmail.com>
 */
function confToHash($file) {
  $conf = array();
  $lines = @file( $file );
  if ( !$lines ) return $conf;

  foreach ( $lines as $line ) {
    //ignore comments
    $line = preg_replace('/[^&]?#.*$/','',$line);
    $line = trim($line);
    if(empty($line)) continue;
    $line = preg_split('/\s+/',$line,2);
    // Build the associative array
    $conf[$line[0]] = $line[1];
  }
    
  return $conf;
}


//Setup VIM: ex: et ts=2 enc=utf-8 :