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 20require_once DOKU_INC . 'inc/parser/renderer.php'; 21require_once DOKU_INC . 'inc/html.php'; 22 23/** 24 * The Renderer 25 */ 26class Doku_Renderer_xhtml extends Doku_Renderer { 27 28 // @access public 29 var $doc = ''; // will contain the whole document 30 var $toc = array(); // will contain the Table of Contents 31 var $date_at = ''; // link pages and media against this revision 32 33 var $sectionedits = array(); // A stack of section edit data 34 private $lastsecid = 0; // last section edit id, used by startSectionEdit 35 36 var $headers = array(); 37 /** @var array a list of footnotes, list starts at 1! */ 38 var $footnotes = array(); 39 var $lastlevel = 0; 40 var $node = array(0,0,0,0,0); 41 var $store = ''; 42 43 var $_counter = array(); // used as global counter, introduced for table classes 44 var $_codeblock = 0; // counts the code and file blocks, used to provide download links 45 46 /** 47 * Register a new edit section range 48 * 49 * @param $type string The section type identifier 50 * @param $title string The section title 51 * @param $start int The byte position for the edit start 52 * @return string A marker class for the starting HTML element 53 * @author Adrian Lang <lang@cosmocode.de> 54 */ 55 public function startSectionEdit($start, $type, $title = null) { 56 $this->sectionedits[] = array(++$this->lastsecid, $start, $type, $title); 57 return 'sectionedit' . $this->lastsecid; 58 } 59 60 /** 61 * Finish an edit section range 62 * 63 * @param $end int The byte position for the edit end; null for the rest of 64 * the page 65 * @author Adrian Lang <lang@cosmocode.de> 66 */ 67 public function finishSectionEdit($end = null) { 68 list($id, $start, $type, $title) = array_pop($this->sectionedits); 69 if (!is_null($end) && $end <= $start) { 70 return; 71 } 72 $this->doc .= "<!-- EDIT$id " . strtoupper($type) . ' '; 73 if (!is_null($title)) { 74 $this->doc .= '"' . str_replace('"', '', $title) . '" '; 75 } 76 $this->doc .= "[$start-" . (is_null($end) ? '' : $end) . '] -->'; 77 } 78 79 function getFormat(){ 80 return 'xhtml'; 81 } 82 83 84 function document_start() { 85 //reset some internals 86 $this->toc = array(); 87 $this->headers = array(); 88 } 89 90 function document_end() { 91 // Finish open section edits. 92 while (count($this->sectionedits) > 0) { 93 if ($this->sectionedits[count($this->sectionedits) - 1][1] <= 1) { 94 // If there is only one section, do not write a section edit 95 // marker. 96 array_pop($this->sectionedits); 97 } else { 98 $this->finishSectionEdit(); 99 } 100 } 101 102 if ( count ($this->footnotes) > 0 ) { 103 $this->doc .= '<div class="footnotes">'.DOKU_LF; 104 105 foreach ( $this->footnotes as $id => $footnote ) { 106 // check its not a placeholder that indicates actual footnote text is elsewhere 107 if (substr($footnote, 0, 5) != "@@FNT") { 108 109 // open the footnote and set the anchor and backlink 110 $this->doc .= '<div class="fn">'; 111 $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">'; 112 $this->doc .= $id.')</a></sup> '.DOKU_LF; 113 114 // get any other footnotes that use the same markup 115 $alt = array_keys($this->footnotes, "@@FNT$id"); 116 117 if (count($alt)) { 118 foreach ($alt as $ref) { 119 // set anchor and backlink for the other footnotes 120 $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">'; 121 $this->doc .= ($ref).')</a></sup> '.DOKU_LF; 122 } 123 } 124 125 // add footnote markup and close this footnote 126 $this->doc .= $footnote; 127 $this->doc .= '</div>' . DOKU_LF; 128 } 129 } 130 $this->doc .= '</div>'.DOKU_LF; 131 } 132 133 // Prepare the TOC 134 global $conf; 135 if($this->info['toc'] && is_array($this->toc) && $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']){ 136 global $TOC; 137 $TOC = $this->toc; 138 } 139 140 // make sure there are no empty paragraphs 141 $this->doc = preg_replace('#<p>\s*</p>#','',$this->doc); 142 } 143 144 function toc_additem($id, $text, $level) { 145 global $conf; 146 147 //handle TOC 148 if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){ 149 $this->toc[] = html_mktocitem($id, $text, $level-$conf['toptoclevel']+1); 150 } 151 } 152 153 function header($text, $level, $pos) { 154 global $conf; 155 156 if(!$text) return; //skip empty headlines 157 158 $hid = $this->_headerToLink($text,true); 159 160 //only add items within configured levels 161 $this->toc_additem($hid, $text, $level); 162 163 // adjust $node to reflect hierarchy of levels 164 $this->node[$level-1]++; 165 if ($level < $this->lastlevel) { 166 for ($i = 0; $i < $this->lastlevel-$level; $i++) { 167 $this->node[$this->lastlevel-$i-1] = 0; 168 } 169 } 170 $this->lastlevel = $level; 171 172 if ($level <= $conf['maxseclevel'] && 173 count($this->sectionedits) > 0 && 174 $this->sectionedits[count($this->sectionedits) - 1][2] === 'section') { 175 $this->finishSectionEdit($pos - 1); 176 } 177 178 // write the header 179 $this->doc .= DOKU_LF.'<h'.$level; 180 if ($level <= $conf['maxseclevel']) { 181 $this->doc .= ' class="' . $this->startSectionEdit($pos, 'section', $text) . '"'; 182 } 183 $this->doc .= ' id="'.$hid.'">'; 184 $this->doc .= $this->_xmlEntities($text); 185 $this->doc .= "</h$level>".DOKU_LF; 186 } 187 188 function section_open($level) { 189 $this->doc .= '<div class="level' . $level . '">' . DOKU_LF; 190 } 191 192 function section_close() { 193 $this->doc .= DOKU_LF.'</div>'.DOKU_LF; 194 } 195 196 function cdata($text) { 197 $this->doc .= $this->_xmlEntities($text); 198 } 199 200 function p_open() { 201 $this->doc .= DOKU_LF.'<p>'.DOKU_LF; 202 } 203 204 function p_close() { 205 $this->doc .= DOKU_LF.'</p>'.DOKU_LF; 206 } 207 208 function linebreak() { 209 $this->doc .= '<br/>'.DOKU_LF; 210 } 211 212 function hr() { 213 $this->doc .= '<hr />'.DOKU_LF; 214 } 215 216 function strong_open() { 217 $this->doc .= '<strong>'; 218 } 219 220 function strong_close() { 221 $this->doc .= '</strong>'; 222 } 223 224 function emphasis_open() { 225 $this->doc .= '<em>'; 226 } 227 228 function emphasis_close() { 229 $this->doc .= '</em>'; 230 } 231 232 function underline_open() { 233 $this->doc .= '<em class="u">'; 234 } 235 236 function underline_close() { 237 $this->doc .= '</em>'; 238 } 239 240 function monospace_open() { 241 $this->doc .= '<code>'; 242 } 243 244 function monospace_close() { 245 $this->doc .= '</code>'; 246 } 247 248 function subscript_open() { 249 $this->doc .= '<sub>'; 250 } 251 252 function subscript_close() { 253 $this->doc .= '</sub>'; 254 } 255 256 function superscript_open() { 257 $this->doc .= '<sup>'; 258 } 259 260 function superscript_close() { 261 $this->doc .= '</sup>'; 262 } 263 264 function deleted_open() { 265 $this->doc .= '<del>'; 266 } 267 268 function deleted_close() { 269 $this->doc .= '</del>'; 270 } 271 272 /** 273 * Callback for footnote start syntax 274 * 275 * All following content will go to the footnote instead of 276 * the document. To achieve this the previous rendered content 277 * is moved to $store and $doc is cleared 278 * 279 * @author Andreas Gohr <andi@splitbrain.org> 280 */ 281 function footnote_open() { 282 283 // move current content to store and record footnote 284 $this->store = $this->doc; 285 $this->doc = ''; 286 } 287 288 /** 289 * Callback for footnote end syntax 290 * 291 * All rendered content is moved to the $footnotes array and the old 292 * content is restored from $store again 293 * 294 * @author Andreas Gohr 295 */ 296 function footnote_close() { 297 /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */ 298 static $fnid = 0; 299 // assign new footnote id (we start at 1) 300 $fnid++; 301 302 // recover footnote into the stack and restore old content 303 $footnote = $this->doc; 304 $this->doc = $this->store; 305 $this->store = ''; 306 307 // check to see if this footnote has been seen before 308 $i = array_search($footnote, $this->footnotes); 309 310 if ($i === false) { 311 // its a new footnote, add it to the $footnotes array 312 $this->footnotes[$fnid] = $footnote; 313 } else { 314 // seen this one before, save a placeholder 315 $this->footnotes[$fnid] = "@@FNT".($i); 316 } 317 318 // output the footnote reference and link 319 $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>'; 320 } 321 322 function listu_open() { 323 $this->doc .= '<ul>'.DOKU_LF; 324 } 325 326 function listu_close() { 327 $this->doc .= '</ul>'.DOKU_LF; 328 } 329 330 function listo_open() { 331 $this->doc .= '<ol>'.DOKU_LF; 332 } 333 334 function listo_close() { 335 $this->doc .= '</ol>'.DOKU_LF; 336 } 337 338 function listitem_open($level) { 339 $this->doc .= '<li class="level'.$level.'">'; 340 } 341 342 function listitem_close() { 343 $this->doc .= '</li>'.DOKU_LF; 344 } 345 346 function listcontent_open() { 347 $this->doc .= '<div class="li">'; 348 } 349 350 function listcontent_close() { 351 $this->doc .= '</div>'.DOKU_LF; 352 } 353 354 function unformatted($text) { 355 $this->doc .= $this->_xmlEntities($text); 356 } 357 358 /** 359 * Execute PHP code if allowed 360 * 361 * @param string $text PHP code that is either executed or printed 362 * @param string $wrapper html element to wrap result if $conf['phpok'] is okff 363 * 364 * @author Andreas Gohr <andi@splitbrain.org> 365 */ 366 function php($text, $wrapper='code') { 367 global $conf; 368 369 if($conf['phpok']){ 370 ob_start(); 371 eval($text); 372 $this->doc .= ob_get_contents(); 373 ob_end_clean(); 374 } else { 375 $this->doc .= p_xhtml_cached_geshi($text, 'php', $wrapper); 376 } 377 } 378 379 function phpblock($text) { 380 $this->php($text, 'pre'); 381 } 382 383 /** 384 * Insert HTML if allowed 385 * 386 * @param string $text html text 387 * @param string $wrapper html element to wrap result if $conf['htmlok'] is okff 388 * 389 * @author Andreas Gohr <andi@splitbrain.org> 390 */ 391 function html($text, $wrapper='code') { 392 global $conf; 393 394 if($conf['htmlok']){ 395 $this->doc .= $text; 396 } else { 397 $this->doc .= p_xhtml_cached_geshi($text, 'html4strict', $wrapper); 398 } 399 } 400 401 function htmlblock($text) { 402 $this->html($text, 'pre'); 403 } 404 405 function quote_open() { 406 $this->doc .= '<blockquote><div class="no">'.DOKU_LF; 407 } 408 409 function quote_close() { 410 $this->doc .= '</div></blockquote>'.DOKU_LF; 411 } 412 413 function preformatted($text) { 414 $this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text),"\n\r") . '</pre>'. DOKU_LF; 415 } 416 417 function file($text, $language=null, $filename=null) { 418 $this->_highlight('file',$text,$language,$filename); 419 } 420 421 function code($text, $language=null, $filename=null) { 422 $this->_highlight('code',$text,$language,$filename); 423 } 424 425 /** 426 * Use GeSHi to highlight language syntax in code and file blocks 427 * 428 * @author Andreas Gohr <andi@splitbrain.org> 429 */ 430 function _highlight($type, $text, $language=null, $filename=null) { 431 global $conf; 432 global $ID; 433 global $lang; 434 435 if($filename){ 436 // add icon 437 list($ext) = mimetype($filename,false); 438 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 439 $class = 'mediafile mf_'.$class; 440 441 $this->doc .= '<dl class="'.$type.'">'.DOKU_LF; 442 $this->doc .= '<dt><a href="'.exportlink($ID,'code',array('codeblock'=>$this->_codeblock)).'" title="'.$lang['download'].'" class="'.$class.'">'; 443 $this->doc .= hsc($filename); 444 $this->doc .= '</a></dt>'.DOKU_LF.'<dd>'; 445 } 446 447 if ($text{0} == "\n") { 448 $text = substr($text, 1); 449 } 450 if (substr($text, -1) == "\n") { 451 $text = substr($text, 0, -1); 452 } 453 454 if ( is_null($language) ) { 455 $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF; 456 } else { 457 $class = 'code'; //we always need the code class to make the syntax highlighting apply 458 if($type != 'code') $class .= ' '.$type; 459 460 $this->doc .= "<pre class=\"$class $language\">".p_xhtml_cached_geshi($text, $language, '').'</pre>'.DOKU_LF; 461 } 462 463 if($filename){ 464 $this->doc .= '</dd></dl>'.DOKU_LF; 465 } 466 467 $this->_codeblock++; 468 } 469 470 function acronym($acronym) { 471 472 if ( array_key_exists($acronym, $this->acronyms) ) { 473 474 $title = $this->_xmlEntities($this->acronyms[$acronym]); 475 476 $this->doc .= '<abbr title="'.$title 477 .'">'.$this->_xmlEntities($acronym).'</abbr>'; 478 479 } else { 480 $this->doc .= $this->_xmlEntities($acronym); 481 } 482 } 483 484 function smiley($smiley) { 485 if ( array_key_exists($smiley, $this->smileys) ) { 486 $title = $this->_xmlEntities($this->smileys[$smiley]); 487 $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley]. 488 '" class="icon" alt="'. 489 $this->_xmlEntities($smiley).'" />'; 490 } else { 491 $this->doc .= $this->_xmlEntities($smiley); 492 } 493 } 494 495 /* 496 * not used 497 function wordblock($word) { 498 if ( array_key_exists($word, $this->badwords) ) { 499 $this->doc .= '** BLEEP **'; 500 } else { 501 $this->doc .= $this->_xmlEntities($word); 502 } 503 } 504 */ 505 506 function entity($entity) { 507 if ( array_key_exists($entity, $this->entities) ) { 508 $this->doc .= $this->entities[$entity]; 509 } else { 510 $this->doc .= $this->_xmlEntities($entity); 511 } 512 } 513 514 function multiplyentity($x, $y) { 515 $this->doc .= "$x×$y"; 516 } 517 518 function singlequoteopening() { 519 global $lang; 520 $this->doc .= $lang['singlequoteopening']; 521 } 522 523 function singlequoteclosing() { 524 global $lang; 525 $this->doc .= $lang['singlequoteclosing']; 526 } 527 528 function apostrophe() { 529 global $lang; 530 $this->doc .= $lang['apostrophe']; 531 } 532 533 function doublequoteopening() { 534 global $lang; 535 $this->doc .= $lang['doublequoteopening']; 536 } 537 538 function doublequoteclosing() { 539 global $lang; 540 $this->doc .= $lang['doublequoteclosing']; 541 } 542 543 /** 544 */ 545 function camelcaselink($link) { 546 $this->internallink($link,$link); 547 } 548 549 550 function locallink($hash, $name = null){ 551 global $ID; 552 $name = $this->_getLinkTitle($name, $hash, $isImage); 553 $hash = $this->_headerToLink($hash); 554 $title = $ID.' ↵'; 555 $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">'; 556 $this->doc .= $name; 557 $this->doc .= '</a>'; 558 } 559 560 /** 561 * Render an internal Wiki Link 562 * 563 * $search,$returnonly & $linktype are not for the renderer but are used 564 * elsewhere - no need to implement them in other renderers 565 * 566 * @author Andreas Gohr <andi@splitbrain.org> 567 */ 568 function internallink($id, $name = null, $search=null,$returnonly=false,$linktype='content') { 569 global $conf; 570 global $ID; 571 global $INFO; 572 573 $params = ''; 574 $parts = explode('?', $id, 2); 575 if (count($parts) === 2) { 576 $id = $parts[0]; 577 $params = $parts[1]; 578 } 579 580 // For empty $id we need to know the current $ID 581 // We need this check because _simpleTitle needs 582 // correct $id and resolve_pageid() use cleanID($id) 583 // (some things could be lost) 584 if ($id === '') { 585 $id = $ID; 586 } 587 588 // default name is based on $id as given 589 $default = $this->_simpleTitle($id); 590 591 // now first resolve and clean up the $id 592 resolve_pageid(getNS($ID),$id,$exists); 593 594 $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype); 595 if ( !$isImage ) { 596 if ( $exists ) { 597 $class='wikilink1'; 598 } else { 599 $class='wikilink2'; 600 $link['rel']='nofollow'; 601 } 602 } else { 603 $class='media'; 604 } 605 606 //keep hash anchor 607 list($id,$hash) = explode('#',$id,2); 608 if(!empty($hash)) $hash = $this->_headerToLink($hash); 609 610 //prepare for formating 611 $link['target'] = $conf['target']['wiki']; 612 $link['style'] = ''; 613 $link['pre'] = ''; 614 $link['suf'] = ''; 615 // highlight link to current page 616 if ($id == $INFO['id']) { 617 $link['pre'] = '<span class="curid">'; 618 $link['suf'] = '</span>'; 619 } 620 $link['more'] = ''; 621 $link['class'] = $class; 622 if($this->date_at) { 623 $params['at'] = $this->date_at; 624 } 625 $link['url'] = wl($id, $params); 626 $link['name'] = $name; 627 $link['title'] = $id; 628 //add search string 629 if($search){ 630 ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&'; 631 if(is_array($search)){ 632 $search = array_map('rawurlencode',$search); 633 $link['url'] .= 's[]='.join('&s[]=',$search); 634 }else{ 635 $link['url'] .= 's='.rawurlencode($search); 636 } 637 } 638 639 //keep hash 640 if($hash) $link['url'].='#'.$hash; 641 642 //output formatted 643 if($returnonly){ 644 return $this->_formatLink($link); 645 }else{ 646 $this->doc .= $this->_formatLink($link); 647 } 648 } 649 650 function externallink($url, $name = null) { 651 global $conf; 652 653 $name = $this->_getLinkTitle($name, $url, $isImage); 654 655 // url might be an attack vector, only allow registered protocols 656 if(is_null($this->schemes)) $this->schemes = getSchemes(); 657 list($scheme) = explode('://',$url); 658 $scheme = strtolower($scheme); 659 if(!in_array($scheme,$this->schemes)) $url = ''; 660 661 // is there still an URL? 662 if(!$url){ 663 $this->doc .= $name; 664 return; 665 } 666 667 // set class 668 if ( !$isImage ) { 669 $class='urlextern'; 670 } else { 671 $class='media'; 672 } 673 674 //prepare for formating 675 $link['target'] = $conf['target']['extern']; 676 $link['style'] = ''; 677 $link['pre'] = ''; 678 $link['suf'] = ''; 679 $link['more'] = ''; 680 $link['class'] = $class; 681 $link['url'] = $url; 682 683 $link['name'] = $name; 684 $link['title'] = $this->_xmlEntities($url); 685 if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"'; 686 687 //output formatted 688 $this->doc .= $this->_formatLink($link); 689 } 690 691 /** 692 */ 693 function interwikilink($match, $name = null, $wikiName, $wikiUri) { 694 global $conf; 695 696 $link = array(); 697 $link['target'] = $conf['target']['interwiki']; 698 $link['pre'] = ''; 699 $link['suf'] = ''; 700 $link['more'] = ''; 701 $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage); 702 703 //get interwiki URL 704 $url = $this->_resolveInterWiki($wikiName,$wikiUri); 705 706 if ( !$isImage ) { 707 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName); 708 $link['class'] = "interwiki iw_$class"; 709 } else { 710 $link['class'] = 'media'; 711 } 712 713 //do we stay at the same server? Use local target 714 if( strpos($url,DOKU_URL) === 0 ){ 715 $link['target'] = $conf['target']['wiki']; 716 } 717 718 $link['url'] = $url; 719 $link['title'] = htmlspecialchars($link['url']); 720 721 //output formatted 722 $this->doc .= $this->_formatLink($link); 723 } 724 725 /** 726 */ 727 function windowssharelink($url, $name = null) { 728 global $conf; 729 global $lang; 730 //simple setup 731 $link['target'] = $conf['target']['windows']; 732 $link['pre'] = ''; 733 $link['suf'] = ''; 734 $link['style'] = ''; 735 736 $link['name'] = $this->_getLinkTitle($name, $url, $isImage); 737 if ( !$isImage ) { 738 $link['class'] = 'windows'; 739 } else { 740 $link['class'] = 'media'; 741 } 742 743 $link['title'] = $this->_xmlEntities($url); 744 $url = str_replace('\\','/',$url); 745 $url = 'file:///'.$url; 746 $link['url'] = $url; 747 748 //output formatted 749 $this->doc .= $this->_formatLink($link); 750 } 751 752 function emaillink($address, $name = null) { 753 global $conf; 754 //simple setup 755 $link = array(); 756 $link['target'] = ''; 757 $link['pre'] = ''; 758 $link['suf'] = ''; 759 $link['style'] = ''; 760 $link['more'] = ''; 761 762 $name = $this->_getLinkTitle($name, '', $isImage); 763 if ( !$isImage ) { 764 $link['class']='mail'; 765 } else { 766 $link['class']='media'; 767 } 768 769 $address = $this->_xmlEntities($address); 770 $address = obfuscate($address); 771 $title = $address; 772 773 if(empty($name)){ 774 $name = $address; 775 } 776 777 if($conf['mailguard'] == 'visible') $address = rawurlencode($address); 778 779 $link['url'] = 'mailto:'.$address; 780 $link['name'] = $name; 781 $link['title'] = $title; 782 783 //output formatted 784 $this->doc .= $this->_formatLink($link); 785 } 786 787 function internalmedia ($src, $title=null, $align=null, $width=null, 788 $height=null, $cache=null, $linking=null, $return=NULL) { 789 global $ID; 790 list($src,$hash) = explode('#',$src,2); 791 resolve_mediaid(getNS($ID),$src, $exists); 792 793 $noLink = false; 794 $render = ($linking == 'linkonly') ? false : true; 795 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render); 796 797 list($ext,$mime,$dl) = mimetype($src,false); 798 if(substr($mime,0,5) == 'image' && $render){ 799 $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache,'rev'=>$this->_getLastMediaRevisionAt($src)),($linking=='direct')); 800 }elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render){ 801 // don't link movies 802 $noLink = true; 803 }else{ 804 // add file icons 805 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 806 $link['class'] .= ' mediafile mf_'.$class; 807 $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache,'rev'=>$this->_getLastMediaRevisionAt($src)),true); 808 if ($exists) $link['title'] .= ' (' . filesize_h(filesize(mediaFN($src))).')'; 809 } 810 811 if($hash) $link['url'] .= '#'.$hash; 812 813 //markup non existing files 814 if (!$exists) { 815 $link['class'] .= ' wikilink2'; 816 } 817 818 //output formatted 819 if ($return) { 820 if ($linking == 'nolink' || $noLink) return $link['name']; 821 else return $this->_formatLink($link); 822 } else { 823 if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; 824 else $this->doc .= $this->_formatLink($link); 825 } 826 } 827 828 function externalmedia ($src, $title=null, $align=null, $width=null, 829 $height=null, $cache=null, $linking=null) { 830 list($src,$hash) = explode('#',$src,2); 831 $noLink = false; 832 $render = ($linking == 'linkonly') ? false : true; 833 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render); 834 835 $link['url'] = ml($src,array('cache'=>$cache)); 836 837 list($ext,$mime,$dl) = mimetype($src,false); 838 if(substr($mime,0,5) == 'image' && $render){ 839 // link only jpeg images 840 // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true; 841 }elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render){ 842 // don't link movies 843 $noLink = true; 844 }else{ 845 // add file icons 846 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 847 $link['class'] .= ' mediafile mf_'.$class; 848 } 849 850 if($hash) $link['url'] .= '#'.$hash; 851 852 //output formatted 853 if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; 854 else $this->doc .= $this->_formatLink($link); 855 } 856 857 /** 858 * Renders an RSS feed 859 * 860 * @author Andreas Gohr <andi@splitbrain.org> 861 */ 862 function rss ($url,$params){ 863 global $lang; 864 global $conf; 865 866 require_once(DOKU_INC.'inc/FeedParser.php'); 867 $feed = new FeedParser(); 868 $feed->set_feed_url($url); 869 870 //disable warning while fetching 871 if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); } 872 $rc = $feed->init(); 873 if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); } 874 875 //decide on start and end 876 if($params['reverse']){ 877 $mod = -1; 878 $start = $feed->get_item_quantity()-1; 879 $end = $start - ($params['max']); 880 $end = ($end < -1) ? -1 : $end; 881 }else{ 882 $mod = 1; 883 $start = 0; 884 $end = $feed->get_item_quantity(); 885 $end = ($end > $params['max']) ? $params['max'] : $end; 886 } 887 888 $this->doc .= '<ul class="rss">'; 889 if($rc){ 890 for ($x = $start; $x != $end; $x += $mod) { 891 $item = $feed->get_item($x); 892 $this->doc .= '<li><div class="li">'; 893 // support feeds without links 894 $lnkurl = $item->get_permalink(); 895 if($lnkurl){ 896 // title is escaped by SimplePie, we unescape here because it 897 // is escaped again in externallink() FS#1705 898 $this->externallink($item->get_permalink(), 899 html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')); 900 }else{ 901 $this->doc .= ' '.$item->get_title(); 902 } 903 if($params['author']){ 904 $author = $item->get_author(0); 905 if($author){ 906 $name = $author->get_name(); 907 if(!$name) $name = $author->get_email(); 908 if($name) $this->doc .= ' '.$lang['by'].' '.$name; 909 } 910 } 911 if($params['date']){ 912 $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')'; 913 } 914 if($params['details']){ 915 $this->doc .= '<div class="detail">'; 916 if($conf['htmlok']){ 917 $this->doc .= $item->get_description(); 918 }else{ 919 $this->doc .= strip_tags($item->get_description()); 920 } 921 $this->doc .= '</div>'; 922 } 923 924 $this->doc .= '</div></li>'; 925 } 926 }else{ 927 $this->doc .= '<li><div class="li">'; 928 $this->doc .= '<em>'.$lang['rssfailed'].'</em>'; 929 $this->externallink($url); 930 if($conf['allowdebug']){ 931 $this->doc .= '<!--'.hsc($feed->error).'-->'; 932 } 933 $this->doc .= '</div></li>'; 934 } 935 $this->doc .= '</ul>'; 936 } 937 938 // $numrows not yet implemented 939 function table_open($maxcols = null, $numrows = null, $pos = null){ 940 global $lang; 941 // initialize the row counter used for classes 942 $this->_counter['row_counter'] = 0; 943 $class = 'table'; 944 if ($pos !== null) { 945 $class .= ' ' . $this->startSectionEdit($pos, 'table'); 946 } 947 $this->doc .= '<div class="' . $class . '"><table class="inline">' . 948 DOKU_LF; 949 } 950 951 function table_close($pos = null){ 952 $this->doc .= '</table></div>'.DOKU_LF; 953 if ($pos !== null) { 954 $this->finishSectionEdit($pos); 955 } 956 } 957 958 function tablerow_open(){ 959 // initialize the cell counter used for classes 960 $this->_counter['cell_counter'] = 0; 961 $class = 'row' . $this->_counter['row_counter']++; 962 $this->doc .= DOKU_TAB . '<tr class="'.$class.'">' . DOKU_LF . DOKU_TAB . DOKU_TAB; 963 } 964 965 function tablerow_close(){ 966 $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF; 967 } 968 969 function tableheader_open($colspan = 1, $align = null, $rowspan = 1){ 970 $class = 'class="col' . $this->_counter['cell_counter']++; 971 if ( !is_null($align) ) { 972 $class .= ' '.$align.'align'; 973 } 974 $class .= '"'; 975 $this->doc .= '<th ' . $class; 976 if ( $colspan > 1 ) { 977 $this->_counter['cell_counter'] += $colspan-1; 978 $this->doc .= ' colspan="'.$colspan.'"'; 979 } 980 if ( $rowspan > 1 ) { 981 $this->doc .= ' rowspan="'.$rowspan.'"'; 982 } 983 $this->doc .= '>'; 984 } 985 986 function tableheader_close(){ 987 $this->doc .= '</th>'; 988 } 989 990 function tablecell_open($colspan = 1, $align = null, $rowspan = 1){ 991 $class = 'class="col' . $this->_counter['cell_counter']++; 992 if ( !is_null($align) ) { 993 $class .= ' '.$align.'align'; 994 } 995 $class .= '"'; 996 $this->doc .= '<td '.$class; 997 if ( $colspan > 1 ) { 998 $this->_counter['cell_counter'] += $colspan-1; 999 $this->doc .= ' colspan="'.$colspan.'"'; 1000 } 1001 if ( $rowspan > 1 ) { 1002 $this->doc .= ' rowspan="'.$rowspan.'"'; 1003 } 1004 $this->doc .= '>'; 1005 } 1006 1007 function tablecell_close(){ 1008 $this->doc .= '</td>'; 1009 } 1010 1011 //---------------------------------------------------------- 1012 // Utils 1013 1014 /** 1015 * Build a link 1016 * 1017 * Assembles all parts defined in $link returns HTML for the link 1018 * 1019 * @author Andreas Gohr <andi@splitbrain.org> 1020 */ 1021 function _formatLink($link){ 1022 //make sure the url is XHTML compliant (skip mailto) 1023 if(substr($link['url'],0,7) != 'mailto:'){ 1024 $link['url'] = str_replace('&','&',$link['url']); 1025 $link['url'] = str_replace('&amp;','&',$link['url']); 1026 } 1027 //remove double encodings in titles 1028 $link['title'] = str_replace('&amp;','&',$link['title']); 1029 1030 // be sure there are no bad chars in url or title 1031 // (we can't do this for name because it can contain an img tag) 1032 $link['url'] = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22')); 1033 $link['title'] = strtr($link['title'],array('>'=>'>','<'=>'<','"'=>'"')); 1034 1035 $ret = ''; 1036 $ret .= $link['pre']; 1037 $ret .= '<a href="'.$link['url'].'"'; 1038 if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"'; 1039 if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"'; 1040 if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"'; 1041 if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"'; 1042 if(!empty($link['rel'])) $ret .= ' rel="'.$link['rel'].'"'; 1043 if(!empty($link['more'])) $ret .= ' '.$link['more']; 1044 $ret .= '>'; 1045 $ret .= $link['name']; 1046 $ret .= '</a>'; 1047 $ret .= $link['suf']; 1048 return $ret; 1049 } 1050 1051 /** 1052 * Renders internal and external media 1053 * 1054 * @author Andreas Gohr <andi@splitbrain.org> 1055 */ 1056 function _media ($src, $title=null, $align=null, $width=null, 1057 $height=null, $cache=null, $render = true) { 1058 1059 $ret = ''; 1060 1061 list($ext,$mime,$dl) = mimetype($src); 1062 if(substr($mime,0,5) == 'image'){ 1063 // first get the $title 1064 if (!is_null($title)) { 1065 $title = $this->_xmlEntities($title); 1066 }elseif($ext == 'jpg' || $ext == 'jpeg'){ 1067 //try to use the caption from IPTC/EXIF 1068 require_once(DOKU_INC.'inc/JpegMeta.php'); 1069 $jpeg =new JpegMeta(mediaFN($src)); 1070 if($jpeg !== false) $cap = $jpeg->getTitle(); 1071 if($cap){ 1072 $title = $this->_xmlEntities($cap); 1073 } 1074 } 1075 if (!$render) { 1076 // if the picture is not supposed to be rendered 1077 // return the title of the picture 1078 if (!$title) { 1079 // just show the sourcename 1080 $title = $this->_xmlEntities(utf8_basename(noNS($src))); 1081 } 1082 return $title; 1083 } 1084 //add image tag 1085 $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache,'rev'=>$this->_getLastMediaRevisionAt($src))).'"'; 1086 $ret .= ' class="media'.$align.'"'; 1087 1088 if ($title) { 1089 $ret .= ' title="' . $title . '"'; 1090 $ret .= ' alt="' . $title .'"'; 1091 }else{ 1092 $ret .= ' alt=""'; 1093 } 1094 1095 if ( !is_null($width) ) 1096 $ret .= ' width="'.$this->_xmlEntities($width).'"'; 1097 1098 if ( !is_null($height) ) 1099 $ret .= ' height="'.$this->_xmlEntities($height).'"'; 1100 1101 $ret .= ' />'; 1102 1103 }elseif(media_supportedav($mime, 'video')){ 1104 // first get the $title 1105 if (!is_null($title)) { 1106 $title = $this->_xmlEntities($title); 1107 } 1108 if (!$title) { 1109 // just show the sourcename 1110 $title = $this->_xmlEntities(utf8_basename(noNS($src))); 1111 } 1112 if (!$render) { 1113 // if the video is not supposed to be rendered 1114 // return the title of the video 1115 return $title; 1116 } 1117 1118 $att = array(); 1119 $att['class'] = "media$align"; 1120 1121 //add video(s) 1122 $ret .= $this->_video($src, $width, $height, $att); 1123 1124 }elseif(media_supportedav($mime, 'audio')){ 1125 // first get the $title 1126 if (!is_null($title)) { 1127 $title = $this->_xmlEntities($title); 1128 } 1129 if (!$title) { 1130 // just show the sourcename 1131 $title = $this->_xmlEntities(utf8_basename(noNS($src))); 1132 } 1133 if (!$render) { 1134 // if the video is not supposed to be rendered 1135 // return the title of the video 1136 return $title; 1137 } 1138 1139 $att = array(); 1140 $att['class'] = "media$align"; 1141 1142 //add audio 1143 $ret .= $this->_audio($src, $att); 1144 1145 }elseif($mime == 'application/x-shockwave-flash'){ 1146 if (!$render) { 1147 // if the flash is not supposed to be rendered 1148 // return the title of the flash 1149 if (!$title) { 1150 // just show the sourcename 1151 $title = utf8_basename(noNS($src)); 1152 } 1153 return $this->_xmlEntities($title); 1154 } 1155 1156 $att = array(); 1157 $att['class'] = "media$align"; 1158 if($align == 'right') $att['align'] = 'right'; 1159 if($align == 'left') $att['align'] = 'left'; 1160 $ret .= html_flashobject(ml($src,array('cache'=>$cache),true,'&'),$width,$height, 1161 array('quality' => 'high'), 1162 null, 1163 $att, 1164 $this->_xmlEntities($title)); 1165 }elseif($title){ 1166 // well at least we have a title to display 1167 $ret .= $this->_xmlEntities($title); 1168 }else{ 1169 // just show the sourcename 1170 $ret .= $this->_xmlEntities(utf8_basename(noNS($src))); 1171 } 1172 1173 return $ret; 1174 } 1175 1176 function _xmlEntities($string) { 1177 return htmlspecialchars($string,ENT_QUOTES,'UTF-8'); 1178 } 1179 1180 /** 1181 * Creates a linkid from a headline 1182 * 1183 * @param string $title The headline title 1184 * @param boolean $create Create a new unique ID? 1185 * @author Andreas Gohr <andi@splitbrain.org> 1186 */ 1187 function _headerToLink($title,$create=false) { 1188 if($create){ 1189 return sectionID($title,$this->headers); 1190 }else{ 1191 $check = false; 1192 return sectionID($title,$check); 1193 } 1194 } 1195 1196 /** 1197 * Construct a title and handle images in titles 1198 * 1199 * @author Harry Fuecks <hfuecks@gmail.com> 1200 */ 1201 function _getLinkTitle($title, $default, & $isImage, $id=null, $linktype='content') { 1202 global $conf; 1203 1204 $isImage = false; 1205 if ( is_array($title) ) { 1206 $isImage = true; 1207 return $this->_imageTitle($title); 1208 } elseif ( is_null($title) || trim($title)=='') { 1209 if (useHeading($linktype) && $id) { 1210 $heading = p_get_first_heading($id); 1211 if ($heading) { 1212 return $this->_xmlEntities($heading); 1213 } 1214 } 1215 return $this->_xmlEntities($default); 1216 } else { 1217 return $this->_xmlEntities($title); 1218 } 1219 } 1220 1221 /** 1222 * Returns an HTML code for images used in link titles 1223 * 1224 * @todo Resolve namespace on internal images 1225 * @author Andreas Gohr <andi@splitbrain.org> 1226 */ 1227 function _imageTitle($img) { 1228 global $ID; 1229 1230 // some fixes on $img['src'] 1231 // see internalmedia() and externalmedia() 1232 list($img['src'],$hash) = explode('#',$img['src'],2); 1233 if ($img['type'] == 'internalmedia') { 1234 resolve_mediaid(getNS($ID),$img['src'],$exists); 1235 } 1236 1237 return $this->_media($img['src'], 1238 $img['title'], 1239 $img['align'], 1240 $img['width'], 1241 $img['height'], 1242 $img['cache']); 1243 } 1244 1245 /** 1246 * _getMediaLinkConf is a helperfunction to internalmedia() and externalmedia() 1247 * which returns a basic link to a media. 1248 * 1249 * @author Pierre Spring <pierre.spring@liip.ch> 1250 * @param string $src 1251 * @param string $title 1252 * @param string $align 1253 * @param string $width 1254 * @param string $height 1255 * @param string $cache 1256 * @param string $render 1257 * @access protected 1258 * @return array 1259 */ 1260 function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) { 1261 global $conf; 1262 1263 $link = array(); 1264 $link['class'] = 'media'; 1265 $link['style'] = ''; 1266 $link['pre'] = ''; 1267 $link['suf'] = ''; 1268 $link['more'] = ''; 1269 $link['target'] = $conf['target']['media']; 1270 $link['title'] = $this->_xmlEntities($src); 1271 $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render); 1272 1273 return $link; 1274 } 1275 1276 1277 /** 1278 * Embed video(s) in HTML 1279 * 1280 * @author Anika Henke <anika@selfthinker.org> 1281 * 1282 * @param string $src - ID of video to embed 1283 * @param int $width - width of the video in pixels 1284 * @param int $height - height of the video in pixels 1285 * @param array $atts - additional attributes for the <video> tag 1286 * @return string 1287 */ 1288 function _video($src,$width,$height,$atts=null){ 1289 1290 // prepare width and height 1291 if(is_null($atts)) $atts = array(); 1292 $atts['width'] = (int) $width; 1293 $atts['height'] = (int) $height; 1294 if(!$atts['width']) $atts['width'] = 320; 1295 if(!$atts['height']) $atts['height'] = 240; 1296 1297 // prepare alternative formats 1298 $extensions = array('webm', 'ogv', 'mp4'); 1299 $alternatives = media_alternativefiles($src, $extensions); 1300 $poster = media_alternativefiles($src, array('jpg', 'png'), true); 1301 $posterUrl = ''; 1302 if (!empty($poster)) { 1303 $posterUrl = ml(reset($poster),array('cache'=>$cache),true,'&'); 1304 } 1305 1306 $out = ''; 1307 // open video tag 1308 $out .= '<video '.buildAttributes($atts).' controls="controls"'; 1309 if ($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"'; 1310 $out .= '>'.NL; 1311 $fallback = ''; 1312 1313 // output source for each alternative video format 1314 foreach($alternatives as $mime => $file) { 1315 $url = ml($file,array('cache'=>$cache),true,'&'); 1316 $title = $this->_xmlEntities(utf8_basename(noNS($file))); 1317 1318 $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL; 1319 // alternative content (just a link to the file) 1320 $fallback .= $this->internalmedia($file, $title, NULL, NULL, NULL, $cache=NULL, $linking='linkonly', $return=true); 1321 } 1322 1323 // finish 1324 $out .= $fallback; 1325 $out .= '</video>'.NL; 1326 return $out; 1327 } 1328 1329 /** 1330 * Embed audio in HTML 1331 * 1332 * @author Anika Henke <anika@selfthinker.org> 1333 * 1334 * @param string $src - ID of audio to embed 1335 * @param array $atts - additional attributes for the <audio> tag 1336 * @return string 1337 */ 1338 function _audio($src,$atts=null){ 1339 1340 // prepare alternative formats 1341 $extensions = array('ogg', 'mp3', 'wav'); 1342 $alternatives = media_alternativefiles($src, $extensions); 1343 1344 $out = ''; 1345 // open audio tag 1346 $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL; 1347 $fallback = ''; 1348 1349 // output source for each alternative audio format 1350 foreach($alternatives as $mime => $file) { 1351 $url = ml($file,array('cache'=>$cache),true,'&'); 1352 $title = $this->_xmlEntities(utf8_basename(noNS($file))); 1353 1354 $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL; 1355 // alternative content (just a link to the file) 1356 $fallback .= $this->internalmedia($file, $title, NULL, NULL, NULL, $cache=NULL, $linking='linkonly', $return=true); 1357 } 1358 1359 // finish 1360 $out .= $fallback; 1361 $out .= '</audio>'.NL; 1362 return $out; 1363 } 1364 1365 /** 1366 * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media() 1367 * which returns an existing media revision less or equal to rev or date_at 1368 * 1369 * @author lisps 1370 * @param string $media_id 1371 * @access protected 1372 * @return string revision ('' for current) 1373 */ 1374 function _getLastMediaRevisionAt($media_id){ 1375 if(!$this->date_at || media_isexternal($media_id)) return ''; 1376 $pagelog = new MediaChangeLog($media_id); 1377 return $pagelog->getLastRevisionAt($this->date_at); 1378 } 1379 1380} 1381 1382//Setup VIM: ex: et ts=4 : 1383