<?php
// PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: func.php,v 1.24 2005/01/10 03:18:58 henoheno Exp $
+// $Id: func.php,v 1.54 2006/04/10 14:44:07 henoheno Exp $
+// Copyright (C)
+// 2002-2005 PukiWiki Developers Team
+// 2001-2002 Originally written by yu-ji
+// License: GPL v2 or (at your option) any later version
//
// General functions
$is_freeze[$page] = FALSE;
return FALSE;
} else {
- $fp = fopen(get_filename($page), 'rb');
- $buffer = fgets($fp, 8);
- fclose($fp);
-
- $is_freeze[$page] = ($buffer != FALSE && rtrim($buffer) == '#freeze');
+ $fp = fopen(get_filename($page), 'rb') or
+ die('is_freeze(): fopen() failed: ' . htmlspecialchars($page));
+ flock($fp, LOCK_SH) or die('is_freeze(): flock() failed');
+ rewind($fp);
+ $buffer = fgets($fp, 9);
+ flock($fp, LOCK_UN) or die('is_freeze(): flock() failed');
+ fclose($fp) or die('is_freeze(): fclose() failed: ' . htmlspecialchars($page));
+
+ $is_freeze[$page] = ($buffer != FALSE && rtrim($buffer, "\r\n") == '#freeze');
return $is_freeze[$page];
}
}
-// ¼«Æ°¥Æ¥ó¥×¥ì¡¼¥È
+// Handling $non_list
+// $non_list will be preg_quote($str, '/') later.
+function check_non_list($page = '')
+{
+ global $non_list;
+ static $regex;
+
+ if (! isset($regex)) $regex = '/' . $non_list . '/';
+
+ return preg_match($regex, $page);
+}
+
+// Auto template
function auto_template($page)
{
global $auto_template_func, $auto_template_rules;
return $body;
}
-// ¸¡º÷¸ì¤òŸ³«¤¹¤ë
-function get_search_words($words, $special = FALSE)
+// Expand all search-words to regexes and push them into an array
+function get_search_words($words = array(), $do_escape = FALSE)
{
- $retval = array();
+ static $init, $mb_convert_kana, $pre, $post, $quote = '/';
- // Perl¥á¥â - Àµ¤·¤¯¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¤µ¤»¤ë
- // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
-
- $eucpre = $eucpost = '';
- if (SOURCE_ENCODING == 'EUC-JP') {
- $eucpre = '(?<!\x8F)';
- // # JIS X 0208 ¤¬ 0ʸ»ú°Ê¾å³¤¤¤Æ # ASCII, SS2, SS3 ¤Þ¤¿¤Ï½ªÃ¼
- $eucpost = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))';
+ if (! isset($init)) {
+ // function: mb_convert_kana() is for Japanese code only
+ if (LANG == 'ja' && function_exists('mb_convert_kana')) {
+ $mb_convert_kana = create_function('$str, $option',
+ 'return mb_convert_kana($str, $option, SOURCE_ENCODING);');
+ } else {
+ $mb_convert_kana = create_function('$str, $option',
+ 'return $str;');
+ }
+ if (SOURCE_ENCODING == 'EUC-JP') {
+ // Perl memo - Correct pattern-matching with EUC-JP
+ // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match (Japanese)
+ $pre = '(?<!\x8F)';
+ $post = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*' . // JIS X 0208
+ '(?:[\x00-\x7F\x8E\x8F]|\z))'; // ASCII, SS2, SS3, or the last
+ } else {
+ $pre = $post = '';
+ }
+ $init = TRUE;
}
- $quote_func = create_function('$str', 'return preg_quote($str, \'/\');');
- // LANG == 'ja'¤Ç¡¢mb_convert_kana¤¬»È¤¨¤ë¾ì¹ç¤Ïmb_convert_kana¤ò»ÈÍÑ
- $convert_kana = create_function('$str, $option',
- (LANG == 'ja' && function_exists('mb_convert_kana')) ?
- 'return mb_convert_kana($str, $option);' : 'return $str;');
+ if (! is_array($words)) $words = array($words);
+ // Generate regex for the words
+ $regex = array();
foreach ($words as $word) {
- // ±Ñ¿ô»ú¤ÏȾ³Ñ,¥«¥¿¥«¥Ê¤ÏÁ´³Ñ,¤Ò¤é¤¬¤Ê¤Ï¥«¥¿¥«¥Ê¤Ë
- $word_zk = $convert_kana($word, 'aKCV');
+ $word = trim($word);
+ if ($word == '') continue;
+
+ // Normalize: ASCII letters = to single-byte. Others = to Zenkaku and Katakana
+ $word_nm = $mb_convert_kana($word, 'aKCV');
+ $nmlen = mb_strlen($word_nm, SOURCE_ENCODING);
+
+ // Each chars may be served ...
$chars = array();
- for ($pos = 0; $pos < mb_strlen($word_zk); $pos++) {
- $char = mb_substr($word_zk, $pos, 1);
- // $special : htmlspecialchars()¤òÄ̤¹¤«
- $arr = array($quote_func($special ? htmlspecialchars($char) : $char));
+ for ($pos = 0; $pos < $nmlen; $pos++) {
+ $char = mb_substr($word_nm, $pos, 1, SOURCE_ENCODING);
+
+ // Just normalized one? (ASCII char or Zenkaku-Katakana?)
+ $or = array(preg_quote($do_escape ? htmlspecialchars($char) : $char, $quote));
if (strlen($char) == 1) {
- // ±Ñ¿ô»ú
+ // An ASCII (single-byte) character
foreach (array(strtoupper($char), strtolower($char)) as $_char) {
- if ($char != '&')
- $arr[] = $quote_func($_char);
- $ord = ord($_char);
- $arr[] = sprintf('&#(?:%d|x%x);', $ord, $ord); // ¼ÂÂλ²¾È
- $arr[] = $quote_func($convert_kana($_char, 'A')); // Á´³Ñ
+ if ($char != '&') $or[] = preg_quote($_char, $quote); // As-is?
+ $ascii = ord($_char);
+ $or[] = sprintf('&#(?:%d|x%x);', $ascii, $ascii); // As an entity reference?
+ $or[] = preg_quote($mb_convert_kana($_char, 'A'), $quote); // As Zenkaku?
}
} else {
- // ¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú
- $arr[] = $quote_func($convert_kana($char, 'c')); // ¤Ò¤é¤¬¤Ê
- $arr[] = $quote_func($convert_kana($char, 'k')); // Ⱦ³Ñ¥«¥¿¥«¥Ê
+ // NEVER COME HERE with mb_substr(string, start, length, 'ASCII')
+ // A multi-byte character
+ $or[] = preg_quote($mb_convert_kana($char, 'c'), $quote); // As Hiragana?
+ $or[] = preg_quote($mb_convert_kana($char, 'k'), $quote); // As Hankaku-Katakana?
}
- $chars[] = '(?:' . join('|', array_unique($arr)) . ')';
+ $chars[] = '(?:' . join('|', array_unique($or)) . ')'; // Regex for the character
}
- $retval[$word] = $eucpre.join('', $chars) . $eucpost;
+
+ $regex[$word] = $pre . join('', $chars) . $post; // For the word
}
- return $retval;
+
+ return $regex; // For all words
}
// 'Search' main function
-function do_search($word, $type = 'AND', $non_format = FALSE)
+function do_search($word, $type = 'AND', $non_format = FALSE, $base = '')
{
- global $script, $whatsnew, $non_list, $search_non_list;
+ global $script, $whatsnew, $search_non_list;
global $_msg_andresult, $_msg_orresult, $_msg_notfoundresult;
global $search_auth;
$keys = get_search_words(preg_split('/\s+/', $word, -1, PREG_SPLIT_NO_EMPTY));
$_pages = get_existpages();
+ if ($base != '') {
+ $_pages = preg_grep('/^' . $base . '/', $_pages);
+ }
$pages = array();
- $non_list_pattern = '/' . $non_list . '/';
foreach ($_pages as $page) {
- if ($page == $whatsnew || (! $search_non_list && preg_match($non_list_pattern, $page)))
+ if ($page == $whatsnew || (! $search_non_list && check_non_list($page)))
continue;
// ¸¡º÷Âоݥڡ¼¥¸¤ÎÀ©¸Â¤ò¤«¤±¤ë¤«¤É¤¦¤« (¥Ú¡¼¥¸Ì¾¤ÏÀ©¸Â³°)
if ($search_auth && ! check_readable($page, false, false)) {
- $source = get_source(); // ¸¡º÷Âоݥڡ¼¥¸ÆâÍƤò¶õ¤Ë¡£
+ $source = get_source(); // Empty
} else {
$source = get_source($page);
}
return $retval;
}
-// ¥×¥í¥°¥é¥à¤Ø¤Î°ú¿ô¤Î¥Á¥§¥Ã¥¯
+// Argument check for program
function arg_check($str)
{
global $vars;
return isset($vars['cmd']) && (strpos($vars['cmd'], $str) === 0);
}
-// ¥Ú¡¼¥¸Ì¾¤Î¥¨¥ó¥³¡¼¥É
+// Encode page-name
function encode($key)
{
return ($key == '') ? '' : strtoupper(bin2hex($key));
// But PHP 4.3.10 says 'Warning: unpack(): Type H: outside of string in ...'
}
-// ¥Ú¡¼¥¸Ì¾¤Î¥Ç¥³¡¼¥É
+// Decode page name
function decode($key)
{
- return ($key == '') ? '' : substr(pack('H*', '20202020' . $key), 4);
+ return hex2bin($key);
}
-// [[ ]] ¤ò¼è¤ê½ü¤¯
+// Inversion of bin2hex()
+function hex2bin($hex_string)
+{
+ // preg_match : Avoid warning : pack(): Type H: illegal hex digit ...
+ // (string) : Always treat as string (not int etc). See BugTrack2/31
+ return preg_match('/^[0-9a-f]+$/i', $hex_string) ?
+ pack('H*', (string)$hex_string) : $hex_string;
+}
+
+// Remove [[ ]] (brackets)
function strip_bracket($str)
{
$match = array();
}
}
-// ¥Ú¡¼¥¸°ìÍ÷¤ÎºîÀ®
+// Create list of pages
function page_list($pages, $cmd = 'read', $withfilename = FALSE)
{
global $script, $list_index;
}
$list = $matches = array();
+
+ // Shrink URI for read
+ if ($cmd == 'read') {
+ $href = $script . '?';
+ } else {
+ $href = $script . '?cmd=' . $cmd . '&page=';
+ }
+
foreach($pages as $file=>$page) {
$r_page = rawurlencode($page);
$s_page = htmlspecialchars($page, ENT_QUOTES);
$passage = get_pg_passage($page);
- $str = ' <li><a href="' .
- $script . '?cmd=' . $cmd . '&page=' . $r_page .
- '">' . $s_page . '</a>' . $passage;
+ $str = ' <li><a href="' . $href . $r_page . '">' .
+ $s_page . '</a>' . $passage;
if ($withfilename) {
$s_file = htmlspecialchars($file);
if($pagereading_enable) {
if(mb_ereg('^([A-Za-z])', mb_convert_kana($page, 'a'), $matches)) {
$head = $matches[1];
- } elseif(mb_ereg('^([¥¡-¥ö])', $readings[$page], $matches)) { // here
+ } elseif (isset($readings[$page]) && mb_ereg('^([¥¡-¥ö])', $readings[$page], $matches)) { // here
$head = $matches[1];
} elseif (mb_ereg('^[ -~]|[^¤¡-¤ó°¡-ô¦]', $page)) { // and here
$head = $symbol;
exit;
}
-// ¸½ºß»þ¹ï¤ò¥Þ¥¤¥¯¥íÉäǼèÆÀ
+// Have the time (as microtime)
function getmicrotime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$sec + (float)$usec);
}
-// Æü»þ¤òÆÀ¤ë
+// Get the date
function get_date($format, $timestamp = NULL)
{
$format = preg_replace('/(?<!\\\)T/',
return date($format, $time);
}
-// Æü»þʸ»úÎó¤òºî¤ë
+// Format date string
function format_date($val, $paren = FALSE)
{
global $date_format, $time_format, $weeklabels;
$val += ZONETIME;
- $date = date($date_format . ' ' . $time_format, $val) .
- ' ' . '(' . $weeklabels[date('w', $val)] . ')';
+ $date = date($date_format, $val) .
+ ' (' . $weeklabels[date('w', $val)] . ') ' .
+ date($time_format, $val);
return $paren ? '(' . $date . ')' : $date;
}
-// ·Ð²á»þ¹ïʸ»úÎó¤òºî¤ë
+// Get short string of the passage, 'N seconds/minutes/hours/days/years ago'
function get_passage($time, $paren = TRUE)
{
static $units = array('m'=>60, 'h'=>24, 'd'=>1);
'<input$1type="hidden"', $str);
}
-// AutoLink¤Î¥Ñ¥¿¡¼¥ó¤òÀ¸À®¤¹¤ë
-// thx for hirofummy
+// Generate AutoLink patterns (thx to hirofummy)
function get_autolink_pattern(& $pages)
{
global $WikiName, $autolink, $nowikiname;
unset($config);
$auto_pages = array_merge($ignorepages, $forceignorepages);
- foreach ($pages as $page) {
+ foreach ($pages as $page)
if (preg_match('/^' . $WikiName . '$/', $page) ?
$nowikiname : strlen($page) >= $autolink)
$auto_pages[] = $page;
- }
- if (empty($auto_pages))
- return $nowikiname ? '(?!)' : $WikiName;
-
- $auto_pages = array_unique($auto_pages);
- sort($auto_pages, SORT_STRING);
-
- $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
- $auto_pages = array_values(array_diff($auto_pages, $auto_pages_a));
+ if (empty($auto_pages)) {
+ $result = $result_a = $nowikiname ? '(?!)' : $WikiName;
+ } else {
+ $auto_pages = array_unique($auto_pages);
+ sort($auto_pages, SORT_STRING);
- $result = get_autolink_pattern_sub($auto_pages, 0, count($auto_pages), 0);
- $result_a = get_autolink_pattern_sub($auto_pages_a, 0, count($auto_pages_a), 0);
+ $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
+ $auto_pages = array_values(array_diff($auto_pages, $auto_pages_a));
+ $result = get_autolink_pattern_sub($auto_pages, 0, count($auto_pages), 0);
+ $result_a = get_autolink_pattern_sub($auto_pages_a, 0, count($auto_pages_a), 0);
+ }
return array($result, $result_a, $forceignorepages);
}
$x = (mb_strlen($pages[$start]) <= $pos);
if ($x) ++$start;
- for ($i = $start; $i < $end; $i = $j)
- {
+ for ($i = $start; $i < $end; $i = $j) {
$char = mb_substr($pages[$i], $pos, 1);
- for ($j = $i; $j < $end; $j++) {
+ for ($j = $i; $j < $end; $j++)
if (mb_substr($pages[$j], $pos, 1) != $char) break;
- }
+
if ($i != $start) $result .= '|';
if ($i >= ($j - 1)) {
$result .= str_replace(' ', '\\ ', preg_quote(mb_substr($pages[$i], $pos), '/'));
return $result;
}
-// pukiwiki.php¥¹¥¯¥ê¥×¥È¤Îabsolute-uri¤òÀ¸À®
+// Get absolute-URI of this script
function get_script_uri($init_uri = '')
{
global $script_directory_index;
return $script;
}
-/*
-ÊÑ¿ôÆâ¤Înull(\0)¥Ð¥¤¥È¤òºï½ü¤¹¤ë
-PHP¤Ïfopen("hoge.php\0.txt")¤Ç"hoge.php"¤ò³«¤¤¤Æ¤·¤Þ¤¦¤Ê¤É¤ÎÌäÂꤢ¤ê
-
-http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
-[PHP-users 12736] null byte attack
-
-2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
-2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥¡¼¤Ïbinary safe
-*/
+// Remove null(\0) bytes from variables
+//
+// NOTE: PHP had vulnerabilities that opens "hoge.php" via fopen("hoge.php\0.txt") etc.
+// [PHP-users 12736] null byte attack
+// http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
+//
+// 2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
+// 2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥¡¼¤Ïbinary safe
+//
function input_filter($param)
{
static $magic_quotes_gpc = NULL;
return input_filter($param);
}
-// CSV·Á¼°¤Îʸ»úÎó¤òÇÛÎó¤Ë
+// Explode Comma-Separated Values to an array
function csv_explode($separator, $string)
{
$retval = $matches = array();
return join($glue, $arr);
}
-function pkwk_login($pass = '')
-{
- global $adminpass;
-
- if ($pass != '' && md5($pass) == $adminpass) {
- return TRUE;
- } else {
- sleep(2); // Blocking brute force attack
- return FALSE;
- }
-}
-
-
//// Compat ////
// is_a -- Returns TRUE if the object is of this class or has this class as one of its parents
return md5($data);
}
}
+
+// sha1 -- Compute SHA-1 hash
+// (PHP 4 >= 4.3.0, PHP5)
+if (! function_exists('sha1')) {
+ if (extension_loaded('mhash')) {
+ function sha1($str)
+ {
+ return bin2hex(mhash(MHASH_SHA1, $str));
+ }
+ } else {
+ function sha1($str, $raw_output = FALSE)
+ {
+ die('Function sha1() not found and extension \'mhash\' not exists');
+ }
+ }
+}
?>