2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2003-2019 PukiWiki Development Team
6 // 2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
9 // Hyperlink-related functions
11 // To get page exists or filetimes without accessing filesystem
12 // Type: array (page => filetime)
13 $_cached_page_filetime = null;
15 // Get filetime from cache
16 function fast_get_filetime($page)
18 global $_cached_page_filetime;
19 if (is_null($_cached_page_filetime)) {
20 return get_filetime($page);
22 if (isset($_cached_page_filetime[$page])) {
23 return $_cached_page_filetime[$page];
25 return get_filetime($page);
28 // Hyperlink decoration
29 function make_link($string, $page = '')
34 if (! isset($converter)) $converter = new InlineConverter();
36 $clone = $converter->get_clone($converter);
38 return $clone->convert($string, ($page != '') ? $page : $vars['page']);
41 // Converters of inline element
44 var $converters; // as array()
49 function get_clone($obj) {
51 if (! isset($clone_exists)) {
52 if (version_compare(PHP_VERSION, '5.0.0', '<')) {
53 $clone_exists = false;
65 $converters = array();
66 foreach ($this->converters as $key=>$converter) {
67 $converters[$key] = $this->get_clone($converter);
69 $this->converters = $converters;
72 function InlineConverter($converters = NULL, $excludes = NULL)
74 $this->__construct($converters, $excludes);
76 function __construct($converters = NULL, $excludes = NULL)
78 if ($converters === NULL) {
80 'plugin', // Inline plugins
83 'url_interwiki', // URLs (interwiki definition)
84 'mailto', // mailto: URL schemes
85 'interwikiname', // InterWikiNames
86 'autolink', // AutoLinks
87 'bracketname', // BracketNames
88 'wikiname', // WikiNames
89 'autolink_a', // AutoLinks(alphabet)
93 if ($excludes !== NULL)
94 $converters = array_diff($converters, $excludes);
96 $this->converters = $patterns = array();
99 foreach ($converters as $name) {
100 $classname = 'Link_' . $name;
101 $converter = new $classname($start);
102 $pattern = $converter->get_pattern();
103 if ($pattern === FALSE) continue;
105 $patterns[] = '(' . "\n" . $pattern . "\n" . ')';
106 $this->converters[$start] = $converter;
107 $start += $converter->get_count();
110 $this->pattern = join('|', $patterns);
113 function convert($string, $page)
116 $this->result = array();
118 $string = preg_replace_callback('/' . $this->pattern . '/x',
119 array(& $this, 'replace'), $string);
121 $arr = explode("\x08", make_line_rules(htmlsc($string)));
123 while (! empty($arr)) {
124 $retval .= array_shift($arr) . array_shift($this->result);
129 function replace($arr)
131 $obj = $this->get_converter($arr);
133 $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
134 $obj->toString() : make_line_rules(htmlsc($arr[0]));
136 return "\x08"; // Add a mark into latest processed part
139 function get_objects($string, $page)
141 $matches = $arr = array();
142 preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
143 foreach ($matches as $match) {
144 $obj = $this->get_converter($match);
145 if ($obj->set($match, $page) !== FALSE) {
146 $arr[] = $this->get_clone($obj);
147 if ($obj->body != '')
148 $arr = array_merge($arr, $this->get_objects($obj->body, $page));
154 function & get_converter(& $arr)
156 foreach (array_keys($this->converters) as $start) {
157 if ($arr[$start] == $arr[0])
158 return $this->converters[$start];
164 // Base class of inline elements
167 var $start; // Origin number of parentheses (0 origin)
168 var $text; // Matched string
176 function Link($start)
178 $this->__construct($start);
180 function __construct($start)
182 $this->start = $start;
185 // Return a regex pattern to match
186 function get_pattern() {}
188 // Return number of parentheses (except (?:...) )
189 function get_count() {}
191 // Set pattern that matches
192 function set($arr, $page) {}
194 function toString() {}
196 // Private: Get needed parts from a matched array()
197 function splice($arr)
199 $count = $this->get_count() + 1;
200 $arr = array_pad(array_splice($arr, $this->start, $count), $count, '');
201 $this->text = $arr[0];
205 // Set basic parameters
206 function setParam($page, $name, $body, $type = '', $alias = '')
208 static $converter = NULL;
214 if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
215 is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
216 $alias = '<img src="' . htmlsc($alias) . '" alt="' . $name . '" />';
217 } else if ($alias != '') {
218 if ($converter === NULL)
219 $converter = new InlineConverter(array('plugin'));
221 $alias = make_line_rules($converter->convert($alias, $page));
223 // BugTrack/669: A hack removing anchor tags added by AutoLink
224 $alias = preg_replace('#</?a[^>]*>#i', '', $alias);
226 $this->alias = $alias;
233 class Link_plugin extends Link
238 function Link_plugin($start)
240 $this->__construct($start);
242 function __construct($start)
244 parent::__construct($start);
247 function get_pattern()
249 $this->pattern = <<<EOD
252 (\w+) # (2) plugin name
255 ((?:(?!\)[;{]).)*) # (3) parameter
264 ((?:(?R)|(?!};).)*) # (4) body
276 function set($arr, $page)
278 list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
280 // Re-get true plugin name and patameters (for PHP 4.1.2)
282 if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
283 && $matches[1] != $this->plain)
284 list(, $this->plain, $name, $this->param) = $matches;
286 return parent::setParam($page, $name, $body, 'plugin');
291 $body = ($this->body == '') ? '' : make_link($this->body);
294 // Try to call the plugin
295 if (exist_plugin_inline($this->name))
296 $str = do_plugin_inline($this->name, $this->param, $body);
298 if ($str !== FALSE) {
299 return $str; // Succeed
301 // No such plugin, or Failed
302 $body = (($body == '') ? '' : '{' . $body . '}') . ';';
303 return make_line_rules(htmlsc('&' . $this->plain) . $body);
309 class Link_note extends Link
311 function Link_note($start)
313 $this->__construct($start);
315 function __construct($start)
317 parent::__construct($start);
320 function get_pattern()
324 ((?>(?=\(\()(?R)|(?!\)\)).)*) # (1) note body
334 function set($arr, $page)
336 global $foot_explain, $vars;
339 list(, $body) = $this->splice($arr);
341 if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
344 $script = get_page_uri($page);
347 $note = make_link($body);
350 $foot_explain[$id] = '<a id="notefoot_' . $id . '" href="' .
351 $script . '#notetext_' . $id . '" class="note_super">*' .
352 $id . '</a>' . "\n" .
353 '<span class="small">' . $note . '</span><br />';
355 // A hyperlink, content-body to footnote
356 if (! is_numeric(PKWK_FOOTNOTE_TITLE_MAX) || PKWK_FOOTNOTE_TITLE_MAX <= 0) {
359 $title = strip_tags($note);
360 $count = mb_strlen($title, SOURCE_ENCODING);
361 $title = mb_substr($title, 0, PKWK_FOOTNOTE_TITLE_MAX, SOURCE_ENCODING);
362 $abbr = (PKWK_FOOTNOTE_TITLE_MAX < $count) ? '...' : '';
363 $title = ' title="' . $title . $abbr . '"';
365 $name = '<a id="notetext_' . $id . '" href="' . $script .
366 '#notefoot_' . $id . '" class="note_super"' . $title .
369 return parent::setParam($page, $name, $body);
379 class Link_url extends Link
381 function Link_url($start)
383 $this->__construct($start);
385 function __construct($start)
387 parent::__construct($start);
390 function get_pattern()
392 $s1 = $this->start + 1;
394 ((?:\[\[))? # (1) open bracket
396 ((?:(?!\]\]).)+) # (3) alias name
400 (?:(?:https?|ftp|news):\/\/|mailto:)[\w\/\@\$()!?&%#:;.,~'=*+-]+
402 (?($s1)\]\]) # close bracket
411 function set($arr, $page)
413 list(, , , $alias, $name) = $this->splice($arr);
414 return parent::setParam($page, htmlsc($name),
415 '', 'url', $alias == '' ? $name : $alias);
423 $rel = ' rel="nofollow"';
425 return '<a href="' . $this->name . '"' . $rel . '>' . $this->alias . '</a>';
429 // URLs (InterWiki definition on "InterWikiName")
430 class Link_url_interwiki extends Link
432 function Link_url_interwiki($start)
434 $this->__construct($start);
436 function __construct($start)
438 parent::__construct($start);
441 function get_pattern()
446 (?:(?:https?|ftp|news):\/\/|\.\.?\/)[!~*'();\/?:\@&=+\$,%#\w.-]*
459 function set($arr, $page)
461 list(, $name, $alias) = $this->splice($arr);
462 return parent::setParam($page, htmlsc($name), '', 'url', $alias);
467 return '<a href="' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
471 // mailto: URL schemes
472 class Link_mailto extends Link
474 var $is_image, $image;
476 function Link_mailto($start)
478 $this->__construct($start);
480 function __construct($start)
482 parent::__construct($start);
485 function get_pattern()
487 $s1 = $this->start + 1;
491 ((?:(?!\]\]).)+)(?:>|:) # (1) alias
493 ([\w.-]+@[\w-]+\.[\w.-]+) # (2) mailto
494 (?($s1)\]\]) # close bracket if (1)
503 function set($arr, $page)
505 list(, $alias, $name) = $this->splice($arr);
506 return parent::setParam($page, $name, '', 'mailto', $alias == '' ? $name : $alias);
511 return '<a href="mailto:' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
515 // InterWikiName-rendered URLs
516 class Link_interwikiname extends Link
522 function Link_interwikiname($start)
524 $this->__construct($start);
526 function __construct($start)
528 parent::__construct($start);
531 function get_pattern()
533 $s2 = $this->start + 2;
534 $s5 = $this->start + 5;
538 ((?:(?!\]\]).)+)> # (1) alias
540 (\[\[)? # (2) open bracket
541 ((?:(?!\s|:|\]\]).)+) # (3) InterWiki
542 (?<! > | >\[\[ ) # not '>' or '>[['
545 (\[\[)? # (5) open bracket
547 (?($s5)\]\]) # close bracket if (5)
549 (?($s2)\]\]) # close bracket if (2)
559 function set($arr, $page)
561 list(, $alias, , $name, $this->param) = $this->splice($arr);
564 if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $this->param, $matches))
565 list(, $this->param, $this->anchor) = $matches;
567 $url = get_interwiki_url($name, $this->param);
568 $this->url = ($url === FALSE) ?
569 get_base_uri() . '?' . pagename_urlencode('[[' . $name . ':' . $this->param . ']]') :
572 return parent::setParam(
574 htmlsc($name . ':' . $this->param),
577 $alias == '' ? $name . ':' . $this->param : $alias
583 return '<a href="' . $this->url . $this->anchor . '" title="' .
584 $this->name . '" rel="nofollow">' . $this->alias . '</a>';
589 class Link_bracketname extends Link
593 function Link_bracketname($start)
595 $this->__construct($start);
597 function __construct($start)
599 parent::__construct($start);
602 function get_pattern()
604 global $WikiName, $BracketName;
606 $s2 = $this->start + 2;
609 (?:((?:(?!\]\]).)+)>)? # (1) Alias
610 (\[\[)? # (2) Open bracket
616 (\#(?:[a-zA-Z][\w-]*)?)? # (4) Anchor
617 (?($s2)\]\]) # Close bracket if (2)
627 function set($arr, $page)
631 list(, $alias, , $name, $this->anchor) = $this->splice($arr);
632 if ($name == '' && $this->anchor == '') return FALSE;
634 if ($name == '' || ! preg_match('/^' . $WikiName . '$/', $name)) {
635 if ($alias == '') $alias = $name . $this->anchor;
637 $name = get_fullname($name, $page);
638 if (! is_pagename($name)) return FALSE;
642 return parent::setParam($page, $name, '', 'pagename', $alias);
647 return make_pagelink(
657 class Link_wikiname extends Link
659 function Link_wikiname($start)
661 $this->__construct($start);
663 function __construct($start)
665 parent::__construct($start);
668 function get_pattern()
670 global $WikiName, $nowikiname;
672 return $nowikiname ? FALSE : '(' . $WikiName . ')';
680 function set($arr, $page)
682 list($name) = $this->splice($arr);
683 return parent::setParam($page, $name, '', 'pagename', $name);
688 return make_pagelink(
698 class Link_autolink extends Link
700 var $forceignorepages = array();
702 var $auto_a; // alphabet only
704 function Link_autolink($start)
706 $this->__construct($start);
708 function __construct($start)
712 parent::__construct($start);
714 if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
717 @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . 'autolink.dat');
719 $this->auto_a = $auto_a;
720 $this->forceignorepages = explode("\t", trim($forceignorepages));
723 function get_pattern()
725 return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
733 function set($arr, $page)
737 list($name) = $this->splice($arr);
739 // Ignore pages listed, or Expire ones not found
740 if (in_array($name, $this->forceignorepages) || ! is_page($name))
743 return parent::setParam($page, $name, '', 'pagename', $name);
748 return make_pagelink($this->name, $this->alias, '', $this->page, TRUE);
752 class Link_autolink_a extends Link_autolink
754 function Link_autolink_a($start)
756 $this->__construct($start);
758 function __construct($start)
760 parent::__construct($start);
763 function get_pattern()
765 return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
769 // Make hyperlink for the page
770 function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
772 global $vars, $link_compact, $related, $_symbol_noexists;
774 $script = get_base_uri();
775 $s_page = htmlsc(strip_bracket($page));
776 $s_alias = ($alias == '') ? $s_page : $alias;
778 if ($page == '') return '<a href="' . $anchor . '">' . $s_alias . '</a>';
780 $r_page = pagename_urlencode($page);
781 $r_refer = ($refer == '') ? '' : '&refer=' . rawurlencode($refer);
783 $page_filetime = fast_get_filetime($page);
784 $is_page = $page_filetime !== 0;
785 if (! isset($related[$page]) && $page !== $vars['page'] && is_page)
786 $related[$page] = $page_filetime;
788 if ($isautolink || $is_page) {
789 // Hyperlink to the page
790 $attrs = get_filetime_a_attrs($page_filetime);
793 $al_left = '<!--autolink-->';
794 $al_right = '<!--/autolink-->';
796 $al_left = $al_right = '';
798 $title_attr_html = '';
799 if ($s_page !== $s_alias) {
800 $title_attr_html = ' title="' . $s_page . '"';
802 return $al_left . '<a ' . 'href="' . $script . '?' . $r_page . $anchor .
803 '"' . $title_attr_html . ' class="' .
804 $attrs['class'] . '" data-mtime="' . $attrs['data_mtime'] .
805 '">' . $s_alias . '</a>' . $al_right;
807 // Support Page redirection
808 $redirect_page = get_pagename_on_redirect($page);
809 if ($redirect_page !== false) {
810 return make_pagelink($redirect_page, $s_alias);
813 if (PKWK_READONLY) return $s_alias; // No dacorations
815 $retval = $s_alias . '<a href="' .
816 $script . '?cmd=edit&page=' . $r_page . $r_refer . '">' .
817 $_symbol_noexists . '</a>';
822 return '<span class="noexists">' . $retval . '</span>';
827 // Resolve relative / (Unix-like)absolute path of the page
828 function get_fullname($name, $refer)
833 if ($name == '' || $name == './') return $refer;
836 if ($name{0} == '/') {
837 $name = substr($name, 1);
838 return ($name == '') ? $defaultpage : $name;
841 // Relative path from 'Here'
842 if (substr($name, 0, 2) == './') {
843 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
845 return join('/', $arrn);
848 // Relative path from dirname()
849 if (substr($name, 0, 3) == '../') {
850 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
851 $arrp = preg_split('#/#', $refer, -1, PREG_SPLIT_NO_EMPTY);
853 while (! empty($arrn) && $arrn[0] == '..') {
857 $name = ! empty($arrp) ? join('/', array_merge($arrp, $arrn)) :
858 (! empty($arrn) ? $defaultpage . '/' . join('/', $arrn) : $defaultpage);
864 // Render an InterWiki into a URL
865 function get_interwiki_url($name, $param)
867 global $WikiName, $interwiki;
868 static $interwikinames;
869 static $encode_aliases = array('sjis'=>'SJIS', 'euc'=>'EUC-JP', 'utf8'=>'UTF-8');
871 if (! isset($interwikinames)) {
872 $interwikinames = $matches = array();
873 foreach (get_source($interwiki) as $line)
874 if (preg_match('/\[(' . '(?:(?:https?|ftp|news):\/\/|\.\.?\/)' .
875 '[!~*\'();\/?:\@&=+\$,%#\w.-]*)\s([^\]]+)\]\s?([^\s]*)/',
877 $interwikinames[$matches[2]] = array($matches[1], $matches[3]);
880 if (! isset($interwikinames[$name])) return FALSE;
882 list($url, $opt) = $interwikinames[$name];
887 case '': /* FALLTHROUGH */
888 case 'std': // Simply URL-encode the string, whose base encoding is the internal-encoding
889 $param = rawurlencode($param);
892 case 'asis': /* FALLTHROUGH */
893 case 'raw' : // Truly as-is
896 case 'yw': // YukiWiki
897 if (! preg_match('/' . $WikiName . '/', $param))
898 $param = '[[' . mb_convert_encoding($param, 'SJIS', SOURCE_ENCODING) . ']]';
901 case 'moin': // MoinMoin
902 $param = str_replace('%', '_', rawurlencode($param));
906 // Alias conversion of $opt
907 if (isset($encode_aliases[$opt])) $opt = & $encode_aliases[$opt];
909 // Encoding conversion into specified encode, and URLencode
910 if (strpos($url, '$1') === FALSE && substr($url, -1) === '?') {
912 $param = pagename_urlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
914 $param = rawurlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
918 // Replace or Add the parameter
919 if (strpos($url, '$1') !== FALSE) {
920 $url = str_replace('$1', $param, $url);
926 if ($len > 512) die_message('InterWiki URL too long: ' . $len . ' characters');
931 function get_autoticketlink_def_page()
933 return 'AutoTicketLinkName';
937 * Get AutoTicketLink - JIRA projects from AutoTiketLinkName page
939 function get_ticketlink_jira_projects()
941 $autoticketlink_def_page = get_autoticketlink_def_page();
942 $active_jira_base_url = null;
943 $jira_projects = array();
944 foreach (get_source($autoticketlink_def_page) as $line) {
945 if (substr($line, 0, 1) !== '-') {
946 $active_jira_base_url = null;
950 if (preg_match('/^-\s*(jira)\s+(https?:\/\/[!~*\'();\/?:\@&=+\$,%#\w.-]+)\s*$/', $line, $m)) {
951 $active_jira_base_url = $m[2];
952 } else if (preg_match('/^--\s*([A-Z][A-Z0-9]{1,10}(?:_[A-Z0-9]{1,10}){0,2})(\s+(.+?))?\s*$/', $line, $m)) {
953 if ($active_jira_base_url) {
954 $project_key = $m[1];
956 array_push($jira_projects, array(
959 'base_url' => $active_jira_base_url,
963 $active_jira_base_url = null;
966 return $jira_projects;
969 function init_autoticketlink_def_page()
971 $autoticketlink_def_page = get_autoticketlink_def_page();
972 if (is_page($autoticketlink_def_page)) {
977 * AutoTicketLink definition [#def]
979 Reference: https://pukiwiki.osdn.jp/?AutoTicketLink
981 - jira https://site1.example.com/jira/browse/
982 -- AAA Project title \$1
983 -- BBB Project title \$1
984 - jira https://site2.example.com/jira/browse/
985 -- PROJECTA Site2 \$1
987 (Default definition) pukiwiki.ini.php
988 $ticket_jira_default_site = array(
989 'title' => 'My JIRA - \$1',
990 'base_url' => 'https://issues.example.com/jira/browse/',
993 page_write($autoticketlink_def_page, $body);