2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: func.php,v 1.65 2004/07/03 05:02:43 henoheno Exp $
8 // ʸ»úÎó¤¬InterWikiName¤«¤É¤¦¤«
9 function is_interwiki($str)
11 global $InterWikiName;
13 return preg_match("/^$InterWikiName$/",$str);
16 // ʸ»úÎ󤬥ڡ¼¥¸Ì¾¤«¤É¤¦¤«
17 function is_pagename($str)
19 global $BracketName,$WikiName;
21 $is_pagename = (!is_interwiki($str) and preg_match("/^(?!\/)$BracketName$(?<!\/$)/",$str)
22 and !preg_match('/(^|\/)\.{1,2}(\/|$)/',$str));
24 if (defined('SOURCE_ENCODING'))
26 if (SOURCE_ENCODING == 'UTF-8')
28 $is_pagename = ($is_pagename and preg_match('/^(?:[\x00-\x7F]|(?:[\xC0-\xDF][\x80-\xBF])|(?:[\xE0-\xEF][\x80-\xBF][\x80-\xBF]))+$/',$str)); // UTF-8
30 else if (SOURCE_ENCODING == 'EUC-JP')
32 $is_pagename = ($is_pagename and preg_match('/^(?:[\x00-\x7F]|(?:[\x8E\xA1-\xFE][\xA1-\xFE])|(?:\x8F[\xA1-\xFE][\xA1-\xFE]))+$/',$str)); // EUC-JP
39 // ʸ»úÎó¤¬URL¤«¤É¤¦¤«
40 function is_url($str,$only_http=FALSE)
42 $scheme = $only_http ? 'https?' : 'https?|ftp|news';
43 return preg_match('/^('.$scheme.')(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]*)$/', $str);
47 function is_page($page,$reload=FALSE)
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
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))
80 list($lines) = get_source($page);
81 return (rtrim($lines) == '#freeze');
85 function auto_template($page)
87 global $auto_template_func,$auto_template_rules;
89 if (!$auto_template_func)
95 foreach ($auto_template_rules as $rule => $template)
97 if (preg_match("/$rule/",$page,$matches))
99 $template_page = preg_replace("/$rule/",$template,$page);
100 if (is_page($template_page))
102 $body = join('',get_source($template_page));
103 // ¸«½Ð¤·¤Î¸ÇÍIDÉô¤òºï½ü
104 $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m','$1$2',$body);
106 $body = preg_replace('/^#freeze\s*$/m','',$body);
108 for ($i = 0; $i < count($matches); $i++)
110 $body = str_replace("\$$i",$matches[$i],$body);
120 function get_search_words($words,$special=FALSE)
123 // Perl¥á¥â - Àµ¤·¤¯¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¤µ¤»¤ë
124 // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
125 $eucpre = $eucpost = '';
126 if (SOURCE_ENCODING == 'EUC-JP')
128 $eucpre = '(?<!\x8F)';
129 // # JIS X 0208 ¤¬ 0ʸ»ú°Ê¾å³¤¤¤Æ # ASCII, SS2, SS3 ¤Þ¤¿¤Ï½ªÃ¼
130 $eucpost = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))';
132 $quote_func = create_function('$str','return preg_quote($str,"/");');
133 // LANG=='ja'¤Ç¡¢mb_convert_kana¤¬»È¤¨¤ë¾ì¹ç¤Ïmb_convert_kana¤ò»ÈÍÑ
134 $convert_kana = create_function('$str,$option',
135 (LANG == 'ja' and function_exists('mb_convert_kana')) ?
136 'return mb_convert_kana($str,$option);' : 'return $str;'
139 foreach ($words as $word)
141 // ±Ñ¿ô»ú¤ÏȾ³Ñ,¥«¥¿¥«¥Ê¤ÏÁ´³Ñ,¤Ò¤é¤¬¤Ê¤Ï¥«¥¿¥«¥Ê¤Ë
142 $word_zk = $convert_kana($word,'aKCV');
144 for ($pos = 0; $pos < mb_strlen($word_zk);$pos++)
146 $char = mb_substr($word_zk,$pos,1);
147 // $special : htmlspecialchars()¤òÄ̤¹¤«
148 $arr = array($quote_func($special ? htmlspecialchars($char) : $char));
149 if (strlen($char) == 1) // ±Ñ¿ô»ú
151 foreach (array(strtoupper($char),strtolower($char)) as $_char)
155 $arr[] = $quote_func($_char);
158 $arr[] = sprintf('&#(?:%d|x%x);',$ord,$ord); // ¼ÂÂλ²¾È
159 $arr[] = $quote_func($convert_kana($_char,"A")); // Á´³Ñ
162 else // ¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú
164 $arr[] = $quote_func($convert_kana($char,"c")); // ¤Ò¤é¤¬¤Ê
165 $arr[] = $quote_func($convert_kana($char,"k")); // Ⱦ³Ñ¥«¥¿¥«¥Ê
167 $chars[] = '(?:'.join('|',array_unique($arr)).')';
169 $retval[$word] = $eucpre.join('',$chars).$eucpost;
175 function do_search($word,$type='AND',$non_format=FALSE)
177 global $script,$vars,$whatsnew,$non_list,$search_non_list;
178 global $_msg_andresult,$_msg_orresult,$_msg_notfoundresult;
184 $b_type = ($type == 'AND'); // AND:TRUE OR:FALSE
185 $keys = get_search_words(preg_split('/\s+/',$word,-1,PREG_SPLIT_NO_EMPTY));
187 $_pages = get_existpages();
190 foreach ($_pages as $page)
192 if ($page == $whatsnew
193 or (!$search_non_list and preg_match("/$non_list/",$page)))
198 // ¸¡º÷Âоݥڡ¼¥¸¤ÎÀ©¸Â¤ò¤«¤±¤ë¤«¤É¤¦¤« (¥Ú¡¼¥¸Ì¾¤ÏÀ©¸Â³°)
199 if ($search_auth and !check_readable($page,false,false)) {
200 $source = get_source(); // ¸¡º÷Âоݥڡ¼¥¸ÆâÍƤò¶õ¤Ë¡£
202 $source = get_source($page);
206 array_unshift($source,$page); // ¥Ú¡¼¥¸Ì¾¤â¸¡º÷ÂоݤË
210 foreach ($keys as $key)
212 $tmp = preg_grep("/$key/",$source);
213 $b_match = (count($tmp) > 0);
214 if ($b_match xor $b_type)
221 $pages[$page] = get_filetime($page);
226 return array_keys($pages);
228 $r_word = rawurlencode($word);
229 $s_word = htmlspecialchars($word);
230 if (count($pages) == 0)
232 return str_replace('$1',$s_word,$_msg_notfoundresult);
236 foreach ($pages as $page=>$time)
238 $r_page = rawurlencode($page);
239 $s_page = htmlspecialchars($page);
240 $passage = get_passage($time);
241 $retval .= " <li><a href=\"$script?cmd=read&page=$r_page&word=$r_word\">$s_page</a>$passage</li>\n";
243 $retval .= "</ul>\n";
245 $retval .= str_replace('$1',$s_word,str_replace('$2',count($pages),
246 str_replace('$3',count($_pages),$b_type ? $_msg_andresult : $_msg_orresult)));
251 // ¥×¥í¥°¥é¥à¤Ø¤Î°ú¿ô¤Î¥Á¥§¥Ã¥¯
252 function arg_check($str)
256 return array_key_exists('cmd',$vars) and (strpos($vars['cmd'],$str) === 0);
259 // ¥Ú¡¼¥¸Ì¾¤Î¥¨¥ó¥³¡¼¥É
260 function encode($key)
262 return ($key == '') ? '' : strtoupper(join('',unpack('H*0',$key)));
265 // ¥Ú¡¼¥¸Ì¾¤Î¥Ç¥³¡¼¥É
266 function decode($key)
268 return ($key == '') ? '' : substr(pack('H*','20202020'.$key),4);
272 function strip_bracket($str)
274 if (preg_match('/^\[\[(.*)\]\]$/',$str,$match))
282 function page_list($pages, $cmd = 'read', $withfilename=FALSE)
284 global $script,$list_index,$top;
285 global $_msg_symbol,$_msg_other;
286 global $pagereading_enable;
288 // ¥½¡¼¥È¥¡¼¤ò·èÄꤹ¤ë¡£ ' ' < '[a-zA-Z]' < 'zz'¤È¤¤¤¦Á°Äó¡£
294 if($pagereading_enable) {
295 mb_regex_encoding(SOURCE_ENCODING);
296 $readings = get_readings($pages);
300 foreach($pages as $file=>$page)
302 $r_page = rawurlencode($page);
303 $s_page = htmlspecialchars($page,ENT_QUOTES);
304 $passage = get_pg_passage($page);
306 $str = " <li><a href=\"$script?cmd=$cmd&page=$r_page\">$s_page</a>$passage";
310 $s_file = htmlspecialchars($file);
311 $str .= "\n <ul><li>$s_file</li></ul>\n ";
315 if($pagereading_enable) {
316 if(mb_ereg('^([A-Za-z])',mb_convert_kana($page,'a'),$matches)) {
319 elseif(mb_ereg('^([¥¡-¥ö])',$readings[$page],$matches)) {
322 elseif (mb_ereg('^[ -~]|[^¤¡-¤ó°¡-ô¦]',$page)) {
330 $head = (preg_match('/^([A-Za-z])/',$page,$matches)) ? $matches[1] :
331 (preg_match('/^([ -~])/',$page,$matches) ? $symbol : $other);
334 $list[$head][$page] = $str;
339 $arr_index = array();
341 foreach ($list as $head=>$pages)
343 if ($head === $symbol)
345 $head = $_msg_symbol;
347 else if ($head === $other)
355 $arr_index[] = "<a id=\"top_$cnt\" href=\"#head_$cnt\"><strong>$head</strong></a>";
356 $retval .= " <li><a id=\"head_$cnt\" href=\"#top_$cnt\"><strong>$head</strong></a>\n <ul>\n";
359 $retval .= join("\n",$pages);
362 $retval .= "\n </ul>\n </li>\n";
365 $retval .= "</ul>\n";
366 if ($list_index and $cnt > 0)
369 while (count($arr_index) > 0)
371 $top[] = join(" | \n",array_splice($arr_index,0,16))."\n";
373 $retval = "<div id=\"top\" style=\"text-align:center\">\n".
374 join("<br />",$top)."</div>\n".$retval;
379 // ¥Æ¥¥¹¥ÈÀ°·Á¥ë¡¼¥ë¤òɽ¼¨¤¹¤ë
384 if (!is_page($rule_page))
386 return "<p>sorry, $rule_page unavailable.</p>";
388 return convert_html(get_source($rule_page));
391 // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë
392 function die_message($msg)
394 $title = $page = 'Runtime error';
397 <h3>Runtime error</h3>
398 <strong>Error message : $msg</strong>
401 if(defined('SKIN_FILE') && file_exists(SKIN_FILE) && is_readable(SKIN_FILE))
403 catbody($title,$page,$body);
407 header('Content-Type: text/html; charset=euc-jp');
409 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
412 <title>$title</title>
413 <meta http-equiv="content-type" content="text/html; charset=euc-jp">
424 // ¸½ºß»þ¹ï¤ò¥Þ¥¤¥¯¥íÉäǼèÆÀ
425 function getmicrotime()
427 list($usec, $sec) = explode(' ',microtime());
428 return ((float)$sec + (float)$usec);
432 function get_date($format,$timestamp = NULL)
434 $time = ($timestamp === NULL) ? UTIME : $timestamp;
437 $format = preg_replace('/(?<!\\\)T/',preg_replace('/(.)/','\\\$1',ZONE),$format);
439 return date($format,$time);
443 function format_date($val, $paren = FALSE)
445 global $date_format,$time_format,$weeklabels;
449 $ins_date = date($date_format,$val);
450 $ins_time = date($time_format,$val);
451 $ins_week = '('.$weeklabels[date('w',$val)].')';
453 $ins = "$ins_date $ins_week $ins_time";
454 return $paren ? "($ins)" : $ins;
457 // ·Ð²á»þ¹ïʸ»úÎó¤òºî¤ë
458 function get_passage($time, $paren = TRUE)
460 static $units = array('m'=>60,'h'=>24,'d'=>1);
462 $time = max(0,(UTIME - $time) / 60); //minutes
464 foreach ($units as $unit=>$card)
472 $time = floor($time).$unit;
474 return $paren ? "($time)" : $time;
477 //<input type="(submit|button|image)"...>¤ò±£¤¹
478 function drop_submit($str)
481 '/<input([^>]+)type="(submit|button|image)"/i',
482 '<input$1type="hidden"',
487 // AutoLink¤Î¥Ñ¥¿¡¼¥ó¤òÀ¸À®¤¹¤ë
489 function get_autolink_pattern(&$pages)
491 global $WikiName,$autolink,$nowikiname;
493 $config = &new Config('AutoLink');
495 $ignorepages = $config->get('IgnoreList');
496 $forceignorepages = $config->get('ForceIgnoreList');
498 $auto_pages = array_merge($ignorepages,$forceignorepages);
500 foreach ($pages as $page)
502 if (preg_match("/^$WikiName$/",$page) ?
503 $nowikiname : strlen($page) >= $autolink)
505 $auto_pages[] = $page;
509 if (count($auto_pages) == 0)
511 return $nowikiname ? '(?!)' : $WikiName;
514 $auto_pages = array_unique($auto_pages);
515 sort($auto_pages,SORT_STRING);
516 $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i',$auto_pages));
517 $auto_pages = array_values(array_diff($auto_pages,$auto_pages_a));
519 $result = get_autolink_pattern_sub($auto_pages,0,count($auto_pages),0);
520 $result_a = get_autolink_pattern_sub($auto_pages_a,0,count($auto_pages_a),0);
522 return array($result,$result_a,$forceignorepages);
525 function get_autolink_pattern_sub(&$pages,$start,$end,$pos)
534 $x = (mb_strlen($pages[$start]) <= $pos);
540 for ($i = $start; $i < $end; $i = $j)
542 $char = mb_substr($pages[$i],$pos,1);
543 for ($j = $i; $j < $end; $j++)
545 if (mb_substr($pages[$j],$pos,1) != $char)
556 $result .= str_replace(' ','\\ ',preg_quote(mb_substr($pages[$i],$pos),'/'));
561 str_replace(' ','\\ ',preg_quote($char,'/')).
562 get_autolink_pattern_sub($pages,$i,$j,$pos + 1);
566 if ($x or $count > 1)
568 $result = '(?:'.$result.')';
577 // pukiwiki.php¥¹¥¯¥ê¥×¥È¤Îabsolute-uri¤òÀ¸À®
578 function get_script_uri()
580 $script = (SERVER_PORT == 443 ? 'https://' : 'http://'); // scheme
581 $script .= SERVER_NAME; // host
582 $script .= (SERVER_PORT == 80 ? '' : ':' . SERVER_PORT); // port
584 // SCRIPT_NAME ¤¬'/'¤Ç»Ï¤Þ¤Ã¤Æ¤¤¤Ê¤¤¾ì¹ç(cgi¤Ê¤É) REQUEST_URI¤ò»È¤Ã¤Æ¤ß¤ë
588 if (!isset($_SERVER['REQUEST_URI']) or $_SERVER['REQUEST_URI']{0} != '/')
591 // REQUEST_URI¤ò¥Ñ¡¼¥¹¤·¡¢pathÉôʬ¤À¤±¤ò¼è¤ê½Ð¤¹
592 $parse_url = parse_url($script.$_SERVER['REQUEST_URI']);
593 if (!isset($parse_url['path']) or $parse_url['path']{0} != '/')
596 $path = $parse_url['path'];
598 $script .= $path; // path
604 ÊÑ¿ôÆâ¤Înull(\0)¥Ð¥¤¥È¤òºï½ü¤¹¤ë
605 PHP¤Ïfopen("hoge.php\0.txt")¤Ç"hoge.php"¤ò³«¤¤¤Æ¤·¤Þ¤¦¤Ê¤É¤ÎÌäÂꤢ¤ê
607 http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
608 [PHP-users 12736] null byte attack
610 2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
611 2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥¡¼¤Ïbinary safe
613 function sanitize($param)
615 if (is_array($param))
617 $result = array_map('sanitize',$param);
621 $result = str_replace("\0",'',$param);
622 if (get_magic_quotes_gpc())
624 $result = stripslashes($result);
630 // CSV·Á¼°¤Îʸ»úÎó¤òÇÛÎó¤Ë
631 function csv_explode($separator, $string)
633 $_separator = preg_quote($separator,'/');
634 if (!preg_match_all('/("[^"]*(?:""[^"]*)*"|[^'.$_separator.']*)'.$_separator.'/', $string.$separator, $matches))
640 foreach ($matches[1] as $str)
643 if ($len > 1 and $str{0} == '"' and $str{$len - 1} == '"')
645 $str = str_replace('""', '"', substr($str, 1, -1));
652 // ÇÛÎó¤òCSV·Á¼°¤Îʸ»úÎó¤Ë
653 function csv_implode($glue, $pieces)
655 $_glue = ($glue != '') ? '\\'.$glue{0} : '';
657 foreach ($pieces as $str)
659 if (ereg("[$_glue\"\n\r]",$str))
661 $str = '"'.str_replace('"', '""', $str).'"';
665 return join($glue, $arr);
671 //is_a -- Returns TRUE if the object is of this class or has this class as one of its parents
673 if (!function_exists('is_a'))
675 function is_a($class, $match)
681 $class = is_object($class) ? get_class($class) : $class;
682 if (strtolower($class) == strtolower($match))
686 return is_a(get_parent_class($class), $match);
693 //array_fill -- Fill an array with values
695 if (!function_exists('array_fill'))
697 function array_fill($start_index,$num,$value)
703 $ret[$start_index++] = $value;
712 //md5_file -- Calculates the md5 hash of a given filename
714 if (!function_exists('md5_file'))
716 function md5_file($filename)
718 if (!file_exists($filename))
722 $fd = fopen($filename, 'rb');
723 $data = fread($fd, filesize($filename));