1<?php 2 3use dokuwiki\ChangeLog\MediaChangeLog; 4use dokuwiki\Feed\FeedParser; 5use dokuwiki\File\MediaResolver; 6use dokuwiki\File\PageResolver; 7use dokuwiki\Utf8\PhpString; 8use SimplePie\Author; 9 10/** 11 * Renderer for XHTML output 12 * 13 * This is DokuWiki's main renderer used to display page content in the wiki 14 * 15 * @author Harry Fuecks <hfuecks@gmail.com> 16 * @author Andreas Gohr <andi@splitbrain.org> 17 * 18 */ 19class Doku_Renderer_xhtml extends Doku_Renderer 20{ 21 /** @var array store the table of contents */ 22 public $toc = []; 23 24 /** @var array A stack of section edit data */ 25 protected $sectionedits = []; 26 27 /** @var int last section edit id, used by startSectionEdit */ 28 protected $lastsecid = 0; 29 30 /** @var array a list of footnotes, list starts at 1! */ 31 protected $footnotes = []; 32 33 /** @var int current section level */ 34 protected $lastlevel = 0; 35 /** @var array section node tracker */ 36 protected $node = [0, 0, 0, 0, 0, 0]; 37 38 /** @var string temporary $doc store */ 39 protected $store = ''; 40 41 /** @var array global counter, for table classes etc. */ 42 protected $_counter = []; // 43 44 /** @var int counts the code and file blocks, used to provide download links */ 45 protected $_codeblock = 0; 46 47 /** @var array list of allowed URL schemes */ 48 protected $schemes; 49 50 /** 51 * Register a new edit section range 52 * 53 * @param int $start The byte position for the edit start 54 * @param array $data Associative array with section data: 55 * Key 'name': the section name/title 56 * Key 'target': the target for the section edit, 57 * e.g. 'section' or 'table' 58 * Key 'hid': header id 59 * Key 'codeblockOffset': actual code block index 60 * Key 'start': set in startSectionEdit(), 61 * do not set yourself 62 * Key 'range': calculated from 'start' and 63 * $key in finishSectionEdit(), 64 * do not set yourself 65 * @return string A marker class for the starting HTML element 66 * 67 * @author Adrian Lang <lang@cosmocode.de> 68 */ 69 public function startSectionEdit($start, $data) 70 { 71 if (!is_array($data)) { 72 msg( 73 sprintf( 74 'startSectionEdit: $data "%s" is NOT an array! One of your plugins needs an update.', 75 hsc((string)$data) 76 ), 77 -1 78 ); 79 80 // @deprecated 2018-04-14, backward compatibility 81 $args = func_get_args(); 82 $data = []; 83 if (isset($args[1])) $data['target'] = $args[1]; 84 if (isset($args[2])) $data['name'] = $args[2]; 85 if (isset($args[3])) $data['hid'] = $args[3]; 86 } 87 $data['secid'] = ++$this->lastsecid; 88 $data['start'] = $start; 89 $this->sectionedits[] = $data; 90 return 'sectionedit' . $data['secid']; 91 } 92 93 /** 94 * Finish an edit section range 95 * 96 * @param int $end The byte position for the edit end; null for the rest of the page 97 * 98 * @author Adrian Lang <lang@cosmocode.de> 99 */ 100 public function finishSectionEdit($end = null, $hid = null) 101 { 102 if (count($this->sectionedits) == 0) { 103 return; 104 } 105 $data = array_pop($this->sectionedits); 106 if (!is_null($end) && $end <= $data['start']) { 107 return; 108 } 109 if (!is_null($hid)) { 110 $data['hid'] .= $hid; 111 } 112 $data['range'] = $data['start'] . '-' . (is_null($end) ? '' : $end); 113 unset($data['start']); 114 $this->doc .= '<!-- EDIT' . hsc(json_encode($data, JSON_THROW_ON_ERROR)) . ' -->'; 115 } 116 117 /** 118 * Returns the format produced by this renderer. 119 * 120 * @return string always 'xhtml' 121 */ 122 public function getFormat() 123 { 124 return 'xhtml'; 125 } 126 127 /** 128 * Initialize the document 129 */ 130 public function document_start() 131 { 132 //reset some internals 133 $this->toc = []; 134 } 135 136 /** 137 * Finalize the document 138 */ 139 public function document_end() 140 { 141 // Finish open section edits. 142 while ($this->sectionedits !== []) { 143 if ($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) { 144 // If there is only one section, do not write a section edit 145 // marker. 146 array_pop($this->sectionedits); 147 } else { 148 $this->finishSectionEdit(); 149 } 150 } 151 152 if ($this->footnotes !== []) { 153 $this->doc .= '<div class="footnotes">' . DOKU_LF; 154 155 foreach ($this->footnotes as $id => $footnote) { 156 // check its not a placeholder that indicates actual footnote text is elsewhere 157 if (!str_starts_with($footnote, "@@FNT")) { 158 // open the footnote and set the anchor and backlink 159 $this->doc .= '<div class="fn">'; 160 $this->doc .= '<sup><a href="#fnt__' . $id . '" id="fn__' . $id . '" class="fn_bot">'; 161 $this->doc .= $id . ')</a></sup> ' . DOKU_LF; 162 163 // get any other footnotes that use the same markup 164 $alt = array_keys($this->footnotes, "@@FNT$id"); 165 166 foreach ($alt as $ref) { 167 // set anchor and backlink for the other footnotes 168 $this->doc .= ', <sup><a href="#fnt__' . ($ref) . '" id="fn__' . ($ref) . '" class="fn_bot">'; 169 $this->doc .= ($ref) . ')</a></sup> ' . DOKU_LF; 170 } 171 172 // add footnote markup and close this footnote 173 $this->doc .= '<div class="content">' . $footnote . '</div>'; 174 $this->doc .= '</div>' . DOKU_LF; 175 } 176 } 177 $this->doc .= '</div>' . DOKU_LF; 178 } 179 180 // Prepare the TOC 181 global $conf; 182 if ( 183 $this->info['toc'] && 184 is_array($this->toc) && 185 $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads'] 186 ) { 187 global $TOC; 188 $TOC = $this->toc; 189 } 190 191 // make sure there are no empty paragraphs 192 $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc); 193 } 194 195 /** 196 * Add an item to the TOC 197 * 198 * @param string $id the hash link 199 * @param string $text the text to display 200 * @param int $level the nesting level 201 */ 202 public function toc_additem($id, $text, $level) 203 { 204 global $conf; 205 206 //handle TOC 207 if ($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) { 208 $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1); 209 } 210 } 211 212 /** 213 * Render a heading 214 * 215 * @param string $text the text to display 216 * @param int $level header level 217 * @param int $pos byte position in the original source 218 * @param bool $returnonly whether to return html or write to doc attribute 219 * @return void|string writes to doc attribute or returns html depends on $returnonly 220 */ 221 public function header($text, $level, $pos, $returnonly = false) 222 { 223 global $conf; 224 225 if (blank($text)) return; //skip empty headlines 226 227 $hid = $this->_headerToLink($text, true); 228 229 //only add items within configured levels 230 $this->toc_additem($hid, $text, $level); 231 232 // adjust $node to reflect hierarchy of levels 233 $this->node[$level - 1]++; 234 if ($level < $this->lastlevel) { 235 for ($i = 0; $i < $this->lastlevel - $level; $i++) { 236 $this->node[$this->lastlevel - $i - 1] = 0; 237 } 238 } 239 $this->lastlevel = $level; 240 241 if ( 242 $level <= $conf['maxseclevel'] && 243 $this->sectionedits !== [] && 244 $this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section' 245 ) { 246 $this->finishSectionEdit($pos - 1); 247 } 248 249 // build the header 250 $header = DOKU_LF . '<h' . $level; 251 if ($level <= $conf['maxseclevel']) { 252 $data = []; 253 $data['target'] = 'section'; 254 $data['name'] = $text; 255 $data['hid'] = $hid; 256 $data['codeblockOffset'] = $this->_codeblock; 257 $header .= ' class="' . $this->startSectionEdit($pos, $data) . '"'; 258 } 259 $header .= ' id="' . $hid . '">'; 260 $header .= $this->_xmlEntities($text); 261 $header .= "</h$level>" . DOKU_LF; 262 263 if ($returnonly) { 264 return $header; 265 } else { 266 $this->doc .= $header; 267 } 268 } 269 270 /** 271 * Open a new section 272 * 273 * @param int $level section level (as determined by the previous header) 274 */ 275 public function section_open($level) 276 { 277 $this->doc .= '<div class="level' . $level . '">' . DOKU_LF; 278 } 279 280 /** 281 * Close the current section 282 */ 283 public function section_close() 284 { 285 $this->doc .= DOKU_LF . '</div>' . DOKU_LF; 286 } 287 288 /** 289 * Render plain text data 290 * 291 * @param $text 292 */ 293 public function cdata($text) 294 { 295 $this->doc .= $this->_xmlEntities($text); 296 } 297 298 /** 299 * Open a paragraph 300 */ 301 public function p_open() 302 { 303 $this->doc .= DOKU_LF . '<p>' . DOKU_LF; 304 } 305 306 /** 307 * Close a paragraph 308 */ 309 public function p_close() 310 { 311 $this->doc .= DOKU_LF . '</p>' . DOKU_LF; 312 } 313 314 /** 315 * Create a line break 316 */ 317 public function linebreak() 318 { 319 $this->doc .= '<br/>' . DOKU_LF; 320 } 321 322 /** 323 * Create a horizontal line 324 */ 325 public function hr() 326 { 327 $this->doc .= '<hr />' . DOKU_LF; 328 } 329 330 /** 331 * Start strong (bold) formatting 332 */ 333 public function strong_open() 334 { 335 $this->doc .= '<strong>'; 336 } 337 338 /** 339 * Stop strong (bold) formatting 340 */ 341 public function strong_close() 342 { 343 $this->doc .= '</strong>'; 344 } 345 346 /** 347 * Start emphasis (italics) formatting 348 */ 349 public function emphasis_open() 350 { 351 $this->doc .= '<em>'; 352 } 353 354 /** 355 * Stop emphasis (italics) formatting 356 */ 357 public function emphasis_close() 358 { 359 $this->doc .= '</em>'; 360 } 361 362 /** 363 * Start underline formatting 364 */ 365 public function underline_open() 366 { 367 $this->doc .= '<em class="u">'; 368 } 369 370 /** 371 * Stop underline formatting 372 */ 373 public function underline_close() 374 { 375 $this->doc .= '</em>'; 376 } 377 378 /** 379 * Start monospace formatting 380 */ 381 public function monospace_open() 382 { 383 $this->doc .= '<code>'; 384 } 385 386 /** 387 * Stop monospace formatting 388 */ 389 public function monospace_close() 390 { 391 $this->doc .= '</code>'; 392 } 393 394 /** 395 * Start a subscript 396 */ 397 public function subscript_open() 398 { 399 $this->doc .= '<sub>'; 400 } 401 402 /** 403 * Stop a subscript 404 */ 405 public function subscript_close() 406 { 407 $this->doc .= '</sub>'; 408 } 409 410 /** 411 * Start a superscript 412 */ 413 public function superscript_open() 414 { 415 $this->doc .= '<sup>'; 416 } 417 418 /** 419 * Stop a superscript 420 */ 421 public function superscript_close() 422 { 423 $this->doc .= '</sup>'; 424 } 425 426 /** 427 * Start deleted (strike-through) formatting 428 */ 429 public function deleted_open() 430 { 431 $this->doc .= '<del>'; 432 } 433 434 /** 435 * Stop deleted (strike-through) formatting 436 */ 437 public function deleted_close() 438 { 439 $this->doc .= '</del>'; 440 } 441 442 /** 443 * Callback for footnote start syntax 444 * 445 * All following content will go to the footnote instead of 446 * the document. To achieve this the previous rendered content 447 * is moved to $store and $doc is cleared 448 * 449 * @author Andreas Gohr <andi@splitbrain.org> 450 */ 451 public function footnote_open() 452 { 453 454 // move current content to store and record footnote 455 $this->store = $this->doc; 456 $this->doc = ''; 457 } 458 459 /** 460 * Callback for footnote end syntax 461 * 462 * All rendered content is moved to the $footnotes array and the old 463 * content is restored from $store again 464 * 465 * @author Andreas Gohr 466 */ 467 public function footnote_close() 468 { 469 /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */ 470 static $fnid = 0; 471 // assign new footnote id (we start at 1) 472 $fnid++; 473 474 // recover footnote into the stack and restore old content 475 $footnote = $this->doc; 476 $this->doc = $this->store; 477 $this->store = ''; 478 479 // check to see if this footnote has been seen before 480 $i = array_search($footnote, $this->footnotes); 481 482 if ($i === false) { 483 // its a new footnote, add it to the $footnotes array 484 $this->footnotes[$fnid] = $footnote; 485 } else { 486 // seen this one before, save a placeholder 487 $this->footnotes[$fnid] = "@@FNT" . ($i); 488 } 489 490 // output the footnote reference and link 491 $this->doc .= sprintf( 492 '<sup><a href="#fn__%d" id="fnt__%d" class="fn_top">%d)</a></sup>', 493 $fnid, 494 $fnid, 495 $fnid 496 ); 497 } 498 499 /** 500 * Open an unordered list 501 * 502 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input 503 */ 504 public function listu_open($classes = null) 505 { 506 $class = ''; 507 if ($classes !== null) { 508 if (is_array($classes)) $classes = implode(' ', $classes); 509 $class = " class=\"$classes\""; 510 } 511 $this->doc .= "<ul$class>" . DOKU_LF; 512 } 513 514 /** 515 * Close an unordered list 516 */ 517 public function listu_close() 518 { 519 $this->doc .= '</ul>' . DOKU_LF; 520 } 521 522 /** 523 * Open an ordered list 524 * 525 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input 526 */ 527 public function listo_open($classes = null) 528 { 529 $class = ''; 530 if ($classes !== null) { 531 if (is_array($classes)) $classes = implode(' ', $classes); 532 $class = " class=\"$classes\""; 533 } 534 $this->doc .= "<ol$class>" . DOKU_LF; 535 } 536 537 /** 538 * Open an ordered list with a non-default starting number 539 * 540 * @param int $start Starting number; emitted as a start="N" attribute 541 */ 542 public function listo_open_start($start = 1) 543 { 544 $start = (int)$start; 545 if ($start === 1) { 546 $this->listo_open(); 547 return; 548 } 549 $this->doc .= '<ol start="' . $start . '">' . DOKU_LF; 550 } 551 552 /** 553 * Close an ordered list 554 */ 555 public function listo_close() 556 { 557 $this->doc .= '</ol>' . DOKU_LF; 558 } 559 560 /** 561 * Open a list item 562 * 563 * @param int $level the nesting level 564 * @param bool $node true when a node; false when a leaf 565 */ 566 public function listitem_open($level, $node = false) 567 { 568 $branching = $node ? ' node' : ''; 569 $this->doc .= '<li class="level' . $level . $branching . '">'; 570 } 571 572 /** 573 * Close a list item 574 */ 575 public function listitem_close() 576 { 577 $this->doc .= '</li>' . DOKU_LF; 578 } 579 580 /** 581 * Start the content of a list item 582 */ 583 public function listcontent_open() 584 { 585 $this->doc .= '<div class="li">'; 586 } 587 588 /** 589 * Stop the content of a list item 590 */ 591 public function listcontent_close() 592 { 593 $this->doc .= '</div>' . DOKU_LF; 594 } 595 596 /** 597 * Output unformatted $text 598 * 599 * Defaults to $this->cdata() 600 * 601 * @param string $text 602 */ 603 public function unformatted($text) 604 { 605 $this->doc .= $this->_xmlEntities($text); 606 } 607 608 /** 609 * Start a block quote 610 */ 611 public function quote_open() 612 { 613 $this->doc .= '<blockquote><div class="no">' . DOKU_LF; 614 } 615 616 /** 617 * Stop a block quote 618 */ 619 public function quote_close() 620 { 621 $this->doc .= '</div></blockquote>' . DOKU_LF; 622 } 623 624 /** 625 * Output preformatted text 626 * 627 * @param string $text 628 */ 629 public function preformatted($text) 630 { 631 $this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text), "\n\r") . '</pre>' . DOKU_LF; 632 } 633 634 /** 635 * Display text as file content, optionally syntax highlighted 636 * 637 * @param string $text text to show 638 * @param string $language programming language to use for syntax highlighting 639 * @param string $filename file path label 640 * @param array $options associative array with additional geshi options 641 */ 642 public function file($text, $language = null, $filename = null, $options = null) 643 { 644 $this->_highlight('file', $text, $language, $filename, $options); 645 } 646 647 /** 648 * Display text as code content, optionally syntax highlighted 649 * 650 * @param string $text text to show 651 * @param string $language programming language to use for syntax highlighting 652 * @param string $filename file path label 653 * @param array $options associative array with additional geshi options 654 */ 655 public function code($text, $language = null, $filename = null, $options = null) 656 { 657 $this->_highlight('code', $text, $language, $filename, $options); 658 } 659 660 /** 661 * Use GeSHi to highlight language syntax in code and file blocks 662 * 663 * @param string $type code|file 664 * @param string $text text to show 665 * @param string $language programming language to use for syntax highlighting 666 * @param string $filename file path label 667 * @param array $options associative array with additional geshi options 668 * @author Andreas Gohr <andi@splitbrain.org> 669 */ 670 public function _highlight($type, $text, $language = null, $filename = null, $options = null) 671 { 672 global $ID; 673 global $lang; 674 global $INPUT; 675 676 $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language ?? ''); 677 678 if ($filename) { 679 // add icon 680 [$ext] = mimetype($filename, false); 681 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext); 682 $class = 'mediafile mf_' . $class; 683 684 $offset = 0; 685 if ($INPUT->has('codeblockOffset')) { 686 $offset = $INPUT->str('codeblockOffset'); 687 } 688 $this->doc .= '<dl class="' . $type . '">' . DOKU_LF; 689 $this->doc .= '<dt><a href="' . 690 exportlink( 691 $ID, 692 'code', 693 ['codeblock' => $offset + $this->_codeblock] 694 ) . '" title="' . $lang['download'] . '" class="' . $class . '">'; 695 $this->doc .= hsc($filename); 696 $this->doc .= '</a></dt>' . DOKU_LF . '<dd>'; 697 } 698 699 if (str_starts_with($text, "\n")) { 700 $text = substr($text, 1); 701 } 702 if (str_ends_with($text, "\n")) { 703 $text = substr($text, 0, -1); 704 } 705 706 if (empty($language)) { // empty is faster than is_null and can prevent '' string 707 $this->doc .= '<pre class="' . $type . '">' . $this->_xmlEntities($text) . '</pre>' . DOKU_LF; 708 } else { 709 $class = 'code'; //we always need the code class to make the syntax highlighting apply 710 if ($type != 'code') $class .= ' ' . $type; 711 712 $this->doc .= "<pre class=\"$class $language\">" . 713 p_xhtml_cached_geshi($text, $language, '', $options) . 714 '</pre>' . DOKU_LF; 715 } 716 717 if ($filename) { 718 $this->doc .= '</dd></dl>' . DOKU_LF; 719 } 720 721 $this->_codeblock++; 722 } 723 724 /** 725 * Format an acronym 726 * 727 * Uses $this->acronyms 728 * 729 * @param string $acronym 730 */ 731 public function acronym($acronym) 732 { 733 734 if (array_key_exists($acronym, $this->acronyms)) { 735 $title = $this->_xmlEntities($this->acronyms[$acronym]); 736 737 $this->doc .= '<abbr title="' . $title 738 . '">' . $this->_xmlEntities($acronym) . '</abbr>'; 739 } else { 740 $this->doc .= $this->_xmlEntities($acronym); 741 } 742 } 743 744 /** 745 * Format a smiley 746 * 747 * Uses $this->smiley 748 * 749 * @param string $smiley 750 */ 751 public function smiley($smiley) 752 { 753 if (isset($this->smileys[$smiley])) { 754 $this->doc .= '<img src="' . DOKU_BASE . 'lib/images/smileys/' . $this->smileys[$smiley] . 755 '" class="icon smiley" alt="' . $this->_xmlEntities($smiley) . '" />'; 756 } else { 757 $this->doc .= $this->_xmlEntities($smiley); 758 } 759 } 760 761 /** 762 * Format an entity 763 * 764 * Entities are basically small text replacements 765 * 766 * Uses $this->entities 767 * 768 * @param string $entity 769 */ 770 public function entity($entity) 771 { 772 if (array_key_exists($entity, $this->entities)) { 773 $this->doc .= $this->entities[$entity]; 774 } else { 775 $this->doc .= $this->_xmlEntities($entity); 776 } 777 } 778 779 /** 780 * Typographically format a multiply sign 781 * 782 * Example: ($x=640, $y=480) should result in "640×480" 783 * 784 * @param string|int $x first value 785 * @param string|int $y second value 786 */ 787 public function multiplyentity($x, $y) 788 { 789 $this->doc .= "$x×$y"; 790 } 791 792 /** 793 * Render an opening single quote char (language specific) 794 */ 795 public function singlequoteopening() 796 { 797 global $lang; 798 $this->doc .= $lang['singlequoteopening']; 799 } 800 801 /** 802 * Render a closing single quote char (language specific) 803 */ 804 public function singlequoteclosing() 805 { 806 global $lang; 807 $this->doc .= $lang['singlequoteclosing']; 808 } 809 810 /** 811 * Render an apostrophe char (language specific) 812 */ 813 public function apostrophe() 814 { 815 global $lang; 816 $this->doc .= $lang['apostrophe']; 817 } 818 819 /** 820 * Render an opening double quote char (language specific) 821 */ 822 public function doublequoteopening() 823 { 824 global $lang; 825 $this->doc .= $lang['doublequoteopening']; 826 } 827 828 /** 829 * Render an closinging double quote char (language specific) 830 */ 831 public function doublequoteclosing() 832 { 833 global $lang; 834 $this->doc .= $lang['doublequoteclosing']; 835 } 836 837 /** 838 * Render a CamelCase link 839 * 840 * @param string $link The link name 841 * @param bool $returnonly whether to return html or write to doc attribute 842 * @return void|string writes to doc attribute or returns html depends on $returnonly 843 * 844 * @see http://en.wikipedia.org/wiki/CamelCase 845 */ 846 public function camelcaselink($link, $returnonly = false) 847 { 848 if ($returnonly) { 849 return $this->internallink($link, $link, null, true); 850 } else { 851 $this->internallink($link, $link); 852 } 853 } 854 855 /** 856 * Render a page local link 857 * 858 * @param string $hash hash link identifier 859 * @param string $name name for the link 860 * @param bool $returnonly whether to return html or write to doc attribute 861 * @return void|string writes to doc attribute or returns html depends on $returnonly 862 */ 863 public function locallink($hash, $name = null, $returnonly = false) 864 { 865 global $ID; 866 $name = $this->_getLinkTitle($name, $hash, $isImage); 867 $hash = $this->_headerToLink($hash); 868 $title = $ID . ' ↵'; 869 870 $doc = '<a href="#' . $hash . '" title="' . $title . '" class="wikilink1">'; 871 $doc .= $name; 872 $doc .= '</a>'; 873 874 if ($returnonly) { 875 return $doc; 876 } else { 877 $this->doc .= $doc; 878 } 879 } 880 881 /** 882 * Render an internal Wiki Link 883 * 884 * $search,$returnonly & $linktype are not for the renderer but are used 885 * elsewhere - no need to implement them in other renderers 886 * 887 * @param string $id pageid 888 * @param string|null $name link name 889 * @param string|null $search adds search url param 890 * @param bool $returnonly whether to return html or write to doc attribute 891 * @param string $linktype type to set use of headings 892 * @return void|string writes to doc attribute or returns html depends on $returnonly 893 * @author Andreas Gohr <andi@splitbrain.org> 894 */ 895 public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') 896 { 897 global $conf; 898 global $ID; 899 global $INFO; 900 901 $params = ''; 902 $parts = explode('?', $id, 2); 903 if (count($parts) === 2) { 904 $id = $parts[0]; 905 $params = $parts[1]; 906 } 907 908 // For empty $id we need to know the current $ID 909 // We need this check because _simpleTitle needs 910 // correct $id and resolve_pageid() use cleanID($id) 911 // (some things could be lost) 912 if ($id === '') { 913 $id = $ID; 914 } 915 916 // default name is based on $id as given 917 $default = $this->_simpleTitle($id); 918 919 // now first resolve and clean up the $id 920 $id = (new PageResolver($ID))->resolveId($id, $this->date_at, true); 921 $exists = page_exists($id, $this->date_at, false, true); 922 923 $link = []; 924 $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype); 925 if (!$isImage) { 926 if ($exists) { 927 $class = 'wikilink1'; 928 } else { 929 $class = 'wikilink2'; 930 $link['rel'] = 'nofollow'; 931 } 932 } else { 933 $class = 'media'; 934 } 935 936 //keep hash anchor 937 [$id, $hash] = sexplode('#', $id, 2); 938 if (!empty($hash)) $hash = $this->_headerToLink($hash); 939 940 //prepare for formating 941 $link['target'] = $conf['target']['wiki']; 942 $link['style'] = ''; 943 $link['pre'] = ''; 944 $link['suf'] = ''; 945 $link['more'] = 'data-wiki-id="' . $id . '"'; // id is already cleaned 946 $link['class'] = $class; 947 if ($this->date_at) { 948 $params = $params . '&at=' . rawurlencode($this->date_at); 949 } 950 $link['url'] = wl($id, $params); 951 $link['name'] = $name; 952 $link['title'] = $id; 953 //add search string 954 if ($search) { 955 ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&'; 956 if (is_array($search)) { 957 $search = array_map(rawurlencode(...), $search); 958 $link['url'] .= 's[]=' . implode('&s[]=', $search); 959 } else { 960 $link['url'] .= 's=' . rawurlencode($search); 961 } 962 } 963 964 //keep hash 965 if ($hash) $link['url'] .= '#' . $hash; 966 967 //output formatted 968 if ($returnonly) { 969 return $this->_formatLink($link); 970 } else { 971 $this->doc .= $this->_formatLink($link); 972 } 973 } 974 975 /** 976 * Render an external link 977 * 978 * @param string $url full URL with scheme 979 * @param string|array $name name for the link, array for media file 980 * @param bool $returnonly whether to return html or write to doc attribute 981 * @return void|string writes to doc attribute or returns html depends on $returnonly 982 */ 983 public function externallink($url, $name = null, $returnonly = false) 984 { 985 global $conf; 986 987 $name = $this->_getLinkTitle($name, $url, $isImage); 988 989 // url might be an attack vector, only allow registered protocols 990 if (is_null($this->schemes)) $this->schemes = getSchemes(); 991 [$scheme] = explode('://', $url); 992 $scheme = strtolower($scheme); 993 if (!in_array($scheme, $this->schemes)) $url = ''; 994 995 // is there still an URL? 996 if (!$url) { 997 if ($returnonly) { 998 return $name; 999 } else { 1000 $this->doc .= $name; 1001 } 1002 return; 1003 } 1004 1005 // set class 1006 if (!$isImage) { 1007 $class = 'urlextern'; 1008 } else { 1009 $class = 'media'; 1010 } 1011 1012 //prepare for formating 1013 $link = []; 1014 $link['target'] = $conf['target']['extern']; 1015 $link['style'] = ''; 1016 $link['pre'] = ''; 1017 $link['suf'] = ''; 1018 $link['more'] = ''; 1019 $link['class'] = $class; 1020 $link['url'] = $url; 1021 $link['rel'] = ''; 1022 1023 $link['name'] = $name; 1024 $link['title'] = $this->_xmlEntities($url); 1025 if ($conf['relnofollow']) $link['rel'] .= ' ugc nofollow'; 1026 if ($conf['target']['extern']) $link['rel'] .= ' noopener'; 1027 1028 //output formatted 1029 if ($returnonly) { 1030 return $this->_formatLink($link); 1031 } else { 1032 $this->doc .= $this->_formatLink($link); 1033 } 1034 } 1035 1036 /** 1037 * Render an interwiki link 1038 * 1039 * You may want to use $this->_resolveInterWiki() here 1040 * 1041 * @param string $match original link - probably not much use 1042 * @param string|array $name name for the link, array for media file 1043 * @param string $wikiName indentifier (shortcut) for the remote wiki 1044 * @param string $wikiUri the fragment parsed from the original link 1045 * @param bool $returnonly whether to return html or write to doc attribute 1046 * @return void|string writes to doc attribute or returns html depends on $returnonly 1047 */ 1048 public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false) 1049 { 1050 global $conf; 1051 1052 $link = []; 1053 $link['target'] = $conf['target']['interwiki']; 1054 $link['pre'] = ''; 1055 $link['suf'] = ''; 1056 $link['more'] = ''; 1057 $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage); 1058 $link['rel'] = ''; 1059 1060 //get interwiki URL 1061 $exists = null; 1062 $url = $this->_resolveInterWiki($wikiName, $wikiUri, $exists); 1063 1064 if (!$isImage) { 1065 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName); 1066 $link['class'] = "interwiki iw_$class"; 1067 } else { 1068 $link['class'] = 'media'; 1069 } 1070 1071 //do we stay at the same server? Use local target 1072 if (str_starts_with($url, DOKU_URL) || str_starts_with($url, DOKU_BASE)) { 1073 $link['target'] = $conf['target']['wiki']; 1074 } 1075 if ($exists !== null && !$isImage) { 1076 if ($exists) { 1077 $link['class'] .= ' wikilink1'; 1078 } else { 1079 $link['class'] .= ' wikilink2'; 1080 $link['rel'] .= ' nofollow'; 1081 } 1082 } 1083 if ($conf['target']['interwiki']) $link['rel'] .= ' noopener'; 1084 1085 $link['url'] = $url; 1086 $link['title'] = $this->_xmlEntities($link['url']); 1087 1088 // output formatted 1089 if ($returnonly) { 1090 if ($url == '') return $link['name']; 1091 return $this->_formatLink($link); 1092 } elseif ($url == '') { 1093 $this->doc .= $link['name']; 1094 } else $this->doc .= $this->_formatLink($link); 1095 } 1096 1097 /** 1098 * Link to windows share 1099 * 1100 * @param string $url the link 1101 * @param string|array $name name for the link, array for media file 1102 * @param bool $returnonly whether to return html or write to doc attribute 1103 * @return void|string writes to doc attribute or returns html depends on $returnonly 1104 */ 1105 public function windowssharelink($url, $name = null, $returnonly = false) 1106 { 1107 global $conf; 1108 1109 //simple setup 1110 $link = []; 1111 $link['target'] = $conf['target']['windows']; 1112 $link['pre'] = ''; 1113 $link['suf'] = ''; 1114 $link['style'] = ''; 1115 1116 $link['name'] = $this->_getLinkTitle($name, $url, $isImage); 1117 if (!$isImage) { 1118 $link['class'] = 'windows'; 1119 } else { 1120 $link['class'] = 'media'; 1121 } 1122 1123 $link['title'] = $this->_xmlEntities($url); 1124 $url = str_replace('\\', '/', $url); 1125 $url = 'file:///' . $url; 1126 $link['url'] = $url; 1127 1128 //output formatted 1129 if ($returnonly) { 1130 return $this->_formatLink($link); 1131 } else { 1132 $this->doc .= $this->_formatLink($link); 1133 } 1134 } 1135 1136 /** 1137 * Render a linked E-Mail Address 1138 * 1139 * Honors $conf['mailguard'] setting 1140 * 1141 * @param string $address Email-Address 1142 * @param string|array $name name for the link, array for media file 1143 * @param bool $returnonly whether to return html or write to doc attribute 1144 * @return void|string writes to doc attribute or returns html depends on $returnonly 1145 */ 1146 public function emaillink($address, $name = null, $returnonly = false) 1147 { 1148 global $conf; 1149 //simple setup 1150 $link = []; 1151 $link['target'] = ''; 1152 $link['pre'] = ''; 1153 $link['suf'] = ''; 1154 $link['style'] = ''; 1155 $link['more'] = ''; 1156 1157 $name = $this->_getLinkTitle($name, '', $isImage); 1158 if (!$isImage) { 1159 $link['class'] = 'mail'; 1160 } else { 1161 $link['class'] = 'media'; 1162 } 1163 1164 $address = $this->_xmlEntities($address); 1165 $address = obfuscate($address); 1166 1167 $title = $address; 1168 1169 if (empty($name)) { 1170 $name = $address; 1171 } 1172 1173 if ($conf['mailguard'] == 'visible') $address = rawurlencode($address); 1174 1175 $link['url'] = 'mailto:' . $address; 1176 $link['name'] = $name; 1177 $link['title'] = $title; 1178 1179 //output formatted 1180 if ($returnonly) { 1181 return $this->_formatLink($link); 1182 } else { 1183 $this->doc .= $this->_formatLink($link); 1184 } 1185 } 1186 1187 /** 1188 * Render an internal media file 1189 * 1190 * @param string $src media ID 1191 * @param string $title descriptive text 1192 * @param string $align left|center|right 1193 * @param int $width width of media in pixel 1194 * @param int $height height of media in pixel 1195 * @param string $cache cache|recache|nocache 1196 * @param string $linking linkonly|detail|nolink 1197 * @param bool $return return HTML instead of adding to $doc 1198 * @return void|string writes to doc attribute or returns html depends on $return 1199 */ 1200 public function internalmedia( 1201 $src, 1202 $title = null, 1203 $align = null, 1204 $width = null, 1205 $height = null, 1206 $cache = null, 1207 $linking = null, 1208 $return = false 1209 ) { 1210 global $ID; 1211 if (str_contains($src, '#')) { 1212 [$src, $hash] = sexplode('#', $src, 2); 1213 } 1214 $src = (new MediaResolver($ID))->resolveId($src, $this->date_at, true); 1215 $exists = media_exists($src); 1216 1217 $noLink = false; 1218 $render = $linking != 'linkonly'; 1219 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render); 1220 1221 [$ext, $mime] = mimetype($src, false); 1222 if (str_starts_with($mime, 'image') && $render) { 1223 $link['url'] = ml( 1224 $src, 1225 [ 1226 'id' => $ID, 1227 'cache' => $cache, 1228 'rev' => $this->_getLastMediaRevisionAt($src) 1229 ], 1230 ($linking == 'direct') 1231 ); 1232 } elseif (($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) { 1233 // don't link movies 1234 $noLink = true; 1235 } else { 1236 // add file icons 1237 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext); 1238 $link['class'] .= ' mediafile mf_' . $class; 1239 $link['url'] = ml( 1240 $src, 1241 [ 1242 'id' => $ID, 1243 'cache' => $cache, 1244 'rev' => $this->_getLastMediaRevisionAt($src) 1245 ], 1246 true 1247 ); 1248 if ($exists) $link['title'] .= ' (' . filesize_h(filesize(mediaFN($src))) . ')'; 1249 } 1250 1251 if (!empty($hash)) $link['url'] .= '#' . $hash; 1252 1253 //markup non existing files 1254 if (!$exists) { 1255 $link['class'] .= ' wikilink2'; 1256 } 1257 1258 //output formatted 1259 if ($return) { 1260 if ($linking == 'nolink' || $noLink) { 1261 return $link['name']; 1262 } else { 1263 return $this->_formatLink($link); 1264 } 1265 } elseif ($linking == 'nolink' || $noLink) { 1266 $this->doc .= $link['name']; 1267 } else { 1268 $this->doc .= $this->_formatLink($link); 1269 } 1270 } 1271 1272 /** 1273 * Render an external media file 1274 * 1275 * @param string $src full media URL 1276 * @param string $title descriptive text 1277 * @param string $align left|center|right 1278 * @param int $width width of media in pixel 1279 * @param int $height height of media in pixel 1280 * @param string $cache cache|recache|nocache 1281 * @param string $linking linkonly|detail|nolink 1282 * @param bool $return return HTML instead of adding to $doc 1283 * @return void|string writes to doc attribute or returns html depends on $return 1284 */ 1285 public function externalmedia( 1286 $src, 1287 $title = null, 1288 $align = null, 1289 $width = null, 1290 $height = null, 1291 $cache = null, 1292 $linking = null, 1293 $return = false 1294 ) { 1295 if (link_isinterwiki($src)) { 1296 [$shortcut, $reference] = sexplode('>', $src, 2, ''); 1297 $exists = null; 1298 $src = $this->_resolveInterWiki($shortcut, $reference, $exists); 1299 if ($src == '' && empty($title)) { 1300 // make sure at least something will be shown in this case 1301 $title = $reference; 1302 } 1303 } 1304 [$src, $hash] = sexplode('#', $src, 2); 1305 $noLink = false; 1306 if ($src == '') { 1307 // only output plaintext without link if there is no src 1308 $noLink = true; 1309 } 1310 $render = $linking != 'linkonly'; 1311 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render); 1312 1313 $link['url'] = ml($src, ['cache' => $cache]); 1314 1315 [$ext, $mime] = mimetype($src, false); 1316 if (str_starts_with($mime, 'image') && $render) { 1317 // link only jpeg images 1318 // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true; 1319 } elseif (($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) { 1320 // don't link movies 1321 $noLink = true; 1322 } else { 1323 // add file icons 1324 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext); 1325 $link['class'] .= ' mediafile mf_' . $class; 1326 } 1327 1328 if ($hash) $link['url'] .= '#' . $hash; 1329 1330 //output formatted 1331 if ($return) { 1332 if ($linking == 'nolink' || $noLink) return $link['name']; 1333 else return $this->_formatLink($link); 1334 } elseif ($linking == 'nolink' || $noLink) { 1335 $this->doc .= $link['name']; 1336 } else $this->doc .= $this->_formatLink($link); 1337 } 1338 1339 /** 1340 * Renders an RSS feed 1341 * 1342 * @param string $url URL of the feed 1343 * @param array $params Finetuning of the output 1344 * 1345 * @author Andreas Gohr <andi@splitbrain.org> 1346 */ 1347 public function rss($url, $params) 1348 { 1349 global $lang; 1350 global $conf; 1351 1352 $feed = new FeedParser(); 1353 $feed->set_feed_url($url); 1354 1355 //disable warning while fetching 1356 if (!defined('DOKU_E_LEVEL')) { 1357 $elvl = error_reporting(E_ERROR); 1358 } 1359 $rc = $feed->init(); 1360 if (isset($elvl)) { 1361 error_reporting($elvl); 1362 } 1363 1364 if ($params['nosort']) $feed->enable_order_by_date(false); 1365 1366 //decide on start and end 1367 if ($params['reverse']) { 1368 $mod = -1; 1369 $start = $feed->get_item_quantity() - 1; 1370 $end = $start - ($params['max']); 1371 $end = ($end < -1) ? -1 : $end; 1372 } else { 1373 $mod = 1; 1374 $start = 0; 1375 $end = $feed->get_item_quantity(); 1376 $end = ($end > $params['max']) ? $params['max'] : $end; 1377 } 1378 1379 $this->doc .= '<ul class="rss">'; 1380 if ($rc) { 1381 for ($x = $start; $x != $end; $x += $mod) { 1382 $item = $feed->get_item($x); 1383 $this->doc .= '<li><div class="li">'; 1384 1385 $lnkurl = $item->get_permalink(); 1386 $title = html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8'); 1387 1388 // support feeds without links 1389 if ($lnkurl) { 1390 $this->externallink($item->get_permalink(), $title); 1391 } else { 1392 $this->doc .= ' ' . hsc($item->get_title()); 1393 } 1394 if ($params['author']) { 1395 $author = $item->get_author(0); 1396 if ($author instanceof Author) { 1397 $name = $author->get_name(); 1398 if (!$name) $name = $author->get_email(); 1399 if ($name) $this->doc .= ' ' . $lang['by'] . ' ' . hsc($name); 1400 } 1401 } 1402 if ($params['date']) { 1403 $this->doc .= ' (' . $item->get_local_date($conf['dformat']) . ')'; 1404 } 1405 if ($params['details']) { 1406 $desc = $item->get_description(); 1407 $desc = strip_tags($desc); 1408 $desc = html_entity_decode($desc, ENT_QUOTES, 'UTF-8'); 1409 $this->doc .= '<div class="detail">'; 1410 $this->doc .= hsc($desc); 1411 $this->doc .= '</div>'; 1412 } 1413 1414 $this->doc .= '</div></li>'; 1415 } 1416 } else { 1417 $this->doc .= '<li><div class="li">'; 1418 $this->doc .= '<em>' . $lang['rssfailed'] . '</em>'; 1419 $this->externallink($url); 1420 if ($conf['allowdebug']) { 1421 $this->doc .= '<!--' . hsc($feed->error) . '-->'; 1422 } 1423 $this->doc .= '</div></li>'; 1424 } 1425 $this->doc .= '</ul>'; 1426 } 1427 1428 /** 1429 * Start a table 1430 * 1431 * @param int $maxcols maximum number of columns 1432 * @param int $numrows NOT IMPLEMENTED 1433 * @param int $pos byte position in the original source 1434 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input 1435 */ 1436 public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null) 1437 { 1438 // initialize the row counter used for classes 1439 $this->_counter['row_counter'] = 0; 1440 $class = 'table'; 1441 if ($classes !== null) { 1442 if (is_array($classes)) $classes = implode(' ', $classes); 1443 $class .= ' ' . $classes; 1444 } 1445 if ($pos !== null) { 1446 $hid = $this->_headerToLink($class, true); 1447 $data = []; 1448 $data['target'] = 'table'; 1449 $data['name'] = ''; 1450 $data['hid'] = $hid; 1451 $class .= ' ' . $this->startSectionEdit($pos, $data); 1452 } 1453 $this->doc .= '<div class="' . $class . '"><table class="inline">' . 1454 DOKU_LF; 1455 } 1456 1457 /** 1458 * Close a table 1459 * 1460 * @param int $pos byte position in the original source 1461 */ 1462 public function table_close($pos = null) 1463 { 1464 $this->doc .= '</table></div>' . DOKU_LF; 1465 if ($pos !== null) { 1466 $this->finishSectionEdit($pos); 1467 } 1468 } 1469 1470 /** 1471 * Open a table header 1472 */ 1473 public function tablethead_open() 1474 { 1475 $this->doc .= DOKU_TAB . '<thead>' . DOKU_LF; 1476 } 1477 1478 /** 1479 * Close a table header 1480 */ 1481 public function tablethead_close() 1482 { 1483 $this->doc .= DOKU_TAB . '</thead>' . DOKU_LF; 1484 } 1485 1486 /** 1487 * Open a table body 1488 */ 1489 public function tabletbody_open() 1490 { 1491 $this->doc .= DOKU_TAB . '<tbody>' . DOKU_LF; 1492 } 1493 1494 /** 1495 * Close a table body 1496 */ 1497 public function tabletbody_close() 1498 { 1499 $this->doc .= DOKU_TAB . '</tbody>' . DOKU_LF; 1500 } 1501 1502 /** 1503 * Open a table footer 1504 */ 1505 public function tabletfoot_open() 1506 { 1507 $this->doc .= DOKU_TAB . '<tfoot>' . DOKU_LF; 1508 } 1509 1510 /** 1511 * Close a table footer 1512 */ 1513 public function tabletfoot_close() 1514 { 1515 $this->doc .= DOKU_TAB . '</tfoot>' . DOKU_LF; 1516 } 1517 1518 /** 1519 * Open a table row 1520 * 1521 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input 1522 */ 1523 public function tablerow_open($classes = null) 1524 { 1525 // initialize the cell counter used for classes 1526 $this->_counter['cell_counter'] = 0; 1527 $class = 'row' . $this->_counter['row_counter']++; 1528 if ($classes !== null) { 1529 if (is_array($classes)) $classes = implode(' ', $classes); 1530 $class .= ' ' . $classes; 1531 } 1532 $this->doc .= DOKU_TAB . '<tr class="' . $class . '">' . DOKU_LF . DOKU_TAB . DOKU_TAB; 1533 } 1534 1535 /** 1536 * Close a table row 1537 */ 1538 public function tablerow_close() 1539 { 1540 $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF; 1541 } 1542 1543 /** 1544 * Open a table header cell 1545 * 1546 * @param int $colspan 1547 * @param string $align left|center|right 1548 * @param int $rowspan 1549 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input 1550 */ 1551 public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) 1552 { 1553 $class = 'class="col' . $this->_counter['cell_counter']++; 1554 if (!is_null($align)) { 1555 $class .= ' ' . $align . 'align'; 1556 } 1557 if ($classes !== null) { 1558 if (is_array($classes)) $classes = implode(' ', $classes); 1559 $class .= ' ' . $classes; 1560 } 1561 $class .= '"'; 1562 $this->doc .= '<th ' . $class; 1563 if ($colspan > 1) { 1564 $this->_counter['cell_counter'] += $colspan - 1; 1565 $this->doc .= ' colspan="' . $colspan . '"'; 1566 } 1567 if ($rowspan > 1) { 1568 $this->doc .= ' rowspan="' . $rowspan . '"'; 1569 } 1570 $this->doc .= '>'; 1571 } 1572 1573 /** 1574 * Close a table header cell 1575 */ 1576 public function tableheader_close() 1577 { 1578 $this->doc .= '</th>'; 1579 } 1580 1581 /** 1582 * Open a table cell 1583 * 1584 * @param int $colspan 1585 * @param string $align left|center|right 1586 * @param int $rowspan 1587 * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input 1588 */ 1589 public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) 1590 { 1591 $class = 'class="col' . $this->_counter['cell_counter']++; 1592 if (!is_null($align)) { 1593 $class .= ' ' . $align . 'align'; 1594 } 1595 if ($classes !== null) { 1596 if (is_array($classes)) $classes = implode(' ', $classes); 1597 $class .= ' ' . $classes; 1598 } 1599 $class .= '"'; 1600 $this->doc .= '<td ' . $class; 1601 if ($colspan > 1) { 1602 $this->_counter['cell_counter'] += $colspan - 1; 1603 $this->doc .= ' colspan="' . $colspan . '"'; 1604 } 1605 if ($rowspan > 1) { 1606 $this->doc .= ' rowspan="' . $rowspan . '"'; 1607 } 1608 $this->doc .= '>'; 1609 } 1610 1611 /** 1612 * Close a table cell 1613 */ 1614 public function tablecell_close() 1615 { 1616 $this->doc .= '</td>'; 1617 } 1618 1619 /** 1620 * Returns the current header level. 1621 * (required e.g. by the filelist plugin) 1622 * 1623 * @return int The current header level 1624 */ 1625 public function getLastlevel() 1626 { 1627 return $this->lastlevel; 1628 } 1629 1630 #region Utility functions 1631 1632 /** 1633 * Build a link 1634 * 1635 * Assembles all parts defined in $link returns HTML for the link 1636 * 1637 * @param array $link attributes of a link 1638 * @return string 1639 * 1640 * @author Andreas Gohr <andi@splitbrain.org> 1641 */ 1642 public function _formatLink($link) 1643 { 1644 //make sure the url is XHTML compliant (skip mailto) 1645 if (!str_starts_with($link['url'], 'mailto:')) { 1646 $link['url'] = str_replace('&', '&', $link['url']); 1647 $link['url'] = str_replace('&amp;', '&', $link['url']); 1648 } 1649 //remove double encodings in titles 1650 $link['title'] = str_replace('&amp;', '&', $link['title']); 1651 1652 // be sure there are no bad chars in url or title 1653 // (we can't do this for name because it can contain an img tag) 1654 $link['url'] = strtr($link['url'], ['>' => '%3E', '<' => '%3C', '"' => '%22']); 1655 $link['title'] = strtr($link['title'], ['>' => '>', '<' => '<', '"' => '"']); 1656 1657 $ret = ''; 1658 $ret .= $link['pre']; 1659 $ret .= '<a href="' . $link['url'] . '"'; 1660 if (!empty($link['class'])) $ret .= ' class="' . $link['class'] . '"'; 1661 if (!empty($link['target'])) $ret .= ' target="' . $link['target'] . '"'; 1662 if (!empty($link['title'])) $ret .= ' title="' . $link['title'] . '"'; 1663 if (!empty($link['style'])) $ret .= ' style="' . $link['style'] . '"'; 1664 if (!empty($link['rel'])) $ret .= ' rel="' . trim($link['rel']) . '"'; 1665 if (!empty($link['more'])) $ret .= ' ' . $link['more']; 1666 $ret .= '>'; 1667 $ret .= $link['name']; 1668 $ret .= '</a>'; 1669 $ret .= $link['suf']; 1670 return $ret; 1671 } 1672 1673 /** 1674 * Renders internal and external media 1675 * 1676 * @param string $src media ID 1677 * @param string $title descriptive text 1678 * @param string $align left|center|right 1679 * @param int $width width of media in pixel 1680 * @param int $height height of media in pixel 1681 * @param string $cache cache|recache|nocache 1682 * @param bool $render should the media be embedded inline or just linked 1683 * @return string 1684 * @author Andreas Gohr <andi@splitbrain.org> 1685 */ 1686 public function _media( 1687 $src, 1688 $title = null, 1689 $align = null, 1690 $width = null, 1691 $height = null, 1692 $cache = null, 1693 $render = true 1694 ) { 1695 1696 $ret = ''; 1697 1698 [$ext, $mime] = mimetype($src); 1699 if (str_starts_with($mime, 'image')) { 1700 // first get the $title 1701 if (!is_null($title)) { 1702 $title = $this->_xmlEntities($title); 1703 } elseif ($ext == 'jpg' || $ext == 'jpeg') { 1704 //try to use the caption from IPTC/EXIF 1705 require_once(DOKU_INC . 'inc/JpegMeta.php'); 1706 $jpeg = new JpegMeta(mediaFN($src)); 1707 $cap = $jpeg->getTitle(); 1708 if (!empty($cap)) { 1709 $title = $this->_xmlEntities($cap); 1710 } 1711 } 1712 if (!$render) { 1713 // if the picture is not supposed to be rendered 1714 // return the title of the picture 1715 if ($title === null || $title === "") { 1716 // just show the sourcename 1717 $title = $this->_xmlEntities(PhpString::basename(noNS($src))); 1718 } 1719 return $title; 1720 } 1721 //add image tag 1722 $ret .= '<img src="' . ml( 1723 $src, 1724 [ 1725 'w' => $width, 1726 'h' => $height, 1727 'cache' => $cache, 1728 'rev' => $this->_getLastMediaRevisionAt($src) 1729 ] 1730 ) . '"'; 1731 $ret .= ' class="media' . $align . '"'; 1732 $ret .= ' loading="lazy"'; 1733 1734 if ($title) { 1735 $ret .= ' title="' . $title . '"'; 1736 $ret .= ' alt="' . $title . '"'; 1737 } else { 1738 $ret .= ' alt=""'; 1739 } 1740 1741 if (!is_null($width)) { 1742 $ret .= ' width="' . $this->_xmlEntities($width) . '"'; 1743 } 1744 1745 if (!is_null($height)) { 1746 $ret .= ' height="' . $this->_xmlEntities($height) . '"'; 1747 } 1748 1749 $ret .= ' />'; 1750 } elseif (media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) { 1751 // first get the $title 1752 $title ??= false; 1753 if (!$render) { 1754 // if the file is not supposed to be rendered 1755 // return the title of the file (just the sourcename if there is no title) 1756 return $this->_xmlEntities($title ?: PhpString::basename(noNS($src))); 1757 } 1758 1759 $att = []; 1760 $att['class'] = "media$align"; 1761 if ($title) { 1762 $att['title'] = $title; 1763 } 1764 1765 if (media_supportedav($mime, 'video')) { 1766 //add video 1767 $ret .= $this->_video($src, $width, $height, $att); 1768 } 1769 if (media_supportedav($mime, 'audio')) { 1770 //add audio 1771 $ret .= $this->_audio($src, $att); 1772 } 1773 } elseif ($mime == 'application/x-shockwave-flash') { 1774 if (!$render) { 1775 // if the flash is not supposed to be rendered 1776 // return the title of the flash 1777 if (!$title) { 1778 // just show the sourcename 1779 $title = PhpString::basename(noNS($src)); 1780 } 1781 return $this->_xmlEntities($title); 1782 } 1783 1784 $att = []; 1785 $att['class'] = "media$align"; 1786 if ($align == 'right') $att['align'] = 'right'; 1787 if ($align == 'left') $att['align'] = 'left'; 1788 $ret .= html_flashobject( 1789 ml($src, ['cache' => $cache], true, '&'), 1790 $width, 1791 $height, 1792 ['quality' => 'high'], 1793 null, 1794 $att, 1795 $this->_xmlEntities($title) 1796 ); 1797 } elseif ($title) { 1798 // well at least we have a title to display 1799 $ret .= $this->_xmlEntities($title); 1800 } else { 1801 // just show the sourcename 1802 $ret .= $this->_xmlEntities(PhpString::basename(noNS($src))); 1803 } 1804 1805 return $ret; 1806 } 1807 1808 /** 1809 * Escape string for output 1810 * 1811 * @param $string 1812 * @return string 1813 */ 1814 public function _xmlEntities($string) 1815 { 1816 return hsc($string); 1817 } 1818 1819 1820 /** 1821 * Construct a title and handle images in titles 1822 * 1823 * @param string|array $title either string title or media array 1824 * @param string $default default title if nothing else is found 1825 * @param bool $isImage will be set to true if it's a media file 1826 * @param null|string $id linked page id (used to extract title from first heading) 1827 * @param string $linktype content|navigation 1828 * @return string HTML of the title, might be full image tag or just escaped text 1829 * @author Harry Fuecks <hfuecks@gmail.com> 1830 */ 1831 public function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') 1832 { 1833 $isImage = false; 1834 if (is_array($title)) { 1835 $isImage = true; 1836 return $this->_imageTitle($title); 1837 } elseif (is_null($title) || trim($title) == '') { 1838 if (useHeading($linktype) && $id) { 1839 $heading = p_get_first_heading($id); 1840 if (!blank($heading)) { 1841 return $this->_xmlEntities($heading); 1842 } 1843 } 1844 return $this->_xmlEntities($default); 1845 } else { 1846 return $this->_xmlEntities($title); 1847 } 1848 } 1849 1850 /** 1851 * Returns HTML code for images used in link titles 1852 * 1853 * @param array $img 1854 * @return string HTML img tag or similar 1855 * @author Andreas Gohr <andi@splitbrain.org> 1856 */ 1857 public function _imageTitle($img) 1858 { 1859 global $ID; 1860 1861 // some fixes on $img['src'] 1862 // see internalmedia() and externalmedia() 1863 [$img['src']] = explode('#', $img['src'], 2); 1864 if ($img['type'] == 'internalmedia') { 1865 $img['src'] = (new MediaResolver($ID))->resolveId($img['src'], $this->date_at, true); 1866 } 1867 1868 return $this->_media( 1869 $img['src'], 1870 $img['title'], 1871 $img['align'], 1872 $img['width'], 1873 $img['height'], 1874 $img['cache'] 1875 ); 1876 } 1877 1878 /** 1879 * helperfunction to return a basic link to a media 1880 * 1881 * used in internalmedia() and externalmedia() 1882 * 1883 * @param string $src media ID 1884 * @param string $title descriptive text 1885 * @param string $align left|center|right 1886 * @param int $width width of media in pixel 1887 * @param int $height height of media in pixel 1888 * @param string $cache cache|recache|nocache 1889 * @param bool $render should the media be embedded inline or just linked 1890 * @return array associative array with link config 1891 * @author Pierre Spring <pierre.spring@liip.ch> 1892 */ 1893 public function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) 1894 { 1895 global $conf; 1896 1897 $link = []; 1898 $link['class'] = 'media'; 1899 $link['style'] = ''; 1900 $link['pre'] = ''; 1901 $link['suf'] = ''; 1902 $link['more'] = ''; 1903 $link['target'] = $conf['target']['media']; 1904 if ($conf['target']['media']) $link['rel'] = 'noopener'; 1905 $link['title'] = $this->_xmlEntities($src); 1906 $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render); 1907 1908 return $link; 1909 } 1910 1911 /** 1912 * Embed video(s) in HTML 1913 * 1914 * @param string $src - ID of video to embed 1915 * @param int $width - width of the video in pixels 1916 * @param int $height - height of the video in pixels 1917 * @param array $atts - additional attributes for the <video> tag 1918 * @return string 1919 * @author Schplurtz le Déboulonné <Schplurtz@laposte.net> 1920 * 1921 * @author Anika Henke <anika@selfthinker.org> 1922 */ 1923 public function _video($src, $width, $height, $atts = null) 1924 { 1925 // prepare width and height 1926 if (is_null($atts)) $atts = []; 1927 $atts['width'] = (int)$width; 1928 $atts['height'] = (int)$height; 1929 if (!$atts['width']) $atts['width'] = 320; 1930 if (!$atts['height']) $atts['height'] = 240; 1931 1932 $posterUrl = ''; 1933 $files = []; 1934 $tracks = []; 1935 $isExternal = media_isexternal($src); 1936 1937 if ($isExternal) { 1938 // take direct source for external files 1939 [/* ext */, $srcMime] = mimetype($src); 1940 $files[$srcMime] = $src; 1941 } else { 1942 // prepare alternative formats 1943 $extensions = ['webm', 'ogv', 'mp4']; 1944 $files = media_alternativefiles($src, $extensions); 1945 $poster = media_alternativefiles($src, ['jpg', 'png']); 1946 $tracks = media_trackfiles($src); 1947 if (!empty($poster)) { 1948 $posterUrl = ml(reset($poster), '', true, '&'); 1949 } 1950 } 1951 1952 $out = ''; 1953 // open video tag 1954 $out .= '<video ' . buildAttributes($atts) . ' controls="controls"'; 1955 if ($posterUrl) $out .= ' poster="' . hsc($posterUrl) . '"'; 1956 $out .= '>' . NL; 1957 $fallback = ''; 1958 1959 // output source for each alternative video format 1960 foreach ($files as $mime => $file) { 1961 if ($isExternal) { 1962 $url = $file; 1963 $linkType = 'externalmedia'; 1964 } else { 1965 $url = ml($file, '', true, '&'); 1966 $linkType = 'internalmedia'; 1967 } 1968 $title = empty($atts['title']) 1969 ? $this->_xmlEntities(PhpString::basename(noNS($file))) 1970 : $atts['title']; 1971 1972 $out .= '<source src="' . hsc($url) . '" type="' . $mime . '" />' . NL; 1973 // alternative content (just a link to the file) 1974 $fallback .= $this->$linkType( 1975 $file, 1976 $title, 1977 null, 1978 null, 1979 null, 1980 $cache = null, 1981 $linking = 'linkonly', 1982 $return = true 1983 ); 1984 } 1985 1986 // output each track if any 1987 foreach ($tracks as $trackid => $info) { 1988 [$kind, $srclang] = array_map(hsc(...), $info); 1989 $out .= "<track kind=\"$kind\" srclang=\"$srclang\" "; 1990 $out .= "label=\"$srclang\" "; 1991 $out .= 'src="' . ml($trackid, '', true) . '">' . NL; 1992 } 1993 1994 // finish 1995 $out .= $fallback; 1996 $out .= '</video>' . NL; 1997 return $out; 1998 } 1999 2000 /** 2001 * Embed audio in HTML 2002 * 2003 * @param string $src - ID of audio to embed 2004 * @param array $atts - additional attributes for the <audio> tag 2005 * @return string 2006 * @author Anika Henke <anika@selfthinker.org> 2007 * 2008 */ 2009 public function _audio($src, $atts = []) 2010 { 2011 $files = []; 2012 $isExternal = media_isexternal($src); 2013 2014 if ($isExternal) { 2015 // take direct source for external files 2016 [/* ext */, $srcMime] = mimetype($src); 2017 $files[$srcMime] = $src; 2018 } else { 2019 // prepare alternative formats 2020 $extensions = ['ogg', 'mp3', 'wav']; 2021 $files = media_alternativefiles($src, $extensions); 2022 } 2023 2024 $out = ''; 2025 // open audio tag 2026 $out .= '<audio ' . buildAttributes($atts) . ' controls="controls">' . NL; 2027 $fallback = ''; 2028 2029 // output source for each alternative audio format 2030 foreach ($files as $mime => $file) { 2031 if ($isExternal) { 2032 $url = $file; 2033 $linkType = 'externalmedia'; 2034 } else { 2035 $url = ml($file, '', true, '&'); 2036 $linkType = 'internalmedia'; 2037 } 2038 $title = $atts['title'] ?: $this->_xmlEntities(PhpString::basename(noNS($file))); 2039 2040 $out .= '<source src="' . hsc($url) . '" type="' . $mime . '" />' . NL; 2041 // alternative content (just a link to the file) 2042 $fallback .= $this->$linkType( 2043 $file, 2044 $title, 2045 null, 2046 null, 2047 null, 2048 $cache = null, 2049 $linking = 'linkonly', 2050 $return = true 2051 ); 2052 } 2053 2054 // finish 2055 $out .= $fallback; 2056 $out .= '</audio>' . NL; 2057 return $out; 2058 } 2059 2060 /** 2061 * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media() 2062 * which returns an existing media revision less or equal to rev or date_at 2063 * 2064 * @param string $media_id 2065 * @access protected 2066 * @return string revision ('' for current) 2067 * @author lisps 2068 */ 2069 protected function _getLastMediaRevisionAt($media_id) 2070 { 2071 if (!$this->date_at || media_isexternal($media_id)) return ''; 2072 $changelog = new MediaChangeLog($media_id); 2073 return $changelog->getLastRevisionAt($this->date_at); 2074 } 2075 2076 #endregion 2077} 2078 2079//Setup VIM: ex: et ts=4 : 2080