OSDN Git Service

BugTrack/2525 Support make_link
[pukiwiki/pukiwiki.git] / lib / func.php
index b05584d..de6df81 100644 (file)
@@ -2,7 +2,7 @@
 // PukiWiki - Yet another WikiWikiWeb clone.
 // func.php
 // Copyright
-//   2002-2017 PukiWiki Development Team
+//   2002-2021 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -350,6 +350,27 @@ function get_page_link_a_attrs($page)
        );
 }
 
+/**
+ * Get page link general attributes from filetime
+ * @param $filetime
+ * @return array('data_mtime' => page mtime or null, 'class' => additinal classes)
+ */
+function get_filetime_a_attrs($filetime)
+{
+       global $show_passage;
+       if ($show_passage) {
+               $pagemtime = get_date_atom($filetime + LOCALZONE);
+               return array(
+                       'data_mtime' => $pagemtime,
+                       'class' => get_link_passage_class(),
+               );
+       }
+       return array(
+               'data_mtime' => '',
+               'class' => ''
+       );
+}
+
 // 'Search' main function
 function do_search($word, $type = 'AND', $non_format = FALSE, $base = '')
 {
@@ -441,7 +462,7 @@ function arg_check($str)
 
 function _pagename_urlencode_callback($matches)
 {
-       return rawurlencode($matches[0]);
+       return urlencode($matches[0]);
 }
 
 function pagename_urlencode($page)
@@ -503,21 +524,18 @@ function page_list($pages, $cmd = 'read', $withfilename = FALSE)
                mb_regex_encoding(SOURCE_ENCODING);
                $readings = get_readings($pages);
        }
-
        $list = $matches = array();
-
-       // Shrink URI for read
-       if ($cmd == 'read') {
-               $href = $script . '?';
-       } else {
-               $href = $script . '?cmd=' . $cmd . '&page=';
-       }
-
+       uasort($pages, 'strnatcmp');
        foreach($pages as $file=>$page) {
-               $r_page  = pagename_urlencode($page);
                $s_page  = htmlsc($page, ENT_QUOTES);
-               $str = '   <li><a href="' . $href . $r_page . '">' .
-                       $s_page . '</a>' . get_passage_html_span($page);
+               // Shrink URI for read
+               if ($cmd == 'read') {
+                       $href = get_page_uri($page);
+               } else {
+                       $href = $script . '?cmd=' . $cmd . '&amp;page=' . rawurlencode($page);
+               }
+               $str = '   <li><a href="' . $href . '">' .
+                       $s_page . '</a> ' . get_pg_passage($page);
                if ($withfilename) {
                        $s_file = htmlsc($file);
                        $str .= "\n" . '    <ul><li>' . $s_file . '</li></ul>' .
@@ -542,12 +560,12 @@ function page_list($pages, $cmd = 'read', $withfilename = FALSE)
                }
                $list[$head][$page] = $str;
        }
-       uksort($pages, 'strnatcmp');
+       uksort($list, 'strnatcmp');
 
        $cnt = 0;
        $arr_index = array();
        $retval .= '<ul>' . "\n";
-       foreach ($list as $head=>$pages) {
+       foreach ($list as $head=>$sub_pages) {
                if ($head === $symbol) {
                        $head = $_msg_symbol;
                } else if ($head === $other) {
@@ -563,8 +581,7 @@ function page_list($pages, $cmd = 'read', $withfilename = FALSE)
                                '"><strong>' . $head . '</strong></a>' . "\n" .
                                '  <ul>' . "\n";
                }
-               ksort($pages, SORT_STRING);
-               $retval .= join("\n", $pages);
+               $retval .= join("\n", $sub_pages);
                if ($list_index)
                        $retval .= "\n  </ul>\n </li>\n";
        }
@@ -703,9 +720,9 @@ function drop_submit($str)
 }
 
 // Generate AutoLink patterns (thx to hirofummy)
-function get_autolink_pattern(& $pages)
+function get_autolink_pattern($pages, $min_length)
 {
-       global $WikiName, $autolink, $nowikiname;
+       global $WikiName, $nowikiname;
 
        $config = new Config('AutoLink');
        $config->read();
@@ -714,11 +731,12 @@ function get_autolink_pattern(& $pages)
        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)
+                   $nowikiname : strlen($page) >= $min_length) {
                        $auto_pages[] = $page;
-
+               }
+       }
        if (empty($auto_pages)) {
                $result = $result_a = '(?!)';
        } else {
@@ -734,7 +752,7 @@ function get_autolink_pattern(& $pages)
        return array($result, $result_a, $forceignorepages);
 }
 
-function get_autolink_pattern_sub($pages, $start, $end, $pos)
+function get_autolink_pattern_sub($pages, $start, $end, $pos)
 {
        if ($end == 0) return '(?!)';
 
@@ -763,6 +781,52 @@ function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
        return $result;
 }
 
