You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

503 lines
14 KiB

  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. * line prefix used to negate single value config items
  10. * (scheme.conf & stopwords.conf), e.g.
  11. * !gopher
  12. */
  13. use dokuwiki\Extension\AuthPlugin;
  14. use dokuwiki\Extension\Event;
  15. const DOKU_CONF_NEGATION = '!';
  16. /**
  17. * Returns the (known) extension and mimetype of a given filename
  18. *
  19. * If $knownonly is true (the default), then only known extensions
  20. * are returned.
  21. *
  22. * @author Andreas Gohr <andi@splitbrain.org>
  23. *
  24. * @param string $file file name
  25. * @param bool $knownonly
  26. * @return array with extension, mimetype and if it should be downloaded
  27. */
  28. function mimetype($file, $knownonly = true)
  29. {
  30. $mtypes = getMimeTypes(); // known mimetypes
  31. $ext = strrpos($file, '.');
  32. if ($ext === false) {
  33. return [false, false, false];
  34. }
  35. $ext = strtolower(substr($file, $ext + 1));
  36. if (!isset($mtypes[$ext])) {
  37. if ($knownonly) {
  38. return [false, false, false];
  39. } else {
  40. return [$ext, 'application/octet-stream', true];
  41. }
  42. }
  43. if ($mtypes[$ext][0] == '!') {
  44. return [$ext, substr($mtypes[$ext], 1), true];
  45. } else {
  46. return [$ext, $mtypes[$ext], false];
  47. }
  48. }
  49. /**
  50. * returns a hash of mimetypes
  51. *
  52. * @author Andreas Gohr <andi@splitbrain.org>
  53. */
  54. function getMimeTypes()
  55. {
  56. static $mime = null;
  57. if (!$mime) {
  58. $mime = retrieveConfig('mime', 'confToHash');
  59. $mime = array_filter($mime);
  60. }
  61. return $mime;
  62. }
  63. /**
  64. * returns a hash of acronyms
  65. *
  66. * @author Harry Fuecks <hfuecks@gmail.com>
  67. */
  68. function getAcronyms()
  69. {
  70. static $acronyms = null;
  71. if (!$acronyms) {
  72. $acronyms = retrieveConfig('acronyms', 'confToHash');
  73. $acronyms = array_filter($acronyms, 'strlen');
  74. }
  75. return $acronyms;
  76. }
  77. /**
  78. * returns a hash of smileys
  79. *
  80. * @author Harry Fuecks <hfuecks@gmail.com>
  81. */
  82. function getSmileys()
  83. {
  84. static $smileys = null;
  85. if (!$smileys) {
  86. $smileys = retrieveConfig('smileys', 'confToHash');
  87. $smileys = array_filter($smileys, 'strlen');
  88. }
  89. return $smileys;
  90. }
  91. /**
  92. * returns a hash of entities
  93. *
  94. * @author Harry Fuecks <hfuecks@gmail.com>
  95. */
  96. function getEntities()
  97. {
  98. static $entities = null;
  99. if (!$entities) {
  100. $entities = retrieveConfig('entities', 'confToHash');
  101. $entities = array_filter($entities, 'strlen');
  102. }
  103. return $entities;
  104. }
  105. /**
  106. * returns a hash of interwikilinks
  107. *
  108. * @author Harry Fuecks <hfuecks@gmail.com>
  109. */
  110. function getInterwiki()
  111. {
  112. static $wikis = null;
  113. if (!$wikis) {
  114. $wikis = retrieveConfig('interwiki', 'confToHash', [true]);
  115. $wikis = array_filter($wikis, 'strlen');
  116. //add sepecial case 'this'
  117. $wikis['this'] = DOKU_URL . '{NAME}';
  118. }
  119. return $wikis;
  120. }
  121. /**
  122. * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
  123. *
  124. * @trigger CONFUTIL_CDN_SELECT
  125. * @return array
  126. */
  127. function getCdnUrls()
  128. {
  129. global $conf;
  130. // load version info
  131. $versions = [];
  132. $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
  133. foreach ($lines as $line) {
  134. $line = trim(preg_replace('/#.*$/', '', $line));
  135. if ($line === '') continue;
  136. [$key, $val] = sexplode('=', $line, 2, '');
  137. $key = trim($key);
  138. $val = trim($val);
  139. $versions[$key] = $val;
  140. }
  141. $src = [];
  142. $data = ['versions' => $versions, 'src' => &$src];
  143. $event = new Event('CONFUTIL_CDN_SELECT', $data);
  144. if ($event->advise_before()) {
  145. if (!$conf['jquerycdn']) {
  146. $jqmod = md5(implode('-', $versions));
  147. $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
  148. } elseif ($conf['jquerycdn'] == 'jquery') {
  149. $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
  150. $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
  151. } elseif ($conf['jquerycdn'] == 'cdnjs') {
  152. $src[] = sprintf(
  153. 'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
  154. $versions['JQ_VERSION']
  155. );
  156. $src[] = sprintf(
  157. 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
  158. $versions['JQUI_VERSION']
  159. );
  160. }
  161. }
  162. $event->advise_after();
  163. return $src;
  164. }
  165. /**
  166. * returns array of wordblock patterns
  167. *
  168. */
  169. function getWordblocks()
  170. {
  171. static $wordblocks = null;
  172. if (!$wordblocks) {
  173. $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal');
  174. }
  175. return $wordblocks;
  176. }
  177. /**
  178. * Gets the list of configured schemes
  179. *
  180. * @return array the schemes
  181. */
  182. function getSchemes()
  183. {
  184. static $schemes = null;
  185. if (!$schemes) {
  186. $schemes = retrieveConfig('scheme', 'file', null, 'array_merge_with_removal');
  187. $schemes = array_map('trim', $schemes);
  188. $schemes = preg_replace('/^#.*/', '', $schemes);
  189. $schemes = array_filter($schemes);
  190. }
  191. return $schemes;
  192. }
  193. /**
  194. * Builds a hash from an array of lines
  195. *
  196. * If $lower is set to true all hash keys are converted to
  197. * lower case.
  198. *
  199. * @author Harry Fuecks <hfuecks@gmail.com>
  200. * @author Andreas Gohr <andi@splitbrain.org>
  201. * @author Gina Haeussge <gina@foosel.net>
  202. *
  203. * @param array $lines
  204. * @param bool $lower
  205. *
  206. * @return array
  207. */
  208. function linesToHash($lines, $lower = false)
  209. {
  210. $conf = [];
  211. // remove BOM
  212. if (isset($lines[0]) && str_starts_with($lines[0], pack('CCC', 0xef, 0xbb, 0xbf)))
  213. $lines[0] = substr($lines[0], 3);
  214. foreach ($lines as $line) {
  215. //ignore comments (except escaped ones)
  216. $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
  217. $line = str_replace('\\#', '#', $line);
  218. $line = trim($line);
  219. if ($line === '') continue;
  220. $line = preg_split('/\s+/', $line, 2);
  221. $line = array_pad($line, 2, '');
  222. // Build the associative array
  223. if ($lower) {
  224. $conf[strtolower($line[0])] = $line[1];
  225. } else {
  226. $conf[$line[0]] = $line[1];
  227. }
  228. }
  229. return $conf;
  230. }
  231. /**
  232. * Builds a hash from a configfile
  233. *
  234. * If $lower is set to true all hash keys are converted to
  235. * lower case.
  236. *
  237. * @author Harry Fuecks <hfuecks@gmail.com>
  238. * @author Andreas Gohr <andi@splitbrain.org>
  239. * @author Gina Haeussge <gina@foosel.net>
  240. *
  241. * @param string $file
  242. * @param bool $lower
  243. *
  244. * @return array
  245. */
  246. function confToHash($file, $lower = false)
  247. {
  248. $conf = [];
  249. $lines = @file($file);
  250. if (!$lines) return $conf;
  251. return linesToHash($lines, $lower);
  252. }
  253. /**
  254. * Read a json config file into an array
  255. *
  256. * @param string $file
  257. * @return array
  258. * @throws JsonException
  259. */
  260. function jsonToArray($file)
  261. {
  262. $json = file_get_contents($file);
  263. $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
  264. if ($conf === null) {
  265. return [];
  266. }
  267. return $conf;
  268. }
  269. /**
  270. * Retrieve the requested configuration information
  271. *
  272. * @author Chris Smith <chris@jalakai.co.uk>
  273. *
  274. * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
  275. * @param callback $fn the function used to process the configuration file into an array
  276. * @param array $params optional additional params to pass to the callback
  277. * @param callback $combine the function used to combine arrays of values read from different configuration files;
  278. * the function takes two parameters,
  279. * $combined - the already read & merged configuration values
  280. * $new - array of config values from the config cascade file being currently processed
  281. * and returns an array of the merged configuration values.
  282. * @return array configuration values
  283. */
  284. function retrieveConfig($type, $fn, $params = null, $combine = 'array_merge')
  285. {
  286. global $config_cascade;
  287. if (!is_array($params)) $params = [];
  288. $combined = [];
  289. if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
  290. foreach (['default', 'local', 'protected'] as $config_group) {
  291. if (empty($config_cascade[$type][$config_group])) continue;
  292. foreach ($config_cascade[$type][$config_group] as $file) {
  293. if (file_exists($file)) {
  294. $config = call_user_func_array($fn, array_merge([$file], $params));
  295. $combined = $combine($combined, $config);
  296. }
  297. }
  298. }
  299. return $combined;
  300. }
  301. /**
  302. * Include the requested configuration information
  303. *
  304. * @author Chris Smith <chris@jalakai.co.uk>
  305. *
  306. * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
  307. * @return array list of files, default before local before protected
  308. */
  309. function getConfigFiles($type)
  310. {
  311. global $config_cascade;
  312. $files = [];
  313. if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
  314. foreach (['default', 'local', 'protected'] as $config_group) {
  315. if (empty($config_cascade[$type][$config_group])) continue;
  316. $files = array_merge($files, $config_cascade[$type][$config_group]);
  317. }
  318. return $files;
  319. }
  320. /**
  321. * check if the given action was disabled in config
  322. *
  323. * @author Andreas Gohr <andi@splitbrain.org>
  324. * @param string $action
  325. * @returns boolean true if enabled, false if disabled
  326. */
  327. function actionOK($action)
  328. {
  329. static $disabled = null;
  330. if (is_null($disabled) || defined('SIMPLE_TEST')) {
  331. global $conf;
  332. /** @var AuthPlugin $auth */
  333. global $auth;
  334. // prepare disabled actions array and handle legacy options
  335. $disabled = explode(',', $conf['disableactions']);
  336. $disabled = array_map('trim', $disabled);
  337. if (
  338. (isset($conf['openregister']) && !$conf['openregister']) || !$auth instanceof AuthPlugin
  339. || !$auth->canDo('addUser')
  340. ) {
  341. $disabled[] = 'register';
  342. }
  343. if (
  344. (isset($conf['resendpasswd']) && !$conf['resendpasswd']) || !$auth instanceof AuthPlugin
  345. || !$auth->canDo('modPass')
  346. ) {
  347. $disabled[] = 'resendpwd';
  348. }
  349. if ((isset($conf['subscribers']) && !$conf['subscribers']) || !$auth instanceof AuthPlugin) {
  350. $disabled[] = 'subscribe';
  351. }
  352. if (!$auth instanceof AuthPlugin || !$auth->canDo('Profile')) {
  353. $disabled[] = 'profile';
  354. }
  355. if (!$auth instanceof AuthPlugin || !$auth->canDo('delUser')) {
  356. $disabled[] = 'profile_delete';
  357. }
  358. if (!$auth instanceof AuthPlugin) {
  359. $disabled[] = 'login';
  360. }
  361. if (!$auth instanceof AuthPlugin || !$auth->canDo('logout')) {
  362. $disabled[] = 'logout';
  363. }
  364. $disabled = array_unique($disabled);
  365. }
  366. return !in_array($action, $disabled);
  367. }
  368. /**
  369. * check if headings should be used as link text for the specified link type
  370. *
  371. * @author Chris Smith <chris@jalakai.co.uk>
  372. *
  373. * @param string $linktype 'content'|'navigation', content applies to links in wiki text
  374. * navigation applies to all other links
  375. * @return boolean true if headings should be used for $linktype, false otherwise
  376. */
  377. function useHeading($linktype)
  378. {
  379. static $useHeading = null;
  380. if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
  381. if (is_null($useHeading)) {
  382. global $conf;
  383. if (!empty($conf['useheading'])) {
  384. switch ($conf['useheading']) {
  385. case 'content':
  386. $useHeading['content'] = true;
  387. break;
  388. case 'navigation':
  389. $useHeading['navigation'] = true;
  390. break;
  391. default:
  392. $useHeading['content'] = true;
  393. $useHeading['navigation'] = true;
  394. }
  395. } else {
  396. $useHeading = [];
  397. }
  398. }
  399. return (!empty($useHeading[$linktype]));
  400. }
  401. /**
  402. * obscure config data so information isn't plain text
  403. *
  404. * @param string $str data to be encoded
  405. * @param string $code encoding method, values: plain, base64, uuencode.
  406. * @return string the encoded value
  407. */
  408. function conf_encodeString($str, $code)
  409. {
  410. switch ($code) {
  411. case 'base64':
  412. return '<b>' . base64_encode($str);
  413. case 'uuencode':
  414. return '<u>' . convert_uuencode($str);
  415. case 'plain':
  416. default:
  417. return $str;
  418. }
  419. }
  420. /**
  421. * return obscured data as plain text
  422. *
  423. * @param string $str encoded data
  424. * @return string plain text
  425. */
  426. function conf_decodeString($str)
  427. {
  428. switch (substr($str, 0, 3)) {
  429. case '<b>':
  430. return base64_decode(substr($str, 3));
  431. case '<u>':
  432. return convert_uudecode(substr($str, 3));
  433. default: // not encoded (or unknown)
  434. return $str;
  435. }
  436. }
  437. /**
  438. * array combination function to remove negated values (prefixed by !)
  439. *
  440. * @param array $current
  441. * @param array $new
  442. *
  443. * @return array the combined array, numeric keys reset
  444. */
  445. function array_merge_with_removal($current, $new)
  446. {
  447. foreach ($new as $val) {
  448. if (str_starts_with($val, DOKU_CONF_NEGATION)) {
  449. $idx = array_search(trim(substr($val, 1)), $current);
  450. if ($idx !== false) {
  451. unset($current[$idx]);
  452. }
  453. } else {
  454. $current[] = trim($val);
  455. }
  456. }
  457. return array_slice($current, 0);
  458. }
  459. //Setup VIM: ex: et ts=4 :