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 $this->__construct($converters, $excludes);
57 function __construct($converters = NULL, $excludes = NULL)
59 if ($converters === NULL) {
61 'plugin', // Inline plugins
64 'url_interwiki', // URLs (interwiki definition)
65 'mailto', // mailto: URL schemes
66 'interwikiname', // InterWikiNames
67 'autolink', // AutoLinks
68 'bracketname', // BracketNames
69 'wikiname', // WikiNames
70 'autolink_a', // AutoLinks(alphabet)
74 if ($excludes !== NULL)
75 $converters = array_diff($converters, $excludes);
77 $this->converters = $patterns = array();
80 foreach ($converters as $name) {
81 $classname = 'Link_' . $name;
82 $converter = new $classname($start);
83 $pattern = $converter->get_pattern();
84 if ($pattern === FALSE) continue;
86 $patterns[] = '(' . "\n" . $pattern . "\n" . ')';
87 $this->converters[$start] = $converter;
88 $start += $converter->get_count();
91 $this->pattern = join('|', $patterns);
94 function convert($string, $page)
97 $this->result = array();
99 $string = preg_replace_callback('/' . $this->pattern . '/x',
100 array(& $this, 'replace'), $string);
102 $arr = explode("\x08", make_line_rules(htmlsc($string)));
104 while (! empty($arr)) {
105 $retval .= array_shift($arr) . array_shift($this->result);
110 function replace($arr)
112 $obj = $this->get_converter($arr);
114 $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
115 $obj->toString() : make_line_rules(htmlsc($arr[0]));
117 return "\x08"; // Add a mark into latest processed part
120 function get_objects($string, $page)
122 $matches = $arr = array();
123 preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
124 foreach ($matches as $match) {
125 $obj = $this->get_converter($match);
126 if ($obj->set($match, $page) !== FALSE) {
127 $arr[] = $this->get_clone($obj);
128 if ($obj->body != '')
129 $arr = array_merge($arr, $this->get_objects($obj->body, $page));
135 function & get_converter(& $arr)
137 foreach (array_keys($this->converters) as $start) {
138 if ($arr[$start] == $arr[0])
139 return $this->converters[$start];
145 // Base class of inline elements
148 var $start; // Origin number of parentheses (0 origin)
149 var $text; // Matched string
157 function Link($start)
159 $this->__construct($start);
161 function __construct($start)
163 $this->start = $start;
166 // Return a regex pattern to match
167 function get_pattern() {}
169 // Return number of parentheses (except (?:...) )
170 function get_count() {}
172 // Set pattern that matches
173 function set($arr, $page) {}
175 function toString() {}
177 // Private: Get needed parts from a matched array()
178 function splice($arr)
180 $count = $this->get_count() + 1;
181 $arr = array_pad(array_splice($arr, $this->start, $count), $count, '');
182 $this->text = $arr[0];
186 // Set basic parameters
187 function setParam($page, $name, $body, $type = '', $alias = '')
189 static $converter = NULL;
195 if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
196 is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
197 $alias = '<img src="' . htmlsc($alias) . '" alt="' . $name . '" />';
198 } else if ($alias != '') {
199 if ($converter === NULL)
200 $converter = new InlineConverter(array('plugin'));
202 $alias = make_line_rules($converter->convert($alias, $page));
204 // BugTrack/669: A hack removing anchor tags added by AutoLink
205 $alias = preg_replace('#</?a[^>]*>#i', '', $alias);
207 $this->alias = $alias;
214 class Link_plugin extends Link
219 function Link_plugin($start)
221 $this->__construct($start);
223 function __construct($start)
225 parent::__construct($start);
228 function get_pattern()
230 $this->pattern = <<<EOD
233 (\w+) # (2) plugin name
236 ((?:(?!\)[;{]).)*) # (3) parameter
245 ((?:(?R)|(?!};).)*) # (4) body
257 function set($arr, $page)
259 list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
261 // Re-get true plugin name and patameters (for PHP 4.1.2)
263 if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
264 && $matches[1] != $this->plain)
265 list(, $this->plain, $name, $this->param) = $matches;
267 return parent::setParam($page, $name, $body, 'plugin');
272 $body = ($this->body == '') ? '' : make_link($this->body);
275 // Try to call the plugin
276 if (exist_plugin_inline($this->name))
277 $str = do_plugin_inline($this->name, $this->param, $body);
279 if ($str !== FALSE) {
280 return $str; // Succeed
282 // No such plugin, or Failed
283 $body = (($body == '') ? '' : '{' . $body . '}') . ';';
284 return make_line_rules(htmlsc('&' . $this->plain) . $body);
290 class Link_note extends Link
292 function Link_note($start)
294 $this->__construct($start);
296 function __construct($start)
298 parent::__construct($start);
301 function get_pattern()
305 ((?>(?=\(\()(?R)|(?!\)\)).)*) # (1) note body
315 function set($arr, $page)
317 global $foot_explain, $vars;
320 list(, $body) = $this->splice($arr);
322 if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
325 $script = get_script_uri() . '?' . pagename_urlencode($page);
329 $note = make_link($body);
330 $page = isset($vars['page']) ? rawurlencode($vars['page']) : '';
333 $foot_explain[$id] = '<a id="notefoot_' . $id . '" href="' .
334 $script . '#notetext_' . $id . '" class="note_super">*' .
335 $id . '</a>' . "\n" .
336 '<span class="small">' . $note . '</span><br />';
338 // A hyperlink, content-body to footnote
339 if (! is_numeric(PKWK_FOOTNOTE_TITLE_MAX) || PKWK_FOOTNOTE_TITLE_MAX <= 0) {
342 $title = strip_tags($note);
343 $count = mb_strlen($title, SOURCE_ENCODING);
344 $title = mb_substr($title, 0, PKWK_FOOTNOTE_TITLE_MAX, SOURCE_ENCODING);
345 $abbr = (mb_strlen($title) < $count) ? '...' : '';
346 $title = ' title="' . $title . $abbr . '"';
348 $name = '<a id="notetext_' . $id . '" href="' . $script .
349 '#notefoot_' . $id . '" class="note_super"' . $title .
352 return parent::setParam($page, $name, $body);
362 class Link_url extends Link
364 function Link_url($start)
366 $this->__construct($start);
368 function __construct($start)
370 parent::__construct($start);
373 function get_pattern()
375 $s1 = $this->start + 1;
377 ((?:\[\[))? # (1) open bracket
379 ((?:(?!\]\]).)+) # (3) alias name
383 (?:(?:https?|ftp|news):\/\/|mailto:)[\w\/\@\$()!?&%#:;.,~'=*+-]+
385 (?($s1)\]\]) # close bracket
394 function set($arr, $page)
396 list(, , , $alias, $name) = $this->splice($arr);
397 return parent::setParam($page, htmlsc($name),
398 '', 'url', $alias == '' ? $name : $alias);
406 $rel = ' rel="nofollow"';
408 return '<a href="' . $this->name . '"' . $rel . '>' . $this->alias . '</a>';
412 // URLs (InterWiki definition on "InterWikiName")
413 class Link_url_interwiki extends Link
415 function Link_url_interwiki($start)
417 $this->__construct($start);
419 function __construct($start)
421 parent::__construct($start);
424 function get_pattern()
429 (?:(?:https?|ftp|news):\/\/|\.\.?\/)[!~*'();\/?:\@&=+\$,%#\w.-]*
442 function set($arr, $page)
444 list(, $name, $alias) = $this->splice($arr);
445 return parent::setParam($page, htmlsc($name), '', 'url', $alias);
450 return '<a href="' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
454 // mailto: URL schemes
455 class Link_mailto extends Link
457 var $is_image, $image;
459 function Link_mailto($start)
461 $this->__construct($start);
463 function __construct($start)
465 parent::__construct($start);
468 function get_pattern()
470 $s1 = $this->start + 1;
474 ((?:(?!\]\]).)+)(?:>|:) # (1) alias
476 ([\w.-]+@[\w-]+\.[\w.-]+) # (2) mailto
477 (?($s1)\]\]) # close bracket if (1)
486 function set($arr, $page)
488 list(, $alias, $name) = $this->splice($arr);
489 return parent::setParam($page, $name, '', 'mailto', $alias == '' ? $name : $alias);
494 return '<a href="mailto:' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
498 // InterWikiName-rendered URLs
499 class Link_interwikiname extends Link
505 function Link_interwikiname($start)
507 $this->__construct($start);
509 function __construct($start)
511 parent::__construct($start);
514 function get_pattern()
516 $s2 = $this->start + 2;
517 $s5 = $this->start + 5;
521 ((?:(?!\]\]).)+)> # (1) alias
523 (\[\[)? # (2) open bracket
524 ((?:(?!\s|:|\]\]).)+) # (3) InterWiki
525 (?<! > | >\[\[ ) # not '>' or '>[['
528 (\[\[)? # (5) open bracket
530 (?($s5)\]\]) # close bracket if (5)
532 (?($s2)\]\]) # close bracket if (2)
542 function set($arr, $page)
546 list(, $alias, , $name, $this->param) = $this->splice($arr);
549 if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $this->param, $matches))
550 list(, $this->param, $this->anchor) = $matches;
552 $url = get_interwiki_url($name, $this->param);
553 $this->url = ($url === FALSE) ?
554 $script . '?' . pagename_urlencode('[[' . $name . ':' . $this->param . ']]') :
557 return parent::setParam(
559 htmlsc($name . ':' . $this->param),
562 $alias == '' ? $name . ':' . $this->param : $alias
568 return '<a href="' . $this->url . $this->anchor . '" title="' .
569 $this->name . '" rel="nofollow">' . $this->alias . '</a>';
574 class Link_bracketname extends Link
578 function Link_bracketname($start)
580 $this->__construct($start);
582 function __construct($start)
584 parent::__construct($start);
587 function get_pattern()
589 global $WikiName, $BracketName;
591 $s2 = $this->start + 2;
594 (?:((?:(?!\]\]).)+)>)? # (1) Alias
595 (\[\[)? # (2) Open bracket
601 (\#(?:[a-zA-Z][\w-]*)?)? # (4) Anchor
602 (?($s2)\]\]) # Close bracket if (2)
612 function set($arr, $page)
616 list(, $alias, , $name, $this->anchor) = $this->splice($arr);
617 if ($name == '' && $this->anchor == '') return FALSE;
619 if ($name == '' || ! preg_match('/^' . $WikiName . '$/', $name)) {
620 if ($alias == '') $alias = $name . $this->anchor;
622 $name = get_fullname($name, $page);
623 if (! is_pagename($name)) return FALSE;
627 return parent::setParam($page, $name, '', 'pagename', $alias);
632 return make_pagelink(
642 class Link_wikiname extends Link
644 function Link_wikiname($start)
646 $this->__construct($start);
648 function __construct($start)
650 parent::__construct($start);
653 function get_pattern()
655 global $WikiName, $nowikiname;
657 return $nowikiname ? FALSE : '(' . $WikiName . ')';
665 function set($arr, $page)
667 list($name) = $this->splice($arr);
668 return parent::setParam($page, $name, '', 'pagename', $name);
673 return make_pagelink(
683 class Link_autolink extends Link
685 var $forceignorepages = array();
687 var $auto_a; // alphabet only
689 function Link_autolink($start)
691 $this->__construct($start);
693 function __construct($start)
697 parent::__construct($start);
699 if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
702 @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . 'autolink.dat');
704 $this->auto_a = $auto_a;
705 $this->forceignorepages = explode("\t", trim($forceignorepages));
708 function get_pattern()
710 return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
718 function set($arr, $page)
722 list($name) = $this->splice($arr);
724 // Ignore pages listed, or Expire ones not found
725 if (in_array($name, $this->forceignorepages) || ! is_page($name))
728 return parent::setParam($page, $name, '', 'pagename', $name);
733 return make_pagelink($this->name, $this->alias, '', $this->page, TRUE);
737 class Link_autolink_a extends Link_autolink
739 function Link_autolink_a($start)
741 $this->__construct($start);
743 function __construct($start)
745 parent::__construct($start);
748 function get_pattern()
750 return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
754 // Make hyperlink for the page
755 function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
757 global $script, $vars, $link_compact, $related, $_symbol_noexists;
759 $s_page = htmlsc(strip_bracket($page));
760 $s_alias = ($alias == '') ? $s_page : $alias;
762 if ($page == '') return '<a href="' . $anchor . '">' . $s_alias . '</a>';
764 $r_page = pagename_urlencode($page);
765 $r_refer = ($refer == '') ? '' : '&refer=' . rawurlencode($refer);
767 if (! isset($related[$page]) && $page !== $vars['page'] && is_page($page))
768 $related[$page] = get_filetime($page);
770 if ($isautolink || is_page($page)) {
771 // Hyperlink to the page
775 $title = ' title="' . $s_page . get_pg_passage($page, FALSE) . '"';
780 $al_left = '<!--autolink-->';
781 $al_right = '<!--/autolink-->';
783 $al_left = $al_right = '';
786 return $al_left . '<a ' . 'href="' . $script . '?' . $r_page . $anchor .
787 '"' . $title . '>' . $s_alias . '</a>' . $al_right;
789 // Support Page redirection
790 $redirect_page = get_pagename_on_redirect($page);
791 if ($redirect_page !== false) {
792 return make_pagelink($redirect_page, $s_alias);
795 if (PKWK_READONLY) return $s_alias; // No dacorations
797 $retval = $s_alias . '<a href="' .
798 $script . '?cmd=edit&page=' . $r_page . $r_refer . '">' .
799 $_symbol_noexists . '</a>';
804 return '<span class="noexists">' . $retval . '</span>';
809 // Resolve relative / (Unix-like)absolute path of the page
810 function get_fullname($name, $refer)
815 if ($name == '' || $name == './') return $refer;
818 if ($name{0} == '/') {
819 $name = substr($name, 1);
820 return ($name == '') ? $defaultpage : $name;
823 // Relative path from 'Here'
824 if (substr($name, 0, 2) == './') {
825 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
827 return join('/', $arrn);
830 // Relative path from dirname()
831 if (substr($name, 0, 3) == '../') {
832 $arrn = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
833 $arrp = preg_split('#/#', $refer, -1, PREG_SPLIT_NO_EMPTY);
835 while (! empty($arrn) && $arrn[0] == '..') {
839 $name = ! empty($arrp) ? join('/', array_merge($arrp, $arrn)) :
840 (! empty($arrn) ? $defaultpage . '/' . join('/', $arrn) : $defaultpage);
846 // Render an InterWiki into a URL
847 function get_interwiki_url($name, $param)
849 global $WikiName, $interwiki;
850 static $interwikinames;
851 static $encode_aliases = array('sjis'=>'SJIS', 'euc'=>'EUC-JP', 'utf8'=>'UTF-8');
853 if (! isset($interwikinames)) {
854 $interwikinames = $matches = array();
855 foreach (get_source($interwiki) as $line)
856 if (preg_match('/\[(' . '(?:(?:https?|ftp|news):\/\/|\.\.?\/)' .
857 '[!~*\'();\/?:\@&=+\$,%#\w.-]*)\s([^\]]+)\]\s?([^\s]*)/',
859 $interwikinames[$matches[2]] = array($matches[1], $matches[3]);
862 if (! isset($interwikinames[$name])) return FALSE;
864 list($url, $opt) = $interwikinames[$name];
869 case '': /* FALLTHROUGH */
870 case 'std': // Simply URL-encode the string, whose base encoding is the internal-encoding
871 $param = rawurlencode($param);
874 case 'asis': /* FALLTHROUGH */
875 case 'raw' : // Truly as-is
878 case 'yw': // YukiWiki
879 if (! preg_match('/' . $WikiName . '/', $param))
880 $param = '[[' . mb_convert_encoding($param, 'SJIS', SOURCE_ENCODING) . ']]';
883 case 'moin': // MoinMoin
884 $param = str_replace('%', '_', rawurlencode($param));
888 // Alias conversion of $opt
889 if (isset($encode_aliases[$opt])) $opt = & $encode_aliases[$opt];
891 // Encoding conversion into specified encode, and URLencode
892 if (strpos($url, '$1') === FALSE && substr($url, -1) === '?') {
894 $param = pagename_urlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
896 $param = rawurlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
900 // Replace or Add the parameter
901 if (strpos($url, '$1') !== FALSE) {
902 $url = str_replace('$1', $param, $url);
908 if ($len > 512) die_message('InterWiki URL too long: ' . $len . ' characters');