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',fullpath(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_CONF.'mime.conf'); 40 if (@file_exists(DOKU_CONF.'mime.local.conf')) { 41 $local = confToHash(DOKU_CONF.'mime.local.conf'); 42 $mime = array_merge($mime, $local); 43 } 44 } 45 return $mime; 46} 47 48/** 49 * returns a hash of acronyms 50 * 51 * @author Harry Fuecks <hfuecks@gmail.com> 52 */ 53function getAcronyms() { 54 static $acronyms = NULL; 55 if ( !$acronyms ) { 56 $acronyms = confToHash(DOKU_CONF.'acronyms.conf'); 57 if (@file_exists(DOKU_CONF.'acronyms.local.conf')) { 58 $local = confToHash(DOKU_CONF.'acronyms.local.conf'); 59 $acronyms = array_merge($acronyms, $local); 60 } 61 } 62 return $acronyms; 63} 64 65/** 66 * returns a hash of smileys 67 * 68 * @author Harry Fuecks <hfuecks@gmail.com> 69 */ 70function getSmileys() { 71 static $smileys = NULL; 72 if ( !$smileys ) { 73 $smileys = confToHash(DOKU_CONF.'smileys.conf'); 74 if (@file_exists(DOKU_CONF.'smileys.local.conf')) { 75 $local = confToHash(DOKU_CONF.'smileys.local.conf'); 76 $smileys = array_merge($smileys, $local); 77 } 78 } 79 return $smileys; 80} 81 82/** 83 * returns a hash of entities 84 * 85 * @author Harry Fuecks <hfuecks@gmail.com> 86 */ 87function getEntities() { 88 static $entities = NULL; 89 if ( !$entities ) { 90 $entities = confToHash(DOKU_CONF.'entities.conf'); 91 if (@file_exists(DOKU_CONF.'entities.local.conf')) { 92 $local = confToHash(DOKU_CONF.'entities.local.conf'); 93 $entities = array_merge($entities, $local); 94 } 95 } 96 return $entities; 97} 98 99/** 100 * returns a hash of interwikilinks 101 * 102 * @author Harry Fuecks <hfuecks@gmail.com> 103 */ 104function getInterwiki() { 105 static $wikis = NULL; 106 if ( !$wikis ) { 107 $wikis = confToHash(DOKU_CONF.'interwiki.conf',true); 108 if (@file_exists(DOKU_CONF.'interwiki.local.conf')) { 109 $local = confToHash(DOKU_CONF.'interwiki.local.conf'); 110 $wikis = array_merge($wikis, $local); 111 } 112 } 113 //add sepecial case 'this' 114 $wikis['this'] = DOKU_URL.'{NAME}'; 115 return $wikis; 116} 117 118/** 119 * returns array of wordblock patterns 120 * 121 */ 122function getWordblocks() { 123 static $wordblocks = NULL; 124 if ( !$wordblocks ) { 125 $wordblocks = file(DOKU_CONF.'wordblock.conf'); 126 if (@file_exists(DOKU_CONF.'wordblock.local.conf')) { 127 $local = file(DOKU_CONF.'wordblock.local.conf'); 128 $wordblocks = array_merge($wordblocks, $local); 129 } 130 } 131 return $wordblocks; 132} 133 134 135function getSchemes() { 136 static $schemes = NULL; 137 if ( !$schemes ) { 138 $schemes = file(DOKU_CONF.'scheme.conf'); 139 if (@file_exists(DOKU_CONF.'scheme.local.conf')) { 140 $local = file(DOKU_CONF.'scheme.local.conf'); 141 $schemes = array_merge($schemes, $local); 142 } 143 } 144 $schemes = array_map('trim', $schemes); 145 $schemes = preg_replace('/^#.*/', '', $schemes); 146 $schemes = array_filter($schemes); 147 return $schemes; 148} 149 150/** 151 * Builds a hash from a configfile 152 * 153 * If $lower is set to true all hash keys are converted to 154 * lower case. 155 * 156 * @author Harry Fuecks <hfuecks@gmail.com> 157 * @author Andreas Gohr <andi@splitbrain.org> 158 */ 159function confToHash($file,$lower=false) { 160 $conf = array(); 161 $lines = @file( $file ); 162 if ( !$lines ) return $conf; 163 164 foreach ( $lines as $line ) { 165 //ignore comments (except escaped ones) 166 $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 167 $line = str_replace('\\#','#',$line); 168 $line = trim($line); 169 if(empty($line)) continue; 170 $line = preg_split('/\s+/',$line,2); 171 // Build the associative array 172 if($lower){ 173 $conf[strtolower($line[0])] = $line[1]; 174 }else{ 175 $conf[$line[0]] = $line[1]; 176 } 177 } 178 179 return $conf; 180} 181 182/** 183 * check if the given action was disabled in config 184 * 185 * @author Andreas Gohr <andi@splitbrain.org> 186 * @returns boolean true if enabled, false if disabled 187 */ 188function actionOK($action){ 189 static $disabled = null; 190 if(is_null($disabled)){ 191 global $conf; 192 193 // prepare disabled actions array and handle legacy options 194 $disabled = explode(',',$conf['disableactions']); 195 $disabled = array_map('trim',$disabled); 196 if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register'; 197 if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd'; 198 if(isset($conf['subscribers']) && !$conf['subscribers']) { 199 $disabled[] = 'subscribe'; 200 $disabled[] = 'subscribens'; 201 } 202 $disabled = array_unique($disabled); 203 } 204 205 return !in_array($action,$disabled); 206} 207 208 209//Setup VIM: ex: et ts=2 enc=utf-8 : 210