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