2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: func.php,v 1.4 2004/08/02 13:11:09 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 return file_exists(get_filename($page));
52 function is_editable($page)
55 static $is_editable = array();
57 if (!array_key_exists($page, $is_editable))
59 $is_editable[$page] = (
60 is_pagename($page) and
61 ! is_freeze($page) and
62 ! in_array($page, $cantedit)
66 return $is_editable[$page];
69 // ¥Ú¡¼¥¸¤¬Åà·ë¤µ¤ì¤Æ¤¤¤ë¤«
70 function is_freeze($page)
72 global $function_freeze;
74 if (! $function_freeze or ! is_page($page)) return FALSE;
76 list($lines) = get_source($page);
77 return (rtrim($lines) == '#freeze');
81 function auto_template($page)
83 global $auto_template_func, $auto_template_rules;
85 if (! $auto_template_func) return '';
89 foreach ($auto_template_rules as $rule => $template)
91 if (preg_match("/$rule/", $page, $matches))
93 $template_page = preg_replace("/$rule/", $template, $page);
94 if (is_page($template_page))
96 $body = join('', get_source($template_page));
97 // ¸«½Ð¤·¤Î¸ÇÍIDÉô¤òºï½ü
98 $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $body);
100 $body = preg_replace('/^#freeze\s*$/m', '', $body);
102 for ($i = 0; $i < count($matches); $i++) {
103 $body = str_replace("\$$i", $matches[$i], $body);
113 function get_search_words($words, $special = FALSE)
116 // Perl¥á¥â - Àµ¤·¤¯¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¤µ¤»¤ë
117 // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
118 $eucpre = $eucpost = '';
119 if (SOURCE_ENCODING == 'EUC-JP')
121 $eucpre = '(?<!\x8F)';
122 // # JIS X 0208 ¤¬ 0ʸ»ú°Ê¾å³¤¤¤Æ # ASCII, SS2, SS3 ¤Þ¤¿¤Ï½ªÃ¼
123 $eucpost = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))';
125 $quote_func = create_function('$str', 'return preg_quote($str, "/");');
127 // LANG=='ja'¤Ç¡¢mb_convert_kana¤¬»È¤¨¤ë¾ì¹ç¤Ïmb_convert_kana¤ò»ÈÍÑ
128 $convert_kana = create_function('$str, $option',
129 (LANG == 'ja' and function_exists('mb_convert_kana')) ?
130 'return mb_convert_kana($str, $option);' : 'return $str;'
133 foreach ($words as $word)
135 // ±Ñ¿ô»ú¤ÏȾ³Ñ,¥«¥¿¥«¥Ê¤ÏÁ´³Ñ,¤Ò¤é¤¬¤Ê¤Ï¥«¥¿¥«¥Ê¤Ë
136 $word_zk = $convert_kana($word, 'aKCV');
138 for ($pos = 0; $pos < mb_strlen($word_zk); $pos++)
140 $char = mb_substr($word_zk, $pos,1);
141 // $special : htmlspecialchars()¤òÄ̤¹¤«
142 $arr = array($quote_func($special ? htmlspecialchars($char) : $char));
143 if (strlen($char) == 1) // ±Ñ¿ô»ú
145 foreach (array(strtoupper($char), strtolower($char)) as $_char)
148 $arr[] = $quote_func($_char);
151 $arr[] = sprintf('&#(?:%d|x%x);', $ord, $ord); // ¼ÂÂλ²¾È
152 $arr[] = $quote_func($convert_kana($_char, 'A')); // Á´³Ñ
155 else // ¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú
157 $arr[] = $quote_func($convert_kana($char, 'c')); // ¤Ò¤é¤¬¤Ê
158 $arr[] = $quote_func($convert_kana($char, 'k')); // Ⱦ³Ñ¥«¥¿¥«¥Ê
160 $chars[] = '(?:' . join('|', array_unique($arr)) . ')';
162 $retval[$word] = $eucpre.join('', $chars) . $eucpost;
168 function do_search($word, $type = 'AND', $non_format = FALSE)
170 global $script, $whatsnew, $non_list, $search_non_list;
171 global $_msg_andresult, $_msg_orresult, $_msg_notfoundresult;
176 $b_type = ($type == 'AND'); // AND:TRUE OR:FALSE
177 $keys = get_search_words(preg_split('/\s+/', $word, -1, PREG_SPLIT_NO_EMPTY));
179 $_pages = get_existpages();
182 foreach ($_pages as $page)
184 if ($page == $whatsnew or (! $search_non_list and preg_match("/$non_list/", $page)))
187 // ¸¡º÷Âоݥڡ¼¥¸¤ÎÀ©¸Â¤ò¤«¤±¤ë¤«¤É¤¦¤« (¥Ú¡¼¥¸Ì¾¤ÏÀ©¸Â³°)
188 if ($search_auth and ! check_readable($page, false, false)) {
189 $source = get_source(); // ¸¡º÷Âоݥڡ¼¥¸ÆâÍƤò¶õ¤Ë¡£
191 $source = get_source($page);
194 array_unshift($source, $page); // ¥Ú¡¼¥¸Ì¾¤â¸¡º÷ÂоݤË
197 foreach ($keys as $key) {
198 $tmp = preg_grep("/$key/", $source);
199 $b_match = (count($tmp) > 0);
200 if ($b_match xor $b_type)
204 $pages[$page] = get_filetime($page);
208 return array_keys($pages);
211 $r_word = rawurlencode($word);
212 $s_word = htmlspecialchars($word);
213 if (count($pages) == 0) {
214 return str_replace('$1', $s_word, $_msg_notfoundresult);
219 foreach ($pages as $page=>$time)
221 $r_page = rawurlencode($page);
222 $s_page = htmlspecialchars($page);
223 $passage = get_passage($time);
224 $retval .= " <li><a href=\"$script?cmd=read&page=$r_page&word=$r_word\">$s_page</a>$passage</li>\n";
226 $retval .= "</ul>\n";
228 $retval .= str_replace('$1', $s_word, str_replace('$2', count($pages),
229 str_replace('$3', count($_pages), $b_type ? $_msg_andresult : $_msg_orresult)));
234 // ¥×¥í¥°¥é¥à¤Ø¤Î°ú¿ô¤Î¥Á¥§¥Ã¥¯
235 function arg_check($str)
239 return array_key_exists('cmd', $vars) and (strpos($vars['cmd'], $str) === 0);
242 // ¥Ú¡¼¥¸Ì¾¤Î¥¨¥ó¥³¡¼¥É
243 function encode($key)
245 return ($key == '') ? '' : strtoupper(join('', unpack('H*0', $key)));
248 // ¥Ú¡¼¥¸Ì¾¤Î¥Ç¥³¡¼¥É
249 function decode($key)
251 return ($key == '') ? '' : substr(pack('H*', '20202020' . $key), 4);
255 function strip_bracket($str)
258 if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
266 function page_list($pages, $cmd = 'read', $withfilename = FALSE)
268 global $script, $list_index, $top;
269 global $_msg_symbol, $_msg_other;
270 global $pagereading_enable;
272 // ¥½¡¼¥È¥¡¼¤ò·èÄꤹ¤ë¡£ ' ' < '[a-zA-Z]' < 'zz'¤È¤¤¤¦Á°Äó¡£
278 if($pagereading_enable) {
279 mb_regex_encoding(SOURCE_ENCODING);
280 $readings = get_readings($pages);
283 $list = $matches = array();
284 foreach($pages as $file=>$page)
286 $r_page = rawurlencode($page);
287 $s_page = htmlspecialchars($page, ENT_QUOTES);
288 $passage = get_pg_passage($page);
290 $str = " <li><a href=\"$script?cmd=$cmd&page=$r_page\">$s_page</a>$passage";
293 $s_file = htmlspecialchars($file);
294 $str .= "\n <ul><li>$s_file</li></ul>\n ";
298 if($pagereading_enable) {
299 if(mb_ereg('^([A-Za-z])', mb_convert_kana($page, 'a'), $matches)) {
302 elseif(mb_ereg('^([¥¡-¥ö])', $readings[$page], $matches)) {
305 elseif (mb_ereg('^[ -~]|[^¤¡-¤ó°¡-ô¦]', $page)) {
313 $head = (preg_match('/^([A-Za-z])/', $page, $matches)) ? $matches[1] :
314 (preg_match('/^([ -~])/', $page, $matches) ? $symbol : $other);
317 $list[$head][$page] = $str;
322 $arr_index = array();
324 foreach ($list as $head=>$pages)
326 if ($head === $symbol) {
327 $head = $_msg_symbol;
328 } else if ($head === $other) {
335 $arr_index[] = "<a id=\"top_$cnt\" href=\"#head_$cnt\"><strong>$head</strong></a>";
336 $retval .= " <li><a id=\"head_$cnt\" href=\"#top_$cnt\"><strong>$head</strong></a>\n <ul>\n";
339 $retval .= join("\n", $pages);
342 $retval .= "\n </ul>\n </li>\n";
345 $retval .= "</ul>\n";
346 if ($list_index and $cnt > 0)
349 while (count($arr_index) > 0) {
350 $top[] = join(" | \n", array_splice($arr_index, 0, 16)) . "\n";
352 $retval = "<div id=\"top\" style=\"text-align:center\">\n" .
353 join('<br />', $top) . "</div>\n" . $retval;
358 // ¥Æ¥¥¹¥ÈÀ°·Á¥ë¡¼¥ë¤òɽ¼¨¤¹¤ë
363 if (! is_page($rule_page))
365 return "<p>sorry, $rule_page unavailable.</p>";
367 return convert_html(get_source($rule_page));
370 // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë
371 function die_message($msg)
373 $title = $page = 'Runtime error';
376 <h3>Runtime error</h3>
377 <strong>Error message : $msg</strong>
380 if(defined('SKIN_FILE') && file_exists(SKIN_FILE) && is_readable(SKIN_FILE)) {
381 catbody($title, $page, $body);
383 header('Content-Type: text/html; charset=euc-jp');
385 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
388 <title>$title</title>
389 <meta http-equiv="content-type" content="text/html; charset=euc-jp">
400 // ¸½ºß»þ¹ï¤ò¥Þ¥¤¥¯¥íÉäǼèÆÀ
401 function getmicrotime()
403 list($usec, $sec) = explode(' ', microtime());
404 return ((float)$sec + (float)$usec);
408 function get_date($format, $timestamp = NULL)
410 $time = ($timestamp === NULL) ? UTIME : $timestamp;
413 $format = preg_replace('/(?<!\\\)T/', preg_replace('/(.)/', '\\\$1', ZONE), $format);
415 return date($format, $time);
419 function format_date($val, $paren = FALSE)
421 global $date_format, $time_format, $weeklabels;
425 $ins_date = date($date_format, $val);
426 $ins_time = date($time_format, $val);
427 $ins_week = '(' . $weeklabels[date('w', $val)] . ')';
429 $ins = "$ins_date $ins_week $ins_time";
430 return $paren ? "($ins)" : $ins;
433 // ·Ð²á»þ¹ïʸ»úÎó¤òºî¤ë
434 function get_passage($time, $paren = TRUE)
436 static $units = array('m'=>60, 'h'=>24, 'd'=>1);
438 $time = max(0, (UTIME - $time) / 60); // minutes
440 foreach ($units as $unit=>$card) {
441 if ($time < $card) break;
444 $time = floor($time) . $unit;
446 return $paren ? "($time)" : $time;
449 // <input type="(submit|button|image)"...>¤ò±£¤¹
450 function drop_submit($str)
453 '/<input([^>]+)type="(submit|button|image)"/i',
454 '<input$1type="hidden"',
459 // AutoLink¤Î¥Ñ¥¿¡¼¥ó¤òÀ¸À®¤¹¤ë
461 function get_autolink_pattern(& $pages)
463 global $WikiName, $autolink, $nowikiname;
465 $config = &new Config('AutoLink');
467 $ignorepages = $config->get('IgnoreList');
468 $forceignorepages = $config->get('ForceIgnoreList');
470 $auto_pages = array_merge($ignorepages, $forceignorepages);
472 foreach ($pages as $page) {
473 if (preg_match("/^$WikiName$/", $page) ?
474 $nowikiname : strlen($page) >= $autolink)
475 $auto_pages[] = $page;
478 if (count($auto_pages) == 0) {
479 return $nowikiname ? '(?!)' : $WikiName;
482 $auto_pages = array_unique($auto_pages);
483 sort($auto_pages, SORT_STRING);
485 $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
486 $auto_pages = array_values(array_diff($auto_pages, $auto_pages_a));
488 $result = get_autolink_pattern_sub($auto_pages, 0, count($auto_pages), 0);
489 $result_a = get_autolink_pattern_sub($auto_pages_a, 0, count($auto_pages_a), 0);
491 return array($result, $result_a, $forceignorepages);
494 function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
496 if ($end == 0) return '(?!)';
500 $x = (mb_strlen($pages[$start]) <= $pos);
506 for ($i = $start; $i < $end; $i = $j) // What is the initial state of $j?
508 $char = mb_substr($pages[$i], $pos, 1);
509 for ($j = $i; $j < $end; $j++) {
510 if (mb_substr($pages[$j], $pos, 1) != $char)
516 if ($i >= ($j - 1)) {
517 $result .= str_replace(' ', '\\ ', preg_quote(mb_substr($pages[$i], $pos), '/'));
519 $result .= str_replace(' ', '\\ ', preg_quote($char, '/')) .
520 get_autolink_pattern_sub($pages, $i, $j, $pos + 1);
524 if ($x or $count > 1) {
525 $result = '(?:' . $result . ')';
533 // pukiwiki.php¥¹¥¯¥ê¥×¥È¤Îabsolute-uri¤òÀ¸À®
534 function get_script_uri()
536 $script = (SERVER_PORT == 443 ? 'https://' : 'http://'); // scheme
537 $script .= SERVER_NAME; // host
538 $script .= (SERVER_PORT == 80 ? '' : ':' . SERVER_PORT); // port
540 // SCRIPT_NAME ¤¬'/'¤Ç»Ï¤Þ¤Ã¤Æ¤¤¤Ê¤¤¾ì¹ç(cgi¤Ê¤É) REQUEST_URI¤ò»È¤Ã¤Æ¤ß¤ë
544 if (!isset($_SERVER['REQUEST_URI']) or $_SERVER['REQUEST_URI']{0} != '/')
547 // REQUEST_URI¤ò¥Ñ¡¼¥¹¤·¡¢pathÉôʬ¤À¤±¤ò¼è¤ê½Ð¤¹
548 $parse_url = parse_url($script.$_SERVER['REQUEST_URI']);
549 if (! isset($parse_url['path']) or $parse_url['path']{0} != '/')
552 $path = $parse_url['path'];
554 $script .= $path; // path
560 ÊÑ¿ôÆâ¤Înull(\0)¥Ð¥¤¥È¤òºï½ü¤¹¤ë
561 PHP¤Ïfopen("hoge.php\0.txt")¤Ç"hoge.php"¤ò³«¤¤¤Æ¤·¤Þ¤¦¤Ê¤É¤ÎÌäÂꤢ¤ê
563 http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
564 [PHP-users 12736] null byte attack
566 2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
567 2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥¡¼¤Ïbinary safe
569 function input_filter($param)
571 static $magic_quotes_gpc = NULL;
572 if ($magic_quotes_gpc === NULL)
573 $magic_quotes_gpc = get_magic_quotes_gpc();
575 if (is_array($param)) {
576 return array_map('input_filter', $param);
578 $result = str_replace("\0", '', $param);
579 if ($magic_quotes_gpc) $result = stripslashes($result);
584 // Compat for 3rd party plugins. Remove this later
585 function sanitize($param) {
586 return input_filter($param);
589 // CSV·Á¼°¤Îʸ»úÎó¤òÇÛÎó¤Ë
590 function csv_explode($separator, $string)
592 $retval = $matches = array();
594 $_separator = preg_quote($separator, '/');
595 if (! preg_match_all('/("[^"]*(?:""[^"]*)*"|[^' . $_separator . ']*)' .
596 $_separator . '/', $string . $separator, $matches))
599 foreach ($matches[1] as $str) {
601 if ($len > 1 and $str{0} == '"' and $str{$len - 1} == '"') {
602 $str = str_replace('""', '"', substr($str, 1, -1));
609 // ÇÛÎó¤òCSV·Á¼°¤Îʸ»úÎó¤Ë
610 function csv_implode($glue, $pieces)
612 $_glue = ($glue != '') ? '\\'.$glue{0} : '';
614 foreach ($pieces as $str)
616 if (ereg("[$_glue\"\n\r]",$str))
618 $str = '"'.str_replace('"', '""', $str).'"';
622 return join($glue, $arr);
625 function pkwk_login($pass = '')
629 if ($pass != '' && md5($pass) == $adminpass) {
632 sleep (2); // Blocking brute force attack
640 // is_a -- Returns TRUE if the object is of this class or has this class as one of its parents
642 if (! function_exists('is_a'))
644 function is_a($class, $match)
646 if (empty($class)) return false;
648 $class = is_object($class) ? get_class($class) : $class;
649 if (strtolower($class) == strtolower($match)) {
652 return is_a(get_parent_class($class), $match); // Recurse
657 // array_fill -- Fill an array with values
659 if (! function_exists('array_fill'))
661 function array_fill($start_index, $num, $value)
664 while ($num-- > 0) $ret[$start_index++] = $value;
669 // md5_file -- Calculates the md5 hash of a given filename
671 if (! function_exists('md5_file'))
673 function md5_file($filename)
675 if (! file_exists($filename)) return FALSE;
677 $fd = fopen($filename, 'rb');
678 $data = fread($fd, filesize($filename));