/
xhtml.php
2064 lines (1851 loc) · 61.4 KB
/
xhtml.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use dokuwiki\ChangeLog\MediaChangeLog;
use dokuwiki\File\MediaResolver;
use dokuwiki\File\PageResolver;
use dokuwiki\Utf8\PhpString;
use SimplePie\Author;
/**
* Renderer for XHTML output
*
* This is DokuWiki's main renderer used to display page content in the wiki
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @author Andreas Gohr <andi@splitbrain.org>
*
*/
class Doku_Renderer_xhtml extends Doku_Renderer
{
/** @var array store the table of contents */
public $toc = [];
/** @var array A stack of section edit data */
protected $sectionedits = [];
/** @var int last section edit id, used by startSectionEdit */
protected $lastsecid = 0;
/** @var array a list of footnotes, list starts at 1! */
protected $footnotes = [];
/** @var int current section level */
protected $lastlevel = 0;
/** @var array section node tracker */
protected $node = [0, 0, 0, 0, 0];
/** @var string temporary $doc store */
protected $store = '';
/** @var array global counter, for table classes etc. */
protected $_counter = []; //
/** @var int counts the code and file blocks, used to provide download links */
protected $_codeblock = 0;
/** @var array list of allowed URL schemes */
protected $schemes;
/**
* Register a new edit section range
*
* @param int $start The byte position for the edit start
* @param array $data Associative array with section data:
* Key 'name': the section name/title
* Key 'target': the target for the section edit,
* e.g. 'section' or 'table'
* Key 'hid': header id
* Key 'codeblockOffset': actual code block index
* Key 'start': set in startSectionEdit(),
* do not set yourself
* Key 'range': calculated from 'start' and
* $key in finishSectionEdit(),
* do not set yourself
* @return string A marker class for the starting HTML element
*
* @author Adrian Lang <lang@cosmocode.de>
*/
public function startSectionEdit($start, $data)
{
if (!is_array($data)) {
msg(
sprintf(
'startSectionEdit: $data "%s" is NOT an array! One of your plugins needs an update.',
hsc((string)$data)
),
-1
);
// @deprecated 2018-04-14, backward compatibility
$args = func_get_args();
$data = [];
if (isset($args[1])) $data['target'] = $args[1];
if (isset($args[2])) $data['name'] = $args[2];
if (isset($args[3])) $data['hid'] = $args[3];
}
$data['secid'] = ++$this->lastsecid;
$data['start'] = $start;
$this->sectionedits[] = $data;
return 'sectionedit' . $data['secid'];
}
/**
* Finish an edit section range
*
* @param int $end The byte position for the edit end; null for the rest of the page
*
* @author Adrian Lang <lang@cosmocode.de>
*/
public function finishSectionEdit($end = null, $hid = null)
{
if (count($this->sectionedits) == 0) {
return;
}
$data = array_pop($this->sectionedits);
if (!is_null($end) && $end <= $data['start']) {
return;
}
if (!is_null($hid)) {
$data['hid'] .= $hid;
}
$data['range'] = $data['start'] . '-' . (is_null($end) ? '' : $end);
unset($data['start']);
$this->doc .= '<!-- EDIT' . hsc(json_encode($data, JSON_THROW_ON_ERROR)) . ' -->';
}
/**
* Returns the format produced by this renderer.
*
* @return string always 'xhtml'
*/
public function getFormat()
{
return 'xhtml';
}
/**
* Initialize the document
*/
public function document_start()
{
//reset some internals
$this->toc = [];
}
/**
* Finalize the document
*/
public function document_end()
{
// Finish open section edits.
while ($this->sectionedits !== []) {
if ($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) {
// If there is only one section, do not write a section edit
// marker.
array_pop($this->sectionedits);
} else {
$this->finishSectionEdit();
}
}
if ($this->footnotes !== []) {
$this->doc .= '<div class="footnotes">' . DOKU_LF;
foreach ($this->footnotes as $id => $footnote) {
// check its not a placeholder that indicates actual footnote text is elsewhere
if (!str_starts_with($footnote, "@@FNT")) {
// open the footnote and set the anchor and backlink
$this->doc .= '<div class="fn">';
$this->doc .= '<sup><a href="#fnt__' . $id . '" id="fn__' . $id . '" class="fn_bot">';
$this->doc .= $id . ')</a></sup> ' . DOKU_LF;
// get any other footnotes that use the same markup
$alt = array_keys($this->footnotes, "@@FNT$id");
foreach ($alt as $ref) {
// set anchor and backlink for the other footnotes
$this->doc .= ', <sup><a href="#fnt__' . ($ref) . '" id="fn__' . ($ref) . '" class="fn_bot">';
$this->doc .= ($ref) . ')</a></sup> ' . DOKU_LF;
}
// add footnote markup and close this footnote
$this->doc .= '<div class="content">' . $footnote . '</div>';
$this->doc .= '</div>' . DOKU_LF;
}
}
$this->doc .= '</div>' . DOKU_LF;
}
// Prepare the TOC
global $conf;
if (
$this->info['toc'] &&
is_array($this->toc) &&
$conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']
) {
global $TOC;
$TOC = $this->toc;
}
// make sure there are no empty paragraphs
$this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
}
/**
* Add an item to the TOC
*
* @param string $id the hash link
* @param string $text the text to display
* @param int $level the nesting level
*/
public function toc_additem($id, $text, $level)
{
global $conf;
//handle TOC
if ($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
$this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
}
}
/**
* Render a heading
*
* @param string $text the text to display
* @param int $level header level
* @param int $pos byte position in the original source
* @param bool $returnonly whether to return html or write to doc attribute
* @return void|string writes to doc attribute or returns html depends on $returnonly
*/
public function header($text, $level, $pos, $returnonly = false)
{
global $conf;
if (blank($text)) return; //skip empty headlines
$hid = $this->_headerToLink($text, true);
//only add items within configured levels
$this->toc_additem($hid, $text, $level);
// adjust $node to reflect hierarchy of levels
$this->node[$level - 1]++;
if ($level < $this->lastlevel) {
for ($i = 0; $i < $this->lastlevel - $level; $i++) {
$this->node[$this->lastlevel - $i - 1] = 0;
}
}
$this->lastlevel = $level;
if (
$level <= $conf['maxseclevel'] &&
$this->sectionedits !== [] &&
$this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section'
) {
$this->finishSectionEdit($pos - 1);
}
// build the header
$header = DOKU_LF . '<h' . $level;
if ($level <= $conf['maxseclevel']) {
$data = [];
$data['target'] = 'section';
$data['name'] = $text;
$data['hid'] = $hid;
$data['codeblockOffset'] = $this->_codeblock;
$header .= ' class="' . $this->startSectionEdit($pos, $data) . '"';
}
$header .= ' id="' . $hid . '">';
$header .= $this->_xmlEntities($text);
$header .= "</h$level>" . DOKU_LF;
if ($returnonly) {
return $header;
} else {
$this->doc .= $header;
}
}
/**
* Open a new section
*
* @param int $level section level (as determined by the previous header)
*/
public function section_open($level)
{
$this->doc .= '<div class="level' . $level . '">' . DOKU_LF;
}
/**
* Close the current section
*/
public function section_close()
{
$this->doc .= DOKU_LF . '</div>' . DOKU_LF;
}
/**
* Render plain text data
*
* @param $text
*/
public function cdata($text)
{
$this->doc .= $this->_xmlEntities($text);
}
/**
* Open a paragraph
*/
public function p_open()
{
$this->doc .= DOKU_LF . '<p>' . DOKU_LF;
}
/**
* Close a paragraph
*/
public function p_close()
{
$this->doc .= DOKU_LF . '</p>' . DOKU_LF;
}
/**
* Create a line break
*/
public function linebreak()
{
$this->doc .= '<br/>' . DOKU_LF;
}
/**
* Create a horizontal line
*/
public function hr()
{
$this->doc .= '<hr />' . DOKU_LF;
}
/**
* Start strong (bold) formatting
*/
public function strong_open()
{
$this->doc .= '<strong>';
}
/**
* Stop strong (bold) formatting
*/
public function strong_close()
{
$this->doc .= '</strong>';
}
/**
* Start emphasis (italics) formatting
*/
public function emphasis_open()
{
$this->doc .= '<em>';
}
/**
* Stop emphasis (italics) formatting
*/
public function emphasis_close()
{
$this->doc .= '</em>';
}
/**
* Start underline formatting
*/
public function underline_open()
{
$this->doc .= '<em class="u">';
}
/**
* Stop underline formatting
*/
public function underline_close()
{
$this->doc .= '</em>';
}
/**
* Start monospace formatting
*/
public function monospace_open()
{
$this->doc .= '<code>';
}
/**
* Stop monospace formatting
*/
public function monospace_close()
{
$this->doc .= '</code>';
}
/**
* Start a subscript
*/
public function subscript_open()
{
$this->doc .= '<sub>';
}
/**
* Stop a subscript
*/
public function subscript_close()
{
$this->doc .= '</sub>';
}
/**
* Start a superscript
*/
public function superscript_open()
{
$this->doc .= '<sup>';
}
/**
* Stop a superscript
*/
public function superscript_close()
{
$this->doc .= '</sup>';
}
/**
* Start deleted (strike-through) formatting
*/
public function deleted_open()
{
$this->doc .= '<del>';
}
/**
* Stop deleted (strike-through) formatting
*/
public function deleted_close()
{
$this->doc .= '</del>';
}
/**
* Callback for footnote start syntax
*
* All following content will go to the footnote instead of
* the document. To achieve this the previous rendered content
* is moved to $store and $doc is cleared
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
public function footnote_open()
{
// move current content to store and record footnote
$this->store = $this->doc;
$this->doc = '';
}
/**
* Callback for footnote end syntax
*
* All rendered content is moved to the $footnotes array and the old
* content is restored from $store again
*
* @author Andreas Gohr
*/
public function footnote_close()
{
/** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
static $fnid = 0;
// assign new footnote id (we start at 1)
$fnid++;
// recover footnote into the stack and restore old content
$footnote = $this->doc;
$this->doc = $this->store;
$this->store = '';
// check to see if this footnote has been seen before
$i = array_search($footnote, $this->footnotes);
if ($i === false) {
// its a new footnote, add it to the $footnotes array
$this->footnotes[$fnid] = $footnote;
} else {
// seen this one before, save a placeholder
$this->footnotes[$fnid] = "@@FNT" . ($i);
}
// output the footnote reference and link
$this->doc .= sprintf(
'<sup><a href="#fn__%d" id="fnt__%d" class="fn_top">%d)</a></sup>',
$fnid,
$fnid,
$fnid
);
}
/**
* Open an unordered list
*
* @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
*/
public function listu_open($classes = null)
{
$class = '';
if ($classes !== null) {
if (is_array($classes)) $classes = implode(' ', $classes);
$class = " class=\"$classes\"";
}
$this->doc .= "<ul$class>" . DOKU_LF;
}
/**
* Close an unordered list
*/
public function listu_close()
{
$this->doc .= '</ul>' . DOKU_LF;
}
/**
* Open an ordered list
*
* @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
*/
public function listo_open($classes = null)
{
$class = '';
if ($classes !== null) {
if (is_array($classes)) $classes = implode(' ', $classes);
$class = " class=\"$classes\"";
}
$this->doc .= "<ol$class>" . DOKU_LF;
}
/**
* Close an ordered list
*/
public function listo_close()
{
$this->doc .= '</ol>' . DOKU_LF;
}
/**
* Open a list item
*
* @param int $level the nesting level
* @param bool $node true when a node; false when a leaf
*/
public function listitem_open($level, $node = false)
{
$branching = $node ? ' node' : '';
$this->doc .= '<li class="level' . $level . $branching . '">';
}
/**
* Close a list item
*/
public function listitem_close()
{
$this->doc .= '</li>' . DOKU_LF;
}
/**
* Start the content of a list item
*/
public function listcontent_open()
{
$this->doc .= '<div class="li">';
}
/**
* Stop the content of a list item
*/
public function listcontent_close()
{
$this->doc .= '</div>' . DOKU_LF;
}
/**
* Output unformatted $text
*
* Defaults to $this->cdata()
*
* @param string $text
*/
public function unformatted($text)
{
$this->doc .= $this->_xmlEntities($text);
}
/**
* Start a block quote
*/
public function quote_open()
{
$this->doc .= '<blockquote><div class="no">' . DOKU_LF;
}
/**
* Stop a block quote
*/
public function quote_close()
{
$this->doc .= '</div></blockquote>' . DOKU_LF;
}
/**
* Output preformatted text
*
* @param string $text
*/
public function preformatted($text)
{
$this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text), "\n\r") . '</pre>' . DOKU_LF;
}
/**
* Display text as file content, optionally syntax highlighted
*
* @param string $text text to show
* @param string $language programming language to use for syntax highlighting
* @param string $filename file path label
* @param array $options assoziative array with additional geshi options
*/
public function file($text, $language = null, $filename = null, $options = null)
{
$this->_highlight('file', $text, $language, $filename, $options);
}
/**
* Display text as code content, optionally syntax highlighted
*
* @param string $text text to show
* @param string $language programming language to use for syntax highlighting
* @param string $filename file path label
* @param array $options assoziative array with additional geshi options
*/
public function code($text, $language = null, $filename = null, $options = null)
{
$this->_highlight('code', $text, $language, $filename, $options);
}
/**
* Use GeSHi to highlight language syntax in code and file blocks
*
* @param string $type code|file
* @param string $text text to show
* @param string $language programming language to use for syntax highlighting
* @param string $filename file path label
* @param array $options assoziative array with additional geshi options
* @author Andreas Gohr <andi@splitbrain.org>
*/
public function _highlight($type, $text, $language = null, $filename = null, $options = null)
{
global $ID;
global $lang;
global $INPUT;
$language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language ?? '');
if ($filename) {
// add icon
[$ext] = mimetype($filename, false);
$class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
$class = 'mediafile mf_' . $class;
$offset = 0;
if ($INPUT->has('codeblockOffset')) {
$offset = $INPUT->str('codeblockOffset');
}
$this->doc .= '<dl class="' . $type . '">' . DOKU_LF;
$this->doc .= '<dt><a href="' .
exportlink(
$ID,
'code',
['codeblock' => $offset + $this->_codeblock]
) . '" title="' . $lang['download'] . '" class="' . $class . '">';
$this->doc .= hsc($filename);
$this->doc .= '</a></dt>' . DOKU_LF . '<dd>';
}
if (str_starts_with($text, "\n")) {
$text = substr($text, 1);
}
if (str_ends_with($text, "\n")) {
$text = substr($text, 0, -1);
}
if (empty($language)) { // empty is faster than is_null and can prevent '' string
$this->doc .= '<pre class="' . $type . '">' . $this->_xmlEntities($text) . '</pre>' . DOKU_LF;
} else {
$class = 'code'; //we always need the code class to make the syntax highlighting apply
if ($type != 'code') $class .= ' ' . $type;
$this->doc .= "<pre class=\"$class $language\">" .
p_xhtml_cached_geshi($text, $language, '', $options) .
'</pre>' . DOKU_LF;
}
if ($filename) {
$this->doc .= '</dd></dl>' . DOKU_LF;
}
$this->_codeblock++;
}
/**
* Format an acronym
*
* Uses $this->acronyms
*
* @param string $acronym
*/
public function acronym($acronym)
{
if (array_key_exists($acronym, $this->acronyms)) {
$title = $this->_xmlEntities($this->acronyms[$acronym]);
$this->doc .= '<abbr title="' . $title
. '">' . $this->_xmlEntities($acronym) . '</abbr>';
} else {
$this->doc .= $this->_xmlEntities($acronym);
}
}
/**
* Format a smiley
*
* Uses $this->smiley
*
* @param string $smiley
*/
public function smiley($smiley)
{
if (isset($this->smileys[$smiley])) {
$this->doc .= '<img src="' . DOKU_BASE . 'lib/images/smileys/' . $this->smileys[$smiley] .
'" class="icon smiley" alt="' . $this->_xmlEntities($smiley) . '" />';
} else {
$this->doc .= $this->_xmlEntities($smiley);
}
}
/**
* Format an entity
*
* Entities are basically small text replacements
*
* Uses $this->entities
*
* @param string $entity
*/
public function entity($entity)
{
if (array_key_exists($entity, $this->entities)) {
$this->doc .= $this->entities[$entity];
} else {
$this->doc .= $this->_xmlEntities($entity);
}
}
/**
* Typographically format a multiply sign
*
* Example: ($x=640, $y=480) should result in "640×480"
*
* @param string|int $x first value
* @param string|int $y second value
*/
public function multiplyentity($x, $y)
{
$this->doc .= "$x×$y";
}
/**
* Render an opening single quote char (language specific)
*/
public function singlequoteopening()
{
global $lang;
$this->doc .= $lang['singlequoteopening'];
}
/**
* Render a closing single quote char (language specific)
*/
public function singlequoteclosing()
{
global $lang;
$this->doc .= $lang['singlequoteclosing'];
}
/**
* Render an apostrophe char (language specific)
*/
public function apostrophe()
{
global $lang;
$this->doc .= $lang['apostrophe'];
}
/**
* Render an opening double quote char (language specific)
*/
public function doublequoteopening()
{
global $lang;
$this->doc .= $lang['doublequoteopening'];
}
/**
* Render an closinging double quote char (language specific)
*/
public function doublequoteclosing()
{
global $lang;
$this->doc .= $lang['doublequoteclosing'];
}
/**
* Render a CamelCase link
*
* @param string $link The link name
* @param bool $returnonly whether to return html or write to doc attribute
* @return void|string writes to doc attribute or returns html depends on $returnonly
*
* @see http://en.wikipedia.org/wiki/CamelCase
*/
public function camelcaselink($link, $returnonly = false)
{
if ($returnonly) {
return $this->internallink($link, $link, null, true);
} else {
$this->internallink($link, $link);
}
}
/**
* Render a page local link
*
* @param string $hash hash link identifier
* @param string $name name for the link
* @param bool $returnonly whether to return html or write to doc attribute
* @return void|string writes to doc attribute or returns html depends on $returnonly
*/
public function locallink($hash, $name = null, $returnonly = false)
{
global $ID;
$name = $this->_getLinkTitle($name, $hash, $isImage);
$hash = $this->_headerToLink($hash);
$title = $ID . ' ↵';
$doc = '<a href="#' . $hash . '" title="' . $title . '" class="wikilink1">';
$doc .= $name;
$doc .= '</a>';
if ($returnonly) {
return $doc;
} else {
$this->doc .= $doc;
}
}
/**
* Render an internal Wiki Link
*
* $search,$returnonly & $linktype are not for the renderer but are used
* elsewhere - no need to implement them in other renderers
*
* @param string $id pageid
* @param string|null $name link name
* @param string|null $search adds search url param
* @param bool $returnonly whether to return html or write to doc attribute
* @param string $linktype type to set use of headings
* @return void|string writes to doc attribute or returns html depends on $returnonly
* @author Andreas Gohr <andi@splitbrain.org>
*/
public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content')
{
global $conf;
global $ID;
global $INFO;
$params = '';
$parts = explode('?', $id, 2);
if (count($parts) === 2) {
$id = $parts[0];
$params = $parts[1];
}
// For empty $id we need to know the current $ID
// We need this check because _simpleTitle needs
// correct $id and resolve_pageid() use cleanID($id)
// (some things could be lost)
if ($id === '') {
$id = $ID;
}
// default name is based on $id as given
$default = $this->_simpleTitle($id);
// now first resolve and clean up the $id
$id = (new PageResolver($ID))->resolveId($id, $this->date_at, true);
$exists = page_exists($id, $this->date_at, false, true);
$link = [];
$name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
if (!$isImage) {
if ($exists) {
$class = 'wikilink1';
} else {
$class = 'wikilink2';
$link['rel'] = 'nofollow';
}
} else {
$class = 'media';
}
//keep hash anchor
[$id, $hash] = sexplode('#', $id, 2);
if (!empty($hash)) $hash = $this->_headerToLink($hash);
//prepare for formating
$link['target'] = $conf['target']['wiki'];
$link['style'] = '';
$link['pre'] = '';
$link['suf'] = '';
$link['more'] = 'data-wiki-id="' . $id . '"'; // id is already cleaned
$link['class'] = $class;
if ($this->date_at) {
$params = $params . '&at=' . rawurlencode($this->date_at);
}
$link['url'] = wl($id, $params);
$link['name'] = $name;
$link['title'] = $id;
//add search string
if ($search) {
($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&';
if (is_array($search)) {
$search = array_map('rawurlencode', $search);
$link['url'] .= 's[]=' . implode('&s[]=', $search);
} else {
$link['url'] .= 's=' . rawurlencode($search);
}
}
//keep hash
if ($hash) $link['url'] .= '#' . $hash;
//output formatted
if ($returnonly) {
return $this->_formatLink($link);
} else {
$this->doc .= $this->_formatLink($link);
}
}
/**
* Render an external link
*
* @param string $url full URL with scheme
* @param string|array $name name for the link, array for media file
* @param bool $returnonly whether to return html or write to doc attribute
* @return void|string writes to doc attribute or returns html depends on $returnonly
*/
public function externallink($url, $name = null, $returnonly = false)
{
global $conf;
$name = $this->_getLinkTitle($name, $url, $isImage);
// url might be an attack vector, only allow registered protocols
if (is_null($this->schemes)) $this->schemes = getSchemes();
[$scheme] = explode('://', $url);
$scheme = strtolower($scheme);
if (!in_array($scheme, $this->schemes)) $url = '';
// is there still an URL?
if (!$url) {
if ($returnonly) {
return $name;
} else {
$this->doc .= $name;
}
return;
}
// set class
if (!$isImage) {
$class = 'urlextern';
} else {
$class = 'media';
}
//prepare for formating
$link = [];
$link['target'] = $conf['target']['extern'];
$link['style'] = '';
$link['pre'] = '';