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