PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/confutils.php

https://gitlab.com/michield/dokuwiki
PHP | 342 lines | 192 code | 29 blank | 121 comment | 48 complexity | b3a3bb54133ef67a5363cd63ca1f813d MD5 | raw file
  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. * Returns the (known) extension and mimetype of a given filename
  10. *
  11. * If $knownonly is true (the default), then only known extensions
  12. * are returned.
  13. *
  14. * @author Andreas Gohr <andi@splitbrain.org>
  15. */
  16. function mimetype($file, $knownonly=true){
  17. $mtypes = getMimeTypes(); // known mimetypes
  18. $ext = strrpos($file, '.');
  19. if ($ext === false) {
  20. return array(false, false, false);
  21. }
  22. $ext = strtolower(substr($file, $ext + 1));
  23. if (!isset($mtypes[$ext])){
  24. if ($knownonly) {
  25. return array(false, false, false);
  26. } else {
  27. return array($ext, 'application/octet-stream', true);
  28. }
  29. }
  30. if($mtypes[$ext][0] == '!'){
  31. return array($ext, substr($mtypes[$ext],1), true);
  32. }else{
  33. return array($ext, $mtypes[$ext], false);
  34. }
  35. }
  36. /**
  37. * returns a hash of mimetypes
  38. *
  39. * @author Andreas Gohr <andi@splitbrain.org>
  40. */
  41. function getMimeTypes() {
  42. static $mime = null;
  43. if ( !$mime ) {
  44. $mime = retrieveConfig('mime','confToHash');
  45. }
  46. return $mime;
  47. }
  48. /**
  49. * returns a hash of acronyms
  50. *
  51. * @author Harry Fuecks <hfuecks@gmail.com>
  52. */
  53. function getAcronyms() {
  54. static $acronyms = null;
  55. if ( !$acronyms ) {
  56. $acronyms = retrieveConfig('acronyms','confToHash');
  57. }
  58. return $acronyms;
  59. }
  60. /**
  61. * returns a hash of smileys
  62. *
  63. * @author Harry Fuecks <hfuecks@gmail.com>
  64. */
  65. function getSmileys() {
  66. static $smileys = null;
  67. if ( !$smileys ) {
  68. $smileys = retrieveConfig('smileys','confToHash');
  69. }
  70. return $smileys;
  71. }
  72. /**
  73. * returns a hash of entities
  74. *
  75. * @author Harry Fuecks <hfuecks@gmail.com>
  76. */
  77. function getEntities() {
  78. static $entities = null;
  79. if ( !$entities ) {
  80. $entities = retrieveConfig('entities','confToHash');
  81. }
  82. return $entities;
  83. }
  84. /**
  85. * returns a hash of interwikilinks
  86. *
  87. * @author Harry Fuecks <hfuecks@gmail.com>
  88. */
  89. function getInterwiki() {
  90. static $wikis = null;
  91. if ( !$wikis ) {
  92. $wikis = retrieveConfig('interwiki','confToHash',array(true));
  93. }
  94. //add sepecial case 'this'
  95. $wikis['this'] = DOKU_URL.'{NAME}';
  96. return $wikis;
  97. }
  98. /**
  99. * returns array of wordblock patterns
  100. *
  101. */
  102. function getWordblocks() {
  103. static $wordblocks = null;
  104. if ( !$wordblocks ) {
  105. $wordblocks = retrieveConfig('wordblock','file');
  106. }
  107. return $wordblocks;
  108. }
  109. /**
  110. * Gets the list of configured schemes
  111. *
  112. * @return array the schemes
  113. */
  114. function getSchemes() {
  115. static $schemes = null;
  116. if ( !$schemes ) {
  117. $schemes = retrieveConfig('scheme','file');
  118. }
  119. $schemes = array_map('trim', $schemes);
  120. $schemes = preg_replace('/^#.*/', '', $schemes);
  121. $schemes = array_filter($schemes);
  122. return $schemes;
  123. }
  124. /**
  125. * Builds a hash from an array of lines
  126. *
  127. * If $lower is set to true all hash keys are converted to
  128. * lower case.
  129. *
  130. * @author Harry Fuecks <hfuecks@gmail.com>
  131. * @author Andreas Gohr <andi@splitbrain.org>
  132. * @author Gina Haeussge <gina@foosel.net>
  133. */
  134. function linesToHash($lines, $lower=false) {
  135. $conf = array();
  136. // remove BOM
  137. if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf))
  138. $lines[0] = substr($lines[0],3);
  139. foreach ( $lines as $line ) {
  140. //ignore comments (except escaped ones)
  141. $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
  142. $line = str_replace('\\#','#',$line);
  143. $line = trim($line);
  144. if(empty($line)) continue;
  145. $line = preg_split('/\s+/',$line,2);
  146. // Build the associative array
  147. if($lower){
  148. $conf[strtolower($line[0])] = $line[1];
  149. }else{
  150. $conf[$line[0]] = $line[1];
  151. }
  152. }
  153. return $conf;
  154. }
  155. /**
  156. * Builds a hash from a configfile
  157. *
  158. * If $lower is set to true all hash keys are converted to
  159. * lower case.
  160. *
  161. * @author Harry Fuecks <hfuecks@gmail.com>
  162. * @author Andreas Gohr <andi@splitbrain.org>
  163. * @author Gina Haeussge <gina@foosel.net>
  164. */
  165. function confToHash($file,$lower=false) {
  166. $conf = array();
  167. $lines = @file( $file );
  168. if ( !$lines ) return $conf;
  169. return linesToHash($lines, $lower);
  170. }
  171. /**
  172. * Retrieve the requested configuration information
  173. *
  174. * @author Chris Smith <chris@jalakai.co.uk>
  175. *
  176. * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
  177. * @param callback $fn the function used to process the configuration file into an array
  178. * @param array $params optional additional params to pass to the callback
  179. * @return array configuration values
  180. */
  181. function retrieveConfig($type,$fn,$params=null) {
  182. global $config_cascade;
  183. if(!is_array($params)) $params = array();
  184. $combined = array();
  185. if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
  186. foreach (array('default','local','protected') as $config_group) {
  187. if (empty($config_cascade[$type][$config_group])) continue;
  188. foreach ($config_cascade[$type][$config_group] as $file) {
  189. if (@file_exists($file)) {
  190. $config = call_user_func_array($fn,array_merge(array($file),$params));
  191. $combined = array_merge($combined, $config);
  192. }
  193. }
  194. }
  195. return $combined;
  196. }
  197. /**
  198. * Include the requested configuration information
  199. *
  200. * @author Chris Smith <chris@jalakai.co.uk>
  201. *
  202. * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
  203. * @return array list of files, default before local before protected
  204. */
  205. function getConfigFiles($type) {
  206. global $config_cascade;
  207. $files = array();
  208. if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
  209. foreach (array('default','local','protected') as $config_group) {
  210. if (empty($config_cascade[$type][$config_group])) continue;
  211. $files = array_merge($files, $config_cascade[$type][$config_group]);
  212. }
  213. return $files;
  214. }
  215. /**
  216. * check if the given action was disabled in config
  217. *
  218. * @author Andreas Gohr <andi@splitbrain.org>
  219. * @returns boolean true if enabled, false if disabled
  220. */
  221. function actionOK($action){
  222. static $disabled = null;
  223. if(is_null($disabled)){
  224. global $conf;
  225. /** @var auth_basic $auth */
  226. global $auth;
  227. // prepare disabled actions array and handle legacy options
  228. $disabled = explode(',',$conf['disableactions']);
  229. $disabled = array_map('trim',$disabled);
  230. if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
  231. $disabled[] = 'register';
  232. }
  233. if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
  234. $disabled[] = 'resendpwd';
  235. }
  236. if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
  237. $disabled[] = 'subscribe';
  238. }
  239. if (is_null($auth) || !$auth->canDo('Profile')) {
  240. $disabled[] = 'profile';
  241. }
  242. if (is_null($auth)) {
  243. $disabled[] = 'login';
  244. }
  245. if (is_null($auth) || !$auth->canDo('logout')) {
  246. $disabled[] = 'logout';
  247. }
  248. $disabled = array_unique($disabled);
  249. }
  250. return !in_array($action,$disabled);
  251. }
  252. /**
  253. * check if headings should be used as link text for the specified link type
  254. *
  255. * @author Chris Smith <chris@jalakai.co.uk>
  256. *
  257. * @param string $linktype 'content'|'navigation', content applies to links in wiki text
  258. * navigation applies to all other links
  259. * @return boolean true if headings should be used for $linktype, false otherwise
  260. */
  261. function useHeading($linktype) {
  262. static $useHeading = null;
  263. if (is_null($useHeading)) {
  264. global $conf;
  265. if (!empty($conf['useheading'])) {
  266. switch ($conf['useheading']) {
  267. case 'content':
  268. $useHeading['content'] = true;
  269. break;
  270. case 'navigation':
  271. $useHeading['navigation'] = true;
  272. break;
  273. default:
  274. $useHeading['content'] = true;
  275. $useHeading['navigation'] = true;
  276. }
  277. } else {
  278. $useHeading = array();
  279. }
  280. }
  281. return (!empty($useHeading[$linktype]));
  282. }
  283. /**
  284. * obscure config data so information isn't plain text
  285. *
  286. * @param string $str data to be encoded
  287. * @param string $code encoding method, values: plain, base64, uuencode.
  288. * @return string the encoded value
  289. */
  290. function conf_encodeString($str,$code) {
  291. switch ($code) {
  292. case 'base64' : return '<b>'.base64_encode($str);
  293. case 'uuencode' : return '<u>'.convert_uuencode($str);
  294. case 'plain':
  295. default:
  296. return $str;
  297. }
  298. }
  299. /**
  300. * return obscured data as plain text
  301. *
  302. * @param string $str encoded data
  303. * @return string plain text
  304. */
  305. function conf_decodeString($str) {
  306. switch (substr($str,0,3)) {
  307. case '<b>' : return base64_decode(substr($str,3));
  308. case '<u>' : return convert_uudecode(substr($str,3));
  309. default: // not encode (or unknown)
  310. return $str;
  311. }
  312. }
  313. //Setup VIM: ex: et ts=4 :