OSDN Git Service

BugTrack/2387 Improve tracker form table border
[pukiwiki/pukiwiki.git] / lib / make_link.php
index 561cfbc..0773ff6 100644 (file)
@@ -1,9 +1,30 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: make_link.php,v 1.14 2005/01/23 09:50:49 henoheno Exp $
+// make_link.php
+// Copyright
+//   2003-2019 PukiWiki Development Team
+//   2001-2002 Originally written by yu-ji
+// License: GPL v2 or (at your option) any later version
 //
 // Hyperlink-related functions
 
+// To get page exists or filetimes without accessing filesystem
+// Type: array (page => filetime)
+$_cached_page_filetime = null;
+
+// Get filetime from cache
+function fast_get_filetime($page)
+{
+       global $_cached_page_filetime;
+       if (is_null($_cached_page_filetime)) {
+               return get_filetime($page);
+       }
+       if (isset($_cached_page_filetime[$page])) {
+               return $_cached_page_filetime[$page];
+       }
+       return get_filetime($page);
+}
+
 // Hyperlink decoration
 function make_link($string, $page = '')
 {
@@ -26,16 +47,18 @@ class InlineConverter
        var $result;
 
        function get_clone($obj) {
-               static $clone_func;
-
-               if (! isset($clone_func)) {
+               static $clone_exists;
+               if (! isset($clone_exists)) {
                        if (version_compare(PHP_VERSION, '5.0.0', '<')) {
-                               $clone_func = create_function('$a', 'return $a;');
+                               $clone_exists = false;
                        } else {
-                               $clone_func = create_function('$a', 'return clone $a;');
+                               $clone_exists = true;
                        }
                }
-               return $clone_func($obj);
+               if ($clone_exists) {
+                       return clone ($obj);
+               }
+               return $obj;
        }
 
        function __clone() {
@@ -48,6 +71,10 @@ class InlineConverter
 
        function InlineConverter($converters = NULL, $excludes = NULL)
        {
+               $this->__construct($converters, $excludes);
+       }
+       function __construct($converters = NULL, $excludes = NULL)
+       {
                if ($converters === NULL) {
                        $converters = array(
                                'plugin',        // Inline plugins
@@ -91,7 +118,7 @@ class InlineConverter
                $string = preg_replace_callback('/' . $this->pattern . '/x',
                        array(& $this, 'replace'), $string);
 
-               $arr = explode("\x08", make_line_rules(htmlspecialchars($string)));
+               $arr = explode("\x08", make_line_rules(htmlsc($string)));
                $retval = '';
                while (! empty($arr)) {
                        $retval .= array_shift($arr) . array_shift($this->result);
@@ -104,7 +131,7 @@ class InlineConverter
                $obj = $this->get_converter($arr);
 
                $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
-                       $obj->toString() : make_line_rules(htmlspecialchars($arr[0]));
+                       $obj->toString() : make_line_rules(htmlsc($arr[0]));
 
                return "\x08"; // Add a mark into latest processed part
        }
@@ -146,9 +173,12 @@ class Link
        var $body;
        var $alias;
 
-       // Constructor
        function Link($start)
        {
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
                $this->start = $start;
        }
 
@@ -164,7 +194,8 @@ class Link
        function toString() {}
 
        // Private: Get needed parts from a matched array()
-       function splice($arr) {
+       function splice($arr)
+       {
                $count = $this->get_count() + 1;
                $arr   = array_pad(array_splice($arr, $this->start, $count), $count, '');
                $this->text = $arr[0];
@@ -180,9 +211,9 @@ class Link
                $this->name = $name;
                $this->body = $body;
                $this->type = $type;
-               if (is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
-                       $alias = htmlspecialchars($alias);
-                       $alias = '<img src="' . $alias . '" alt="' . $name . '" />';
+               if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
+                       is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
+                       $alias = '<img src="' . htmlsc($alias) . '" alt="' . $name . '" />';
                } else if ($alias != '') {
                        if ($converter === NULL)
                                $converter = new InlineConverter(array('plugin'));
@@ -206,7 +237,11 @@ class Link_plugin extends Link
 
        function Link_plugin($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
@@ -265,7 +300,7 @@ EOD;
                } else {
                        // No such plugin, or Failed
                        $body = (($body == '') ? '' : '{' . $body . '}') . ';';
-                       return make_line_rules(htmlspecialchars('&' . $this->plain) . $body);
+                       return make_line_rules(htmlsc('&' . $this->plain) . $body);
                }
        }
 }
@@ -275,14 +310,18 @@ class Link_note extends Link
 {
        function Link_note($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
        {
                return <<<EOD
 \(\(
- ((?:(?R)|(?!\)\)).)*) # (1) note body
+ ((?>(?=\(\()(?R)|(?!\)\)).)*) # (1) note body
 \)\)
 EOD;
        }
@@ -294,25 +333,38 @@ EOD;
 
        function set($arr, $page)
        {
-               global $foot_explain, $script, $vars;
+               global $foot_explain, $vars;
                static $note_id = 0;
 
                list(, $body) = $this->splice($arr);
 
+               if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
+                       $script = '';
+               } else {
+                       $script = get_page_uri($page);
+               }
                $id   = ++$note_id;
                $note = make_link($body);
-               $page = isset($vars['page']) ? htmlspecialchars($vars['page']) : '';
 
                // Footnote
-               $foot_explain[$id] = <<<EOD
-<a id="notefoot_$id" href="$script?$page#notetext_$id" class="note_super">*$id</a>
-<span class="small">$note</span>
-<br />
-EOD;
+               $foot_explain[$id] = '<a id="notefoot_' . $id . '" href="' .
+                       $script . '#notetext_' . $id . '" class="note_super">*' .
+                       $id . '</a>' . "\n" .
+                       '<span class="small">' . $note . '</span><br />';
+
                // A hyperlink, content-body to footnote
-               $name = '<a id="notetext_' . $id . '" href="' . $script . '?' . $page .
-                       '#notefoot_' . $id . '" class="note_super" title="' .
-                       htmlspecialchars(strip_tags($note)) . '">*' . $id . '</a>';
+               if (! is_numeric(PKWK_FOOTNOTE_TITLE_MAX) || PKWK_FOOTNOTE_TITLE_MAX <= 0) {
+                       $title = '';
+               } else {
+                       $title = strip_tags($note);
+                       $count = mb_strlen($title, SOURCE_ENCODING);
+                       $title = mb_substr($title, 0, PKWK_FOOTNOTE_TITLE_MAX, SOURCE_ENCODING);
+                       $abbr  = (PKWK_FOOTNOTE_TITLE_MAX < $count) ? '...' : '';
+                       $title = ' title="' . $title . $abbr . '"';
+               }
+               $name = '<a id="notetext_' . $id . '" href="' . $script .
+                       '#notefoot_' . $id . '" class="note_super"' . $title .
+                       '>*' . $id . '</a>';
 
                return parent::setParam($page, $name, $body);
        }
@@ -328,18 +380,23 @@ class Link_url extends Link
 {
        function Link_url($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
        {
                $s1 = $this->start + 1;
                return <<<EOD
-(\[\[             # (1) open bracket
- ((?:(?!\]\]).)+) # (2) alias
+((?:\[\[))?       # (1) open bracket
+((?($s1)          # (2) alias
+((?:(?!\]\]).)+)  # (3) alias name
  (?:>|:)
-)?
-(                 # (3) url
+))?
+(                 # (4) url
  (?:(?:https?|ftp|news):\/\/|mailto:)[\w\/\@\$()!?&%#:;.,~'=*+-]+
 )
 (?($s1)\]\])      # close bracket
@@ -348,19 +405,19 @@ EOD;
 
        function get_count()
        {
-               return 3;
+               return 4;
        }
 
        function set($arr, $page)
        {
-               list(, , $alias, $name) = $this->splice($arr);
-               return parent::setParam($page, htmlspecialchars($name),
+               list(, , $alias, $name) = $this->splice($arr);
+               return parent::setParam($page, htmlsc($name),
                        '', 'url', $alias == '' ? $name : $alias);
        }
 
        function toString()
        {
-               if (PKWK_READONLY) {
+               if (FALSE) {
                        $rel = '';
                } else {
                        $rel = ' rel="nofollow"';
@@ -374,7 +431,11 @@ class Link_url_interwiki extends Link
 {
        function Link_url_interwiki($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
@@ -398,7 +459,7 @@ EOD;
        function set($arr, $page)
        {
                list(, $name, $alias) = $this->splice($arr);
-               return parent::setParam($page, htmlspecialchars($name), '', 'url', $alias);
+               return parent::setParam($page, htmlsc($name), '', 'url', $alias);
        }
 
        function toString()
@@ -414,7 +475,11 @@ class Link_mailto extends Link
 
        function Link_mailto($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
@@ -456,7 +521,11 @@ class Link_interwikiname extends Link
 
        function Link_interwikiname($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
@@ -489,8 +558,6 @@ EOD;
 
        function set($arr, $page)
        {
-               global $script;
-
                list(, $alias, , $name, $this->param) = $this->splice($arr);
 
                $matches = array();
@@ -499,12 +566,12 @@ EOD;
 
                $url = get_interwiki_url($name, $this->param);
                $this->url = ($url === FALSE) ?
-                       $script . '?' . rawurlencode('[[' . $name . ':' . $this->param . ']]') :
-                       htmlspecialchars($url);
+                       get_base_uri() . '?' . pagename_urlencode('[[' . $name . ':' . $this->param . ']]') :
+                       htmlsc($url);
 
                return parent::setParam(
                        $page,
-                       htmlspecialchars($name . ':' . $this->param),
+                       htmlsc($name . ':' . $this->param),
                        '',
                        'InterWikiName',
                        $alias == '' ? $name . ':' . $this->param : $alias
@@ -525,7 +592,11 @@ class Link_bracketname extends Link
 
        function Link_bracketname($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
@@ -587,7 +658,11 @@ class Link_wikiname extends Link
 {
        function Link_wikiname($start)
        {
-               parent::Link($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
@@ -628,9 +703,13 @@ class Link_autolink extends Link
 
        function Link_autolink($start)
        {
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
                global $autolink;
 
-               parent::Link($start);
+               parent::__construct($start);
 
                if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
                        return;
@@ -666,7 +745,7 @@ class Link_autolink extends Link
 
        function toString()
        {
-               return make_pagelink($this->name, $this->alias, '', $this->page);
+               return make_pagelink($this->name, $this->alias, '', $this->page, TRUE);
        }
 }
 
@@ -674,7 +753,11 @@ class Link_autolink_a extends Link_autolink
 {
        function Link_autolink_a($start)
        {
-               parent::Link_autolink($start);
+               $this->__construct($start);
+       }
+       function __construct($start)
+       {
+               parent::__construct($start);
        }
 
        function get_pattern()
@@ -684,38 +767,60 @@ class Link_autolink_a extends Link_autolink
 }
 
 // Make hyperlink for the page
-function make_pagelink($page, $alias = '', $anchor = '', $refer = '')
+function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
 {
-       global $script, $vars, $link_compact, $related, $_symbol_noexists;
+       global $vars, $link_compact, $related, $_symbol_noexists;
 
-       $s_page = htmlspecialchars(strip_bracket($page));
+       $script = get_base_uri();
+       $s_page = htmlsc(strip_bracket($page));
        $s_alias = ($alias == '') ? $s_page : $alias;
 
        if ($page == '') return '<a href="' . $anchor . '">' . $s_alias . '</a>';
 
-       $r_page  = rawurlencode($page);
+       $r_page  = pagename_urlencode($page);
        $r_refer = ($refer == '') ? '' : '&amp;refer=' . rawurlencode($refer);
 
-       if (! isset($related[$page]) && $page != $vars['page'] && is_page($page))
-               $related[$page] = get_filetime($page);
-
-       if (is_page($page)) {
-               // Hyperlinks
-               $passage = get_pg_passage($page, FALSE);
-               $title   = $link_compact ? '' : ' title="' . $s_page . $passage . '"';
-               return '<a href="' . $script . '?' . $r_page . $anchor . '"' . $title . '>' .
-                       $s_alias . '</a>';
-       } else if (PKWK_READONLY) {
-               // Without hyperlink (= Suppress dangling link)
-               return $s_alias;
+       $page_filetime = fast_get_filetime($page);
+       $is_page = $page_filetime !== 0;
+       if (! isset($related[$page]) && $page !== $vars['page'] && is_page)
+               $related[$page] = $page_filetime;
+
+       if ($isautolink || $is_page) {
+               // Hyperlink to the page
+               $attrs = get_filetime_a_attrs($page_filetime);
+               // AutoLink marker
+               if ($isautolink) {
+                       $al_left  = '<!--autolink-->';
+                       $al_right = '<!--/autolink-->';
+               } else {
+                       $al_left = $al_right = '';
+               }
+               $title_attr_html = '';
+               if ($s_page !== $s_alias) {
+                       $title_attr_html = ' title="' . $s_page . '"';
+               }
+               return $al_left . '<a ' . 'href="' . $script . '?' . $r_page . $anchor .
+                       '"' . $title_attr_html . ' class="' .
+                       $attrs['class'] . '" data-mtime="' . $attrs['data_mtime'] .
+                       '">' . $s_alias . '</a>' . $al_right;
        } else {
-               // Dangling links
+               // Support Page redirection
+               $redirect_page = get_pagename_on_redirect($page);
+               if ($redirect_page !== false) {
+                       return make_pagelink($redirect_page, $s_alias);
+               }
+               // Dangling link
+               if (PKWK_READONLY) return $s_alias; // No dacorations
+
                $retval = $s_alias . '<a href="' .
                        $script . '?cmd=edit&amp;page=' . $r_page . $r_refer . '">' .
                        $_symbol_noexists . '</a>';
-               if (! $link_compact)
-                       $retval = '<span class="noexists">' . $retval . '</span>';
-               return $retval;
+
+               if ($link_compact) {
+                       return $retval;
+               } else {
+                       return '<span class="noexists">' . $retval . '</span>';
+               }
        }
 }
 
@@ -779,20 +884,18 @@ function get_interwiki_url($name, $param)
        // Encoding
        switch ($opt) {
 
-       case '':
-       case 'std': // As-Is (Internal encoding of this PukiWiki will be used)
+       case '':    /* FALLTHROUGH */
+       case 'std': // Simply URL-encode the string, whose base encoding is the internal-encoding
                $param = rawurlencode($param);
                break;
 
-       case 'asis': // As-Is
-       case 'raw':
-               // $param = htmlspecialchars($param);
+       case 'asis': /* FALLTHROUGH */
+       case 'raw' : // Truly as-is
                break;
 
        case 'yw': // YukiWiki
                if (! preg_match('/' . $WikiName . '/', $param))
                        $param = '[[' . mb_convert_encoding($param, 'SJIS', SOURCE_ENCODING) . ']]';
-               // $param = htmlspecialchars($param);
                break;
 
        case 'moin': // MoinMoin
@@ -800,13 +903,19 @@ function get_interwiki_url($name, $param)
                break;
 
        default:
-               // Alias conversion
-               if (isset($encode_aliases[$opt])) $opt = $encode_aliases[$opt];
+               // Alias conversion of $opt
+               if (isset($encode_aliases[$opt])) $opt = & $encode_aliases[$opt];
+
                // Encoding conversion into specified encode, and URLencode
-               $param = rawurlencode(mb_convert_encoding($param, $opt, 'auto'));
+               if (strpos($url, '$1') === FALSE && substr($url, -1) === '?') {
+                       // PukiWiki site
+                       $param = pagename_urlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
+               } else {
+                       $param = rawurlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
+               }
        }
 
-       // Replace parameters
+       // Replace or Add the parameter
        if (strpos($url, '$1') !== FALSE) {
                $url = str_replace('$1', $param, $url);
        } else {
@@ -818,4 +927,68 @@ function get_interwiki_url($name, $param)
 
        return $url;
 }
-?>
+
+function get_autoticketlink_def_page()
+{
+       return 'AutoTicketLinkName';
+}
+
+/**
+ * Get AutoTicketLink - JIRA projects from AutoTiketLinkName page
+ */
+function get_ticketlink_jira_projects()
+{
+       $autoticketlink_def_page = get_autoticketlink_def_page();
+       $active_jira_base_url = null;
+       $jira_projects = array();
+       foreach (get_source($autoticketlink_def_page) as $line) {
+               if (substr($line, 0, 1) !== '-') {
+                       $active_jira_base_url = null;
+                       continue;
+               }
+               $m = null;
+               if (preg_match('/^-\s*(jira)\s+(https?:\/\/[!~*\'();\/?:\@&=+\$,%#\w.-]+)\s*$/', $line, $m)) {
+                       $active_jira_base_url = $m[2];
+               } else if (preg_match('/^--\s*([A-Z][A-Z0-9]{1,10}(?:_[A-Z0-9]{1,10}){0,2})(\s+(.+?))?\s*$/', $line, $m)) {
+                       if ($active_jira_base_url) {
+                               $project_key = $m[1];
+                               $title = $m[2];
+                               array_push($jira_projects, array(
+                                       'key' => $m[1],
+                                       'title' => $title,
+                                       'base_url' => $active_jira_base_url,
+                               ));
+                       }
+               } else {
+                       $active_jira_base_url = null;
+               }
+       }
+       return $jira_projects;
+}
+
+function init_autoticketlink_def_page()
+{
+       $autoticketlink_def_page = get_autoticketlink_def_page();
+       if (is_page($autoticketlink_def_page)) {
+               return;
+       }
+       $body = <<<EOS
+#freeze
+* AutoTicketLink definition [#def]
+
+Reference: https://pukiwiki.osdn.jp/?AutoTicketLink
+
+ - jira https://site1.example.com/jira/browse/
+ -- AAA Project title \$1
+ -- BBB Project title \$1
+ - jira https://site2.example.com/jira/browse/
+ -- PROJECTA Site2 \$1
+
+ (Default definition) pukiwiki.ini.php
+ $ticket_jira_default_site = array(
+   'title' => 'My JIRA - \$1',
+   'base_url' => 'https://issues.example.com/jira/browse/',
+ );
+EOS;
+       page_write($autoticketlink_def_page, $body);
+}