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) { 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' && $render){ 801 // don't link flash 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 ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; 820 else $this->doc .= $this->_formatLink($link); 821 } 822 823 function externalmedia ($src, $title=null, $align=null, $width=null, 824 $height=null, $cache=null, $linking=null) { 825 list($src,$hash) = explode('#',$src,2); 826 $noLink = false; 827 $render = ($linking == 'linkonly') ? false : true; 828 $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render); 829 830 $link['url'] = ml($src,array('cache'=>$cache)); 831 832 list($ext,$mime,$dl) = mimetype($src,false); 833 if(substr($mime,0,5) == 'image' && $render){ 834 // link only jpeg images 835 // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true; 836 }elseif($mime == 'application/x-shockwave-flash' && $render){ 837 // don't link flash movies 838 $noLink = true; 839 }else{ 840 // add file icons 841 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 842 $link['class'] .= ' mediafile mf_'.$class; 843 } 844 845 if($hash) $link['url'] .= '#'.$hash; 846 847 //output formatted 848 if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; 849 else $this->doc .= $this->_formatLink($link); 850 } 851 852 /** 853 * Renders an RSS feed 854 * 855 * @author Andreas Gohr <andi@splitbrain.org> 856 */ 857 function rss ($url,$params){ 858 global $lang; 859 global $conf; 860 861 require_once(DOKU_INC.'inc/FeedParser.php'); 862 $feed = new FeedParser(); 863 $feed->set_feed_url($url); 864 865 //disable warning while fetching 866 if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); } 867 $rc = $feed->init(); 868 if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); } 869 870 //decide on start and end 871 if($params['reverse']){ 872 $mod = -1; 873 $start = $feed->get_item_quantity()-1; 874 $end = $start - ($params['max']); 875 $end = ($end < -1) ? -1 : $end; 876 }else{ 877 $mod = 1; 878 $start = 0; 879 $end = $feed->get_item_quantity(); 880 $end = ($end > $params['max']) ? $params['max'] : $end; 881 } 882 883 $this->doc .= '<ul class="rss">'; 884 if($rc){ 885 for ($x = $start; $x != $end; $x += $mod) { 886 $item = $feed->get_item($x); 887 $this->doc .= '<li><div class="li">'; 888 // support feeds without links 889 $lnkurl = $item->get_permalink(); 890 if($lnkurl){ 891 // title is escaped by SimplePie, we unescape here because it 892 // is escaped again in externallink() FS#1705 893 $this->externallink($item->get_permalink(), 894 html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')); 895 }else{ 896 $this->doc .= ' '.$item->get_title(); 897 } 898 if($params['author']){ 899 $author = $item->get_author(0); 900 if($author){ 901 $name = $author->get_name(); 902 if(!$name) $name = $author->get_email(); 903 if($name) $this->doc .= ' '.$lang['by'].' '.$name; 904 } 905 } 906 if($params['date']){ 907 $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')'; 908 } 909 if($params['details']){ 910 $this->doc .= '<div class="detail">'; 911 if($conf['htmlok']){ 912 $this->doc .= $item->get_description(); 913 }else{ 914 $this->doc .= strip_tags($item->get_description()); 915 } 916 $this->doc .= '</div>'; 917 } 918 919 $this->doc .= '</div></li>'; 920 } 921 }else{ 922 $this->doc .= '<li><div class="li">'; 923 $this->doc .= '<em>'.$lang['rssfailed'].'</em>'; 924 $this->externallink($url); 925 if($conf['allowdebug']){ 926 $this->doc .= '<!--'.hsc($feed->error).'-->'; 927 } 928 $this->doc .= '</div></li>'; 929 } 930 $this->doc .= '</ul>'; 931 } 932 933 // $numrows not yet implemented 934 function table_open($maxcols = null, $numrows = null, $pos = null){ 935 global $lang; 936 // initialize the row counter used for classes 937 $this->_counter['row_counter'] = 0; 938 $class = 'table'; 939 if ($pos !== null) { 940 $class .= ' ' . $this->startSectionEdit($pos, 'table'); 941 } 942 $this->doc .= '<div class="' . $class . '"><table class="inline">' . 943 DOKU_LF; 944 } 945 946 function table_close($pos = null){ 947 $this->doc .= '</table></div>'.DOKU_LF; 948 if ($pos !== null) { 949 $this->finishSectionEdit($pos); 950 } 951 } 952 953 function tablerow_open(){ 954 // initialize the cell counter used for classes 955 $this->_counter['cell_counter'] = 0; 956 $class = 'row' . $this->_counter['row_counter']++; 957 $this->doc .= DOKU_TAB . '<tr class="'.$class.'">' . DOKU_LF . DOKU_TAB . DOKU_TAB; 958 } 959 960 function tablerow_close(){ 961 $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF; 962 } 963 964 function tableheader_open($colspan = 1, $align = null, $rowspan = 1){ 965 $class = 'class="col' . $this->_counter['cell_counter']++; 966 if ( !is_null($align) ) { 967 $class .= ' '.$align.'align'; 968 } 969 $class .= '"'; 970 $this->doc .= '<th ' . $class; 971 if ( $colspan > 1 ) { 972 $this->_counter['cell_counter'] += $colspan-1; 973 $this->doc .= ' colspan="'.$colspan.'"'; 974 } 975 if ( $rowspan > 1 ) { 976 $this->doc .= ' rowspan="'.$rowspan.'"'; 977 } 978 $this->doc .= '>'; 979 } 980 981 function tableheader_close(){ 982 $this->doc .= '</th>'; 983 } 984 985 function tablecell_open($colspan = 1, $align = null, $rowspan = 1){ 986 $class = 'class="col' . $this->_counter['cell_counter']++; 987 if ( !is_null($align) ) { 988 $class .= ' '.$align.'align'; 989 } 990 $class .= '"'; 991 $this->doc .= '<td '.$class; 992 if ( $colspan > 1 ) { 993 $this->_counter['cell_counter'] += $colspan-1; 994 $this->doc .= ' colspan="'.$colspan.'"'; 995 } 996 if ( $rowspan > 1 ) { 997 $this->doc .= ' rowspan="'.$rowspan.'"'; 998 } 999 $this->doc .= '>'; 1000 } 1001 1002 function tablecell_close(){ 1003 $this->doc .= '</td>'; 1004 } 1005 1006 //---------------------------------------------------------- 1007 // Utils 1008 1009 /** 1010 * Build a link 1011 * 1012 * Assembles all parts defined in $link returns HTML for the link 1013 * 1014 * @author Andreas Gohr <andi@splitbrain.org> 1015 */ 1016 function _formatLink($link){ 1017 //make sure the url is XHTML compliant (skip mailto) 1018 if(substr($link['url'],0,7) != 'mailto:'){ 1019 $link['url'] = str_replace('&','&',$link['url']); 1020 $link['url'] = str_replace('&amp;','&',$link['url']); 1021 } 1022 //remove double encodings in titles 1023 $link['title'] = str_replace('&amp;','&',$link['title']); 1024 1025 // be sure there are no bad chars in url or title 1026 // (we can't do this for name because it can contain an img tag) 1027 $link['url'] = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22')); 1028 $link['title'] = strtr($link['title'],array('>'=>'>','<'=>'<','"'=>'"')); 1029 1030 $ret = ''; 1031 $ret .= $link['pre']; 1032 $ret .= '<a href="'.$link['url'].'"'; 1033 if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"'; 1034 if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"'; 1035 if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"'; 1036 if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"'; 1037 if(!empty($link['rel'])) $ret .= ' rel="'.$link['rel'].'"'; 1038 if(!empty($link['more'])) $ret .= ' '.$link['more']; 1039 $ret .= '>'; 1040 $ret .= $link['name']; 1041 $ret .= '</a>'; 1042 $ret .= $link['suf']; 1043 return $ret; 1044 } 1045 1046 /** 1047 * Renders internal and external media 1048 * 1049 * @author Andreas Gohr <andi@splitbrain.org> 1050 */ 1051 function _media ($src, $title=null, $align=null, $width=null, 1052 $height=null, $cache=null, $render = true) { 1053 1054 $ret = ''; 1055 list($ext,$mime,$dl) = mimetype($src); 1056 if(substr($mime,0,5) == 'image'){ 1057 // first get the $title 1058 if (!is_null($title)) { 1059 $title = $this->_xmlEntities($title); 1060 }elseif($ext == 'jpg' || $ext == 'jpeg'){ 1061 //try to use the caption from IPTC/EXIF 1062 require_once(DOKU_INC.'inc/JpegMeta.php'); 1063 $jpeg =new JpegMeta(mediaFN($src)); 1064 if($jpeg !== false) $cap = $jpeg->getTitle(); 1065 if($cap){ 1066 $title = $this->_xmlEntities($cap); 1067 } 1068 } 1069 if (!$render) { 1070 // if the picture is not supposed to be rendered 1071 // return the title of the picture 1072 if (!$title) { 1073 // just show the sourcename 1074 $title = $this->_xmlEntities(utf8_basename(noNS($src))); 1075 } 1076 return $title; 1077 } 1078 //add image tag 1079 $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache,'rev'=>$this->_getLastMediaRevisionAt($src))).'"'; 1080 $ret .= ' class="media'.$align.'"'; 1081 1082 if ($title) { 1083 $ret .= ' title="' . $title . '"'; 1084 $ret .= ' alt="' . $title .'"'; 1085 }else{ 1086 $ret .= ' alt=""'; 1087 } 1088 1089 if ( !is_null($width) ) 1090 $ret .= ' width="'.$this->_xmlEntities($width).'"'; 1091 1092 if ( !is_null($height) ) 1093 $ret .= ' height="'.$this->_xmlEntities($height).'"'; 1094 1095 $ret .= ' />'; 1096 1097 }elseif($mime == 'application/x-shockwave-flash'){ 1098 if (!$render) { 1099 // if the flash is not supposed to be rendered 1100 // return the title of the flash 1101 if (!$title) { 1102 // just show the sourcename 1103 $title = utf8_basename(noNS($src)); 1104 } 1105 return $this->_xmlEntities($title); 1106 } 1107 1108 $att = array(); 1109 $att['class'] = "media$align"; 1110 if($align == 'right') $att['align'] = 'right'; 1111 if($align == 'left') $att['align'] = 'left'; 1112 $ret .= html_flashobject(ml($src,array('cache'=>$cache),true,'&'),$width,$height, 1113 array('quality' => 'high'), 1114 null, 1115 $att, 1116 $this->_xmlEntities($title)); 1117 }elseif($title){ 1118 // well at least we have a title to display 1119 $ret .= $this->_xmlEntities($title); 1120 }else{ 1121 // just show the sourcename 1122 $ret .= $this->_xmlEntities(utf8_basename(noNS($src))); 1123 } 1124 1125 return $ret; 1126 } 1127 1128 function _xmlEntities($string) { 1129 return htmlspecialchars($string,ENT_QUOTES,'UTF-8'); 1130 } 1131 1132 /** 1133 * Creates a linkid from a headline 1134 * 1135 * @param string $title The headline title 1136 * @param boolean $create Create a new unique ID? 1137 * @author Andreas Gohr <andi@splitbrain.org> 1138 */ 1139 function _headerToLink($title,$create=false) { 1140 if($create){ 1141 return sectionID($title,$this->headers); 1142 }else{ 1143 $check = false; 1144 return sectionID($title,$check); 1145 } 1146 } 1147 1148 /** 1149 * Construct a title and handle images in titles 1150 * 1151 * @author Harry Fuecks <hfuecks@gmail.com> 1152 */ 1153 function _getLinkTitle($title, $default, & $isImage, $id=null, $linktype='content') { 1154 global $conf; 1155 1156 $isImage = false; 1157 if ( is_array($title) ) { 1158 $isImage = true; 1159 return $this->_imageTitle($title); 1160 } elseif ( is_null($title) || trim($title)=='') { 1161 if (useHeading($linktype) && $id) { 1162 $heading = p_get_first_heading($id); 1163 if ($heading) { 1164 return $this->_xmlEntities($heading); 1165 } 1166 } 1167 return $this->_xmlEntities($default); 1168 } else { 1169 return $this->_xmlEntities($title); 1170 } 1171 } 1172 1173 /** 1174 * Returns an HTML code for images used in link titles 1175 * 1176 * @todo Resolve namespace on internal images 1177 * @author Andreas Gohr <andi@splitbrain.org> 1178 */ 1179 function _imageTitle($img) { 1180 global $ID; 1181 1182 // some fixes on $img['src'] 1183 // see internalmedia() and externalmedia() 1184 list($img['src'],$hash) = explode('#',$img['src'],2); 1185 if ($img['type'] == 'internalmedia') { 1186 resolve_mediaid(getNS($ID),$img['src'],$exists); 1187 } 1188 1189 return $this->_media($img['src'], 1190 $img['title'], 1191 $img['align'], 1192 $img['width'], 1193 $img['height'], 1194 $img['cache']); 1195 } 1196 1197 /** 1198 * _getMediaLinkConf is a helperfunction to internalmedia() and externalmedia() 1199 * which returns a basic link to a media. 1200 * 1201 * @author Pierre Spring <pierre.spring@liip.ch> 1202 * @param string $src 1203 * @param string $title 1204 * @param string $align 1205 * @param string $width 1206 * @param string $height 1207 * @param string $cache 1208 * @param string $render 1209 * @access protected 1210 * @return array 1211 */ 1212 function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) { 1213 global $conf; 1214 1215 $link = array(); 1216 $link['class'] = 'media'; 1217 $link['style'] = ''; 1218 $link['pre'] = ''; 1219 $link['suf'] = ''; 1220 $link['more'] = ''; 1221 $link['target'] = $conf['target']['media']; 1222 $link['title'] = $this->_xmlEntities($src); 1223 $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render); 1224 1225 return $link; 1226 } 1227 1228 /** 1229 * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media() 1230 * which returns an existing media revision less or equal to rev or date_at 1231 * 1232 * @author lisps 1233 * @param string $media_id 1234 * @access protected 1235 * @return string revision ('' for current) 1236 */ 1237 function _getLastMediaRevisionAt($media_id){ 1238 if(!$this->date_at || media_isexternal($media_id)) return ''; 1239 $pagelog = new MediaChangeLog($media_id); 1240 return $pagelog->getLastRevisionAt($this->date_at); 1241 } 1242 1243 1244} 1245 1246//Setup VIM: ex: et ts=4 : 1247