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