+// Get AutoAlias value
+function get_autoalias_right_link($alias_name)
+{
+       $pairs = get_autoaliases();
+       // A string: Seek the pair
+       if (isset($pairs[$alias_name])) {
+               return $pairs[$alias_name];
+       }
+       return '';
+}
+
+// Load setting pairs from AutoAliasName
+function get_autoaliases()
+{
+       global $aliaspage, $autoalias_max_words;
+       static $pairs;
+       $preg_u = get_preg_u();
+
+       if (! isset($pairs)) {
+               $pairs = array();
+               $pattern = <<<EOD
+\[\[                # open bracket
+((?:(?!\]\]).)+)>   # (1) alias name
+((?:(?!\]\]).)+)    # (2) alias link
+\]\]                # close bracket
+EOD;
+               $postdata = join('', get_source($aliaspage));
+               $matches  = array();
+               $count = 0;
+               $max   = max($autoalias_max_words, 0);
+               if (preg_match_all('/' . $pattern . '/x' . get_preg_u(), $postdata,
+                       $matches, PREG_SET_ORDER)) {
+                       foreach($matches as $key => $value) {
+                               if ($count ==  $max) break;
+                               $name = trim($value[1]);
+                               if (! isset($pairs[$name])) {
+                                       ++$count;
+                                        $pairs[$name] = trim($value[2]);
+                               }
+                               unset($matches[$key]);
+                       }
+               }
+       }
+       return $pairs;
+}
+
 /**
  * Get propery URI of this script
  *
@@ -794,11 +858,8 @@ function get_base_uri($uri_type = PKWK_URI_RELATIVE)
  */
 function get_page_uri($page, $uri_type = PKWK_URI_RELATIVE)
 {
-       global $defaultpage;
-       if ($page === $defaultpage) {
-               return get_base_uri($uri_type);
-       }
-       return get_base_uri($uri_type) . '?' . pagename_urlencode($page);
+       global $pkwk_page_uri_handler;
+       return get_base_uri($uri_type) . $pkwk_page_uri_handler->get_page_uri_fragment($page);
 }
 
 // Get absolute-URI of this script
@@ -972,9 +1033,14 @@ function guess_script_absolute_uri()
 function input_filter($param)
 {
        static $magic_quotes_gpc = NULL;
-       if ($magic_quotes_gpc === NULL)
-           $magic_quotes_gpc = get_magic_quotes_gpc();
-
+       if ($magic_quotes_gpc === NULL) {
+               if (function_exists('get_magic_quotes_gpc')) {
+                       // No 'get_magic_quotes_gpc' function in PHP8
+                       $magic_quotes_gpc = get_magic_quotes_gpc();
+               } else {
+                       $magic_quotes_gpc = 0;
+               }
+       }
        if (is_array($param)) {
                return array_map('input_filter', $param);
        } else {
@@ -1001,7 +1067,7 @@ function csv_explode($separator, $string)
 
        foreach ($matches[1] as $str) {
                $len = strlen($str);
-               if ($len > 1 && $str{0} == '"' && $str{$len - 1} == '"')
+               if ($len > 1 && $str[0] == '"' && $str[$len - 1] == '"')
                        $str = str_replace('""', '"', substr($str, 1, -1));
                $retval[] = $str;
        }
@@ -1011,7 +1077,7 @@ function csv_explode($separator, $string)
 // Implode an array with CSV data format (escape double quotes)
 function csv_implode($glue, $pieces)
 {
-       $_glue = ($glue != '') ? '\\' . $glue{0} : '';
+       $_glue = ($glue != '') ? '\\' . $glue[0] : '';
        $arr = array();
        foreach ($pieces as $str) {
                if (preg_match('/[' . '"' . "\n\r" . $_glue . ']/', $str))
@@ -1028,6 +1094,21 @@ function htmlsc($string = '', $flags = ENT_COMPAT, $charset = CONTENT_CHARSET)
 }
 
 /**
+ * Get JSON string with htmlspecialchars().
+ */
+function htmlsc_json($obj)
+{
+       // json_encode: PHP 5.2+
+       // JSON_UNESCAPED_UNICODE: PHP 5.4+
+       // JSON_UNESCAPED_SLASHES: PHP 5.4+
+       if (defined('JSON_UNESCAPED_UNICODE')) {
+               return htmlsc(json_encode($obj,
+                       JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
+       }
+       return '';
+}
+
+/**
  * Get redirect page name on Page Redirect Rules
  *
  * This function returns exactly false if it doesn't need redirection.
@@ -1077,6 +1158,48 @@ function manage_page_redirect() {
        return FALSE;
 }
 
+/**
+ * Return 'u' (PCRE_UTF8) if PHP7+ and UTF-8.
+ */
+function get_preg_u() {
+       static $utf8u; // 'u'(PCRE_UTF8) or ''
+       if (! isset($utf8u)) {
+               if (version_compare('7.0.0', PHP_VERSION, '<=')
+                       && defined('PKWK_UTF8_ENABLE')) {
+                       $utf8u = 'u';
+               } else {
+                       $utf8u = '';
+               }
+       }
+       return $utf8u;
+}
+
+class PukiWikiStandardPageURIHandler {
+       function filter_raw_query_string($query_string) {
+               return $query_string;
+       }
+
+       function get_page_uri_fragment($page) {
+               return '?' . pagename_urlencode($page);
+       }
+
+       function get_page_from_query_string($query_string) {
+               $arg = $query_string;
+               $arg = preg_replace("#^([^&]*)&.*$#", "$1", $arg);
+               if ($arg == '') {
+                       return null;
+               }
+               if (strpos($arg, '=') !== false) {
+                       // Found '/?key=value'
+                       return null;
+               }
+               $arg = urldecode($arg);
+               $arg = strip_bracket($arg);
+               $arg = input_filter($arg);
+               return $arg;
+       }
+}
+
 //// Compat ////
 
 // is_a --  Returns TRUE if the object is of this class or has this class as one of its parents