2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2003-2016 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 // Hyperlink decoration
12 function make_link($string, $page = '')
17 if (! isset($converter)) $converter = new InlineConverter();
19 $clone = $converter->get_clone($converter);
21 return $clone->convert($string, ($page != '') ? $page : $vars['page']);
24 // Converters of inline element
27 var $converters; // as array()
32 function get_clone($obj) {
35 if (! isset($clone_func)) {
36 if (version_compare(PHP_VERSION, '5.0.0', '<')) {
37 $clone_func = create_function('$a', 'return $a;');
39 $clone_func = create_function('$a', 'return clone $a;');
42 return $clone_func($obj);
46 $converters = array();
47 foreach ($this->converters as $key=>$converter) {
48 $converters[$key] = $this->get_clone($converter);
50 $this->converters = $converters;
53 function InlineConverter($converters = NULL, $excludes = NULL)
55 if ($converters === NULL) {
57 'plugin', // Inline plugins
60 'url_interwiki', // URLs (interwiki definition)
61 'mailto', // mailto: URL schemes
62 'interwikiname', // InterWikiNames
63 'autolink', // AutoLinks
64 'bracketname', // BracketNames
65 'wikiname', // WikiNames
66 'autolink_a', // AutoLinks(alphabet)
70 if ($excludes !== NULL)
71 $converters = array_diff($converters, $excludes);
73 $this->converters = $patterns = array();
76 foreach ($converters as $name) {
77 $classname = 'Link_' . $name;
78 $converter = new $classname($start);
79 $pattern = $converter->get_pattern();
80 if ($pattern === FALSE) continue;
82 $patterns[] = '(' . "\n" . $pattern . "\n" . ')';
83 $this->converters[$start] = $converter;
84 $start += $converter->get_count();
87 $this->pattern = join('|', $patterns);
90 function convert($string, $page)
93 $this->result = array();
95 $string = preg_replace_callback('/' . $this->pattern . '/x',
96 array(& $this, 'replace'), $string);
98 $arr = explode("\x08", make_line_rules(htmlsc($string)));
100 while (! empty($arr)) {
101 $retval .= array_shift($arr) . array_shift($this->result);
106 function replace($arr)
108 $obj = $this->get_converter($arr);
110 $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
111 $obj->toString() : make_line_rules(htmlsc($arr[0]));
113 return "\x08"; // Add a mark into latest processed part
116 function get_objects($string, $page)
118 $matches = $arr = array();
119 preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
120 foreach ($matches as $match) {
121 $obj = $this->get_converter($match);
122 if ($obj->set($match, $page) !== FALSE) {
123 $arr[] = $this->get_clone($obj);
124 if ($obj->body != '')
125 $arr = array_merge($arr, $this->get_objects($obj->body, $page));
131 function & get_converter(& $arr)
133 foreach (array_keys($this->converters) as $start) {
134 if ($arr[$start] == $arr[0])
135 return $this->converters[$start];
141 // Base class of inline elements
144 var $start; // Origin number of parentheses (0 origin)
145 var $text; // Matched string
154 function Link($start)
156 $this->start = $start;
159 // Return a regex pattern to match
160 function get_pattern() {}
162 // Return number of parentheses (except (?:...) )
163 function get_count() {}
165 // Set pattern that matches
166 function set($arr, $page) {}
168 function toString() {}
170 // Private: Get needed parts from a matched array()
171 function splice($arr)
173 $count = $this->get_count() + 1;
174 $arr = array_pad(array_splice($arr, $this->start, $count), $count, '');
175 $this->text = $arr[0];
179 // Set basic parameters
180 function setParam($page, $name, $body, $type = '', $alias = '')
182 static $converter = NULL;
188 if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
189 is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
190 $alias = '<img src="' . htmlsc($alias) . '" alt="' . $name . '" />';
191 } else if ($alias != '') {
192 if ($converter === NULL)
193 $converter = new InlineConverter(array('plugin'));
195 $alias = make_line_rules($converter->convert($alias, $page));
197 // BugTrack/669: A hack removing anchor tags added by AutoLink
198 $alias = preg_replace('#</?a[^>]*>#i', '', $alias);
200 $this->alias = $alias;
207 class Link_plugin extends Link
212 function Link_plugin($start)
214 parent::Link($start);
217 function get_pattern()
219 $this->pattern = <<<EOD
222 (\w+) # (2) plugin name
225 ((?:(?!\)[;{]).)*) # (3) parameter
234 ((?:(?R)|(?!};).)*) # (4) body
246 function set($arr, $page)
248 list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
250 // Re-get true plugin name and patameters (for PHP 4.1.2)
252 if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
253 && $matches[1] != $this->plain)
254 list(, $this->plain, $name, $this->param) = $matches;
256 return parent::setParam($page, $name, $body, 'plugin');
261 $body = ($this->body == '') ? '' : make_link($this->body);
264 // Try to call the plugin
265 if (exist_plugin_inline($this->name))
266 $str = do_plugin_inline($this->name, $this->param, $body);
268 if ($str !== FALSE) {
269 return $str; // Succeed
271 // No such plugin, or Failed
272 $body = (($body == '') ? '' : '{' . $body . '}') . ';';
273 return make_line_rules(htmlsc('&' . $this->plain) . $body);
279 class Link_note extends Link
281 function Link_note($start)
283 parent::Link($start);
286 function get_pattern()
290 ((?>(?=\(\()(?R)|(?!\)\)).)*) # (1) note body
300 function set($arr, $page)
302 global $foot_explain, $vars;
305 list(, $body) = $this->splice($arr);
307 if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
310 $script = get_script_uri() . '?' . pagename_urlencode($page);
314 $note = make_link($body);
315 $page = isset($vars['page']) ? rawurlencode($vars['page']) : '';
318 $foot_explain[$id] = '<a id="notefoot_' . $id . '" href="' .
319 $script . '#notetext_' . $id . '" class="note_super">*' .
320 $id . '</a>' . "\n" .
321 '<span class="small">' . $note . '</span><br />';
323 // A hyperlink, content-body to footnote
324 if (! is_numeric(PKWK_FOOTNOTE_TITLE_MAX) || PKWK_FOOTNOTE_TITLE_MAX <= 0) {
327 $title = strip_tags($note);
328 $count = mb_strlen($title, SOURCE_ENCODING);
329 $title = mb_substr($title, 0, PKWK_FOOTNOTE_TITLE_MAX, SOURCE_ENCODING);
330 $abbr = (mb_strlen($title) < $count) ? '...' : '';
331 $title = ' title="' . $title . $abbr . '"';
333 $name = '<a id="notetext_' . $id . '" href="' . $script .
334 '#notefoot_' . $id . '" class="note_super"' . $title .
337 return parent::setParam($page, $name, $body);
347 class Link_url extends Link
349 function Link_url($start)
351 parent::Link($start);
354 function get_pattern()
356 $s1 = $this->start + 1;
358 ((?:\[\[))? # (1) open bracket
360 ((?:(?!\]\]).)+) # (3) alias name
364 (?:(?:https?|ftp|news):\/\/|mailto:)[\w\/\@\$()!?&%#:;.,~'=*+-]+
366 (?($s1)\]\]) # close bracket
375 function set($arr, $page)
377 list(, , , $alias, $name) = $this->splice($arr);
378 return parent::setParam($page, htmlsc($name),
379 '', 'url', $alias == '' ? $name : $alias);
387 $rel = ' rel="nofollow"';
389 return '<a href="' . $this->name . '"' . $rel . '>' . $this->alias . '</a>';
393 // URLs (InterWiki definition on "InterWikiName")
394 class Link_url_interwiki extends Link
396 function Link_url_interwiki($start)
398 parent::Link($start);
401 function get_pattern()
406 (?:(?:https?|ftp|news):\/\/|\.\.?\/)[!~*'();\/?:\@&=+\$,%#\w.-]*
419 function set($arr, $page)
421 list(, $name, $alias) = $this->splice($arr);
422 return parent::setParam($page, htmlsc($name), '', 'url', $alias);
427 return '<a href="' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
431 // mailto: URL schemes
432 class Link_mailto extends Link
434 var $is_image, $image;
436 function Link_mailto($start)
438 parent::Link($start);
441 function get_pattern()
443 $s1 = $this->start + 1;
447 ((?:(?!\]\]).)+)(?:>|:) # (1) alias
449 ([\w.-]+@[\w-]+\.[\w.-]+) # (2) mailto
450 (?($s1)\]\]) # close bracket if (1)
459 function set($arr, $page)
461 list(, $alias, $name) = $this->splice($arr);
462 return parent::setParam($page, $name, '', 'mailto', $alias == '' ? $name : $alias);
467 return '<a href="mailto:' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
471 // InterWikiName-rendered URLs
472 class Link_interwikiname extends Link
478 function Link_interwikiname($start)
480 parent::Link($start);
483 function get_pattern()
485 $s2 = $this->start + 2;
486 $s5 = $this->start + 5;
490 ((?:(?!\]\]).)+)> # (1) alias
492 (\[\[)? # (2) open bracket
493 ((?:(?!\s|:|\]\]).)+) # (3) InterWiki
494 (?<! > | >\[\[ ) # not '>' or '>[['
497 (\[\[)? # (5) open bracket
499 (?($s5)\]\]) # close bracket if (5)
501 (?($s2)\]\]) # close bracket if (2)
511 function set($arr, $page)
515 list(, $alias, , $name, $this->param) = $this->splice($arr);
518 if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $this->param, $matches))
519 list(, $this->param, $this->anchor) = $matches;
521 $url = get_interwiki_url($name, $this->param);
522 $this->url = ($url === FALSE) ?
523 $script . '?' . pagename_urlencode('[[' . $name . ':' . $this->param . ']]') :
526 return parent::setParam(
528 htmlsc($name . ':' . $this->param),
531 $alias == '' ? $name . ':' . $this->param : $alias
537 return '<a href="' . $this->url . $this->anchor . '" title="' .
538 $this->name . '" rel="nofollow">' . $this->alias . '</a>';
543 class Link_bracketname extends Link
547 function Link_bracketname($start)
549 parent::Link($start);
552 function get_pattern()
554 global $WikiName, $BracketName;
556 $s2 = $this->start + 2;
559 (?:((?:(?!\]\]).)+)>)? # (1) Alias
560 (\[\[)? # (2) Open bracket
566 (\#(?:[a-zA-Z][\w-]*)?)? # (4) Anchor
567 (?($s2)\]\]) # Close bracket if (2)
577 function set($arr, $page)
581 list(, $alias, , $name, $this->anchor) = $this->splice($arr);
582 if ($name == '' && $this->anchor == '') return FALSE;
584 if ($name == '' || ! preg_match('/^' . $WikiName . '$/', $name)) {
585 if ($alias == '') $alias = $name . $this->anchor;
587 $name = get_fullname($name, $page);
588 if (! is_pagename($name)) return FALSE;
592 return parent::setParam($page, $name, '', 'pagename', $alias);
597 return make_pagelink(
607 class Link_wikiname extends Link
609 function Link_wikiname($start)
611 parent::Link($start);
614 function get_pattern()
616 global $WikiName, $nowikiname;
618 return $nowikiname ? FALSE : '(' . $WikiName . ')';
626 function set($arr, $page)
628 list($name) = $this->splice($arr);
629 return parent::setParam($page, $name, '', 'pagename', $name);
634 return make_pagelink(
644 class Link_autolink extends Link
646 var $forceignorepages = array();
648 var $auto_a; // alphabet only
650 function Link_autolink($start)
654 parent::Link($start);
656 if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
659 @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . 'autolink.dat');
661 $this->auto_a = $auto_a;
662 $this->forceignorepages = explode("\t", trim($forceignorepages));
665 function get_pattern()
667 return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
675 function set($arr, $page)
679 list($name) = $this->splice($arr);
681 // Ignore pages listed, or Expire ones not found
682 if (in_array($name, $this->forceignorepages) || ! is_page($name))
685 return parent::setParam($page, $name, '', 'pagename', $name);
690 return make_pagelink($this->name, $this->alias, '', $this->page, TRUE);
694 class Link_autolink_a extends Link_autolink
696 function Link_autolink_a($start)
698 parent::Link_autolink($start);
701 function get_pattern()
703 return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
707 // Make hyperlink for the page
708 function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
710 global $script, $vars, $link_compact, $related, $_symbol_noexists;
712 $s_page = htmlsc(strip_bracket($page));
713 $s_alias = ($alias == '') ? $s_page : $alias;
715 if ($page == '') return '<a href="' . $anchor . '">' . $s_alias . '</a>';
717 $r_page = pagename_urlencode($page);
718 $r_refer = ($refer == '') ? '' : '&refer=' . rawurlencode($refer);
720 if (! isset($related[$page]) && $page !== $vars['page'] && is_page($page))
721 $related[$page] = get_filetime($page);
723 if ($isautolink || is_page($page)) {
724 // Hyperlink to the page
728 $title = ' title="' . $s_page . get_pg_passage($page, FALSE) . '"';
733 $al_left = '<!--autolink-->';
734 $al_right = '<!--/autolink-->';
736 $al_left = $al_right = '';
739 return $al_left . '<a ' . 'href="' . $script . '?' . $r_page . $anchor .
740 '"' . $title . '>' . $s_alias . '</a>' . $al_right;
743 if (PKWK_READONLY) return $s_alias; // No dacorations
745 $retval = $s_alias . '<a href="' .
746 $script . '?cmd=edit&page=' . $r_page . $r_refer . '">' .
747 $_symbol_noexists . '</a>';
752 return '<span class="noexists">' . $retval . '</span>';
757 // Resolve relative / (Unix-like)absolute path of the page
758 function get_fullname($name, $refer)
763 if ($name == '' || $name == './') return $refer;
766 if ($name{0} == '/') {
767 $name = substr($name, 1);
768 return ($name == '') ? $defaultpage : $name;
771 // Relative path from 'Here'
772 if (substr($name, 0, 2) == './') {
773 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
775 return join('/', $arrn);
778 // Relative path from dirname()
779 if (substr($name, 0, 3) == '../') {
780 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
781 $arrp = preg_split('#/#', $refer, -1, PREG_SPLIT_NO_EMPTY);
783 while (! empty($arrn) && $arrn[0] == '..') {
787 $name = ! empty($arrp) ? join('/', array_merge($arrp, $arrn)) :
788 (! empty($arrn) ? $defaultpage . '/' . join('/', $arrn) : $defaultpage);
794 // Render an InterWiki into a URL
795 function get_interwiki_url($name, $param)
797 global $WikiName, $interwiki;
798 static $interwikinames;
799 static $encode_aliases = array('sjis'=>'SJIS', 'euc'=>'EUC-JP', 'utf8'=>'UTF-8');
801 if (! isset($interwikinames)) {
802 $interwikinames = $matches = array();
803 foreach (get_source($interwiki) as $line)
804 if (preg_match('/\[(' . '(?:(?:https?|ftp|news):\/\/|\.\.?\/)' .
805 '[!~*\'();\/?:\@&=+\$,%#\w.-]*)\s([^\]]+)\]\s?([^\s]*)/',
807 $interwikinames[$matches[2]] = array($matches[1], $matches[3]);
810 if (! isset($interwikinames[$name])) return FALSE;
812 list($url, $opt) = $interwikinames[$name];
817 case '': /* FALLTHROUGH */
818 case 'std': // Simply URL-encode the string, whose base encoding is the internal-encoding
819 $param = rawurlencode($param);
822 case 'asis': /* FALLTHROUGH */
823 case 'raw' : // Truly as-is
826 case 'yw': // YukiWiki
827 if (! preg_match('/' . $WikiName . '/', $param))
828 $param = '[[' . mb_convert_encoding($param, 'SJIS', SOURCE_ENCODING) . ']]';
831 case 'moin': // MoinMoin
832 $param = str_replace('%', '_', rawurlencode($param));
836 // Alias conversion of $opt
837 if (isset($encode_aliases[$opt])) $opt = & $encode_aliases[$opt];
839 // Encoding conversion into specified encode, and URLencode
840 $param = rawurlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
843 // Replace or Add the parameter
844 if (strpos($url, '$1') !== FALSE) {
845 $url = str_replace('$1', $param, $url);
851 if ($len > 512) die_message('InterWiki URL too long: ' . $len . ' characters');