xref: /dokuwiki/inc/parser/xhtml.php (revision a8a703a4d824a2a611926012072647b4a84cdfec)
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&times;$y";
452    }
453
454    function singlequoteopening() {
455        $this->doc .= "&lsquo;";
456    }
457
458    function singlequoteclosing() {
459        $this->doc .= "&rsquo;";
460    }
461
462    function doublequoteopening() {
463        $this->doc .= "&ldquo;";
464    }
465
466    function doublequoteclosing() {
467        $this->doc .= "&rdquo;";
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.' &crarr;';
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'].='&amp;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}&amp;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
725        $link['title']  = $this->_xmlEntities($src);
726        list($ext,$mime) = mimetype($src);
727        if(substr($mime,0,5) == 'image'){
728             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
729         }elseif($mime == 'application/x-shockwave-flash'){
730             // don't link flash movies
731             $noLink = TRUE;
732         }else{
733             // add file icons
734             $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
735             $link['class'] .= ' mediafile mf_'.$class;
736             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
737         }
738         $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
739
740         //output formatted
741         if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
742         else $this->doc .= $this->_formatLink($link);
743    }
744
745    /**
746     * @todo don't add link for flash
747     */
748    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
749                            $height=NULL, $cache=NULL, $linking=NULL) {
750        global $conf;
751
752        $link = array();
753        $link['class']  = 'media';
754        $link['style']  = '';
755        $link['pre']    = '';
756        $link['suf']    = '';
757        $link['more']   = '';
758        $link['target'] = $conf['target']['media'];
759
760        $link['title']  = $this->_xmlEntities($src);
761        $link['url']    = ml($src,array('cache'=>$cache));
762        $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
763
764
765        list($ext,$mime) = mimetype($src);
766        if(substr($mime,0,5) == 'image'){
767             // link only jpeg images
768             // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = TRUE;
769        }elseif($mime == 'application/x-shockwave-flash'){
770             // don't link flash movies
771             $noLink = TRUE;
772        }else{
773             // add file icons
774             $link['class'] .= ' mediafile mf_'.$ext;
775         }
776
777        //output formatted
778        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
779        else $this->doc .= $this->_formatLink($link);
780    }
781
782    /**
783     * Renders an RSS feed
784     *
785     * @author Andreas Gohr <andi@splitbrain.org>
786     */
787    function rss ($url,$params){
788        global $lang;
789        global $conf;
790
791        require_once(DOKU_INC.'inc/FeedParser.php');
792        $feed = new FeedParser();
793        $feed->feed_url($url);
794
795        //disable warning while fetching
796        $elvl = error_reporting(E_ERROR);
797        $rc = $feed->init();
798        error_reporting($elvl);
799
800        //decide on start and end
801        if($params['reverse']){
802            $mod = -1;
803            $start = $feed->get_item_quantity()-1;
804            $end   = $start - ($params['max']);
805            $end   = ($end < 0) ? 0 : $end;
806        }else{
807            $mod   = 1;
808            $start = 0;
809            $end   = $feed->get_item_quantity();
810            $end   = ($end > $params['max']) ? $params['max'] : $end;;
811        }
812
813        $this->doc .= '<ul class="rss">';
814        if($rc){
815            for ($x = $start; $x != $end; $x += $mod) {
816                $item = $feed->get_item($x);
817                $this->doc .= '<li><div class="li">';
818                $this->externallink($item->get_permalink(),
819                                    $item->get_title());
820                if($params['author']){
821                    $author = $item->get_author(0);
822                    if($author){
823                        $name = $author->get_name();
824                        if(!$name) $name = $author->get_email();
825                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
826                    }
827                }
828                if($params['date']){
829                    $this->doc .= ' ('.$item->get_date($conf['dformat']).')';
830                }
831                if($params['details']){
832                    $this->doc .= '<div class="detail">';
833                    if($htmlok){
834                        $this->doc .= $item->get_description();
835                    }else{
836                        $this->doc .= strip_tags($item->get_description());
837                    }
838                    $this->doc .= '</div>';
839                }
840
841                $this->doc .= '</div></li>';
842            }
843        }else{
844            $this->doc .= '<li><div class="li">';
845            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
846            $this->externallink($url);
847            $this->doc .= '</div></li>';
848        }
849        $this->doc .= '</ul>';
850    }
851
852    // $numrows not yet implemented
853    function table_open($maxcols = NULL, $numrows = NULL){
854        $this->doc .= '<table class="inline">'.DOKU_LF;
855    }
856
857    function table_close(){
858        $this->doc .= '</table>'.DOKU_LF.'<br />'.DOKU_LF;
859    }
860
861    function tablerow_open(){
862        $this->doc .= DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB;
863    }
864
865    function tablerow_close(){
866        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
867    }
868
869    function tableheader_open($colspan = 1, $align = NULL){
870        $this->doc .= '<th';
871        if ( !is_null($align) ) {
872            $this->doc .= ' class="'.$align.'align"';
873        }
874        if ( $colspan > 1 ) {
875            $this->doc .= ' colspan="'.$colspan.'"';
876        }
877        $this->doc .= '>';
878    }
879
880    function tableheader_close(){
881        $this->doc .= '</th>';
882    }
883
884    function tablecell_open($colspan = 1, $align = NULL){
885        $this->doc .= '<td';
886        if ( !is_null($align) ) {
887            $this->doc .= ' class="'.$align.'align"';
888        }
889        if ( $colspan > 1 ) {
890            $this->doc .= ' colspan="'.$colspan.'"';
891        }
892        $this->doc .= '>';
893    }
894
895    function tablecell_close(){
896        $this->doc .= '</td>';
897    }
898
899    //----------------------------------------------------------
900    // Utils
901
902    /**
903     * Build a link
904     *
905     * Assembles all parts defined in $link returns HTML for the link
906     *
907     * @author Andreas Gohr <andi@splitbrain.org>
908     */
909    function _formatLink($link){
910        //make sure the url is XHTML compliant (skip mailto)
911        if(substr($link['url'],0,7) != 'mailto:'){
912            $link['url'] = str_replace('&','&amp;',$link['url']);
913            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
914        }
915        //remove double encodings in titles
916        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
917
918        // be sure there are no bad chars in url or title
919        // (we can't do this for name because it can contain an img tag)
920        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
921        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
922
923        $ret  = '';
924        $ret .= $link['pre'];
925        $ret .= '<a href="'.$link['url'].'"';
926        if($link['class'])  $ret .= ' class="'.$link['class'].'"';
927        if($link['target']) $ret .= ' target="'.$link['target'].'"';
928        if($link['title'])  $ret .= ' title="'.$link['title'].'"';
929        if($link['style'])  $ret .= ' style="'.$link['style'].'"';
930        if($link['more'])   $ret .= ' '.$link['more'];
931        $ret .= '>';
932        $ret .= $link['name'];
933        $ret .= '</a>';
934        $ret .= $link['suf'];
935        return $ret;
936    }
937
938    /**
939     * Removes any Namespace from the given name but keeps
940     * casing and special chars
941     *
942     * @author Andreas Gohr <andi@splitbrain.org>
943     */
944    function _simpleTitle($name){
945        global $conf;
946
947        //if there is a hash we use the ancor name only
948        list($name,$hash) = explode('#',$name,2);
949        if($hash) return $hash;
950
951        //trim colons of a namespace link
952        $name = rtrim($name,':');
953
954        if($conf['useslash']){
955            $nssep = '[:;/]';
956        }else{
957            $nssep = '[:;]';
958        }
959        $name = preg_replace('!.*'.$nssep.'!','',$name);
960
961        if(!$name) return $this->_simpleTitle($conf['start']);
962        return $name;
963    }
964
965    /**
966     * Renders internal and external media
967     *
968     * @author Andreas Gohr <andi@splitbrain.org>
969     */
970    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
971                      $height=NULL, $cache=NULL) {
972
973        $ret = '';
974
975        list($ext,$mime) = mimetype($src);
976        if(substr($mime,0,5) == 'image'){
977            //add image tag
978            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
979            $ret .= ' class="media'.$align.'"';
980
981            if (!is_null($title)) {
982                $ret .= ' title="'.$this->_xmlEntities($title).'"';
983                $ret .= ' alt="'.$this->_xmlEntities($title).'"';
984            }elseif($ext == 'jpg' || $ext == 'jpeg'){
985                //try to use the caption from IPTC/EXIF
986                require_once(DOKU_INC.'inc/JpegMeta.php');
987                $jpeg =& new JpegMeta(mediaFN($src));
988                if($jpeg !== false) $cap = $jpeg->getTitle();
989                if($cap){
990                    $ret .= ' title="'.$this->_xmlEntities($cap).'"';
991                    $ret .= ' alt="'.$this->_xmlEntities($cap).'"';
992                }
993            }else{
994                $ret .= ' alt=""';
995            }
996
997            if ( !is_null($width) )
998                $ret .= ' width="'.$this->_xmlEntities($width).'"';
999
1000            if ( !is_null($height) )
1001                $ret .= ' height="'.$this->_xmlEntities($height).'"';
1002
1003            $ret .= ' />';
1004
1005        }elseif($mime == 'application/x-shockwave-flash'){
1006            $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'.
1007                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"';
1008            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
1009            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
1010            $ret .= '>'.DOKU_LF;
1011            $ret .= '<param name="movie" value="'.ml($src).'" />'.DOKU_LF;
1012            $ret .= '<param name="quality" value="high" />'.DOKU_LF;
1013            $ret .= '<embed src="'.ml($src).'"'.
1014                    ' quality="high"';
1015            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
1016            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
1017            $ret .= ' type="application/x-shockwave-flash"'.
1018                    ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'.DOKU_LF;
1019            $ret .= '</object>'.DOKU_LF;
1020
1021        }elseif(!is_null($title)){
1022            // well at least we have a title to display
1023            $ret .= $this->_xmlEntities($title);
1024        }else{
1025            // just show the sourcename
1026            $ret .= $this->_xmlEntities(noNS($src));
1027        }
1028
1029        return $ret;
1030    }
1031
1032    function _xmlEntities($string) {
1033        return htmlspecialchars($string);
1034    }
1035
1036    /**
1037     * Creates a linkid from a headline
1038     *
1039     * @param string  $title   The headline title
1040     * @param boolean $create  Create a new unique ID?
1041     * @author Andreas Gohr <andi@splitbrain.org>
1042     */
1043    function _headerToLink($title,$create=false) {
1044        $title = str_replace(':','',cleanID($title,true)); //force ASCII
1045        $title = ltrim($title,'0123456789._-');
1046        if(empty($title)) $title='section';
1047
1048        if($create){
1049            // make sure tiles are unique
1050            $num = '';
1051            while(in_array($title.$num,$this->headers)){
1052                ($num) ? $num++ : $num = 1;
1053            }
1054            $title = $title.$num;
1055            $this->headers[] = $title;
1056        }
1057
1058        return $title;
1059    }
1060
1061    /**
1062     * Construct a title and handle images in titles
1063     *
1064     * @author Harry Fuecks <hfuecks@gmail.com>
1065     */
1066    function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
1067        global $conf;
1068
1069        $isImage = FALSE;
1070        if ( is_null($title) ) {
1071            if ($conf['useheading'] && $id) {
1072                $heading = p_get_first_heading($id);
1073                if ($heading) {
1074                    return $this->_xmlEntities($heading);
1075                }
1076            }
1077            return $this->_xmlEntities($default);
1078        } else if ( is_string($title) ) {
1079            return $this->_xmlEntities($title);
1080        } else if ( is_array($title) ) {
1081            $isImage = TRUE;
1082            return $this->_imageTitle($title);
1083        }
1084    }
1085
1086    /**
1087     * Returns an HTML code for images used in link titles
1088     *
1089     * @todo Resolve namespace on internal images
1090     * @author Andreas Gohr <andi@splitbrain.org>
1091     */
1092    function _imageTitle($img) {
1093        return $this->_media($img['src'],
1094                              $img['title'],
1095                              $img['align'],
1096                              $img['width'],
1097                              $img['height'],
1098                              $img['cache']);
1099    }
1100}
1101
1102//Setup VIM: ex: et ts=4 enc=utf-8 :
1103