2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: func.php,v 1.6 2004/08/06 15:39:52 henoheno Exp $
8 // ʸ»úÎó¤¬InterWikiName¤«¤É¤¦¤«
9 function is_interwiki($str)
11 global $InterWikiName;
13 return preg_match("/^$InterWikiName$/", $str);
16 // ʸ»úÎ󤬥ڡ¼¥¸Ì¾¤«¤É¤¦¤«
17 function is_pagename($str)
21 $is_pagename = (! is_interwiki($str) and
22 preg_match("/^(?!\/)$BracketName$(?<!\/$)/", $str) and
23 ! preg_match('/(^|\/)\.{1,2}(\/|$)/', $str));
25 if (defined('SOURCE_ENCODING')) {
26 if (SOURCE_ENCODING == 'UTF-8') {
27 $pattern = '/^(?:[\x00-\x7F]|(?:[\xC0-\xDF][\x80-\xBF])|(?:[\xE0-\xEF][\x80-\xBF][\x80-\xBF]))+$/';
28 $is_pagename = ($is_pagename and preg_match($pattern, $str));
29 } else if (SOURCE_ENCODING == 'EUC-JP') {
30 $pattern = '/^(?:[\x00-\x7F]|(?:[\x8E\xA1-\xFE][\xA1-\xFE])|(?:\x8F[\xA1-\xFE][\xA1-\xFE]))+$/';
31 $is_pagename = ($is_pagename and preg_match($pattern, $str));
38 // ʸ»úÎó¤¬URL¤«¤É¤¦¤«
39 function is_url($str, $only_http = FALSE)
41 $scheme = $only_http ? 'https?' : 'https?|ftp|news';
42 return preg_match('/^('.$scheme.')(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]*)$/', $str);
46 function is_page($page, $reload = FALSE)
48 if ($reload) clearstatcache();
49 return file_exists(get_filename($page));
53 function is_editable($page)
56 static $is_editable = array();
58 if (!array_key_exists($page, $is_editable))
60 $is_editable[$page] = (
61 is_pagename($page) and
62 ! is_freeze($page) and
63 ! in_array($page, $cantedit)
67 return $is_editable[$page];
70 // ¥Ú¡¼¥¸¤¬Åà·ë¤µ¤ì¤Æ¤¤¤ë¤«
71 function is_freeze($page)
73 global $function_freeze;
75 if (! $function_freeze or ! is_page($page)) return FALSE;
77 list($lines) = get_source($page);
78 return (rtrim($lines) == '#freeze');
82 function auto_template($page)
84 global $auto_template_func, $auto_template_rules;
86 if (! $auto_template_func) return '';
90 foreach ($auto_template_rules as $rule => $template)
92 if (preg_match("/$rule/", $page, $matches))
94 $template_page = preg_replace("/$rule/", $template, $page);
95 if (is_page($template_page))
97 $body = join('', get_source($template_page));
98 // ¸«½Ð¤·¤Î¸ÇÍIDÉô¤òºï½ü
99 $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $body);
101 $body = preg_replace('/^#freeze\s*$/m', '', $body);
103 for ($i = 0; $i < count($matches); $i++) {
104 $body = str_replace("\$$i", $matches[$i], $body);
114 function get_search_words($words, $special = FALSE)
117 // Perl¥á¥â - Àµ¤·¤¯¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¤µ¤»¤ë
118 // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
119 $eucpre = $eucpost = '';
120 if (SOURCE_ENCODING == 'EUC-JP')
122 $eucpre = '(?<!\x8F)';
123 // # JIS X 0208 ¤¬ 0ʸ»ú°Ê¾å³¤¤¤Æ # ASCII, SS2, SS3 ¤Þ¤¿¤Ï½ªÃ¼
124 $eucpost = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))';
126 $quote_func = create_function('$str', 'return preg_quote($str, "/");');
128 // LANG=='ja'¤Ç¡¢mb_convert_kana¤¬»È¤¨¤ë¾ì¹ç¤Ïmb_convert_kana¤ò»ÈÍÑ
129 $convert_kana = create_function('$str, $option',
130 (LANG == 'ja' and function_exists('mb_convert_kana')) ?
131 'return mb_convert_kana($str, $option);' : 'return $str;'
134 foreach ($words as $word)
136 // ±Ñ¿ô»ú¤ÏȾ³Ñ,¥«¥¿¥«¥Ê¤ÏÁ´³Ñ,¤Ò¤é¤¬¤Ê¤Ï¥«¥¿¥«¥Ê¤Ë
137 $word_zk = $convert_kana($word, 'aKCV');
139 for ($pos = 0; $pos < mb_strlen($word_zk); $pos++)
141 $char = mb_substr($word_zk, $pos,1);
142 // $special : htmlspecialchars()¤òÄ̤¹¤«
143 $arr = array($quote_func($special ? htmlspecialchars($char) : $char));
144 if (strlen($char) == 1) // ±Ñ¿ô»ú
146 foreach (array(strtoupper($char), strtolower($char)) as $_char)
149 $arr[] = $quote_func($_char);
152 $arr[] = sprintf('&#(?:%d|x%x);', $ord, $ord); // ¼ÂÂλ²¾È
153 $arr[] = $quote_func($convert_kana($_char, 'A')); // Á´³Ñ
156 else // ¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú
158 $arr[] = $quote_func($convert_kana($char, 'c')); // ¤Ò¤é¤¬¤Ê
159 $arr[] = $quote_func($convert_kana($char, 'k')); // Ⱦ³Ñ¥«¥¿¥«¥Ê
161 $chars[] = '(?:' . join('|', array_unique($arr)) . ')';
163 $retval[$word] = $eucpre.join('', $chars) . $eucpost;
169 function do_search($word, $type = 'AND', $non_format = FALSE)
171 global $script, $whatsnew, $non_list, $search_non_list;
172 global $_msg_andresult, $_msg_orresult, $_msg_notfoundresult;
177 $b_type = ($type == 'AND'); // AND:TRUE OR:FALSE
178 $keys = get_search_words(preg_split('/\s+/', $word, -1, PREG_SPLIT_NO_EMPTY));
180 $_pages = get_existpages();
183 foreach ($_pages as $page)
185 if ($page == $whatsnew or (! $search_non_list and preg_match("/$non_list/", $page)))
188 // ¸¡º÷Âоݥڡ¼¥¸¤ÎÀ©¸Â¤ò¤«¤±¤ë¤«¤É¤¦¤« (¥Ú¡¼¥¸Ì¾¤ÏÀ©¸Â³°)
189 if ($search_auth and ! check_readable($page, false, false)) {
190 $source = get_source(); // ¸¡º÷Âоݥڡ¼¥¸ÆâÍƤò¶õ¤Ë¡£
192 $source = get_source($page);
195 array_unshift($source, $page); // ¥Ú¡¼¥¸Ì¾¤â¸¡º÷ÂоݤË
198 foreach ($keys as $key) {
199 $tmp = preg_grep("/$key/", $source);
200 $b_match = (count($tmp) > 0);
201 if ($b_match xor $b_type)
205 $pages[$page] = get_filetime($page);
209 return array_keys($pages);
212 $r_word = rawurlencode($word);
213 $s_word = htmlspecialchars($word);
214 if (count($pages) == 0) {
215 return str_replace('$1', $s_word, $_msg_notfoundresult);
220 foreach ($pages as $page=>$time)
222 $r_page = rawurlencode($page);
223 $s_page = htmlspecialchars($page);
224 $passage = get_passage($time);
225 $retval .= " <li><a href=\"$script?cmd=read&page=$r_page&word=$r_word\">$s_page</a>$passage</li>\n";
227 $retval .= "</ul>\n";
229 $retval .= str_replace('$1', $s_word, str_replace('$2', count($pages),
230 str_replace('$3', count($_pages), $b_type ? $_msg_andresult : $_msg_orresult)));
235 // ¥×¥í¥°¥é¥à¤Ø¤Î°ú¿ô¤Î¥Á¥§¥Ã¥¯
236 function arg_check($str)
240 return array_key_exists('cmd', $vars) and (strpos($vars['cmd'], $str) === 0);
243 // ¥Ú¡¼¥¸Ì¾¤Î¥¨¥ó¥³¡¼¥É
244 function encode($key)
246 return ($key == '') ? '' : strtoupper(join('', unpack('H*0', $key)));
249 // ¥Ú¡¼¥¸Ì¾¤Î¥Ç¥³¡¼¥É
250 function decode($key)
252 return ($key == '') ? '' : substr(pack('H*', '20202020' . $key), 4);
256 function strip_bracket($str)
259 if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
267 function page_list($pages, $cmd = 'read', $withfilename = FALSE)
269 global $script, $list_index, $top;
270 global $_msg_symbol, $_msg_other;
271 global $pagereading_enable;
273 // ¥½¡¼¥È¥¡¼¤ò·èÄꤹ¤ë¡£ ' ' < '[a-zA-Z]' < 'zz'¤È¤¤¤¦Á°Äó¡£
279 if($pagereading_enable) {
280 mb_regex_encoding(SOURCE_ENCODING);
281 $readings = get_readings($pages);
284 $list = $matches = array();
285 foreach($pages as $file=>$page)
287 $r_page = rawurlencode($page);
288 $s_page = htmlspecialchars($page, ENT_QUOTES);
289 $passage = get_pg_passage($page);
291 $str = " <li><a href=\"$script?cmd=$cmd&page=$r_page\">$s_page</a>$passage";
294 $s_file = htmlspecialchars($file);
295 $str .= "\n <ul><li>$s_file</li></ul>\n ";
299 if($pagereading_enable) {
300 if(mb_ereg('^([A-Za-z])', mb_convert_kana($page, 'a'), $matches)) {
303 elseif(mb_ereg('^([¥¡-¥ö])', $readings[$page], $matches)) {
306 elseif (mb_ereg('^[ -~]|[^¤¡-¤ó°¡-ô¦]', $page)) {
314 $head = (preg_match('/^([A-Za-z])/', $page, $matches)) ? $matches[1] :
315 (preg_match('/^([ -~])/', $page, $matches) ? $symbol : $other);
318 $list[$head][$page] = $str;
323 $arr_index = array();
325 foreach ($list as $head=>$pages)
327 if ($head === $symbol) {
328 $head = $_msg_symbol;
329 } else if ($head === $other) {
336 $arr_index[] = "<a id=\"top_$cnt\" href=\"#head_$cnt\"><strong>$head</strong></a>";
337 $retval .= " <li><a id=\"head_$cnt\" href=\"#top_$cnt\"><strong>$head</strong></a>\n <ul>\n";
340 $retval .= join("\n", $pages);
343 $retval .= "\n </ul>\n </li>\n";
346 $retval .= "</ul>\n";
347 if ($list_index and $cnt > 0)
350 while (count($arr_index) > 0) {
351 $top[] = join(" | \n", array_splice($arr_index, 0, 16)) . "\n";
353 $retval = "<div id=\"top\" style=\"text-align:center\">\n" .
354 join('<br />', $top) . "</div>\n" . $retval;
359 // ¥Æ¥¥¹¥ÈÀ°·Á¥ë¡¼¥ë¤òɽ¼¨¤¹¤ë
364 if (! is_page($rule_page))
366 return "<p>sorry, $rule_page unavailable.</p>";
368 return convert_html(get_source($rule_page));
371 // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë
372 function die_message($msg)
374 $title = $page = 'Runtime error';
377 <h3>Runtime error</h3>
378 <strong>Error message : $msg</strong>
381 if(defined('SKIN_FILE') && file_exists(SKIN_FILE) && is_readable(SKIN_FILE)) {
382 catbody($title, $page, $body);
384 header('Content-Type: text/html; charset=euc-jp');
386 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
389 <title>$title</title>
390 <meta http-equiv="content-type" content="text/html; charset=euc-jp">
401 // ¸½ºß»þ¹ï¤ò¥Þ¥¤¥¯¥íÉäǼèÆÀ
402 function getmicrotime()
404 list($usec, $sec) = explode(' ', microtime());
405 return ((float)$sec + (float)$usec);
409 function get_date($format, $timestamp = NULL)
411 $time = ($timestamp === NULL) ? UTIME : $timestamp;
414 $format = preg_replace('/(?<!\\\)T/', preg_replace('/(.)/', '\\\$1', ZONE), $format);
416 return date($format, $time);
420 function format_date($val, $paren = FALSE)
422 global $date_format, $time_format, $weeklabels;
426 $ins_date = date($date_format, $val);
427 $ins_time = date($time_format, $val);
428 $ins_week = '(' . $weeklabels[date('w', $val)] . ')';
430 $ins = "$ins_date $ins_week $ins_time";
431 return $paren ? "($ins)" : $ins;
434 // ·Ð²á»þ¹ïʸ»úÎó¤òºî¤ë
435 function get_passage($time, $paren = TRUE)
437 static $units = array('m'=>60, 'h'=>24, 'd'=>1);
439 $time = max(0, (UTIME - $time) / 60); // minutes
441 foreach ($units as $unit=>$card) {
442 if ($time < $card) break;
445 $time = floor($time) . $unit;
447 return $paren ? "($time)" : $time;
450 // <input type="(submit|button|image)"...>¤ò±£¤¹
451 function drop_submit($str)
454 '/<input([^>]+)type="(submit|button|image)"/i',
455 '<input$1type="hidden"',
460 // AutoLink¤Î¥Ñ¥¿¡¼¥ó¤òÀ¸À®¤¹¤ë
462 function get_autolink_pattern(& $pages)
464 global $WikiName, $autolink, $nowikiname;
466 $config = &new Config('AutoLink');
468 $ignorepages = $config->get('IgnoreList');
469 $forceignorepages = $config->get('ForceIgnoreList');
471 $auto_pages = array_merge($ignorepages, $forceignorepages);
473 foreach ($pages as $page) {
474 if (preg_match("/^$WikiName$/", $page) ?
475 $nowikiname : strlen($page) >= $autolink)
476 $auto_pages[] = $page;
479 if (count($auto_pages) == 0) {
480 return $nowikiname ? '(?!)' : $WikiName;
483 $auto_pages = array_unique($auto_pages);
484 sort($auto_pages, SORT_STRING);
486 $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
487 $auto_pages = array_values(array_diff($auto_pages, $auto_pages_a));
489 $result = get_autolink_pattern_sub($auto_pages, 0, count($auto_pages), 0);
490 $result_a = get_autolink_pattern_sub($auto_pages_a, 0, count($auto_pages_a), 0);
492 return array($result, $result_a, $forceignorepages);
495 function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
497 if ($end == 0) return '(?!)';
501 $x = (mb_strlen($pages[$start]) <= $pos);
507 for ($i = $start; $i < $end; $i = $j) // What is the initial state of $j?
509 $char = mb_substr($pages[$i], $pos, 1);
510 for ($j = $i; $j < $end; $j++) {
511 if (mb_substr($pages[$j], $pos, 1) != $char)
517 if ($i >= ($j - 1)) {
518 $result .= str_replace(' ', '\\ ', preg_quote(mb_substr($pages[$i], $pos), '/'));
520 $result .= str_replace(' ', '\\ ', preg_quote($char, '/')) .
521 get_autolink_pattern_sub($pages, $i, $j, $pos + 1);
525 if ($x or $count > 1) {
526 $result = '(?:' . $result . ')';
534 // pukiwiki.php¥¹¥¯¥ê¥×¥È¤Îabsolute-uri¤òÀ¸À®
535 function get_script_uri()
537 $script = (SERVER_PORT == 443 ? 'https://' : 'http://'); // scheme
538 $script .= SERVER_NAME; // host
539 $script .= (SERVER_PORT == 80 ? '' : ':' . SERVER_PORT); // port
541 // SCRIPT_NAME ¤¬'/'¤Ç»Ï¤Þ¤Ã¤Æ¤¤¤Ê¤¤¾ì¹ç(cgi¤Ê¤É) REQUEST_URI¤ò»È¤Ã¤Æ¤ß¤ë
545 if (!isset($_SERVER['REQUEST_URI']) or $_SERVER['REQUEST_URI']{0} != '/')
548 // REQUEST_URI¤ò¥Ñ¡¼¥¹¤·¡¢pathÉôʬ¤À¤±¤ò¼è¤ê½Ð¤¹
549 $parse_url = parse_url($script.$_SERVER['REQUEST_URI']);
550 if (! isset($parse_url['path']) or $parse_url['path']{0} != '/')
553 $path = $parse_url['path'];
555 $script .= $path; // path
561 ÊÑ¿ôÆâ¤Înull(\0)¥Ð¥¤¥È¤òºï½ü¤¹¤ë
562 PHP¤Ïfopen("hoge.php\0.txt")¤Ç"hoge.php"¤ò³«¤¤¤Æ¤·¤Þ¤¦¤Ê¤É¤ÎÌäÂꤢ¤ê
564 http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
565 [PHP-users 12736] null byte attack
567 2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
568 2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥¡¼¤Ïbinary safe
570 function input_filter($param)
572 static $magic_quotes_gpc = NULL;
573 if ($magic_quotes_gpc === NULL)
574 $magic_quotes_gpc = get_magic_quotes_gpc();
576 if (is_array($param)) {
577 return array_map('input_filter', $param);
579 $result = str_replace("\0", '', $param);
580 if ($magic_quotes_gpc) $result = stripslashes($result);
585 // Compat for 3rd party plugins. Remove this later
586 function sanitize($param) {
587 return input_filter($param);
590 // CSV·Á¼°¤Îʸ»úÎó¤òÇÛÎó¤Ë
591 function csv_explode($separator, $string)
593 $retval = $matches = array();
595 $_separator = preg_quote($separator, '/');
596 if (! preg_match_all('/("[^"]*(?:""[^"]*)*"|[^' . $_separator . ']*)' .
597 $_separator . '/', $string . $separator, $matches))
600 foreach ($matches[1] as $str) {
602 if ($len > 1 and $str{0} == '"' and $str{$len - 1} == '"') {
603 $str = str_replace('""', '"', substr($str, 1, -1));
610 // ÇÛÎó¤òCSV·Á¼°¤Îʸ»úÎó¤Ë
611 function csv_implode($glue, $pieces)
613 $_glue = ($glue != '') ? '\\'.$glue{0} : '';
615 foreach ($pieces as $str)
617 if (ereg("[$_glue\"\n\r]",$str))
619 $str = '"'.str_replace('"', '""', $str).'"';
623 return join($glue, $arr);
626 function pkwk_login($pass = '')
630 if ($pass != '' && md5($pass) == $adminpass) {
633 sleep (2); // Blocking brute force attack
641 // is_a -- Returns TRUE if the object is of this class or has this class as one of its parents
643 if (! function_exists('is_a'))
645 function is_a($class, $match)
647 if (empty($class)) return false;
649 $class = is_object($class) ? get_class($class) : $class;
650 if (strtolower($class) == strtolower($match)) {
653 return is_a(get_parent_class($class), $match); // Recurse
658 // array_fill -- Fill an array with values
660 if (! function_exists('array_fill'))
662 function array_fill($start_index, $num, $value)
665 while ($num-- > 0) $ret[$start_index++] = $value;
670 // md5_file -- Calculates the md5 hash of a given filename
672 if (! function_exists('md5_file'))
674 function md5_file($filename)
676 if (! file_exists($filename)) return FALSE;
678 $fd = fopen($filename, 'rb');
679 $data = fread($fd, filesize($filename));