OSDN Git Service

BugTrack2/235: Sort as string
[pukiwiki/pukiwiki.git] / lib / func.php
index 92e1fd6..f4f13b7 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: func.php,v 1.73 2006/05/15 16:41:39 teanan Exp $
+// $Id: func.php,v 1.104 2011/01/25 15:01:01 henoheno Exp $
 // Copyright (C)
 //   2002-2006 PukiWiki Developers Team
 //   2001-2002 Originally written by yu-ji
@@ -80,12 +80,12 @@ function is_freeze($page, $clearcache = FALSE)
                return FALSE;
        } else {
                $fp = fopen(get_filename($page), 'rb') or
-                       die('is_freeze(): fopen() failed: ' . htmlspecialchars($page));
+                       die('is_freeze(): fopen() failed: ' . htmlsc($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));
+               fclose($fp) or die('is_freeze(): fclose() failed: ' . htmlsc($page));
 
                $is_freeze[$page] = ($buffer != FALSE && rtrim($buffer, "\r\n") == '#freeze');
                return $is_freeze[$page];
@@ -182,7 +182,7 @@ function get_search_words($words = array(), $do_escape = FALSE)
                        $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));
+                       $or = array(preg_quote($do_escape ? htmlsc($char) : $char, $quote));
                        if (strlen($char) == 1) {
                                // An ASCII (single-byte) character
                                foreach (array(strtoupper($char), strtolower($char)) as $_char) {
@@ -263,16 +263,16 @@ function do_search($word, $type = 'AND', $non_format = FALSE, $base = '')
        if ($non_format) return array_keys($pages);
 
        $r_word = rawurlencode($word);
-       $s_word = htmlspecialchars($word);
+       $s_word = htmlsc($word);
        if (empty($pages))
                return str_replace('$1', $s_word, $_msg_notfoundresult);
 
-       ksort($pages);
+       ksort($pages, SORT_STRING);
 
        $retval = '<ul>' . "\n";
        foreach (array_keys($pages) as $page) {
                $r_page  = rawurlencode($page);
-               $s_page  = htmlspecialchars($page);
+               $s_page  = htmlsc($page);
                $passage = $show_passage ? ' ' . get_passage(get_filetime($page)) : '';
                $retval .= ' <li><a href="' . $script . '?cmd=read&amp;page=' .
                        $r_page . '&amp;word=' . $r_word . '">' . $s_page .
@@ -294,21 +294,22 @@ function arg_check($str)
 }
 
 // Encode page-name
-function encode($key)
+function encode($str)
 {
-       return ($key == '') ? '' : strtoupper(bin2hex($key));
+       $str = strval($str);
+       return ($str == '') ? '' : strtoupper(bin2hex($str));
        // Equal to strtoupper(join('', unpack('H*0', $key)));
        // But PHP 4.3.10 says 'Warning: unpack(): Type H: outside of string in ...'
 }
 
 // Decode page name
-function decode($key)
+function decode($str)
 {
-       return hex2bin($key);
+       return pkwk_hex2bin($str);
 }
 
 // Inversion of bin2hex()
-function hex2bin($hex_string)
+function pkwk_hex2bin($hex_string)
 {
        // preg_match : Avoid warning : pack(): Type H: illegal hex digit ...
        // (string)   : Always treat as string (not int etc). See BugTrack2/31
@@ -356,14 +357,14 @@ function page_list($pages, $cmd = 'read', $withfilename = FALSE)
 
        foreach($pages as $file=>$page) {
                $r_page  = rawurlencode($page);
-               $s_page  = htmlspecialchars($page, ENT_QUOTES);
+               $s_page  = htmlsc($page, ENT_QUOTES);
                $passage = get_pg_passage($page);
 
                $str = '   <li><a href="' . $href . $r_page . '">' .
                        $s_page . '</a>' . $passage;
 
                if ($withfilename) {
-                       $s_file = htmlspecialchars($file);
+                       $s_file = htmlsc($file);
                        $str .= "\n" . '    <ul><li>' . $s_file . '</li></ul>' .
                                "\n" . '   ';
                }
@@ -382,7 +383,7 @@ function page_list($pages, $cmd = 'read', $withfilename = FALSE)
                        }
                } else {
                        $head = (preg_match('/^([A-Za-z])/', $page, $matches)) ? $matches[1] :
-                               (preg_match('/^([ -~])/', $page, $matches) ? $symbol : $other);
+                               (preg_match('/^([ -~])/', $page) ? $symbol : $other);
                }
 
                $list[$head][$page] = $str;
@@ -431,7 +432,7 @@ function catrule()
        global $rule_page;
 
        if (! is_page($rule_page)) {
-               return '<p>Sorry, page \'' . htmlspecialchars($rule_page) .
+               return '<p>Sorry, page \'' . htmlsc($rule_page) .
                        '\' unavailable.</p>';
        } else {
                return convert_html(get_source($rule_page));
@@ -631,7 +632,7 @@ function get_script_uri($init_uri = '')
        if (isset($script_directory_index)) {
                if (! file_exists($script_directory_index))
                        die_message('Directory index file not found: ' .
-                               htmlspecialchars($script_directory_index));
+                               htmlsc($script_directory_index));
                $matches = array();
                if (preg_match('#^(.+/)' . preg_quote($script_directory_index, '#') . '$#',
                        $script, $matches)) $script = $matches[1];
@@ -701,6 +702,13 @@ function csv_implode($glue, $pieces)
        return join($glue, $arr);
 }
 
+// Sugar with default settings
+function htmlsc($string = '', $flags = ENT_COMPAT, $charset = CONTENT_CHARSET)
+{
+       return htmlspecialchars($string, $flags, $charset);     // htmlsc()
+}
+
+
 //// Compat ////
 
 // is_a --  Returns TRUE if the object is of this class or has this class as one of its parents