xref: /dokuwiki/inc/parser/xhtml.php (revision e3a24861f53db7293b2b17f05d5821871b85b2f6)
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();
314bde2196Slisps    var $date_at = '';    // link pages and media against this revision
32c5a8fd96SAndreas Gohr
333dd5c225SAndreas Gohr    /** @var int last section edit id, used by startSectionEdit */
343dd5c225SAndreas Gohr    protected $lastsecid = 0;
350cecf9d5Sandi
363dd5c225SAndreas Gohr    /** @var array the list of headers used to create unique link ids */
373dd5c225SAndreas Gohr    protected $headers = array();
383dd5c225SAndreas Gohr
3916ec3e37SAndreas Gohr    /** @var array a list of footnotes, list starts at 1! */
403dd5c225SAndreas Gohr    protected $footnotes = array();
417764a90aSandi
423dd5c225SAndreas Gohr    /** @var int current section level */
433dd5c225SAndreas Gohr    protected $lastlevel = 0;
443dd5c225SAndreas Gohr    /** @var array section node tracker */
453dd5c225SAndreas Gohr    protected $node = array(0, 0, 0, 0, 0);
463dd5c225SAndreas Gohr
473dd5c225SAndreas Gohr    /** @var string temporary $doc store */
483dd5c225SAndreas Gohr    protected $store = '';
493dd5c225SAndreas Gohr
503dd5c225SAndreas Gohr    /** @var array global counter, for table classes etc. */
513dd5c225SAndreas Gohr    protected $_counter = array(); //
523dd5c225SAndreas Gohr
533dd5c225SAndreas Gohr    /** @var int counts the code and file blocks, used to provide download links */
543dd5c225SAndreas Gohr    protected $_codeblock = 0;
553dd5c225SAndreas Gohr
563dd5c225SAndreas Gohr    /** @var array list of allowed URL schemes */
573dd5c225SAndreas Gohr    protected $schemes = null;
58b5742cedSPierre Spring
5990df9a4dSAdrian Lang    /**
6090df9a4dSAdrian Lang     * Register a new edit section range
6190df9a4dSAdrian Lang     *
6290df9a4dSAdrian Lang     * @param $type  string The section type identifier
6390df9a4dSAdrian Lang     * @param $title string The section title
6490df9a4dSAdrian Lang     * @param $start int    The byte position for the edit start
6590df9a4dSAdrian Lang     * @return string A marker class for the starting HTML element
6690df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
6790df9a4dSAdrian Lang     */
683f9e3215SAdrian Lang    public function startSectionEdit($start, $type, $title = null) {
69b04a190dSMichael Hamann        $this->sectionedits[] = array(++$this->lastsecid, $start, $type, $title);
70b04a190dSMichael Hamann        return 'sectionedit'.$this->lastsecid;
7190df9a4dSAdrian Lang    }
7290df9a4dSAdrian Lang
7390df9a4dSAdrian Lang    /**
7490df9a4dSAdrian Lang     * Finish an edit section range
7590df9a4dSAdrian Lang     *
76d9e36cbeSAdrian Lang     * @param $end     int The byte position for the edit end; null for the rest of
77c404cb3bSMatt Perry     *                 the page
7890df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
7990df9a4dSAdrian Lang     */
803f9e3215SAdrian Lang    public function finishSectionEdit($end = null) {
8190df9a4dSAdrian Lang        list($id, $start, $type, $title) = array_pop($this->sectionedits);
82d9e36cbeSAdrian Lang        if(!is_null($end) && $end <= $start) {
8300c13053SAdrian Lang            return;
8400c13053SAdrian Lang        }
8540868f2fSAdrian Lang        $this->doc .= "<!-- EDIT$id ".strtoupper($type).' ';
8640868f2fSAdrian Lang        if(!is_null($title)) {
8740868f2fSAdrian Lang            $this->doc .= '"'.str_replace('"', '', $title).'" ';
8840868f2fSAdrian Lang        }
89d9e36cbeSAdrian Lang        $this->doc .= "[$start-".(is_null($end) ? '' : $end).'] -->';
9090df9a4dSAdrian Lang    }
9190df9a4dSAdrian Lang
923dd5c225SAndreas Gohr    /**
933dd5c225SAndreas Gohr     * Returns the format produced by this renderer.
943dd5c225SAndreas Gohr     *
953dd5c225SAndreas Gohr     * @return string always 'xhtml'
963dd5c225SAndreas Gohr     */
975f70445dSAndreas Gohr    function getFormat() {
985f70445dSAndreas Gohr        return 'xhtml';
995f70445dSAndreas Gohr    }
1005f70445dSAndreas Gohr
1013dd5c225SAndreas Gohr    /**
1023dd5c225SAndreas Gohr     * Initialize the document
1033dd5c225SAndreas Gohr     */
1040cecf9d5Sandi    function document_start() {
105c5a8fd96SAndreas Gohr        //reset some internals
106c5a8fd96SAndreas Gohr        $this->toc     = array();
107c5a8fd96SAndreas Gohr        $this->headers = array();
1080cecf9d5Sandi    }
1090cecf9d5Sandi
1103dd5c225SAndreas Gohr    /**
1113dd5c225SAndreas Gohr     * Finalize the document
1123dd5c225SAndreas Gohr     */
1130cecf9d5Sandi    function document_end() {
11490df9a4dSAdrian Lang        // Finish open section edits.
11590df9a4dSAdrian Lang        while(count($this->sectionedits) > 0) {
11690df9a4dSAdrian Lang            if($this->sectionedits[count($this->sectionedits) - 1][1] <= 1) {
11790df9a4dSAdrian Lang                // If there is only one section, do not write a section edit
11890df9a4dSAdrian Lang                // marker.
11990df9a4dSAdrian Lang                array_pop($this->sectionedits);
12090df9a4dSAdrian Lang            } else {
121d9e36cbeSAdrian Lang                $this->finishSectionEdit();
12290df9a4dSAdrian Lang            }
12390df9a4dSAdrian Lang        }
12490df9a4dSAdrian Lang
1250cecf9d5Sandi        if(count($this->footnotes) > 0) {
126a2d649c4Sandi            $this->doc .= '<div class="footnotes">'.DOKU_LF;
127d74aace9Schris
12816ec3e37SAndreas Gohr            foreach($this->footnotes as $id => $footnote) {
129d74aace9Schris                // check its not a placeholder that indicates actual footnote text is elsewhere
130d74aace9Schris                if(substr($footnote, 0, 5) != "@@FNT") {
131d74aace9Schris
132d74aace9Schris                    // open the footnote and set the anchor and backlink
133d74aace9Schris                    $this->doc .= '<div class="fn">';
13416cc7ed7SAnika Henke                    $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
13529bfcd16SAndreas Gohr                    $this->doc .= $id.')</a></sup> '.DOKU_LF;
136d74aace9Schris
137d74aace9Schris                    // get any other footnotes that use the same markup
138d74aace9Schris                    $alt = array_keys($this->footnotes, "@@FNT$id");
139d74aace9Schris
140d74aace9Schris                    if(count($alt)) {
141d74aace9Schris                        foreach($alt as $ref) {
142d74aace9Schris                            // set anchor and backlink for the other footnotes
14316ec3e37SAndreas Gohr                            $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
14416ec3e37SAndreas Gohr                            $this->doc .= ($ref).')</a></sup> '.DOKU_LF;
145d74aace9Schris                        }
146d74aace9Schris                    }
147d74aace9Schris
148d74aace9Schris                    // add footnote markup and close this footnote
149a2d649c4Sandi                    $this->doc .= $footnote;
150d74aace9Schris                    $this->doc .= '</div>'.DOKU_LF;
151d74aace9Schris                }
1520cecf9d5Sandi            }
153a2d649c4Sandi            $this->doc .= '</div>'.DOKU_LF;
1540cecf9d5Sandi        }
155c5a8fd96SAndreas Gohr
156b8595a66SAndreas Gohr        // Prepare the TOC
157851f2e89SAnika Henke        global $conf;
158851f2e89SAnika Henke        if($this->info['toc'] && is_array($this->toc) && $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']) {
159b8595a66SAndreas Gohr            global $TOC;
160b8595a66SAndreas Gohr            $TOC = $this->toc;
1610cecf9d5Sandi        }
1623e55d035SAndreas Gohr
1633e55d035SAndreas Gohr        // make sure there are no empty paragraphs
16427918226Schris        $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
165e41c4da9SAndreas Gohr    }
1660cecf9d5Sandi
1673dd5c225SAndreas Gohr    /**
1683dd5c225SAndreas Gohr     * Add an item to the TOC
1693dd5c225SAndreas Gohr     *
1703dd5c225SAndreas Gohr     * @param string $id       the hash link
1713dd5c225SAndreas Gohr     * @param string $text     the text to display
1723dd5c225SAndreas Gohr     * @param int    $level    the nesting level
1733dd5c225SAndreas Gohr     */
174e7856beaSchris    function toc_additem($id, $text, $level) {
175af587fa8Sandi        global $conf;
176af587fa8Sandi
177c5a8fd96SAndreas Gohr        //handle TOC
178c5a8fd96SAndreas Gohr        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
1797d91652aSAndreas Gohr            $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
180c5a8fd96SAndreas Gohr        }
181e7856beaSchris    }
182e7856beaSchris
1833dd5c225SAndreas Gohr    /**
1843dd5c225SAndreas Gohr     * Render a heading
1853dd5c225SAndreas Gohr     *
1863dd5c225SAndreas Gohr     * @param string $text  the text to display
1873dd5c225SAndreas Gohr     * @param int    $level header level
1883dd5c225SAndreas Gohr     * @param int    $pos   byte position in the original source
1893dd5c225SAndreas Gohr     */
190e7856beaSchris    function header($text, $level, $pos) {
19190df9a4dSAdrian Lang        global $conf;
19290df9a4dSAdrian Lang
193bdd8111bSAndreas Gohr        if(!$text) return; //skip empty headlines
194e7856beaSchris
195e7856beaSchris        $hid = $this->_headerToLink($text, true);
196e7856beaSchris
197e7856beaSchris        //only add items within configured levels
198e7856beaSchris        $this->toc_additem($hid, $text, $level);
199c5a8fd96SAndreas Gohr
20091459163SAnika Henke        // adjust $node to reflect hierarchy of levels
20191459163SAnika Henke        $this->node[$level - 1]++;
20291459163SAnika Henke        if($level < $this->lastlevel) {
20391459163SAnika Henke            for($i = 0; $i < $this->lastlevel - $level; $i++) {
20491459163SAnika Henke                $this->node[$this->lastlevel - $i - 1] = 0;
20591459163SAnika Henke            }
20691459163SAnika Henke        }
20791459163SAnika Henke        $this->lastlevel = $level;
20891459163SAnika Henke
20990df9a4dSAdrian Lang        if($level <= $conf['maxseclevel'] &&
21090df9a4dSAdrian Lang            count($this->sectionedits) > 0 &&
2113dd5c225SAndreas Gohr            $this->sectionedits[count($this->sectionedits) - 1][2] === 'section'
2123dd5c225SAndreas Gohr        ) {
2136c1f778cSAdrian Lang            $this->finishSectionEdit($pos - 1);
21490df9a4dSAdrian Lang        }
21590df9a4dSAdrian Lang
216c5a8fd96SAndreas Gohr        // write the header
21790df9a4dSAdrian Lang        $this->doc .= DOKU_LF.'<h'.$level;
21890df9a4dSAdrian Lang        if($level <= $conf['maxseclevel']) {
21990df9a4dSAdrian Lang            $this->doc .= ' class="'.$this->startSectionEdit($pos, 'section', $text).'"';
22090df9a4dSAdrian Lang        }
22116cc7ed7SAnika Henke        $this->doc .= ' id="'.$hid.'">';
222a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
22316cc7ed7SAnika Henke        $this->doc .= "</h$level>".DOKU_LF;
2240cecf9d5Sandi    }
2250cecf9d5Sandi
2263dd5c225SAndreas Gohr    /**
2273dd5c225SAndreas Gohr     * Open a new section
2283dd5c225SAndreas Gohr     *
2293dd5c225SAndreas Gohr     * @param int $level section level (as determined by the previous header)
2303dd5c225SAndreas Gohr     */
2310cecf9d5Sandi    function section_open($level) {
2329864e7b1SAdrian Lang        $this->doc .= '<div class="level'.$level.'">'.DOKU_LF;
2330cecf9d5Sandi    }
2340cecf9d5Sandi
2353dd5c225SAndreas Gohr    /**
2363dd5c225SAndreas Gohr     * Close the current section
2373dd5c225SAndreas Gohr     */
2380cecf9d5Sandi    function section_close() {
239a2d649c4Sandi        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
2400cecf9d5Sandi    }
2410cecf9d5Sandi
2423dd5c225SAndreas Gohr    /**
2433dd5c225SAndreas Gohr     * Render plain text data
2443dd5c225SAndreas Gohr     *
2453dd5c225SAndreas Gohr     * @param $text
2463dd5c225SAndreas Gohr     */
2470cecf9d5Sandi    function cdata($text) {
248a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
2490cecf9d5Sandi    }
2500cecf9d5Sandi
2513dd5c225SAndreas Gohr    /**
2523dd5c225SAndreas Gohr     * Open a paragraph
2533dd5c225SAndreas Gohr     */
2540cecf9d5Sandi    function p_open() {
25559869a4bSAnika Henke        $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
2560cecf9d5Sandi    }
2570cecf9d5Sandi
2583dd5c225SAndreas Gohr    /**
2593dd5c225SAndreas Gohr     * Close a paragraph
2603dd5c225SAndreas Gohr     */
2610cecf9d5Sandi    function p_close() {
26259869a4bSAnika Henke        $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
2630cecf9d5Sandi    }
2640cecf9d5Sandi
2653dd5c225SAndreas Gohr    /**
2663dd5c225SAndreas Gohr     * Create a line break
2673dd5c225SAndreas Gohr     */
2680cecf9d5Sandi    function linebreak() {
269a2d649c4Sandi        $this->doc .= '<br/>'.DOKU_LF;
2700cecf9d5Sandi    }
2710cecf9d5Sandi
2723dd5c225SAndreas Gohr    /**
2733dd5c225SAndreas Gohr     * Create a horizontal line
2743dd5c225SAndreas Gohr     */
2750cecf9d5Sandi    function hr() {
2764beabca9SAnika Henke        $this->doc .= '<hr />'.DOKU_LF;
2770cecf9d5Sandi    }
2780cecf9d5Sandi
2793dd5c225SAndreas Gohr    /**
2803dd5c225SAndreas Gohr     * Start strong (bold) formatting
2813dd5c225SAndreas Gohr     */
2820cecf9d5Sandi    function strong_open() {
283a2d649c4Sandi        $this->doc .= '<strong>';
2840cecf9d5Sandi    }
2850cecf9d5Sandi
2863dd5c225SAndreas Gohr    /**
2873dd5c225SAndreas Gohr     * Stop strong (bold) formatting
2883dd5c225SAndreas Gohr     */
2890cecf9d5Sandi    function strong_close() {
290a2d649c4Sandi        $this->doc .= '</strong>';
2910cecf9d5Sandi    }
2920cecf9d5Sandi
2933dd5c225SAndreas Gohr    /**
2943dd5c225SAndreas Gohr     * Start emphasis (italics) formatting
2953dd5c225SAndreas Gohr     */
2960cecf9d5Sandi    function emphasis_open() {
297a2d649c4Sandi        $this->doc .= '<em>';
2980cecf9d5Sandi    }
2990cecf9d5Sandi
3003dd5c225SAndreas Gohr    /**
3013dd5c225SAndreas Gohr     * Stop emphasis (italics) formatting
3023dd5c225SAndreas Gohr     */
3030cecf9d5Sandi    function emphasis_close() {
304a2d649c4Sandi        $this->doc .= '</em>';
3050cecf9d5Sandi    }
3060cecf9d5Sandi
3073dd5c225SAndreas Gohr    /**
3083dd5c225SAndreas Gohr     * Start underline formatting
3093dd5c225SAndreas Gohr     */
3100cecf9d5Sandi    function underline_open() {
31102e51121SAnika Henke        $this->doc .= '<em class="u">';
3120cecf9d5Sandi    }
3130cecf9d5Sandi
3143dd5c225SAndreas Gohr    /**
3153dd5c225SAndreas Gohr     * Stop underline formatting
3163dd5c225SAndreas Gohr     */
3170cecf9d5Sandi    function underline_close() {
31802e51121SAnika Henke        $this->doc .= '</em>';
3190cecf9d5Sandi    }
3200cecf9d5Sandi
3213dd5c225SAndreas Gohr    /**
3223dd5c225SAndreas Gohr     * Start monospace formatting
3233dd5c225SAndreas Gohr     */
3240cecf9d5Sandi    function monospace_open() {
325a2d649c4Sandi        $this->doc .= '<code>';
3260cecf9d5Sandi    }
3270cecf9d5Sandi
3283dd5c225SAndreas Gohr    /**
3293dd5c225SAndreas Gohr     * Stop monospace formatting
3303dd5c225SAndreas Gohr     */
3310cecf9d5Sandi    function monospace_close() {
332a2d649c4Sandi        $this->doc .= '</code>';
3330cecf9d5Sandi    }
3340cecf9d5Sandi
3353dd5c225SAndreas Gohr    /**
3363dd5c225SAndreas Gohr     * Start a subscript
3373dd5c225SAndreas Gohr     */
3380cecf9d5Sandi    function subscript_open() {
339a2d649c4Sandi        $this->doc .= '<sub>';
3400cecf9d5Sandi    }
3410cecf9d5Sandi
3423dd5c225SAndreas Gohr    /**
3433dd5c225SAndreas Gohr     * Stop a subscript
3443dd5c225SAndreas Gohr     */
3450cecf9d5Sandi    function subscript_close() {
346a2d649c4Sandi        $this->doc .= '</sub>';
3470cecf9d5Sandi    }
3480cecf9d5Sandi
3493dd5c225SAndreas Gohr    /**
3503dd5c225SAndreas Gohr     * Start a superscript
3513dd5c225SAndreas Gohr     */
3520cecf9d5Sandi    function superscript_open() {
353a2d649c4Sandi        $this->doc .= '<sup>';
3540cecf9d5Sandi    }
3550cecf9d5Sandi
3563dd5c225SAndreas Gohr    /**
3573dd5c225SAndreas Gohr     * Stop a superscript
3583dd5c225SAndreas Gohr     */
3590cecf9d5Sandi    function superscript_close() {
360a2d649c4Sandi        $this->doc .= '</sup>';
3610cecf9d5Sandi    }
3620cecf9d5Sandi
3633dd5c225SAndreas Gohr    /**
3643dd5c225SAndreas Gohr     * Start deleted (strike-through) formatting
3653dd5c225SAndreas Gohr     */
3660cecf9d5Sandi    function deleted_open() {
367a2d649c4Sandi        $this->doc .= '<del>';
3680cecf9d5Sandi    }
3690cecf9d5Sandi
3703dd5c225SAndreas Gohr    /**
3713dd5c225SAndreas Gohr     * Stop deleted (strike-through) formatting
3723dd5c225SAndreas Gohr     */
3730cecf9d5Sandi    function deleted_close() {
374a2d649c4Sandi        $this->doc .= '</del>';
3750cecf9d5Sandi    }
3760cecf9d5Sandi
3773fd0b676Sandi    /**
3783fd0b676Sandi     * Callback for footnote start syntax
3793fd0b676Sandi     *
3803fd0b676Sandi     * All following content will go to the footnote instead of
381d74aace9Schris     * the document. To achieve this the previous rendered content
3823fd0b676Sandi     * is moved to $store and $doc is cleared
3833fd0b676Sandi     *
3843fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
3853fd0b676Sandi     */
3860cecf9d5Sandi    function footnote_open() {
3877764a90aSandi
3887764a90aSandi        // move current content to store and record footnote
3897764a90aSandi        $this->store = $this->doc;
3907764a90aSandi        $this->doc   = '';
3910cecf9d5Sandi    }
3920cecf9d5Sandi
3933fd0b676Sandi    /**
3943fd0b676Sandi     * Callback for footnote end syntax
3953fd0b676Sandi     *
3963fd0b676Sandi     * All rendered content is moved to the $footnotes array and the old
3973fd0b676Sandi     * content is restored from $store again
3983fd0b676Sandi     *
3993fd0b676Sandi     * @author Andreas Gohr
4003fd0b676Sandi     */
4010cecf9d5Sandi    function footnote_close() {
40216ec3e37SAndreas Gohr        /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
40316ec3e37SAndreas Gohr        static $fnid = 0;
40416ec3e37SAndreas Gohr        // assign new footnote id (we start at 1)
40516ec3e37SAndreas Gohr        $fnid++;
4067764a90aSandi
407d74aace9Schris        // recover footnote into the stack and restore old content
408d74aace9Schris        $footnote    = $this->doc;
4097764a90aSandi        $this->doc   = $this->store;
4107764a90aSandi        $this->store = '';
411d74aace9Schris
412d74aace9Schris        // check to see if this footnote has been seen before
413d74aace9Schris        $i = array_search($footnote, $this->footnotes);
414d74aace9Schris
415d74aace9Schris        if($i === false) {
416d74aace9Schris            // its a new footnote, add it to the $footnotes array
41716ec3e37SAndreas Gohr            $this->footnotes[$fnid] = $footnote;
418d74aace9Schris        } else {
41916ec3e37SAndreas Gohr            // seen this one before, save a placeholder
42016ec3e37SAndreas Gohr            $this->footnotes[$fnid] = "@@FNT".($i);
421d74aace9Schris        }
422d74aace9Schris
4236b379cbfSAndreas Gohr        // output the footnote reference and link
42416ec3e37SAndreas Gohr        $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
4250cecf9d5Sandi    }
4260cecf9d5Sandi
4273dd5c225SAndreas Gohr    /**
4283dd5c225SAndreas Gohr     * Open an unordered list
4293dd5c225SAndreas Gohr     */
4300cecf9d5Sandi    function listu_open() {
431a2d649c4Sandi        $this->doc .= '<ul>'.DOKU_LF;
4320cecf9d5Sandi    }
4330cecf9d5Sandi
4343dd5c225SAndreas Gohr    /**
4353dd5c225SAndreas Gohr     * Close an unordered list
4363dd5c225SAndreas Gohr     */
4370cecf9d5Sandi    function listu_close() {
438a2d649c4Sandi        $this->doc .= '</ul>'.DOKU_LF;
4390cecf9d5Sandi    }
4400cecf9d5Sandi
4413dd5c225SAndreas Gohr    /**
4423dd5c225SAndreas Gohr     * Open an ordered list
4433dd5c225SAndreas Gohr     */
4440cecf9d5Sandi    function listo_open() {
445a2d649c4Sandi        $this->doc .= '<ol>'.DOKU_LF;
4460cecf9d5Sandi    }
4470cecf9d5Sandi
4483dd5c225SAndreas Gohr    /**
4493dd5c225SAndreas Gohr     * Close an ordered list
4503dd5c225SAndreas Gohr     */
4510cecf9d5Sandi    function listo_close() {
452a2d649c4Sandi        $this->doc .= '</ol>'.DOKU_LF;
4530cecf9d5Sandi    }
4540cecf9d5Sandi
4553dd5c225SAndreas Gohr    /**
4563dd5c225SAndreas Gohr     * Open a list item
4573dd5c225SAndreas Gohr     *
4583dd5c225SAndreas Gohr     * @param int $level the nesting level
459*e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
4603dd5c225SAndreas Gohr     */
461*e3a24861SChristopher Smith    function listitem_open($level, $node=false) {
462*e3a24861SChristopher Smith        $branching = $node ? ' node' : '';
463*e3a24861SChristopher Smith        $this->doc .= '<li class="level'.$level.$branching.'">';
4640cecf9d5Sandi    }
4650cecf9d5Sandi
4663dd5c225SAndreas Gohr    /**
4673dd5c225SAndreas Gohr     * Close a list item
4683dd5c225SAndreas Gohr     */
4690cecf9d5Sandi    function listitem_close() {
470a2d649c4Sandi        $this->doc .= '</li>'.DOKU_LF;
4710cecf9d5Sandi    }
4720cecf9d5Sandi
4733dd5c225SAndreas Gohr    /**
4743dd5c225SAndreas Gohr     * Start the content of a list item
4753dd5c225SAndreas Gohr     */
4760cecf9d5Sandi    function listcontent_open() {
47790db23d7Schris        $this->doc .= '<div class="li">';
4780cecf9d5Sandi    }
4790cecf9d5Sandi
4803dd5c225SAndreas Gohr    /**
4813dd5c225SAndreas Gohr     * Stop the content of a list item
4823dd5c225SAndreas Gohr     */
4830cecf9d5Sandi    function listcontent_close() {
48459869a4bSAnika Henke        $this->doc .= '</div>'.DOKU_LF;
4850cecf9d5Sandi    }
4860cecf9d5Sandi
4873dd5c225SAndreas Gohr    /**
4883dd5c225SAndreas Gohr     * Output unformatted $text
4893dd5c225SAndreas Gohr     *
4903dd5c225SAndreas Gohr     * Defaults to $this->cdata()
4913dd5c225SAndreas Gohr     *
4923dd5c225SAndreas Gohr     * @param string $text
4933dd5c225SAndreas Gohr     */
4940cecf9d5Sandi    function unformatted($text) {
495a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
4960cecf9d5Sandi    }
4970cecf9d5Sandi
4980cecf9d5Sandi    /**
4993fd0b676Sandi     * Execute PHP code if allowed
5003fd0b676Sandi     *
501d9764001SMichael Hamann     * @param  string $text      PHP code that is either executed or printed
5025d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['phpok'] is okff
5035d568b99SChris Smith     *
5043fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5050cecf9d5Sandi     */
5065d568b99SChris Smith    function php($text, $wrapper = 'code') {
50735a56260SChris Smith        global $conf;
50835a56260SChris Smith
509d86d5af0SChris Smith        if($conf['phpok']) {
510bad0b545Sandi            ob_start();
5114de671bcSandi            eval($text);
5123fd0b676Sandi            $this->doc .= ob_get_contents();
513bad0b545Sandi            ob_end_clean();
514d86d5af0SChris Smith        } else {
5155d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'php', $wrapper);
516d86d5af0SChris Smith        }
5170cecf9d5Sandi    }
5180cecf9d5Sandi
5193dd5c225SAndreas Gohr    /**
5203dd5c225SAndreas Gohr     * Output block level PHP code
5213dd5c225SAndreas Gohr     *
5223dd5c225SAndreas Gohr     * If $conf['phpok'] is true this should evaluate the given code and append the result
5233dd5c225SAndreas Gohr     * to $doc
5243dd5c225SAndreas Gohr     *
5253dd5c225SAndreas Gohr     * @param string $text The PHP code
5263dd5c225SAndreas Gohr     */
52707f89c3cSAnika Henke    function phpblock($text) {
5285d568b99SChris Smith        $this->php($text, 'pre');
52907f89c3cSAnika Henke    }
53007f89c3cSAnika Henke
5310cecf9d5Sandi    /**
5323fd0b676Sandi     * Insert HTML if allowed
5333fd0b676Sandi     *
534d9764001SMichael Hamann     * @param  string $text      html text
5355d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['htmlok'] is okff
5365d568b99SChris Smith     *
5373fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5380cecf9d5Sandi     */
5395d568b99SChris Smith    function html($text, $wrapper = 'code') {
54035a56260SChris Smith        global $conf;
54135a56260SChris Smith
542d86d5af0SChris Smith        if($conf['htmlok']) {
543a2d649c4Sandi            $this->doc .= $text;
544d86d5af0SChris Smith        } else {
5455d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'html4strict', $wrapper);
546d86d5af0SChris Smith        }
5474de671bcSandi    }
5480cecf9d5Sandi
5493dd5c225SAndreas Gohr    /**
5503dd5c225SAndreas Gohr     * Output raw block-level HTML
5513dd5c225SAndreas Gohr     *
5523dd5c225SAndreas Gohr     * If $conf['htmlok'] is true this should add the code as is to $doc
5533dd5c225SAndreas Gohr     *
5543dd5c225SAndreas Gohr     * @param string $text The HTML
5553dd5c225SAndreas Gohr     */
55607f89c3cSAnika Henke    function htmlblock($text) {
5575d568b99SChris Smith        $this->html($text, 'pre');
55807f89c3cSAnika Henke    }
55907f89c3cSAnika Henke
5603dd5c225SAndreas Gohr    /**
5613dd5c225SAndreas Gohr     * Start a block quote
5623dd5c225SAndreas Gohr     */
5630cecf9d5Sandi    function quote_open() {
56496331712SAnika Henke        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
5650cecf9d5Sandi    }
5660cecf9d5Sandi
5673dd5c225SAndreas Gohr    /**
5683dd5c225SAndreas Gohr     * Stop a block quote
5693dd5c225SAndreas Gohr     */
5700cecf9d5Sandi    function quote_close() {
57196331712SAnika Henke        $this->doc .= '</div></blockquote>'.DOKU_LF;
5720cecf9d5Sandi    }
5730cecf9d5Sandi
5743dd5c225SAndreas Gohr    /**
5753dd5c225SAndreas Gohr     * Output preformatted text
5763dd5c225SAndreas Gohr     *
5773dd5c225SAndreas Gohr     * @param string $text
5783dd5c225SAndreas Gohr     */
5793d491f75SAndreas Gohr    function preformatted($text) {
580c9250713SAnika Henke        $this->doc .= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF;
5813d491f75SAndreas Gohr    }
5823d491f75SAndreas Gohr
5833dd5c225SAndreas Gohr    /**
5843dd5c225SAndreas Gohr     * Display text as file content, optionally syntax highlighted
5853dd5c225SAndreas Gohr     *
5863dd5c225SAndreas Gohr     * @param string $text     text to show
5873dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5883dd5c225SAndreas Gohr     * @param string $filename file path label
5893dd5c225SAndreas Gohr     */
5903d491f75SAndreas Gohr    function file($text, $language = null, $filename = null) {
5913d491f75SAndreas Gohr        $this->_highlight('file', $text, $language, $filename);
5923d491f75SAndreas Gohr    }
5933d491f75SAndreas Gohr
5943dd5c225SAndreas Gohr    /**
5953dd5c225SAndreas Gohr     * Display text as code content, optionally syntax highlighted
5963dd5c225SAndreas Gohr     *
5973dd5c225SAndreas Gohr     * @param string $text     text to show
5983dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5993dd5c225SAndreas Gohr     * @param string $filename file path label
6003dd5c225SAndreas Gohr     */
6013d491f75SAndreas Gohr    function code($text, $language = null, $filename = null) {
6023d491f75SAndreas Gohr        $this->_highlight('code', $text, $language, $filename);
6033d491f75SAndreas Gohr    }
6043d491f75SAndreas Gohr
6050cecf9d5Sandi    /**
6063d491f75SAndreas Gohr     * Use GeSHi to highlight language syntax in code and file blocks
6073fd0b676Sandi     *
6083fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
6093dd5c225SAndreas Gohr     * @param string $type     code|file
6103dd5c225SAndreas Gohr     * @param string $text     text to show
6113dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6123dd5c225SAndreas Gohr     * @param string $filename file path label
6130cecf9d5Sandi     */
6143d491f75SAndreas Gohr    function _highlight($type, $text, $language = null, $filename = null) {
6153d491f75SAndreas Gohr        global $ID;
6163d491f75SAndreas Gohr        global $lang;
6173d491f75SAndreas Gohr
6183d491f75SAndreas Gohr        if($filename) {
619190c56e8SAndreas Gohr            // add icon
62027bf7924STom N Harris            list($ext) = mimetype($filename, false);
621190c56e8SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
622190c56e8SAndreas Gohr            $class = 'mediafile mf_'.$class;
623190c56e8SAndreas Gohr
6243d491f75SAndreas Gohr            $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
625190c56e8SAndreas Gohr            $this->doc .= '<dt><a href="'.exportlink($ID, 'code', array('codeblock' => $this->_codeblock)).'" title="'.$lang['download'].'" class="'.$class.'">';
6263d491f75SAndreas Gohr            $this->doc .= hsc($filename);
6273d491f75SAndreas Gohr            $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
6283d491f75SAndreas Gohr        }
6290cecf9d5Sandi
630d43aac1cSGina Haeussge        if($text{0} == "\n") {
631d43aac1cSGina Haeussge            $text = substr($text, 1);
632d43aac1cSGina Haeussge        }
633d43aac1cSGina Haeussge        if(substr($text, -1) == "\n") {
634d43aac1cSGina Haeussge            $text = substr($text, 0, -1);
635d43aac1cSGina Haeussge        }
636d43aac1cSGina Haeussge
6370cecf9d5Sandi        if(is_null($language)) {
6383d491f75SAndreas Gohr            $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
6390cecf9d5Sandi        } else {
6403d491f75SAndreas Gohr            $class = 'code'; //we always need the code class to make the syntax highlighting apply
6413d491f75SAndreas Gohr            if($type != 'code') $class .= ' '.$type;
6423d491f75SAndreas Gohr
6433d491f75SAndreas Gohr            $this->doc .= "<pre class=\"$class $language\">".p_xhtml_cached_geshi($text, $language, '').'</pre>'.DOKU_LF;
6440cecf9d5Sandi        }
6453d491f75SAndreas Gohr
6463d491f75SAndreas Gohr        if($filename) {
6473d491f75SAndreas Gohr            $this->doc .= '</dd></dl>'.DOKU_LF;
6483d491f75SAndreas Gohr        }
6493d491f75SAndreas Gohr
6503d491f75SAndreas Gohr        $this->_codeblock++;
6510cecf9d5Sandi    }
6520cecf9d5Sandi
6533dd5c225SAndreas Gohr    /**
6543dd5c225SAndreas Gohr     * Format an acronym
6553dd5c225SAndreas Gohr     *
6563dd5c225SAndreas Gohr     * Uses $this->acronyms
6573dd5c225SAndreas Gohr     *
6583dd5c225SAndreas Gohr     * @param string $acronym
6593dd5c225SAndreas Gohr     */
6600cecf9d5Sandi    function acronym($acronym) {
6610cecf9d5Sandi
6620cecf9d5Sandi        if(array_key_exists($acronym, $this->acronyms)) {
6630cecf9d5Sandi
664433bef32Sandi            $title = $this->_xmlEntities($this->acronyms[$acronym]);
6650cecf9d5Sandi
666940db3a3SAnika Henke            $this->doc .= '<abbr title="'.$title
667940db3a3SAnika Henke                .'">'.$this->_xmlEntities($acronym).'</abbr>';
6680cecf9d5Sandi
6690cecf9d5Sandi        } else {
670a2d649c4Sandi            $this->doc .= $this->_xmlEntities($acronym);
6710cecf9d5Sandi        }
6720cecf9d5Sandi    }
6730cecf9d5Sandi
6743dd5c225SAndreas Gohr    /**
6753dd5c225SAndreas Gohr     * Format a smiley
6763dd5c225SAndreas Gohr     *
6773dd5c225SAndreas Gohr     * Uses $this->smiley
6783dd5c225SAndreas Gohr     *
6793dd5c225SAndreas Gohr     * @param string $smiley
6803dd5c225SAndreas Gohr     */
6810cecf9d5Sandi    function smiley($smiley) {
6820cecf9d5Sandi        if(array_key_exists($smiley, $this->smileys)) {
683f62ea8a1Sandi            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
6848e38227fSAnika Henke                '" class="icon" alt="'.
685433bef32Sandi                $this->_xmlEntities($smiley).'" />';
6860cecf9d5Sandi        } else {
687a2d649c4Sandi            $this->doc .= $this->_xmlEntities($smiley);
6880cecf9d5Sandi        }
6890cecf9d5Sandi    }
6900cecf9d5Sandi
6913dd5c225SAndreas Gohr    /**
6923dd5c225SAndreas Gohr     * Format an entity
6933dd5c225SAndreas Gohr     *
6943dd5c225SAndreas Gohr     * Entities are basically small text replacements
6953dd5c225SAndreas Gohr     *
6963dd5c225SAndreas Gohr     * Uses $this->entities
6973dd5c225SAndreas Gohr     *
6983dd5c225SAndreas Gohr     * @param string $entity
6994de671bcSandi     */
7000cecf9d5Sandi    function entity($entity) {
7010cecf9d5Sandi        if(array_key_exists($entity, $this->entities)) {
702a2d649c4Sandi            $this->doc .= $this->entities[$entity];
7030cecf9d5Sandi        } else {
704a2d649c4Sandi            $this->doc .= $this->_xmlEntities($entity);
7050cecf9d5Sandi        }
7060cecf9d5Sandi    }
7070cecf9d5Sandi
7083dd5c225SAndreas Gohr    /**
7093dd5c225SAndreas Gohr     * Typographically format a multiply sign
7103dd5c225SAndreas Gohr     *
7113dd5c225SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
7123dd5c225SAndreas Gohr     *
7133dd5c225SAndreas Gohr     * @param string|int $x first value
7143dd5c225SAndreas Gohr     * @param string|int $y second value
7153dd5c225SAndreas Gohr     */
7160cecf9d5Sandi    function multiplyentity($x, $y) {
717a2d649c4Sandi        $this->doc .= "$x&times;$y";
7180cecf9d5Sandi    }
7190cecf9d5Sandi
7203dd5c225SAndreas Gohr    /**
7213dd5c225SAndreas Gohr     * Render an opening single quote char (language specific)
7223dd5c225SAndreas Gohr     */
7230cecf9d5Sandi    function singlequoteopening() {
72471b40da2SAnika Henke        global $lang;
72571b40da2SAnika Henke        $this->doc .= $lang['singlequoteopening'];
7260cecf9d5Sandi    }
7270cecf9d5Sandi
7283dd5c225SAndreas Gohr    /**
7293dd5c225SAndreas Gohr     * Render a closing single quote char (language specific)
7303dd5c225SAndreas Gohr     */
7310cecf9d5Sandi    function singlequoteclosing() {
73271b40da2SAnika Henke        global $lang;
73371b40da2SAnika Henke        $this->doc .= $lang['singlequoteclosing'];
7340cecf9d5Sandi    }
7350cecf9d5Sandi
7363dd5c225SAndreas Gohr    /**
7373dd5c225SAndreas Gohr     * Render an apostrophe char (language specific)
7383dd5c225SAndreas Gohr     */
73957d757d1SAndreas Gohr    function apostrophe() {
74057d757d1SAndreas Gohr        global $lang;
741a8bd192aSAndreas Gohr        $this->doc .= $lang['apostrophe'];
74257d757d1SAndreas Gohr    }
74357d757d1SAndreas Gohr
7443dd5c225SAndreas Gohr    /**
7453dd5c225SAndreas Gohr     * Render an opening double quote char (language specific)
7463dd5c225SAndreas Gohr     */
7470cecf9d5Sandi    function doublequoteopening() {
74871b40da2SAnika Henke        global $lang;
74971b40da2SAnika Henke        $this->doc .= $lang['doublequoteopening'];
7500cecf9d5Sandi    }
7510cecf9d5Sandi
7523dd5c225SAndreas Gohr    /**
7533dd5c225SAndreas Gohr     * Render an closinging double quote char (language specific)
7543dd5c225SAndreas Gohr     */
7550cecf9d5Sandi    function doublequoteclosing() {
75671b40da2SAnika Henke        global $lang;
75771b40da2SAnika Henke        $this->doc .= $lang['doublequoteclosing'];
7580cecf9d5Sandi    }
7590cecf9d5Sandi
7600cecf9d5Sandi    /**
7613dd5c225SAndreas Gohr     * Render a CamelCase link
7623dd5c225SAndreas Gohr     *
7633dd5c225SAndreas Gohr     * @param string $link The link name
7643dd5c225SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
7650cecf9d5Sandi     */
7660cecf9d5Sandi    function camelcaselink($link) {
76711d0aa47Sandi        $this->internallink($link, $link);
7680cecf9d5Sandi    }
7690cecf9d5Sandi
7703dd5c225SAndreas Gohr    /**
7713dd5c225SAndreas Gohr     * Render a page local link
7723dd5c225SAndreas Gohr     *
7733dd5c225SAndreas Gohr     * @param string $hash hash link identifier
7743dd5c225SAndreas Gohr     * @param string $name name for the link
7753dd5c225SAndreas Gohr     */
7760ea51e63SMatt Perry    function locallink($hash, $name = null) {
7770b7c14c2Sandi        global $ID;
7780b7c14c2Sandi        $name  = $this->_getLinkTitle($name, $hash, $isImage);
7790b7c14c2Sandi        $hash  = $this->_headerToLink($hash);
780e260f93bSAnika Henke        $title = $ID.' ↵';
7810b7c14c2Sandi        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
7820b7c14c2Sandi        $this->doc .= $name;
7830b7c14c2Sandi        $this->doc .= '</a>';
7840b7c14c2Sandi    }
7850b7c14c2Sandi
786cffcc403Sandi    /**
7873fd0b676Sandi     * Render an internal Wiki Link
7883fd0b676Sandi     *
789fe9ec250SChris Smith     * $search,$returnonly & $linktype are not for the renderer but are used
790cffcc403Sandi     * elsewhere - no need to implement them in other renderers
7913fd0b676Sandi     *
7923dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
793f23eef27SGerrit Uitslag     * @param string      $id         pageid
794f23eef27SGerrit Uitslag     * @param string|null $name       link name
795f23eef27SGerrit Uitslag     * @param string|null $search     adds search url param
796f23eef27SGerrit Uitslag     * @param bool        $returnonly whether to return html or write to doc attribute
797f23eef27SGerrit Uitslag     * @param string      $linktype   type to set use of headings
798f23eef27SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
799cffcc403Sandi     */
8000ea51e63SMatt Perry    function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
801ba11bd29Sandi        global $conf;
80237e34a5eSandi        global $ID;
803c4dda6afSAnika Henke        global $INFO;
80444653a53SAdrian Lang
8053d5e07d9SAdrian Lang        $params = '';
8063d5e07d9SAdrian Lang        $parts  = explode('?', $id, 2);
8073d5e07d9SAdrian Lang        if(count($parts) === 2) {
8083d5e07d9SAdrian Lang            $id     = $parts[0];
8093d5e07d9SAdrian Lang            $params = $parts[1];
81044653a53SAdrian Lang        }
81144653a53SAdrian Lang
812fda14ffcSIzidor Matušov        // For empty $id we need to know the current $ID
813fda14ffcSIzidor Matušov        // We need this check because _simpleTitle needs
814fda14ffcSIzidor Matušov        // correct $id and resolve_pageid() use cleanID($id)
815fda14ffcSIzidor Matušov        // (some things could be lost)
816fda14ffcSIzidor Matušov        if($id === '') {
817fda14ffcSIzidor Matušov            $id = $ID;
818fda14ffcSIzidor Matušov        }
819fda14ffcSIzidor Matušov
8200339c872Sjan        // default name is based on $id as given
8210339c872Sjan        $default = $this->_simpleTitle($id);
822ad32e47eSAndreas Gohr
8230339c872Sjan        // now first resolve and clean up the $id
82490bee600Slisps        resolve_pageid(getNS($ID), $id, $exists, $this->date_at, true);
825fda14ffcSIzidor Matušov
826fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
8270e1c636eSandi        if(!$isImage) {
8280e1c636eSandi            if($exists) {
829ba11bd29Sandi                $class = 'wikilink1';
8300cecf9d5Sandi            } else {
831ba11bd29Sandi                $class       = 'wikilink2';
83244a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
8330cecf9d5Sandi            }
8340cecf9d5Sandi        } else {
835ba11bd29Sandi            $class = 'media';
8360cecf9d5Sandi        }
8370cecf9d5Sandi
838a1685bedSandi        //keep hash anchor
8396d2af55dSChristopher Smith        @list($id, $hash) = explode('#', $id, 2);
840943dedc6SAndreas Gohr        if(!empty($hash)) $hash = $this->_headerToLink($hash);
841a1685bedSandi
842ba11bd29Sandi        //prepare for formating
843ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
844ba11bd29Sandi        $link['style']  = '';
845ba11bd29Sandi        $link['pre']    = '';
846ba11bd29Sandi        $link['suf']    = '';
84740eb54bbSjan        // highlight link to current page
848c4dda6afSAnika Henke        if($id == $INFO['id']) {
84992795d04Sandi            $link['pre'] = '<span class="curid">';
85092795d04Sandi            $link['suf'] = '</span>';
85140eb54bbSjan        }
8525e163278SAndreas Gohr        $link['more']   = '';
853ba11bd29Sandi        $link['class']  = $class;
8545c2eed9aSlisps        if($this->date_at) {
8555c2eed9aSlisps            $params['at'] = $this->date_at;
8565c2eed9aSlisps        }
85744653a53SAdrian Lang        $link['url']    = wl($id, $params);
858ba11bd29Sandi        $link['name']   = $name;
859ba11bd29Sandi        $link['title']  = $id;
860723d78dbSandi        //add search string
861723d78dbSandi        if($search) {
862546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
863546d3a99SAndreas Gohr            if(is_array($search)) {
864546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
865546d3a99SAndreas Gohr                $link['url'] .= 's[]='.join('&amp;s[]=', $search);
866546d3a99SAndreas Gohr            } else {
867546d3a99SAndreas Gohr                $link['url'] .= 's='.rawurlencode($search);
868546d3a99SAndreas Gohr            }
869723d78dbSandi        }
870723d78dbSandi
871a1685bedSandi        //keep hash
872a1685bedSandi        if($hash) $link['url'] .= '#'.$hash;
873a1685bedSandi
874ba11bd29Sandi        //output formatted
875cffcc403Sandi        if($returnonly) {
876cffcc403Sandi            return $this->_formatLink($link);
877cffcc403Sandi        } else {
878a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
8790cecf9d5Sandi        }
880cffcc403Sandi    }
8810cecf9d5Sandi
8823dd5c225SAndreas Gohr    /**
8833dd5c225SAndreas Gohr     * Render an external link
8843dd5c225SAndreas Gohr     *
8853dd5c225SAndreas Gohr     * @param string       $url  full URL with scheme
8863dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
8873dd5c225SAndreas Gohr     */
8880ea51e63SMatt Perry    function externallink($url, $name = null) {
889b625487dSandi        global $conf;
8900cecf9d5Sandi
891433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
8926f0c5dbfSandi
893b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
894b52b1596SAndreas Gohr        if(is_null($this->schemes)) $this->schemes = getSchemes();
895b52b1596SAndreas Gohr        list($scheme) = explode('://', $url);
896b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
897b52b1596SAndreas Gohr        if(!in_array($scheme, $this->schemes)) $url = '';
898b52b1596SAndreas Gohr
899b52b1596SAndreas Gohr        // is there still an URL?
900b52b1596SAndreas Gohr        if(!$url) {
901b52b1596SAndreas Gohr            $this->doc .= $name;
902b52b1596SAndreas Gohr            return;
903b52b1596SAndreas Gohr        }
904b52b1596SAndreas Gohr
905b52b1596SAndreas Gohr        // set class
9060cecf9d5Sandi        if(!$isImage) {
907b625487dSandi            $class = 'urlextern';
9080cecf9d5Sandi        } else {
909b625487dSandi            $class = 'media';
9100cecf9d5Sandi        }
9110cecf9d5Sandi
912b625487dSandi        //prepare for formating
913b625487dSandi        $link['target'] = $conf['target']['extern'];
914b625487dSandi        $link['style']  = '';
915b625487dSandi        $link['pre']    = '';
916b625487dSandi        $link['suf']    = '';
9175e163278SAndreas Gohr        $link['more']   = '';
918b625487dSandi        $link['class']  = $class;
919b625487dSandi        $link['url']    = $url;
920e1c10e4dSchris
921b625487dSandi        $link['name']  = $name;
922433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
923b625487dSandi        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
9240cecf9d5Sandi
925b625487dSandi        //output formatted
926a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9270cecf9d5Sandi    }
9280cecf9d5Sandi
9290cecf9d5Sandi    /**
9303dd5c225SAndreas Gohr     * Render an interwiki link
9313dd5c225SAndreas Gohr     *
9323dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
9333dd5c225SAndreas Gohr     *
9343dd5c225SAndreas Gohr     * @param string       $match     original link - probably not much use
9353dd5c225SAndreas Gohr     * @param string|array $name      name for the link, array for media file
9363dd5c225SAndreas Gohr     * @param string       $wikiName  indentifier (shortcut) for the remote wiki
9373dd5c225SAndreas Gohr     * @param string       $wikiUri   the fragment parsed from the original link
9380cecf9d5Sandi     */
9390ea51e63SMatt Perry    function interwikilink($match, $name = null, $wikiName, $wikiUri) {
940b625487dSandi        global $conf;
9410cecf9d5Sandi
94297a3e4e3Sandi        $link           = array();
94397a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
94497a3e4e3Sandi        $link['pre']    = '';
94597a3e4e3Sandi        $link['suf']    = '';
9465e163278SAndreas Gohr        $link['more']   = '';
947433bef32Sandi        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
9480cecf9d5Sandi
94997a3e4e3Sandi        //get interwiki URL
9506496c33fSGerrit Uitslag        $exists = null;
9516496c33fSGerrit Uitslag        $url    = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
9520cecf9d5Sandi
95397a3e4e3Sandi        if(!$isImage) {
9549d2ddea4SAndreas Gohr            $class         = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
9559d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
9561c2d1019SAndreas Gohr        } else {
9571c2d1019SAndreas Gohr            $link['class'] = 'media';
95897a3e4e3Sandi        }
9590cecf9d5Sandi
96097a3e4e3Sandi        //do we stay at the same server? Use local target
9612345e871SGerrit Uitslag        if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
96297a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
96397a3e4e3Sandi        }
9646496c33fSGerrit Uitslag        if($exists !== null && !$isImage) {
9656496c33fSGerrit Uitslag            if($exists) {
9666496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
9676496c33fSGerrit Uitslag            } else {
9686496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
9696496c33fSGerrit Uitslag                $link['rel'] = 'nofollow';
9706496c33fSGerrit Uitslag            }
9716496c33fSGerrit Uitslag        }
9720cecf9d5Sandi
97397a3e4e3Sandi        $link['url']   = $url;
97497a3e4e3Sandi        $link['title'] = htmlspecialchars($link['url']);
97597a3e4e3Sandi
97697a3e4e3Sandi        //output formatted
977a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
9780cecf9d5Sandi    }
9790cecf9d5Sandi
9800cecf9d5Sandi    /**
9813dd5c225SAndreas Gohr     * Link to windows share
9823dd5c225SAndreas Gohr     *
9833dd5c225SAndreas Gohr     * @param string       $url  the link
9843dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
9850cecf9d5Sandi     */
9860ea51e63SMatt Perry    function windowssharelink($url, $name = null) {
9871d47afe1Sandi        global $conf;
9883dd5c225SAndreas Gohr
9891d47afe1Sandi        //simple setup
9901d47afe1Sandi        $link['target'] = $conf['target']['windows'];
9911d47afe1Sandi        $link['pre']    = '';
9921d47afe1Sandi        $link['suf']    = '';
9931d47afe1Sandi        $link['style']  = '';
9940cecf9d5Sandi
995433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
9960cecf9d5Sandi        if(!$isImage) {
9971d47afe1Sandi            $link['class'] = 'windows';
9980cecf9d5Sandi        } else {
9991d47afe1Sandi            $link['class'] = 'media';
10000cecf9d5Sandi        }
10010cecf9d5Sandi
1002433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
10031d47afe1Sandi        $url           = str_replace('\\', '/', $url);
100483b7e38dSMichael Große        $url           = ltrim($url,'/');
10051d47afe1Sandi        $url           = 'file:///'.$url;
10061d47afe1Sandi        $link['url']   = $url;
10070cecf9d5Sandi
10081d47afe1Sandi        //output formatted
1009a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10100cecf9d5Sandi    }
10110cecf9d5Sandi
10123dd5c225SAndreas Gohr    /**
10133dd5c225SAndreas Gohr     * Render a linked E-Mail Address
10143dd5c225SAndreas Gohr     *
10153dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
10163dd5c225SAndreas Gohr     *
10173dd5c225SAndreas Gohr     * @param string       $address Email-Address
10183dd5c225SAndreas Gohr     * @param string|array $name    name for the link, array for media file
10193dd5c225SAndreas Gohr     */
10200ea51e63SMatt Perry    function emaillink($address, $name = null) {
102171352defSandi        global $conf;
102271352defSandi        //simple setup
102371352defSandi        $link           = array();
102471352defSandi        $link['target'] = '';
102571352defSandi        $link['pre']    = '';
102671352defSandi        $link['suf']    = '';
102771352defSandi        $link['style']  = '';
102871352defSandi        $link['more']   = '';
10290cecf9d5Sandi
1030c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
10310cecf9d5Sandi        if(!$isImage) {
1032be96545cSAnika Henke            $link['class'] = 'mail';
10330cecf9d5Sandi        } else {
1034be96545cSAnika Henke            $link['class'] = 'media';
10350cecf9d5Sandi        }
10360cecf9d5Sandi
103707738714SAndreas Gohr        $address = $this->_xmlEntities($address);
103800a7b5adSEsther Brunner        $address = obfuscate($address);
103900a7b5adSEsther Brunner        $title   = $address;
10408c128049SAndreas Gohr
104171352defSandi        if(empty($name)) {
104200a7b5adSEsther Brunner            $name = $address;
104371352defSandi        }
10440cecf9d5Sandi
1045776b36ecSAndreas Gohr        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1046776b36ecSAndreas Gohr
1047776b36ecSAndreas Gohr        $link['url']   = 'mailto:'.$address;
104871352defSandi        $link['name']  = $name;
104971352defSandi        $link['title'] = $title;
10500cecf9d5Sandi
105171352defSandi        //output formatted
1052a2d649c4Sandi        $this->doc .= $this->_formatLink($link);
10530cecf9d5Sandi    }
10540cecf9d5Sandi
10553dd5c225SAndreas Gohr    /**
10563dd5c225SAndreas Gohr     * Render an internal media file
10573dd5c225SAndreas Gohr     *
10583dd5c225SAndreas Gohr     * @param string $src       media ID
10593dd5c225SAndreas Gohr     * @param string $title     descriptive text
10603dd5c225SAndreas Gohr     * @param string $align     left|center|right
10613dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
10623dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
10633dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
10643dd5c225SAndreas Gohr     * @param string $linking   linkonly|detail|nolink
10653dd5c225SAndreas Gohr     * @param bool   $return    return HTML instead of adding to $doc
10663dd5c225SAndreas Gohr     * @return void|string
10673dd5c225SAndreas Gohr     */
10680ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
10693dd5c225SAndreas Gohr                           $height = null, $cache = null, $linking = null, $return = false) {
107037e34a5eSandi        global $ID;
107191df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
1072cdb5e961Slisps        resolve_mediaid(getNS($ID), $src, $exists, $this->date_at, true);
10730cecf9d5Sandi
1074d98d4540SBen Coburn        $noLink = false;
10758acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1076b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
10773685f775Sandi
10783dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1079b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
108052dc5eadSlisps            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src)), ($linking == 'direct'));
1081f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
10822a2a2ba2SAnika Henke            // don't link movies
108344881bd0Shenning.noren            $noLink = true;
108455efc227SAndreas Gohr        } else {
10852ca14335SEsther Brunner            // add file icons
10869d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
10879d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_'.$class;
108852dc5eadSlisps            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache , 'rev'=>$this->_getLastMediaRevisionAt($src)), true);
108991328684SMichael Hamann            if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
109055efc227SAndreas Gohr        }
10913685f775Sandi
109291df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
109391df343aSAndreas Gohr
10946fe20453SGina Haeussge        //markup non existing files
10954a24b459SKate Arzamastseva        if(!$exists) {
10966fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
10974a24b459SKate Arzamastseva        }
10986fe20453SGina Haeussge
10993685f775Sandi        //output formatted
1100f50634f0SAnika Henke        if($return) {
1101f50634f0SAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1102f50634f0SAnika Henke            else return $this->_formatLink($link);
1103f50634f0SAnika Henke        } else {
1104dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11052ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11060cecf9d5Sandi        }
1107f50634f0SAnika Henke    }
11080cecf9d5Sandi
11093dd5c225SAndreas Gohr    /**
11103dd5c225SAndreas Gohr     * Render an external media file
11113dd5c225SAndreas Gohr     *
11123dd5c225SAndreas Gohr     * @param string $src     full media URL
11133dd5c225SAndreas Gohr     * @param string $title   descriptive text
11143dd5c225SAndreas Gohr     * @param string $align   left|center|right
11153dd5c225SAndreas Gohr     * @param int    $width   width of media in pixel
11163dd5c225SAndreas Gohr     * @param int    $height  height of media in pixel
11173dd5c225SAndreas Gohr     * @param string $cache   cache|recache|nocache
11183dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1119410ee62aSAnika Henke     * @param bool   $return  return HTML instead of adding to $doc
11203dd5c225SAndreas Gohr     */
11210ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
1122410ee62aSAnika Henke                           $height = null, $cache = null, $linking = null, $return = false) {
112391df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
1124d98d4540SBen Coburn        $noLink = false;
11258acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1126b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1127b739ff0fSPierre Spring
1128b739ff0fSPierre Spring        $link['url'] = ml($src, array('cache' => $cache));
11293685f775Sandi
11303dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1131b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
11322ca14335SEsther Brunner            // link only jpeg images
113344881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1134f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
11352a2a2ba2SAnika Henke            // don't link movies
113644881bd0Shenning.noren            $noLink = true;
11372ca14335SEsther Brunner        } else {
11382ca14335SEsther Brunner            // add file icons
113927bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
114027bf7924STom N Harris            $link['class'] .= ' mediafile mf_'.$class;
11412ca14335SEsther Brunner        }
11422ca14335SEsther Brunner
114391df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
114491df343aSAndreas Gohr
11453685f775Sandi        //output formatted
1146410ee62aSAnika Henke        if($return) {
1147410ee62aSAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1148410ee62aSAnika Henke            else return $this->_formatLink($link);
1149410ee62aSAnika Henke        } else {
1150dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11512ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11520cecf9d5Sandi        }
1153410ee62aSAnika Henke    }
11540cecf9d5Sandi
11554826ab45Sandi    /**
11563db95becSAndreas Gohr     * Renders an RSS feed
1157b625487dSandi     *
1158b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1159b625487dSandi     */
11603db95becSAndreas Gohr    function rss($url, $params) {
1161b625487dSandi        global $lang;
11623db95becSAndreas Gohr        global $conf;
11633db95becSAndreas Gohr
11643db95becSAndreas Gohr        require_once(DOKU_INC.'inc/FeedParser.php');
11653db95becSAndreas Gohr        $feed = new FeedParser();
116600077af8SAndreas Gohr        $feed->set_feed_url($url);
1167b625487dSandi
1168b625487dSandi        //disable warning while fetching
11693dd5c225SAndreas Gohr        if(!defined('DOKU_E_LEVEL')) {
11703dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
11713dd5c225SAndreas Gohr        }
11723db95becSAndreas Gohr        $rc = $feed->init();
11733dd5c225SAndreas Gohr        if(isset($elvl)) {
11743dd5c225SAndreas Gohr            error_reporting($elvl);
11753dd5c225SAndreas Gohr        }
1176b625487dSandi
11773db95becSAndreas Gohr        //decide on start and end
11783db95becSAndreas Gohr        if($params['reverse']) {
11793db95becSAndreas Gohr            $mod   = -1;
11803db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
11813db95becSAndreas Gohr            $end   = $start - ($params['max']);
1182b2a412b0SAndreas Gohr            $end   = ($end < -1) ? -1 : $end;
11833db95becSAndreas Gohr        } else {
11843db95becSAndreas Gohr            $mod   = 1;
11853db95becSAndreas Gohr            $start = 0;
11863db95becSAndreas Gohr            $end   = $feed->get_item_quantity();
1187d91ab76fSMatt Perry            $end   = ($end > $params['max']) ? $params['max'] : $end;
11883db95becSAndreas Gohr        }
11893db95becSAndreas Gohr
1190a2d649c4Sandi        $this->doc .= '<ul class="rss">';
11913db95becSAndreas Gohr        if($rc) {
11923db95becSAndreas Gohr            for($x = $start; $x != $end; $x += $mod) {
11931bde1582SAndreas Gohr                $item = $feed->get_item($x);
11943db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
1195d2ea3363SAndreas Gohr                // support feeds without links
1196d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
1197d2ea3363SAndreas Gohr                if($lnkurl) {
1198793361f8SAndreas Gohr                    // title is escaped by SimplePie, we unescape here because it
1199793361f8SAndreas Gohr                    // is escaped again in externallink() FS#1705
12003dd5c225SAndreas Gohr                    $this->externallink(
12013dd5c225SAndreas Gohr                        $item->get_permalink(),
12023dd5c225SAndreas Gohr                        html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')
12033dd5c225SAndreas Gohr                    );
1204d2ea3363SAndreas Gohr                } else {
1205d2ea3363SAndreas Gohr                    $this->doc .= ' '.$item->get_title();
1206d2ea3363SAndreas Gohr                }
12073db95becSAndreas Gohr                if($params['author']) {
12081bde1582SAndreas Gohr                    $author = $item->get_author(0);
12091bde1582SAndreas Gohr                    if($author) {
12101bde1582SAndreas Gohr                        $name = $author->get_name();
12111bde1582SAndreas Gohr                        if(!$name) $name = $author->get_email();
12121bde1582SAndreas Gohr                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
12131bde1582SAndreas Gohr                    }
12143db95becSAndreas Gohr                }
12153db95becSAndreas Gohr                if($params['date']) {
12162e7e0c29SAndreas Gohr                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
12173db95becSAndreas Gohr                }
12181bde1582SAndreas Gohr                if($params['details']) {
12193db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
1220173dccb7STom N Harris                    if($conf['htmlok']) {
12211bde1582SAndreas Gohr                        $this->doc .= $item->get_description();
12223db95becSAndreas Gohr                    } else {
12231bde1582SAndreas Gohr                        $this->doc .= strip_tags($item->get_description());
12243db95becSAndreas Gohr                    }
12253db95becSAndreas Gohr                    $this->doc .= '</div>';
12263db95becSAndreas Gohr                }
12273db95becSAndreas Gohr
12283db95becSAndreas Gohr                $this->doc .= '</div></li>';
1229b625487dSandi            }
1230b625487dSandi        } else {
12313db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1232a2d649c4Sandi            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
1233b625487dSandi            $this->externallink($url);
123445e147ccSAndreas Gohr            if($conf['allowdebug']) {
123545e147ccSAndreas Gohr                $this->doc .= '<!--'.hsc($feed->error).'-->';
123645e147ccSAndreas Gohr            }
12373db95becSAndreas Gohr            $this->doc .= '</div></li>';
1238b625487dSandi        }
1239a2d649c4Sandi        $this->doc .= '</ul>';
1240b625487dSandi    }
1241b625487dSandi
12423dd5c225SAndreas Gohr    /**
12433dd5c225SAndreas Gohr     * Start a table
12443dd5c225SAndreas Gohr     *
12453dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
12463dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
12473dd5c225SAndreas Gohr     * @param int $pos     byte position in the original source
12483dd5c225SAndreas Gohr     */
1249619736fdSAdrian Lang    function table_open($maxcols = null, $numrows = null, $pos = null) {
1250b5742cedSPierre Spring        // initialize the row counter used for classes
1251b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1252619736fdSAdrian Lang        $class                         = 'table';
1253619736fdSAdrian Lang        if($pos !== null) {
1254619736fdSAdrian Lang            $class .= ' '.$this->startSectionEdit($pos, 'table');
1255619736fdSAdrian Lang        }
1256619736fdSAdrian Lang        $this->doc .= '<div class="'.$class.'"><table class="inline">'.
1257619736fdSAdrian Lang            DOKU_LF;
12580cecf9d5Sandi    }
12590cecf9d5Sandi
12603dd5c225SAndreas Gohr    /**
12613dd5c225SAndreas Gohr     * Close a table
12623dd5c225SAndreas Gohr     *
12633dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
12643dd5c225SAndreas Gohr     */
1265619736fdSAdrian Lang    function table_close($pos = null) {
1266a8574918SAnika Henke        $this->doc .= '</table></div>'.DOKU_LF;
1267619736fdSAdrian Lang        if($pos !== null) {
126890df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
12690cecf9d5Sandi        }
1270619736fdSAdrian Lang    }
12710cecf9d5Sandi
12723dd5c225SAndreas Gohr    /**
12733dd5c225SAndreas Gohr     * Open a table header
12743dd5c225SAndreas Gohr     */
1275f05a1cc5SGerrit Uitslag    function tablethead_open() {
1276f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
1277f05a1cc5SGerrit Uitslag    }
1278f05a1cc5SGerrit Uitslag
12793dd5c225SAndreas Gohr    /**
12803dd5c225SAndreas Gohr     * Close a table header
12813dd5c225SAndreas Gohr     */
1282f05a1cc5SGerrit Uitslag    function tablethead_close() {
1283f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
1284f05a1cc5SGerrit Uitslag    }
1285f05a1cc5SGerrit Uitslag
12863dd5c225SAndreas Gohr    /**
12873dd5c225SAndreas Gohr     * Open a table row
12883dd5c225SAndreas Gohr     */
12890cecf9d5Sandi    function tablerow_open() {
1290b5742cedSPierre Spring        // initialize the cell counter used for classes
1291b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1292b5742cedSPierre Spring        $class                          = 'row'.$this->_counter['row_counter']++;
1293b5742cedSPierre Spring        $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
12940cecf9d5Sandi    }
12950cecf9d5Sandi
12963dd5c225SAndreas Gohr    /**
12973dd5c225SAndreas Gohr     * Close a table row
12983dd5c225SAndreas Gohr     */
12990cecf9d5Sandi    function tablerow_close() {
1300a2d649c4Sandi        $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
13010cecf9d5Sandi    }
13020cecf9d5Sandi
13033dd5c225SAndreas Gohr    /**
13043dd5c225SAndreas Gohr     * Open a table header cell
13053dd5c225SAndreas Gohr     *
13063dd5c225SAndreas Gohr     * @param int    $colspan
13073dd5c225SAndreas Gohr     * @param string $align left|center|right
13083dd5c225SAndreas Gohr     * @param int    $rowspan
13093dd5c225SAndreas Gohr     */
13100ea51e63SMatt Perry    function tableheader_open($colspan = 1, $align = null, $rowspan = 1) {
1311b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13120cecf9d5Sandi        if(!is_null($align)) {
1313b5742cedSPierre Spring            $class .= ' '.$align.'align';
13140cecf9d5Sandi        }
1315b5742cedSPierre Spring        $class .= '"';
1316b5742cedSPierre Spring        $this->doc .= '<th '.$class;
13170cecf9d5Sandi        if($colspan > 1) {
1318a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1319a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13200cecf9d5Sandi        }
132125b97867Shakan.sandell        if($rowspan > 1) {
132225b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
132325b97867Shakan.sandell        }
1324a2d649c4Sandi        $this->doc .= '>';
13250cecf9d5Sandi    }
13260cecf9d5Sandi
13273dd5c225SAndreas Gohr    /**
13283dd5c225SAndreas Gohr     * Close a table header cell
13293dd5c225SAndreas Gohr     */
13300cecf9d5Sandi    function tableheader_close() {
1331a2d649c4Sandi        $this->doc .= '</th>';
13320cecf9d5Sandi    }
13330cecf9d5Sandi
13343dd5c225SAndreas Gohr    /**
13353dd5c225SAndreas Gohr     * Open a table cell
13363dd5c225SAndreas Gohr     *
13373dd5c225SAndreas Gohr     * @param int    $colspan
13383dd5c225SAndreas Gohr     * @param string $align left|center|right
13393dd5c225SAndreas Gohr     * @param int    $rowspan
13403dd5c225SAndreas Gohr     */
13410ea51e63SMatt Perry    function tablecell_open($colspan = 1, $align = null, $rowspan = 1) {
1342b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
13430cecf9d5Sandi        if(!is_null($align)) {
1344b5742cedSPierre Spring            $class .= ' '.$align.'align';
13450cecf9d5Sandi        }
1346b5742cedSPierre Spring        $class .= '"';
1347b5742cedSPierre Spring        $this->doc .= '<td '.$class;
13480cecf9d5Sandi        if($colspan > 1) {
1349a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1350a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
13510cecf9d5Sandi        }
135225b97867Shakan.sandell        if($rowspan > 1) {
135325b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
135425b97867Shakan.sandell        }
1355a2d649c4Sandi        $this->doc .= '>';
13560cecf9d5Sandi    }
13570cecf9d5Sandi
13583dd5c225SAndreas Gohr    /**
13593dd5c225SAndreas Gohr     * Close a table cell
13603dd5c225SAndreas Gohr     */
13610cecf9d5Sandi    function tablecell_close() {
1362a2d649c4Sandi        $this->doc .= '</td>';
13630cecf9d5Sandi    }
13640cecf9d5Sandi
13653dd5c225SAndreas Gohr    #region Utility functions
13660cecf9d5Sandi
1367ba11bd29Sandi    /**
13683fd0b676Sandi     * Build a link
13693fd0b676Sandi     *
13703fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1371ba11bd29Sandi     *
1372ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1373ba11bd29Sandi     */
1374433bef32Sandi    function _formatLink($link) {
1375ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1376ba11bd29Sandi        if(substr($link['url'], 0, 7) != 'mailto:') {
1377ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1378ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1379ba11bd29Sandi        }
1380ba11bd29Sandi        //remove double encodings in titles
1381ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1382ba11bd29Sandi
1383453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1384453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1385453493f2SAndreas Gohr        $link['url']   = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1386453493f2SAndreas Gohr        $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
1387453493f2SAndreas Gohr
1388ba11bd29Sandi        $ret = '';
1389ba11bd29Sandi        $ret .= $link['pre'];
1390ba11bd29Sandi        $ret .= '<a href="'.$link['url'].'"';
1391bb4866bdSchris        if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1392bb4866bdSchris        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1393bb4866bdSchris        if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1394bb4866bdSchris        if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
139544a6b4c7SAndreas Gohr        if(!empty($link['rel'])) $ret .= ' rel="'.$link['rel'].'"';
1396bb4866bdSchris        if(!empty($link['more'])) $ret .= ' '.$link['more'];
1397ba11bd29Sandi        $ret .= '>';
1398ba11bd29Sandi        $ret .= $link['name'];
1399ba11bd29Sandi        $ret .= '</a>';
1400ba11bd29Sandi        $ret .= $link['suf'];
1401ba11bd29Sandi        return $ret;
1402ba11bd29Sandi    }
1403ba11bd29Sandi
1404ba11bd29Sandi    /**
14053fd0b676Sandi     * Renders internal and external media
14063fd0b676Sandi     *
14073fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
14083dd5c225SAndreas Gohr     * @param string $src       media ID
14093dd5c225SAndreas Gohr     * @param string $title     descriptive text
14103dd5c225SAndreas Gohr     * @param string $align     left|center|right
14113dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
14123dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
14133dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
14143dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
14153dd5c225SAndreas Gohr     * @return string
14163fd0b676Sandi     */
14170ea51e63SMatt Perry    function _media($src, $title = null, $align = null, $width = null,
14180ea51e63SMatt Perry                    $height = null, $cache = null, $render = true) {
14193fd0b676Sandi
14203fd0b676Sandi        $ret = '';
14213fd0b676Sandi
14223dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src);
14233fd0b676Sandi        if(substr($mime, 0, 5) == 'image') {
1424b739ff0fSPierre Spring            // first get the $title
1425b739ff0fSPierre Spring            if(!is_null($title)) {
1426b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1427b739ff0fSPierre Spring            } elseif($ext == 'jpg' || $ext == 'jpeg') {
1428b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1429b739ff0fSPierre Spring                require_once(DOKU_INC.'inc/JpegMeta.php');
143067f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1431b739ff0fSPierre Spring                if($jpeg !== false) $cap = $jpeg->getTitle();
14323dd5c225SAndreas Gohr                if(!empty($cap)) {
1433b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1434b739ff0fSPierre Spring                }
1435b739ff0fSPierre Spring            }
1436b739ff0fSPierre Spring            if(!$render) {
1437b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1438b739ff0fSPierre Spring                // return the title of the picture
1439b739ff0fSPierre Spring                if(!$title) {
1440b739ff0fSPierre Spring                    // just show the sourcename
14413009a773SAndreas Gohr                    $title = $this->_xmlEntities(utf8_basename(noNS($src)));
1442b739ff0fSPierre Spring                }
1443b739ff0fSPierre Spring                return $title;
1444b739ff0fSPierre Spring            }
14453fd0b676Sandi            //add image tag
144652dc5eadSlisps            $ret .= '<img src="'.ml($src, array('w' => $width, 'h' => $height, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src))).'"';
14473fd0b676Sandi            $ret .= ' class="media'.$align.'"';
14483fd0b676Sandi
1449b739ff0fSPierre Spring            if($title) {
1450b739ff0fSPierre Spring                $ret .= ' title="'.$title.'"';
1451b739ff0fSPierre Spring                $ret .= ' alt="'.$title.'"';
14523fd0b676Sandi            } else {
14533fd0b676Sandi                $ret .= ' alt=""';
14543fd0b676Sandi            }
14553fd0b676Sandi
14563fd0b676Sandi            if(!is_null($width))
14573fd0b676Sandi                $ret .= ' width="'.$this->_xmlEntities($width).'"';
14583fd0b676Sandi
14593fd0b676Sandi            if(!is_null($height))
14603fd0b676Sandi                $ret .= ' height="'.$this->_xmlEntities($height).'"';
14613fd0b676Sandi
14623fd0b676Sandi            $ret .= ' />';
14633fd0b676Sandi
146417954bb5SAnika Henke        } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
14652a2a2ba2SAnika Henke            // first get the $title
146617954bb5SAnika Henke            $title = !is_null($title) ? $this->_xmlEntities($title) : false;
14672a2a2ba2SAnika Henke            if(!$render) {
146817954bb5SAnika Henke                // if the file is not supposed to be rendered
146917954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
147017954bb5SAnika Henke                return $title ? $title : $this->_xmlEntities(utf8_basename(noNS($src)));
14712a2a2ba2SAnika Henke            }
14722a2a2ba2SAnika Henke
14732a2a2ba2SAnika Henke            $att          = array();
14742a2a2ba2SAnika Henke            $att['class'] = "media$align";
147517954bb5SAnika Henke            if($title) {
147617954bb5SAnika Henke                $att['title'] = $title;
147717954bb5SAnika Henke            }
14782a2a2ba2SAnika Henke
147917954bb5SAnika Henke            if(media_supportedav($mime, 'video')) {
148017954bb5SAnika Henke                //add video
148179e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1482b44a5dceSAnika Henke            }
148317954bb5SAnika Henke            if(media_supportedav($mime, 'audio')) {
1484b44a5dceSAnika Henke                //add audio
1485b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
148617954bb5SAnika Henke            }
1487b44a5dceSAnika Henke
14883fd0b676Sandi        } elseif($mime == 'application/x-shockwave-flash') {
14891c882ba8SAndreas Gohr            if(!$render) {
14901c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
14911c882ba8SAndreas Gohr                // return the title of the flash
14921c882ba8SAndreas Gohr                if(!$title) {
14931c882ba8SAndreas Gohr                    // just show the sourcename
14943009a773SAndreas Gohr                    $title = utf8_basename(noNS($src));
14951c882ba8SAndreas Gohr                }
149607bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
14971c882ba8SAndreas Gohr            }
14981c882ba8SAndreas Gohr
149907bf32b2SAndreas Gohr            $att          = array();
150007bf32b2SAndreas Gohr            $att['class'] = "media$align";
150107bf32b2SAndreas Gohr            if($align == 'right') $att['align'] = 'right';
150207bf32b2SAndreas Gohr            if($align == 'left') $att['align'] = 'left';
15033dd5c225SAndreas Gohr            $ret .= html_flashobject(
15043dd5c225SAndreas Gohr                ml($src, array('cache' => $cache), true, '&'), $width, $height,
150507bf32b2SAndreas Gohr                array('quality' => 'high'),
150607bf32b2SAndreas Gohr                null,
150707bf32b2SAndreas Gohr                $att,
15083dd5c225SAndreas Gohr                $this->_xmlEntities($title)
15093dd5c225SAndreas Gohr            );
15100f428d7dSAndreas Gohr        } elseif($title) {
15113fd0b676Sandi            // well at least we have a title to display
15123fd0b676Sandi            $ret .= $this->_xmlEntities($title);
15133fd0b676Sandi        } else {
15145291ca3aSAndreas Gohr            // just show the sourcename
15153009a773SAndreas Gohr            $ret .= $this->_xmlEntities(utf8_basename(noNS($src)));
15163fd0b676Sandi        }
15173fd0b676Sandi
15183fd0b676Sandi        return $ret;
15193fd0b676Sandi    }
15203fd0b676Sandi
15213dd5c225SAndreas Gohr    /**
15223dd5c225SAndreas Gohr     * Escape string for output
15233dd5c225SAndreas Gohr     *
15243dd5c225SAndreas Gohr     * @param $string
15253dd5c225SAndreas Gohr     * @return string
15263dd5c225SAndreas Gohr     */
1527433bef32Sandi    function _xmlEntities($string) {
1528de117061Schris        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
15290cecf9d5Sandi    }
15300cecf9d5Sandi
15318a831f2bSAndreas Gohr    /**
15328a831f2bSAndreas Gohr     * Creates a linkid from a headline
1533c5a8fd96SAndreas Gohr     *
15343dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1535c5a8fd96SAndreas Gohr     * @param string  $title   The headline title
1536c5a8fd96SAndreas Gohr     * @param boolean $create  Create a new unique ID?
15373dd5c225SAndreas Gohr     * @return string
15388a831f2bSAndreas Gohr     */
1539c5a8fd96SAndreas Gohr    function _headerToLink($title, $create = false) {
1540c5a8fd96SAndreas Gohr        if($create) {
15414ceab83fSAndreas Gohr            return sectionID($title, $this->headers);
15424ceab83fSAndreas Gohr        } else {
1543443d207bSAndreas Gohr            $check = false;
1544443d207bSAndreas Gohr            return sectionID($title, $check);
1545c5a8fd96SAndreas Gohr        }
15460cecf9d5Sandi    }
15470cecf9d5Sandi
1548af587fa8Sandi    /**
15493fd0b676Sandi     * Construct a title and handle images in titles
15503fd0b676Sandi     *
15510b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
15523dd5c225SAndreas Gohr     * @param string|array $title    either string title or media array
15533dd5c225SAndreas Gohr     * @param string       $default  default title if nothing else is found
15543dd5c225SAndreas Gohr     * @param bool         $isImage  will be set to true if it's a media file
15553dd5c225SAndreas Gohr     * @param null|string  $id       linked page id (used to extract title from first heading)
15563dd5c225SAndreas Gohr     * @param string       $linktype content|navigation
15573dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
15583fd0b676Sandi     */
15590ea51e63SMatt Perry    function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
156044881bd0Shenning.noren        $isImage = false;
156129657f9eSAndreas Gohr        if(is_array($title)) {
156229657f9eSAndreas Gohr            $isImage = true;
156329657f9eSAndreas Gohr            return $this->_imageTitle($title);
156429657f9eSAndreas Gohr        } elseif(is_null($title) || trim($title) == '') {
1565fe9ec250SChris Smith            if(useHeading($linktype) && $id) {
156667c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1567bb0a59d4Sjan                if($heading) {
1568433bef32Sandi                    return $this->_xmlEntities($heading);
1569bb0a59d4Sjan                }
1570bb0a59d4Sjan            }
1571433bef32Sandi            return $this->_xmlEntities($default);
157268c26e6dSMichael Klier        } else {
157368c26e6dSMichael Klier            return $this->_xmlEntities($title);
15740cecf9d5Sandi        }
15750cecf9d5Sandi    }
15760cecf9d5Sandi
15770cecf9d5Sandi    /**
15783dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
15793fd0b676Sandi     *
15803fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
15813dd5c225SAndreas Gohr     * @param string $img
15823dd5c225SAndreas Gohr     * @return string HTML img tag or similar
15830cecf9d5Sandi     */
1584433bef32Sandi    function _imageTitle($img) {
1585d9baf1a7SKazutaka Miyasaka        global $ID;
1586d9baf1a7SKazutaka Miyasaka
1587d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1588d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
15893dd5c225SAndreas Gohr        list($img['src']) = explode('#', $img['src'], 2);
1590d9baf1a7SKazutaka Miyasaka        if($img['type'] == 'internalmedia') {
1591cdb5e961Slisps            resolve_mediaid(getNS($ID), $img['src'], $exists ,$this->date_at, true);
1592d9baf1a7SKazutaka Miyasaka        }
1593d9baf1a7SKazutaka Miyasaka
15943dd5c225SAndreas Gohr        return $this->_media(
15953dd5c225SAndreas Gohr            $img['src'],
15964826ab45Sandi            $img['title'],
15974826ab45Sandi            $img['align'],
15984826ab45Sandi            $img['width'],
15994826ab45Sandi            $img['height'],
16003dd5c225SAndreas Gohr            $img['cache']
16013dd5c225SAndreas Gohr        );
16020cecf9d5Sandi    }
1603b739ff0fSPierre Spring
1604b739ff0fSPierre Spring    /**
16053dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
16063dd5c225SAndreas Gohr     *
16073dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1608b739ff0fSPierre Spring     *
1609b739ff0fSPierre Spring     * @author   Pierre Spring <pierre.spring@liip.ch>
16103dd5c225SAndreas Gohr     * @param string $src       media ID
16113dd5c225SAndreas Gohr     * @param string $title     descriptive text
16123dd5c225SAndreas Gohr     * @param string $align     left|center|right
16133dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
16143dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
16153dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
16163dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
16173dd5c225SAndreas Gohr     * @return array associative array with link config
1618b739ff0fSPierre Spring     */
1619d91ab76fSMatt Perry    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1620b739ff0fSPierre Spring        global $conf;
1621b739ff0fSPierre Spring
1622b739ff0fSPierre Spring        $link           = array();
1623b739ff0fSPierre Spring        $link['class']  = 'media';
1624b739ff0fSPierre Spring        $link['style']  = '';
1625b739ff0fSPierre Spring        $link['pre']    = '';
1626b739ff0fSPierre Spring        $link['suf']    = '';
1627b739ff0fSPierre Spring        $link['more']   = '';
1628b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1629b739ff0fSPierre Spring        $link['title']  = $this->_xmlEntities($src);
1630b739ff0fSPierre Spring        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1631b739ff0fSPierre Spring
1632b739ff0fSPierre Spring        return $link;
1633b739ff0fSPierre Spring    }
163491459163SAnika Henke
16352a2a2ba2SAnika Henke    /**
16362a2a2ba2SAnika Henke     * Embed video(s) in HTML
16372a2a2ba2SAnika Henke     *
16382a2a2ba2SAnika Henke     * @author Anika Henke <anika@selfthinker.org>
16392a2a2ba2SAnika Henke     *
16402a2a2ba2SAnika Henke     * @param string $src         - ID of video to embed
16412a2a2ba2SAnika Henke     * @param int    $width       - width of the video in pixels
16422a2a2ba2SAnika Henke     * @param int    $height      - height of the video in pixels
16432a2a2ba2SAnika Henke     * @param array  $atts        - additional attributes for the <video> tag
1644f50634f0SAnika Henke     * @return string
16452a2a2ba2SAnika Henke     */
164679e53fe5SAnika Henke    function _video($src, $width, $height, $atts = null) {
16472a2a2ba2SAnika Henke        // prepare width and height
16482a2a2ba2SAnika Henke        if(is_null($atts)) $atts = array();
16492a2a2ba2SAnika Henke        $atts['width']  = (int) $width;
16502a2a2ba2SAnika Henke        $atts['height'] = (int) $height;
16512a2a2ba2SAnika Henke        if(!$atts['width']) $atts['width'] = 320;
16522a2a2ba2SAnika Henke        if(!$atts['height']) $atts['height'] = 240;
16532a2a2ba2SAnika Henke
1654410ee62aSAnika Henke        $posterUrl = '';
1655410ee62aSAnika Henke        $files = array();
1656410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1657410ee62aSAnika Henke
1658410ee62aSAnika Henke        if ($isExternal) {
1659410ee62aSAnika Henke            // take direct source for external files
1660702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1661410ee62aSAnika Henke            $files[$srcMime] = $src;
1662410ee62aSAnika Henke        } else {
16633d7a9e0aSAnika Henke            // prepare alternative formats
16643d7a9e0aSAnika Henke            $extensions   = array('webm', 'ogv', 'mp4');
1665410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
166699f943f6SAnika Henke            $poster       = media_alternativefiles($src, array('jpg', 'png'), true);
166799f943f6SAnika Henke            if(!empty($poster)) {
16682d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
166999f943f6SAnika Henke            }
1670410ee62aSAnika Henke        }
16712a2a2ba2SAnika Henke
1672f50634f0SAnika Henke        $out = '';
167379e53fe5SAnika Henke        // open video tag
1674f50634f0SAnika Henke        $out .= '<video '.buildAttributes($atts).' controls="controls"';
16753641199aSAnika Henke        if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1676f50634f0SAnika Henke        $out .= '>'.NL;
16773641199aSAnika Henke        $fallback = '';
167879e53fe5SAnika Henke
167979e53fe5SAnika Henke        // output source for each alternative video format
1680410ee62aSAnika Henke        foreach($files as $mime => $file) {
1681410ee62aSAnika Henke            if ($isExternal) {
1682410ee62aSAnika Henke                $url = $file;
1683410ee62aSAnika Henke                $linkType = 'externalmedia';
1684410ee62aSAnika Henke            } else {
16852d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1686410ee62aSAnika Henke                $linkType = 'internalmedia';
1687410ee62aSAnika Henke            }
168817954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
16893d7a9e0aSAnika Henke
1690f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
169179e53fe5SAnika Henke            // alternative content (just a link to the file)
1692410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
16933d7a9e0aSAnika Henke        }
16942a2a2ba2SAnika Henke
16952a2a2ba2SAnika Henke        // finish
16963641199aSAnika Henke        $out .= $fallback;
1697f50634f0SAnika Henke        $out .= '</video>'.NL;
1698f50634f0SAnika Henke        return $out;
16992a2a2ba2SAnika Henke    }
17002a2a2ba2SAnika Henke
1701b44a5dceSAnika Henke    /**
1702b44a5dceSAnika Henke     * Embed audio in HTML
1703b44a5dceSAnika Henke     *
1704b44a5dceSAnika Henke     * @author Anika Henke <anika@selfthinker.org>
1705b44a5dceSAnika Henke     *
1706b44a5dceSAnika Henke     * @param string $src       - ID of audio to embed
17076d4af72aSAnika Henke     * @param array  $atts      - additional attributes for the <audio> tag
1708f50634f0SAnika Henke     * @return string
1709b44a5dceSAnika Henke     */
1710b44a5dceSAnika Henke    function _audio($src, $atts = null) {
1711702e97d3SAnika Henke        $files = array();
1712410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1713b44a5dceSAnika Henke
1714410ee62aSAnika Henke        if ($isExternal) {
1715410ee62aSAnika Henke            // take direct source for external files
1716702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1717410ee62aSAnika Henke            $files[$srcMime] = $src;
1718410ee62aSAnika Henke        } else {
1719b44a5dceSAnika Henke            // prepare alternative formats
1720b44a5dceSAnika Henke            $extensions   = array('ogg', 'mp3', 'wav');
1721410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
1722410ee62aSAnika Henke        }
1723b44a5dceSAnika Henke
1724f50634f0SAnika Henke        $out = '';
1725b44a5dceSAnika Henke        // open audio tag
1726f50634f0SAnika Henke        $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
17273641199aSAnika Henke        $fallback = '';
1728b44a5dceSAnika Henke
1729b44a5dceSAnika Henke        // output source for each alternative audio format
1730410ee62aSAnika Henke        foreach($files as $mime => $file) {
1731410ee62aSAnika Henke            if ($isExternal) {
1732410ee62aSAnika Henke                $url = $file;
1733410ee62aSAnika Henke                $linkType = 'externalmedia';
1734410ee62aSAnika Henke            } else {
17352d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1736410ee62aSAnika Henke                $linkType = 'internalmedia';
1737410ee62aSAnika Henke            }
173817954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
1739b44a5dceSAnika Henke
1740f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1741b44a5dceSAnika Henke            // alternative content (just a link to the file)
1742410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
1743b44a5dceSAnika Henke        }
1744b44a5dceSAnika Henke
1745b44a5dceSAnika Henke        // finish
17463641199aSAnika Henke        $out .= $fallback;
1747f50634f0SAnika Henke        $out .= '</audio>'.NL;
1748f50634f0SAnika Henke        return $out;
1749b44a5dceSAnika Henke    }
1750b44a5dceSAnika Henke
17515c2eed9aSlisps    /**
175252dc5eadSlisps     * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
17535c2eed9aSlisps     * which returns an existing media revision less or equal to rev or date_at
17545c2eed9aSlisps     *
17555c2eed9aSlisps     * @author lisps
17565c2eed9aSlisps     * @param string $media_id
17575c2eed9aSlisps     * @access protected
17585c2eed9aSlisps     * @return string revision ('' for current)
17595c2eed9aSlisps     */
176052dc5eadSlisps    function _getLastMediaRevisionAt($media_id){
176152dc5eadSlisps        if(!$this->date_at || media_isexternal($media_id)) return '';
176278b874e6Slisps        $pagelog = new MediaChangeLog($media_id);
176378b874e6Slisps        return $pagelog->getLastRevisionAt($this->date_at);
17645c2eed9aSlisps    }
17655c2eed9aSlisps
17663dd5c225SAndreas Gohr    #endregion
17670cecf9d5Sandi}
17680cecf9d5Sandi
1769e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1770