xref: /dokuwiki/inc/parser/xhtml.php (revision 1ca2719c7488662ebd7964c0d026e0890f923ee9)
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
32    private $sectionedits = array(); // A stack of section edit data
33
34    var $headers = array();
35    var $footnotes = array();
36    var $lastlevel = 0;
37    var $node = array(0,0,0,0,0);
38    var $store = '';
39
40    var $_counter   = array(); // used as global counter, introduced for table classes
41    var $_codeblock = 0; // counts the code and file blocks, used to provide download links
42
43    private $schemes = null; // protocol schemes
44
45    /**
46     * Register a new edit section range
47     *
48     * @param $type  string The section type identifier
49     * @param $title string The section title
50     * @param $start int    The byte position for the edit start
51     * @return string A marker class for the starting HTML element
52     * @author Adrian Lang <lang@cosmocode.de>
53     */
54    public function startSectionEdit($start, $type, $title = null) {
55        static $lastsecid = 0;
56        $this->sectionedits[] = array(++$lastsecid, $start, $type, $title);
57        return 'sectionedit' . $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            $id = 0;
106            foreach ( $this->footnotes as $footnote ) {
107                $id++;   // the number of the current footnote
108
109                // check its not a placeholder that indicates actual footnote text is elsewhere
110                if (substr($footnote, 0, 5) != "@@FNT") {
111
112                    // open the footnote and set the anchor and backlink
113                    $this->doc .= '<div class="fn">';
114                    $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" name="fn__'.$id.'" class="fn_bot">';
115                    $this->doc .= $id.')</a></sup> '.DOKU_LF;
116
117                    // get any other footnotes that use the same markup
118                    $alt = array_keys($this->footnotes, "@@FNT$id");
119
120                    if (count($alt)) {
121                      foreach ($alt as $ref) {
122                        // set anchor and backlink for the other footnotes
123                        $this->doc .= ', <sup><a href="#fnt__'.($ref+1).'" id="fn__'.($ref+1).'" name="fn__'.($ref+1).'" class="fn_bot">';
124                        $this->doc .= ($ref+1).')</a></sup> '.DOKU_LF;
125                      }
126                    }
127
128                    // add footnote markup and close this footnote
129                    $this->doc .= $footnote;
130                    $this->doc .= '</div>' . DOKU_LF;
131                }
132            }
133            $this->doc .= '</div>'.DOKU_LF;
134        }
135
136        // Prepare the TOC
137        global $conf;
138        if($this->info['toc'] && is_array($this->toc) && $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']){
139            global $TOC;
140            $TOC = $this->toc;
141        }
142
143        // make sure there are no empty paragraphs
144        $this->doc = preg_replace('#<p>\s*</p>#','',$this->doc);
145    }
146
147    function toc_additem($id, $text, $level) {
148        global $conf;
149
150        //handle TOC
151        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){
152            $this->toc[] = html_mktocitem($id, $text, $level-$conf['toptoclevel']+1);
153        }
154    }
155
156    function header($text, $level, $pos) {
157        global $conf;
158
159        if(!$text) return; //skip empty headlines
160
161        $hid = $this->_headerToLink($text,true);
162
163        //only add items within configured levels
164        $this->toc_additem($hid, $text, $level);
165
166        // adjust $node to reflect hierarchy of levels
167        $this->node[$level-1]++;
168        if ($level < $this->lastlevel) {
169            for ($i = 0; $i < $this->lastlevel-$level; $i++) {
170                $this->node[$this->lastlevel-$i-1] = 0;
171            }
172        }
173        $this->lastlevel = $level;
174
175        if ($level <= $conf['maxseclevel'] &&
176            count($this->sectionedits) > 0 &&
177            $this->sectionedits[count($this->sectionedits) - 1][2] === 'section') {
178            $this->finishSectionEdit($pos - 1);
179        }
180
181        // write the header
182        $this->doc .= DOKU_LF.'<h'.$level;
183        if ($level <= $conf['maxseclevel']) {
184            $this->doc .= ' class="' . $this->startSectionEdit($pos, 'section', $text) . '"';
185        }
186        $this->doc .= '><a name="'.$hid.'" id="'.$hid.'">';
187        $this->doc .= $this->_xmlEntities($text);
188        $this->doc .= "</a></h$level>".DOKU_LF;
189    }
190
191    function section_open($level) {
192        $this->doc .= '<div class="level' . $level . '">' . DOKU_LF;
193    }
194
195    function section_close() {
196        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
197    }
198
199    function cdata($text) {
200        $this->doc .= $this->_xmlEntities($text);
201    }
202
203    function p_open() {
204        $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
205    }
206
207    function p_close() {
208        $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
209    }
210
211    function linebreak() {
212        $this->doc .= '<br/>'.DOKU_LF;
213    }
214
215    function hr() {
216        $this->doc .= '<hr />'.DOKU_LF;
217    }
218
219    function strong_open() {
220        $this->doc .= '<strong>';
221    }
222
223    function strong_close() {
224        $this->doc .= '</strong>';
225    }
226
227    function emphasis_open() {
228        $this->doc .= '<em>';
229    }
230
231    function emphasis_close() {
232        $this->doc .= '</em>';
233    }
234
235    function underline_open() {
236        $this->doc .= '<em class="u">';
237    }
238
239    function underline_close() {
240        $this->doc .= '</em>';
241    }
242
243    function monospace_open() {
244        $this->doc .= '<code>';
245    }
246
247    function monospace_close() {
248        $this->doc .= '</code>';
249    }
250
251    function subscript_open() {
252        $this->doc .= '<sub>';
253    }
254
255    function subscript_close() {
256        $this->doc .= '</sub>';
257    }
258
259    function superscript_open() {
260        $this->doc .= '<sup>';
261    }
262
263    function superscript_close() {
264        $this->doc .= '</sup>';
265    }
266
267    function deleted_open() {
268        $this->doc .= '<del>';
269    }
270
271    function deleted_close() {
272        $this->doc .= '</del>';
273    }
274
275    /**
276     * Callback for footnote start syntax
277     *
278     * All following content will go to the footnote instead of
279     * the document. To achieve this the previous rendered content
280     * is moved to $store and $doc is cleared
281     *
282     * @author Andreas Gohr <andi@splitbrain.org>
283     */
284    function footnote_open() {
285
286        // move current content to store and record footnote
287        $this->store = $this->doc;
288        $this->doc   = '';
289    }
290
291    /**
292     * Callback for footnote end syntax
293     *
294     * All rendered content is moved to the $footnotes array and the old
295     * content is restored from $store again
296     *
297     * @author Andreas Gohr
298     */
299    function footnote_close() {
300
301        // recover footnote into the stack and restore old content
302        $footnote = $this->doc;
303        $this->doc = $this->store;
304        $this->store = '';
305
306        // check to see if this footnote has been seen before
307        $i = array_search($footnote, $this->footnotes);
308
309        if ($i === false) {
310            // its a new footnote, add it to the $footnotes array
311            $id = count($this->footnotes)+1;
312            $this->footnotes[count($this->footnotes)] = $footnote;
313        } else {
314            // seen this one before, translate the index to an id and save a placeholder
315            $i++;
316            $id = count($this->footnotes)+1;
317            $this->footnotes[count($this->footnotes)] = "@@FNT".($i);
318        }
319
320        // output the footnote reference and link
321        $this->doc .= '<sup><a href="#fn__'.$id.'" name="fnt__'.$id.'" id="fnt__'.$id.'" class="fn_top">'.$id.')</a></sup>';
322    }
323
324    function listu_open() {
325        $this->doc .= '<ul>'.DOKU_LF;
326    }
327
328    function listu_close() {
329        $this->doc .= '</ul>'.DOKU_LF;
330    }
331
332    function listo_open() {
333        $this->doc .= '<ol>'.DOKU_LF;
334    }
335
336    function listo_close() {
337        $this->doc .= '</ol>'.DOKU_LF;
338    }
339
340    function listitem_open($level) {
341        $this->doc .= '<li class="level'.$level.'">';
342    }
343
344    function listitem_close() {
345        $this->doc .= '</li>'.DOKU_LF;
346    }
347
348    function listcontent_open() {
349        $this->doc .= '<div class="li">';
350    }
351
352    function listcontent_close() {
353        $this->doc .= '</div>'.DOKU_LF;
354    }
355
356    function unformatted($text) {
357        $this->doc .= $this->_xmlEntities($text);
358    }
359
360    /**
361     * Execute PHP code if allowed
362     *
363     * @param  string   $wrapper   html element to wrap result if $conf['phpok'] is okff
364     *
365     * @author Andreas Gohr <andi@splitbrain.org>
366     */
367    function php($text, $wrapper='code') {
368        global $conf;
369
370        if($conf['phpok']){
371          ob_start();
372          eval($text);
373          $this->doc .= ob_get_contents();
374          ob_end_clean();
375        } else {
376          $this->doc .= p_xhtml_cached_geshi($text, 'php', $wrapper);
377        }
378    }
379
380    function phpblock($text) {
381        $this->php($text, 'pre');
382    }
383
384    /**
385     * Insert HTML if allowed
386     *
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 .= '<acronym title="'.$title
477                .'">'.$this->_xmlEntities($acronym).'</acronym>';
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="middle" 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&times;$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.' &crarr;';
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
572        $params = '';
573        $parts = explode('?', $id, 2);
574        if (count($parts) === 2) {
575            $id = $parts[0];
576            $params = $parts[1];
577        }
578
579        // For empty $id we need to know the current $ID
580        // We need this check because _simpleTitle needs
581        // correct $id and resolve_pageid() use cleanID($id)
582        // (some things could be lost)
583        if ($id === '') {
584            $id = $ID;
585        }
586
587        // default name is based on $id as given
588        $default = $this->_simpleTitle($id);
589
590        // now first resolve and clean up the $id
591        resolve_pageid(getNS($ID),$id,$exists);
592
593        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
594        if ( !$isImage ) {
595            if ( $exists ) {
596                $class='wikilink1';
597            } else {
598                $class='wikilink2';
599                $link['rel']='nofollow';
600            }
601        } else {
602            $class='media';
603        }
604
605        //keep hash anchor
606        list($id,$hash) = explode('#',$id,2);
607        if(!empty($hash)) $hash = $this->_headerToLink($hash);
608
609        //prepare for formating
610        $link['target'] = $conf['target']['wiki'];
611        $link['style']  = '';
612        $link['pre']    = '';
613        $link['suf']    = '';
614        // highlight link to current page
615        if ($id == $ID) {
616            $link['pre']    = '<span class="curid">';
617            $link['suf']    = '</span>';
618        }
619        $link['more']   = '';
620        $link['class']  = $class;
621        $link['url']    = wl($id, $params);
622        $link['name']   = $name;
623        $link['title']  = $id;
624        //add search string
625        if($search){
626            ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&amp;';
627            if(is_array($search)){
628                $search = array_map('rawurlencode',$search);
629                $link['url'] .= 's[]='.join('&amp;s[]=',$search);
630            }else{
631                $link['url'] .= 's='.rawurlencode($search);
632            }
633        }
634
635        //keep hash
636        if($hash) $link['url'].='#'.$hash;
637
638        //output formatted
639        if($returnonly){
640            return $this->_formatLink($link);
641        }else{
642            $this->doc .= $this->_formatLink($link);
643        }
644    }
645
646    function externallink($url, $name = NULL) {
647        global $conf;
648
649        $name = $this->_getLinkTitle($name, $url, $isImage);
650
651        if ( !$isImage ) {
652            $class='urlextern';
653        } else {
654            $class='media';
655        }
656
657        //prepare for formating
658        $link['target'] = $conf['target']['extern'];
659        $link['style']  = '';
660        $link['pre']    = '';
661        $link['suf']    = '';
662        $link['more']   = '';
663        $link['class']  = $class;
664        $link['url']    = $url;
665
666        $link['name']   = $name;
667        $link['title']  = $this->_xmlEntities($url);
668        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
669
670        //output formatted
671        $this->doc .= $this->_formatLink($link);
672    }
673
674    /**
675    */
676    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
677        global $conf;
678
679        $link = array();
680        $link['target'] = $conf['target']['interwiki'];
681        $link['pre']    = '';
682        $link['suf']    = '';
683        $link['more']   = '';
684        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
685
686        //get interwiki URL
687        $url = $this->_resolveInterWiki($wikiName,$wikiUri);
688
689        if ( !$isImage ) {
690            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName);
691            $link['class'] = "interwiki iw_$class";
692        } else {
693            $link['class'] = 'media';
694        }
695
696        //do we stay at the same server? Use local target
697        if( strpos($url,DOKU_URL) === 0 ){
698            $link['target'] = $conf['target']['wiki'];
699        }
700
701        $link['url'] = $url;
702        $link['title'] = htmlspecialchars($link['url']);
703
704        //output formatted
705        $this->doc .= $this->_formatLink($link);
706    }
707
708    /**
709     */
710    function windowssharelink($url, $name = NULL) {
711        global $conf;
712        global $lang;
713        //simple setup
714        $link['target'] = $conf['target']['windows'];
715        $link['pre']    = '';
716        $link['suf']   = '';
717        $link['style']  = '';
718
719        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
720        if ( !$isImage ) {
721            $link['class'] = 'windows';
722        } else {
723            $link['class'] = 'media';
724        }
725
726
727        $link['title'] = $this->_xmlEntities($url);
728        $url = str_replace('\\','/',$url);
729        $url = 'file:///'.$url;
730        $link['url'] = $url;
731
732        //output formatted
733        $this->doc .= $this->_formatLink($link);
734    }
735
736    function emaillink($address, $name = NULL) {
737        global $conf;
738        //simple setup
739        $link = array();
740        $link['target'] = '';
741        $link['pre']    = '';
742        $link['suf']   = '';
743        $link['style']  = '';
744        $link['more']   = '';
745
746        $name = $this->_getLinkTitle($name, '', $isImage);
747        if ( !$isImage ) {
748            $link['class']='mail';
749        } else {
750            $link['class']='media';
751        }
752
753        $address = $this->_xmlEntities($address);
754        $address = obfuscate($address);
755        $title   = $address;
756
757        if(empty($name)){
758            $name = $address;
759        }
760
761        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
762
763        $link['url']   = 'mailto:'.$address;
764        $link['name']  = $name;
765        $link['title'] = $title;
766
767        //output formatted
768        $this->doc .= $this->_formatLink($link);
769    }
770
771    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
772                            $height=NULL, $cache=NULL, $linking=NULL) {
773        global $ID;
774        list($src,$hash) = explode('#',$src,2);
775        resolve_mediaid(getNS($ID),$src, $exists);
776
777        $noLink = false;
778        $render = ($linking == 'linkonly') ? false : true;
779        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
780
781        list($ext,$mime,$dl) = mimetype($src,false);
782        if(substr($mime,0,5) == 'image' && $render){
783            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
784        }elseif($mime == 'application/x-shockwave-flash' && $render){
785            // don't link flash movies
786            $noLink = true;
787        }else{
788            // add file icons
789            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
790            $link['class'] .= ' mediafile mf_'.$class;
791            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
792        }
793
794        if($hash) $link['url'] .= '#'.$hash;
795
796        //markup non existing files
797        if (!$exists)
798          $link['class'] .= ' wikilink2';
799
800        //output formatted
801        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
802        else $this->doc .= $this->_formatLink($link);
803    }
804
805    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
806                            $height=NULL, $cache=NULL, $linking=NULL) {
807        list($src,$hash) = explode('#',$src,2);
808        $noLink = false;
809        $render = ($linking == 'linkonly') ? false : true;
810        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
811
812        $link['url']    = ml($src,array('cache'=>$cache));
813
814        list($ext,$mime,$dl) = mimetype($src,false);
815        if(substr($mime,0,5) == 'image' && $render){
816            // link only jpeg images
817            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
818        }elseif($mime == 'application/x-shockwave-flash' && $render){
819            // don't link flash movies
820            $noLink = true;
821        }else{
822            // add file icons
823            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
824            $link['class'] .= ' mediafile mf_'.$class;
825        }
826
827        if($hash) $link['url'] .= '#'.$hash;
828
829        //output formatted
830        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
831        else $this->doc .= $this->_formatLink($link);
832    }
833
834    /**
835     * Renders an RSS feed
836     *
837     * @author Andreas Gohr <andi@splitbrain.org>
838     */
839    function rss ($url,$params){
840        global $lang;
841        global $conf;
842
843        require_once(DOKU_INC.'inc/FeedParser.php');
844        $feed = new FeedParser();
845        $feed->set_feed_url($url);
846
847        //disable warning while fetching
848        if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); }
849        $rc = $feed->init();
850        if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); }
851
852        //decide on start and end
853        if($params['reverse']){
854            $mod = -1;
855            $start = $feed->get_item_quantity()-1;
856            $end   = $start - ($params['max']);
857            $end   = ($end < -1) ? -1 : $end;
858        }else{
859            $mod   = 1;
860            $start = 0;
861            $end   = $feed->get_item_quantity();
862            $end   = ($end > $params['max']) ? $params['max'] : $end;;
863        }
864
865        $this->doc .= '<ul class="rss">';
866        if($rc){
867            for ($x = $start; $x != $end; $x += $mod) {
868                $item = $feed->get_item($x);
869                $this->doc .= '<li><div class="li">';
870                // support feeds without links
871                $lnkurl = $item->get_permalink();
872                if($lnkurl){
873                    // lnkurl might be an attack vector, only allow registered protocols
874                    if(is_null($this->schemes)) $this->schemes = getSchemes();
875                    list($scheme) = explode('://',$lnkurl);
876                    $scheme = strtolower($scheme);
877                    if(!in_array($scheme,$this->schemes)) $lnkurl = '';
878                }
879
880                if($lnkurl){
881                    // title is escaped by SimplePie, we unescape here because it
882                    // is escaped again in externallink() FS#1705
883                    $this->externallink($item->get_permalink(),
884                                        htmlspecialchars_decode($item->get_title()));
885                }else{
886                    $this->doc .= ' '.$item->get_title();
887                }
888                if($params['author']){
889                    $author = $item->get_author(0);
890                    if($author){
891                        $name = $author->get_name();
892                        if(!$name) $name = $author->get_email();
893                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
894                    }
895                }
896                if($params['date']){
897                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
898                }
899                if($params['details']){
900                    $this->doc .= '<div class="detail">';
901                    if($conf['htmlok']){
902                        $this->doc .= $item->get_description();
903                    }else{
904                        $this->doc .= strip_tags($item->get_description());
905                    }
906                    $this->doc .= '</div>';
907                }
908
909                $this->doc .= '</div></li>';
910            }
911        }else{
912            $this->doc .= '<li><div class="li">';
913            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
914            $this->externallink($url);
915            if($conf['allowdebug']){
916                $this->doc .= '<!--'.hsc($feed->error).'-->';
917            }
918            $this->doc .= '</div></li>';
919        }
920        $this->doc .= '</ul>';
921    }
922
923    // $numrows not yet implemented
924    function table_open($maxcols = null, $numrows = null, $pos = null){
925        global $lang;
926        // initialize the row counter used for classes
927        $this->_counter['row_counter'] = 0;
928        $class = 'table';
929        if ($pos !== null) {
930            $class .= ' ' . $this->startSectionEdit($pos, 'table');
931        }
932        $this->doc .= '<div class="' . $class . '"><table class="inline">' .
933                      DOKU_LF;
934    }
935
936    function table_close($pos = null){
937        $this->doc .= '</table></div>'.DOKU_LF;
938        if ($pos !== null) {
939            $this->finishSectionEdit($pos);
940        }
941    }
942
943    function tablerow_open(){
944        // initialize the cell counter used for classes
945        $this->_counter['cell_counter'] = 0;
946        $class = 'row' . $this->_counter['row_counter']++;
947        $this->doc .= DOKU_TAB . '<tr class="'.$class.'">' . DOKU_LF . DOKU_TAB . DOKU_TAB;
948    }
949
950    function tablerow_close(){
951        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
952    }
953
954    function tableheader_open($colspan = 1, $align = NULL, $rowspan = 1){
955        $class = 'class="col' . $this->_counter['cell_counter']++;
956        if ( !is_null($align) ) {
957            $class .= ' '.$align.'align';
958        }
959        $class .= '"';
960        $this->doc .= '<th ' . $class;
961        if ( $colspan > 1 ) {
962            $this->_counter['cell_counter'] += $colspan-1;
963            $this->doc .= ' colspan="'.$colspan.'"';
964        }
965        if ( $rowspan > 1 ) {
966            $this->doc .= ' rowspan="'.$rowspan.'"';
967        }
968        $this->doc .= '>';
969    }
970
971    function tableheader_close(){
972        $this->doc .= '</th>';
973    }
974
975    function tablecell_open($colspan = 1, $align = NULL, $rowspan = 1){
976        $class = 'class="col' . $this->_counter['cell_counter']++;
977        if ( !is_null($align) ) {
978            $class .= ' '.$align.'align';
979        }
980        $class .= '"';
981        $this->doc .= '<td '.$class;
982        if ( $colspan > 1 ) {
983            $this->_counter['cell_counter'] += $colspan-1;
984            $this->doc .= ' colspan="'.$colspan.'"';
985        }
986        if ( $rowspan > 1 ) {
987            $this->doc .= ' rowspan="'.$rowspan.'"';
988        }
989        $this->doc .= '>';
990    }
991
992    function tablecell_close(){
993        $this->doc .= '</td>';
994    }
995
996    //----------------------------------------------------------
997    // Utils
998
999    /**
1000     * Build a link
1001     *
1002     * Assembles all parts defined in $link returns HTML for the link
1003     *
1004     * @author Andreas Gohr <andi@splitbrain.org>
1005     */
1006    function _formatLink($link){
1007        //make sure the url is XHTML compliant (skip mailto)
1008        if(substr($link['url'],0,7) != 'mailto:'){
1009            $link['url'] = str_replace('&','&amp;',$link['url']);
1010            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
1011        }
1012        //remove double encodings in titles
1013        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
1014
1015        // be sure there are no bad chars in url or title
1016        // (we can't do this for name because it can contain an img tag)
1017        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
1018        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
1019
1020        $ret  = '';
1021        $ret .= $link['pre'];
1022        $ret .= '<a href="'.$link['url'].'"';
1023        if(!empty($link['class']))  $ret .= ' class="'.$link['class'].'"';
1024        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1025        if(!empty($link['title']))  $ret .= ' title="'.$link['title'].'"';
1026        if(!empty($link['style']))  $ret .= ' style="'.$link['style'].'"';
1027        if(!empty($link['rel']))    $ret .= ' rel="'.$link['rel'].'"';
1028        if(!empty($link['more']))   $ret .= ' '.$link['more'];
1029        $ret .= '>';
1030        $ret .= $link['name'];
1031        $ret .= '</a>';
1032        $ret .= $link['suf'];
1033        return $ret;
1034    }
1035
1036    /**
1037     * Renders internal and external media
1038     *
1039     * @author Andreas Gohr <andi@splitbrain.org>
1040     */
1041    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
1042                      $height=NULL, $cache=NULL, $render = true) {
1043
1044        $ret = '';
1045
1046        list($ext,$mime,$dl) = mimetype($src);
1047        if(substr($mime,0,5) == 'image'){
1048            // first get the $title
1049            if (!is_null($title)) {
1050                $title  = $this->_xmlEntities($title);
1051            }elseif($ext == 'jpg' || $ext == 'jpeg'){
1052                //try to use the caption from IPTC/EXIF
1053                require_once(DOKU_INC.'inc/JpegMeta.php');
1054                $jpeg =new JpegMeta(mediaFN($src));
1055                if($jpeg !== false) $cap = $jpeg->getTitle();
1056                if($cap){
1057                    $title = $this->_xmlEntities($cap);
1058                }
1059            }
1060            if (!$render) {
1061                // if the picture is not supposed to be rendered
1062                // return the title of the picture
1063                if (!$title) {
1064                    // just show the sourcename
1065                    $title = $this->_xmlEntities(basename(noNS($src)));
1066                }
1067                return $title;
1068            }
1069            //add image tag
1070            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
1071            $ret .= ' class="media'.$align.'"';
1072
1073            // make left/right alignment for no-CSS view work (feeds)
1074            if($align == 'right') $ret .= ' align="right"';
1075            if($align == 'left')  $ret .= ' align="left"';
1076
1077            if ($title) {
1078                $ret .= ' title="' . $title . '"';
1079                $ret .= ' alt="'   . $title .'"';
1080            }else{
1081                $ret .= ' alt=""';
1082            }
1083
1084            if ( !is_null($width) )
1085                $ret .= ' width="'.$this->_xmlEntities($width).'"';
1086
1087            if ( !is_null($height) )
1088                $ret .= ' height="'.$this->_xmlEntities($height).'"';
1089
1090            $ret .= ' />';
1091
1092        }elseif($mime == 'application/x-shockwave-flash'){
1093            if (!$render) {
1094                // if the flash is not supposed to be rendered
1095                // return the title of the flash
1096                if (!$title) {
1097                    // just show the sourcename
1098                    $title = basename(noNS($src));
1099                }
1100                return $this->_xmlEntities($title);
1101            }
1102
1103            $att = array();
1104            $att['class'] = "media$align";
1105            if($align == 'right') $att['align'] = 'right';
1106            if($align == 'left')  $att['align'] = 'left';
1107            $ret .= html_flashobject(ml($src,array('cache'=>$cache),true,'&'),$width,$height,
1108                                     array('quality' => 'high'),
1109                                     null,
1110                                     $att,
1111                                     $this->_xmlEntities($title));
1112        }elseif($title){
1113            // well at least we have a title to display
1114            $ret .= $this->_xmlEntities($title);
1115        }else{
1116            // just show the sourcename
1117            $ret .= $this->_xmlEntities(basename(noNS($src)));
1118        }
1119
1120        return $ret;
1121    }
1122
1123    function _xmlEntities($string) {
1124        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
1125    }
1126
1127    /**
1128     * Creates a linkid from a headline
1129     *
1130     * @param string  $title   The headline title
1131     * @param boolean $create  Create a new unique ID?
1132     * @author Andreas Gohr <andi@splitbrain.org>
1133     */
1134    function _headerToLink($title,$create=false) {
1135        if($create){
1136            return sectionID($title,$this->headers);
1137        }else{
1138            $check = false;
1139            return sectionID($title,$check);
1140        }
1141    }
1142
1143    /**
1144     * Construct a title and handle images in titles
1145     *
1146     * @author Harry Fuecks <hfuecks@gmail.com>
1147     */
1148    function _getLinkTitle($title, $default, & $isImage, $id=NULL, $linktype='content') {
1149        global $conf;
1150
1151        $isImage = false;
1152        if ( is_array($title) ) {
1153            $isImage = true;
1154            return $this->_imageTitle($title);
1155        } elseif ( is_null($title) || trim($title)=='') {
1156            if (useHeading($linktype) && $id) {
1157                $heading = p_get_first_heading($id);
1158                if ($heading) {
1159                    return $this->_xmlEntities($heading);
1160                }
1161            }
1162            return $this->_xmlEntities($default);
1163        } else {
1164            return $this->_xmlEntities($title);
1165        }
1166    }
1167
1168    /**
1169     * Returns an HTML code for images used in link titles
1170     *
1171     * @todo Resolve namespace on internal images
1172     * @author Andreas Gohr <andi@splitbrain.org>
1173     */
1174    function _imageTitle($img) {
1175        global $ID;
1176
1177        // some fixes on $img['src']
1178        // see internalmedia() and externalmedia()
1179        list($img['src'],$hash) = explode('#',$img['src'],2);
1180        if ($img['type'] == 'internalmedia') {
1181            resolve_mediaid(getNS($ID),$img['src'],$exists);
1182        }
1183
1184        return $this->_media($img['src'],
1185                              $img['title'],
1186                              $img['align'],
1187                              $img['width'],
1188                              $img['height'],
1189                              $img['cache']);
1190    }
1191
1192    /**
1193     * _getMediaLinkConf is a helperfunction to internalmedia() and externalmedia()
1194     * which returns a basic link to a media.
1195     *
1196     * @author Pierre Spring <pierre.spring@liip.ch>
1197     * @param string $src
1198     * @param string $title
1199     * @param string $align
1200     * @param string $width
1201     * @param string $height
1202     * @param string $cache
1203     * @param string $render
1204     * @access protected
1205     * @return array
1206     */
1207    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render)
1208    {
1209        global $conf;
1210
1211        $link = array();
1212        $link['class']  = 'media';
1213        $link['style']  = '';
1214        $link['pre']    = '';
1215        $link['suf']    = '';
1216        $link['more']   = '';
1217        $link['target'] = $conf['target']['media'];
1218        $link['title']  = $this->_xmlEntities($src);
1219        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1220
1221        return $link;
1222    }
1223
1224
1225}
1226
1227//Setup VIM: ex: et ts=4 :
1228