PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/func.php

https://github.com/miya5n/pukiwiki
PHP | 921 lines | 677 code | 126 blank | 118 comment | 148 complexity | 37a3d1de271f4715567014db8a19b81b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // PukiWiki - Yet another WikiWikiWeb clone.
  3. // $Id: func.php,v 1.104 2011/01/25 15:01:01 henoheno Exp $
  4. // Copyright (C)
  5. // 2002-2007,2009-2010 PukiWiki Developers Team
  6. // 2001-2002 Originally written by yu-ji
  7. // License: GPL v2 or (at your option) any later version
  8. //
  9. // General functions
  10. function is_interwiki($str)
  11. {
  12. global $InterWikiName;
  13. return preg_match('/^' . $InterWikiName . '$/', $str);
  14. }
  15. function is_pagename($str)
  16. {
  17. global $BracketName;
  18. $is_pagename = (! is_interwiki($str) &&
  19. preg_match('/^(?!\/)' . $BracketName . '$(?<!\/$)/', $str) &&
  20. ! preg_match('#(^|/)\.{1,2}(/|$)#', $str));
  21. if (defined('SOURCE_ENCODING')) {
  22. switch(SOURCE_ENCODING){
  23. case 'UTF-8': $pattern =
  24. '/^(?:[\x00-\x7F]|(?:[\xC0-\xDF][\x80-\xBF])|(?:[\xE0-\xEF][\x80-\xBF][\x80-\xBF]))+$/';
  25. break;
  26. case 'EUC-JP': $pattern =
  27. '/^(?:[\x00-\x7F]|(?:[\x8E\xA1-\xFE][\xA1-\xFE])|(?:\x8F[\xA1-\xFE][\xA1-\xFE]))+$/';
  28. break;
  29. }
  30. if (isset($pattern) && $pattern != '')
  31. $is_pagename = ($is_pagename && preg_match($pattern, $str));
  32. }
  33. return $is_pagename;
  34. }
  35. function is_url($str, $only_http = FALSE)
  36. {
  37. $scheme = $only_http ? 'https?' : 'https?|ftp|news';
  38. return preg_match('/^(' . $scheme . ')(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]*)$/', $str);
  39. }
  40. // If the page exists
  41. function is_page($page, $clearcache = FALSE)
  42. {
  43. if ($clearcache) clearstatcache();
  44. return file_exists(get_filename($page));
  45. }
  46. // Handling $cantedit
  47. function is_cantedit($page)
  48. {
  49. global $cantedit;
  50. static $is_cantedit;
  51. if (! isset($is_cantedit)) {
  52. foreach($cantedit as $key) {
  53. $is_cantedit[$key] = TRUE;
  54. }
  55. }
  56. return isset($is_cantedit[$page]);
  57. }
  58. function is_editable($page)
  59. {
  60. static $is_editable = array();
  61. if (! isset($is_editable[$page])) {
  62. $is_editable[$page] = (
  63. is_pagename($page) &&
  64. ! is_freeze($page) &&
  65. ! is_cantedit($page)
  66. );
  67. }
  68. return $is_editable[$page];
  69. }
  70. function is_freeze($page, $clearcache = FALSE)
  71. {
  72. global $function_freeze;
  73. static $is_freeze = array();
  74. if ($clearcache === TRUE) $is_freeze = array();
  75. if (isset($is_freeze[$page])) return $is_freeze[$page];
  76. if (! $function_freeze || ! is_page($page)) {
  77. $is_freeze[$page] = FALSE;
  78. return FALSE;
  79. } else {
  80. $fp = fopen(get_filename($page), 'rb') or
  81. die('is_freeze(): fopen() failed: ' . htmlsc($page));
  82. flock($fp, LOCK_SH) or die('is_freeze(): flock() failed');
  83. rewind($fp);
  84. $buffer = fgets($fp, 9);
  85. flock($fp, LOCK_UN) or die('is_freeze(): flock() failed');
  86. fclose($fp) or die('is_freeze(): fclose() failed: ' . htmlsc($page));
  87. $is_freeze[$page] = ($buffer != FALSE && rtrim($buffer, "\r\n") == '#freeze');
  88. return $is_freeze[$page];
  89. }
  90. }
  91. // Handling $non_list
  92. // $non_list will be preg_quote($str, '/') later.
  93. function check_non_list($page = '')
  94. {
  95. global $non_list;
  96. static $regex;
  97. if (! isset($regex)) $regex = '/' . $non_list . '/';
  98. return preg_match($regex, $page);
  99. }
  100. // Auto template
  101. function auto_template($page)
  102. {
  103. global $auto_template_func, $auto_template_rules;
  104. if (! $auto_template_func) return '';
  105. $body = '';
  106. $matches = array();
  107. foreach ($auto_template_rules as $rule => $template) {
  108. $rule_pattrn = '/' . $rule . '/';
  109. if (! preg_match($rule_pattrn, $page, $matches)) continue;
  110. $template_page = preg_replace($rule_pattrn, $template, $page);
  111. if (! is_page($template_page)) continue;
  112. $body = get_source($template_page, TRUE, TRUE);
  113. // Remove fixed-heading anchors
  114. $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $body);
  115. // Remove '#freeze'
  116. $body = preg_replace('/^#freeze\s*$/m', '', $body);
  117. $count = count($matches);
  118. for ($i = 0; $i < $count; $i++)
  119. $body = str_replace('$' . $i, $matches[$i], $body);
  120. break;
  121. }
  122. return $body;
  123. }
  124. // Expand all search-words to regexes and push them into an array
  125. function get_search_words($words = array(), $do_escape = FALSE)
  126. {
  127. static $init, $mb_convert_kana, $pre, $post, $quote = '/';
  128. if (! isset($init)) {
  129. // function: mb_convert_kana() is for Japanese code only
  130. if (LANG == 'ja' && function_exists('mb_convert_kana')) {
  131. $mb_convert_kana = create_function('$str, $option',
  132. 'return mb_convert_kana($str, $option, SOURCE_ENCODING);');
  133. } else {
  134. $mb_convert_kana = create_function('$str, $option',
  135. 'return $str;');
  136. }
  137. if (SOURCE_ENCODING == 'EUC-JP') {
  138. // Perl memo - Correct pattern-matching with EUC-JP
  139. // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match (Japanese)
  140. $pre = '(?<!\x8F)';
  141. $post = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*' . // JIS X 0208
  142. '(?:[\x00-\x7F\x8E\x8F]|\z))'; // ASCII, SS2, SS3, or the last
  143. } else {
  144. $pre = $post = '';
  145. }
  146. $init = TRUE;
  147. }
  148. if (! is_array($words)) $words = array($words);
  149. // Generate regex for the words
  150. $regex = array();
  151. foreach ($words as $word) {
  152. $word = trim($word);
  153. if ($word == '') continue;
  154. // Normalize: ASCII letters = to single-byte. Others = to Zenkaku and Katakana
  155. $word_nm = $mb_convert_kana($word, 'aKCV');
  156. $nmlen = mb_strlen($word_nm, SOURCE_ENCODING);
  157. // Each chars may be served ...
  158. $chars = array();
  159. for ($pos = 0; $pos < $nmlen; $pos++) {
  160. $char = mb_substr($word_nm, $pos, 1, SOURCE_ENCODING);
  161. // Just normalized one? (ASCII char or Zenkaku-Katakana?)
  162. $or = array(preg_quote($do_escape ? htmlsc($char) : $char, $quote));
  163. if (strlen($char) == 1) {
  164. // An ASCII (single-byte) character
  165. foreach (array(strtoupper($char), strtolower($char)) as $_char) {
  166. if ($char != '&') $or[] = preg_quote($_char, $quote); // As-is?
  167. $ascii = ord($_char);
  168. $or[] = sprintf('&#(?:%d|x%x);', $ascii, $ascii); // As an entity reference?
  169. $or[] = preg_quote($mb_convert_kana($_char, 'A'), $quote); // As Zenkaku?
  170. }
  171. } else {
  172. // NEVER COME HERE with mb_substr(string, start, length, 'ASCII')
  173. // A multi-byte character
  174. $or[] = preg_quote($mb_convert_kana($char, 'c'), $quote); // As Hiragana?
  175. $or[] = preg_quote($mb_convert_kana($char, 'k'), $quote); // As Hankaku-Katakana?
  176. }
  177. $chars[] = '(?:' . join('|', array_unique($or)) . ')'; // Regex for the character
  178. }
  179. $regex[$word] = $pre . join('', $chars) . $post; // For the word
  180. }
  181. return $regex; // For all words
  182. }
  183. // 'Search' main function
  184. function do_search($word, $type = 'AND', $non_format = FALSE, $base = '')
  185. {
  186. global $script, $whatsnew, $non_list, $search_non_list;
  187. global $_msg_andresult, $_msg_orresult, $_msg_notfoundresult;
  188. global $search_auth, $show_passage, $search_word_color;
  189. $retval = array();
  190. $b_type = ($type == 'AND'); // AND:TRUE OR:FALSE
  191. $keys = get_search_words(preg_split('/\s+/', $word, -1, PREG_SPLIT_NO_EMPTY));
  192. foreach ($keys as $key=>$value)
  193. $keys[$key] = '/' . $value . '/S';
  194. $pages = get_existpages();
  195. // Avoid
  196. if ($base != '') {
  197. $pages = preg_grep('/^' . preg_quote($base, '/') . '/S', $pages);
  198. }
  199. if (! $search_non_list) {
  200. $pages = array_diff($pages, preg_grep('/' . $non_list . '/S', $pages));
  201. }
  202. $pages = array_flip($pages);
  203. unset($pages[$whatsnew]);
  204. $count = count($pages);
  205. foreach (array_keys($pages) as $page) {
  206. $b_match = FALSE;
  207. // Search for page name
  208. if (! $non_format) {
  209. foreach ($keys as $key) {
  210. $b_match = preg_match($key, $page);
  211. if ($b_type xor $b_match) break; // OR
  212. }
  213. if ($b_match) continue;
  214. }
  215. // Search auth for page contents
  216. if ($search_auth && ! check_readable($page, false, false)) {
  217. unset($pages[$page]);
  218. --$count;
  219. }
  220. // Search for page contents
  221. foreach ($keys as $key) {
  222. $b_match = preg_match($key, get_source($page, TRUE, TRUE));
  223. if ($b_type xor $b_match) break; // OR
  224. }
  225. if ($b_match) continue;
  226. unset($pages[$page]); // Miss
  227. }
  228. if ($non_format) return array_keys($pages);
  229. $r_word = rawurlencode($word);
  230. $s_word = htmlsc($word);
  231. if (empty($pages))
  232. return str_replace('$1', $s_word, $_msg_notfoundresult);
  233. ksort($pages, SORT_STRING);
  234. $retval = '<ul>' . "\n";
  235. foreach (array_keys($pages) as $page) {
  236. $r_page = rawurlencode($page);
  237. $s_page = htmlsc($page);
  238. $passage = $show_passage ? ' ' . get_passage(get_filetime($page)) : '';
  239. if ($search_word_color) {
  240. $uri = $script . '?' . 'cmd=read&amp;page=' . $r_page . '&amp;word=' . $r_word;
  241. } else {
  242. $uri = $script . '?' . $r_page;
  243. }
  244. $retval .= ' <li><a href="' . $uri . '">' . $s_page . '</a>' . $passage . '</li>' . "\n";
  245. }
  246. $retval .= '</ul>' . "\n";
  247. $retval .= str_replace('$1', $s_word, str_replace('$2', count($pages),
  248. str_replace('$3', $count, $b_type ? $_msg_andresult : $_msg_orresult)));
  249. return $retval;
  250. }
  251. // Argument check for program
  252. function arg_check($str)
  253. {
  254. global $vars;
  255. return isset($vars['cmd']) && (strpos($vars['cmd'], $str) === 0);
  256. }
  257. // Encode page-name
  258. function encode($str)
  259. {
  260. $str = strval($str);
  261. return ($str == '') ? '' : strtoupper(bin2hex($str));
  262. // Equal to strtoupper(join('', unpack('H*0', $key)));
  263. // But PHP 4.3.10 says 'Warning: unpack(): Type H: outside of string in ...'
  264. }
  265. // Decode page name
  266. function decode($str)
  267. {
  268. return hex2bin_impl($str);
  269. }
  270. // Inversion of bin2hex()
  271. function hex2bin_impl($hex_string)
  272. {
  273. // preg_match : Avoid warning : pack(): Type H: illegal hex digit ...
  274. // (string) : Always treat as string (not int etc). See BugTrack2/31
  275. return preg_match('/^[0-9a-f]+$/i', $hex_string) ?
  276. pack('H*', (string)$hex_string) : $hex_string;
  277. }
  278. // Remove [[ ]] (brackets)
  279. function strip_bracket($str)
  280. {
  281. $match = array();
  282. if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
  283. return $match[1];
  284. } else {
  285. return $str;
  286. }
  287. }
  288. // Generate sorted "list of pages" XHTML, with page-reading hints
  289. function page_list($pages = array('pagename.txt' => 'pagename'), $cmd = 'read', $withfilename = FALSE)
  290. {
  291. global $pagereading_enable, $list_index, $_msg_symbol, $_msg_other;
  292. // Sentinel: symbolic-chars < alphabetic-chars < another(multibyte)-chars
  293. // = ' ' < '[a-zA-Z]' < 'zz'
  294. $sentinel_symbol = ' ';
  295. $sentinel_another = 'zz';
  296. $href = get_script_uri() . '?' . ($cmd == 'read' ? '' : 'cmd=' . rawurlencode($cmd) . '&amp;page=');
  297. $array = $matches = array();
  298. if ($pagereading_enable) {
  299. mb_regex_encoding(SOURCE_ENCODING);
  300. $readings = get_readings($pages);
  301. }
  302. foreach($pages as $file => $page) {
  303. // Get the initial letter of the page name
  304. if ($pagereading_enable) {
  305. // WARNING: Japanese code hard-wired
  306. if(mb_ereg('^([A-Za-z])', mb_convert_kana($page, 'a'), $matches)) {
  307. $initial = & $matches[1];
  308. } elseif (isset($readings[$page]) && mb_ereg('^([ァ-ヶ])', $readings[$page], $matches)) { // here
  309. $initial = & $matches[1];
  310. } elseif (mb_ereg('^[ -~]|[^ぁ-ん亜-熙]', $page)) { // and here
  311. $initial = & $sentinel_symbol;
  312. } else {
  313. $initial = & $sentinel_another;
  314. }
  315. } else {
  316. if (preg_match('/^([A-Za-z])/', $page, $matches)) {
  317. $initial = & $matches[1];
  318. } elseif (preg_match('/^([ -~])/', $page)) {
  319. $initial = & $sentinel_symbol;
  320. } else {
  321. $initial = & $sentinel_another;
  322. }
  323. }
  324. $str = ' <li>' .
  325. '<a href="' . $href . rawurlencode($page) . '">' .
  326. htmlsc($page, ENT_QUOTES) .
  327. '</a>' .
  328. get_pg_passage($page);
  329. if ($withfilename) {
  330. $str .= "\n" .
  331. ' <ul><li>' . htmlsc($file) . '</li></ul>' . "\n" .
  332. ' ';
  333. }
  334. $str .= '</li>';
  335. $array[$initial][$page] = $str;
  336. }
  337. unset($pages);
  338. ksort($array, SORT_STRING);
  339. if ($list_index) {
  340. $s_msg_symbol = htmlsc($_msg_symbol);
  341. $s_msg_another = htmlsc($_msg_other);
  342. }
  343. $cnt = 0;
  344. $retval = $contents = array();
  345. $retval[] = '<ul>';
  346. foreach ($array as $_initial => $pages) {
  347. ksort($pages, SORT_STRING);
  348. if ($list_index) {
  349. ++$cnt;
  350. if ($_initial == $sentinel_symbol) {
  351. $_initial = & $s_msg_symbol;
  352. } else if ($_initial == $sentinel_another) {
  353. $_initial = & $s_msg_another;
  354. }
  355. $retval[] = ' <li><a id="head_' . $cnt .
  356. '" href="#top_' . $cnt .
  357. '"><strong>' . $_initial . '</strong></a>';
  358. $retval[] = ' <ul>';
  359. $contents[] = '<a id="top_' . $cnt .
  360. '" href="#head_' . $cnt . '"><strong>' .
  361. $_initial . '</strong></a>';
  362. }
  363. $retval[] = join("\n", $pages);
  364. if ($list_index) {
  365. $retval[] = ' </ul>';
  366. $retval[] = ' </li>';
  367. }
  368. }
  369. $retval[] = '</ul>';
  370. unset($array);
  371. // Insert a table of contents
  372. if ($list_index && $cnt) {
  373. // Breaks in every N characters
  374. $N = 16;
  375. $tmp = array();
  376. while (! empty($contents)) {
  377. $tmp[] = join(' | ' . "\n", array_splice($contents, 0, $N));
  378. }
  379. $contents = & $tmp;
  380. array_unshift(
  381. $retval,
  382. '<div id="top" style="text-align:center">',
  383. join("\n" . '<br />' . "\n", $contents),
  384. '</div>');
  385. }
  386. return implode("\n", $retval) . "\n";
  387. }
  388. // Show text formatting rules
  389. function catrule()
  390. {
  391. global $rule_page;
  392. if (! is_page($rule_page)) {
  393. return '<p>Sorry, page \'' . htmlsc($rule_page) .
  394. '\' unavailable.</p>';
  395. } else {
  396. return convert_html(get_source($rule_page));
  397. }
  398. }
  399. // Show (critical) error message
  400. function die_message($msg)
  401. {
  402. $title = $page = 'Runtime error';
  403. $body = <<<EOD
  404. <h3>Runtime error</h3>
  405. <strong>Error message : $msg</strong>
  406. EOD;
  407. pkwk_common_headers();
  408. if(defined('SKIN_FILE') && file_exists(SKIN_FILE) && is_readable(SKIN_FILE)) {
  409. catbody($title, $page, $body);
  410. } else {
  411. header('Content-Type: text/html; charset=euc-jp');
  412. print <<<EOD
  413. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  414. <html>
  415. <head>
  416. <title>$title</title>
  417. <meta http-equiv="content-type" content="text/html; charset=euc-jp">
  418. </head>
  419. <body>
  420. $body
  421. </body>
  422. </html>
  423. EOD;
  424. }
  425. exit;
  426. }
  427. // Have the time (as microtime)
  428. function getmicrotime()
  429. {
  430. list($usec, $sec) = explode(' ', microtime());
  431. return ((float)$sec + (float)$usec);
  432. }
  433. // Elapsed time by second
  434. //define('MUTIME', getmicrotime());
  435. function elapsedtime()
  436. {
  437. $at_the_microtime = MUTIME;
  438. return sprintf('%01.03f', getmicrotime() - $at_the_microtime);
  439. }
  440. // Get the date
  441. function get_date($format, $timestamp = NULL)
  442. {
  443. $format = preg_replace('/(?<!\\\)T/',
  444. preg_replace('/(.)/', '\\\$1', ZONE), $format);
  445. $time = ZONETIME + (($timestamp !== NULL) ? $timestamp : UTIME);
  446. return date($format, $time);
  447. }
  448. // Format date string
  449. function format_date($val, $paren = FALSE)
  450. {
  451. global $date_format, $time_format, $weeklabels;
  452. $val += ZONETIME;
  453. $date = date($date_format, $val) .
  454. ' (' . $weeklabels[date('w', $val)] . ') ' .
  455. date($time_format, $val);
  456. return $paren ? '(' . $date . ')' : $date;
  457. }
  458. // Get short string of the passage, 'N seconds/minutes/hours/days/years ago'
  459. function get_passage($time, $paren = TRUE)
  460. {
  461. static $units = array('m'=>60, 'h'=>24, 'd'=>1);
  462. $time = max(0, (UTIME - $time) / 60); // minutes
  463. foreach ($units as $unit=>$card) {
  464. if ($time < $card) break;
  465. $time /= $card;
  466. }
  467. $time = floor($time) . $unit;
  468. return $paren ? '(' . $time . ')' : $time;
  469. }
  470. // Hide <input type="(submit|button|image)"...>
  471. function drop_submit($str)
  472. {
  473. return preg_replace('/<input([^>]+)type="(submit|button|image)"/i',
  474. '<input$1type="hidden"', $str);
  475. }
  476. // Generate AutoLink patterns (thx to hirofummy)
  477. function get_autolink_pattern(& $pages, $min_len = -1)
  478. {
  479. global $WikiName, $autolink, $nowikiname;
  480. $config = new Config('AutoLink');
  481. $config->read();
  482. $ignorepages = $config->get('IgnoreList');
  483. $forceignorepages = $config->get('ForceIgnoreList');
  484. unset($config);
  485. $auto_pages = array_merge($ignorepages, $forceignorepages);
  486. if ($min_len == -1) {
  487. $min_len = $autolink; // set $autolink, when omitted.
  488. }
  489. foreach ($pages as $page)
  490. if (preg_match('/^' . $WikiName . '$/', $page) ?
  491. $nowikiname : strlen($page) >= $min_len)
  492. $auto_pages[] = $page;
  493. if (empty($auto_pages)) {
  494. $result = $result_a = $nowikiname ? '(?!)' : $WikiName;
  495. } else {
  496. $auto_pages = array_unique($auto_pages);
  497. sort($auto_pages, SORT_STRING);
  498. $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
  499. $auto_pages = array_values(array_diff($auto_pages, $auto_pages_a));
  500. $result = generate_trie_regex($auto_pages);
  501. $result_a = generate_trie_regex($auto_pages_a);
  502. }
  503. return array($result, $result_a, $forceignorepages);
  504. }
  505. // preg_quote(), and also escape PCRE_EXTENDED-related chars
  506. // REFERENCE: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
  507. // NOTE: Some special whitespace characters may warned by PCRE_EXTRA,
  508. // because of mismatch-possibility between PCRE_EXTENDED and '[:space:]#'.
  509. function preg_quote_extended($string, $delimiter = NULL)
  510. {
  511. // Escape some more chars
  512. $regex_from = '/([[:space:]#])/';
  513. $regex_to = '\\\\$1';
  514. if (is_string($delimiter) && preg_match($regex_from, $delimiter)) {
  515. $delimiter = NULL;
  516. }
  517. return preg_replace($regex_from, $regex_to, preg_quote($string, $delimiter));
  518. }
  519. // Generate one compact regex for quick reTRIEval,
  520. // that just matches with all $array-values.
  521. //
  522. // USAGE (PHP >= 4.4.0, PHP >= 5.0.2):
  523. // $array = array(7 => 'fooa', 5 => 'foob');
  524. // $array = array_unique($array);
  525. // sort($array, SORT_LOCALE_STRING); // Keys will be replaced
  526. // echo generate_trie_regex($array); // 'foo(?:a|b)'
  527. //
  528. // USAGE (PHP >= 5.2.9):
  529. // $array = array(7 => 'fooa', 5 => 'foob');
  530. // $array = array_unique($array, SORT_LOCALE_STRING);
  531. // $array = array_values($array);
  532. // echo generate_trie_regex($array); // 'foo(?:a|b)'
  533. //
  534. // ARGUMENTS:
  535. // $array : A _sorted_string_ array
  536. // * array_keys($array) MUST BE _continuous_integers_started_with_0_.
  537. // * Type of all $array-values MUST BE string.
  538. // $_offset : (int) internal use. $array[$_offset ] is the first value to check
  539. // $_sentry : (int) internal use. $array[$_sentry - 1] is the last value to check
  540. // $_pos : (int) internal use. Position of the letter to start checking. (0 = the first letter)
  541. //
  542. // REFERENCE: http://en.wikipedia.org/wiki/Trie
  543. //
  544. function generate_trie_regex($array, $_offset = 0, $_sentry = NULL, $_pos = 0)
  545. {
  546. if (empty($array)) return '(?!)'; // Match with nothing
  547. if ($_sentry === NULL) $_sentry = count($array);
  548. // Question mark: array('', 'something') => '(?:something)?'
  549. $skip = ($_pos >= mb_strlen($array[$_offset]));
  550. if ($skip) ++$_offset;
  551. // Generate regex for each value
  552. $regex = array();
  553. $index = $_offset;
  554. $multi = FALSE;
  555. while ($index < $_sentry) {
  556. if ($index != $_offset) {
  557. $multi = TRUE;
  558. $regex[] = '|'; // OR
  559. }
  560. // Get one character from left side of the value
  561. $char = mb_substr($array[$index], $_pos, 1);
  562. // How many continuous keys have the same letter
  563. // at the same position?
  564. for ($i = $index + 1; $i < $_sentry; $i++) {
  565. if (mb_substr($array[$i], $_pos, 1) != $char) break;
  566. }
  567. if ($index < ($i - 1)) {
  568. // Some more keys found
  569. // Recurse
  570. $regex[] = preg_quote_extended($char, '/');
  571. $regex[] = generate_trie_regex($array, $index, $i, $_pos + 1);
  572. } else {
  573. // Not found
  574. $regex[] = preg_quote_extended(mb_substr($array[$index], $_pos), '/');
  575. }
  576. $index = $i;
  577. }
  578. if ($skip || $multi) {
  579. array_unshift($regex, '(?:');
  580. $regex[] = ')';
  581. }
  582. if ($skip) $regex[] = '?'; // Match for $pages[$_offset - 1]
  583. return implode('', $regex);
  584. }
  585. // Compat
  586. function get_autolink_pattern_sub($pages, $_start, $_end, $_pos)
  587. {
  588. return generate_trie_regex($pages, $_start, $_end, $_pos);
  589. }
  590. // Load/get setting pairs from AutoAliasName
  591. function get_autoaliases($word = '')
  592. {
  593. global $aliaspage, $autoalias_max_words;
  594. static $pairs;
  595. if (! isset($pairs)) {
  596. $pairs = array();
  597. $pattern = <<<EOD
  598. \[\[ # open bracket
  599. ((?:(?!\]\]).)+)> # (1) alias name
  600. ((?:(?!\]\]).)+) # (2) alias link
  601. \]\] # close bracket
  602. EOD;
  603. $postdata = get_source($aliaspage, TRUE, TRUE);
  604. $matches = array();
  605. $count = 0;
  606. $max = max($autoalias_max_words, 0);
  607. if (preg_match_all('/' . $pattern . '/x', $postdata, $matches, PREG_SET_ORDER)) {
  608. foreach($matches as $key => $value) {
  609. if ($count == $max) break;
  610. $name = trim($value[1]);
  611. if (! isset($pairs[$name])) {
  612. ++$count;
  613. $pairs[$name] = trim($value[2]);
  614. }
  615. unset($matches[$key]);
  616. }
  617. }
  618. }
  619. if ($word === '') {
  620. // An array(): All pairs
  621. return $pairs;
  622. } else {
  623. // A string: Seek the pair
  624. if (isset($pairs[$word])) {
  625. return $pairs[$word];
  626. } else {
  627. return '';
  628. }
  629. }
  630. }
  631. // Get absolute-URI of this script
  632. function get_script_uri($init_uri = '')
  633. {
  634. global $script_directory_index;
  635. static $script;
  636. if ($init_uri == '') {
  637. // Get
  638. if (isset($script)) return $script;
  639. // Set automatically
  640. $msg = 'get_script_uri() failed: Please set $script at INI_FILE manually';
  641. $script = (SERVER_PORT == 443 ? 'https://' : 'http://'); // scheme
  642. $script .= SERVER_NAME; // host
  643. $script .= (SERVER_PORT == 80 ? '' : ':' . SERVER_PORT); // port
  644. // SCRIPT_NAME が'/'で始まっていない場合(cgiなど) REQUEST_URIを使ってみる
  645. $path = SCRIPT_NAME;
  646. if ($path{0} != '/') {
  647. if (! isset($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI']{0} != '/')
  648. die_message($msg);
  649. // REQUEST_URIをパースし、path部分だけを取り出す
  650. $parse_url = parse_url($script . $_SERVER['REQUEST_URI']);
  651. if (! isset($parse_url['path']) || $parse_url['path']{0} != '/')
  652. die_message($msg);
  653. $path = $parse_url['path'];
  654. }
  655. $script .= $path;
  656. if (! is_url($script, TRUE) && php_sapi_name() == 'cgi')
  657. die_message($msg);
  658. unset($msg);
  659. } else {
  660. // Set manually
  661. if (isset($script)) die_message('$script: Already init');
  662. if (! is_url($init_uri, TRUE)) die_message('$script: Invalid URI');
  663. $script = $init_uri;
  664. }
  665. // Cut filename or not
  666. if (isset($script_directory_index)) {
  667. if (! file_exists($script_directory_index))
  668. die_message('Directory index file not found: ' .
  669. htmlsc($script_directory_index));
  670. $matches = array();
  671. if (preg_match('#^(.+/)' . preg_quote($script_directory_index, '#') . '$#',
  672. $script, $matches)) $script = $matches[1];
  673. }
  674. return $script;
  675. }
  676. // Remove null(\0) bytes from variables
  677. //
  678. // NOTE: PHP had vulnerabilities that opens "hoge.php" via fopen("hoge.php\0.txt") etc.
  679. // [PHP-users 12736] null byte attack
  680. // http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
  681. //
  682. // 2003-05-16: magic quotes gpcの復元処理を統合
  683. // 2003-05-21: 連想配列のキーはbinary safe
  684. //
  685. function input_filter($param)
  686. {
  687. static $magic_quotes_gpc = NULL;
  688. if ($magic_quotes_gpc === NULL)
  689. $magic_quotes_gpc = get_magic_quotes_gpc();
  690. if (is_array($param)) {
  691. return array_map('input_filter', $param);
  692. } else {
  693. $result = str_replace("\0", '', $param);
  694. if ($magic_quotes_gpc) $result = stripslashes($result);
  695. return $result;
  696. }
  697. }
  698. // Compat for 3rd party plugins. Remove this later
  699. function sanitize($param) {
  700. return input_filter($param);
  701. }
  702. // Explode Comma-Separated Values to an array
  703. function csv_explode($separator, $string)
  704. {
  705. $retval = $matches = array();
  706. $_separator = preg_quote($separator, '/');
  707. if (! preg_match_all('/("[^"]*(?:""[^"]*)*"|[^' . $_separator . ']*)' .
  708. $_separator . '/', $string . $separator, $matches))
  709. return array();
  710. foreach ($matches[1] as $str) {
  711. $len = strlen($str);
  712. if ($len > 1 && $str{0} == '"' && $str{$len - 1} == '"')
  713. $str = str_replace('""', '"', substr($str, 1, -1));
  714. $retval[] = $str;
  715. }
  716. return $retval;
  717. }
  718. // Implode an array with CSV data format (escape double quotes)
  719. function csv_implode($glue, $pieces)
  720. {
  721. $_glue = ($glue != '') ? '\\' . $glue{0} : '';
  722. $arr = array();
  723. foreach ($pieces as $str) {
  724. if (ereg('[' . $_glue . '"' . "\n\r" . ']', $str))
  725. $str = '"' . str_replace('"', '""', $str) . '"';
  726. $arr[] = $str;
  727. }
  728. return join($glue, $arr);
  729. }
  730. // Sugar with default settings
  731. function htmlsc($string = '', $flags = ENT_QUOTES, $charset = CONTENT_CHARSET)
  732. {
  733. return htmlspecialchars($string, $flags, $charset); // htmlsc()
  734. }
  735. //// Compat ////
  736. // is_a -- Returns TRUE if the object is of this class or has this class as one of its parents
  737. // (PHP 4 >= 4.2.0, PHP 5)
  738. if (! function_exists('is_a')) {
  739. function is_a($class, $match)
  740. {
  741. if (empty($class)) return FALSE;
  742. $class = is_object($class) ? get_class($class) : $class;
  743. if (strtolower($class) == strtolower($match)) {
  744. return TRUE;
  745. } else {
  746. return is_a(get_parent_class($class), $match); // Recurse
  747. }
  748. }
  749. }
  750. // array_fill -- Fill an array with values
  751. // (PHP 4 >= 4.2.0, PHP 5)
  752. if (! function_exists('array_fill')) {
  753. function array_fill($start_index, $num, $value)
  754. {
  755. $ret = array();
  756. while ($num-- > 0) $ret[$start_index++] = $value;
  757. return $ret;
  758. }
  759. }
  760. // md5_file -- Calculates the md5 hash of a given filename
  761. // (PHP 4 >= 4.2.0, PHP 5)
  762. if (! function_exists('md5_file')) {
  763. function md5_file($filename)
  764. {
  765. if (! file_exists($filename)) return FALSE;
  766. $fd = fopen($filename, 'rb');
  767. if ($fd === FALSE ) return FALSE;
  768. $data = fread($fd, filesize($filename));
  769. fclose($fd);
  770. return md5($data);
  771. }
  772. }
  773. // sha1 -- Compute SHA-1 hash
  774. // (PHP 4 >= 4.3.0, PHP 5)
  775. if (! function_exists('sha1')) {
  776. if (extension_loaded('mhash')) {
  777. function sha1($str)
  778. {
  779. return bin2hex(mhash(MHASH_SHA1, $str));
  780. }
  781. }
  782. }
  783. ?>