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