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