xref: /dokuwiki/inc/parser/xhtml.php (revision 702e97d357ff9f4820d505170007cad60723c25c)
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     *
6190df9a4dSAdrian Lang     * @param $type  string The section type identifier
6290df9a4dSAdrian Lang     * @param $title string The section title
6390df9a4dSAdrian Lang     * @param $start int    The byte position for the edit start
6490df9a4dSAdrian Lang     * @return string A marker class for the starting HTML element
6590df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
6690df9a4dSAdrian Lang     */
673f9e3215SAdrian Lang    public function startSectionEdit($start, $type, $title = null) {
68b04a190dSMichael Hamann        $this->sectionedits[] = array(++$this->lastsecid, $start, $type, $title);
69b04a190dSMichael Hamann        return 'sectionedit'.$this->lastsecid;
7090df9a4dSAdrian Lang    }
7190df9a4dSAdrian Lang
7290df9a4dSAdrian Lang    /**
7390df9a4dSAdrian Lang     * Finish an edit section range
7490df9a4dSAdrian Lang     *
75d9e36cbeSAdrian Lang     * @param $end     int The byte position for the edit end; null for the rest of
76c404cb3bSMatt Perry     *                 the page
7790df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
7890df9a4dSAdrian Lang     */
793f9e3215SAdrian Lang    public function finishSectionEdit($end = null) {
8090df9a4dSAdrian Lang        list($id, $start, $type, $title) = array_pop($this->sectionedits);
81d9e36cbeSAdrian Lang        if(!is_null($end) && $end <= $start) {
8200c13053SAdrian Lang            return;
8300c13053SAdrian Lang        }
8440868f2fSAdrian Lang        $this->doc .= "<!-- EDIT$id ".strtoupper($type).' ';
8540868f2fSAdrian Lang        if(!is_null($title)) {
8640868f2fSAdrian Lang            $this->doc .= '"'.str_replace('"', '', $title).'" ';
8740868f2fSAdrian Lang        }
88d9e36cbeSAdrian Lang        $this->doc .= "[$start-".(is_null($end) ? '' : $end).'] -->';
8990df9a4dSAdrian Lang    }
9090df9a4dSAdrian Lang
913dd5c225SAndreas Gohr    /**
923dd5c225SAndreas Gohr     * Returns the format produced by this renderer.
933dd5c225SAndreas Gohr     *
943dd5c225SAndreas Gohr     * @return string always 'xhtml'
953dd5c225SAndreas Gohr     */
965f70445dSAndreas Gohr    function getFormat() {
975f70445dSAndreas Gohr        return 'xhtml';
985f70445dSAndreas Gohr    }
995f70445dSAndreas Gohr
1003dd5c225SAndreas Gohr    /**
1013dd5c225SAndreas Gohr     * Initialize the document
1023dd5c225SAndreas Gohr     */
1030cecf9d5Sandi    function document_start() {
104c5a8fd96SAndreas Gohr        //reset some internals
105c5a8fd96SAndreas Gohr        $this->toc     = array();
106c5a8fd96SAndreas Gohr        $this->headers = array();
1070cecf9d5Sandi    }
1080cecf9d5Sandi
1093dd5c225SAndreas Gohr    /**
1103dd5c225SAndreas Gohr     * Finalize the document
1113dd5c225SAndreas Gohr     */
1120cecf9d5Sandi    function document_end() {
11390df9a4dSAdrian Lang        // Finish open section edits.
11490df9a4dSAdrian Lang        while(count($this->sectionedits) > 0) {
11590df9a4dSAdrian Lang            if($this->sectionedits[count($this->sectionedits) - 1][1] <= 1) {
11690df9a4dSAdrian Lang                // If there is only one section, do not write a section edit
11790df9a4dSAdrian Lang                // marker.
11890df9a4dSAdrian Lang                array_pop($this->sectionedits);
11990df9a4dSAdrian Lang            } else {
120d9e36cbeSAdrian Lang                $this->finishSectionEdit();
12190df9a4dSAdrian Lang            }
12290df9a4dSAdrian Lang        }
12390df9a4dSAdrian Lang
1240cecf9d5Sandi        if(count($this->footnotes) > 0) {
125a2d649c4Sandi            $this->doc .= '<div class="footnotes">'.DOKU_LF;
126d74aace9Schris
12716ec3e37SAndreas Gohr            foreach($this->footnotes as $id => $footnote) {
128d74aace9Schris                // check its not a placeholder that indicates actual footnote text is elsewhere
129d74aace9Schris                if(substr($footnote, 0, 5) != "@@FNT") {
130d74aace9Schris
131d74aace9Schris                    // open the footnote and set the anchor and backlink
132d74aace9Schris                    $this->doc .= '<div class="fn">';
13316cc7ed7SAnika Henke                    $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
13429bfcd16SAndreas Gohr                    $this->doc .= $id.')</a></sup> '.DOKU_LF;
135d74aace9Schris
136d74aace9Schris                    // get any other footnotes that use the same markup
137d74aace9Schris                    $alt = array_keys($this->footnotes, "@@FNT$id");
138d74aace9Schris
139d74aace9Schris                    if(count($alt)) {
140d74aace9Schris                        foreach($alt as $ref) {
141d74aace9Schris                            // set anchor and backlink for the other footnotes
14216ec3e37SAndreas Gohr                            $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
14316ec3e37SAndreas Gohr                            $this->doc .= ($ref).')</a></sup> '.DOKU_LF;
144d74aace9Schris                        }
145d74aace9Schris                    }
146d74aace9Schris
147d74aace9Schris                    // add footnote markup and close this footnote
148a2d649c4Sandi                    $this->doc .= $footnote;
149d74aace9Schris                    $this->doc .= '</div>'.DOKU_LF;
150d74aace9Schris                }
1510cecf9d5Sandi            }
152a2d649c4Sandi            $this->doc .= '</div>'.DOKU_LF;
1530cecf9d5Sandi        }
154c5a8fd96SAndreas Gohr
155b8595a66SAndreas Gohr        // Prepare the TOC
156851f2e89SAnika Henke        global $conf;
157851f2e89SAnika Henke        if($this->info['toc'] && is_array($this->toc) && $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']) {
158b8595a66SAndreas Gohr            global $TOC;
159b8595a66SAndreas Gohr            $TOC = $this->toc;
1600cecf9d5Sandi        }
1613e55d035SAndreas Gohr
1623e55d035SAndreas Gohr        // make sure there are no empty paragraphs
16327918226Schris        $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
164e41c4da9SAndreas Gohr    }
1650cecf9d5Sandi
1663dd5c225SAndreas Gohr    /**
1673dd5c225SAndreas Gohr     * Add an item to the TOC
1683dd5c225SAndreas Gohr     *
1693dd5c225SAndreas Gohr     * @param string $id       the hash link
1703dd5c225SAndreas Gohr     * @param string $text     the text to display
1713dd5c225SAndreas Gohr     * @param int    $level    the nesting level
1723dd5c225SAndreas Gohr     */
173e7856beaSchris    function toc_additem($id, $text, $level) {
174af587fa8Sandi        global $conf;
175af587fa8Sandi
176c5a8fd96SAndreas Gohr        //handle TOC
177c5a8fd96SAndreas Gohr        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
1787d91652aSAndreas Gohr            $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
179c5a8fd96SAndreas Gohr        }
180e7856beaSchris    }
181e7856beaSchris
1823dd5c225SAndreas Gohr    /**
1833dd5c225SAndreas Gohr     * Render a heading
1843dd5c225SAndreas Gohr     *
1853dd5c225SAndreas Gohr     * @param string $text  the text to display
1863dd5c225SAndreas Gohr     * @param int    $level header level
1873dd5c225SAndreas Gohr     * @param int    $pos   byte position in the original source
1883dd5c225SAndreas Gohr     */
189e7856beaSchris    function header($text, $level, $pos) {
19090df9a4dSAdrian Lang        global $conf;
19190df9a4dSAdrian Lang
192bdd8111bSAndreas Gohr        if(!$text) return; //skip empty headlines
193e7856beaSchris
194e7856beaSchris        $hid = $this->_headerToLink($text, true);
195e7856beaSchris
196e7856beaSchris        //only add items within configured levels
197e7856beaSchris        $this->toc_additem($hid, $text, $level);
198c5a8fd96SAndreas Gohr
19991459163SAnika Henke        // adjust $node to reflect hierarchy of levels
20091459163SAnika Henke        $this->node[$level - 1]++;
20191459163SAnika Henke        if($level < $this->lastlevel) {
20291459163SAnika Henke            for($i = 0; $i < $this->lastlevel - $level; $i++) {
20391459163SAnika Henke                $this->node[$this->lastlevel - $i - 1] = 0;
20491459163SAnika Henke            }
20591459163SAnika Henke        }
20691459163SAnika Henke        $this->lastlevel = $level;
20791459163SAnika Henke
20890df9a4dSAdrian Lang        if($level <= $conf['maxseclevel'] &&
20990df9a4dSAdrian Lang            count($this->sectionedits) > 0 &&
2103dd5c225SAndreas Gohr            $this->sectionedits[count($this->sectionedits) - 1][2] === 'section'
2113dd5c225SAndreas Gohr        ) {
2126c1f778cSAdrian Lang            $this->finishSectionEdit($pos - 1);
21390df9a4dSAdrian Lang        }
21490df9a4dSAdrian Lang
215c5a8fd96SAndreas Gohr        // write the header
21690df9a4dSAdrian Lang        $this->doc .= DOKU_LF.'<h'.$level;
21790df9a4dSAdrian Lang        if($level <= $conf['maxseclevel']) {
21890df9a4dSAdrian Lang            $this->doc .= ' class="'.$this->startSectionEdit($pos, 'section', $text).'"';
21990df9a4dSAdrian Lang        }
22016cc7ed7SAnika Henke        $this->doc .= ' id="'.$hid.'">';
221a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
22216cc7ed7SAnika Henke        $this->doc .= "</h$level>".DOKU_LF;
2230cecf9d5Sandi    }
2240cecf9d5Sandi
2253dd5c225SAndreas Gohr    /**
2263dd5c225SAndreas Gohr     * Open a new section
2273dd5c225SAndreas Gohr     *
2283dd5c225SAndreas Gohr     * @param int $level section level (as determined by the previous header)
2293dd5c225SAndreas Gohr     */
2300cecf9d5Sandi    function section_open($level) {
2319864e7b1SAdrian Lang        $this->doc .= '<div class="level'.$level.'">'.DOKU_LF;
2320cecf9d5Sandi    }
2330cecf9d5Sandi
2343dd5c225SAndreas Gohr    /**
2353dd5c225SAndreas Gohr     * Close the current section
2363dd5c225SAndreas Gohr     */
2370cecf9d5Sandi    function section_close() {
238a2d649c4Sandi        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
2390cecf9d5Sandi    }
2400cecf9d5Sandi
2413dd5c225SAndreas Gohr    /**
2423dd5c225SAndreas Gohr     * Render plain text data
2433dd5c225SAndreas Gohr     *
2443dd5c225SAndreas Gohr     * @param $text
2453dd5c225SAndreas Gohr     */
2460cecf9d5Sandi    function cdata($text) {
247a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
2480cecf9d5Sandi    }
2490cecf9d5Sandi
2503dd5c225SAndreas Gohr    /**
2513dd5c225SAndreas Gohr     * Open a paragraph
2523dd5c225SAndreas Gohr     */
2530cecf9d5Sandi    function p_open() {
25459869a4bSAnika Henke        $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
2550cecf9d5Sandi    }
2560cecf9d5Sandi
2573dd5c225SAndreas Gohr    /**
2583dd5c225SAndreas Gohr     * Close a paragraph
2593dd5c225SAndreas Gohr     */
2600cecf9d5Sandi    function p_close() {
26159869a4bSAnika Henke        $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
2620cecf9d5Sandi    }
2630cecf9d5Sandi
2643dd5c225SAndreas Gohr    /**
2653dd5c225SAndreas Gohr     * Create a line break
2663dd5c225SAndreas Gohr     */
2670cecf9d5Sandi    function linebreak() {
268a2d649c4Sandi        $this->doc .= '<br/>'.DOKU_LF;
2690cecf9d5Sandi    }
2700cecf9d5Sandi
2713dd5c225SAndreas Gohr    /**
2723dd5c225SAndreas Gohr     * Create a horizontal line
2733dd5c225SAndreas Gohr     */
2740cecf9d5Sandi    function hr() {
2754beabca9SAnika Henke        $this->doc .= '<hr />'.DOKU_LF;
2760cecf9d5Sandi    }
2770cecf9d5Sandi
2783dd5c225SAndreas Gohr    /**
2793dd5c225SAndreas Gohr     * Start strong (bold) formatting
2803dd5c225SAndreas Gohr     */
2810cecf9d5Sandi    function strong_open() {
282a2d649c4Sandi        $this->doc .= '<strong>';
2830cecf9d5Sandi    }
2840cecf9d5Sandi
2853dd5c225SAndreas Gohr    /**
2863dd5c225SAndreas Gohr     * Stop strong (bold) formatting
2873dd5c225SAndreas Gohr     */
2880cecf9d5Sandi    function strong_close() {
289a2d649c4Sandi        $this->doc .= '</strong>';
2900cecf9d5Sandi    }
2910cecf9d5Sandi
2923dd5c225SAndreas Gohr    /**
2933dd5c225SAndreas Gohr     * Start emphasis (italics) formatting
2943dd5c225SAndreas Gohr     */
2950cecf9d5Sandi    function emphasis_open() {
296a2d649c4Sandi        $this->doc .= '<em>';
2970cecf9d5Sandi    }
2980cecf9d5Sandi
2993dd5c225SAndreas Gohr    /**
3003dd5c225SAndreas Gohr     * Stop emphasis (italics) formatting
3013dd5c225SAndreas Gohr     */
3020cecf9d5Sandi    function emphasis_close() {
303a2d649c4Sandi        $this->doc .= '</em>';
3040cecf9d5Sandi    }
3050cecf9d5Sandi
3063dd5c225SAndreas Gohr    /**
3073dd5c225SAndreas Gohr     * Start underline formatting
3083dd5c225SAndreas Gohr     */
3090cecf9d5Sandi    function underline_open() {
31002e51121SAnika Henke        $this->doc .= '<em class="u">';
3110cecf9d5Sandi    }
3120cecf9d5Sandi
3133dd5c225SAndreas Gohr    /**
3143dd5c225SAndreas Gohr     * Stop underline formatting
3153dd5c225SAndreas Gohr     */
3160cecf9d5Sandi    function underline_close() {
31702e51121SAnika Henke        $this->doc .= '</em>';
3180cecf9d5Sandi    }
3190cecf9d5Sandi
3203dd5c225SAndreas Gohr    /**
3213dd5c225SAndreas Gohr     * Start monospace formatting
3223dd5c225SAndreas Gohr     */
3230cecf9d5Sandi    function monospace_open() {
324a2d649c4Sandi        $this->doc .= '<code>';
3250cecf9d5Sandi    }
3260cecf9d5Sandi
3273dd5c225SAndreas Gohr    /**
3283dd5c225SAndreas Gohr     * Stop monospace formatting
3293dd5c225SAndreas Gohr     */
3300cecf9d5Sandi    function monospace_close() {
331a2d649c4Sandi        $this->doc .= '</code>';
3320cecf9d5Sandi    }
3330cecf9d5Sandi
3343dd5c225SAndreas Gohr    /**
3353dd5c225SAndreas Gohr     * Start a subscript
3363dd5c225SAndreas Gohr     */
3370cecf9d5Sandi    function subscript_open() {
338a2d649c4Sandi        $this->doc .= '<sub>';
3390cecf9d5Sandi    }
3400cecf9d5Sandi
3413dd5c225SAndreas Gohr    /**
3423dd5c225SAndreas Gohr     * Stop a subscript
3433dd5c225SAndreas Gohr     */
3440cecf9d5Sandi    function subscript_close() {
345a2d649c4Sandi        $this->doc .= '</sub>';
3460cecf9d5Sandi    }
3470cecf9d5Sandi
3483dd5c225SAndreas Gohr    /**
3493dd5c225SAndreas Gohr     * Start a superscript
3503dd5c225SAndreas Gohr     */
3510cecf9d5Sandi    function superscript_open() {
352a2d649c4Sandi        $this->doc .= '<sup>';
3530cecf9d5Sandi    }
3540cecf9d5Sandi
3553dd5c225SAndreas Gohr    /**
3563dd5c225SAndreas Gohr     * Stop a superscript
3573dd5c225SAndreas Gohr     */
3580cecf9d5Sandi    function superscript_close() {
359a2d649c4Sandi        $this->doc .= '</sup>';
3600cecf9d5Sandi    }
3610cecf9d5Sandi
3623dd5c225SAndreas Gohr    /**
3633dd5c225SAndreas Gohr     * Start deleted (strike-through) formatting
3643dd5c225SAndreas Gohr     */
3650cecf9d5Sandi    function deleted_open() {
366a2d649c4Sandi        $this->doc .= '<del>';
3670cecf9d5Sandi    }
3680cecf9d5Sandi
3693dd5c225SAndreas Gohr    /**
3703dd5c225SAndreas Gohr     * Stop deleted (strike-through) formatting
3713dd5c225SAndreas Gohr     */
3720cecf9d5Sandi    function deleted_close() {
373a2d649c4Sandi        $this->doc .= '</del>';
3740cecf9d5Sandi    }
3750cecf9d5Sandi
3763fd0b676Sandi    /**
3773fd0b676Sandi     * Callback for footnote start syntax
3783fd0b676Sandi     *
3793fd0b676Sandi     * All following content will go to the footnote instead of
380d74aace9Schris     * the document. To achieve this the previous rendered content
3813fd0b676Sandi     * is moved to $store and $doc is cleared
3823fd0b676Sandi     *
3833fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
3843fd0b676Sandi     */
3850cecf9d5Sandi    function footnote_open() {
3867764a90aSandi
3877764a90aSandi        // move current content to store and record footnote
3887764a90aSandi        $this->store = $this->doc;
3897764a90aSandi        $this->doc   = '';
3900cecf9d5Sandi    }
3910cecf9d5Sandi
3923fd0b676Sandi    /**
3933fd0b676Sandi     * Callback for footnote end syntax
3943fd0b676Sandi     *
3953fd0b676Sandi     * All rendered content is moved to the $footnotes array and the old
3963fd0b676Sandi     * content is restored from $store again
3973fd0b676Sandi     *
3983fd0b676Sandi     * @author Andreas Gohr
3993fd0b676Sandi     */
4000cecf9d5Sandi    function footnote_close() {
40116ec3e37SAndreas Gohr        /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
40216ec3e37SAndreas Gohr        static $fnid = 0;
40316ec3e37SAndreas Gohr        // assign new footnote id (we start at 1)
40416ec3e37SAndreas Gohr        $fnid++;
4057764a90aSandi
406d74aace9Schris        // recover footnote into the stack and restore old content
407d74aace9Schris        $footnote    = $this->doc;
4087764a90aSandi        $this->doc   = $this->store;
4097764a90aSandi        $this->store = '';
410d74aace9Schris
411d74aace9Schris        // check to see if this footnote has been seen before
412d74aace9Schris        $i = array_search($footnote, $this->footnotes);
413d74aace9Schris
414d74aace9Schris        if($i === false) {
415d74aace9Schris            // its a new footnote, add it to the $footnotes array
41616ec3e37SAndreas Gohr            $this->footnotes[$fnid] = $footnote;
417d74aace9Schris        } else {
41816ec3e37SAndreas Gohr            // seen this one before, save a placeholder
41916ec3e37SAndreas Gohr            $this->footnotes[$fnid] = "@@FNT".($i);
420d74aace9Schris        }
421d74aace9Schris
4226b379cbfSAndreas Gohr        // output the footnote reference and link
42316ec3e37SAndreas Gohr        $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
4240cecf9d5Sandi    }
4250cecf9d5Sandi
4263dd5c225SAndreas Gohr    /**
4273dd5c225SAndreas Gohr     * Open an unordered list
4283dd5c225SAndreas Gohr     */
4290cecf9d5Sandi    function listu_open() {
430a2d649c4Sandi        $this->doc .= '<ul>'.DOKU_LF;
4310cecf9d5Sandi    }
4320cecf9d5Sandi
4333dd5c225SAndreas Gohr    /**
4343dd5c225SAndreas Gohr     * Close an unordered list
4353dd5c225SAndreas Gohr     */
4360cecf9d5Sandi    function listu_close() {
437a2d649c4Sandi        $this->doc .= '</ul>'.DOKU_LF;
4380cecf9d5Sandi    }
4390cecf9d5Sandi
4403dd5c225SAndreas Gohr    /**
4413dd5c225SAndreas Gohr     * Open an ordered list
4423dd5c225SAndreas Gohr     */
4430cecf9d5Sandi    function listo_open() {
444a2d649c4Sandi        $this->doc .= '<ol>'.DOKU_LF;
4450cecf9d5Sandi    }
4460cecf9d5Sandi
4473dd5c225SAndreas Gohr    /**
4483dd5c225SAndreas Gohr     * Close an ordered list
4493dd5c225SAndreas Gohr     */
4500cecf9d5Sandi    function listo_close() {
451a2d649c4Sandi        $this->doc .= '</ol>'.DOKU_LF;
4520cecf9d5Sandi    }
4530cecf9d5Sandi
4543dd5c225SAndreas Gohr    /**
4553dd5c225SAndreas Gohr     * Open a list item
4563dd5c225SAndreas Gohr     *
4573dd5c225SAndreas Gohr     * @param int $level the nesting level
4583dd5c225SAndreas Gohr     */
4590cecf9d5Sandi    function listitem_open($level) {
46059869a4bSAnika Henke        $this->doc .= '<li class="level'.$level.'">';
4610cecf9d5Sandi    }
4620cecf9d5Sandi
4633dd5c225SAndreas Gohr    /**
4643dd5c225SAndreas Gohr     * Close a list item
4653dd5c225SAndreas Gohr     */
4660cecf9d5Sandi    function listitem_close() {
467a2d649c4Sandi        $this->doc .= '</li>'.DOKU_LF;
4680cecf9d5Sandi    }
4690cecf9d5Sandi
4703dd5c225SAndreas Gohr    /**
4713dd5c225SAndreas Gohr     * Start the content of a list item
4723dd5c225SAndreas Gohr     */
4730cecf9d5Sandi    function listcontent_open() {
47490db23d7Schris        $this->doc .= '<div class="li">';
4750cecf9d5Sandi    }
4760cecf9d5Sandi
4773dd5c225SAndreas Gohr    /**
4783dd5c225SAndreas Gohr     * Stop the content of a list item
4793dd5c225SAndreas Gohr     */
4800cecf9d5Sandi    function listcontent_close() {
48159869a4bSAnika Henke        $this->doc .= '</div>'.DOKU_LF;
4820cecf9d5Sandi    }
4830cecf9d5Sandi
4843dd5c225SAndreas Gohr    /**
4853dd5c225SAndreas Gohr     * Output unformatted $text
4863dd5c225SAndreas Gohr     *
4873dd5c225SAndreas Gohr     * Defaults to $this->cdata()
4883dd5c225SAndreas Gohr     *
4893dd5c225SAndreas Gohr     * @param string $text
4903dd5c225SAndreas Gohr     */
4910cecf9d5Sandi    function unformatted($text) {
492a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
4930cecf9d5Sandi    }
4940cecf9d5Sandi
4950cecf9d5Sandi    /**
4963fd0b676Sandi     * Execute PHP code if allowed
4973fd0b676Sandi     *
498d9764001SMichael Hamann     * @param  string $text      PHP code that is either executed or printed
4995d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['phpok'] is okff
5005d568b99SChris Smith     *
5013fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5020cecf9d5Sandi     */
5035d568b99SChris Smith    function php($text, $wrapper = 'code') {
50435a56260SChris Smith        global $conf;
50535a56260SChris Smith
506d86d5af0SChris Smith        if($conf['phpok']) {
507bad0b545Sandi            ob_start();
5084de671bcSandi            eval($text);
5093fd0b676Sandi            $this->doc .= ob_get_contents();
510bad0b545Sandi            ob_end_clean();
511d86d5af0SChris Smith        } else {
5125d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'php', $wrapper);
513d86d5af0SChris Smith        }
5140cecf9d5Sandi    }
5150cecf9d5Sandi
5163dd5c225SAndreas Gohr    /**
5173dd5c225SAndreas Gohr     * Output block level PHP code
5183dd5c225SAndreas Gohr     *
5193dd5c225SAndreas Gohr     * If $conf['phpok'] is true this should evaluate the given code and append the result
5203dd5c225SAndreas Gohr     * to $doc
5213dd5c225SAndreas Gohr     *
5223dd5c225SAndreas Gohr     * @param string $text The PHP code
5233dd5c225SAndreas Gohr     */
52407f89c3cSAnika Henke    function phpblock($text) {
5255d568b99SChris Smith        $this->php($text, 'pre');
52607f89c3cSAnika Henke    }
52707f89c3cSAnika Henke
5280cecf9d5Sandi    /**
5293fd0b676Sandi     * Insert HTML if allowed
5303fd0b676Sandi     *
531d9764001SMichael Hamann     * @param  string $text      html text
5325d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['htmlok'] is okff
5335d568b99SChris Smith     *
5343fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5350cecf9d5Sandi     */
5365d568b99SChris Smith    function html($text, $wrapper = 'code') {
53735a56260SChris Smith        global $conf;
53835a56260SChris Smith
539d86d5af0SChris Smith        if($conf['htmlok']) {
540a2d649c4Sandi            $this->doc .= $text;
541d86d5af0SChris Smith        } else {
5425d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'html4strict', $wrapper);
543d86d5af0SChris Smith        }
5444de671bcSandi    }
5450cecf9d5Sandi
5463dd5c225SAndreas Gohr    /**
5473dd5c225SAndreas Gohr     * Output raw block-level HTML
5483dd5c225SAndreas Gohr     *
5493dd5c225SAndreas Gohr     * If $conf['htmlok'] is true this should add the code as is to $doc
5503dd5c225SAndreas Gohr     *
5513dd5c225SAndreas Gohr     * @param string $text The HTML
5523dd5c225SAndreas Gohr     */
55307f89c3cSAnika Henke    function htmlblock($text) {
5545d568b99SChris Smith        $this->html($text, 'pre');
55507f89c3cSAnika Henke    }
55607f89c3cSAnika Henke
5573dd5c225SAndreas Gohr    /**
5583dd5c225SAndreas Gohr     * Start a block quote
5593dd5c225SAndreas Gohr     */
5600cecf9d5Sandi    function quote_open() {
56196331712SAnika Henke        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
5620cecf9d5Sandi    }
5630cecf9d5Sandi
5643dd5c225SAndreas Gohr    /**
5653dd5c225SAndreas Gohr     * Stop a block quote
5663dd5c225SAndreas Gohr     */
5670cecf9d5Sandi    function quote_close() {
56896331712SAnika Henke        $this->doc .= '</div></blockquote>'.DOKU_LF;
5690cecf9d5Sandi    }
5700cecf9d5Sandi
5713dd5c225SAndreas Gohr    /**
5723dd5c225SAndreas Gohr     * Output preformatted text
5733dd5c225SAndreas Gohr     *
5743dd5c225SAndreas Gohr     * @param string $text
5753dd5c225SAndreas Gohr     */
5763d491f75SAndreas Gohr    function preformatted($text) {
577c9250713SAnika Henke        $this->doc .= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF;
5783d491f75SAndreas Gohr    }
5793d491f75SAndreas Gohr
5803dd5c225SAndreas Gohr    /**
5813dd5c225SAndreas Gohr     * Display text as file content, optionally syntax highlighted
5823dd5c225SAndreas Gohr     *
5833dd5c225SAndreas Gohr     * @param string $text     text to show
5843dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5853dd5c225SAndreas Gohr     * @param string $filename file path label
5863dd5c225SAndreas Gohr     */
5873d491f75SAndreas Gohr    function file($text, $language = null, $filename = null) {
5883d491f75SAndreas Gohr        $this->_highlight('file', $text, $language, $filename);
5893d491f75SAndreas Gohr    }
5903d491f75SAndreas Gohr
5913dd5c225SAndreas Gohr    /**
5923dd5c225SAndreas Gohr     * Display text as code content, optionally syntax highlighted
5933dd5c225SAndreas Gohr     *
5943dd5c225SAndreas Gohr     * @param string $text     text to show
5953dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5963dd5c225SAndreas Gohr     * @param string $filename file path label
5973dd5c225SAndreas Gohr     */
5983d491f75SAndreas Gohr    function code($text, $language = null, $filename = null) {
5993d491f75SAndreas Gohr        $this->_highlight('code', $text, $language, $filename);
6003d491f75SAndreas Gohr    }
6013d491f75SAndreas Gohr
6020cecf9d5Sandi    /**
6033d491f75SAndreas Gohr     * Use GeSHi to highlight language syntax in code and file blocks
6043fd0b676Sandi     *
6053fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
6063dd5c225SAndreas Gohr     * @param string $type     code|file
6073dd5c225SAndreas Gohr     * @param string $text     text to show
6083dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6093dd5c225SAndreas Gohr     * @param string $filename file path label
6100cecf9d5Sandi     */
6113d491f75SAndreas Gohr    function _highlight($type, $text, $language = null, $filename = null) {
6123d491f75SAndreas Gohr        global $ID;
6133d491f75SAndreas Gohr        global $lang;
6143d491f75SAndreas Gohr
6153d491f75SAndreas Gohr        if($filename) {
616190c56e8SAndreas Gohr            // add icon
61727bf7924STom N Harris            list($ext) = mimetype($filename, false);
618190c56e8SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
619190c56e8SAndreas Gohr            $class = 'mediafile mf_'.$class;
620190c56e8SAndreas Gohr
6213d491f75SAndreas Gohr            $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
622190c56e8SAndreas Gohr            $this->doc .= '<dt><a href="'.exportlink($ID, 'code', array('codeblock' => $this->_codeblock)).'" title="'.$lang['download'].'" class="'.$class.'">';
6233d491f75SAndreas Gohr            $this->doc .= hsc($filename);
6243d491f75SAndreas Gohr            $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
6253d491f75SAndreas Gohr        }
6260cecf9d5Sandi
627d43aac1cSGina Haeussge        if($text{0} == "\n") {
628d43aac1cSGina Haeussge            $text = substr($text, 1);
629d43aac1cSGina Haeussge        }
630d43aac1cSGina Haeussge        if(substr($text, -1) == "\n") {
631d43aac1cSGina Haeussge            $text = substr($text, 0, -1);
632d43aac1cSGina Haeussge        }
633d43aac1cSGina Haeussge
6340cecf9d5Sandi        if(is_null($language)) {
6353d491f75SAndreas Gohr            $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
6360cecf9d5Sandi        } else {
6373d491f75SAndreas Gohr            $class = 'code'; //we always need the code class to make the syntax highlighting apply
6383d491f75SAndreas Gohr            if($type != 'code') $class .= ' '.$type;
6393d491f75SAndreas Gohr
6403d491f75SAndreas Gohr            $this->doc .= "<pre class=\"$class $language\">".p_xhtml_cached_geshi($text, $language, '').'</pre>'.DOKU_LF;
6410cecf9d5Sandi        }
6423d491f75SAndreas Gohr
6433d491f75SAndreas Gohr        if($filename) {
6443d491f75SAndreas Gohr            $this->doc .= '</dd></dl>'.DOKU_LF;
6453d491f75SAndreas Gohr        }
6463d491f75SAndreas Gohr
6473d491f75SAndreas Gohr        $this->_codeblock++;
6480cecf9d5Sandi    }
6490cecf9d5Sandi
6503dd5c225SAndreas Gohr    /**
6513dd5c225SAndreas Gohr     * Format an acronym
6523dd5c225SAndreas Gohr     *
6533dd5c225SAndreas Gohr     * Uses $this->acronyms
6543dd5c225SAndreas Gohr     *
6553dd5c225SAndreas Gohr     * @param string $acronym
6563dd5c225SAndreas Gohr     */
6570cecf9d5Sandi    function acronym($acronym) {
6580cecf9d5Sandi
6590cecf9d5Sandi        if(array_key_exists($acronym, $this->acronyms)) {
6600cecf9d5Sandi
661433bef32Sandi            $title = $this->_xmlEntities($this->acronyms[$acronym]);
6620cecf9d5Sandi
663940db3a3SAnika Henke            $this->doc .= '<abbr title="'.$title
664940db3a3SAnika Henke                .'">'.$this->_xmlEntities($acronym).'</abbr>';
6650cecf9d5Sandi
6660cecf9d5Sandi        } else {
667a2d649c4Sandi            $this->doc .= $this->_xmlEntities($acronym);
6680cecf9d5Sandi        }
6690cecf9d5Sandi    }
6700cecf9d5Sandi
6713dd5c225SAndreas Gohr    /**
6723dd5c225SAndreas Gohr     * Format a smiley
6733dd5c225SAndreas Gohr     *
6743dd5c225SAndreas Gohr     * Uses $this->smiley
6753dd5c225SAndreas Gohr     *
6763dd5c225SAndreas Gohr     * @param string $smiley
6773dd5c225SAndreas Gohr     */
6780cecf9d5Sandi    function smiley($smiley) {
6790cecf9d5Sandi        if(array_key_exists($smiley, $this->smileys)) {
680f62ea8a1Sandi            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
6818e38227fSAnika Henke                '" class="icon" alt="'.
682433bef32Sandi                $this->_xmlEntities($smiley).'" />';
6830cecf9d5Sandi        } else {
684a2d649c4Sandi            $this->doc .= $this->_xmlEntities($smiley);
6850cecf9d5Sandi        }
6860cecf9d5Sandi    }
6870cecf9d5Sandi
6883dd5c225SAndreas Gohr    /**
6893dd5c225SAndreas Gohr     * Format an entity
6903dd5c225SAndreas Gohr     *
6913dd5c225SAndreas Gohr     * Entities are basically small text replacements
6923dd5c225SAndreas Gohr     *
6933dd5c225SAndreas Gohr     * Uses $this->entities
6943dd5c225SAndreas Gohr     *
6953dd5c225SAndreas Gohr     * @param string $entity
6964de671bcSandi     */
6970cecf9d5Sandi    function entity($entity) {
6980cecf9d5Sandi        if(array_key_exists($entity, $this->entities)) {
699a2d649c4Sandi            $this->doc .= $this->entities[$entity];
7000cecf9d5Sandi        } else {
701a2d649c4Sandi            $this->doc .= $this->_xmlEntities($entity);
7020cecf9d5Sandi        }
7030cecf9d5Sandi    }
7040cecf9d5Sandi
7053dd5c225SAndreas Gohr    /**
7063dd5c225SAndreas Gohr     * Typographically format a multiply sign
7073dd5c225SAndreas Gohr     *
7083dd5c225SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
7093dd5c225SAndreas Gohr     *
7103dd5c225SAndreas Gohr     * @param string|int $x first value
7113dd5c225SAndreas Gohr     * @param string|int $y second value
7123dd5c225SAndreas Gohr     */
7130cecf9d5Sandi    function multiplyentity($x, $y) {
714a2d649c4Sandi        $this->doc .= "$x&times;$y";
7150cecf9d5Sandi    }
7160cecf9d5Sandi
7173dd5c225SAndreas Gohr    /**
7183dd5c225SAndreas Gohr     * Render an opening single quote char (language specific)
7193dd5c225SAndreas Gohr     */
7200cecf9d5Sandi    function singlequoteopening() {
72171b40da2SAnika Henke        global $lang;
72271b40da2SAnika Henke        $this->doc .= $lang['singlequoteopening'];
7230cecf9d5Sandi    }
7240cecf9d5Sandi
7253dd5c225SAndreas Gohr    /**
7263dd5c225SAndreas Gohr     * Render a closing single quote char (language specific)
7273dd5c225SAndreas Gohr     */
7280cecf9d5Sandi    function singlequoteclosing() {
72971b40da2SAnika Henke        global $lang;
73071b40da2SAnika Henke        $this->doc .= $lang['singlequoteclosing'];
7310cecf9d5Sandi    }
7320cecf9d5Sandi
7333dd5c225SAndreas Gohr    /**
7343dd5c225SAndreas Gohr     * Render an apostrophe char (language specific)
7353dd5c225SAndreas Gohr     */
73657d757d1SAndreas Gohr    function apostrophe() {
73757d757d1SAndreas Gohr        global $lang;
738a8bd192aSAndreas Gohr        $this->doc .= $lang['apostrophe'];
73957d757d1SAndreas Gohr    }
74057d757d1SAndreas Gohr
7413dd5c225SAndreas Gohr    /**
7423dd5c225SAndreas Gohr     * Render an opening double quote char (language specific)
7433dd5c225SAndreas Gohr     */
7440cecf9d5Sandi    function doublequoteopening() {
74571b40da2SAnika Henke        global $lang;
74671b40da2SAnika Henke        $this->doc .= $lang['doublequoteopening'];
7470cecf9d5Sandi    }
7480cecf9d5Sandi
7493dd5c225SAndreas Gohr    /**
7503dd5c225SAndreas Gohr     * Render an closinging double quote char (language specific)
7513dd5c225SAndreas Gohr     */
7520cecf9d5Sandi    function doublequoteclosing() {
75371b40da2SAnika Henke        global $lang;
75471b40da2SAnika Henke        $this->doc .= $lang['doublequoteclosing'];
7550cecf9d5Sandi    }
7560cecf9d5Sandi
7570cecf9d5Sandi    /**
7583dd5c225SAndreas Gohr     * Render a CamelCase link
7593dd5c225SAndreas Gohr     *
7603dd5c225SAndreas Gohr     * @param string $link The link name
7613dd5c225SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
7620cecf9d5Sandi     */
7630cecf9d5Sandi    function camelcaselink($link) {
76411d0aa47Sandi        $this->internallink($link, $link);
7650cecf9d5Sandi    }
7660cecf9d5Sandi
7673dd5c225SAndreas Gohr    /**
7683dd5c225SAndreas Gohr     * Render a page local link
7693dd5c225SAndreas Gohr     *
7703dd5c225SAndreas Gohr     * @param string $hash hash link identifier
7713dd5c225SAndreas Gohr     * @param string $name name for the link
7723dd5c225SAndreas Gohr     */
7730ea51e63SMatt Perry    function locallink($hash, $name = null) {
7740b7c14c2Sandi        global $ID;
7750b7c14c2Sandi        $name  = $this->_getLinkTitle($name, $hash, $isImage);
7760b7c14c2Sandi        $hash  = $this->_headerToLink($hash);
777e260f93bSAnika Henke        $title = $ID.' ↵';
7780b7c14c2Sandi        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
7790b7c14c2Sandi        $this->doc .= $name;
7800b7c14c2Sandi        $this->doc .= '</a>';
7810b7c14c2Sandi    }
7820b7c14c2Sandi
783cffcc403Sandi    /**
7843fd0b676Sandi     * Render an internal Wiki Link
7853fd0b676Sandi     *
786fe9ec250SChris Smith     * $search,$returnonly & $linktype are not for the renderer but are used
787cffcc403Sandi     * elsewhere - no need to implement them in other renderers
7883fd0b676Sandi     *
7893dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
790f23eef27SGerrit Uitslag     * @param string      $id         pageid
791f23eef27SGerrit Uitslag     * @param string|null $name       link name
792f23eef27SGerrit Uitslag     * @param string|null $search     adds search url param
793f23eef27SGerrit Uitslag     * @param bool        $returnonly whether to return html or write to doc attribute
794f23eef27SGerrit Uitslag     * @param string      $linktype   type to set use of headings
795f23eef27SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
796cffcc403Sandi     */
7970ea51e63SMatt Perry    function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
798ba11bd29Sandi        global $conf;
79937e34a5eSandi        global $ID;
800c4dda6afSAnika Henke        global $INFO;
80144653a53SAdrian Lang
8023d5e07d9SAdrian Lang        $params = '';
8033d5e07d9SAdrian Lang        $parts  = explode('?', $id, 2);
8043d5e07d9SAdrian Lang        if(count($parts) === 2) {
8053d5e07d9SAdrian Lang            $id     = $parts[0];
8063d5e07d9SAdrian Lang            $params = $parts[1];
80744653a53SAdrian Lang        }
80844653a53SAdrian Lang
809fda14ffcSIzidor Matušov        // For empty $id we need to know the current $ID
810fda14ffcSIzidor Matušov        // We need this check because _simpleTitle needs
811fda14ffcSIzidor Matušov        // correct $id and resolve_pageid() use cleanID($id)
812fda14ffcSIzidor Matušov        // (some things could be lost)
813fda14ffcSIzidor Matušov        if($id === '') {
814fda14ffcSIzidor Matušov            $id = $ID;
815fda14ffcSIzidor Matušov        }
816fda14ffcSIzidor Matušov
8170339c872Sjan        // default name is based on $id as given
8180339c872Sjan        $default = $this->_simpleTitle($id);
819ad32e47eSAndreas Gohr
8200339c872Sjan        // now first resolve and clean up the $id
82137e34a5eSandi        resolve_pageid(getNS($ID), $id, $exists);
822fda14ffcSIzidor Matušov
823fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
8240e1c636eSandi        if(!$isImage) {
8250e1c636eSandi            if($exists) {
826ba11bd29Sandi                $class = 'wikilink1';
8270cecf9d5Sandi            } else {
828ba11bd29Sandi                $class       = 'wikilink2';
82944a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
8300cecf9d5Sandi            }
8310cecf9d5Sandi        } else {
832ba11bd29Sandi            $class = 'media';
8330cecf9d5Sandi        }
8340cecf9d5Sandi
835a1685bedSandi        //keep hash anchor
8366d2af55dSChristopher Smith        @list($id, $hash) = explode('#', $id, 2);
837943dedc6SAndreas Gohr        if(!empty($hash)) $hash = $this->_headerToLink($hash);
838a1685bedSandi
839ba11bd29Sandi        //prepare for formating
840ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
841ba11bd29Sandi        $link['style']  = '';
842ba11bd29Sandi        $link['pre']    = '';
843ba11bd29Sandi        $link['suf']    = '';
84440eb54bbSjan        // highlight link to current page
845c4dda6afSAnika Henke        if($id == $INFO['id']) {
84692795d04Sandi            $link['pre'] = '<span class="curid">';
84792795d04Sandi            $link['suf'] = '</span>';
84840eb54bbSjan        }
8495e163278SAndreas Gohr        $link['more']  = '';
850ba11bd29Sandi        $link['class'] = $class;
85144653a53SAdrian Lang        $link['url']   = wl($id, $params);
852ba11bd29Sandi        $link['name']  = $name;
853ba11bd29Sandi        $link['title'] = $id;
854723d78dbSandi        //add search string
855723d78dbSandi        if($search) {
856546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
857546d3a99SAndreas Gohr            if(is_array($search)) {
858546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
859546d3a99SAndreas Gohr                $link['url'] .= 's[]='.join('&amp;s[]=', $search);
860546d3a99SAndreas Gohr            } else {
861546d3a99SAndreas Gohr                $link['url'] .= 's='.rawurlencode($search);
862546d3a99SAndreas Gohr            }
863723d78dbSandi        }
864723d78dbSandi
865a1685bedSandi        //keep hash
866a1685bedSandi        if($hash) $link['url'] .= '#'.$hash;
867a1685bedSandi
868ba11bd29Sandi        //output formatted
869cffcc403Sandi        if($returnonly) {
870cffcc403Sandi            return $this->_formatLink($link);
871cffcc403Sandi        } else {
872a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
8730cecf9d5Sandi        }
874cffcc403Sandi    }
8750cecf9d5Sandi
8763dd5c225SAndreas Gohr    /**
8773dd5c225SAndreas Gohr     * Render an external link
8783dd5c225SAndreas Gohr     *
8793dd5c225SAndreas Gohr     * @param string       $url  full URL with scheme
8803dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
8813dd5c225SAndreas Gohr     */
8820ea51e63SMatt Perry    function externallink($url, $name = null) {
883b625487dSandi        global $conf;
8840cecf9d5Sandi
885433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
8866f0c5dbfSandi
887b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
888b52b1596SAndreas Gohr        if(is_null($this->schemes)) $this->schemes = getSchemes();
889b52b1596SAndreas Gohr        list($scheme) = explode('://', $url);
890b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
891b52b1596SAndreas Gohr        if(!in_array($scheme, $this->schemes)) $url = '';
892b52b1596SAndreas Gohr
893b52b1596SAndreas Gohr        // is there still an URL?
894b52b1596SAndreas Gohr        if(!$url) {
895b52b1596SAndreas Gohr            $this->doc .= $name;
896b52b1596SAndreas Gohr            return;
897b52b1596SAndreas Gohr        }
898b52b1596SAndreas Gohr
899b52b1596SAndreas Gohr        // set class
9000cecf9d5Sandi        if(!$isImage) {
901b625487dSandi            $class = 'urlextern';
9020cecf9d5Sandi        } else {
903b625487dSandi            $class = 'media';
9040cecf9d5Sandi        }
9050cecf9d5Sandi
906b625487dSandi        //prepare for formating
907b625487dSandi        $link['target'] = $conf['target']['extern'];
908b625487dSandi        $link['style']  = '';
909b625487dSandi        $link['pre']    = '';
910b625487dSandi        $link['suf']    = '';
9115e163278SAndreas Gohr        $link['more']   = '';
912b625487dSandi        $link['class']  = $class;
913b625487dSandi        $link['url']    = $url;
914e1c10e4dSchris
915b625487dSandi        $link['name']  = $name;
916433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
917b625487dSandi        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
9180cecf9d5Sandi
919b625487dSandi        //output formatted
920a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9210cecf9d5Sandi    }
9220cecf9d5Sandi
9230cecf9d5Sandi    /**
9243dd5c225SAndreas Gohr     * Render an interwiki link
9253dd5c225SAndreas Gohr     *
9263dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
9273dd5c225SAndreas Gohr     *
9283dd5c225SAndreas Gohr     * @param string       $match     original link - probably not much use
9293dd5c225SAndreas Gohr     * @param string|array $name      name for the link, array for media file
9303dd5c225SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
9313dd5c225SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
9320cecf9d5Sandi     */
9330ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
934b625487dSandi        global $conf;
9350cecf9d5Sandi
93697a3e4e3Sandi        $link           = array();
93797a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
93897a3e4e3Sandi        $link['pre']    = '';
93997a3e4e3Sandi        $link['suf']    = '';
9405e163278SAndreas Gohr        $link['more']   = '';
941433bef32Sandi        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
9420cecf9d5Sandi
94397a3e4e3Sandi        //get interwiki URL
9446496c33fSGerrit Uitslag        $exists = null;
9456496c33fSGerrit Uitslag        $url    = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
9460cecf9d5Sandi
94797a3e4e3Sandi        if(!$isImage) {
9489d2ddea4SAndreas Gohr            $class         = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
9499d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
9501c2d1019SAndreas Gohr        } else {
9511c2d1019SAndreas Gohr            $link['class'] = 'media';
95297a3e4e3Sandi        }
9530cecf9d5Sandi
95497a3e4e3Sandi        //do we stay at the same server? Use local target
9552345e871SGerrit Uitslag        if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
95697a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
95797a3e4e3Sandi        }
9586496c33fSGerrit Uitslag        if($exists !== null && !$isImage) {
9596496c33fSGerrit Uitslag            if($exists) {
9606496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
9616496c33fSGerrit Uitslag            } else {
9626496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
9636496c33fSGerrit Uitslag                $link['rel'] = 'nofollow';
9646496c33fSGerrit Uitslag            }
9656496c33fSGerrit Uitslag        }
9660cecf9d5Sandi
96797a3e4e3Sandi        $link['url']   = $url;
96897a3e4e3Sandi        $link['title'] = htmlspecialchars($link['url']);
96997a3e4e3Sandi
97097a3e4e3Sandi        //output formatted
971a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9720cecf9d5Sandi    }
9730cecf9d5Sandi
9740cecf9d5Sandi    /**
9753dd5c225SAndreas Gohr     * Link to windows share
9763dd5c225SAndreas Gohr     *
9773dd5c225SAndreas Gohr     * @param string       $url  the link
9783dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
9790cecf9d5Sandi     */
9800ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
9811d47afe1Sandi        global $conf;
9823dd5c225SAndreas Gohr
9831d47afe1Sandi        //simple setup
9841d47afe1Sandi        $link['target'] = $conf['target']['windows'];
9851d47afe1Sandi        $link['pre']    = '';
9861d47afe1Sandi        $link['suf']    = '';
9871d47afe1Sandi        $link['style']  = '';
9880cecf9d5Sandi
989433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
9900cecf9d5Sandi        if(!$isImage) {
9911d47afe1Sandi            $link['class'] = 'windows';
9920cecf9d5Sandi        } else {
9931d47afe1Sandi            $link['class'] = 'media';
9940cecf9d5Sandi        }
9950cecf9d5Sandi
996433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
9971d47afe1Sandi        $url           = str_replace('\\', '/', $url);
9981d47afe1Sandi        $url           = 'file:///'.$url;
9991d47afe1Sandi        $link['url']   = $url;
10000cecf9d5Sandi
10011d47afe1Sandi        //output formatted
1002a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10030cecf9d5Sandi    }
10040cecf9d5Sandi
10053dd5c225SAndreas Gohr    /**
10063dd5c225SAndreas Gohr     * Render a linked E-Mail Address
10073dd5c225SAndreas Gohr     *
10083dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
10093dd5c225SAndreas Gohr     *
10103dd5c225SAndreas Gohr     * @param string       $address Email-Address
10113dd5c225SAndreas Gohr     * @param string|array $name    name for the link, array for media file
10123dd5c225SAndreas Gohr     */
10130ea51e63SMatt Perry    function emaillink($address, $name = null) {
101471352defSandi        global $conf;
101571352defSandi        //simple setup
101671352defSandi        $link           = array();
101771352defSandi        $link['target'] = '';
101871352defSandi        $link['pre']    = '';
101971352defSandi        $link['suf']    = '';
102071352defSandi        $link['style']  = '';
102171352defSandi        $link['more']   = '';
10220cecf9d5Sandi
1023c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
10240cecf9d5Sandi        if(!$isImage) {
1025be96545cSAnika Henke            $link['class'] = 'mail';
10260cecf9d5Sandi        } else {
1027be96545cSAnika Henke            $link['class'] = 'media';
10280cecf9d5Sandi        }
10290cecf9d5Sandi
103007738714SAndreas Gohr        $address = $this->_xmlEntities($address);
103100a7b5adSEsther Brunner        $address = obfuscate($address);
103200a7b5adSEsther Brunner        $title   = $address;
10338c128049SAndreas Gohr
103471352defSandi        if(empty($name)) {
103500a7b5adSEsther Brunner            $name = $address;
103671352defSandi        }
10370cecf9d5Sandi
1038776b36ecSAndreas Gohr        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1039776b36ecSAndreas Gohr
1040776b36ecSAndreas Gohr        $link['url']   = 'mailto:'.$address;
104171352defSandi        $link['name']  = $name;
104271352defSandi        $link['title'] = $title;
10430cecf9d5Sandi
104471352defSandi        //output formatted
1045a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10460cecf9d5Sandi    }
10470cecf9d5Sandi
10483dd5c225SAndreas Gohr    /**
10493dd5c225SAndreas Gohr     * Render an internal media file
10503dd5c225SAndreas Gohr     *
10513dd5c225SAndreas Gohr     * @param string $src       media ID
10523dd5c225SAndreas Gohr     * @param string $title     descriptive text
10533dd5c225SAndreas Gohr     * @param string $align     left|center|right
10543dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
10553dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
10563dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
10573dd5c225SAndreas Gohr     * @param string $linking   linkonly|detail|nolink
10583dd5c225SAndreas Gohr     * @param bool   $return    return HTML instead of adding to $doc
10593dd5c225SAndreas Gohr     * @return void|string
10603dd5c225SAndreas Gohr     */
10610ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
10623dd5c225SAndreas Gohr                           $height = null, $cache = null, $linking = null, $return = false) {
106337e34a5eSandi        global $ID;
106491df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
106537e34a5eSandi        resolve_mediaid(getNS($ID), $src, $exists);
10660cecf9d5Sandi
1067d98d4540SBen Coburn        $noLink = false;
10688acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1069b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
10703685f775Sandi
10713dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1072b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
1073dc673a5bSjoe.lapp            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), ($linking == 'direct'));
1074f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
10752a2a2ba2SAnika Henke            // don't link movies
107644881bd0Shenning.noren            $noLink = true;
107755efc227SAndreas Gohr        } else {
10782ca14335SEsther Brunner            // add file icons
10799d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
10809d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_'.$class;
10816de3759aSAndreas Gohr            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), true);
108291328684SMichael Hamann            if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
108355efc227SAndreas Gohr        }
10843685f775Sandi
108591df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
108691df343aSAndreas Gohr
10876fe20453SGina Haeussge        //markup non existing files
10884a24b459SKate Arzamastseva        if(!$exists) {
10896fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
10904a24b459SKate Arzamastseva        }
10916fe20453SGina Haeussge
10923685f775Sandi        //output formatted
1093f50634f0SAnika Henke        if($return) {
1094f50634f0SAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1095f50634f0SAnika Henke            else return $this->_formatLink($link);
1096f50634f0SAnika Henke        } else {
1097dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
10982ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
10990cecf9d5Sandi        }
1100f50634f0SAnika Henke    }
11010cecf9d5Sandi
11023dd5c225SAndreas Gohr    /**
11033dd5c225SAndreas Gohr     * Render an external media file
11043dd5c225SAndreas Gohr     *
11053dd5c225SAndreas Gohr     * @param string $src     full media URL
11063dd5c225SAndreas Gohr     * @param string $title   descriptive text
11073dd5c225SAndreas Gohr     * @param string $align   left|center|right
11083dd5c225SAndreas Gohr     * @param int    $width   width of media in pixel
11093dd5c225SAndreas Gohr     * @param int    $height  height of media in pixel
11103dd5c225SAndreas Gohr     * @param string $cache   cache|recache|nocache
11113dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1112410ee62aSAnika Henke     * @param bool   $return  return HTML instead of adding to $doc
11133dd5c225SAndreas Gohr     */
11140ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
1115410ee62aSAnika Henke                           $height = null, $cache = null, $linking = null, $return = false) {
111691df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
1117d98d4540SBen Coburn        $noLink = false;
11188acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1119b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1120b739ff0fSPierre Spring
1121b739ff0fSPierre Spring        $link['url'] = ml($src, array('cache' => $cache));
11223685f775Sandi
11233dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1124b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
11252ca14335SEsther Brunner            // link only jpeg images
112644881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1127f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
11282a2a2ba2SAnika Henke            // don't link movies
112944881bd0Shenning.noren            $noLink = true;
11302ca14335SEsther Brunner        } else {
11312ca14335SEsther Brunner            // add file icons
113227bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
113327bf7924STom N Harris            $link['class'] .= ' mediafile mf_'.$class;
11342ca14335SEsther Brunner        }
11352ca14335SEsther Brunner
113691df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
113791df343aSAndreas Gohr
11383685f775Sandi        //output formatted
1139410ee62aSAnika Henke        if($return) {
1140410ee62aSAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1141410ee62aSAnika Henke            else return $this->_formatLink($link);
1142410ee62aSAnika Henke        } else {
1143dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11442ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11450cecf9d5Sandi        }
1146410ee62aSAnika Henke    }
11470cecf9d5Sandi
11484826ab45Sandi    /**
11493db95becSAndreas Gohr     * Renders an RSS feed
1150b625487dSandi     *
1151b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1152b625487dSandi     */
11533db95becSAndreas Gohr    function rss($url, $params) {
1154b625487dSandi        global $lang;
11553db95becSAndreas Gohr        global $conf;
11563db95becSAndreas Gohr
11573db95becSAndreas Gohr        require_once(DOKU_INC.'inc/FeedParser.php');
11583db95becSAndreas Gohr        $feed = new FeedParser();
115900077af8SAndreas Gohr        $feed->set_feed_url($url);
1160b625487dSandi
1161b625487dSandi        //disable warning while fetching
11623dd5c225SAndreas Gohr        if(!defined('DOKU_E_LEVEL')) {
11633dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
11643dd5c225SAndreas Gohr        }
11653db95becSAndreas Gohr        $rc = $feed->init();
11663dd5c225SAndreas Gohr        if(isset($elvl)) {
11673dd5c225SAndreas Gohr            error_reporting($elvl);
11683dd5c225SAndreas Gohr        }
1169b625487dSandi
11703db95becSAndreas Gohr        //decide on start and end
11713db95becSAndreas Gohr        if($params['reverse']) {
11723db95becSAndreas Gohr            $mod   = -1;
11733db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
11743db95becSAndreas Gohr            $end   = $start - ($params['max']);
1175b2a412b0SAndreas Gohr            $end   = ($end < -1) ? -1 : $end;
11763db95becSAndreas Gohr        } else {
11773db95becSAndreas Gohr            $mod   = 1;
11783db95becSAndreas Gohr            $start = 0;
11793db95becSAndreas Gohr            $end   = $feed->get_item_quantity();
1180d91ab76fSMatt Perry            $end   = ($end > $params['max']) ? $params['max'] : $end;
11813db95becSAndreas Gohr        }
11823db95becSAndreas Gohr
1183a2d649c4Sandi        $this->doc .= '<ul class="rss">';
11843db95becSAndreas Gohr        if($rc) {
11853db95becSAndreas Gohr            for($x = $start; $x != $end; $x += $mod) {
11861bde1582SAndreas Gohr                $item = $feed->get_item($x);
11873db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
1188d2ea3363SAndreas Gohr                // support feeds without links
1189d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
1190d2ea3363SAndreas Gohr                if($lnkurl) {
1191793361f8SAndreas Gohr                    // title is escaped by SimplePie, we unescape here because it
1192793361f8SAndreas Gohr                    // is escaped again in externallink() FS#1705
11933dd5c225SAndreas Gohr                    $this->externallink(
11943dd5c225SAndreas Gohr                        $item->get_permalink(),
11953dd5c225SAndreas Gohr                        html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')
11963dd5c225SAndreas Gohr                    );
1197d2ea3363SAndreas Gohr                } else {
1198d2ea3363SAndreas Gohr                    $this->doc .= ' '.$item->get_title();
1199d2ea3363SAndreas Gohr                }
12003db95becSAndreas Gohr                if($params['author']) {
12011bde1582SAndreas Gohr                    $author = $item->get_author(0);
12021bde1582SAndreas Gohr                    if($author) {
12031bde1582SAndreas Gohr                        $name = $author->get_name();
12041bde1582SAndreas Gohr                        if(!$name) $name = $author->get_email();
12051bde1582SAndreas Gohr                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
12061bde1582SAndreas Gohr                    }
12073db95becSAndreas Gohr                }
12083db95becSAndreas Gohr                if($params['date']) {
12092e7e0c29SAndreas Gohr                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
12103db95becSAndreas Gohr                }
12111bde1582SAndreas Gohr                if($params['details']) {
12123db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
1213173dccb7STom N Harris                    if($conf['htmlok']) {
12141bde1582SAndreas Gohr                        $this->doc .= $item->get_description();
12153db95becSAndreas Gohr                    } else {
12161bde1582SAndreas Gohr                        $this->doc .= strip_tags($item->get_description());
12173db95becSAndreas Gohr                    }
12183db95becSAndreas Gohr                    $this->doc .= '</div>';
12193db95becSAndreas Gohr                }
12203db95becSAndreas Gohr
12213db95becSAndreas Gohr                $this->doc .= '</div></li>';
1222b625487dSandi            }
1223b625487dSandi        } else {
12243db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1225a2d649c4Sandi            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
1226b625487dSandi            $this->externallink($url);
122745e147ccSAndreas Gohr            if($conf['allowdebug']) {
122845e147ccSAndreas Gohr                $this->doc .= '<!--'.hsc($feed->error).'-->';
122945e147ccSAndreas Gohr            }
12303db95becSAndreas Gohr            $this->doc .= '</div></li>';
1231b625487dSandi        }
1232a2d649c4Sandi        $this->doc .= '</ul>';
1233b625487dSandi    }
1234b625487dSandi
12353dd5c225SAndreas Gohr    /**
12363dd5c225SAndreas Gohr     * Start a table
12373dd5c225SAndreas Gohr     *
12383dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
12393dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
12403dd5c225SAndreas Gohr     * @param int $pos     byte position in the original source
12413dd5c225SAndreas Gohr     */
1242619736fdSAdrian Lang    function table_open($maxcols = null, $numrows = null, $pos = null) {
1243b5742cedSPierre Spring        // initialize the row counter used for classes
1244b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1245619736fdSAdrian Lang        $class                         = 'table';
1246619736fdSAdrian Lang        if($pos !== null) {
1247619736fdSAdrian Lang            $class .= ' '.$this->startSectionEdit($pos, 'table');
1248619736fdSAdrian Lang        }
1249619736fdSAdrian Lang        $this->doc .= '<div class="'.$class.'"><table class="inline">'.
1250619736fdSAdrian Lang            DOKU_LF;
12510cecf9d5Sandi    }
12520cecf9d5Sandi
12533dd5c225SAndreas Gohr    /**
12543dd5c225SAndreas Gohr     * Close a table
12553dd5c225SAndreas Gohr     *
12563dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
12573dd5c225SAndreas Gohr     */
1258619736fdSAdrian Lang    function table_close($pos = null) {
1259a8574918SAnika Henke        $this->doc .= '</table></div>'.DOKU_LF;
1260619736fdSAdrian Lang        if($pos !== null) {
126190df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
12620cecf9d5Sandi        }
1263619736fdSAdrian Lang    }
12640cecf9d5Sandi
12653dd5c225SAndreas Gohr    /**
12663dd5c225SAndreas Gohr     * Open a table header
12673dd5c225SAndreas Gohr     */
1268f05a1cc5SGerrit Uitslag    function tablethead_open() {
1269f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
1270f05a1cc5SGerrit Uitslag    }
1271f05a1cc5SGerrit Uitslag
12723dd5c225SAndreas Gohr    /**
12733dd5c225SAndreas Gohr     * Close a table header
12743dd5c225SAndreas Gohr     */
1275f05a1cc5SGerrit Uitslag    function tablethead_close() {
1276f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
1277f05a1cc5SGerrit Uitslag    }
1278f05a1cc5SGerrit Uitslag
12793dd5c225SAndreas Gohr    /**
12803dd5c225SAndreas Gohr     * Open a table row
12813dd5c225SAndreas Gohr     */
12820cecf9d5Sandi    function tablerow_open() {
1283b5742cedSPierre Spring        // initialize the cell counter used for classes
1284b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1285b5742cedSPierre Spring        $class                          = 'row'.$this->_counter['row_counter']++;
1286b5742cedSPierre Spring        $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
12870cecf9d5Sandi    }
12880cecf9d5Sandi
12893dd5c225SAndreas Gohr    /**
12903dd5c225SAndreas Gohr     * Close a table row
12913dd5c225SAndreas Gohr     */
12920cecf9d5Sandi    function tablerow_close() {
1293a2d649c4Sandi        $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
12940cecf9d5Sandi    }
12950cecf9d5Sandi
12963dd5c225SAndreas Gohr    /**
12973dd5c225SAndreas Gohr     * Open a table header cell
12983dd5c225SAndreas Gohr     *
12993dd5c225SAndreas Gohr     * @param int    $colspan
13003dd5c225SAndreas Gohr     * @param string $align left|center|right
13013dd5c225SAndreas Gohr     * @param int    $rowspan
13023dd5c225SAndreas Gohr     */
13030ea51e63SMatt Perry    function tableheader_open($colspan = 1, $align = null, $rowspan = 1) {
1304b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13050cecf9d5Sandi        if(!is_null($align)) {
1306b5742cedSPierre Spring            $class .= ' '.$align.'align';
13070cecf9d5Sandi        }
1308b5742cedSPierre Spring        $class .= '"';
1309b5742cedSPierre Spring        $this->doc .= '<th '.$class;
13100cecf9d5Sandi        if($colspan > 1) {
1311a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1312a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13130cecf9d5Sandi        }
131425b97867Shakan.sandell        if($rowspan > 1) {
131525b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
131625b97867Shakan.sandell        }
1317a2d649c4Sandi        $this->doc .= '>';
13180cecf9d5Sandi    }
13190cecf9d5Sandi
13203dd5c225SAndreas Gohr    /**
13213dd5c225SAndreas Gohr     * Close a table header cell
13223dd5c225SAndreas Gohr     */
13230cecf9d5Sandi    function tableheader_close() {
1324a2d649c4Sandi        $this->doc .= '</th>';
13250cecf9d5Sandi    }
13260cecf9d5Sandi
13273dd5c225SAndreas Gohr    /**
13283dd5c225SAndreas Gohr     * Open a table cell
13293dd5c225SAndreas Gohr     *
13303dd5c225SAndreas Gohr     * @param int    $colspan
13313dd5c225SAndreas Gohr     * @param string $align left|center|right
13323dd5c225SAndreas Gohr     * @param int    $rowspan
13333dd5c225SAndreas Gohr     */
13340ea51e63SMatt Perry    function tablecell_open($colspan = 1, $align = null, $rowspan = 1) {
1335b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13360cecf9d5Sandi        if(!is_null($align)) {
1337b5742cedSPierre Spring            $class .= ' '.$align.'align';
13380cecf9d5Sandi        }
1339b5742cedSPierre Spring        $class .= '"';
1340b5742cedSPierre Spring        $this->doc .= '<td '.$class;
13410cecf9d5Sandi        if($colspan > 1) {
1342a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1343a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13440cecf9d5Sandi        }
134525b97867Shakan.sandell        if($rowspan > 1) {
134625b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
134725b97867Shakan.sandell        }
1348a2d649c4Sandi        $this->doc .= '>';
13490cecf9d5Sandi    }
13500cecf9d5Sandi
13513dd5c225SAndreas Gohr    /**
13523dd5c225SAndreas Gohr     * Close a table cell
13533dd5c225SAndreas Gohr     */
13540cecf9d5Sandi    function tablecell_close() {
1355a2d649c4Sandi        $this->doc .= '</td>';
13560cecf9d5Sandi    }
13570cecf9d5Sandi
13583dd5c225SAndreas Gohr    #region Utility functions
13590cecf9d5Sandi
1360ba11bd29Sandi    /**
13613fd0b676Sandi     * Build a link
13623fd0b676Sandi     *
13633fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1364ba11bd29Sandi     *
1365ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1366ba11bd29Sandi     */
1367433bef32Sandi    function _formatLink($link) {
1368ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1369ba11bd29Sandi        if(substr($link['url'], 0, 7) != 'mailto:') {
1370ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1371ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1372ba11bd29Sandi        }
1373ba11bd29Sandi        //remove double encodings in titles
1374ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1375ba11bd29Sandi
1376453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1377453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1378453493f2SAndreas Gohr        $link['url']   = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1379453493f2SAndreas Gohr        $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
1380453493f2SAndreas Gohr
1381ba11bd29Sandi        $ret = '';
1382ba11bd29Sandi        $ret .= $link['pre'];
1383ba11bd29Sandi        $ret .= '<a href="'.$link['url'].'"';
1384bb4866bdSchris        if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1385bb4866bdSchris        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1386bb4866bdSchris        if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1387bb4866bdSchris        if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
138844a6b4c7SAndreas Gohr        if(!empty($link['rel'])) $ret .= ' rel="'.$link['rel'].'"';
1389bb4866bdSchris        if(!empty($link['more'])) $ret .= ' '.$link['more'];
1390ba11bd29Sandi        $ret .= '>';
1391ba11bd29Sandi        $ret .= $link['name'];
1392ba11bd29Sandi        $ret .= '</a>';
1393ba11bd29Sandi        $ret .= $link['suf'];
1394ba11bd29Sandi        return $ret;
1395ba11bd29Sandi    }
1396ba11bd29Sandi
1397ba11bd29Sandi    /**
13983fd0b676Sandi     * Renders internal and external media
13993fd0b676Sandi     *
14003fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
14013dd5c225SAndreas Gohr     * @param string $src       media ID
14023dd5c225SAndreas Gohr     * @param string $title     descriptive text
14033dd5c225SAndreas Gohr     * @param string $align     left|center|right
14043dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
14053dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
14063dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
14073dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
14083dd5c225SAndreas Gohr     * @return string
14093fd0b676Sandi     */
14100ea51e63SMatt Perry    function _media($src, $title = null, $align = null, $width = null,
14110ea51e63SMatt Perry                    $height = null, $cache = null, $render = true) {
14123fd0b676Sandi
14133fd0b676Sandi        $ret = '';
14143fd0b676Sandi
14153dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src);
14163fd0b676Sandi        if(substr($mime, 0, 5) == 'image') {
1417b739ff0fSPierre Spring            // first get the $title
1418b739ff0fSPierre Spring            if(!is_null($title)) {
1419b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1420b739ff0fSPierre Spring            } elseif($ext == 'jpg' || $ext == 'jpeg') {
1421b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1422b739ff0fSPierre Spring                require_once(DOKU_INC.'inc/JpegMeta.php');
142367f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1424b739ff0fSPierre Spring                if($jpeg !== false) $cap = $jpeg->getTitle();
14253dd5c225SAndreas Gohr                if(!empty($cap)) {
1426b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1427b739ff0fSPierre Spring                }
1428b739ff0fSPierre Spring            }
1429b739ff0fSPierre Spring            if(!$render) {
1430b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1431b739ff0fSPierre Spring                // return the title of the picture
1432b739ff0fSPierre Spring                if(!$title) {
1433b739ff0fSPierre Spring                    // just show the sourcename
14343009a773SAndreas Gohr                    $title = $this->_xmlEntities(utf8_basename(noNS($src)));
1435b739ff0fSPierre Spring                }
1436b739ff0fSPierre Spring                return $title;
1437b739ff0fSPierre Spring            }
14383fd0b676Sandi            //add image tag
14396de3759aSAndreas Gohr            $ret .= '<img src="'.ml($src, array('w' => $width, 'h' => $height, 'cache' => $cache)).'"';
14403fd0b676Sandi            $ret .= ' class="media'.$align.'"';
14413fd0b676Sandi
1442b739ff0fSPierre Spring            if($title) {
1443b739ff0fSPierre Spring                $ret .= ' title="'.$title.'"';
1444b739ff0fSPierre Spring                $ret .= ' alt="'.$title.'"';
14453fd0b676Sandi            } else {
14463fd0b676Sandi                $ret .= ' alt=""';
14473fd0b676Sandi            }
14483fd0b676Sandi
14493fd0b676Sandi            if(!is_null($width))
14503fd0b676Sandi                $ret .= ' width="'.$this->_xmlEntities($width).'"';
14513fd0b676Sandi
14523fd0b676Sandi            if(!is_null($height))
14533fd0b676Sandi                $ret .= ' height="'.$this->_xmlEntities($height).'"';
14543fd0b676Sandi
14553fd0b676Sandi            $ret .= ' />';
14563fd0b676Sandi
145717954bb5SAnika Henke        } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
14582a2a2ba2SAnika Henke            // first get the $title
145917954bb5SAnika Henke            $title = !is_null($title) ? $this->_xmlEntities($title) : false;
14602a2a2ba2SAnika Henke            if(!$render) {
146117954bb5SAnika Henke                // if the file is not supposed to be rendered
146217954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
146317954bb5SAnika Henke                return $title ? $title : $this->_xmlEntities(utf8_basename(noNS($src)));
14642a2a2ba2SAnika Henke            }
14652a2a2ba2SAnika Henke
14662a2a2ba2SAnika Henke            $att          = array();
14672a2a2ba2SAnika Henke            $att['class'] = "media$align";
146817954bb5SAnika Henke            if($title) {
146917954bb5SAnika Henke                $att['title'] = $title;
147017954bb5SAnika Henke            }
14712a2a2ba2SAnika Henke
147217954bb5SAnika Henke            if(media_supportedav($mime, 'video')) {
147317954bb5SAnika Henke                //add video
147479e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1475b44a5dceSAnika Henke            }
147617954bb5SAnika Henke            if(media_supportedav($mime, 'audio')) {
1477b44a5dceSAnika Henke                //add audio
1478b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
147917954bb5SAnika Henke            }
1480b44a5dceSAnika Henke
14813fd0b676Sandi        } elseif($mime == 'application/x-shockwave-flash') {
14821c882ba8SAndreas Gohr            if(!$render) {
14831c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
14841c882ba8SAndreas Gohr                // return the title of the flash
14851c882ba8SAndreas Gohr                if(!$title) {
14861c882ba8SAndreas Gohr                    // just show the sourcename
14873009a773SAndreas Gohr                    $title = utf8_basename(noNS($src));
14881c882ba8SAndreas Gohr                }
148907bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
14901c882ba8SAndreas Gohr            }
14911c882ba8SAndreas Gohr
149207bf32b2SAndreas Gohr            $att          = array();
149307bf32b2SAndreas Gohr            $att['class'] = "media$align";
149407bf32b2SAndreas Gohr            if($align == 'right') $att['align'] = 'right';
149507bf32b2SAndreas Gohr            if($align == 'left') $att['align'] = 'left';
14963dd5c225SAndreas Gohr            $ret .= html_flashobject(
14973dd5c225SAndreas Gohr                ml($src, array('cache' => $cache), true, '&'), $width, $height,
149807bf32b2SAndreas Gohr                array('quality' => 'high'),
149907bf32b2SAndreas Gohr                null,
150007bf32b2SAndreas Gohr                $att,
15013dd5c225SAndreas Gohr                $this->_xmlEntities($title)
15023dd5c225SAndreas Gohr            );
15030f428d7dSAndreas Gohr        } elseif($title) {
15043fd0b676Sandi            // well at least we have a title to display
15053fd0b676Sandi            $ret .= $this->_xmlEntities($title);
15063fd0b676Sandi        } else {
15075291ca3aSAndreas Gohr            // just show the sourcename
15083009a773SAndreas Gohr            $ret .= $this->_xmlEntities(utf8_basename(noNS($src)));
15093fd0b676Sandi        }
15103fd0b676Sandi
15113fd0b676Sandi        return $ret;
15123fd0b676Sandi    }
15133fd0b676Sandi
15143dd5c225SAndreas Gohr    /**
15153dd5c225SAndreas Gohr     * Escape string for output
15163dd5c225SAndreas Gohr     *
15173dd5c225SAndreas Gohr     * @param $string
15183dd5c225SAndreas Gohr     * @return string
15193dd5c225SAndreas Gohr     */
1520433bef32Sandi    function _xmlEntities($string) {
1521de117061Schris        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
15220cecf9d5Sandi    }
15230cecf9d5Sandi
15248a831f2bSAndreas Gohr    /**
15258a831f2bSAndreas Gohr     * Creates a linkid from a headline
1526c5a8fd96SAndreas Gohr     *
15273dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1528c5a8fd96SAndreas Gohr     * @param string  $title   The headline title
1529c5a8fd96SAndreas Gohr     * @param boolean $create  Create a new unique ID?
15303dd5c225SAndreas Gohr     * @return string
15318a831f2bSAndreas Gohr     */
1532c5a8fd96SAndreas Gohr    function _headerToLink($title, $create = false) {
1533c5a8fd96SAndreas Gohr        if($create) {
15344ceab83fSAndreas Gohr            return sectionID($title, $this->headers);
15354ceab83fSAndreas Gohr        } else {
1536443d207bSAndreas Gohr            $check = false;
1537443d207bSAndreas Gohr            return sectionID($title, $check);
1538c5a8fd96SAndreas Gohr        }
15390cecf9d5Sandi    }
15400cecf9d5Sandi
1541af587fa8Sandi    /**
15423fd0b676Sandi     * Construct a title and handle images in titles
15433fd0b676Sandi     *
15440b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15453dd5c225SAndreas Gohr     * @param string|array $title    either string title or media array
15463dd5c225SAndreas Gohr     * @param string       $default  default title if nothing else is found
15473dd5c225SAndreas Gohr     * @param bool         $isImage  will be set to true if it's a media file
15483dd5c225SAndreas Gohr     * @param null|string  $id       linked page id (used to extract title from first heading)
15493dd5c225SAndreas Gohr     * @param string       $linktype content|navigation
15503dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
15513fd0b676Sandi     */
15520ea51e63SMatt Perry    function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
155344881bd0Shenning.noren        $isImage = false;
155429657f9eSAndreas Gohr        if(is_array($title)) {
155529657f9eSAndreas Gohr            $isImage = true;
155629657f9eSAndreas Gohr            return $this->_imageTitle($title);
155729657f9eSAndreas Gohr        } elseif(is_null($title) || trim($title) == '') {
1558fe9ec250SChris Smith            if(useHeading($linktype) && $id) {
155967c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1560bb0a59d4Sjan                if($heading) {
1561433bef32Sandi                    return $this->_xmlEntities($heading);
1562bb0a59d4Sjan                }
1563bb0a59d4Sjan            }
1564433bef32Sandi            return $this->_xmlEntities($default);
156568c26e6dSMichael Klier        } else {
156668c26e6dSMichael Klier            return $this->_xmlEntities($title);
15670cecf9d5Sandi        }
15680cecf9d5Sandi    }
15690cecf9d5Sandi
15700cecf9d5Sandi    /**
15713dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
15723fd0b676Sandi     *
15733fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
15743dd5c225SAndreas Gohr     * @param string $img
15753dd5c225SAndreas Gohr     * @return string HTML img tag or similar
15760cecf9d5Sandi     */
1577433bef32Sandi    function _imageTitle($img) {
1578d9baf1a7SKazutaka Miyasaka        global $ID;
1579d9baf1a7SKazutaka Miyasaka
1580d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1581d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
15823dd5c225SAndreas Gohr        list($img['src']) = explode('#', $img['src'], 2);
1583d9baf1a7SKazutaka Miyasaka        if($img['type'] == 'internalmedia') {
1584d9baf1a7SKazutaka Miyasaka            resolve_mediaid(getNS($ID), $img['src'], $exists);
1585d9baf1a7SKazutaka Miyasaka        }
1586d9baf1a7SKazutaka Miyasaka
15873dd5c225SAndreas Gohr        return $this->_media(
15883dd5c225SAndreas Gohr            $img['src'],
15894826ab45Sandi            $img['title'],
15904826ab45Sandi            $img['align'],
15914826ab45Sandi            $img['width'],
15924826ab45Sandi            $img['height'],
15933dd5c225SAndreas Gohr            $img['cache']
15943dd5c225SAndreas Gohr        );
15950cecf9d5Sandi    }
1596b739ff0fSPierre Spring
1597b739ff0fSPierre Spring    /**
15983dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
15993dd5c225SAndreas Gohr     *
16003dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1601b739ff0fSPierre Spring     *
1602b739ff0fSPierre Spring     * @author   Pierre Spring <pierre.spring@liip.ch>
16033dd5c225SAndreas Gohr     * @param string $src       media ID
16043dd5c225SAndreas Gohr     * @param string $title     descriptive text
16053dd5c225SAndreas Gohr     * @param string $align     left|center|right
16063dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
16073dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
16083dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
16093dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
16103dd5c225SAndreas Gohr     * @return array associative array with link config
1611b739ff0fSPierre Spring     */
1612d91ab76fSMatt Perry    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1613b739ff0fSPierre Spring        global $conf;
1614b739ff0fSPierre Spring
1615b739ff0fSPierre Spring        $link           = array();
1616b739ff0fSPierre Spring        $link['class']  = 'media';
1617b739ff0fSPierre Spring        $link['style']  = '';
1618b739ff0fSPierre Spring        $link['pre']    = '';
1619b739ff0fSPierre Spring        $link['suf']    = '';
1620b739ff0fSPierre Spring        $link['more']   = '';
1621b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1622b739ff0fSPierre Spring        $link['title']  = $this->_xmlEntities($src);
1623b739ff0fSPierre Spring        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1624b739ff0fSPierre Spring
1625b739ff0fSPierre Spring        return $link;
1626b739ff0fSPierre Spring    }
162791459163SAnika Henke
16282a2a2ba2SAnika Henke    /**
16292a2a2ba2SAnika Henke     * Embed video(s) in HTML
16302a2a2ba2SAnika Henke     *
16312a2a2ba2SAnika Henke     * @author Anika Henke <anika@selfthinker.org>
16322a2a2ba2SAnika Henke     *
16332a2a2ba2SAnika Henke     * @param string $src         - ID of video to embed
16342a2a2ba2SAnika Henke     * @param int    $width       - width of the video in pixels
16352a2a2ba2SAnika Henke     * @param int    $height      - height of the video in pixels
16362a2a2ba2SAnika Henke     * @param array  $atts        - additional attributes for the <video> tag
1637f50634f0SAnika Henke     * @return string
16382a2a2ba2SAnika Henke     */
163979e53fe5SAnika Henke    function _video($src, $width, $height, $atts = null) {
16402a2a2ba2SAnika Henke        // prepare width and height
16412a2a2ba2SAnika Henke        if(is_null($atts)) $atts = array();
16422a2a2ba2SAnika Henke        $atts['width']  = (int) $width;
16432a2a2ba2SAnika Henke        $atts['height'] = (int) $height;
16442a2a2ba2SAnika Henke        if(!$atts['width']) $atts['width'] = 320;
16452a2a2ba2SAnika Henke        if(!$atts['height']) $atts['height'] = 240;
16462a2a2ba2SAnika Henke
1647410ee62aSAnika Henke        $posterUrl = '';
1648410ee62aSAnika Henke        $files = array();
1649410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1650410ee62aSAnika Henke
1651410ee62aSAnika Henke        if ($isExternal) {
1652410ee62aSAnika Henke            // take direct source for external files
1653*702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1654410ee62aSAnika Henke            $files[$srcMime] = $src;
1655410ee62aSAnika Henke        } else {
16563d7a9e0aSAnika Henke            // prepare alternative formats
16573d7a9e0aSAnika Henke            $extensions   = array('webm', 'ogv', 'mp4');
1658410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
165999f943f6SAnika Henke            $poster       = media_alternativefiles($src, array('jpg', 'png'), true);
166099f943f6SAnika Henke            if(!empty($poster)) {
16612d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
166299f943f6SAnika Henke            }
1663410ee62aSAnika Henke        }
16642a2a2ba2SAnika Henke
1665f50634f0SAnika Henke        $out = '';
166679e53fe5SAnika Henke        // open video tag
1667f50634f0SAnika Henke        $out .= '<video '.buildAttributes($atts).' controls="controls"';
16683641199aSAnika Henke        if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1669f50634f0SAnika Henke        $out .= '>'.NL;
16703641199aSAnika Henke        $fallback = '';
167179e53fe5SAnika Henke
167279e53fe5SAnika Henke        // output source for each alternative video format
1673410ee62aSAnika Henke        foreach($files as $mime => $file) {
1674410ee62aSAnika Henke            if ($isExternal) {
1675410ee62aSAnika Henke                $url = $file;
1676410ee62aSAnika Henke                $linkType = 'externalmedia';
1677410ee62aSAnika Henke            } else {
16782d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1679410ee62aSAnika Henke                $linkType = 'internalmedia';
1680410ee62aSAnika Henke            }
168117954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
16823d7a9e0aSAnika Henke
1683f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
168479e53fe5SAnika Henke            // alternative content (just a link to the file)
1685410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
16863d7a9e0aSAnika Henke        }
16872a2a2ba2SAnika Henke
16882a2a2ba2SAnika Henke        // finish
16893641199aSAnika Henke        $out .= $fallback;
1690f50634f0SAnika Henke        $out .= '</video>'.NL;
1691f50634f0SAnika Henke        return $out;
16922a2a2ba2SAnika Henke    }
16932a2a2ba2SAnika Henke
1694b44a5dceSAnika Henke    /**
1695b44a5dceSAnika Henke     * Embed audio in HTML
1696b44a5dceSAnika Henke     *
1697b44a5dceSAnika Henke     * @author Anika Henke <anika@selfthinker.org>
1698b44a5dceSAnika Henke     *
1699b44a5dceSAnika Henke     * @param string $src       - ID of audio to embed
17006d4af72aSAnika Henke     * @param array  $atts      - additional attributes for the <audio> tag
1701f50634f0SAnika Henke     * @return string
1702b44a5dceSAnika Henke     */
1703b44a5dceSAnika Henke    function _audio($src, $atts = null) {
1704*702e97d3SAnika Henke        $files = array();
1705410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1706b44a5dceSAnika Henke
1707410ee62aSAnika Henke        if ($isExternal) {
1708410ee62aSAnika Henke            // take direct source for external files
1709*702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1710410ee62aSAnika Henke            $files[$srcMime] = $src;
1711410ee62aSAnika Henke        } else {
1712b44a5dceSAnika Henke            // prepare alternative formats
1713b44a5dceSAnika Henke            $extensions   = array('ogg', 'mp3', 'wav');
1714410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
1715410ee62aSAnika Henke        }
1716b44a5dceSAnika Henke
1717f50634f0SAnika Henke        $out = '';
1718b44a5dceSAnika Henke        // open audio tag
1719f50634f0SAnika Henke        $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
17203641199aSAnika Henke        $fallback = '';
1721b44a5dceSAnika Henke
1722b44a5dceSAnika Henke        // output source for each alternative audio format
1723410ee62aSAnika Henke        foreach($files as $mime => $file) {
1724410ee62aSAnika Henke            if ($isExternal) {
1725410ee62aSAnika Henke                $url = $file;
1726410ee62aSAnika Henke                $linkType = 'externalmedia';
1727410ee62aSAnika Henke            } else {
17282d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1729410ee62aSAnika Henke                $linkType = 'internalmedia';
1730410ee62aSAnika Henke            }
173117954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
1732b44a5dceSAnika Henke
1733f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1734b44a5dceSAnika Henke            // alternative content (just a link to the file)
1735410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
1736b44a5dceSAnika Henke        }
1737b44a5dceSAnika Henke
1738b44a5dceSAnika Henke        // finish
17393641199aSAnika Henke        $out .= $fallback;
1740f50634f0SAnika Henke        $out .= '</audio>'.NL;
1741f50634f0SAnika Henke        return $out;
1742b44a5dceSAnika Henke    }
1743b44a5dceSAnika Henke
17443dd5c225SAndreas Gohr    #endregion
17450cecf9d5Sandi}
17460cecf9d5Sandi
1747e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1748