OSDN Git Service

PKWK_SAFE_MODE Prohibits not using nofollow anchor
[pukiwiki/pukiwiki.git] / lib / make_link.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: make_link.php,v 1.15 2005/01/25 14:18:57 henoheno Exp $
4 //
5 // Hyperlink-related functions
6
7 // Hyperlink decoration
8 function make_link($string, $page = '')
9 {
10         global $vars;
11         static $converter;
12
13         if (! isset($converter)) $converter = new InlineConverter();
14
15         $clone = $converter->get_clone($converter);
16
17         return $clone->convert($string, ($page != '') ? $page : $vars['page']);
18 }
19
20 // Converters of inline element
21 class InlineConverter
22 {
23         var $converters; // as array()
24         var $pattern;
25         var $pos;
26         var $result;
27
28         function get_clone($obj) {
29                 static $clone_func;
30
31                 if (! isset($clone_func)) {
32                         if (version_compare(PHP_VERSION, '5.0.0', '<')) {
33                                 $clone_func = create_function('$a', 'return $a;');
34                         } else {
35                                 $clone_func = create_function('$a', 'return clone $a;');
36                         }
37                 }
38                 return $clone_func($obj);
39         }
40
41         function __clone() {
42                 $converters = array();
43                 foreach ($this->converters as $key=>$converter) {
44                         $converters[$key] = $this->get_clone($converter);
45                 }
46                 $this->converters = $converters;
47         }
48
49         function InlineConverter($converters = NULL, $excludes = NULL)
50         {
51                 if ($converters === NULL) {
52                         $converters = array(
53                                 'plugin',        // Inline plugins
54                                 'note',          // Footnotes
55                                 'url',           // URLs
56                                 'url_interwiki', // URLs (interwiki definition)
57                                 'mailto',        // mailto: URL schemes
58                                 'interwikiname', // InterWikiNames
59                                 'autolink',      // AutoLinks
60                                 'bracketname',   // BracketNames
61                                 'wikiname',      // WikiNames
62                                 'autolink_a',    // AutoLinks(alphabet)
63                         );
64                 }
65
66                 if ($excludes !== NULL)
67                         $converters = array_diff($converters, $excludes);
68
69                 $this->converters = $patterns = array();
70                 $start = 1;
71
72                 foreach ($converters as $name) {
73                         $classname = 'Link_' . $name;
74                         $converter = new $classname($start);
75                         $pattern   = $converter->get_pattern();
76                         if ($pattern === FALSE) continue;
77
78                         $patterns[] = '(' . "\n" . $pattern . "\n" . ')';
79                         $this->converters[$start] = $converter;
80                         $start += $converter->get_count();
81                         ++$start;
82                 }
83                 $this->pattern = join('|', $patterns);
84         }
85
86         function convert($string, $page)
87         {
88                 $this->page   = $page;
89                 $this->result = array();
90
91                 $string = preg_replace_callback('/' . $this->pattern . '/x',
92                         array(& $this, 'replace'), $string);
93
94                 $arr = explode("\x08", make_line_rules(htmlspecialchars($string)));
95                 $retval = '';
96                 while (! empty($arr)) {
97                         $retval .= array_shift($arr) . array_shift($this->result);
98                 }
99                 return $retval;
100         }
101
102         function replace($arr)
103         {
104                 $obj = $this->get_converter($arr);
105
106                 $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
107                         $obj->toString() : make_line_rules(htmlspecialchars($arr[0]));
108
109                 return "\x08"; // Add a mark into latest processed part
110         }
111
112         function get_objects($string, $page)
113         {
114                 $matches = $arr = array();
115                 preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
116                 foreach ($matches as $match) {
117                         $obj = $this->get_converter($match);
118                         if ($obj->set($match, $page) !== FALSE) {
119                                 $arr[] = $this->get_clone($obj);
120                                 if ($obj->body != '')
121                                         $arr = array_merge($arr, $this->get_objects($obj->body, $page));
122                         }
123                 }
124                 return $arr;
125         }
126
127         function & get_converter(& $arr)
128         {
129                 foreach (array_keys($this->converters) as $start) {
130                         if ($arr[$start] == $arr[0])
131                                 return $this->converters[$start];
132                 }
133                 return NULL;
134         }
135 }
136
137 // Base class of inline elements
138 class Link
139 {
140         var $start;   // Origin number of parentheses (0 origin)
141         var $text;    // Matched string
142
143         var $type;
144         var $page;
145         var $name;
146         var $body;
147         var $alias;
148
149         // Constructor
150         function Link($start)
151         {
152                 $this->start = $start;
153         }
154
155         // Return a regex pattern to match
156         function get_pattern() {}
157
158         // Return number of parentheses (except (?:...) )
159         function get_count() {}
160
161         // Set pattern that matches
162         function set($arr, $page) {}
163
164         function toString() {}
165
166         // Private: Get needed parts from a matched array()
167         function splice($arr) {
168                 $count = $this->get_count() + 1;
169                 $arr   = array_pad(array_splice($arr, $this->start, $count), $count, '');
170                 $this->text = $arr[0];
171                 return $arr;
172         }
173
174         // Set basic parameters
175         function setParam($page, $name, $body, $type = '', $alias = '')
176         {
177                 static $converter = NULL;
178
179                 $this->page = $page;
180                 $this->name = $name;
181                 $this->body = $body;
182                 $this->type = $type;
183                 if (is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
184                         $alias = htmlspecialchars($alias);
185                         $alias = '<img src="' . $alias . '" alt="' . $name . '" />';
186                 } else if ($alias != '') {
187                         if ($converter === NULL)
188                                 $converter = new InlineConverter(array('plugin'));
189
190                         $alias = make_line_rules($converter->convert($alias, $page));
191
192                         // BugTrack/669: A hack removing anchor tags added by AutoLink
193                         $alias = preg_replace('#</?a[^>]*>#i', '', $alias);
194                 }
195                 $this->alias = $alias;
196
197                 return TRUE;
198         }
199 }
200
201 // Inline plugins
202 class Link_plugin extends Link
203 {
204         var $pattern;
205         var $plain,$param;
206
207         function Link_plugin($start)
208         {
209                 parent::Link($start);
210         }
211
212         function get_pattern()
213         {
214                 $this->pattern = <<<EOD
215 &
216 (      # (1) plain
217  (\w+) # (2) plugin name
218  (?:
219   \(
220    ((?:(?!\)[;{]).)*) # (3) parameter
221   \)
222  )?
223 )
224 EOD;
225                 return <<<EOD
226 {$this->pattern}
227 (?:
228  \{
229   ((?:(?R)|(?!};).)*) # (4) body
230  \}
231 )?
232 ;
233 EOD;
234         }
235
236         function get_count()
237         {
238                 return 4;
239         }
240
241         function set($arr, $page)
242         {
243                 list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
244
245                 // Re-get true plugin name and patameters (for PHP 4.1.2)
246                 $matches = array();
247                 if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
248                         && $matches[1] != $this->plain) 
249                         list(, $this->plain, $name, $this->param) = $matches;
250
251                 return parent::setParam($page, $name, $body, 'plugin');
252         }
253
254         function toString()
255         {
256                 $body = ($this->body == '') ? '' : make_link($this->body);
257                 $str = FALSE;
258
259                 // Try to call the plugin
260                 if (exist_plugin_inline($this->name))
261                         $str = do_plugin_inline($this->name, $this->param, $body);
262
263                 if ($str !== FALSE) {
264                         return $str; // Succeed
265                 } else {
266                         // No such plugin, or Failed
267                         $body = (($body == '') ? '' : '{' . $body . '}') . ';';
268                         return make_line_rules(htmlspecialchars('&' . $this->plain) . $body);
269                 }
270         }
271 }
272
273 // Footnotes
274 class Link_note extends Link
275 {
276         function Link_note($start)
277         {
278                 parent::Link($start);
279         }
280
281         function get_pattern()
282         {
283                 return <<<EOD
284 \(\(
285  ((?:(?R)|(?!\)\)).)*) # (1) note body
286 \)\)
287 EOD;
288         }
289
290         function get_count()
291         {
292                 return 1;
293         }
294
295         function set($arr, $page)
296         {
297                 global $foot_explain, $script, $vars;
298                 static $note_id = 0;
299
300                 list(, $body) = $this->splice($arr);
301
302                 $id   = ++$note_id;
303                 $note = make_link($body);
304                 $page = isset($vars['page']) ? htmlspecialchars($vars['page']) : '';
305
306                 // Footnote
307                 $foot_explain[$id] = <<<EOD
308 <a id="notefoot_$id" href="$script?$page#notetext_$id" class="note_super">*$id</a>
309 <span class="small">$note</span>
310 <br />
311 EOD;
312                 // A hyperlink, content-body to footnote
313                 $name = '<a id="notetext_' . $id . '" href="' . $script . '?' . $page .
314                         '#notefoot_' . $id . '" class="note_super" title="' .
315                         htmlspecialchars(strip_tags($note)) . '">*' . $id . '</a>';
316
317                 return parent::setParam($page, $name, $body);
318         }
319
320         function toString()
321         {
322                 return $this->name;
323         }
324 }
325
326 // URLs
327 class Link_url extends Link
328 {
329         function Link_url($start)
330         {
331                 parent::Link($start);
332         }
333
334         function get_pattern()
335         {
336                 $s1 = $this->start + 1;
337                 return <<<EOD
338 (\[\[             # (1) open bracket
339  ((?:(?!\]\]).)+) # (2) alias
340  (?:>|:)
341 )?
342 (                 # (3) url
343  (?:(?:https?|ftp|news):\/\/|mailto:)[\w\/\@\$()!?&%#:;.,~'=*+-]+
344 )
345 (?($s1)\]\])      # close bracket
346 EOD;
347         }
348
349         function get_count()
350         {
351                 return 3;
352         }
353
354         function set($arr, $page)
355         {
356                 list(, , $alias, $name) = $this->splice($arr);
357                 return parent::setParam($page, htmlspecialchars($name),
358                         '', 'url', $alias == '' ? $name : $alias);
359         }
360
361         function toString()
362         {
363                 if (! PKWK_SAFE_MODE && PKWK_READONLY) {
364                         $rel = '';
365                 } else {
366                         $rel = ' rel="nofollow"';
367                 }
368                 return '<a href="' . $this->name . '"' . $rel . '>' . $this->alias . '</a>';
369         }
370 }
371
372 // URLs (InterWiki definition on "InterWikiName")
373 class Link_url_interwiki extends Link
374 {
375         function Link_url_interwiki($start)
376         {
377                 parent::Link($start);
378         }
379
380         function get_pattern()
381         {
382                 return <<<EOD
383 \[       # open bracket
384 (        # (1) url
385  (?:(?:https?|ftp|news):\/\/|\.\.?\/)[!~*'();\/?:\@&=+\$,%#\w.-]*
386 )
387 \s
388 ([^\]]+) # (2) alias
389 \]       # close bracket
390 EOD;
391         }
392
393         function get_count()
394         {
395                 return 2;
396         }
397
398         function set($arr, $page)
399         {
400                 list(, $name, $alias) = $this->splice($arr);
401                 return parent::setParam($page, htmlspecialchars($name), '', 'url', $alias);
402         }
403
404         function toString()
405         {
406                 return '<a href="' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
407         }
408 }
409
410 // mailto: URL schemes
411 class Link_mailto extends Link
412 {
413         var $is_image, $image;
414
415         function Link_mailto($start)
416         {
417                 parent::Link($start);
418         }
419
420         function get_pattern()
421         {
422                 $s1 = $this->start + 1;
423                 return <<<EOD
424 (?:
425  \[\[
426  ((?:(?!\]\]).)+)(?:>|:)  # (1) alias
427 )?
428 ([\w.-]+@[\w-]+\.[\w.-]+) # (2) mailto
429 (?($s1)\]\])              # close bracket if (1)
430 EOD;
431         }
432
433         function get_count()
434         {
435                 return 2;
436         }
437
438         function set($arr, $page)
439         {
440                 list(, $alias, $name) = $this->splice($arr);
441                 return parent::setParam($page, $name, '', 'mailto', $alias == '' ? $name : $alias);
442         }
443         
444         function toString()
445         {
446                 return '<a href="mailto:' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
447         }
448 }
449
450 // InterWikiName-rendered URLs
451 class Link_interwikiname extends Link
452 {
453         var $url    = '';
454         var $param  = '';
455         var $anchor = '';
456
457         function Link_interwikiname($start)
458         {
459                 parent::Link($start);
460         }
461
462         function get_pattern()
463         {
464                 $s2 = $this->start + 2;
465                 $s5 = $this->start + 5;
466                 return <<<EOD
467 \[\[                  # open bracket
468 (?:
469  ((?:(?!\]\]).)+)>    # (1) alias
470 )?
471 (\[\[)?               # (2) open bracket
472 ((?:(?!\s|:|\]\]).)+) # (3) InterWiki
473 (?<! > | >\[\[ )      # not '>' or '>[['
474 :                     # separator
475 (                     # (4) param
476  (\[\[)?              # (5) open bracket
477  (?:(?!>|\]\]).)+
478  (?($s5)\]\])         # close bracket if (5)
479 )
480 (?($s2)\]\])          # close bracket if (2)
481 \]\]                  # close bracket
482 EOD;
483         }
484
485         function get_count()
486         {
487                 return 5;
488         }
489
490         function set($arr, $page)
491         {
492                 global $script;
493
494                 list(, $alias, , $name, $this->param) = $this->splice($arr);
495
496                 $matches = array();
497                 if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $this->param, $matches))
498                         list(, $this->param, $this->anchor) = $matches;
499
500                 $url = get_interwiki_url($name, $this->param);
501                 $this->url = ($url === FALSE) ?
502                         $script . '?' . rawurlencode('[[' . $name . ':' . $this->param . ']]') :
503                         htmlspecialchars($url);
504
505                 return parent::setParam(
506                         $page,
507                         htmlspecialchars($name . ':' . $this->param),
508                         '',
509                         'InterWikiName',
510                         $alias == '' ? $name . ':' . $this->param : $alias
511                 );
512         }
513
514         function toString()
515         {
516                 return '<a href="' . $this->url . $this->anchor . '" title="' .
517                         $this->name . '" rel="nofollow">' . $this->alias . '</a>';
518         }
519 }
520
521 // BracketNames
522 class Link_bracketname extends Link
523 {
524         var $anchor, $refer;
525
526         function Link_bracketname($start)
527         {
528                 parent::Link($start);
529         }
530
531         function get_pattern()
532         {
533                 global $WikiName, $BracketName;
534
535                 $s2 = $this->start + 2;
536                 return <<<EOD
537 \[\[                     # Open bracket
538 (?:((?:(?!\]\]).)+)>)?   # (1) Alias
539 (\[\[)?                  # (2) Open bracket
540 (                        # (3) PageName
541  (?:$WikiName)
542  |
543  (?:$BracketName)
544 )?
545 (\#(?:[a-zA-Z][\w-]*)?)? # (4) Anchor
546 (?($s2)\]\])             # Close bracket if (2)
547 \]\]                     # Close bracket
548 EOD;
549         }
550
551         function get_count()
552         {
553                 return 4;
554         }
555
556         function set($arr, $page)
557         {
558                 global $WikiName;
559
560                 list(, $alias, , $name, $this->anchor) = $this->splice($arr);
561                 if ($name == '' && $this->anchor == '') return FALSE;
562
563                 if ($name == '' || ! preg_match('/^' . $WikiName . '$/', $name)) {
564                         if ($alias == '') $alias = $name . $this->anchor;
565                         if ($name != '') {
566                                 $name = get_fullname($name, $page);
567                                 if (! is_pagename($name)) return FALSE;
568                         }
569                 }
570
571                 return parent::setParam($page, $name, '', 'pagename', $alias);
572         }
573
574         function toString()
575         {
576                 return make_pagelink(
577                         $this->name,
578                         $this->alias,
579                         $this->anchor,
580                         $this->page
581                 );
582         }
583 }
584
585 // WikiNames
586 class Link_wikiname extends Link
587 {
588         function Link_wikiname($start)
589         {
590                 parent::Link($start);
591         }
592
593         function get_pattern()
594         {
595                 global $WikiName, $nowikiname;
596
597                 return $nowikiname ? FALSE : '(' . $WikiName . ')';
598         }
599
600         function get_count()
601         {
602                 return 1;
603         }
604
605         function set($arr, $page)
606         {
607                 list($name) = $this->splice($arr);
608                 return parent::setParam($page, $name, '', 'pagename', $name);
609         }
610
611         function toString()
612         {
613                 return make_pagelink(
614                         $this->name,
615                         $this->alias,
616                         '',
617                         $this->page
618                 );
619         }
620 }
621
622 // AutoLinks
623 class Link_autolink extends Link
624 {
625         var $forceignorepages = array();
626         var $auto;
627         var $auto_a; // alphabet only
628
629         function Link_autolink($start)
630         {
631                 global $autolink;
632
633                 parent::Link($start);
634
635                 if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
636                         return;
637
638                 @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . 'autolink.dat');
639                 $this->auto   = $auto;
640                 $this->auto_a = $auto_a;
641                 $this->forceignorepages = explode("\t", trim($forceignorepages));
642         }
643
644         function get_pattern()
645         {
646                 return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
647         }
648
649         function get_count()
650         {
651                 return 1;
652         }
653
654         function set($arr, $page)
655         {
656                 global $WikiName;
657
658                 list($name) = $this->splice($arr);
659
660                 // Ignore pages listed, or Expire ones not found
661                 if (in_array($name, $this->forceignorepages) || ! is_page($name))
662                         return FALSE;
663
664                 return parent::setParam($page, $name, '', 'pagename', $name);
665         }
666
667         function toString()
668         {
669                 return make_pagelink($this->name, $this->alias, '', $this->page);
670         }
671 }
672
673 class Link_autolink_a extends Link_autolink
674 {
675         function Link_autolink_a($start)
676         {
677                 parent::Link_autolink($start);
678         }
679
680         function get_pattern()
681         {
682                 return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
683         }
684 }
685
686 // Make hyperlink for the page
687 function make_pagelink($page, $alias = '', $anchor = '', $refer = '')
688 {
689         global $script, $vars, $link_compact, $related, $_symbol_noexists;
690
691         $s_page = htmlspecialchars(strip_bracket($page));
692         $s_alias = ($alias == '') ? $s_page : $alias;
693
694         if ($page == '') return '<a href="' . $anchor . '">' . $s_alias . '</a>';
695
696         $r_page  = rawurlencode($page);
697         $r_refer = ($refer == '') ? '' : '&amp;refer=' . rawurlencode($refer);
698
699         if (! isset($related[$page]) && $page != $vars['page'] && is_page($page))
700                 $related[$page] = get_filetime($page);
701
702         if (is_page($page)) {
703                 // Hyperlinks
704                 $passage = get_pg_passage($page, FALSE);
705                 $title   = $link_compact ? '' : ' title="' . $s_page . $passage . '"';
706                 return '<a href="' . $script . '?' . $r_page . $anchor . '"' . $title . '>' .
707                         $s_alias . '</a>';
708         } else if (PKWK_READONLY) {
709                 // Without hyperlink (= Suppress dangling link)
710                 return $s_alias;
711         } else {
712                 // Dangling links
713                 $retval = $s_alias . '<a href="' .
714                         $script . '?cmd=edit&amp;page=' . $r_page . $r_refer . '">' .
715                         $_symbol_noexists . '</a>';
716                 if (! $link_compact)
717                         $retval = '<span class="noexists">' . $retval . '</span>';
718                 return $retval;
719         }
720 }
721
722 // Resolve relative / (Unix-like)absolute path of the page
723 function get_fullname($name, $refer)
724 {
725         global $defaultpage;
726
727         // 'Here'
728         if ($name == '' || $name == './') return $refer;
729
730         // Absolute path
731         if ($name{0} == '/') {
732                 $name = substr($name, 1);
733                 return ($name == '') ? $defaultpage : $name;
734         }
735
736         // Relative path from 'Here'
737         if (substr($name, 0, 2) == './') {
738                 $arrn    = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
739                 $arrn[0] = $refer;
740                 return join('/', $arrn);
741         }
742
743         // Relative path from dirname()
744         if (substr($name, 0, 3) == '../') {
745                 $arrn = preg_split('#/#', $name,  -1, PREG_SPLIT_NO_EMPTY);
746                 $arrp = preg_split('#/#', $refer, -1, PREG_SPLIT_NO_EMPTY);
747
748                 while (! empty($arrn) && $arrn[0] == '..') {
749                         array_shift($arrn);
750                         array_pop($arrp);
751                 }
752                 $name = ! empty($arrp) ? join('/', array_merge($arrp, $arrn)) :
753                         (! empty($arrn) ? $defaultpage . '/' . join('/', $arrn) : $defaultpage);
754         }
755
756         return $name;
757 }
758
759 // Render an InterWiki into a URL
760 function get_interwiki_url($name, $param)
761 {
762         global $WikiName, $interwiki;
763         static $interwikinames;
764         static $encode_aliases = array('sjis'=>'SJIS', 'euc'=>'EUC-JP', 'utf8'=>'UTF-8');
765
766         if (! isset($interwikinames)) {
767                 $interwikinames = $matches = array();
768                 foreach (get_source($interwiki) as $line)
769                         if (preg_match('/\[(' . '(?:(?:https?|ftp|news):\/\/|\.\.?\/)' .
770                             '[!~*\'();\/?:\@&=+\$,%#\w.-]*)\s([^\]]+)\]\s?([^\s]*)/',
771                             $line, $matches))
772                                 $interwikinames[$matches[2]] = array($matches[1], $matches[3]);
773         }
774
775         if (! isset($interwikinames[$name])) return FALSE;
776
777         list($url, $opt) = $interwikinames[$name];
778
779         // Encoding
780         switch ($opt) {
781
782         case '':
783         case 'std': // As-Is (Internal encoding of this PukiWiki will be used)
784                 $param = rawurlencode($param);
785                 break;
786
787         case 'asis': // As-Is
788         case 'raw':
789                 // $param = htmlspecialchars($param);
790                 break;
791
792         case 'yw': // YukiWiki
793                 if (! preg_match('/' . $WikiName . '/', $param))
794                         $param = '[[' . mb_convert_encoding($param, 'SJIS', SOURCE_ENCODING) . ']]';
795                 // $param = htmlspecialchars($param);
796                 break;
797
798         case 'moin': // MoinMoin
799                 $param = str_replace('%', '_', rawurlencode($param));
800                 break;
801
802         default:
803                 // Alias conversion
804                 if (isset($encode_aliases[$opt])) $opt = $encode_aliases[$opt];
805                 // Encoding conversion into specified encode, and URLencode
806                 $param = rawurlencode(mb_convert_encoding($param, $opt, 'auto'));
807         }
808
809         // Replace parameters
810         if (strpos($url, '$1') !== FALSE) {
811                 $url = str_replace('$1', $param, $url);
812         } else {
813                 $url .= $param;
814         }
815
816         $len = strlen($url);
817         if ($len > 512) die_message('InterWiki URL too long: ' . $len . ' characters');
818
819         return $url;
820 }
821 ?>