xref: /dokuwiki/inc/parser/xhtml.php (revision 42ea7f447f39fbc2f79eaaec31f8c10ede59c5d0)
10cecf9d5Sandi<?php
2b625487dSandi/**
3b625487dSandi * Renderer for XHTML output
4b625487dSandi *
5b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
6b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
7b625487dSandi */
8fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
90cecf9d5Sandi
100cecf9d5Sandiif(!defined('DOKU_LF')) {
110cecf9d5Sandi    // Some whitespace to help View > Source
120cecf9d5Sandi    define ('DOKU_LF', "\n");
130cecf9d5Sandi}
140cecf9d5Sandi
150cecf9d5Sandiif(!defined('DOKU_TAB')) {
160cecf9d5Sandi    // Some whitespace to help View > Source
170cecf9d5Sandi    define ('DOKU_TAB', "\t");
180cecf9d5Sandi}
190cecf9d5Sandi
200cecf9d5Sandi/**
213dd5c225SAndreas Gohr * The XHTML Renderer
223dd5c225SAndreas Gohr *
233dd5c225SAndreas Gohr * This is DokuWiki's main renderer used to display page content in the wiki
240cecf9d5Sandi */
25ac83b9d8Sandiclass Doku_Renderer_xhtml extends Doku_Renderer {
263dd5c225SAndreas Gohr    /** @var array store the table of contents */
273dd5c225SAndreas Gohr    public $toc = array();
280cecf9d5Sandi
293dd5c225SAndreas Gohr    /** @var array A stack of section edit data */
303dd5c225SAndreas Gohr    protected $sectionedits = array();
31c5a8fd96SAndreas Gohr
323dd5c225SAndreas Gohr    /** @var int last section edit id, used by startSectionEdit */
333dd5c225SAndreas Gohr    protected $lastsecid = 0;
340cecf9d5Sandi
353dd5c225SAndreas Gohr    /** @var array the list of headers used to create unique link ids */
363dd5c225SAndreas Gohr    protected $headers = array();
373dd5c225SAndreas Gohr
3816ec3e37SAndreas Gohr    /** @var array a list of footnotes, list starts at 1! */
393dd5c225SAndreas Gohr    protected $footnotes = array();
407764a90aSandi
413dd5c225SAndreas Gohr    /** @var int current section level */
423dd5c225SAndreas Gohr    protected $lastlevel = 0;
433dd5c225SAndreas Gohr    /** @var array section node tracker */
443dd5c225SAndreas Gohr    protected $node = array(0, 0, 0, 0, 0);
453dd5c225SAndreas Gohr
463dd5c225SAndreas Gohr    /** @var string temporary $doc store */
473dd5c225SAndreas Gohr    protected $store = '';
483dd5c225SAndreas Gohr
493dd5c225SAndreas Gohr    /** @var array global counter, for table classes etc. */
503dd5c225SAndreas Gohr    protected $_counter = array(); //
513dd5c225SAndreas Gohr
523dd5c225SAndreas Gohr    /** @var int counts the code and file blocks, used to provide download links */
533dd5c225SAndreas Gohr    protected $_codeblock = 0;
543dd5c225SAndreas Gohr
553dd5c225SAndreas Gohr    /** @var array list of allowed URL schemes */
563dd5c225SAndreas Gohr    protected $schemes = null;
57b5742cedSPierre Spring
5890df9a4dSAdrian Lang    /**
5990df9a4dSAdrian Lang     * Register a new edit section range
6090df9a4dSAdrian Lang     *
61*42ea7f44SGerrit Uitslag     * @param string $type   The section type identifier
62*42ea7f44SGerrit Uitslag     * @param string $title  The section title
63*42ea7f44SGerrit Uitslag     * @param int    $start  The byte position for the edit start
6490df9a4dSAdrian Lang     * @return string  A marker class for the starting HTML element
65*42ea7f44SGerrit Uitslag     *
6690df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
6790df9a4dSAdrian Lang     */
683f9e3215SAdrian Lang    public function startSectionEdit($start, $type, $title = null) {
69b04a190dSMichael Hamann        $this->sectionedits[] = array(++$this->lastsecid, $start, $type, $title);
70b04a190dSMichael Hamann        return 'sectionedit'.$this->lastsecid;
7190df9a4dSAdrian Lang    }
7290df9a4dSAdrian Lang
7390df9a4dSAdrian Lang    /**
7490df9a4dSAdrian Lang     * Finish an edit section range
7590df9a4dSAdrian Lang     *
76*42ea7f44SGerrit Uitslag     * @param int  $end     The byte position for the edit end; null for the rest of the page
77*42ea7f44SGerrit Uitslag     *
7890df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
7990df9a4dSAdrian Lang     */
803f9e3215SAdrian Lang    public function finishSectionEdit($end = null) {
8190df9a4dSAdrian Lang        list($id, $start, $type, $title) = array_pop($this->sectionedits);
82d9e36cbeSAdrian Lang        if(!is_null($end) && $end <= $start) {
8300c13053SAdrian Lang            return;
8400c13053SAdrian Lang        }
8540868f2fSAdrian Lang        $this->doc .= "<!-- EDIT$id ".strtoupper($type).' ';
8640868f2fSAdrian Lang        if(!is_null($title)) {
8740868f2fSAdrian Lang            $this->doc .= '"'.str_replace('"', '', $title).'" ';
8840868f2fSAdrian Lang        }
89d9e36cbeSAdrian Lang        $this->doc .= "[$start-".(is_null($end) ? '' : $end).'] -->';
9090df9a4dSAdrian Lang    }
9190df9a4dSAdrian Lang
923dd5c225SAndreas Gohr    /**
933dd5c225SAndreas Gohr     * Returns the format produced by this renderer.
943dd5c225SAndreas Gohr     *
953dd5c225SAndreas Gohr     * @return string always 'xhtml'
963dd5c225SAndreas Gohr     */
975f70445dSAndreas Gohr    function getFormat() {
985f70445dSAndreas Gohr        return 'xhtml';
995f70445dSAndreas Gohr    }
1005f70445dSAndreas Gohr
1013dd5c225SAndreas Gohr    /**
1023dd5c225SAndreas Gohr     * Initialize the document
1033dd5c225SAndreas Gohr     */
1040cecf9d5Sandi    function document_start() {
105c5a8fd96SAndreas Gohr        //reset some internals
106c5a8fd96SAndreas Gohr        $this->toc     = array();
107c5a8fd96SAndreas Gohr        $this->headers = array();
1080cecf9d5Sandi    }
1090cecf9d5Sandi
1103dd5c225SAndreas Gohr    /**
1113dd5c225SAndreas Gohr     * Finalize the document
1123dd5c225SAndreas Gohr     */
1130cecf9d5Sandi    function document_end() {
11490df9a4dSAdrian Lang        // Finish open section edits.
11590df9a4dSAdrian Lang        while(count($this->sectionedits) > 0) {
11690df9a4dSAdrian Lang            if($this->sectionedits[count($this->sectionedits) - 1][1] <= 1) {
11790df9a4dSAdrian Lang                // If there is only one section, do not write a section edit
11890df9a4dSAdrian Lang                // marker.
11990df9a4dSAdrian Lang                array_pop($this->sectionedits);
12090df9a4dSAdrian Lang            } else {
121d9e36cbeSAdrian Lang                $this->finishSectionEdit();
12290df9a4dSAdrian Lang            }
12390df9a4dSAdrian Lang        }
12490df9a4dSAdrian Lang
1250cecf9d5Sandi        if(count($this->footnotes) > 0) {
126a2d649c4Sandi            $this->doc .= '<div class="footnotes">'.DOKU_LF;
127d74aace9Schris
12816ec3e37SAndreas Gohr            foreach($this->footnotes as $id => $footnote) {
129d74aace9Schris                // check its not a placeholder that indicates actual footnote text is elsewhere
130d74aace9Schris                if(substr($footnote, 0, 5) != "@@FNT") {
131d74aace9Schris
132d74aace9Schris                    // open the footnote and set the anchor and backlink
133d74aace9Schris                    $this->doc .= '<div class="fn">';
13416cc7ed7SAnika Henke                    $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
13529bfcd16SAndreas Gohr                    $this->doc .= $id.')</a></sup> '.DOKU_LF;
136d74aace9Schris
137d74aace9Schris                    // get any other footnotes that use the same markup
138d74aace9Schris                    $alt = array_keys($this->footnotes, "@@FNT$id");
139d74aace9Schris
140d74aace9Schris                    if(count($alt)) {
141d74aace9Schris                        foreach($alt as $ref) {
142d74aace9Schris                            // set anchor and backlink for the other footnotes
14316ec3e37SAndreas Gohr                            $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
14416ec3e37SAndreas Gohr                            $this->doc .= ($ref).')</a></sup> '.DOKU_LF;
145d74aace9Schris                        }
146d74aace9Schris                    }
147d74aace9Schris
148d74aace9Schris                    // add footnote markup and close this footnote
149a2d649c4Sandi                    $this->doc .= $footnote;
150d74aace9Schris                    $this->doc .= '</div>'.DOKU_LF;
151d74aace9Schris                }
1520cecf9d5Sandi            }
153a2d649c4Sandi            $this->doc .= '</div>'.DOKU_LF;
1540cecf9d5Sandi        }
155c5a8fd96SAndreas Gohr
156b8595a66SAndreas Gohr        // Prepare the TOC
157851f2e89SAnika Henke        global $conf;
158851f2e89SAnika Henke        if($this->info['toc'] && is_array($this->toc) && $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']) {
159b8595a66SAndreas Gohr            global $TOC;
160b8595a66SAndreas Gohr            $TOC = $this->toc;
1610cecf9d5Sandi        }
1623e55d035SAndreas Gohr
1633e55d035SAndreas Gohr        // make sure there are no empty paragraphs
16427918226Schris        $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
165e41c4da9SAndreas Gohr    }
1660cecf9d5Sandi
1673dd5c225SAndreas Gohr    /**
1683dd5c225SAndreas Gohr     * Add an item to the TOC
1693dd5c225SAndreas Gohr     *
1703dd5c225SAndreas Gohr     * @param string $id       the hash link
1713dd5c225SAndreas Gohr     * @param string $text     the text to display
1723dd5c225SAndreas Gohr     * @param int    $level    the nesting level
1733dd5c225SAndreas Gohr     */
174e7856beaSchris    function toc_additem($id, $text, $level) {
175af587fa8Sandi        global $conf;
176af587fa8Sandi
177c5a8fd96SAndreas Gohr        //handle TOC
178c5a8fd96SAndreas Gohr        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
1797d91652aSAndreas Gohr            $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
180c5a8fd96SAndreas Gohr        }
181e7856beaSchris    }
182e7856beaSchris
1833dd5c225SAndreas Gohr    /**
1843dd5c225SAndreas Gohr     * Render a heading
1853dd5c225SAndreas Gohr     *
1863dd5c225SAndreas Gohr     * @param string $text  the text to display
1873dd5c225SAndreas Gohr     * @param int    $level header level
1883dd5c225SAndreas Gohr     * @param int    $pos   byte position in the original source
1893dd5c225SAndreas Gohr     */
190e7856beaSchris    function header($text, $level, $pos) {
19190df9a4dSAdrian Lang        global $conf;
19290df9a4dSAdrian Lang
193bdd8111bSAndreas Gohr        if(!$text) return; //skip empty headlines
194e7856beaSchris
195e7856beaSchris        $hid = $this->_headerToLink($text, true);
196e7856beaSchris
197e7856beaSchris        //only add items within configured levels
198e7856beaSchris        $this->toc_additem($hid, $text, $level);
199c5a8fd96SAndreas Gohr
20091459163SAnika Henke        // adjust $node to reflect hierarchy of levels
20191459163SAnika Henke        $this->node[$level - 1]++;
20291459163SAnika Henke        if($level < $this->lastlevel) {
20391459163SAnika Henke            for($i = 0; $i < $this->lastlevel - $level; $i++) {
20491459163SAnika Henke                $this->node[$this->lastlevel - $i - 1] = 0;
20591459163SAnika Henke            }
20691459163SAnika Henke        }
20791459163SAnika Henke        $this->lastlevel = $level;
20891459163SAnika Henke
20990df9a4dSAdrian Lang        if($level <= $conf['maxseclevel'] &&
21090df9a4dSAdrian Lang            count($this->sectionedits) > 0 &&
2113dd5c225SAndreas Gohr            $this->sectionedits[count($this->sectionedits) - 1][2] === 'section'
2123dd5c225SAndreas Gohr        ) {
2136c1f778cSAdrian Lang            $this->finishSectionEdit($pos - 1);
21490df9a4dSAdrian Lang        }
21590df9a4dSAdrian Lang
216c5a8fd96SAndreas Gohr        // write the header
21790df9a4dSAdrian Lang        $this->doc .= DOKU_LF.'<h'.$level;
21890df9a4dSAdrian Lang        if($level <= $conf['maxseclevel']) {
21990df9a4dSAdrian Lang            $this->doc .= ' class="'.$this->startSectionEdit($pos, 'section', $text).'"';
22090df9a4dSAdrian Lang        }
22116cc7ed7SAnika Henke        $this->doc .= ' id="'.$hid.'">';
222a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
22316cc7ed7SAnika Henke        $this->doc .= "</h$level>".DOKU_LF;
2240cecf9d5Sandi    }
2250cecf9d5Sandi
2263dd5c225SAndreas Gohr    /**
2273dd5c225SAndreas Gohr     * Open a new section
2283dd5c225SAndreas Gohr     *
2293dd5c225SAndreas Gohr     * @param int $level section level (as determined by the previous header)
2303dd5c225SAndreas Gohr     */
2310cecf9d5Sandi    function section_open($level) {
2329864e7b1SAdrian Lang        $this->doc .= '<div class="level'.$level.'">'.DOKU_LF;
2330cecf9d5Sandi    }
2340cecf9d5Sandi
2353dd5c225SAndreas Gohr    /**
2363dd5c225SAndreas Gohr     * Close the current section
2373dd5c225SAndreas Gohr     */
2380cecf9d5Sandi    function section_close() {
239a2d649c4Sandi        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
2400cecf9d5Sandi    }
2410cecf9d5Sandi
2423dd5c225SAndreas Gohr    /**
2433dd5c225SAndreas Gohr     * Render plain text data
2443dd5c225SAndreas Gohr     *
2453dd5c225SAndreas Gohr     * @param $text
2463dd5c225SAndreas Gohr     */
2470cecf9d5Sandi    function cdata($text) {
248a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
2490cecf9d5Sandi    }
2500cecf9d5Sandi
2513dd5c225SAndreas Gohr    /**
2523dd5c225SAndreas Gohr     * Open a paragraph
2533dd5c225SAndreas Gohr     */
2540cecf9d5Sandi    function p_open() {
25559869a4bSAnika Henke        $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
2560cecf9d5Sandi    }
2570cecf9d5Sandi
2583dd5c225SAndreas Gohr    /**
2593dd5c225SAndreas Gohr     * Close a paragraph
2603dd5c225SAndreas Gohr     */
2610cecf9d5Sandi    function p_close() {
26259869a4bSAnika Henke        $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
2630cecf9d5Sandi    }
2640cecf9d5Sandi
2653dd5c225SAndreas Gohr    /**
2663dd5c225SAndreas Gohr     * Create a line break
2673dd5c225SAndreas Gohr     */
2680cecf9d5Sandi    function linebreak() {
269a2d649c4Sandi        $this->doc .= '<br/>'.DOKU_LF;
2700cecf9d5Sandi    }
2710cecf9d5Sandi
2723dd5c225SAndreas Gohr    /**
2733dd5c225SAndreas Gohr     * Create a horizontal line
2743dd5c225SAndreas Gohr     */
2750cecf9d5Sandi    function hr() {
2764beabca9SAnika Henke        $this->doc .= '<hr />'.DOKU_LF;
2770cecf9d5Sandi    }
2780cecf9d5Sandi
2793dd5c225SAndreas Gohr    /**
2803dd5c225SAndreas Gohr     * Start strong (bold) formatting
2813dd5c225SAndreas Gohr     */
2820cecf9d5Sandi    function strong_open() {
283a2d649c4Sandi        $this->doc .= '<strong>';
2840cecf9d5Sandi    }
2850cecf9d5Sandi
2863dd5c225SAndreas Gohr    /**
2873dd5c225SAndreas Gohr     * Stop strong (bold) formatting
2883dd5c225SAndreas Gohr     */
2890cecf9d5Sandi    function strong_close() {
290a2d649c4Sandi        $this->doc .= '</strong>';
2910cecf9d5Sandi    }
2920cecf9d5Sandi
2933dd5c225SAndreas Gohr    /**
2943dd5c225SAndreas Gohr     * Start emphasis (italics) formatting
2953dd5c225SAndreas Gohr     */
2960cecf9d5Sandi    function emphasis_open() {
297a2d649c4Sandi        $this->doc .= '<em>';
2980cecf9d5Sandi    }
2990cecf9d5Sandi
3003dd5c225SAndreas Gohr    /**
3013dd5c225SAndreas Gohr     * Stop emphasis (italics) formatting
3023dd5c225SAndreas Gohr     */
3030cecf9d5Sandi    function emphasis_close() {
304a2d649c4Sandi        $this->doc .= '</em>';
3050cecf9d5Sandi    }
3060cecf9d5Sandi
3073dd5c225SAndreas Gohr    /**
3083dd5c225SAndreas Gohr     * Start underline formatting
3093dd5c225SAndreas Gohr     */
3100cecf9d5Sandi    function underline_open() {
31102e51121SAnika Henke        $this->doc .= '<em class="u">';
3120cecf9d5Sandi    }
3130cecf9d5Sandi
3143dd5c225SAndreas Gohr    /**
3153dd5c225SAndreas Gohr     * Stop underline formatting
3163dd5c225SAndreas Gohr     */
3170cecf9d5Sandi    function underline_close() {
31802e51121SAnika Henke        $this->doc .= '</em>';
3190cecf9d5Sandi    }
3200cecf9d5Sandi
3213dd5c225SAndreas Gohr    /**
3223dd5c225SAndreas Gohr     * Start monospace formatting
3233dd5c225SAndreas Gohr     */
3240cecf9d5Sandi    function monospace_open() {
325a2d649c4Sandi        $this->doc .= '<code>';
3260cecf9d5Sandi    }
3270cecf9d5Sandi
3283dd5c225SAndreas Gohr    /**
3293dd5c225SAndreas Gohr     * Stop monospace formatting
3303dd5c225SAndreas Gohr     */
3310cecf9d5Sandi    function monospace_close() {
332a2d649c4Sandi        $this->doc .= '</code>';
3330cecf9d5Sandi    }
3340cecf9d5Sandi
3353dd5c225SAndreas Gohr    /**
3363dd5c225SAndreas Gohr     * Start a subscript
3373dd5c225SAndreas Gohr     */
3380cecf9d5Sandi    function subscript_open() {
339a2d649c4Sandi        $this->doc .= '<sub>';
3400cecf9d5Sandi    }
3410cecf9d5Sandi
3423dd5c225SAndreas Gohr    /**
3433dd5c225SAndreas Gohr     * Stop a subscript
3443dd5c225SAndreas Gohr     */
3450cecf9d5Sandi    function subscript_close() {
346a2d649c4Sandi        $this->doc .= '</sub>';
3470cecf9d5Sandi    }
3480cecf9d5Sandi
3493dd5c225SAndreas Gohr    /**
3503dd5c225SAndreas Gohr     * Start a superscript
3513dd5c225SAndreas Gohr     */
3520cecf9d5Sandi    function superscript_open() {
353a2d649c4Sandi        $this->doc .= '<sup>';
3540cecf9d5Sandi    }
3550cecf9d5Sandi
3563dd5c225SAndreas Gohr    /**
3573dd5c225SAndreas Gohr     * Stop a superscript
3583dd5c225SAndreas Gohr     */
3590cecf9d5Sandi    function superscript_close() {
360a2d649c4Sandi        $this->doc .= '</sup>';
3610cecf9d5Sandi    }
3620cecf9d5Sandi
3633dd5c225SAndreas Gohr    /**
3643dd5c225SAndreas Gohr     * Start deleted (strike-through) formatting
3653dd5c225SAndreas Gohr     */
3660cecf9d5Sandi    function deleted_open() {
367a2d649c4Sandi        $this->doc .= '<del>';
3680cecf9d5Sandi    }
3690cecf9d5Sandi
3703dd5c225SAndreas Gohr    /**
3713dd5c225SAndreas Gohr     * Stop deleted (strike-through) formatting
3723dd5c225SAndreas Gohr     */
3730cecf9d5Sandi    function deleted_close() {
374a2d649c4Sandi        $this->doc .= '</del>';
3750cecf9d5Sandi    }
3760cecf9d5Sandi
3773fd0b676Sandi    /**
3783fd0b676Sandi     * Callback for footnote start syntax
3793fd0b676Sandi     *
3803fd0b676Sandi     * All following content will go to the footnote instead of
381d74aace9Schris     * the document. To achieve this the previous rendered content
3823fd0b676Sandi     * is moved to $store and $doc is cleared
3833fd0b676Sandi     *
3843fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
3853fd0b676Sandi     */
3860cecf9d5Sandi    function footnote_open() {
3877764a90aSandi
3887764a90aSandi        // move current content to store and record footnote
3897764a90aSandi        $this->store = $this->doc;
3907764a90aSandi        $this->doc   = '';
3910cecf9d5Sandi    }
3920cecf9d5Sandi
3933fd0b676Sandi    /**
3943fd0b676Sandi     * Callback for footnote end syntax
3953fd0b676Sandi     *
3963fd0b676Sandi     * All rendered content is moved to the $footnotes array and the old
3973fd0b676Sandi     * content is restored from $store again
3983fd0b676Sandi     *
3993fd0b676Sandi     * @author Andreas Gohr
4003fd0b676Sandi     */
4010cecf9d5Sandi    function footnote_close() {
40216ec3e37SAndreas Gohr        /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
40316ec3e37SAndreas Gohr        static $fnid = 0;
40416ec3e37SAndreas Gohr        // assign new footnote id (we start at 1)
40516ec3e37SAndreas Gohr        $fnid++;
4067764a90aSandi
407d74aace9Schris        // recover footnote into the stack and restore old content
408d74aace9Schris        $footnote    = $this->doc;
4097764a90aSandi        $this->doc   = $this->store;
4107764a90aSandi        $this->store = '';
411d74aace9Schris
412d74aace9Schris        // check to see if this footnote has been seen before
413d74aace9Schris        $i = array_search($footnote, $this->footnotes);
414d74aace9Schris
415d74aace9Schris        if($i === false) {
416d74aace9Schris            // its a new footnote, add it to the $footnotes array
41716ec3e37SAndreas Gohr            $this->footnotes[$fnid] = $footnote;
418d74aace9Schris        } else {
41916ec3e37SAndreas Gohr            // seen this one before, save a placeholder
42016ec3e37SAndreas Gohr            $this->footnotes[$fnid] = "@@FNT".($i);
421d74aace9Schris        }
422d74aace9Schris
4236b379cbfSAndreas Gohr        // output the footnote reference and link
42416ec3e37SAndreas Gohr        $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
4250cecf9d5Sandi    }
4260cecf9d5Sandi
4273dd5c225SAndreas Gohr    /**
4283dd5c225SAndreas Gohr     * Open an unordered list
4293dd5c225SAndreas Gohr     */
4300cecf9d5Sandi    function listu_open() {
431a2d649c4Sandi        $this->doc .= '<ul>'.DOKU_LF;
4320cecf9d5Sandi    }
4330cecf9d5Sandi
4343dd5c225SAndreas Gohr    /**
4353dd5c225SAndreas Gohr     * Close an unordered list
4363dd5c225SAndreas Gohr     */
4370cecf9d5Sandi    function listu_close() {
438a2d649c4Sandi        $this->doc .= '</ul>'.DOKU_LF;
4390cecf9d5Sandi    }
4400cecf9d5Sandi
4413dd5c225SAndreas Gohr    /**
4423dd5c225SAndreas Gohr     * Open an ordered list
4433dd5c225SAndreas Gohr     */
4440cecf9d5Sandi    function listo_open() {
445a2d649c4Sandi        $this->doc .= '<ol>'.DOKU_LF;
4460cecf9d5Sandi    }
4470cecf9d5Sandi
4483dd5c225SAndreas Gohr    /**
4493dd5c225SAndreas Gohr     * Close an ordered list
4503dd5c225SAndreas Gohr     */
4510cecf9d5Sandi    function listo_close() {
452a2d649c4Sandi        $this->doc .= '</ol>'.DOKU_LF;
4530cecf9d5Sandi    }
4540cecf9d5Sandi
4553dd5c225SAndreas Gohr    /**
4563dd5c225SAndreas Gohr     * Open a list item
4573dd5c225SAndreas Gohr     *
4583dd5c225SAndreas Gohr     * @param int $level the nesting level
4593dd5c225SAndreas Gohr     */
4600cecf9d5Sandi    function listitem_open($level) {
46159869a4bSAnika Henke        $this->doc .= '<li class="level'.$level.'">';
4620cecf9d5Sandi    }
4630cecf9d5Sandi
4643dd5c225SAndreas Gohr    /**
4653dd5c225SAndreas Gohr     * Close a list item
4663dd5c225SAndreas Gohr     */
4670cecf9d5Sandi    function listitem_close() {
468a2d649c4Sandi        $this->doc .= '</li>'.DOKU_LF;
4690cecf9d5Sandi    }
4700cecf9d5Sandi
4713dd5c225SAndreas Gohr    /**
4723dd5c225SAndreas Gohr     * Start the content of a list item
4733dd5c225SAndreas Gohr     */
4740cecf9d5Sandi    function listcontent_open() {
47590db23d7Schris        $this->doc .= '<div class="li">';
4760cecf9d5Sandi    }
4770cecf9d5Sandi
4783dd5c225SAndreas Gohr    /**
4793dd5c225SAndreas Gohr     * Stop the content of a list item
4803dd5c225SAndreas Gohr     */
4810cecf9d5Sandi    function listcontent_close() {
48259869a4bSAnika Henke        $this->doc .= '</div>'.DOKU_LF;
4830cecf9d5Sandi    }
4840cecf9d5Sandi
4853dd5c225SAndreas Gohr    /**
4863dd5c225SAndreas Gohr     * Output unformatted $text
4873dd5c225SAndreas Gohr     *
4883dd5c225SAndreas Gohr     * Defaults to $this->cdata()
4893dd5c225SAndreas Gohr     *
4903dd5c225SAndreas Gohr     * @param string $text
4913dd5c225SAndreas Gohr     */
4920cecf9d5Sandi    function unformatted($text) {
493a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
4940cecf9d5Sandi    }
4950cecf9d5Sandi
4960cecf9d5Sandi    /**
4973fd0b676Sandi     * Execute PHP code if allowed
4983fd0b676Sandi     *
499d9764001SMichael Hamann     * @param  string $text      PHP code that is either executed or printed
5005d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['phpok'] is okff
5015d568b99SChris Smith     *
5023fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5030cecf9d5Sandi     */
5045d568b99SChris Smith    function php($text, $wrapper = 'code') {
50535a56260SChris Smith        global $conf;
50635a56260SChris Smith
507d86d5af0SChris Smith        if($conf['phpok']) {
508bad0b545Sandi            ob_start();
5094de671bcSandi            eval($text);
5103fd0b676Sandi            $this->doc .= ob_get_contents();
511bad0b545Sandi            ob_end_clean();
512d86d5af0SChris Smith        } else {
5135d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'php', $wrapper);
514d86d5af0SChris Smith        }
5150cecf9d5Sandi    }
5160cecf9d5Sandi
5173dd5c225SAndreas Gohr    /**
5183dd5c225SAndreas Gohr     * Output block level PHP code
5193dd5c225SAndreas Gohr     *
5203dd5c225SAndreas Gohr     * If $conf['phpok'] is true this should evaluate the given code and append the result
5213dd5c225SAndreas Gohr     * to $doc
5223dd5c225SAndreas Gohr     *
5233dd5c225SAndreas Gohr     * @param string $text The PHP code
5243dd5c225SAndreas Gohr     */
52507f89c3cSAnika Henke    function phpblock($text) {
5265d568b99SChris Smith        $this->php($text, 'pre');
52707f89c3cSAnika Henke    }
52807f89c3cSAnika Henke
5290cecf9d5Sandi    /**
5303fd0b676Sandi     * Insert HTML if allowed
5313fd0b676Sandi     *
532d9764001SMichael Hamann     * @param  string $text      html text
5335d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['htmlok'] is okff
5345d568b99SChris Smith     *
5353fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5360cecf9d5Sandi     */
5375d568b99SChris Smith    function html($text, $wrapper = 'code') {
53835a56260SChris Smith        global $conf;
53935a56260SChris Smith
540d86d5af0SChris Smith        if($conf['htmlok']) {
541a2d649c4Sandi            $this->doc .= $text;
542d86d5af0SChris Smith        } else {
5435d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'html4strict', $wrapper);
544d86d5af0SChris Smith        }
5454de671bcSandi    }
5460cecf9d5Sandi
5473dd5c225SAndreas Gohr    /**
5483dd5c225SAndreas Gohr     * Output raw block-level HTML
5493dd5c225SAndreas Gohr     *
5503dd5c225SAndreas Gohr     * If $conf['htmlok'] is true this should add the code as is to $doc
5513dd5c225SAndreas Gohr     *
5523dd5c225SAndreas Gohr     * @param string $text The HTML
5533dd5c225SAndreas Gohr     */
55407f89c3cSAnika Henke    function htmlblock($text) {
5555d568b99SChris Smith        $this->html($text, 'pre');
55607f89c3cSAnika Henke    }
55707f89c3cSAnika Henke
5583dd5c225SAndreas Gohr    /**
5593dd5c225SAndreas Gohr     * Start a block quote
5603dd5c225SAndreas Gohr     */
5610cecf9d5Sandi    function quote_open() {
56296331712SAnika Henke        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
5630cecf9d5Sandi    }
5640cecf9d5Sandi
5653dd5c225SAndreas Gohr    /**
5663dd5c225SAndreas Gohr     * Stop a block quote
5673dd5c225SAndreas Gohr     */
5680cecf9d5Sandi    function quote_close() {
56996331712SAnika Henke        $this->doc .= '</div></blockquote>'.DOKU_LF;
5700cecf9d5Sandi    }
5710cecf9d5Sandi
5723dd5c225SAndreas Gohr    /**
5733dd5c225SAndreas Gohr     * Output preformatted text
5743dd5c225SAndreas Gohr     *
5753dd5c225SAndreas Gohr     * @param string $text
5763dd5c225SAndreas Gohr     */
5773d491f75SAndreas Gohr    function preformatted($text) {
578c9250713SAnika Henke        $this->doc .= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF;
5793d491f75SAndreas Gohr    }
5803d491f75SAndreas Gohr
5813dd5c225SAndreas Gohr    /**
5823dd5c225SAndreas Gohr     * Display text as file content, optionally syntax highlighted
5833dd5c225SAndreas Gohr     *
5843dd5c225SAndreas Gohr     * @param string $text     text to show
5853dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5863dd5c225SAndreas Gohr     * @param string $filename file path label
5873dd5c225SAndreas Gohr     */
5883d491f75SAndreas Gohr    function file($text, $language = null, $filename = null) {
5893d491f75SAndreas Gohr        $this->_highlight('file', $text, $language, $filename);
5903d491f75SAndreas Gohr    }
5913d491f75SAndreas Gohr
5923dd5c225SAndreas Gohr    /**
5933dd5c225SAndreas Gohr     * Display text as code content, optionally syntax highlighted
5943dd5c225SAndreas Gohr     *
5953dd5c225SAndreas Gohr     * @param string $text     text to show
5963dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5973dd5c225SAndreas Gohr     * @param string $filename file path label
5983dd5c225SAndreas Gohr     */
5993d491f75SAndreas Gohr    function code($text, $language = null, $filename = null) {
6003d491f75SAndreas Gohr        $this->_highlight('code', $text, $language, $filename);
6013d491f75SAndreas Gohr    }
6023d491f75SAndreas Gohr
6030cecf9d5Sandi    /**
6043d491f75SAndreas Gohr     * Use GeSHi to highlight language syntax in code and file blocks
6053fd0b676Sandi     *
6063fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
6073dd5c225SAndreas Gohr     * @param string $type     code|file
6083dd5c225SAndreas Gohr     * @param string $text     text to show
6093dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6103dd5c225SAndreas Gohr     * @param string $filename file path label
6110cecf9d5Sandi     */
6123d491f75SAndreas Gohr    function _highlight($type, $text, $language = null, $filename = null) {
6133d491f75SAndreas Gohr        global $ID;
6143d491f75SAndreas Gohr        global $lang;
6153d491f75SAndreas Gohr
6163d491f75SAndreas Gohr        if($filename) {
617190c56e8SAndreas Gohr            // add icon
61827bf7924STom N Harris            list($ext) = mimetype($filename, false);
619190c56e8SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
620190c56e8SAndreas Gohr            $class = 'mediafile mf_'.$class;
621190c56e8SAndreas Gohr
6223d491f75SAndreas Gohr            $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
623190c56e8SAndreas Gohr            $this->doc .= '<dt><a href="'.exportlink($ID, 'code', array('codeblock' => $this->_codeblock)).'" title="'.$lang['download'].'" class="'.$class.'">';
6243d491f75SAndreas Gohr            $this->doc .= hsc($filename);
6253d491f75SAndreas Gohr            $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
6263d491f75SAndreas Gohr        }
6270cecf9d5Sandi
628d43aac1cSGina Haeussge        if($text{0} == "\n") {
629d43aac1cSGina Haeussge            $text = substr($text, 1);
630d43aac1cSGina Haeussge        }
631d43aac1cSGina Haeussge        if(substr($text, -1) == "\n") {
632d43aac1cSGina Haeussge            $text = substr($text, 0, -1);
633d43aac1cSGina Haeussge        }
634d43aac1cSGina Haeussge
6350cecf9d5Sandi        if(is_null($language)) {
6363d491f75SAndreas Gohr            $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
6370cecf9d5Sandi        } else {
6383d491f75SAndreas Gohr            $class = 'code'; //we always need the code class to make the syntax highlighting apply
6393d491f75SAndreas Gohr            if($type != 'code') $class .= ' '.$type;
6403d491f75SAndreas Gohr
6413d491f75SAndreas Gohr            $this->doc .= "<pre class=\"$class $language\">".p_xhtml_cached_geshi($text, $language, '').'</pre>'.DOKU_LF;
6420cecf9d5Sandi        }
6433d491f75SAndreas Gohr
6443d491f75SAndreas Gohr        if($filename) {
6453d491f75SAndreas Gohr            $this->doc .= '</dd></dl>'.DOKU_LF;
6463d491f75SAndreas Gohr        }
6473d491f75SAndreas Gohr
6483d491f75SAndreas Gohr        $this->_codeblock++;
6490cecf9d5Sandi    }
6500cecf9d5Sandi
6513dd5c225SAndreas Gohr    /**
6523dd5c225SAndreas Gohr     * Format an acronym
6533dd5c225SAndreas Gohr     *
6543dd5c225SAndreas Gohr     * Uses $this->acronyms
6553dd5c225SAndreas Gohr     *
6563dd5c225SAndreas Gohr     * @param string $acronym
6573dd5c225SAndreas Gohr     */
6580cecf9d5Sandi    function acronym($acronym) {
6590cecf9d5Sandi
6600cecf9d5Sandi        if(array_key_exists($acronym, $this->acronyms)) {
6610cecf9d5Sandi
662433bef32Sandi            $title = $this->_xmlEntities($this->acronyms[$acronym]);
6630cecf9d5Sandi
664940db3a3SAnika Henke            $this->doc .= '<abbr title="'.$title
665940db3a3SAnika Henke                .'">'.$this->_xmlEntities($acronym).'</abbr>';
6660cecf9d5Sandi
6670cecf9d5Sandi        } else {
668a2d649c4Sandi            $this->doc .= $this->_xmlEntities($acronym);
6690cecf9d5Sandi        }
6700cecf9d5Sandi    }
6710cecf9d5Sandi
6723dd5c225SAndreas Gohr    /**
6733dd5c225SAndreas Gohr     * Format a smiley
6743dd5c225SAndreas Gohr     *
6753dd5c225SAndreas Gohr     * Uses $this->smiley
6763dd5c225SAndreas Gohr     *
6773dd5c225SAndreas Gohr     * @param string $smiley
6783dd5c225SAndreas Gohr     */
6790cecf9d5Sandi    function smiley($smiley) {
6800cecf9d5Sandi        if(array_key_exists($smiley, $this->smileys)) {
681f62ea8a1Sandi            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
6828e38227fSAnika Henke                '" class="icon" alt="'.
683433bef32Sandi                $this->_xmlEntities($smiley).'" />';
6840cecf9d5Sandi        } else {
685a2d649c4Sandi            $this->doc .= $this->_xmlEntities($smiley);
6860cecf9d5Sandi        }
6870cecf9d5Sandi    }
6880cecf9d5Sandi
6893dd5c225SAndreas Gohr    /**
6903dd5c225SAndreas Gohr     * Format an entity
6913dd5c225SAndreas Gohr     *
6923dd5c225SAndreas Gohr     * Entities are basically small text replacements
6933dd5c225SAndreas Gohr     *
6943dd5c225SAndreas Gohr     * Uses $this->entities
6953dd5c225SAndreas Gohr     *
6963dd5c225SAndreas Gohr     * @param string $entity
6974de671bcSandi     */
6980cecf9d5Sandi    function entity($entity) {
6990cecf9d5Sandi        if(array_key_exists($entity, $this->entities)) {
700a2d649c4Sandi            $this->doc .= $this->entities[$entity];
7010cecf9d5Sandi        } else {
702a2d649c4Sandi            $this->doc .= $this->_xmlEntities($entity);
7030cecf9d5Sandi        }
7040cecf9d5Sandi    }
7050cecf9d5Sandi
7063dd5c225SAndreas Gohr    /**
7073dd5c225SAndreas Gohr     * Typographically format a multiply sign
7083dd5c225SAndreas Gohr     *
7093dd5c225SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
7103dd5c225SAndreas Gohr     *
7113dd5c225SAndreas Gohr     * @param string|int $x first value
7123dd5c225SAndreas Gohr     * @param string|int $y second value
7133dd5c225SAndreas Gohr     */
7140cecf9d5Sandi    function multiplyentity($x, $y) {
715a2d649c4Sandi        $this->doc .= "$x&times;$y";
7160cecf9d5Sandi    }
7170cecf9d5Sandi
7183dd5c225SAndreas Gohr    /**
7193dd5c225SAndreas Gohr     * Render an opening single quote char (language specific)
7203dd5c225SAndreas Gohr     */
7210cecf9d5Sandi    function singlequoteopening() {
72271b40da2SAnika Henke        global $lang;
72371b40da2SAnika Henke        $this->doc .= $lang['singlequoteopening'];
7240cecf9d5Sandi    }
7250cecf9d5Sandi
7263dd5c225SAndreas Gohr    /**
7273dd5c225SAndreas Gohr     * Render a closing single quote char (language specific)
7283dd5c225SAndreas Gohr     */
7290cecf9d5Sandi    function singlequoteclosing() {
73071b40da2SAnika Henke        global $lang;
73171b40da2SAnika Henke        $this->doc .= $lang['singlequoteclosing'];
7320cecf9d5Sandi    }
7330cecf9d5Sandi
7343dd5c225SAndreas Gohr    /**
7353dd5c225SAndreas Gohr     * Render an apostrophe char (language specific)
7363dd5c225SAndreas Gohr     */
73757d757d1SAndreas Gohr    function apostrophe() {
73857d757d1SAndreas Gohr        global $lang;
739a8bd192aSAndreas Gohr        $this->doc .= $lang['apostrophe'];
74057d757d1SAndreas Gohr    }
74157d757d1SAndreas Gohr
7423dd5c225SAndreas Gohr    /**
7433dd5c225SAndreas Gohr     * Render an opening double quote char (language specific)
7443dd5c225SAndreas Gohr     */
7450cecf9d5Sandi    function doublequoteopening() {
74671b40da2SAnika Henke        global $lang;
74771b40da2SAnika Henke        $this->doc .= $lang['doublequoteopening'];
7480cecf9d5Sandi    }
7490cecf9d5Sandi
7503dd5c225SAndreas Gohr    /**
7513dd5c225SAndreas Gohr     * Render an closinging double quote char (language specific)
7523dd5c225SAndreas Gohr     */
7530cecf9d5Sandi    function doublequoteclosing() {
75471b40da2SAnika Henke        global $lang;
75571b40da2SAnika Henke        $this->doc .= $lang['doublequoteclosing'];
7560cecf9d5Sandi    }
7570cecf9d5Sandi
7580cecf9d5Sandi    /**
7593dd5c225SAndreas Gohr     * Render a CamelCase link
7603dd5c225SAndreas Gohr     *
7613dd5c225SAndreas Gohr     * @param string $link The link name
7623dd5c225SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
7630cecf9d5Sandi     */
7640cecf9d5Sandi    function camelcaselink($link) {
76511d0aa47Sandi        $this->internallink($link, $link);
7660cecf9d5Sandi    }
7670cecf9d5Sandi
7683dd5c225SAndreas Gohr    /**
7693dd5c225SAndreas Gohr     * Render a page local link
7703dd5c225SAndreas Gohr     *
7713dd5c225SAndreas Gohr     * @param string $hash hash link identifier
7723dd5c225SAndreas Gohr     * @param string $name name for the link
7733dd5c225SAndreas Gohr     */
7740ea51e63SMatt Perry    function locallink($hash, $name = null) {
7750b7c14c2Sandi        global $ID;
7760b7c14c2Sandi        $name  = $this->_getLinkTitle($name, $hash, $isImage);
7770b7c14c2Sandi        $hash  = $this->_headerToLink($hash);
778e260f93bSAnika Henke        $title = $ID.' ↵';
7790b7c14c2Sandi        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
7800b7c14c2Sandi        $this->doc .= $name;
7810b7c14c2Sandi        $this->doc .= '</a>';
7820b7c14c2Sandi    }
7830b7c14c2Sandi
784cffcc403Sandi    /**
7853fd0b676Sandi     * Render an internal Wiki Link
7863fd0b676Sandi     *
787fe9ec250SChris Smith     * $search,$returnonly & $linktype are not for the renderer but are used
788cffcc403Sandi     * elsewhere - no need to implement them in other renderers
7893fd0b676Sandi     *
7903dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
791f23eef27SGerrit Uitslag     * @param string      $id         pageid
792f23eef27SGerrit Uitslag     * @param string|null $name       link name
793f23eef27SGerrit Uitslag     * @param string|null $search     adds search url param
794f23eef27SGerrit Uitslag     * @param bool        $returnonly whether to return html or write to doc attribute
795f23eef27SGerrit Uitslag     * @param string      $linktype   type to set use of headings
796f23eef27SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
797cffcc403Sandi     */
7980ea51e63SMatt Perry    function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
799ba11bd29Sandi        global $conf;
80037e34a5eSandi        global $ID;
801c4dda6afSAnika Henke        global $INFO;
80244653a53SAdrian Lang
8033d5e07d9SAdrian Lang        $params = '';
8043d5e07d9SAdrian Lang        $parts  = explode('?', $id, 2);
8053d5e07d9SAdrian Lang        if(count($parts) === 2) {
8063d5e07d9SAdrian Lang            $id     = $parts[0];
8073d5e07d9SAdrian Lang            $params = $parts[1];
80844653a53SAdrian Lang        }
80944653a53SAdrian Lang
810fda14ffcSIzidor Matušov        // For empty $id we need to know the current $ID
811fda14ffcSIzidor Matušov        // We need this check because _simpleTitle needs
812fda14ffcSIzidor Matušov        // correct $id and resolve_pageid() use cleanID($id)
813fda14ffcSIzidor Matušov        // (some things could be lost)
814fda14ffcSIzidor Matušov        if($id === '') {
815fda14ffcSIzidor Matušov            $id = $ID;
816fda14ffcSIzidor Matušov        }
817fda14ffcSIzidor Matušov
8180339c872Sjan        // default name is based on $id as given
8190339c872Sjan        $default = $this->_simpleTitle($id);
820ad32e47eSAndreas Gohr
8210339c872Sjan        // now first resolve and clean up the $id
82237e34a5eSandi        resolve_pageid(getNS($ID), $id, $exists);
823fda14ffcSIzidor Matušov
82459bc3b48SGerrit Uitslag        $link = array();
825fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
8260e1c636eSandi        if(!$isImage) {
8270e1c636eSandi            if($exists) {
828ba11bd29Sandi                $class = 'wikilink1';
8290cecf9d5Sandi            } else {
830ba11bd29Sandi                $class       = 'wikilink2';
83144a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
8320cecf9d5Sandi            }
8330cecf9d5Sandi        } else {
834ba11bd29Sandi            $class = 'media';
8350cecf9d5Sandi        }
8360cecf9d5Sandi
837a1685bedSandi        //keep hash anchor
8386d2af55dSChristopher Smith        @list($id, $hash) = explode('#', $id, 2);
839943dedc6SAndreas Gohr        if(!empty($hash)) $hash = $this->_headerToLink($hash);
840a1685bedSandi
841ba11bd29Sandi        //prepare for formating
842ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
843ba11bd29Sandi        $link['style']  = '';
844ba11bd29Sandi        $link['pre']    = '';
845ba11bd29Sandi        $link['suf']    = '';
84640eb54bbSjan        // highlight link to current page
847c4dda6afSAnika Henke        if($id == $INFO['id']) {
84892795d04Sandi            $link['pre'] = '<span class="curid">';
84992795d04Sandi            $link['suf'] = '</span>';
85040eb54bbSjan        }
8515e163278SAndreas Gohr        $link['more']  = '';
852ba11bd29Sandi        $link['class'] = $class;
85344653a53SAdrian Lang        $link['url']   = wl($id, $params);
854ba11bd29Sandi        $link['name']  = $name;
855ba11bd29Sandi        $link['title'] = $id;
856723d78dbSandi        //add search string
857723d78dbSandi        if($search) {
858546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
859546d3a99SAndreas Gohr            if(is_array($search)) {
860546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
861546d3a99SAndreas Gohr                $link['url'] .= 's[]='.join('&amp;s[]=', $search);
862546d3a99SAndreas Gohr            } else {
863546d3a99SAndreas Gohr                $link['url'] .= 's='.rawurlencode($search);
864546d3a99SAndreas Gohr            }
865723d78dbSandi        }
866723d78dbSandi
867a1685bedSandi        //keep hash
868a1685bedSandi        if($hash) $link['url'] .= '#'.$hash;
869a1685bedSandi
870ba11bd29Sandi        //output formatted
871cffcc403Sandi        if($returnonly) {
872cffcc403Sandi            return $this->_formatLink($link);
873cffcc403Sandi        } else {
874a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
8750cecf9d5Sandi        }
876cffcc403Sandi    }
8770cecf9d5Sandi
8783dd5c225SAndreas Gohr    /**
8793dd5c225SAndreas Gohr     * Render an external link
8803dd5c225SAndreas Gohr     *
8813dd5c225SAndreas Gohr     * @param string       $url  full URL with scheme
8823dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
8833dd5c225SAndreas Gohr     */
8840ea51e63SMatt Perry    function externallink($url, $name = null) {
885b625487dSandi        global $conf;
8860cecf9d5Sandi
887433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
8886f0c5dbfSandi
889b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
890b52b1596SAndreas Gohr        if(is_null($this->schemes)) $this->schemes = getSchemes();
891b52b1596SAndreas Gohr        list($scheme) = explode('://', $url);
892b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
893b52b1596SAndreas Gohr        if(!in_array($scheme, $this->schemes)) $url = '';
894b52b1596SAndreas Gohr
895b52b1596SAndreas Gohr        // is there still an URL?
896b52b1596SAndreas Gohr        if(!$url) {
897b52b1596SAndreas Gohr            $this->doc .= $name;
898b52b1596SAndreas Gohr            return;
899b52b1596SAndreas Gohr        }
900b52b1596SAndreas Gohr
901b52b1596SAndreas Gohr        // set class
9020cecf9d5Sandi        if(!$isImage) {
903b625487dSandi            $class = 'urlextern';
9040cecf9d5Sandi        } else {
905b625487dSandi            $class = 'media';
9060cecf9d5Sandi        }
9070cecf9d5Sandi
908b625487dSandi        //prepare for formating
90959bc3b48SGerrit Uitslag        $link = array();
910b625487dSandi        $link['target'] = $conf['target']['extern'];
911b625487dSandi        $link['style']  = '';
912b625487dSandi        $link['pre']    = '';
913b625487dSandi        $link['suf']    = '';
9145e163278SAndreas Gohr        $link['more']   = '';
915b625487dSandi        $link['class']  = $class;
916b625487dSandi        $link['url']    = $url;
917e1c10e4dSchris
918b625487dSandi        $link['name']  = $name;
919433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
920b625487dSandi        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
9210cecf9d5Sandi
922b625487dSandi        //output formatted
923a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9240cecf9d5Sandi    }
9250cecf9d5Sandi
9260cecf9d5Sandi    /**
9273dd5c225SAndreas Gohr     * Render an interwiki link
9283dd5c225SAndreas Gohr     *
9293dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
9303dd5c225SAndreas Gohr     *
9313dd5c225SAndreas Gohr     * @param string       $match     original link - probably not much use
9323dd5c225SAndreas Gohr     * @param string|array $name      name for the link, array for media file
9333dd5c225SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
9343dd5c225SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
9350cecf9d5Sandi     */
9360ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
937b625487dSandi        global $conf;
9380cecf9d5Sandi
93997a3e4e3Sandi        $link           = array();
94097a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
94197a3e4e3Sandi        $link['pre']    = '';
94297a3e4e3Sandi        $link['suf']    = '';
9435e163278SAndreas Gohr        $link['more']   = '';
944433bef32Sandi        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
9450cecf9d5Sandi
94697a3e4e3Sandi        //get interwiki URL
9476496c33fSGerrit Uitslag        $exists = null;
9486496c33fSGerrit Uitslag        $url    = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
9490cecf9d5Sandi
95097a3e4e3Sandi        if(!$isImage) {
9519d2ddea4SAndreas Gohr            $class         = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
9529d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
9531c2d1019SAndreas Gohr        } else {
9541c2d1019SAndreas Gohr            $link['class'] = 'media';
95597a3e4e3Sandi        }
9560cecf9d5Sandi
95797a3e4e3Sandi        //do we stay at the same server? Use local target
9582345e871SGerrit Uitslag        if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
95997a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
96097a3e4e3Sandi        }
9616496c33fSGerrit Uitslag        if($exists !== null && !$isImage) {
9626496c33fSGerrit Uitslag            if($exists) {
9636496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
9646496c33fSGerrit Uitslag            } else {
9656496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
9666496c33fSGerrit Uitslag                $link['rel'] = 'nofollow';
9676496c33fSGerrit Uitslag            }
9686496c33fSGerrit Uitslag        }
9690cecf9d5Sandi
97097a3e4e3Sandi        $link['url']   = $url;
97197a3e4e3Sandi        $link['title'] = htmlspecialchars($link['url']);
97297a3e4e3Sandi
97397a3e4e3Sandi        //output formatted
974a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9750cecf9d5Sandi    }
9760cecf9d5Sandi
9770cecf9d5Sandi    /**
9783dd5c225SAndreas Gohr     * Link to windows share
9793dd5c225SAndreas Gohr     *
9803dd5c225SAndreas Gohr     * @param string       $url  the link
9813dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
9820cecf9d5Sandi     */
9830ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
9841d47afe1Sandi        global $conf;
9853dd5c225SAndreas Gohr
9861d47afe1Sandi        //simple setup
98759bc3b48SGerrit Uitslag        $link = array();
9881d47afe1Sandi        $link['target'] = $conf['target']['windows'];
9891d47afe1Sandi        $link['pre']    = '';
9901d47afe1Sandi        $link['suf']    = '';
9911d47afe1Sandi        $link['style']  = '';
9920cecf9d5Sandi
993433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
9940cecf9d5Sandi        if(!$isImage) {
9951d47afe1Sandi            $link['class'] = 'windows';
9960cecf9d5Sandi        } else {
9971d47afe1Sandi            $link['class'] = 'media';
9980cecf9d5Sandi        }
9990cecf9d5Sandi
1000433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
10011d47afe1Sandi        $url           = str_replace('\\', '/', $url);
10021d47afe1Sandi        $url           = 'file:///'.$url;
10031d47afe1Sandi        $link['url']   = $url;
10040cecf9d5Sandi
10051d47afe1Sandi        //output formatted
1006a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10070cecf9d5Sandi    }
10080cecf9d5Sandi
10093dd5c225SAndreas Gohr    /**
10103dd5c225SAndreas Gohr     * Render a linked E-Mail Address
10113dd5c225SAndreas Gohr     *
10123dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
10133dd5c225SAndreas Gohr     *
10143dd5c225SAndreas Gohr     * @param string       $address Email-Address
10153dd5c225SAndreas Gohr     * @param string|array $name    name for the link, array for media file
10163dd5c225SAndreas Gohr     */
10170ea51e63SMatt Perry    function emaillink($address, $name = null) {
101871352defSandi        global $conf;
101971352defSandi        //simple setup
102071352defSandi        $link           = array();
102171352defSandi        $link['target'] = '';
102271352defSandi        $link['pre']    = '';
102371352defSandi        $link['suf']    = '';
102471352defSandi        $link['style']  = '';
102571352defSandi        $link['more']   = '';
10260cecf9d5Sandi
1027c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
10280cecf9d5Sandi        if(!$isImage) {
1029be96545cSAnika Henke            $link['class'] = 'mail';
10300cecf9d5Sandi        } else {
1031be96545cSAnika Henke            $link['class'] = 'media';
10320cecf9d5Sandi        }
10330cecf9d5Sandi
103407738714SAndreas Gohr        $address = $this->_xmlEntities($address);
103500a7b5adSEsther Brunner        $address = obfuscate($address);
103600a7b5adSEsther Brunner        $title   = $address;
10378c128049SAndreas Gohr
103871352defSandi        if(empty($name)) {
103900a7b5adSEsther Brunner            $name = $address;
104071352defSandi        }
10410cecf9d5Sandi
1042776b36ecSAndreas Gohr        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1043776b36ecSAndreas Gohr
1044776b36ecSAndreas Gohr        $link['url']   = 'mailto:'.$address;
104571352defSandi        $link['name']  = $name;
104671352defSandi        $link['title'] = $title;
10470cecf9d5Sandi
104871352defSandi        //output formatted
1049a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10500cecf9d5Sandi    }
10510cecf9d5Sandi
10523dd5c225SAndreas Gohr    /**
10533dd5c225SAndreas Gohr     * Render an internal media file
10543dd5c225SAndreas Gohr     *
10553dd5c225SAndreas Gohr     * @param string $src       media ID
10563dd5c225SAndreas Gohr     * @param string $title     descriptive text
10573dd5c225SAndreas Gohr     * @param string $align     left|center|right
10583dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
10593dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
10603dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
10613dd5c225SAndreas Gohr     * @param string $linking   linkonly|detail|nolink
10623dd5c225SAndreas Gohr     * @param bool   $return    return HTML instead of adding to $doc
10633dd5c225SAndreas Gohr     * @return void|string
10643dd5c225SAndreas Gohr     */
10650ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
10663dd5c225SAndreas Gohr                           $height = null, $cache = null, $linking = null, $return = false) {
106737e34a5eSandi        global $ID;
106891df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
106937e34a5eSandi        resolve_mediaid(getNS($ID), $src, $exists);
10700cecf9d5Sandi
1071d98d4540SBen Coburn        $noLink = false;
10728acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1073b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
10743685f775Sandi
10753dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1076b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
1077dc673a5bSjoe.lapp            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), ($linking == 'direct'));
1078f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
10792a2a2ba2SAnika Henke            // don't link movies
108044881bd0Shenning.noren            $noLink = true;
108155efc227SAndreas Gohr        } else {
10822ca14335SEsther Brunner            // add file icons
10839d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
10849d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_'.$class;
10856de3759aSAndreas Gohr            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), true);
108691328684SMichael Hamann            if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
108755efc227SAndreas Gohr        }
10883685f775Sandi
108991df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
109091df343aSAndreas Gohr
10916fe20453SGina Haeussge        //markup non existing files
10924a24b459SKate Arzamastseva        if(!$exists) {
10936fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
10944a24b459SKate Arzamastseva        }
10956fe20453SGina Haeussge
10963685f775Sandi        //output formatted
1097f50634f0SAnika Henke        if($return) {
1098f50634f0SAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1099f50634f0SAnika Henke            else return $this->_formatLink($link);
1100f50634f0SAnika Henke        } else {
1101dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11022ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11030cecf9d5Sandi        }
1104f50634f0SAnika Henke    }
11050cecf9d5Sandi
11063dd5c225SAndreas Gohr    /**
11073dd5c225SAndreas Gohr     * Render an external media file
11083dd5c225SAndreas Gohr     *
11093dd5c225SAndreas Gohr     * @param string $src     full media URL
11103dd5c225SAndreas Gohr     * @param string $title   descriptive text
11113dd5c225SAndreas Gohr     * @param string $align   left|center|right
11123dd5c225SAndreas Gohr     * @param int    $width   width of media in pixel
11133dd5c225SAndreas Gohr     * @param int    $height  height of media in pixel
11143dd5c225SAndreas Gohr     * @param string $cache   cache|recache|nocache
11153dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1116410ee62aSAnika Henke     * @param bool   $return  return HTML instead of adding to $doc
11173dd5c225SAndreas Gohr     */
11180ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
1119410ee62aSAnika Henke                           $height = null, $cache = null, $linking = null, $return = false) {
112091df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
1121d98d4540SBen Coburn        $noLink = false;
11228acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1123b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1124b739ff0fSPierre Spring
1125b739ff0fSPierre Spring        $link['url'] = ml($src, array('cache' => $cache));
11263685f775Sandi
11273dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1128b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
11292ca14335SEsther Brunner            // link only jpeg images
113044881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1131f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
11322a2a2ba2SAnika Henke            // don't link movies
113344881bd0Shenning.noren            $noLink = true;
11342ca14335SEsther Brunner        } else {
11352ca14335SEsther Brunner            // add file icons
113627bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
113727bf7924STom N Harris            $link['class'] .= ' mediafile mf_'.$class;
11382ca14335SEsther Brunner        }
11392ca14335SEsther Brunner
114091df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
114191df343aSAndreas Gohr
11423685f775Sandi        //output formatted
1143410ee62aSAnika Henke        if($return) {
1144410ee62aSAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1145410ee62aSAnika Henke            else return $this->_formatLink($link);
1146410ee62aSAnika Henke        } else {
1147dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11482ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11490cecf9d5Sandi        }
1150410ee62aSAnika Henke    }
11510cecf9d5Sandi
11524826ab45Sandi    /**
11533db95becSAndreas Gohr     * Renders an RSS feed
1154b625487dSandi     *
1155b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1156b625487dSandi     */
11573db95becSAndreas Gohr    function rss($url, $params) {
1158b625487dSandi        global $lang;
11593db95becSAndreas Gohr        global $conf;
11603db95becSAndreas Gohr
11613db95becSAndreas Gohr        require_once(DOKU_INC.'inc/FeedParser.php');
11623db95becSAndreas Gohr        $feed = new FeedParser();
116300077af8SAndreas Gohr        $feed->set_feed_url($url);
1164b625487dSandi
1165b625487dSandi        //disable warning while fetching
11663dd5c225SAndreas Gohr        if(!defined('DOKU_E_LEVEL')) {
11673dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
11683dd5c225SAndreas Gohr        }
11693db95becSAndreas Gohr        $rc = $feed->init();
11703dd5c225SAndreas Gohr        if(isset($elvl)) {
11713dd5c225SAndreas Gohr            error_reporting($elvl);
11723dd5c225SAndreas Gohr        }
1173b625487dSandi
11743db95becSAndreas Gohr        //decide on start and end
11753db95becSAndreas Gohr        if($params['reverse']) {
11763db95becSAndreas Gohr            $mod   = -1;
11773db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
11783db95becSAndreas Gohr            $end   = $start - ($params['max']);
1179b2a412b0SAndreas Gohr            $end   = ($end < -1) ? -1 : $end;
11803db95becSAndreas Gohr        } else {
11813db95becSAndreas Gohr            $mod   = 1;
11823db95becSAndreas Gohr            $start = 0;
11833db95becSAndreas Gohr            $end   = $feed->get_item_quantity();
1184d91ab76fSMatt Perry            $end   = ($end > $params['max']) ? $params['max'] : $end;
11853db95becSAndreas Gohr        }
11863db95becSAndreas Gohr
1187a2d649c4Sandi        $this->doc .= '<ul class="rss">';
11883db95becSAndreas Gohr        if($rc) {
11893db95becSAndreas Gohr            for($x = $start; $x != $end; $x += $mod) {
11901bde1582SAndreas Gohr                $item = $feed->get_item($x);
11913db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
1192d2ea3363SAndreas Gohr                // support feeds without links
1193d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
1194d2ea3363SAndreas Gohr                if($lnkurl) {
1195793361f8SAndreas Gohr                    // title is escaped by SimplePie, we unescape here because it
1196793361f8SAndreas Gohr                    // is escaped again in externallink() FS#1705
11973dd5c225SAndreas Gohr                    $this->externallink(
11983dd5c225SAndreas Gohr                        $item->get_permalink(),
11993dd5c225SAndreas Gohr                        html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')
12003dd5c225SAndreas Gohr                    );
1201d2ea3363SAndreas Gohr                } else {
1202d2ea3363SAndreas Gohr                    $this->doc .= ' '.$item->get_title();
1203d2ea3363SAndreas Gohr                }
12043db95becSAndreas Gohr                if($params['author']) {
12051bde1582SAndreas Gohr                    $author = $item->get_author(0);
12061bde1582SAndreas Gohr                    if($author) {
12071bde1582SAndreas Gohr                        $name = $author->get_name();
12081bde1582SAndreas Gohr                        if(!$name) $name = $author->get_email();
12091bde1582SAndreas Gohr                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
12101bde1582SAndreas Gohr                    }
12113db95becSAndreas Gohr                }
12123db95becSAndreas Gohr                if($params['date']) {
12132e7e0c29SAndreas Gohr                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
12143db95becSAndreas Gohr                }
12151bde1582SAndreas Gohr                if($params['details']) {
12163db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
1217173dccb7STom N Harris                    if($conf['htmlok']) {
12181bde1582SAndreas Gohr                        $this->doc .= $item->get_description();
12193db95becSAndreas Gohr                    } else {
12201bde1582SAndreas Gohr                        $this->doc .= strip_tags($item->get_description());
12213db95becSAndreas Gohr                    }
12223db95becSAndreas Gohr                    $this->doc .= '</div>';
12233db95becSAndreas Gohr                }
12243db95becSAndreas Gohr
12253db95becSAndreas Gohr                $this->doc .= '</div></li>';
1226b625487dSandi            }
1227b625487dSandi        } else {
12283db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1229a2d649c4Sandi            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
1230b625487dSandi            $this->externallink($url);
123145e147ccSAndreas Gohr            if($conf['allowdebug']) {
123245e147ccSAndreas Gohr                $this->doc .= '<!--'.hsc($feed->error).'-->';
123345e147ccSAndreas Gohr            }
12343db95becSAndreas Gohr            $this->doc .= '</div></li>';
1235b625487dSandi        }
1236a2d649c4Sandi        $this->doc .= '</ul>';
1237b625487dSandi    }
1238b625487dSandi
12393dd5c225SAndreas Gohr    /**
12403dd5c225SAndreas Gohr     * Start a table
12413dd5c225SAndreas Gohr     *
12423dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
12433dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
12443dd5c225SAndreas Gohr     * @param int $pos     byte position in the original source
12453dd5c225SAndreas Gohr     */
1246619736fdSAdrian Lang    function table_open($maxcols = null, $numrows = null, $pos = null) {
1247b5742cedSPierre Spring        // initialize the row counter used for classes
1248b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1249619736fdSAdrian Lang        $class                         = 'table';
1250619736fdSAdrian Lang        if($pos !== null) {
1251619736fdSAdrian Lang            $class .= ' '.$this->startSectionEdit($pos, 'table');
1252619736fdSAdrian Lang        }
1253619736fdSAdrian Lang        $this->doc .= '<div class="'.$class.'"><table class="inline">'.
1254619736fdSAdrian Lang            DOKU_LF;
12550cecf9d5Sandi    }
12560cecf9d5Sandi
12573dd5c225SAndreas Gohr    /**
12583dd5c225SAndreas Gohr     * Close a table
12593dd5c225SAndreas Gohr     *
12603dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
12613dd5c225SAndreas Gohr     */
1262619736fdSAdrian Lang    function table_close($pos = null) {
1263a8574918SAnika Henke        $this->doc .= '</table></div>'.DOKU_LF;
1264619736fdSAdrian Lang        if($pos !== null) {
126590df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
12660cecf9d5Sandi        }
1267619736fdSAdrian Lang    }
12680cecf9d5Sandi
12693dd5c225SAndreas Gohr    /**
12703dd5c225SAndreas Gohr     * Open a table header
12713dd5c225SAndreas Gohr     */
1272f05a1cc5SGerrit Uitslag    function tablethead_open() {
1273f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
1274f05a1cc5SGerrit Uitslag    }
1275f05a1cc5SGerrit Uitslag
12763dd5c225SAndreas Gohr    /**
12773dd5c225SAndreas Gohr     * Close a table header
12783dd5c225SAndreas Gohr     */
1279f05a1cc5SGerrit Uitslag    function tablethead_close() {
1280f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
1281f05a1cc5SGerrit Uitslag    }
1282f05a1cc5SGerrit Uitslag
12833dd5c225SAndreas Gohr    /**
12843dd5c225SAndreas Gohr     * Open a table row
12853dd5c225SAndreas Gohr     */
12860cecf9d5Sandi    function tablerow_open() {
1287b5742cedSPierre Spring        // initialize the cell counter used for classes
1288b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1289b5742cedSPierre Spring        $class                          = 'row'.$this->_counter['row_counter']++;
1290b5742cedSPierre Spring        $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
12910cecf9d5Sandi    }
12920cecf9d5Sandi
12933dd5c225SAndreas Gohr    /**
12943dd5c225SAndreas Gohr     * Close a table row
12953dd5c225SAndreas Gohr     */
12960cecf9d5Sandi    function tablerow_close() {
1297a2d649c4Sandi        $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
12980cecf9d5Sandi    }
12990cecf9d5Sandi
13003dd5c225SAndreas Gohr    /**
13013dd5c225SAndreas Gohr     * Open a table header cell
13023dd5c225SAndreas Gohr     *
13033dd5c225SAndreas Gohr     * @param int    $colspan
13043dd5c225SAndreas Gohr     * @param string $align left|center|right
13053dd5c225SAndreas Gohr     * @param int    $rowspan
13063dd5c225SAndreas Gohr     */
13070ea51e63SMatt Perry    function tableheader_open($colspan = 1, $align = null, $rowspan = 1) {
1308b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13090cecf9d5Sandi        if(!is_null($align)) {
1310b5742cedSPierre Spring            $class .= ' '.$align.'align';
13110cecf9d5Sandi        }
1312b5742cedSPierre Spring        $class .= '"';
1313b5742cedSPierre Spring        $this->doc .= '<th '.$class;
13140cecf9d5Sandi        if($colspan > 1) {
1315a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1316a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13170cecf9d5Sandi        }
131825b97867Shakan.sandell        if($rowspan > 1) {
131925b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
132025b97867Shakan.sandell        }
1321a2d649c4Sandi        $this->doc .= '>';
13220cecf9d5Sandi    }
13230cecf9d5Sandi
13243dd5c225SAndreas Gohr    /**
13253dd5c225SAndreas Gohr     * Close a table header cell
13263dd5c225SAndreas Gohr     */
13270cecf9d5Sandi    function tableheader_close() {
1328a2d649c4Sandi        $this->doc .= '</th>';
13290cecf9d5Sandi    }
13300cecf9d5Sandi
13313dd5c225SAndreas Gohr    /**
13323dd5c225SAndreas Gohr     * Open a table cell
13333dd5c225SAndreas Gohr     *
13343dd5c225SAndreas Gohr     * @param int    $colspan
13353dd5c225SAndreas Gohr     * @param string $align left|center|right
13363dd5c225SAndreas Gohr     * @param int    $rowspan
13373dd5c225SAndreas Gohr     */
13380ea51e63SMatt Perry    function tablecell_open($colspan = 1, $align = null, $rowspan = 1) {
1339b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13400cecf9d5Sandi        if(!is_null($align)) {
1341b5742cedSPierre Spring            $class .= ' '.$align.'align';
13420cecf9d5Sandi        }
1343b5742cedSPierre Spring        $class .= '"';
1344b5742cedSPierre Spring        $this->doc .= '<td '.$class;
13450cecf9d5Sandi        if($colspan > 1) {
1346a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1347a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13480cecf9d5Sandi        }
134925b97867Shakan.sandell        if($rowspan > 1) {
135025b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
135125b97867Shakan.sandell        }
1352a2d649c4Sandi        $this->doc .= '>';
13530cecf9d5Sandi    }
13540cecf9d5Sandi
13553dd5c225SAndreas Gohr    /**
13563dd5c225SAndreas Gohr     * Close a table cell
13573dd5c225SAndreas Gohr     */
13580cecf9d5Sandi    function tablecell_close() {
1359a2d649c4Sandi        $this->doc .= '</td>';
13600cecf9d5Sandi    }
13610cecf9d5Sandi
13623dd5c225SAndreas Gohr    #region Utility functions
13630cecf9d5Sandi
1364ba11bd29Sandi    /**
13653fd0b676Sandi     * Build a link
13663fd0b676Sandi     *
13673fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1368ba11bd29Sandi     *
1369ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1370ba11bd29Sandi     */
1371433bef32Sandi    function _formatLink($link) {
1372ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1373ba11bd29Sandi        if(substr($link['url'], 0, 7) != 'mailto:') {
1374ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1375ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1376ba11bd29Sandi        }
1377ba11bd29Sandi        //remove double encodings in titles
1378ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1379ba11bd29Sandi
1380453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1381453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1382453493f2SAndreas Gohr        $link['url']   = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1383453493f2SAndreas Gohr        $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
1384453493f2SAndreas Gohr
1385ba11bd29Sandi        $ret = '';
1386ba11bd29Sandi        $ret .= $link['pre'];
1387ba11bd29Sandi        $ret .= '<a href="'.$link['url'].'"';
1388bb4866bdSchris        if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1389bb4866bdSchris        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1390bb4866bdSchris        if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1391bb4866bdSchris        if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
139244a6b4c7SAndreas Gohr        if(!empty($link['rel'])) $ret .= ' rel="'.$link['rel'].'"';
1393bb4866bdSchris        if(!empty($link['more'])) $ret .= ' '.$link['more'];
1394ba11bd29Sandi        $ret .= '>';
1395ba11bd29Sandi        $ret .= $link['name'];
1396ba11bd29Sandi        $ret .= '</a>';
1397ba11bd29Sandi        $ret .= $link['suf'];
1398ba11bd29Sandi        return $ret;
1399ba11bd29Sandi    }
1400ba11bd29Sandi
1401ba11bd29Sandi    /**
14023fd0b676Sandi     * Renders internal and external media
14033fd0b676Sandi     *
14043fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
14053dd5c225SAndreas Gohr     * @param string $src       media ID
14063dd5c225SAndreas Gohr     * @param string $title     descriptive text
14073dd5c225SAndreas Gohr     * @param string $align     left|center|right
14083dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
14093dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
14103dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
14113dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
14123dd5c225SAndreas Gohr     * @return string
14133fd0b676Sandi     */
14140ea51e63SMatt Perry    function _media($src, $title = null, $align = null, $width = null,
14150ea51e63SMatt Perry                    $height = null, $cache = null, $render = true) {
14163fd0b676Sandi
14173fd0b676Sandi        $ret = '';
14183fd0b676Sandi
14193dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src);
14203fd0b676Sandi        if(substr($mime, 0, 5) == 'image') {
1421b739ff0fSPierre Spring            // first get the $title
1422b739ff0fSPierre Spring            if(!is_null($title)) {
1423b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1424b739ff0fSPierre Spring            } elseif($ext == 'jpg' || $ext == 'jpeg') {
1425b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1426b739ff0fSPierre Spring                require_once(DOKU_INC.'inc/JpegMeta.php');
142767f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1428b739ff0fSPierre Spring                if($jpeg !== false) $cap = $jpeg->getTitle();
14293dd5c225SAndreas Gohr                if(!empty($cap)) {
1430b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1431b739ff0fSPierre Spring                }
1432b739ff0fSPierre Spring            }
1433b739ff0fSPierre Spring            if(!$render) {
1434b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1435b739ff0fSPierre Spring                // return the title of the picture
1436b739ff0fSPierre Spring                if(!$title) {
1437b739ff0fSPierre Spring                    // just show the sourcename
14383009a773SAndreas Gohr                    $title = $this->_xmlEntities(utf8_basename(noNS($src)));
1439b739ff0fSPierre Spring                }
1440b739ff0fSPierre Spring                return $title;
1441b739ff0fSPierre Spring            }
14423fd0b676Sandi            //add image tag
14436de3759aSAndreas Gohr            $ret .= '<img src="'.ml($src, array('w' => $width, 'h' => $height, 'cache' => $cache)).'"';
14443fd0b676Sandi            $ret .= ' class="media'.$align.'"';
14453fd0b676Sandi
1446b739ff0fSPierre Spring            if($title) {
1447b739ff0fSPierre Spring                $ret .= ' title="'.$title.'"';
1448b739ff0fSPierre Spring                $ret .= ' alt="'.$title.'"';
14493fd0b676Sandi            } else {
14503fd0b676Sandi                $ret .= ' alt=""';
14513fd0b676Sandi            }
14523fd0b676Sandi
14533fd0b676Sandi            if(!is_null($width))
14543fd0b676Sandi                $ret .= ' width="'.$this->_xmlEntities($width).'"';
14553fd0b676Sandi
14563fd0b676Sandi            if(!is_null($height))
14573fd0b676Sandi                $ret .= ' height="'.$this->_xmlEntities($height).'"';
14583fd0b676Sandi
14593fd0b676Sandi            $ret .= ' />';
14603fd0b676Sandi
146117954bb5SAnika Henke        } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
14622a2a2ba2SAnika Henke            // first get the $title
146317954bb5SAnika Henke            $title = !is_null($title) ? $this->_xmlEntities($title) : false;
14642a2a2ba2SAnika Henke            if(!$render) {
146517954bb5SAnika Henke                // if the file is not supposed to be rendered
146617954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
146717954bb5SAnika Henke                return $title ? $title : $this->_xmlEntities(utf8_basename(noNS($src)));
14682a2a2ba2SAnika Henke            }
14692a2a2ba2SAnika Henke
14702a2a2ba2SAnika Henke            $att          = array();
14712a2a2ba2SAnika Henke            $att['class'] = "media$align";
147217954bb5SAnika Henke            if($title) {
147317954bb5SAnika Henke                $att['title'] = $title;
147417954bb5SAnika Henke            }
14752a2a2ba2SAnika Henke
147617954bb5SAnika Henke            if(media_supportedav($mime, 'video')) {
147717954bb5SAnika Henke                //add video
147879e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1479b44a5dceSAnika Henke            }
148017954bb5SAnika Henke            if(media_supportedav($mime, 'audio')) {
1481b44a5dceSAnika Henke                //add audio
1482b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
148317954bb5SAnika Henke            }
1484b44a5dceSAnika Henke
14853fd0b676Sandi        } elseif($mime == 'application/x-shockwave-flash') {
14861c882ba8SAndreas Gohr            if(!$render) {
14871c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
14881c882ba8SAndreas Gohr                // return the title of the flash
14891c882ba8SAndreas Gohr                if(!$title) {
14901c882ba8SAndreas Gohr                    // just show the sourcename
14913009a773SAndreas Gohr                    $title = utf8_basename(noNS($src));
14921c882ba8SAndreas Gohr                }
149307bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
14941c882ba8SAndreas Gohr            }
14951c882ba8SAndreas Gohr
149607bf32b2SAndreas Gohr            $att          = array();
149707bf32b2SAndreas Gohr            $att['class'] = "media$align";
149807bf32b2SAndreas Gohr            if($align == 'right') $att['align'] = 'right';
149907bf32b2SAndreas Gohr            if($align == 'left') $att['align'] = 'left';
15003dd5c225SAndreas Gohr            $ret .= html_flashobject(
15013dd5c225SAndreas Gohr                ml($src, array('cache' => $cache), true, '&'), $width, $height,
150207bf32b2SAndreas Gohr                array('quality' => 'high'),
150307bf32b2SAndreas Gohr                null,
150407bf32b2SAndreas Gohr                $att,
15053dd5c225SAndreas Gohr                $this->_xmlEntities($title)
15063dd5c225SAndreas Gohr            );
15070f428d7dSAndreas Gohr        } elseif($title) {
15083fd0b676Sandi            // well at least we have a title to display
15093fd0b676Sandi            $ret .= $this->_xmlEntities($title);
15103fd0b676Sandi        } else {
15115291ca3aSAndreas Gohr            // just show the sourcename
15123009a773SAndreas Gohr            $ret .= $this->_xmlEntities(utf8_basename(noNS($src)));
15133fd0b676Sandi        }
15143fd0b676Sandi
15153fd0b676Sandi        return $ret;
15163fd0b676Sandi    }
15173fd0b676Sandi
15183dd5c225SAndreas Gohr    /**
15193dd5c225SAndreas Gohr     * Escape string for output
15203dd5c225SAndreas Gohr     *
15213dd5c225SAndreas Gohr     * @param $string
15223dd5c225SAndreas Gohr     * @return string
15233dd5c225SAndreas Gohr     */
1524433bef32Sandi    function _xmlEntities($string) {
1525de117061Schris        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
15260cecf9d5Sandi    }
15270cecf9d5Sandi
15288a831f2bSAndreas Gohr    /**
15298a831f2bSAndreas Gohr     * Creates a linkid from a headline
1530c5a8fd96SAndreas Gohr     *
15313dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1532c5a8fd96SAndreas Gohr     * @param string  $title   The headline title
1533c5a8fd96SAndreas Gohr     * @param boolean $create  Create a new unique ID?
15343dd5c225SAndreas Gohr     * @return string
15358a831f2bSAndreas Gohr     */
1536c5a8fd96SAndreas Gohr    function _headerToLink($title, $create = false) {
1537c5a8fd96SAndreas Gohr        if($create) {
15384ceab83fSAndreas Gohr            return sectionID($title, $this->headers);
15394ceab83fSAndreas Gohr        } else {
1540443d207bSAndreas Gohr            $check = false;
1541443d207bSAndreas Gohr            return sectionID($title, $check);
1542c5a8fd96SAndreas Gohr        }
15430cecf9d5Sandi    }
15440cecf9d5Sandi
1545af587fa8Sandi    /**
15463fd0b676Sandi     * Construct a title and handle images in titles
15473fd0b676Sandi     *
15480b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15493dd5c225SAndreas Gohr     * @param string|array $title    either string title or media array
15503dd5c225SAndreas Gohr     * @param string       $default  default title if nothing else is found
15513dd5c225SAndreas Gohr     * @param bool         $isImage  will be set to true if it's a media file
15523dd5c225SAndreas Gohr     * @param null|string  $id       linked page id (used to extract title from first heading)
15533dd5c225SAndreas Gohr     * @param string       $linktype content|navigation
15543dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
15553fd0b676Sandi     */
15560ea51e63SMatt Perry    function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
155744881bd0Shenning.noren        $isImage = false;
155829657f9eSAndreas Gohr        if(is_array($title)) {
155929657f9eSAndreas Gohr            $isImage = true;
156029657f9eSAndreas Gohr            return $this->_imageTitle($title);
156129657f9eSAndreas Gohr        } elseif(is_null($title) || trim($title) == '') {
1562fe9ec250SChris Smith            if(useHeading($linktype) && $id) {
156367c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1564bb0a59d4Sjan                if($heading) {
1565433bef32Sandi                    return $this->_xmlEntities($heading);
1566bb0a59d4Sjan                }
1567bb0a59d4Sjan            }
1568433bef32Sandi            return $this->_xmlEntities($default);
156968c26e6dSMichael Klier        } else {
157068c26e6dSMichael Klier            return $this->_xmlEntities($title);
15710cecf9d5Sandi        }
15720cecf9d5Sandi    }
15730cecf9d5Sandi
15740cecf9d5Sandi    /**
15753dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
15763fd0b676Sandi     *
15773fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1578e0c26282SGerrit Uitslag     * @param array $img
15793dd5c225SAndreas Gohr     * @return string HTML img tag or similar
15800cecf9d5Sandi     */
1581433bef32Sandi    function _imageTitle($img) {
1582d9baf1a7SKazutaka Miyasaka        global $ID;
1583d9baf1a7SKazutaka Miyasaka
1584d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1585d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
15863dd5c225SAndreas Gohr        list($img['src']) = explode('#', $img['src'], 2);
1587d9baf1a7SKazutaka Miyasaka        if($img['type'] == 'internalmedia') {
1588d9baf1a7SKazutaka Miyasaka            resolve_mediaid(getNS($ID), $img['src'], $exists);
1589d9baf1a7SKazutaka Miyasaka        }
1590d9baf1a7SKazutaka Miyasaka
15913dd5c225SAndreas Gohr        return $this->_media(
15923dd5c225SAndreas Gohr            $img['src'],
15934826ab45Sandi            $img['title'],
15944826ab45Sandi            $img['align'],
15954826ab45Sandi            $img['width'],
15964826ab45Sandi            $img['height'],
15973dd5c225SAndreas Gohr            $img['cache']
15983dd5c225SAndreas Gohr        );
15990cecf9d5Sandi    }
1600b739ff0fSPierre Spring
1601b739ff0fSPierre Spring    /**
16023dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
16033dd5c225SAndreas Gohr     *
16043dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1605b739ff0fSPierre Spring     *
1606b739ff0fSPierre Spring     * @author   Pierre Spring <pierre.spring@liip.ch>
16073dd5c225SAndreas Gohr     * @param string $src       media ID
16083dd5c225SAndreas Gohr     * @param string $title     descriptive text
16093dd5c225SAndreas Gohr     * @param string $align     left|center|right
16103dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
16113dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
16123dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
16133dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
16143dd5c225SAndreas Gohr     * @return array associative array with link config
1615b739ff0fSPierre Spring     */
1616d91ab76fSMatt Perry    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1617b739ff0fSPierre Spring        global $conf;
1618b739ff0fSPierre Spring
1619b739ff0fSPierre Spring        $link           = array();
1620b739ff0fSPierre Spring        $link['class']  = 'media';
1621b739ff0fSPierre Spring        $link['style']  = '';
1622b739ff0fSPierre Spring        $link['pre']    = '';
1623b739ff0fSPierre Spring        $link['suf']    = '';
1624b739ff0fSPierre Spring        $link['more']   = '';
1625b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1626b739ff0fSPierre Spring        $link['title']  = $this->_xmlEntities($src);
1627b739ff0fSPierre Spring        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1628b739ff0fSPierre Spring
1629b739ff0fSPierre Spring        return $link;
1630b739ff0fSPierre Spring    }
163191459163SAnika Henke
16322a2a2ba2SAnika Henke    /**
16332a2a2ba2SAnika Henke     * Embed video(s) in HTML
16342a2a2ba2SAnika Henke     *
16352a2a2ba2SAnika Henke     * @author Anika Henke <anika@selfthinker.org>
16362a2a2ba2SAnika Henke     *
16372a2a2ba2SAnika Henke     * @param string $src         - ID of video to embed
16382a2a2ba2SAnika Henke     * @param int    $width       - width of the video in pixels
16392a2a2ba2SAnika Henke     * @param int    $height      - height of the video in pixels
16402a2a2ba2SAnika Henke     * @param array  $atts        - additional attributes for the <video> tag
1641f50634f0SAnika Henke     * @return string
16422a2a2ba2SAnika Henke     */
164379e53fe5SAnika Henke    function _video($src, $width, $height, $atts = null) {
16442a2a2ba2SAnika Henke        // prepare width and height
16452a2a2ba2SAnika Henke        if(is_null($atts)) $atts = array();
16462a2a2ba2SAnika Henke        $atts['width']  = (int) $width;
16472a2a2ba2SAnika Henke        $atts['height'] = (int) $height;
16482a2a2ba2SAnika Henke        if(!$atts['width']) $atts['width'] = 320;
16492a2a2ba2SAnika Henke        if(!$atts['height']) $atts['height'] = 240;
16502a2a2ba2SAnika Henke
1651410ee62aSAnika Henke        $posterUrl = '';
1652410ee62aSAnika Henke        $files = array();
1653410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1654410ee62aSAnika Henke
1655410ee62aSAnika Henke        if ($isExternal) {
1656410ee62aSAnika Henke            // take direct source for external files
1657702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1658410ee62aSAnika Henke            $files[$srcMime] = $src;
1659410ee62aSAnika Henke        } else {
16603d7a9e0aSAnika Henke            // prepare alternative formats
16613d7a9e0aSAnika Henke            $extensions   = array('webm', 'ogv', 'mp4');
1662410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
166359bc3b48SGerrit Uitslag            $poster       = media_alternativefiles($src, array('jpg', 'png'));
166499f943f6SAnika Henke            if(!empty($poster)) {
16652d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
166699f943f6SAnika Henke            }
1667410ee62aSAnika Henke        }
16682a2a2ba2SAnika Henke
1669f50634f0SAnika Henke        $out = '';
167079e53fe5SAnika Henke        // open video tag
1671f50634f0SAnika Henke        $out .= '<video '.buildAttributes($atts).' controls="controls"';
16723641199aSAnika Henke        if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1673f50634f0SAnika Henke        $out .= '>'.NL;
16743641199aSAnika Henke        $fallback = '';
167579e53fe5SAnika Henke
167679e53fe5SAnika Henke        // output source for each alternative video format
1677410ee62aSAnika Henke        foreach($files as $mime => $file) {
1678410ee62aSAnika Henke            if ($isExternal) {
1679410ee62aSAnika Henke                $url = $file;
1680410ee62aSAnika Henke                $linkType = 'externalmedia';
1681410ee62aSAnika Henke            } else {
16822d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1683410ee62aSAnika Henke                $linkType = 'internalmedia';
1684410ee62aSAnika Henke            }
168517954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
16863d7a9e0aSAnika Henke
1687f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
168879e53fe5SAnika Henke            // alternative content (just a link to the file)
1689410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
16903d7a9e0aSAnika Henke        }
16912a2a2ba2SAnika Henke
16922a2a2ba2SAnika Henke        // finish
16933641199aSAnika Henke        $out .= $fallback;
1694f50634f0SAnika Henke        $out .= '</video>'.NL;
1695f50634f0SAnika Henke        return $out;
16962a2a2ba2SAnika Henke    }
16972a2a2ba2SAnika Henke
1698b44a5dceSAnika Henke    /**
1699b44a5dceSAnika Henke     * Embed audio in HTML
1700b44a5dceSAnika Henke     *
1701b44a5dceSAnika Henke     * @author Anika Henke <anika@selfthinker.org>
1702b44a5dceSAnika Henke     *
1703b44a5dceSAnika Henke     * @param string $src       - ID of audio to embed
17046d4af72aSAnika Henke     * @param array  $atts      - additional attributes for the <audio> tag
1705f50634f0SAnika Henke     * @return string
1706b44a5dceSAnika Henke     */
170759bc3b48SGerrit Uitslag    function _audio($src, $atts = array()) {
1708702e97d3SAnika Henke        $files = array();
1709410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1710b44a5dceSAnika Henke
1711410ee62aSAnika Henke        if ($isExternal) {
1712410ee62aSAnika Henke            // take direct source for external files
1713702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1714410ee62aSAnika Henke            $files[$srcMime] = $src;
1715410ee62aSAnika Henke        } else {
1716b44a5dceSAnika Henke            // prepare alternative formats
1717b44a5dceSAnika Henke            $extensions   = array('ogg', 'mp3', 'wav');
1718410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
1719410ee62aSAnika Henke        }
1720b44a5dceSAnika Henke
1721f50634f0SAnika Henke        $out = '';
1722b44a5dceSAnika Henke        // open audio tag
1723f50634f0SAnika Henke        $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
17243641199aSAnika Henke        $fallback = '';
1725b44a5dceSAnika Henke
1726b44a5dceSAnika Henke        // output source for each alternative audio format
1727410ee62aSAnika Henke        foreach($files as $mime => $file) {
1728410ee62aSAnika Henke            if ($isExternal) {
1729410ee62aSAnika Henke                $url = $file;
1730410ee62aSAnika Henke                $linkType = 'externalmedia';
1731410ee62aSAnika Henke            } else {
17322d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1733410ee62aSAnika Henke                $linkType = 'internalmedia';
1734410ee62aSAnika Henke            }
173517954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
1736b44a5dceSAnika Henke
1737f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1738b44a5dceSAnika Henke            // alternative content (just a link to the file)
1739410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
1740b44a5dceSAnika Henke        }
1741b44a5dceSAnika Henke
1742b44a5dceSAnika Henke        // finish
17433641199aSAnika Henke        $out .= $fallback;
1744f50634f0SAnika Henke        $out .= '</audio>'.NL;
1745f50634f0SAnika Henke        return $out;
1746b44a5dceSAnika Henke    }
1747b44a5dceSAnika Henke
17483dd5c225SAndreas Gohr    #endregion
17490cecf9d5Sandi}
17500cecf9d5Sandi
1751e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1752