xref: /dokuwiki/inc/parser/xhtml.php (revision 59bc3b48fdffb76ee65a4b630be3ffa1f6c20c80)
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
823*59bc3b48SGerrit Uitslag        $link = array();
824fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
8250e1c636eSandi        if(!$isImage) {
8260e1c636eSandi            if($exists) {
827ba11bd29Sandi                $class = 'wikilink1';
8280cecf9d5Sandi            } else {
829ba11bd29Sandi                $class       = 'wikilink2';
83044a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
8310cecf9d5Sandi            }
8320cecf9d5Sandi        } else {
833ba11bd29Sandi            $class = 'media';
8340cecf9d5Sandi        }
8350cecf9d5Sandi
836a1685bedSandi        //keep hash anchor
8376d2af55dSChristopher Smith        @list($id, $hash) = explode('#', $id, 2);
838943dedc6SAndreas Gohr        if(!empty($hash)) $hash = $this->_headerToLink($hash);
839a1685bedSandi
840ba11bd29Sandi        //prepare for formating
841ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
842ba11bd29Sandi        $link['style']  = '';
843ba11bd29Sandi        $link['pre']    = '';
844ba11bd29Sandi        $link['suf']    = '';
84540eb54bbSjan        // highlight link to current page
846c4dda6afSAnika Henke        if($id == $INFO['id']) {
84792795d04Sandi            $link['pre'] = '<span class="curid">';
84892795d04Sandi            $link['suf'] = '</span>';
84940eb54bbSjan        }
8505e163278SAndreas Gohr        $link['more']  = '';
851ba11bd29Sandi        $link['class'] = $class;
85244653a53SAdrian Lang        $link['url']   = wl($id, $params);
853ba11bd29Sandi        $link['name']  = $name;
854ba11bd29Sandi        $link['title'] = $id;
855723d78dbSandi        //add search string
856723d78dbSandi        if($search) {
857546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
858546d3a99SAndreas Gohr            if(is_array($search)) {
859546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
860546d3a99SAndreas Gohr                $link['url'] .= 's[]='.join('&amp;s[]=', $search);
861546d3a99SAndreas Gohr            } else {
862546d3a99SAndreas Gohr                $link['url'] .= 's='.rawurlencode($search);
863546d3a99SAndreas Gohr            }
864723d78dbSandi        }
865723d78dbSandi
866a1685bedSandi        //keep hash
867a1685bedSandi        if($hash) $link['url'] .= '#'.$hash;
868a1685bedSandi
869ba11bd29Sandi        //output formatted
870cffcc403Sandi        if($returnonly) {
871cffcc403Sandi            return $this->_formatLink($link);
872cffcc403Sandi        } else {
873a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
8740cecf9d5Sandi        }
875cffcc403Sandi    }
8760cecf9d5Sandi
8773dd5c225SAndreas Gohr    /**
8783dd5c225SAndreas Gohr     * Render an external link
8793dd5c225SAndreas Gohr     *
8803dd5c225SAndreas Gohr     * @param string       $url  full URL with scheme
8813dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
8823dd5c225SAndreas Gohr     */
8830ea51e63SMatt Perry    function externallink($url, $name = null) {
884b625487dSandi        global $conf;
8850cecf9d5Sandi
886433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
8876f0c5dbfSandi
888b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
889b52b1596SAndreas Gohr        if(is_null($this->schemes)) $this->schemes = getSchemes();
890b52b1596SAndreas Gohr        list($scheme) = explode('://', $url);
891b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
892b52b1596SAndreas Gohr        if(!in_array($scheme, $this->schemes)) $url = '';
893b52b1596SAndreas Gohr
894b52b1596SAndreas Gohr        // is there still an URL?
895b52b1596SAndreas Gohr        if(!$url) {
896b52b1596SAndreas Gohr            $this->doc .= $name;
897b52b1596SAndreas Gohr            return;
898b52b1596SAndreas Gohr        }
899b52b1596SAndreas Gohr
900b52b1596SAndreas Gohr        // set class
9010cecf9d5Sandi        if(!$isImage) {
902b625487dSandi            $class = 'urlextern';
9030cecf9d5Sandi        } else {
904b625487dSandi            $class = 'media';
9050cecf9d5Sandi        }
9060cecf9d5Sandi
907b625487dSandi        //prepare for formating
908*59bc3b48SGerrit Uitslag        $link = array();
909b625487dSandi        $link['target'] = $conf['target']['extern'];
910b625487dSandi        $link['style']  = '';
911b625487dSandi        $link['pre']    = '';
912b625487dSandi        $link['suf']    = '';
9135e163278SAndreas Gohr        $link['more']   = '';
914b625487dSandi        $link['class']  = $class;
915b625487dSandi        $link['url']    = $url;
916e1c10e4dSchris
917b625487dSandi        $link['name']  = $name;
918433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
919b625487dSandi        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
9200cecf9d5Sandi
921b625487dSandi        //output formatted
922a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9230cecf9d5Sandi    }
9240cecf9d5Sandi
9250cecf9d5Sandi    /**
9263dd5c225SAndreas Gohr     * Render an interwiki link
9273dd5c225SAndreas Gohr     *
9283dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
9293dd5c225SAndreas Gohr     *
9303dd5c225SAndreas Gohr     * @param string       $match     original link - probably not much use
9313dd5c225SAndreas Gohr     * @param string|array $name      name for the link, array for media file
9323dd5c225SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
9333dd5c225SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
9340cecf9d5Sandi     */
9350ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
936b625487dSandi        global $conf;
9370cecf9d5Sandi
93897a3e4e3Sandi        $link           = array();
93997a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
94097a3e4e3Sandi        $link['pre']    = '';
94197a3e4e3Sandi        $link['suf']    = '';
9425e163278SAndreas Gohr        $link['more']   = '';
943433bef32Sandi        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
9440cecf9d5Sandi
94597a3e4e3Sandi        //get interwiki URL
9466496c33fSGerrit Uitslag        $exists = null;
9476496c33fSGerrit Uitslag        $url    = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
9480cecf9d5Sandi
94997a3e4e3Sandi        if(!$isImage) {
9509d2ddea4SAndreas Gohr            $class         = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
9519d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
9521c2d1019SAndreas Gohr        } else {
9531c2d1019SAndreas Gohr            $link['class'] = 'media';
95497a3e4e3Sandi        }
9550cecf9d5Sandi
95697a3e4e3Sandi        //do we stay at the same server? Use local target
9572345e871SGerrit Uitslag        if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
95897a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
95997a3e4e3Sandi        }
9606496c33fSGerrit Uitslag        if($exists !== null && !$isImage) {
9616496c33fSGerrit Uitslag            if($exists) {
9626496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
9636496c33fSGerrit Uitslag            } else {
9646496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
9656496c33fSGerrit Uitslag                $link['rel'] = 'nofollow';
9666496c33fSGerrit Uitslag            }
9676496c33fSGerrit Uitslag        }
9680cecf9d5Sandi
96997a3e4e3Sandi        $link['url']   = $url;
97097a3e4e3Sandi        $link['title'] = htmlspecialchars($link['url']);
97197a3e4e3Sandi
97297a3e4e3Sandi        //output formatted
973a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9740cecf9d5Sandi    }
9750cecf9d5Sandi
9760cecf9d5Sandi    /**
9773dd5c225SAndreas Gohr     * Link to windows share
9783dd5c225SAndreas Gohr     *
9793dd5c225SAndreas Gohr     * @param string       $url  the link
9803dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
9810cecf9d5Sandi     */
9820ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
9831d47afe1Sandi        global $conf;
9843dd5c225SAndreas Gohr
9851d47afe1Sandi        //simple setup
986*59bc3b48SGerrit Uitslag        $link = array();
9871d47afe1Sandi        $link['target'] = $conf['target']['windows'];
9881d47afe1Sandi        $link['pre']    = '';
9891d47afe1Sandi        $link['suf']    = '';
9901d47afe1Sandi        $link['style']  = '';
9910cecf9d5Sandi
992433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
9930cecf9d5Sandi        if(!$isImage) {
9941d47afe1Sandi            $link['class'] = 'windows';
9950cecf9d5Sandi        } else {
9961d47afe1Sandi            $link['class'] = 'media';
9970cecf9d5Sandi        }
9980cecf9d5Sandi
999433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
10001d47afe1Sandi        $url           = str_replace('\\', '/', $url);
10011d47afe1Sandi        $url           = 'file:///'.$url;
10021d47afe1Sandi        $link['url']   = $url;
10030cecf9d5Sandi
10041d47afe1Sandi        //output formatted
1005a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10060cecf9d5Sandi    }
10070cecf9d5Sandi
10083dd5c225SAndreas Gohr    /**
10093dd5c225SAndreas Gohr     * Render a linked E-Mail Address
10103dd5c225SAndreas Gohr     *
10113dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
10123dd5c225SAndreas Gohr     *
10133dd5c225SAndreas Gohr     * @param string       $address Email-Address
10143dd5c225SAndreas Gohr     * @param string|array $name    name for the link, array for media file
10153dd5c225SAndreas Gohr     */
10160ea51e63SMatt Perry    function emaillink($address, $name = null) {
101771352defSandi        global $conf;
101871352defSandi        //simple setup
101971352defSandi        $link           = array();
102071352defSandi        $link['target'] = '';
102171352defSandi        $link['pre']    = '';
102271352defSandi        $link['suf']    = '';
102371352defSandi        $link['style']  = '';
102471352defSandi        $link['more']   = '';
10250cecf9d5Sandi
1026c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
10270cecf9d5Sandi        if(!$isImage) {
1028be96545cSAnika Henke            $link['class'] = 'mail';
10290cecf9d5Sandi        } else {
1030be96545cSAnika Henke            $link['class'] = 'media';
10310cecf9d5Sandi        }
10320cecf9d5Sandi
103307738714SAndreas Gohr        $address = $this->_xmlEntities($address);
103400a7b5adSEsther Brunner        $address = obfuscate($address);
103500a7b5adSEsther Brunner        $title   = $address;
10368c128049SAndreas Gohr
103771352defSandi        if(empty($name)) {
103800a7b5adSEsther Brunner            $name = $address;
103971352defSandi        }
10400cecf9d5Sandi
1041776b36ecSAndreas Gohr        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1042776b36ecSAndreas Gohr
1043776b36ecSAndreas Gohr        $link['url']   = 'mailto:'.$address;
104471352defSandi        $link['name']  = $name;
104571352defSandi        $link['title'] = $title;
10460cecf9d5Sandi
104771352defSandi        //output formatted
1048a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10490cecf9d5Sandi    }
10500cecf9d5Sandi
10513dd5c225SAndreas Gohr    /**
10523dd5c225SAndreas Gohr     * Render an internal media file
10533dd5c225SAndreas Gohr     *
10543dd5c225SAndreas Gohr     * @param string $src       media ID
10553dd5c225SAndreas Gohr     * @param string $title     descriptive text
10563dd5c225SAndreas Gohr     * @param string $align     left|center|right
10573dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
10583dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
10593dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
10603dd5c225SAndreas Gohr     * @param string $linking   linkonly|detail|nolink
10613dd5c225SAndreas Gohr     * @param bool   $return    return HTML instead of adding to $doc
10623dd5c225SAndreas Gohr     * @return void|string
10633dd5c225SAndreas Gohr     */
10640ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
10653dd5c225SAndreas Gohr                           $height = null, $cache = null, $linking = null, $return = false) {
106637e34a5eSandi        global $ID;
106791df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
106837e34a5eSandi        resolve_mediaid(getNS($ID), $src, $exists);
10690cecf9d5Sandi
1070d98d4540SBen Coburn        $noLink = false;
10718acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1072b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
10733685f775Sandi
10743dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1075b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
1076dc673a5bSjoe.lapp            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), ($linking == 'direct'));
1077f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
10782a2a2ba2SAnika Henke            // don't link movies
107944881bd0Shenning.noren            $noLink = true;
108055efc227SAndreas Gohr        } else {
10812ca14335SEsther Brunner            // add file icons
10829d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
10839d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_'.$class;
10846de3759aSAndreas Gohr            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), true);
108591328684SMichael Hamann            if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
108655efc227SAndreas Gohr        }
10873685f775Sandi
108891df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
108991df343aSAndreas Gohr
10906fe20453SGina Haeussge        //markup non existing files
10914a24b459SKate Arzamastseva        if(!$exists) {
10926fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
10934a24b459SKate Arzamastseva        }
10946fe20453SGina Haeussge
10953685f775Sandi        //output formatted
1096f50634f0SAnika Henke        if($return) {
1097f50634f0SAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1098f50634f0SAnika Henke            else return $this->_formatLink($link);
1099f50634f0SAnika Henke        } else {
1100dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11012ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11020cecf9d5Sandi        }
1103f50634f0SAnika Henke    }
11040cecf9d5Sandi
11053dd5c225SAndreas Gohr    /**
11063dd5c225SAndreas Gohr     * Render an external media file
11073dd5c225SAndreas Gohr     *
11083dd5c225SAndreas Gohr     * @param string $src     full media URL
11093dd5c225SAndreas Gohr     * @param string $title   descriptive text
11103dd5c225SAndreas Gohr     * @param string $align   left|center|right
11113dd5c225SAndreas Gohr     * @param int    $width   width of media in pixel
11123dd5c225SAndreas Gohr     * @param int    $height  height of media in pixel
11133dd5c225SAndreas Gohr     * @param string $cache   cache|recache|nocache
11143dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1115410ee62aSAnika Henke     * @param bool   $return  return HTML instead of adding to $doc
11163dd5c225SAndreas Gohr     */
11170ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
1118410ee62aSAnika Henke                           $height = null, $cache = null, $linking = null, $return = false) {
111991df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
1120d98d4540SBen Coburn        $noLink = false;
11218acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1122b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1123b739ff0fSPierre Spring
1124b739ff0fSPierre Spring        $link['url'] = ml($src, array('cache' => $cache));
11253685f775Sandi
11263dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1127b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
11282ca14335SEsther Brunner            // link only jpeg images
112944881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1130f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
11312a2a2ba2SAnika Henke            // don't link movies
113244881bd0Shenning.noren            $noLink = true;
11332ca14335SEsther Brunner        } else {
11342ca14335SEsther Brunner            // add file icons
113527bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
113627bf7924STom N Harris            $link['class'] .= ' mediafile mf_'.$class;
11372ca14335SEsther Brunner        }
11382ca14335SEsther Brunner
113991df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
114091df343aSAndreas Gohr
11413685f775Sandi        //output formatted
1142410ee62aSAnika Henke        if($return) {
1143410ee62aSAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1144410ee62aSAnika Henke            else return $this->_formatLink($link);
1145410ee62aSAnika Henke        } else {
1146dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11472ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11480cecf9d5Sandi        }
1149410ee62aSAnika Henke    }
11500cecf9d5Sandi
11514826ab45Sandi    /**
11523db95becSAndreas Gohr     * Renders an RSS feed
1153b625487dSandi     *
1154b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1155b625487dSandi     */
11563db95becSAndreas Gohr    function rss($url, $params) {
1157b625487dSandi        global $lang;
11583db95becSAndreas Gohr        global $conf;
11593db95becSAndreas Gohr
11603db95becSAndreas Gohr        require_once(DOKU_INC.'inc/FeedParser.php');
11613db95becSAndreas Gohr        $feed = new FeedParser();
116200077af8SAndreas Gohr        $feed->set_feed_url($url);
1163b625487dSandi
1164b625487dSandi        //disable warning while fetching
11653dd5c225SAndreas Gohr        if(!defined('DOKU_E_LEVEL')) {
11663dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
11673dd5c225SAndreas Gohr        }
11683db95becSAndreas Gohr        $rc = $feed->init();
11693dd5c225SAndreas Gohr        if(isset($elvl)) {
11703dd5c225SAndreas Gohr            error_reporting($elvl);
11713dd5c225SAndreas Gohr        }
1172b625487dSandi
11733db95becSAndreas Gohr        //decide on start and end
11743db95becSAndreas Gohr        if($params['reverse']) {
11753db95becSAndreas Gohr            $mod   = -1;
11763db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
11773db95becSAndreas Gohr            $end   = $start - ($params['max']);
1178b2a412b0SAndreas Gohr            $end   = ($end < -1) ? -1 : $end;
11793db95becSAndreas Gohr        } else {
11803db95becSAndreas Gohr            $mod   = 1;
11813db95becSAndreas Gohr            $start = 0;
11823db95becSAndreas Gohr            $end   = $feed->get_item_quantity();
1183d91ab76fSMatt Perry            $end   = ($end > $params['max']) ? $params['max'] : $end;
11843db95becSAndreas Gohr        }
11853db95becSAndreas Gohr
1186a2d649c4Sandi        $this->doc .= '<ul class="rss">';
11873db95becSAndreas Gohr        if($rc) {
11883db95becSAndreas Gohr            for($x = $start; $x != $end; $x += $mod) {
11891bde1582SAndreas Gohr                $item = $feed->get_item($x);
11903db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
1191d2ea3363SAndreas Gohr                // support feeds without links
1192d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
1193d2ea3363SAndreas Gohr                if($lnkurl) {
1194793361f8SAndreas Gohr                    // title is escaped by SimplePie, we unescape here because it
1195793361f8SAndreas Gohr                    // is escaped again in externallink() FS#1705
11963dd5c225SAndreas Gohr                    $this->externallink(
11973dd5c225SAndreas Gohr                        $item->get_permalink(),
11983dd5c225SAndreas Gohr                        html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')
11993dd5c225SAndreas Gohr                    );
1200d2ea3363SAndreas Gohr                } else {
1201d2ea3363SAndreas Gohr                    $this->doc .= ' '.$item->get_title();
1202d2ea3363SAndreas Gohr                }
12033db95becSAndreas Gohr                if($params['author']) {
12041bde1582SAndreas Gohr                    $author = $item->get_author(0);
12051bde1582SAndreas Gohr                    if($author) {
12061bde1582SAndreas Gohr                        $name = $author->get_name();
12071bde1582SAndreas Gohr                        if(!$name) $name = $author->get_email();
12081bde1582SAndreas Gohr                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
12091bde1582SAndreas Gohr                    }
12103db95becSAndreas Gohr                }
12113db95becSAndreas Gohr                if($params['date']) {
12122e7e0c29SAndreas Gohr                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
12133db95becSAndreas Gohr                }
12141bde1582SAndreas Gohr                if($params['details']) {
12153db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
1216173dccb7STom N Harris                    if($conf['htmlok']) {
12171bde1582SAndreas Gohr                        $this->doc .= $item->get_description();
12183db95becSAndreas Gohr                    } else {
12191bde1582SAndreas Gohr                        $this->doc .= strip_tags($item->get_description());
12203db95becSAndreas Gohr                    }
12213db95becSAndreas Gohr                    $this->doc .= '</div>';
12223db95becSAndreas Gohr                }
12233db95becSAndreas Gohr
12243db95becSAndreas Gohr                $this->doc .= '</div></li>';
1225b625487dSandi            }
1226b625487dSandi        } else {
12273db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1228a2d649c4Sandi            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
1229b625487dSandi            $this->externallink($url);
123045e147ccSAndreas Gohr            if($conf['allowdebug']) {
123145e147ccSAndreas Gohr                $this->doc .= '<!--'.hsc($feed->error).'-->';
123245e147ccSAndreas Gohr            }
12333db95becSAndreas Gohr            $this->doc .= '</div></li>';
1234b625487dSandi        }
1235a2d649c4Sandi        $this->doc .= '</ul>';
1236b625487dSandi    }
1237b625487dSandi
12383dd5c225SAndreas Gohr    /**
12393dd5c225SAndreas Gohr     * Start a table
12403dd5c225SAndreas Gohr     *
12413dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
12423dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
12433dd5c225SAndreas Gohr     * @param int $pos     byte position in the original source
12443dd5c225SAndreas Gohr     */
1245619736fdSAdrian Lang    function table_open($maxcols = null, $numrows = null, $pos = null) {
1246b5742cedSPierre Spring        // initialize the row counter used for classes
1247b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1248619736fdSAdrian Lang        $class                         = 'table';
1249619736fdSAdrian Lang        if($pos !== null) {
1250619736fdSAdrian Lang            $class .= ' '.$this->startSectionEdit($pos, 'table');
1251619736fdSAdrian Lang        }
1252619736fdSAdrian Lang        $this->doc .= '<div class="'.$class.'"><table class="inline">'.
1253619736fdSAdrian Lang            DOKU_LF;
12540cecf9d5Sandi    }
12550cecf9d5Sandi
12563dd5c225SAndreas Gohr    /**
12573dd5c225SAndreas Gohr     * Close a table
12583dd5c225SAndreas Gohr     *
12593dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
12603dd5c225SAndreas Gohr     */
1261619736fdSAdrian Lang    function table_close($pos = null) {
1262a8574918SAnika Henke        $this->doc .= '</table></div>'.DOKU_LF;
1263619736fdSAdrian Lang        if($pos !== null) {
126490df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
12650cecf9d5Sandi        }
1266619736fdSAdrian Lang    }
12670cecf9d5Sandi
12683dd5c225SAndreas Gohr    /**
12693dd5c225SAndreas Gohr     * Open a table header
12703dd5c225SAndreas Gohr     */
1271f05a1cc5SGerrit Uitslag    function tablethead_open() {
1272f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
1273f05a1cc5SGerrit Uitslag    }
1274f05a1cc5SGerrit Uitslag
12753dd5c225SAndreas Gohr    /**
12763dd5c225SAndreas Gohr     * Close a table header
12773dd5c225SAndreas Gohr     */
1278f05a1cc5SGerrit Uitslag    function tablethead_close() {
1279f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
1280f05a1cc5SGerrit Uitslag    }
1281f05a1cc5SGerrit Uitslag
12823dd5c225SAndreas Gohr    /**
12833dd5c225SAndreas Gohr     * Open a table row
12843dd5c225SAndreas Gohr     */
12850cecf9d5Sandi    function tablerow_open() {
1286b5742cedSPierre Spring        // initialize the cell counter used for classes
1287b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1288b5742cedSPierre Spring        $class                          = 'row'.$this->_counter['row_counter']++;
1289b5742cedSPierre Spring        $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
12900cecf9d5Sandi    }
12910cecf9d5Sandi
12923dd5c225SAndreas Gohr    /**
12933dd5c225SAndreas Gohr     * Close a table row
12943dd5c225SAndreas Gohr     */
12950cecf9d5Sandi    function tablerow_close() {
1296a2d649c4Sandi        $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
12970cecf9d5Sandi    }
12980cecf9d5Sandi
12993dd5c225SAndreas Gohr    /**
13003dd5c225SAndreas Gohr     * Open a table header cell
13013dd5c225SAndreas Gohr     *
13023dd5c225SAndreas Gohr     * @param int    $colspan
13033dd5c225SAndreas Gohr     * @param string $align left|center|right
13043dd5c225SAndreas Gohr     * @param int    $rowspan
13053dd5c225SAndreas Gohr     */
13060ea51e63SMatt Perry    function tableheader_open($colspan = 1, $align = null, $rowspan = 1) {
1307b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13080cecf9d5Sandi        if(!is_null($align)) {
1309b5742cedSPierre Spring            $class .= ' '.$align.'align';
13100cecf9d5Sandi        }
1311b5742cedSPierre Spring        $class .= '"';
1312b5742cedSPierre Spring        $this->doc .= '<th '.$class;
13130cecf9d5Sandi        if($colspan > 1) {
1314a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1315a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13160cecf9d5Sandi        }
131725b97867Shakan.sandell        if($rowspan > 1) {
131825b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
131925b97867Shakan.sandell        }
1320a2d649c4Sandi        $this->doc .= '>';
13210cecf9d5Sandi    }
13220cecf9d5Sandi
13233dd5c225SAndreas Gohr    /**
13243dd5c225SAndreas Gohr     * Close a table header cell
13253dd5c225SAndreas Gohr     */
13260cecf9d5Sandi    function tableheader_close() {
1327a2d649c4Sandi        $this->doc .= '</th>';
13280cecf9d5Sandi    }
13290cecf9d5Sandi
13303dd5c225SAndreas Gohr    /**
13313dd5c225SAndreas Gohr     * Open a table cell
13323dd5c225SAndreas Gohr     *
13333dd5c225SAndreas Gohr     * @param int    $colspan
13343dd5c225SAndreas Gohr     * @param string $align left|center|right
13353dd5c225SAndreas Gohr     * @param int    $rowspan
13363dd5c225SAndreas Gohr     */
13370ea51e63SMatt Perry    function tablecell_open($colspan = 1, $align = null, $rowspan = 1) {
1338b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13390cecf9d5Sandi        if(!is_null($align)) {
1340b5742cedSPierre Spring            $class .= ' '.$align.'align';
13410cecf9d5Sandi        }
1342b5742cedSPierre Spring        $class .= '"';
1343b5742cedSPierre Spring        $this->doc .= '<td '.$class;
13440cecf9d5Sandi        if($colspan > 1) {
1345a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1346a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13470cecf9d5Sandi        }
134825b97867Shakan.sandell        if($rowspan > 1) {
134925b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
135025b97867Shakan.sandell        }
1351a2d649c4Sandi        $this->doc .= '>';
13520cecf9d5Sandi    }
13530cecf9d5Sandi
13543dd5c225SAndreas Gohr    /**
13553dd5c225SAndreas Gohr     * Close a table cell
13563dd5c225SAndreas Gohr     */
13570cecf9d5Sandi    function tablecell_close() {
1358a2d649c4Sandi        $this->doc .= '</td>';
13590cecf9d5Sandi    }
13600cecf9d5Sandi
13613dd5c225SAndreas Gohr    #region Utility functions
13620cecf9d5Sandi
1363ba11bd29Sandi    /**
13643fd0b676Sandi     * Build a link
13653fd0b676Sandi     *
13663fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1367ba11bd29Sandi     *
1368ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1369ba11bd29Sandi     */
1370433bef32Sandi    function _formatLink($link) {
1371ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1372ba11bd29Sandi        if(substr($link['url'], 0, 7) != 'mailto:') {
1373ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1374ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1375ba11bd29Sandi        }
1376ba11bd29Sandi        //remove double encodings in titles
1377ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1378ba11bd29Sandi
1379453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1380453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1381453493f2SAndreas Gohr        $link['url']   = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1382453493f2SAndreas Gohr        $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
1383453493f2SAndreas Gohr
1384ba11bd29Sandi        $ret = '';
1385ba11bd29Sandi        $ret .= $link['pre'];
1386ba11bd29Sandi        $ret .= '<a href="'.$link['url'].'"';
1387bb4866bdSchris        if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1388bb4866bdSchris        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1389bb4866bdSchris        if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1390bb4866bdSchris        if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
139144a6b4c7SAndreas Gohr        if(!empty($link['rel'])) $ret .= ' rel="'.$link['rel'].'"';
1392bb4866bdSchris        if(!empty($link['more'])) $ret .= ' '.$link['more'];
1393ba11bd29Sandi        $ret .= '>';
1394ba11bd29Sandi        $ret .= $link['name'];
1395ba11bd29Sandi        $ret .= '</a>';
1396ba11bd29Sandi        $ret .= $link['suf'];
1397ba11bd29Sandi        return $ret;
1398ba11bd29Sandi    }
1399ba11bd29Sandi
1400ba11bd29Sandi    /**
14013fd0b676Sandi     * Renders internal and external media
14023fd0b676Sandi     *
14033fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
14043dd5c225SAndreas Gohr     * @param string $src       media ID
14053dd5c225SAndreas Gohr     * @param string $title     descriptive text
14063dd5c225SAndreas Gohr     * @param string $align     left|center|right
14073dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
14083dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
14093dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
14103dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
14113dd5c225SAndreas Gohr     * @return string
14123fd0b676Sandi     */
14130ea51e63SMatt Perry    function _media($src, $title = null, $align = null, $width = null,
14140ea51e63SMatt Perry                    $height = null, $cache = null, $render = true) {
14153fd0b676Sandi
14163fd0b676Sandi        $ret = '';
14173fd0b676Sandi
14183dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src);
14193fd0b676Sandi        if(substr($mime, 0, 5) == 'image') {
1420b739ff0fSPierre Spring            // first get the $title
1421b739ff0fSPierre Spring            if(!is_null($title)) {
1422b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1423b739ff0fSPierre Spring            } elseif($ext == 'jpg' || $ext == 'jpeg') {
1424b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1425b739ff0fSPierre Spring                require_once(DOKU_INC.'inc/JpegMeta.php');
142667f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1427b739ff0fSPierre Spring                if($jpeg !== false) $cap = $jpeg->getTitle();
14283dd5c225SAndreas Gohr                if(!empty($cap)) {
1429b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1430b739ff0fSPierre Spring                }
1431b739ff0fSPierre Spring            }
1432b739ff0fSPierre Spring            if(!$render) {
1433b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1434b739ff0fSPierre Spring                // return the title of the picture
1435b739ff0fSPierre Spring                if(!$title) {
1436b739ff0fSPierre Spring                    // just show the sourcename
14373009a773SAndreas Gohr                    $title = $this->_xmlEntities(utf8_basename(noNS($src)));
1438b739ff0fSPierre Spring                }
1439b739ff0fSPierre Spring                return $title;
1440b739ff0fSPierre Spring            }
14413fd0b676Sandi            //add image tag
14426de3759aSAndreas Gohr            $ret .= '<img src="'.ml($src, array('w' => $width, 'h' => $height, 'cache' => $cache)).'"';
14433fd0b676Sandi            $ret .= ' class="media'.$align.'"';
14443fd0b676Sandi
1445b739ff0fSPierre Spring            if($title) {
1446b739ff0fSPierre Spring                $ret .= ' title="'.$title.'"';
1447b739ff0fSPierre Spring                $ret .= ' alt="'.$title.'"';
14483fd0b676Sandi            } else {
14493fd0b676Sandi                $ret .= ' alt=""';
14503fd0b676Sandi            }
14513fd0b676Sandi
14523fd0b676Sandi            if(!is_null($width))
14533fd0b676Sandi                $ret .= ' width="'.$this->_xmlEntities($width).'"';
14543fd0b676Sandi
14553fd0b676Sandi            if(!is_null($height))
14563fd0b676Sandi                $ret .= ' height="'.$this->_xmlEntities($height).'"';
14573fd0b676Sandi
14583fd0b676Sandi            $ret .= ' />';
14593fd0b676Sandi
146017954bb5SAnika Henke        } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
14612a2a2ba2SAnika Henke            // first get the $title
146217954bb5SAnika Henke            $title = !is_null($title) ? $this->_xmlEntities($title) : false;
14632a2a2ba2SAnika Henke            if(!$render) {
146417954bb5SAnika Henke                // if the file is not supposed to be rendered
146517954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
146617954bb5SAnika Henke                return $title ? $title : $this->_xmlEntities(utf8_basename(noNS($src)));
14672a2a2ba2SAnika Henke            }
14682a2a2ba2SAnika Henke
14692a2a2ba2SAnika Henke            $att          = array();
14702a2a2ba2SAnika Henke            $att['class'] = "media$align";
147117954bb5SAnika Henke            if($title) {
147217954bb5SAnika Henke                $att['title'] = $title;
147317954bb5SAnika Henke            }
14742a2a2ba2SAnika Henke
147517954bb5SAnika Henke            if(media_supportedav($mime, 'video')) {
147617954bb5SAnika Henke                //add video
147779e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1478b44a5dceSAnika Henke            }
147917954bb5SAnika Henke            if(media_supportedav($mime, 'audio')) {
1480b44a5dceSAnika Henke                //add audio
1481b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
148217954bb5SAnika Henke            }
1483b44a5dceSAnika Henke
14843fd0b676Sandi        } elseif($mime == 'application/x-shockwave-flash') {
14851c882ba8SAndreas Gohr            if(!$render) {
14861c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
14871c882ba8SAndreas Gohr                // return the title of the flash
14881c882ba8SAndreas Gohr                if(!$title) {
14891c882ba8SAndreas Gohr                    // just show the sourcename
14903009a773SAndreas Gohr                    $title = utf8_basename(noNS($src));
14911c882ba8SAndreas Gohr                }
149207bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
14931c882ba8SAndreas Gohr            }
14941c882ba8SAndreas Gohr
149507bf32b2SAndreas Gohr            $att          = array();
149607bf32b2SAndreas Gohr            $att['class'] = "media$align";
149707bf32b2SAndreas Gohr            if($align == 'right') $att['align'] = 'right';
149807bf32b2SAndreas Gohr            if($align == 'left') $att['align'] = 'left';
14993dd5c225SAndreas Gohr            $ret .= html_flashobject(
15003dd5c225SAndreas Gohr                ml($src, array('cache' => $cache), true, '&'), $width, $height,
150107bf32b2SAndreas Gohr                array('quality' => 'high'),
150207bf32b2SAndreas Gohr                null,
150307bf32b2SAndreas Gohr                $att,
15043dd5c225SAndreas Gohr                $this->_xmlEntities($title)
15053dd5c225SAndreas Gohr            );
15060f428d7dSAndreas Gohr        } elseif($title) {
15073fd0b676Sandi            // well at least we have a title to display
15083fd0b676Sandi            $ret .= $this->_xmlEntities($title);
15093fd0b676Sandi        } else {
15105291ca3aSAndreas Gohr            // just show the sourcename
15113009a773SAndreas Gohr            $ret .= $this->_xmlEntities(utf8_basename(noNS($src)));
15123fd0b676Sandi        }
15133fd0b676Sandi
15143fd0b676Sandi        return $ret;
15153fd0b676Sandi    }
15163fd0b676Sandi
15173dd5c225SAndreas Gohr    /**
15183dd5c225SAndreas Gohr     * Escape string for output
15193dd5c225SAndreas Gohr     *
15203dd5c225SAndreas Gohr     * @param $string
15213dd5c225SAndreas Gohr     * @return string
15223dd5c225SAndreas Gohr     */
1523433bef32Sandi    function _xmlEntities($string) {
1524de117061Schris        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
15250cecf9d5Sandi    }
15260cecf9d5Sandi
15278a831f2bSAndreas Gohr    /**
15288a831f2bSAndreas Gohr     * Creates a linkid from a headline
1529c5a8fd96SAndreas Gohr     *
15303dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1531c5a8fd96SAndreas Gohr     * @param string  $title   The headline title
1532c5a8fd96SAndreas Gohr     * @param boolean $create  Create a new unique ID?
15333dd5c225SAndreas Gohr     * @return string
15348a831f2bSAndreas Gohr     */
1535c5a8fd96SAndreas Gohr    function _headerToLink($title, $create = false) {
1536c5a8fd96SAndreas Gohr        if($create) {
15374ceab83fSAndreas Gohr            return sectionID($title, $this->headers);
15384ceab83fSAndreas Gohr        } else {
1539443d207bSAndreas Gohr            $check = false;
1540443d207bSAndreas Gohr            return sectionID($title, $check);
1541c5a8fd96SAndreas Gohr        }
15420cecf9d5Sandi    }
15430cecf9d5Sandi
1544af587fa8Sandi    /**
15453fd0b676Sandi     * Construct a title and handle images in titles
15463fd0b676Sandi     *
15470b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15483dd5c225SAndreas Gohr     * @param string|array $title    either string title or media array
15493dd5c225SAndreas Gohr     * @param string       $default  default title if nothing else is found
15503dd5c225SAndreas Gohr     * @param bool         $isImage  will be set to true if it's a media file
15513dd5c225SAndreas Gohr     * @param null|string  $id       linked page id (used to extract title from first heading)
15523dd5c225SAndreas Gohr     * @param string       $linktype content|navigation
15533dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
15543fd0b676Sandi     */
15550ea51e63SMatt Perry    function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
155644881bd0Shenning.noren        $isImage = false;
155729657f9eSAndreas Gohr        if(is_array($title)) {
155829657f9eSAndreas Gohr            $isImage = true;
155929657f9eSAndreas Gohr            return $this->_imageTitle($title);
156029657f9eSAndreas Gohr        } elseif(is_null($title) || trim($title) == '') {
1561fe9ec250SChris Smith            if(useHeading($linktype) && $id) {
156267c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1563bb0a59d4Sjan                if($heading) {
1564433bef32Sandi                    return $this->_xmlEntities($heading);
1565bb0a59d4Sjan                }
1566bb0a59d4Sjan            }
1567433bef32Sandi            return $this->_xmlEntities($default);
156868c26e6dSMichael Klier        } else {
156968c26e6dSMichael Klier            return $this->_xmlEntities($title);
15700cecf9d5Sandi        }
15710cecf9d5Sandi    }
15720cecf9d5Sandi
15730cecf9d5Sandi    /**
15743dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
15753fd0b676Sandi     *
15763fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1577e0c26282SGerrit Uitslag     * @param array $img
15783dd5c225SAndreas Gohr     * @return string HTML img tag or similar
15790cecf9d5Sandi     */
1580433bef32Sandi    function _imageTitle($img) {
1581d9baf1a7SKazutaka Miyasaka        global $ID;
1582d9baf1a7SKazutaka Miyasaka
1583d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1584d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
15853dd5c225SAndreas Gohr        list($img['src']) = explode('#', $img['src'], 2);
1586d9baf1a7SKazutaka Miyasaka        if($img['type'] == 'internalmedia') {
1587d9baf1a7SKazutaka Miyasaka            resolve_mediaid(getNS($ID), $img['src'], $exists);
1588d9baf1a7SKazutaka Miyasaka        }
1589d9baf1a7SKazutaka Miyasaka
15903dd5c225SAndreas Gohr        return $this->_media(
15913dd5c225SAndreas Gohr            $img['src'],
15924826ab45Sandi            $img['title'],
15934826ab45Sandi            $img['align'],
15944826ab45Sandi            $img['width'],
15954826ab45Sandi            $img['height'],
15963dd5c225SAndreas Gohr            $img['cache']
15973dd5c225SAndreas Gohr        );
15980cecf9d5Sandi    }
1599b739ff0fSPierre Spring
1600b739ff0fSPierre Spring    /**
16013dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
16023dd5c225SAndreas Gohr     *
16033dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1604b739ff0fSPierre Spring     *
1605b739ff0fSPierre Spring     * @author   Pierre Spring <pierre.spring@liip.ch>
16063dd5c225SAndreas Gohr     * @param string $src       media ID
16073dd5c225SAndreas Gohr     * @param string $title     descriptive text
16083dd5c225SAndreas Gohr     * @param string $align     left|center|right
16093dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
16103dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
16113dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
16123dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
16133dd5c225SAndreas Gohr     * @return array associative array with link config
1614b739ff0fSPierre Spring     */
1615d91ab76fSMatt Perry    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1616b739ff0fSPierre Spring        global $conf;
1617b739ff0fSPierre Spring
1618b739ff0fSPierre Spring        $link           = array();
1619b739ff0fSPierre Spring        $link['class']  = 'media';
1620b739ff0fSPierre Spring        $link['style']  = '';
1621b739ff0fSPierre Spring        $link['pre']    = '';
1622b739ff0fSPierre Spring        $link['suf']    = '';
1623b739ff0fSPierre Spring        $link['more']   = '';
1624b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1625b739ff0fSPierre Spring        $link['title']  = $this->_xmlEntities($src);
1626b739ff0fSPierre Spring        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1627b739ff0fSPierre Spring
1628b739ff0fSPierre Spring        return $link;
1629b739ff0fSPierre Spring    }
163091459163SAnika Henke
16312a2a2ba2SAnika Henke    /**
16322a2a2ba2SAnika Henke     * Embed video(s) in HTML
16332a2a2ba2SAnika Henke     *
16342a2a2ba2SAnika Henke     * @author Anika Henke <anika@selfthinker.org>
16352a2a2ba2SAnika Henke     *
16362a2a2ba2SAnika Henke     * @param string $src         - ID of video to embed
16372a2a2ba2SAnika Henke     * @param int    $width       - width of the video in pixels
16382a2a2ba2SAnika Henke     * @param int    $height      - height of the video in pixels
16392a2a2ba2SAnika Henke     * @param array  $atts        - additional attributes for the <video> tag
1640f50634f0SAnika Henke     * @return string
16412a2a2ba2SAnika Henke     */
164279e53fe5SAnika Henke    function _video($src, $width, $height, $atts = null) {
16432a2a2ba2SAnika Henke        // prepare width and height
16442a2a2ba2SAnika Henke        if(is_null($atts)) $atts = array();
16452a2a2ba2SAnika Henke        $atts['width']  = (int) $width;
16462a2a2ba2SAnika Henke        $atts['height'] = (int) $height;
16472a2a2ba2SAnika Henke        if(!$atts['width']) $atts['width'] = 320;
16482a2a2ba2SAnika Henke        if(!$atts['height']) $atts['height'] = 240;
16492a2a2ba2SAnika Henke
1650410ee62aSAnika Henke        $posterUrl = '';
1651410ee62aSAnika Henke        $files = array();
1652410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1653410ee62aSAnika Henke
1654410ee62aSAnika Henke        if ($isExternal) {
1655410ee62aSAnika Henke            // take direct source for external files
1656702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1657410ee62aSAnika Henke            $files[$srcMime] = $src;
1658410ee62aSAnika Henke        } else {
16593d7a9e0aSAnika Henke            // prepare alternative formats
16603d7a9e0aSAnika Henke            $extensions   = array('webm', 'ogv', 'mp4');
1661410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
1662*59bc3b48SGerrit Uitslag            $poster       = media_alternativefiles($src, array('jpg', 'png'));
166399f943f6SAnika Henke            if(!empty($poster)) {
16642d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
166599f943f6SAnika Henke            }
1666410ee62aSAnika Henke        }
16672a2a2ba2SAnika Henke
1668f50634f0SAnika Henke        $out = '';
166979e53fe5SAnika Henke        // open video tag
1670f50634f0SAnika Henke        $out .= '<video '.buildAttributes($atts).' controls="controls"';
16713641199aSAnika Henke        if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1672f50634f0SAnika Henke        $out .= '>'.NL;
16733641199aSAnika Henke        $fallback = '';
167479e53fe5SAnika Henke
167579e53fe5SAnika Henke        // output source for each alternative video format
1676410ee62aSAnika Henke        foreach($files as $mime => $file) {
1677410ee62aSAnika Henke            if ($isExternal) {
1678410ee62aSAnika Henke                $url = $file;
1679410ee62aSAnika Henke                $linkType = 'externalmedia';
1680410ee62aSAnika Henke            } else {
16812d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1682410ee62aSAnika Henke                $linkType = 'internalmedia';
1683410ee62aSAnika Henke            }
168417954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
16853d7a9e0aSAnika Henke
1686f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
168779e53fe5SAnika Henke            // alternative content (just a link to the file)
1688410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
16893d7a9e0aSAnika Henke        }
16902a2a2ba2SAnika Henke
16912a2a2ba2SAnika Henke        // finish
16923641199aSAnika Henke        $out .= $fallback;
1693f50634f0SAnika Henke        $out .= '</video>'.NL;
1694f50634f0SAnika Henke        return $out;
16952a2a2ba2SAnika Henke    }
16962a2a2ba2SAnika Henke
1697b44a5dceSAnika Henke    /**
1698b44a5dceSAnika Henke     * Embed audio in HTML
1699b44a5dceSAnika Henke     *
1700b44a5dceSAnika Henke     * @author Anika Henke <anika@selfthinker.org>
1701b44a5dceSAnika Henke     *
1702b44a5dceSAnika Henke     * @param string $src       - ID of audio to embed
17036d4af72aSAnika Henke     * @param array  $atts      - additional attributes for the <audio> tag
1704f50634f0SAnika Henke     * @return string
1705b44a5dceSAnika Henke     */
1706*59bc3b48SGerrit Uitslag    function _audio($src, $atts = array()) {
1707702e97d3SAnika Henke        $files = array();
1708410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1709b44a5dceSAnika Henke
1710410ee62aSAnika Henke        if ($isExternal) {
1711410ee62aSAnika Henke            // take direct source for external files
1712702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1713410ee62aSAnika Henke            $files[$srcMime] = $src;
1714410ee62aSAnika Henke        } else {
1715b44a5dceSAnika Henke            // prepare alternative formats
1716b44a5dceSAnika Henke            $extensions   = array('ogg', 'mp3', 'wav');
1717410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
1718410ee62aSAnika Henke        }
1719b44a5dceSAnika Henke
1720f50634f0SAnika Henke        $out = '';
1721b44a5dceSAnika Henke        // open audio tag
1722f50634f0SAnika Henke        $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
17233641199aSAnika Henke        $fallback = '';
1724b44a5dceSAnika Henke
1725b44a5dceSAnika Henke        // output source for each alternative audio format
1726410ee62aSAnika Henke        foreach($files as $mime => $file) {
1727410ee62aSAnika Henke            if ($isExternal) {
1728410ee62aSAnika Henke                $url = $file;
1729410ee62aSAnika Henke                $linkType = 'externalmedia';
1730410ee62aSAnika Henke            } else {
17312d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1732410ee62aSAnika Henke                $linkType = 'internalmedia';
1733410ee62aSAnika Henke            }
173417954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
1735b44a5dceSAnika Henke
1736f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1737b44a5dceSAnika Henke            // alternative content (just a link to the file)
1738410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
1739b44a5dceSAnika Henke        }
1740b44a5dceSAnika Henke
1741b44a5dceSAnika Henke        // finish
17423641199aSAnika Henke        $out .= $fallback;
1743f50634f0SAnika Henke        $out .= '</audio>'.NL;
1744f50634f0SAnika Henke        return $out;
1745b44a5dceSAnika Henke    }
1746b44a5dceSAnika Henke
17473dd5c225SAndreas Gohr    #endregion
17480cecf9d5Sandi}
17490cecf9d5Sandi
1750e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1751