2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2003-2020 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 'autoalias', // AutoAlias
87 'autolink', // AutoLinks
88 'bracketname', // BracketNames
89 'wikiname', // WikiNames
90 'autoalias_a', // AutoAlias(alphabet)
91 'autolink_a', // AutoLinks(alphabet)
95 if ($excludes !== NULL)
96 $converters = array_diff($converters, $excludes);
98 $this->converters = $patterns = array();
101 foreach ($converters as $name) {
102 $classname = 'Link_' . $name;
103 $converter = new $classname($start);
104 $pattern = $converter->get_pattern();
105 if ($pattern === FALSE) continue;
107 $patterns[] = '(' . "\n" . $pattern . "\n" . ')';
108 $this->converters[$start] = $converter;
109 $start += $converter->get_count();
112 $this->pattern = join('|', $patterns);
115 function convert($string, $page)
118 $this->result = array();
120 $string = preg_replace_callback('/' . $this->pattern . '/x',
121 array(& $this, 'replace'), $string);
123 $arr = explode("\x08", make_line_rules(htmlsc($string)));
125 while (! empty($arr)) {
126 $retval .= array_shift($arr) . array_shift($this->result);
131 function replace($arr)
133 $obj = $this->get_converter($arr);
135 $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
136 $obj->toString() : make_line_rules(htmlsc($arr[0]));
138 return "\x08"; // Add a mark into latest processed part
141 function get_objects($string, $page)
143 $matches = $arr = array();
144 preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
145 foreach ($matches as $match) {
146 $obj = $this->get_converter($match);
147 if ($obj->set($match, $page) !== FALSE) {
148 $arr[] = $this->get_clone($obj);
149 if ($obj->body != '')
150 $arr = array_merge($arr, $this->get_objects($obj->body, $page));
156 function & get_converter(& $arr)
158 foreach (array_keys($this->converters) as $start) {
159 if ($arr[$start] == $arr[0])
160 return $this->converters[$start];
166 // Base class of inline elements
169 var $start; // Origin number of parentheses (0 origin)
170 var $text; // Matched string
178 function Link($start)
180 $this->__construct($start);
182 function __construct($start)
184 $this->start = $start;
187 // Return a regex pattern to match
188 function get_pattern() {}
190 // Return number of parentheses (except (?:...) )
191 function get_count() {}
193 // Set pattern that matches
194 function set($arr, $page) {}
196 function toString() {}
198 // Private: Get needed parts from a matched array()
199 function splice($arr)
201 $count = $this->get_count() + 1;
202 $arr = array_pad(array_splice($arr, $this->start, $count), $count, '');
203 $this->text = $arr[0];
207 // Set basic parameters
208 function setParam($page, $name, $body, $type = '', $alias = '')
210 static $converter = NULL;
216 if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
217 is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
218 $alias = '<img src="' . htmlsc($alias) . '" alt="' . $name . '" />';
219 } else if ($alias != '') {
220 if ($converter === NULL)
221 $converter = new InlineConverter(array('plugin'));
223 $alias = make_line_rules($converter->convert($alias, $page));
225 // BugTrack/669: A hack removing anchor tags added by AutoLink
226 $alias = preg_replace('#</?a[^>]*>#i', '', $alias);
228 $this->alias = $alias;
235 class Link_plugin extends Link
240 function Link_plugin($start)
242 $this->__construct($start);
244 function __construct($start)
246 parent::__construct($start);
249 function get_pattern()
251 $this->pattern = <<<EOD
254 (\w+) # (2) plugin name
257 ((?:(?!\)[;{]).)*) # (3) parameter
266 ((?:(?R)|(?!};).)*) # (4) body
278 function set($arr, $page)
280 list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
282 // Re-get true plugin name and patameters (for PHP 4.1.2)
284 if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
285 && $matches[1] != $this->plain)
286 list(, $this->plain, $name, $this->param) = $matches;
288 return parent::setParam($page, $name, $body, 'plugin');
293 $body = ($this->body == '') ? '' : make_link($this->body);
296 // Try to call the plugin
297 if (exist_plugin_inline($this->name))
298 $str = do_plugin_inline($this->name, $this->param, $body);
300 if ($str !== FALSE) {
301 return $str; // Succeed
303 // No such plugin, or Failed
304 $body = (($body == '') ? '' : '{' . $body . '}') . ';';
305 return make_line_rules(htmlsc('&' . $this->plain) . $body);
311 class Link_note extends Link
313 function Link_note($start)
315 $this->__construct($start);
317 function __construct($start)
319 parent::__construct($start);
322 function get_pattern()
326 ((?>(?=\(\()(?R)|(?!\)\)).)*) # (1) note body
336 function set($arr, $page)
338 global $foot_explain, $vars;
341 list(, $body) = $this->splice($arr);
343 if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
346 $script = get_page_uri($page);
349 $note = make_link($body);
352 $foot_explain[$id] = '<a id="notefoot_' . $id . '" href="' .
353 $script . '#notetext_' . $id . '" class="note_super">*' .
354 $id . '</a>' . "\n" .
355 '<span class="small">' . $note . '</span><br />';
357 // A hyperlink, content-body to footnote
358 if (! is_numeric(PKWK_FOOTNOTE_TITLE_MAX) || PKWK_FOOTNOTE_TITLE_MAX <= 0) {
361 $title = strip_tags($note);
362 $count = mb_strlen($title, SOURCE_ENCODING);
363 $title = mb_substr($title, 0, PKWK_FOOTNOTE_TITLE_MAX, SOURCE_ENCODING);
364 $abbr = (PKWK_FOOTNOTE_TITLE_MAX < $count) ? '...' : '';
365 $title = ' title="' . $title . $abbr . '"';
367 $name = '<a id="notetext_' . $id . '" href="' . $script .
368 '#notefoot_' . $id . '" class="note_super"' . $title .
371 return parent::setParam($page, $name, $body);
381 class Link_url extends Link
383 function Link_url($start)
385 $this->__construct($start);
387 function __construct($start)
389 parent::__construct($start);
392 function get_pattern()
394 $s1 = $this->start + 1;
396 ((?:\[\[))? # (1) open bracket
398 ((?:(?!\]\]).)+) # (3) alias name
402 (?:(?:https?|ftp|news):\/\/|mailto:)[\w\/\@\$()!?&%#:;.,~'=*+-]+
404 (?($s1)\]\]) # close bracket
413 function set($arr, $page)
415 list(, , , $alias, $name) = $this->splice($arr);
416 return parent::setParam($page, htmlsc($name),
417 '', 'url', $alias == '' ? $name : $alias);
425 $rel = ' rel="nofollow"';
427 return '<a href="' . $this->name . '"' . $rel . '>' . $this->alias . '</a>';
431 // URLs (InterWiki definition on "InterWikiName")
432 class Link_url_interwiki extends Link
434 function Link_url_interwiki($start)
436 $this->__construct($start);
438 function __construct($start)
440 parent::__construct($start);
443 function get_pattern()
448 (?:(?:https?|ftp|news):\/\/|\.\.?\/)[!~*'();\/?:\@&=+\$,%#\w.-]*
461 function set($arr, $page)
463 list(, $name, $alias) = $this->splice($arr);
464 return parent::setParam($page, htmlsc($name), '', 'url', $alias);
469 return '<a href="' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
473 // mailto: URL schemes
474 class Link_mailto extends Link
476 var $is_image, $image;
478 function Link_mailto($start)
480 $this->__construct($start);
482 function __construct($start)
484 parent::__construct($start);
487 function get_pattern()
489 $s1 = $this->start + 1;
493 ((?:(?!\]\]).)+)(?:>|:) # (1) alias
495 ([\w.-]+@[\w-]+\.[\w.-]+) # (2) mailto
496 (?($s1)\]\]) # close bracket if (1)
505 function set($arr, $page)
507 list(, $alias, $name) = $this->splice($arr);
508 return parent::setParam($page, $name, '', 'mailto', $alias == '' ? $name : $alias);
513 return '<a href="mailto:' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
517 // InterWikiName-rendered URLs
518 class Link_interwikiname extends Link
524 function Link_interwikiname($start)
526 $this->__construct($start);
528 function __construct($start)
530 parent::__construct($start);
533 function get_pattern()
535 $s2 = $this->start + 2;
536 $s5 = $this->start + 5;
540 ((?:(?!\]\]).)+)> # (1) alias
542 (\[\[)? # (2) open bracket
543 ((?:(?!\s|:|\]\]).)+) # (3) InterWiki
544 (?<! > | >\[\[ ) # not '>' or '>[['
547 (\[\[)? # (5) open bracket
549 (?($s5)\]\]) # close bracket if (5)
551 (?($s2)\]\]) # close bracket if (2)
561 function set($arr, $page)
563 list(, $alias, , $name, $this->param) = $this->splice($arr);
566 if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $this->param, $matches))
567 list(, $this->param, $this->anchor) = $matches;
569 $url = get_interwiki_url($name, $this->param);
570 $this->url = ($url === FALSE) ?
571 get_base_uri() . '?' . pagename_urlencode('[[' . $name . ':' . $this->param . ']]') :
574 return parent::setParam(
576 htmlsc($name . ':' . $this->param),
579 $alias == '' ? $name . ':' . $this->param : $alias
585 return '<a href="' . $this->url . $this->anchor . '" title="' .
586 $this->name . '" rel="nofollow">' . $this->alias . '</a>';
591 class Link_bracketname extends Link
595 function Link_bracketname($start)
597 $this->__construct($start);
599 function __construct($start)
601 parent::__construct($start);
604 function get_pattern()
606 global $WikiName, $BracketName;
608 $s2 = $this->start + 2;
611 (?:((?:(?!\]\]).)+)>)? # (1) Alias
612 (\[\[)? # (2) Open bracket
618 (\#(?:[a-zA-Z][\w-]*)?)? # (4) Anchor
619 (?($s2)\]\]) # Close bracket if (2)
629 function set($arr, $page)
633 list(, $alias, , $name, $this->anchor) = $this->splice($arr);
634 if ($name == '' && $this->anchor == '') return FALSE;
636 if ($name == '' || ! preg_match('/^' . $WikiName . '$/', $name)) {
637 if ($alias == '') $alias = $name . $this->anchor;
639 $name = get_fullname($name, $page);
640 if (! is_pagename($name)) return FALSE;
644 return parent::setParam($page, $name, '', 'pagename', $alias);
649 return make_pagelink(
659 class Link_wikiname extends Link
661 function Link_wikiname($start)
663 $this->__construct($start);
665 function __construct($start)
667 parent::__construct($start);
670 function get_pattern()
672 global $WikiName, $nowikiname;
674 return $nowikiname ? FALSE : '(' . $WikiName . ')';
682 function set($arr, $page)
684 list($name) = $this->splice($arr);
685 return parent::setParam($page, $name, '', 'pagename', $name);
690 return make_pagelink(
700 class Link_autolink extends Link
702 var $forceignorepages = array();
704 var $auto_a; // alphabet only
706 function Link_autolink($start)
708 $this->__construct($start);
710 function __construct($start)
714 parent::__construct($start);
716 if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
719 @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . 'autolink.dat');
721 $this->auto_a = $auto_a;
722 $this->forceignorepages = explode("\t", trim($forceignorepages));
725 function get_pattern()
727 return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
735 function set($arr, $page)
739 list($name) = $this->splice($arr);
741 // Ignore pages listed, or Expire ones not found
742 if (in_array($name, $this->forceignorepages) || ! is_page($name))
745 return parent::setParam($page, $name, '', 'pagename', $name);
750 return make_pagelink($this->name, $this->alias, '', $this->page, TRUE);
754 class Link_autolink_a extends Link_autolink
756 function Link_autolink_a($start)
758 $this->__construct($start);
760 function __construct($start)
762 parent::__construct($start);
765 function get_pattern()
767 return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
772 class Link_autoalias extends Link
774 var $forceignorepages = array();
776 var $auto_a; // alphabet only
779 function Link_autoalias($start)
781 global $autoalias, $aliaspage;
783 parent::Link($start);
785 if (! $autoalias || ! file_exists(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE) || $this->page == $aliaspage)
790 @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE);
792 $this->auto_a = $auto_a;
793 $this->forceignorepages = explode("\t", trim($forceignorepages));
796 function get_pattern()
798 return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
804 function set($arr,$page)
806 list($name) = $this->splice($arr);
807 // Ignore pages listed
808 if (in_array($name, $this->forceignorepages) || get_autoalias_right_link($name) == '') {
811 return parent::setParam($page,$name,'','pagename',$name);
816 $this->alias = get_autoalias_right_link($this->name);
817 if ($this->alias != '') {
818 $link = '[[' . $this->name . '>' . $this->alias . ']]';
819 return make_link($link);
825 class Link_autoalias_a extends Link_autoalias
827 function Link_autoalias_a($start)
829 parent::Link_autoalias($start);
831 function get_pattern()
833 return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
837 // Make hyperlink for the page
838 function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
840 global $vars, $link_compact, $related, $_symbol_noexists;
842 $script = get_base_uri();
843 $s_page = htmlsc(strip_bracket($page));
844 $s_alias = ($alias == '') ? $s_page : $alias;
846 if ($page == '') return '<a href="' . $anchor . '">' . $s_alias . '</a>';
848 $r_page = pagename_urlencode($page);
849 $r_refer = ($refer == '') ? '' : '&refer=' . rawurlencode($refer);
851 $page_filetime = fast_get_filetime($page);
852 $is_page = $page_filetime !== 0;
853 if (! isset($related[$page]) && $page !== $vars['page'] && is_page)
854 $related[$page] = $page_filetime;
856 if ($isautolink || $is_page) {
857 // Hyperlink to the page
858 $attrs = get_filetime_a_attrs($page_filetime);
861 $al_left = '<!--autolink-->';
862 $al_right = '<!--/autolink-->';
864 $al_left = $al_right = '';
866 $title_attr_html = '';
867 if ($s_page !== $s_alias) {
868 $title_attr_html = ' title="' . $s_page . '"';
870 return $al_left . '<a ' . 'href="' . $script . '?' . $r_page . $anchor .
871 '"' . $title_attr_html . ' class="' .
872 $attrs['class'] . '" data-mtime="' . $attrs['data_mtime'] .
873 '">' . $s_alias . '</a>' . $al_right;
875 // Support Page redirection
876 $redirect_page = get_pagename_on_redirect($page);
877 if ($redirect_page !== false) {
878 return make_pagelink($redirect_page, $s_alias);
881 if (PKWK_READONLY) return $s_alias; // No dacorations
883 if ($_symbol_noexists !== '') {
884 $symbol_html = '<span style="user-select:none;">' .
885 htmlsc($_symbol_noexists) . '</span>';
887 $href = $script . '?cmd=edit&page=' . $r_page . $r_refer;
888 if ($link_compact && $_symbol_noexists != '') {
889 $retval = '<a href="' . $href . '">' . $_symbol_noexists . '</a>';
892 $retval = '<a href="' . $href . '">' . $s_alias . '</a>';
893 return '<span class="noexists">' . $retval . $symbol_html . '</span>';
898 // Resolve relative / (Unix-like)absolute path of the page
899 function get_fullname($name, $refer)
904 if ($name == '' || $name == './') return $refer;
907 if ($name{0} == '/') {
908 $name = substr($name, 1);
909 return ($name == '') ? $defaultpage : $name;
912 // Relative path from 'Here'
913 if (substr($name, 0, 2) == './') {
914 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
916 return join('/', $arrn);
919 // Relative path from dirname()
920 if (substr($name, 0, 3) == '../') {
921 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
922 $arrp = preg_split('#/#', $refer, -1, PREG_SPLIT_NO_EMPTY);
924 while (! empty($arrn) && $arrn[0] == '..') {
928 $name = ! empty($arrp) ? join('/', array_merge($arrp, $arrn)) :
929 (! empty($arrn) ? $defaultpage . '/' . join('/', $arrn) : $defaultpage);
935 // Render an InterWiki into a URL
936 function get_interwiki_url($name, $param)
938 global $WikiName, $interwiki;
939 static $interwikinames;
940 static $encode_aliases = array('sjis'=>'SJIS', 'euc'=>'EUC-JP', 'utf8'=>'UTF-8');
942 if (! isset($interwikinames)) {
943 $interwikinames = $matches = array();
944 foreach (get_source($interwiki) as $line)
945 if (preg_match('/\[(' . '(?:(?:https?|ftp|news):\/\/|\.\.?\/)' .
946 '[!~*\'();\/?:\@&=+\$,%#\w.-]*)\s([^\]]+)\]\s?([^\s]*)/',
948 $interwikinames[$matches[2]] = array($matches[1], $matches[3]);
951 if (! isset($interwikinames[$name])) return FALSE;
953 list($url, $opt) = $interwikinames[$name];
958 case '': /* FALLTHROUGH */
959 case 'std': // Simply URL-encode the string, whose base encoding is the internal-encoding
960 $param = rawurlencode($param);
963 case 'asis': /* FALLTHROUGH */
964 case 'raw' : // Truly as-is
967 case 'yw': // YukiWiki
968 if (! preg_match('/' . $WikiName . '/', $param))
969 $param = '[[' . mb_convert_encoding($param, 'SJIS', SOURCE_ENCODING) . ']]';
972 case 'moin': // MoinMoin
973 $param = str_replace('%', '_', rawurlencode($param));
977 // Alias conversion of $opt
978 if (isset($encode_aliases[$opt])) $opt = & $encode_aliases[$opt];
980 // Encoding conversion into specified encode, and URLencode
981 if (strpos($url, '$1') === FALSE && substr($url, -1) === '?') {
983 $param = pagename_urlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
985 $param = rawurlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
989 // Replace or Add the parameter
990 if (strpos($url, '$1') !== FALSE) {
991 $url = str_replace('$1', $param, $url);
997 if ($len > 512) die_message('InterWiki URL too long: ' . $len . ' characters');
1002 function get_autoticketlink_def_page()
1004 return 'AutoTicketLinkName';
1008 * Get AutoTicketLink - JIRA projects from AutoTiketLinkName page
1010 function get_ticketlink_jira_projects()
1012 $autoticketlink_def_page = get_autoticketlink_def_page();
1013 $active_jira_base_url = null;
1014 $jira_projects = array();
1015 foreach (get_source($autoticketlink_def_page) as $line) {
1016 if (substr($line, 0, 1) !== '-') {
1017 $active_jira_base_url = null;
1021 if (preg_match('/^-\s*(jira)\s+(https?:\/\/[!~*\'();\/?:\@&=+\$,%#\w.-]+)\s*$/', $line, $m)) {
1022 $active_jira_base_url = $m[2];
1023 } else if (preg_match('/^--\s*([A-Z][A-Z0-9]{1,10}(?:_[A-Z0-9]{1,10}){0,2})(\s+(.+?))?\s*$/', $line, $m)) {
1024 if ($active_jira_base_url) {
1025 $project_key = $m[1];
1027 array_push($jira_projects, array(
1030 'base_url' => $active_jira_base_url,
1034 $active_jira_base_url = null;
1037 return $jira_projects;
1040 function init_autoticketlink_def_page()
1042 $autoticketlink_def_page = get_autoticketlink_def_page();
1043 if (is_page($autoticketlink_def_page)) {
1048 * AutoTicketLink definition [#def]
1050 Reference: https://pukiwiki.osdn.jp/?AutoTicketLink
1052 - jira https://site1.example.com/jira/browse/
1053 -- AAA Project title \$1
1054 -- BBB Project title \$1
1055 - jira https://site2.example.com/jira/browse/
1056 -- PROJECTA Site2 \$1
1058 (Default definition) pukiwiki.ini.php
1059 $ticket_jira_default_site = array(
1060 'title' => 'My JIRA - \$1',
1061 'base_url' => 'https://issues.example.com/jira/browse/',
1064 page_write($autoticketlink_def_page, $body);
1067 function init_autoalias_def_page()
1069 global $aliaspage; // 'AutoAliasName'
1070 $autoticketlink_def_page = get_autoticketlink_def_page();
1071 if (is_page($aliaspage)) {
1076 *AutoAliasName [#qf9311bb]
1077 AutoAlias definition
1079 Reference: https://pukiwiki.osdn.jp/?AutoAlias
1081 * PukiWiki [#ee87d39e]
1082 -[[pukiwiki.official>https://pukiwiki.osdn.jp/]]
1083 -[[pukiwiki.dev>https://pukiwiki.osdn.jp/dev/]]
1085 page_write($aliaspage, $body);
1086 update_autoalias_cache_file();