1<?php 2/** 3 * Renderer for XHTML output 4 * 5 * @author Harry Fuecks <hfuecks@gmail.com> 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 10 11if ( !defined('DOKU_LF') ) { 12 // Some whitespace to help View > Source 13 define ('DOKU_LF',"\n"); 14} 15 16if ( !defined('DOKU_TAB') ) { 17 // Some whitespace to help View > Source 18 define ('DOKU_TAB',"\t"); 19} 20 21require_once DOKU_INC . 'inc/parser/renderer.php'; 22require_once DOKU_INC . 'inc/html.php'; 23 24/** 25 * The Renderer 26 */ 27class Doku_Renderer_xhtml extends Doku_Renderer { 28 29 // @access public 30 var $doc = ''; // will contain the whole document 31 var $toc = array(); // will contain the Table of Contents 32 33 34 var $headers = array(); 35 36 var $footnotes = array(); 37 38 var $acronyms = array(); 39 var $smileys = array(); 40 var $badwords = array(); 41 var $entities = array(); 42 var $interwiki = array(); 43 44 var $lastsec = 0; 45 46 var $store = ''; 47 48 function document_start() { 49 //reset some internals 50 $this->toc = array(); 51 $this->headers = array(); 52 } 53 54 function document_end() { 55 if ( count ($this->footnotes) > 0 ) { 56 $this->doc .= '<div class="footnotes">'.DOKU_LF; 57 58 $id = 0; 59 foreach ( $this->footnotes as $footnote ) { 60 $id++; // the number of the current footnote 61 62 // check its not a placeholder that indicates actual footnote text is elsewhere 63 if (substr($footnote, 0, 5) != "@@FNT") { 64 65 // open the footnote and set the anchor and backlink 66 $this->doc .= '<div class="fn">'; 67 $this->doc .= '<a href="#fnt__'.$id.'" id="fn__'.$id.'" name="fn__'.$id.'" class="fn_bot">'; 68 $this->doc .= $id.')</a> '.DOKU_LF; 69 70 // get any other footnotes that use the same markup 71 $alt = array_keys($this->footnotes, "@@FNT$id"); 72 73 if (count($alt)) { 74 foreach ($alt as $ref) { 75 // set anchor and backlink for the other footnotes 76 $this->doc .= ', <a href="#fnt__'.($ref+1).'" id="fn__'.($ref+1).'" name="fn__'.($ref+1).'" class="fn_bot">'; 77 $this->doc .= ($ref+1).')</a> '.DOKU_LF; 78 } 79 } 80 81 // add footnote markup and close this footnote 82 $this->doc .= $footnote; 83 $this->doc .= '</div>' . DOKU_LF; 84 } 85 } 86 $this->doc .= '</div>'.DOKU_LF; 87 } 88 89 // prepend the TOC 90 if($this->info['toc']){ 91 $this->doc = $this->render_TOC().$this->doc; 92 } 93 94 // make sure there are no empty paragraphs 95 $this->doc = preg_replace('#<p>\s*</p>#','',$this->doc); 96 } 97 98 /** 99 * Return the TOC rendered to XHTML 100 * 101 * @author Andreas Gohr <andi@splitbrain.org> 102 */ 103 function render_TOC(){ 104 if(count($this->toc) < 3) return ''; 105 global $lang; 106 $out = '<div class="toc">'.DOKU_LF; 107 $out .= '<div class="tocheader toctoggle" id="toc__header">'; 108 $out .= $lang['toc']; 109 $out .= '</div>'.DOKU_LF; 110 $out .= '<div id="toc__inside">'.DOKU_LF; 111 $out .= html_buildlist($this->toc,'toc',array($this,'_tocitem')); 112 $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF; 113 return $out; 114 } 115 116 /** 117 * Callback for html_buildlist 118 */ 119 function _tocitem($item){ 120 return '<span class="li"><a href="#'.$item['hid'].'" class="toc">'. 121 $this->_xmlEntities($item['title']).'</a></span>'; 122 } 123 124 function header($text, $level, $pos) { 125 global $conf; 126 127 // create a unique header id 128 $hid = $this->_headerToLink($text,'true'); 129 130 //handle TOC 131 if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){ 132 // the TOC is one of our standard ul list arrays ;-) 133 $this->toc[] = array( 'hid' => $hid, 134 'title' => $text, 135 'type' => 'ul', 136 'level' => $level-$conf['toptoclevel']+1); 137 } 138 139 // write the header 140 $this->doc .= DOKU_LF.'<h'.$level.'><a name="'.$hid.'" id="'.$hid.'">'; 141 $this->doc .= $this->_xmlEntities($text); 142 $this->doc .= "</a></h$level>".DOKU_LF; 143 } 144 145 /** 146 * Section edit marker is replaced by an edit button when 147 * the page is editable. Replacement done in 'inc/html.php#html_secedit' 148 * 149 * @author Andreas Gohr <andi@splitbrain.org> 150 * @author Ben Coburn <btcoburn@silicodon.net> 151 */ 152 function section_edit($start, $end, $level, $name) { 153 global $conf; 154 155 if ($start!=-1 && $level<=$conf['maxseclevel']) { 156 $name = str_replace('"', '', $name); 157 $this->doc .= '<!-- SECTION "'.$name.'" ['.$start.'-'.(($end===0)?'':$end).'] -->'; 158 } 159 } 160 161 function section_open($level) { 162 $this->doc .= "<div class=\"level$level\">".DOKU_LF; 163 } 164 165 function section_close() { 166 $this->doc .= DOKU_LF.'</div>'.DOKU_LF; 167 } 168 169 function cdata($text) { 170 $this->doc .= $this->_xmlEntities($text); 171 } 172 173 function p_open() { 174 $this->doc .= DOKU_LF.'<p>'.DOKU_LF; 175 } 176 177 function p_close() { 178 $this->doc .= DOKU_LF.'</p>'.DOKU_LF; 179 } 180 181 function linebreak() { 182 $this->doc .= '<br/>'.DOKU_LF; 183 } 184 185 function hr() { 186 $this->doc .= '<hr />'.DOKU_LF; 187 } 188 189 function strong_open() { 190 $this->doc .= '<strong>'; 191 } 192 193 function strong_close() { 194 $this->doc .= '</strong>'; 195 } 196 197 function emphasis_open() { 198 $this->doc .= '<em>'; 199 } 200 201 function emphasis_close() { 202 $this->doc .= '</em>'; 203 } 204 205 function underline_open() { 206 $this->doc .= '<em class="u">'; 207 } 208 209 function underline_close() { 210 $this->doc .= '</em>'; 211 } 212 213 function monospace_open() { 214 $this->doc .= '<code>'; 215 } 216 217 function monospace_close() { 218 $this->doc .= '</code>'; 219 } 220 221 function subscript_open() { 222 $this->doc .= '<sub>'; 223 } 224 225 function subscript_close() { 226 $this->doc .= '</sub>'; 227 } 228 229 function superscript_open() { 230 $this->doc .= '<sup>'; 231 } 232 233 function superscript_close() { 234 $this->doc .= '</sup>'; 235 } 236 237 function deleted_open() { 238 $this->doc .= '<del>'; 239 } 240 241 function deleted_close() { 242 $this->doc .= '</del>'; 243 } 244 245 /** 246 * Callback for footnote start syntax 247 * 248 * All following content will go to the footnote instead of 249 * the document. To achieve this the previous rendered content 250 * is moved to $store and $doc is cleared 251 * 252 * @author Andreas Gohr <andi@splitbrain.org> 253 */ 254 function footnote_open() { 255 256 // move current content to store and record footnote 257 $this->store = $this->doc; 258 $this->doc = ''; 259 } 260 261 /** 262 * Callback for footnote end syntax 263 * 264 * All rendered content is moved to the $footnotes array and the old 265 * content is restored from $store again 266 * 267 * @author Andreas Gohr 268 */ 269 function footnote_close() { 270 271 // recover footnote into the stack and restore old content 272 $footnote = $this->doc; 273 $this->doc = $this->store; 274 $this->store = ''; 275 276 // check to see if this footnote has been seen before 277 $i = array_search($footnote, $this->footnotes); 278 279 if ($i === false) { 280 // its a new footnote, add it to the $footnotes array 281 $id = count($this->footnotes)+1; 282 $this->footnotes[count($this->footnotes)] = $footnote; 283 } else { 284 // seen this one before, translate the index to an id and save a placeholder 285 $i++; 286 $id = count($this->footnotes)+1; 287 $this->footnotes[count($this->footnotes)] = "@@FNT".($i); 288 } 289 290 // output the footnote reference and link, incl. onmouseover for insitu footnote popup 291 $this->doc .= '<a href="#fn__'.$id.'" name="fnt__'.$id.'" id="fnt__'.$id.'" class="fn_top" onmouseover="fnt(\''.$id.'\', this, event);">'.$id.')</a>'; 292 } 293 294 function listu_open() { 295 $this->doc .= '<ul>'.DOKU_LF; 296 } 297 298 function listu_close() { 299 $this->doc .= '</ul>'.DOKU_LF; 300 } 301 302 function listo_open() { 303 $this->doc .= '<ol>'.DOKU_LF; 304 } 305 306 function listo_close() { 307 $this->doc .= '</ol>'.DOKU_LF; 308 } 309 310 function listitem_open($level) { 311 $this->doc .= '<li class="level'.$level.'">'; 312 } 313 314 function listitem_close() { 315 $this->doc .= '</li>'.DOKU_LF; 316 } 317 318 function listcontent_open() { 319 $this->doc .= '<div class="li">'; 320 } 321 322 function listcontent_close() { 323 $this->doc .= '</div>'.DOKU_LF; 324 } 325 326 function unformatted($text) { 327 $this->doc .= $this->_xmlEntities($text); 328 } 329 330 /** 331 * Execute PHP code if allowed 332 * 333 * @author Andreas Gohr <andi@splitbrain.org> 334 */ 335 function php($text) { 336 global $conf; 337 if($conf['phpok']){ 338 ob_start(); 339 eval($text); 340 $this->doc .= ob_get_contents(); 341 ob_end_clean(); 342 }else{ 343 $this->file($text); 344 } 345 } 346 347 /** 348 * Insert HTML if allowed 349 * 350 * @author Andreas Gohr <andi@splitbrain.org> 351 */ 352 function html($text) { 353 global $conf; 354 if($conf['htmlok']){ 355 $this->doc .= $text; 356 }else{ 357 $this->file($text); 358 } 359 } 360 361 function preformatted($text) { 362 $this->doc .= '<pre class="code">' . $this->_xmlEntities($text) . '</pre>'. DOKU_LF; 363 } 364 365 function file($text) { 366 $this->doc .= '<pre class="file">' . $this->_xmlEntities($text). '</pre>'. DOKU_LF; 367 } 368 369 function quote_open() { 370 $this->doc .= '<blockquote><div class="no">'.DOKU_LF; 371 } 372 373 function quote_close() { 374 $this->doc .= '</div></blockquote>'.DOKU_LF; 375 } 376 377 /** 378 * Callback for code text 379 * 380 * Uses GeSHi to highlight language syntax 381 * 382 * @author Andreas Gohr <andi@splitbrain.org> 383 */ 384 function code($text, $language = NULL) { 385 global $conf; 386 387 if ( is_null($language) ) { 388 $this->preformatted($text); 389 } else { 390 //strip leading blank line 391 $text = preg_replace('/^\s*?\n/','',$text); 392 // Handle with Geshi here 393 require_once(DOKU_INC . 'inc/geshi.php'); 394 $geshi = new GeSHi($text, strtolower($language), DOKU_INC . 'inc/geshi'); 395 $geshi->set_encoding('utf-8'); 396 $geshi->enable_classes(); 397 $geshi->set_header_type(GESHI_HEADER_PRE); 398 $geshi->set_overall_class("code $language"); 399 $geshi->set_link_target($conf['target']['extern']); 400 401 $text = $geshi->parse_code(); 402 $this->doc .= $text; 403 } 404 } 405 406 function acronym($acronym) { 407 408 if ( array_key_exists($acronym, $this->acronyms) ) { 409 410 $title = $this->_xmlEntities($this->acronyms[$acronym]); 411 412 $this->doc .= '<acronym title="'.$title 413 .'">'.$this->_xmlEntities($acronym).'</acronym>'; 414 415 } else { 416 $this->doc .= $this->_xmlEntities($acronym); 417 } 418 } 419 420 function smiley($smiley) { 421 if ( array_key_exists($smiley, $this->smileys) ) { 422 $title = $this->_xmlEntities($this->smileys[$smiley]); 423 $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley]. 424 '" class="middle" alt="'. 425 $this->_xmlEntities($smiley).'" />'; 426 } else { 427 $this->doc .= $this->_xmlEntities($smiley); 428 } 429 } 430 431 /* 432 * not used 433 function wordblock($word) { 434 if ( array_key_exists($word, $this->badwords) ) { 435 $this->doc .= '** BLEEP **'; 436 } else { 437 $this->doc .= $this->_xmlEntities($word); 438 } 439 } 440 */ 441 442 function entity($entity) { 443 if ( array_key_exists($entity, $this->entities) ) { 444 $this->doc .= $this->entities[$entity]; 445 } else { 446 $this->doc .= $this->_xmlEntities($entity); 447 } 448 } 449 450 function multiplyentity($x, $y) { 451 $this->doc .= "$x×$y"; 452 } 453 454 function singlequoteopening() { 455 $this->doc .= "‘"; 456 } 457 458 function singlequoteclosing() { 459 $this->doc .= "’"; 460 } 461 462 function doublequoteopening() { 463 $this->doc .= "“"; 464 } 465 466 function doublequoteclosing() { 467 $this->doc .= "”"; 468 } 469 470 /** 471 */ 472 function camelcaselink($link) { 473 $this->internallink($link,$link); 474 } 475 476 477 function locallink($hash, $name = NULL){ 478 global $ID; 479 $name = $this->_getLinkTitle($name, $hash, $isImage); 480 $hash = $this->_headerToLink($hash); 481 $title = $ID.' ↵'; 482 $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">'; 483 $this->doc .= $name; 484 $this->doc .= '</a>'; 485 } 486 487 /** 488 * Render an internal Wiki Link 489 * 490 * $search and $returnonly are not for the renderer but are used 491 * elsewhere - no need to implement them in other renderers 492 * 493 * @author Andreas Gohr <andi@splitbrain.org> 494 */ 495 function internallink($id, $name = NULL, $search=NULL,$returnonly=false) { 496 global $conf; 497 global $ID; 498 // default name is based on $id as given 499 $default = $this->_simpleTitle($id); 500 // now first resolve and clean up the $id 501 resolve_pageid(getNS($ID),$id,$exists); 502 $name = $this->_getLinkTitle($name, $default, $isImage, $id); 503 if ( !$isImage ) { 504 if ( $exists ) { 505 $class='wikilink1'; 506 } else { 507 $class='wikilink2'; 508 } 509 } else { 510 $class='media'; 511 } 512 513 //keep hash anchor 514 list($id,$hash) = split('#',$id,2); 515 516 //prepare for formating 517 $link['target'] = $conf['target']['wiki']; 518 $link['style'] = ''; 519 $link['pre'] = ''; 520 $link['suf'] = ''; 521 // highlight link to current page 522 if ($id == $ID) { 523 $link['pre'] = '<span class="curid">'; 524 $link['suf'] = '</span>'; 525 } 526 $link['more'] = ''; 527 $link['class'] = $class; 528 $link['url'] = wl($id); 529 $link['name'] = $name; 530 $link['title'] = $id; 531 //add search string 532 if($search){ 533 ($conf['userewrite']) ? $link['url'].='?s=' : $link['url'].='&s='; 534 $link['url'] .= rawurlencode($search); 535 } 536 537 //keep hash 538 if($hash) $link['url'].='#'.$hash; 539 540 //output formatted 541 if($returnonly){ 542 return $this->_formatLink($link); 543 }else{ 544 $this->doc .= $this->_formatLink($link); 545 } 546 } 547 548 function externallink($url, $name = NULL) { 549 global $conf; 550 551 $name = $this->_getLinkTitle($name, $url, $isImage); 552 553 // add protocol on simple short URLs 554 if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')) $url = 'ftp://'.$url; 555 if(substr($url,0,3) == 'www') $url = 'http://'.$url; 556 557 if ( !$isImage ) { 558 $class='urlextern'; 559 } else { 560 $class='media'; 561 } 562 563 //prepare for formating 564 $link['target'] = $conf['target']['extern']; 565 $link['style'] = ''; 566 $link['pre'] = ''; 567 $link['suf'] = ''; 568 $link['more'] = ''; 569 $link['class'] = $class; 570 $link['url'] = $url; 571 572 $link['name'] = $name; 573 $link['title'] = $this->_xmlEntities($url); 574 if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"'; 575 576 //output formatted 577 $this->doc .= $this->_formatLink($link); 578 } 579 580 /** 581 */ 582 function interwikilink($match, $name = NULL, $wikiName, $wikiUri) { 583 global $conf; 584 585 $link = array(); 586 $link['target'] = $conf['target']['interwiki']; 587 $link['pre'] = ''; 588 $link['suf'] = ''; 589 $link['more'] = ''; 590 $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage); 591 592 //get interwiki URL 593 if ( isset($this->interwiki[$wikiName]) ) { 594 $url = $this->interwiki[$wikiName]; 595 } else { 596 // Default to Google I'm feeling lucky 597 $url = 'http://www.google.com/search?q={URL}&btnI=lucky'; 598 $wikiName = 'go'; 599 } 600 601 if ( !$isImage ) { 602 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName); 603 $link['class'] = "interwiki iw_$class"; 604 } else { 605 $link['class'] = 'media'; 606 } 607 608 //do we stay at the same server? Use local target 609 if( strpos($url,DOKU_URL) === 0 ){ 610 $link['target'] = $conf['target']['wiki']; 611 } 612 613 //split into hash and url part 614 list($wikiUri,$hash) = explode('#',$wikiUri,2); 615 616 //replace placeholder 617 if(preg_match('#\{(URL|NAME|SCHEME|HOST|PORT|PATH|QUERY)\}#',$url)){ 618 //use placeholders 619 $url = str_replace('{URL}',rawurlencode($wikiUri),$url); 620 $url = str_replace('{NAME}',$wikiUri,$url); 621 $parsed = parse_url($wikiUri); 622 if(!$parsed['port']) $parsed['port'] = 80; 623 $url = str_replace('{SCHEME}',$parsed['scheme'],$url); 624 $url = str_replace('{HOST}',$parsed['host'],$url); 625 $url = str_replace('{PORT}',$parsed['port'],$url); 626 $url = str_replace('{PATH}',$parsed['path'],$url); 627 $url = str_replace('{QUERY}',$parsed['query'],$url); 628 $link['url'] = $url; 629 }else{ 630 //default 631 $link['url'] = $url.rawurlencode($wikiUri); 632 } 633 if($hash) $link['url'] .= '#'.rawurlencode($hash); 634 635 $link['title'] = htmlspecialchars($link['url']); 636 637 //output formatted 638 $this->doc .= $this->_formatLink($link); 639 } 640 641 /** 642 */ 643 function windowssharelink($url, $name = NULL) { 644 global $conf; 645 global $lang; 646 //simple setup 647 $link['target'] = $conf['target']['windows']; 648 $link['pre'] = ''; 649 $link['suf'] = ''; 650 $link['style'] = ''; 651 //Display error on browsers other than IE 652 $link['more'] = 'onclick="if(document.all == null){alert(\''. 653 $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES). 654 '\');}" onkeypress="if(document.all == null){alert(\''. 655 $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).'\');}"'; 656 657 $link['name'] = $this->_getLinkTitle($name, $url, $isImage); 658 if ( !$isImage ) { 659 $link['class'] = 'windows'; 660 } else { 661 $link['class'] = 'media'; 662 } 663 664 665 $link['title'] = $this->_xmlEntities($url); 666 $url = str_replace('\\','/',$url); 667 $url = 'file:///'.$url; 668 $link['url'] = $url; 669 670 //output formatted 671 $this->doc .= $this->_formatLink($link); 672 } 673 674 function emaillink($address, $name = NULL) { 675 global $conf; 676 //simple setup 677 $link = array(); 678 $link['target'] = ''; 679 $link['pre'] = ''; 680 $link['suf'] = ''; 681 $link['style'] = ''; 682 $link['more'] = ''; 683 684 //we just test for image here - we need to encode the title our self 685 $this->_getLinkTitle($name, $address, $isImage); 686 if ( !$isImage ) { 687 $link['class']='mail JSnocheck'; 688 } else { 689 $link['class']='media JSnocheck'; 690 } 691 692 $address = $this->_xmlEntities($address); 693 $address = obfuscate($address); 694 $title = $address; 695 if(empty($name)){ 696 $name = $address; 697 }else{ 698 $name = $this->_xmlEntities($name); 699 } 700 701 if($conf['mailguard'] == 'visible') $address = rawurlencode($address); 702 703 $link['url'] = 'mailto:'.$address; 704 $link['name'] = $name; 705 $link['title'] = $title; 706 707 //output formatted 708 $this->doc .= $this->_formatLink($link); 709 } 710 711 function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL, 712 $height=NULL, $cache=NULL, $linking=NULL) { 713 global $conf; 714 global $ID; 715 resolve_mediaid(getNS($ID),$src, $exists); 716 717 $link = array(); 718 $link['class'] = 'media'; 719 $link['style'] = ''; 720 $link['pre'] = ''; 721 $link['suf'] = ''; 722 $link['more'] = ''; 723 $link['target'] = $conf['target']['media']; 724 $noLink = false; 725 726 $link['title'] = $this->_xmlEntities($src); 727 list($ext,$mime) = mimetype($src); 728 if(substr($mime,0,5) == 'image'){ 729 $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct')); 730 }elseif($mime == 'application/x-shockwave-flash'){ 731 // don't link flash movies 732 $noLink = TRUE; 733 }else{ 734 // add file icons 735 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 736 $link['class'] .= ' mediafile mf_'.$class; 737 $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true); 738 } 739 $link['name'] = $this->_media ($src, $title, $align, $width, $height, $cache); 740 741 //output formatted 742 if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; 743 else $this->doc .= $this->_formatLink($link); 744 } 745 746 /** 747 * @todo don't add link for flash 748 */ 749 function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL, 750 $height=NULL, $cache=NULL, $linking=NULL) { 751 global $conf; 752 753 $link = array(); 754 $link['class'] = 'media'; 755 $link['style'] = ''; 756 $link['pre'] = ''; 757 $link['suf'] = ''; 758 $link['more'] = ''; 759 $link['target'] = $conf['target']['media']; 760 761 $link['title'] = $this->_xmlEntities($src); 762 $link['url'] = ml($src,array('cache'=>$cache)); 763 $link['name'] = $this->_media ($src, $title, $align, $width, $height, $cache); 764 $noLink = false; 765 766 list($ext,$mime) = mimetype($src); 767 if(substr($mime,0,5) == 'image'){ 768 // link only jpeg images 769 // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = TRUE; 770 }elseif($mime == 'application/x-shockwave-flash'){ 771 // don't link flash movies 772 $noLink = TRUE; 773 }else{ 774 // add file icons 775 $link['class'] .= ' mediafile mf_'.$ext; 776 } 777 778 //output formatted 779 if ($linking == 'nolink' || $noLink) $this->doc .= $link['name']; 780 else $this->doc .= $this->_formatLink($link); 781 } 782 783 /** 784 * Renders an RSS feed 785 * 786 * @author Andreas Gohr <andi@splitbrain.org> 787 */ 788 function rss ($url,$params){ 789 global $lang; 790 global $conf; 791 792 require_once(DOKU_INC.'inc/FeedParser.php'); 793 $feed = new FeedParser(); 794 $feed->feed_url($url); 795 796 //disable warning while fetching 797 if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); } 798 $rc = $feed->init(); 799 if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); } 800 801 //decide on start and end 802 if($params['reverse']){ 803 $mod = -1; 804 $start = $feed->get_item_quantity()-1; 805 $end = $start - ($params['max']); 806 $end = ($end < 0) ? 0 : $end; 807 }else{ 808 $mod = 1; 809 $start = 0; 810 $end = $feed->get_item_quantity(); 811 $end = ($end > $params['max']) ? $params['max'] : $end;; 812 } 813 814 $this->doc .= '<ul class="rss">'; 815 if($rc){ 816 for ($x = $start; $x != $end; $x += $mod) { 817 $item = $feed->get_item($x); 818 $this->doc .= '<li><div class="li">'; 819 $this->externallink($item->get_permalink(), 820 $item->get_title()); 821 if($params['author']){ 822 $author = $item->get_author(0); 823 if($author){ 824 $name = $author->get_name(); 825 if(!$name) $name = $author->get_email(); 826 if($name) $this->doc .= ' '.$lang['by'].' '.$name; 827 } 828 } 829 if($params['date']){ 830 $this->doc .= ' ('.$item->get_date($conf['dformat']).')'; 831 } 832 if($params['details']){ 833 $this->doc .= '<div class="detail">'; 834 if($htmlok){ 835 $this->doc .= $item->get_description(); 836 }else{ 837 $this->doc .= strip_tags($item->get_description()); 838 } 839 $this->doc .= '</div>'; 840 } 841 842 $this->doc .= '</div></li>'; 843 } 844 }else{ 845 $this->doc .= '<li><div class="li">'; 846 $this->doc .= '<em>'.$lang['rssfailed'].'</em>'; 847 $this->externallink($url); 848 $this->doc .= '</div></li>'; 849 } 850 $this->doc .= '</ul>'; 851 } 852 853 // $numrows not yet implemented 854 function table_open($maxcols = NULL, $numrows = NULL){ 855 $this->doc .= '<table class="inline">'.DOKU_LF; 856 } 857 858 function table_close(){ 859 $this->doc .= '</table>'.DOKU_LF.'<br />'.DOKU_LF; 860 } 861 862 function tablerow_open(){ 863 $this->doc .= DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB; 864 } 865 866 function tablerow_close(){ 867 $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF; 868 } 869 870 function tableheader_open($colspan = 1, $align = NULL){ 871 $this->doc .= '<th'; 872 if ( !is_null($align) ) { 873 $this->doc .= ' class="'.$align.'align"'; 874 } 875 if ( $colspan > 1 ) { 876 $this->doc .= ' colspan="'.$colspan.'"'; 877 } 878 $this->doc .= '>'; 879 } 880 881 function tableheader_close(){ 882 $this->doc .= '</th>'; 883 } 884 885 function tablecell_open($colspan = 1, $align = NULL){ 886 $this->doc .= '<td'; 887 if ( !is_null($align) ) { 888 $this->doc .= ' class="'.$align.'align"'; 889 } 890 if ( $colspan > 1 ) { 891 $this->doc .= ' colspan="'.$colspan.'"'; 892 } 893 $this->doc .= '>'; 894 } 895 896 function tablecell_close(){ 897 $this->doc .= '</td>'; 898 } 899 900 //---------------------------------------------------------- 901 // Utils 902 903 /** 904 * Build a link 905 * 906 * Assembles all parts defined in $link returns HTML for the link 907 * 908 * @author Andreas Gohr <andi@splitbrain.org> 909 */ 910 function _formatLink($link){ 911 //make sure the url is XHTML compliant (skip mailto) 912 if(substr($link['url'],0,7) != 'mailto:'){ 913 $link['url'] = str_replace('&','&',$link['url']); 914 $link['url'] = str_replace('&amp;','&',$link['url']); 915 } 916 //remove double encodings in titles 917 $link['title'] = str_replace('&amp;','&',$link['title']); 918 919 // be sure there are no bad chars in url or title 920 // (we can't do this for name because it can contain an img tag) 921 $link['url'] = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22')); 922 $link['title'] = strtr($link['title'],array('>'=>'>','<'=>'<','"'=>'"')); 923 924 $ret = ''; 925 $ret .= $link['pre']; 926 $ret .= '<a href="'.$link['url'].'"'; 927 if($link['class']) $ret .= ' class="'.$link['class'].'"'; 928 if($link['target']) $ret .= ' target="'.$link['target'].'"'; 929 if($link['title']) $ret .= ' title="'.$link['title'].'"'; 930 if($link['style']) $ret .= ' style="'.$link['style'].'"'; 931 if($link['more']) $ret .= ' '.$link['more']; 932 $ret .= '>'; 933 $ret .= $link['name']; 934 $ret .= '</a>'; 935 $ret .= $link['suf']; 936 return $ret; 937 } 938 939 /** 940 * Removes any Namespace from the given name but keeps 941 * casing and special chars 942 * 943 * @author Andreas Gohr <andi@splitbrain.org> 944 */ 945 function _simpleTitle($name){ 946 global $conf; 947 948 //if there is a hash we use the ancor name only 949 list($name,$hash) = explode('#',$name,2); 950 if($hash) return $hash; 951 952 //trim colons of a namespace link 953 $name = rtrim($name,':'); 954 955 if($conf['useslash']){ 956 $nssep = '[:;/]'; 957 }else{ 958 $nssep = '[:;]'; 959 } 960 $name = preg_replace('!.*'.$nssep.'!','',$name); 961 962 if(!$name) return $this->_simpleTitle($conf['start']); 963 return $name; 964 } 965 966 /** 967 * Renders internal and external media 968 * 969 * @author Andreas Gohr <andi@splitbrain.org> 970 */ 971 function _media ($src, $title=NULL, $align=NULL, $width=NULL, 972 $height=NULL, $cache=NULL) { 973 974 $ret = ''; 975 976 list($ext,$mime) = mimetype($src); 977 if(substr($mime,0,5) == 'image'){ 978 //add image tag 979 $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"'; 980 $ret .= ' class="media'.$align.'"'; 981 982 if (!is_null($title)) { 983 $ret .= ' title="'.$this->_xmlEntities($title).'"'; 984 $ret .= ' alt="'.$this->_xmlEntities($title).'"'; 985 }elseif($ext == 'jpg' || $ext == 'jpeg'){ 986 //try to use the caption from IPTC/EXIF 987 require_once(DOKU_INC.'inc/JpegMeta.php'); 988 $jpeg =& new JpegMeta(mediaFN($src)); 989 if($jpeg !== false) $cap = $jpeg->getTitle(); 990 if($cap){ 991 $ret .= ' title="'.$this->_xmlEntities($cap).'"'; 992 $ret .= ' alt="'.$this->_xmlEntities($cap).'"'; 993 } 994 }else{ 995 $ret .= ' alt=""'; 996 } 997 998 if ( !is_null($width) ) 999 $ret .= ' width="'.$this->_xmlEntities($width).'"'; 1000 1001 if ( !is_null($height) ) 1002 $ret .= ' height="'.$this->_xmlEntities($height).'"'; 1003 1004 $ret .= ' />'; 1005 1006 }elseif($mime == 'application/x-shockwave-flash'){ 1007 $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'. 1008 ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"'; 1009 if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"'; 1010 if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"'; 1011 $ret .= '>'.DOKU_LF; 1012 $ret .= '<param name="movie" value="'.ml($src).'" />'.DOKU_LF; 1013 $ret .= '<param name="quality" value="high" />'.DOKU_LF; 1014 $ret .= '<embed src="'.ml($src).'"'. 1015 ' quality="high"'; 1016 if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"'; 1017 if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"'; 1018 $ret .= ' type="application/x-shockwave-flash"'. 1019 ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'.DOKU_LF; 1020 $ret .= '</object>'.DOKU_LF; 1021 1022 }elseif(!is_null($title)){ 1023 // well at least we have a title to display 1024 $ret .= $this->_xmlEntities($title); 1025 }else{ 1026 // just show the sourcename 1027 $ret .= $this->_xmlEntities(noNS($src)); 1028 } 1029 1030 return $ret; 1031 } 1032 1033 function _xmlEntities($string) { 1034 return htmlspecialchars($string); 1035 } 1036 1037 /** 1038 * Creates a linkid from a headline 1039 * 1040 * @param string $title The headline title 1041 * @param boolean $create Create a new unique ID? 1042 * @author Andreas Gohr <andi@splitbrain.org> 1043 */ 1044 function _headerToLink($title,$create=false) { 1045 $title = str_replace(':','',cleanID($title,true)); //force ASCII 1046 $title = ltrim($title,'0123456789._-'); 1047 if(empty($title)) $title='section'; 1048 1049 if($create){ 1050 // make sure tiles are unique 1051 $num = ''; 1052 while(in_array($title.$num,$this->headers)){ 1053 ($num) ? $num++ : $num = 1; 1054 } 1055 $title = $title.$num; 1056 $this->headers[] = $title; 1057 } 1058 1059 return $title; 1060 } 1061 1062 /** 1063 * Construct a title and handle images in titles 1064 * 1065 * @author Harry Fuecks <hfuecks@gmail.com> 1066 */ 1067 function _getLinkTitle($title, $default, & $isImage, $id=NULL) { 1068 global $conf; 1069 1070 $isImage = FALSE; 1071 if ( is_null($title) ) { 1072 if ($conf['useheading'] && $id) { 1073 $heading = p_get_first_heading($id); 1074 if ($heading) { 1075 return $this->_xmlEntities($heading); 1076 } 1077 } 1078 return $this->_xmlEntities($default); 1079 } else if ( is_string($title) ) { 1080 return $this->_xmlEntities($title); 1081 } else if ( is_array($title) ) { 1082 $isImage = TRUE; 1083 return $this->_imageTitle($title); 1084 } 1085 } 1086 1087 /** 1088 * Returns an HTML code for images used in link titles 1089 * 1090 * @todo Resolve namespace on internal images 1091 * @author Andreas Gohr <andi@splitbrain.org> 1092 */ 1093 function _imageTitle($img) { 1094 return $this->_media($img['src'], 1095 $img['title'], 1096 $img['align'], 1097 $img['width'], 1098 $img['height'], 1099 $img['cache']); 1100 } 1101} 1102 1103//Setup VIM: ex: et ts=4 enc=utf-8 : 1104