xref: /dokuwiki/inc/parser/xhtml.php (revision 2571786c763e04c7abbf27c2245a5720878dc3f1)
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     *
6242ea7f44SGerrit Uitslag     * @param string $type   The section type identifier
6342ea7f44SGerrit Uitslag     * @param string $title  The section title
6442ea7f44SGerrit Uitslag     * @param int    $start  The byte position for the edit start
6590df9a4dSAdrian Lang     * @return string  A marker class for the starting HTML element
6642ea7f44SGerrit Uitslag     *
6790df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
6890df9a4dSAdrian Lang     */
69*2571786cSLarsDW223    public function startSectionEdit($start, $type, $title = null, $hid = null) {
70*2571786cSLarsDW223        $this->sectionedits[] = array(++$this->lastsecid, $start, $type, $title, $hid);
71b04a190dSMichael Hamann        return 'sectionedit'.$this->lastsecid;
7290df9a4dSAdrian Lang    }
7390df9a4dSAdrian Lang
7490df9a4dSAdrian Lang    /**
7590df9a4dSAdrian Lang     * Finish an edit section range
7690df9a4dSAdrian Lang     *
7742ea7f44SGerrit Uitslag     * @param int  $end     The byte position for the edit end; null for the rest of the page
7842ea7f44SGerrit Uitslag     *
7990df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
8090df9a4dSAdrian Lang     */
81*2571786cSLarsDW223    public function finishSectionEdit($end = null, $hid = null) {
82*2571786cSLarsDW223        list($id, $start, $type, $title, $hid) = array_pop($this->sectionedits);
83d9e36cbeSAdrian Lang        if(!is_null($end) && $end <= $start) {
8400c13053SAdrian Lang            return;
8500c13053SAdrian Lang        }
8640868f2fSAdrian Lang        $this->doc .= "<!-- EDIT$id ".strtoupper($type).' ';
8740868f2fSAdrian Lang        if(!is_null($title)) {
8840868f2fSAdrian Lang            $this->doc .= '"'.str_replace('"', '', $title).'" ';
8940868f2fSAdrian Lang        }
90*2571786cSLarsDW223        if(!is_null($hid)) {
91*2571786cSLarsDW223            $this->doc .= '"'.$hid.'" ';
92*2571786cSLarsDW223        }
93d9e36cbeSAdrian Lang        $this->doc .= "[$start-".(is_null($end) ? '' : $end).'] -->';
9490df9a4dSAdrian Lang    }
9590df9a4dSAdrian Lang
963dd5c225SAndreas Gohr    /**
973dd5c225SAndreas Gohr     * Returns the format produced by this renderer.
983dd5c225SAndreas Gohr     *
993dd5c225SAndreas Gohr     * @return string always 'xhtml'
1003dd5c225SAndreas Gohr     */
1015f70445dSAndreas Gohr    function getFormat() {
1025f70445dSAndreas Gohr        return 'xhtml';
1035f70445dSAndreas Gohr    }
1045f70445dSAndreas Gohr
1053dd5c225SAndreas Gohr    /**
1063dd5c225SAndreas Gohr     * Initialize the document
1073dd5c225SAndreas Gohr     */
1080cecf9d5Sandi    function document_start() {
109c5a8fd96SAndreas Gohr        //reset some internals
110c5a8fd96SAndreas Gohr        $this->toc     = array();
111c5a8fd96SAndreas Gohr        $this->headers = array();
1120cecf9d5Sandi    }
1130cecf9d5Sandi
1143dd5c225SAndreas Gohr    /**
1153dd5c225SAndreas Gohr     * Finalize the document
1163dd5c225SAndreas Gohr     */
1170cecf9d5Sandi    function document_end() {
11890df9a4dSAdrian Lang        // Finish open section edits.
11990df9a4dSAdrian Lang        while(count($this->sectionedits) > 0) {
12090df9a4dSAdrian Lang            if($this->sectionedits[count($this->sectionedits) - 1][1] <= 1) {
12190df9a4dSAdrian Lang                // If there is only one section, do not write a section edit
12290df9a4dSAdrian Lang                // marker.
12390df9a4dSAdrian Lang                array_pop($this->sectionedits);
12490df9a4dSAdrian Lang            } else {
125d9e36cbeSAdrian Lang                $this->finishSectionEdit();
12690df9a4dSAdrian Lang            }
12790df9a4dSAdrian Lang        }
12890df9a4dSAdrian Lang
1290cecf9d5Sandi        if(count($this->footnotes) > 0) {
130a2d649c4Sandi            $this->doc .= '<div class="footnotes">'.DOKU_LF;
131d74aace9Schris
13216ec3e37SAndreas Gohr            foreach($this->footnotes as $id => $footnote) {
133d74aace9Schris                // check its not a placeholder that indicates actual footnote text is elsewhere
134d74aace9Schris                if(substr($footnote, 0, 5) != "@@FNT") {
135d74aace9Schris
136d74aace9Schris                    // open the footnote and set the anchor and backlink
137d74aace9Schris                    $this->doc .= '<div class="fn">';
13816cc7ed7SAnika Henke                    $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
13929bfcd16SAndreas Gohr                    $this->doc .= $id.')</a></sup> '.DOKU_LF;
140d74aace9Schris
141d74aace9Schris                    // get any other footnotes that use the same markup
142d74aace9Schris                    $alt = array_keys($this->footnotes, "@@FNT$id");
143d74aace9Schris
144d74aace9Schris                    if(count($alt)) {
145d74aace9Schris                        foreach($alt as $ref) {
146d74aace9Schris                            // set anchor and backlink for the other footnotes
14716ec3e37SAndreas Gohr                            $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
14816ec3e37SAndreas Gohr                            $this->doc .= ($ref).')</a></sup> '.DOKU_LF;
149d74aace9Schris                        }
150d74aace9Schris                    }
151d74aace9Schris
152d74aace9Schris                    // add footnote markup and close this footnote
153694afa06SAnika Henke                    $this->doc .= '<div class="content">'.$footnote.'</div>';
154d74aace9Schris                    $this->doc .= '</div>'.DOKU_LF;
155d74aace9Schris                }
1560cecf9d5Sandi            }
157a2d649c4Sandi            $this->doc .= '</div>'.DOKU_LF;
1580cecf9d5Sandi        }
159c5a8fd96SAndreas Gohr
160b8595a66SAndreas Gohr        // Prepare the TOC
161851f2e89SAnika Henke        global $conf;
162851f2e89SAnika Henke        if($this->info['toc'] && is_array($this->toc) && $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']) {
163b8595a66SAndreas Gohr            global $TOC;
164b8595a66SAndreas Gohr            $TOC = $this->toc;
1650cecf9d5Sandi        }
1663e55d035SAndreas Gohr
1673e55d035SAndreas Gohr        // make sure there are no empty paragraphs
16827918226Schris        $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
169e41c4da9SAndreas Gohr    }
1700cecf9d5Sandi
1713dd5c225SAndreas Gohr    /**
1723dd5c225SAndreas Gohr     * Add an item to the TOC
1733dd5c225SAndreas Gohr     *
1743dd5c225SAndreas Gohr     * @param string $id       the hash link
1753dd5c225SAndreas Gohr     * @param string $text     the text to display
1763dd5c225SAndreas Gohr     * @param int    $level    the nesting level
1773dd5c225SAndreas Gohr     */
178e7856beaSchris    function toc_additem($id, $text, $level) {
179af587fa8Sandi        global $conf;
180af587fa8Sandi
181c5a8fd96SAndreas Gohr        //handle TOC
182c5a8fd96SAndreas Gohr        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
1837d91652aSAndreas Gohr            $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
184c5a8fd96SAndreas Gohr        }
185e7856beaSchris    }
186e7856beaSchris
1873dd5c225SAndreas Gohr    /**
1883dd5c225SAndreas Gohr     * Render a heading
1893dd5c225SAndreas Gohr     *
1903dd5c225SAndreas Gohr     * @param string $text  the text to display
1913dd5c225SAndreas Gohr     * @param int    $level header level
1923dd5c225SAndreas Gohr     * @param int    $pos   byte position in the original source
1933dd5c225SAndreas Gohr     */
194e7856beaSchris    function header($text, $level, $pos) {
19590df9a4dSAdrian Lang        global $conf;
19690df9a4dSAdrian Lang
197f515db7fSAndreas Gohr        if(blank($text)) return; //skip empty headlines
198e7856beaSchris
199e7856beaSchris        $hid = $this->_headerToLink($text, true);
200e7856beaSchris
201e7856beaSchris        //only add items within configured levels
202e7856beaSchris        $this->toc_additem($hid, $text, $level);
203c5a8fd96SAndreas Gohr
20491459163SAnika Henke        // adjust $node to reflect hierarchy of levels
20591459163SAnika Henke        $this->node[$level - 1]++;
20691459163SAnika Henke        if($level < $this->lastlevel) {
20791459163SAnika Henke            for($i = 0; $i < $this->lastlevel - $level; $i++) {
20891459163SAnika Henke                $this->node[$this->lastlevel - $i - 1] = 0;
20991459163SAnika Henke            }
21091459163SAnika Henke        }
21191459163SAnika Henke        $this->lastlevel = $level;
21291459163SAnika Henke
21390df9a4dSAdrian Lang        if($level <= $conf['maxseclevel'] &&
21490df9a4dSAdrian Lang            count($this->sectionedits) > 0 &&
2153dd5c225SAndreas Gohr            $this->sectionedits[count($this->sectionedits) - 1][2] === 'section'
2163dd5c225SAndreas Gohr        ) {
2176c1f778cSAdrian Lang            $this->finishSectionEdit($pos - 1);
21890df9a4dSAdrian Lang        }
21990df9a4dSAdrian Lang
220c5a8fd96SAndreas Gohr        // write the header
22190df9a4dSAdrian Lang        $this->doc .= DOKU_LF.'<h'.$level;
22290df9a4dSAdrian Lang        if($level <= $conf['maxseclevel']) {
223*2571786cSLarsDW223            $this->doc .= ' class="'.$this->startSectionEdit($pos, 'section', $text, $hid).'"';
22490df9a4dSAdrian Lang        }
22516cc7ed7SAnika Henke        $this->doc .= ' id="'.$hid.'">';
226a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
22716cc7ed7SAnika Henke        $this->doc .= "</h$level>".DOKU_LF;
2280cecf9d5Sandi    }
2290cecf9d5Sandi
2303dd5c225SAndreas Gohr    /**
2313dd5c225SAndreas Gohr     * Open a new section
2323dd5c225SAndreas Gohr     *
2333dd5c225SAndreas Gohr     * @param int $level section level (as determined by the previous header)
2343dd5c225SAndreas Gohr     */
2350cecf9d5Sandi    function section_open($level) {
2369864e7b1SAdrian Lang        $this->doc .= '<div class="level'.$level.'">'.DOKU_LF;
2370cecf9d5Sandi    }
2380cecf9d5Sandi
2393dd5c225SAndreas Gohr    /**
2403dd5c225SAndreas Gohr     * Close the current section
2413dd5c225SAndreas Gohr     */
2420cecf9d5Sandi    function section_close() {
243a2d649c4Sandi        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
2440cecf9d5Sandi    }
2450cecf9d5Sandi
2463dd5c225SAndreas Gohr    /**
2473dd5c225SAndreas Gohr     * Render plain text data
2483dd5c225SAndreas Gohr     *
2493dd5c225SAndreas Gohr     * @param $text
2503dd5c225SAndreas Gohr     */
2510cecf9d5Sandi    function cdata($text) {
252a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
2530cecf9d5Sandi    }
2540cecf9d5Sandi
2553dd5c225SAndreas Gohr    /**
2563dd5c225SAndreas Gohr     * Open a paragraph
2573dd5c225SAndreas Gohr     */
2580cecf9d5Sandi    function p_open() {
25959869a4bSAnika Henke        $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
2600cecf9d5Sandi    }
2610cecf9d5Sandi
2623dd5c225SAndreas Gohr    /**
2633dd5c225SAndreas Gohr     * Close a paragraph
2643dd5c225SAndreas Gohr     */
2650cecf9d5Sandi    function p_close() {
26659869a4bSAnika Henke        $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
2670cecf9d5Sandi    }
2680cecf9d5Sandi
2693dd5c225SAndreas Gohr    /**
2703dd5c225SAndreas Gohr     * Create a line break
2713dd5c225SAndreas Gohr     */
2720cecf9d5Sandi    function linebreak() {
273a2d649c4Sandi        $this->doc .= '<br/>'.DOKU_LF;
2740cecf9d5Sandi    }
2750cecf9d5Sandi
2763dd5c225SAndreas Gohr    /**
2773dd5c225SAndreas Gohr     * Create a horizontal line
2783dd5c225SAndreas Gohr     */
2790cecf9d5Sandi    function hr() {
2804beabca9SAnika Henke        $this->doc .= '<hr />'.DOKU_LF;
2810cecf9d5Sandi    }
2820cecf9d5Sandi
2833dd5c225SAndreas Gohr    /**
2843dd5c225SAndreas Gohr     * Start strong (bold) formatting
2853dd5c225SAndreas Gohr     */
2860cecf9d5Sandi    function strong_open() {
287a2d649c4Sandi        $this->doc .= '<strong>';
2880cecf9d5Sandi    }
2890cecf9d5Sandi
2903dd5c225SAndreas Gohr    /**
2913dd5c225SAndreas Gohr     * Stop strong (bold) formatting
2923dd5c225SAndreas Gohr     */
2930cecf9d5Sandi    function strong_close() {
294a2d649c4Sandi        $this->doc .= '</strong>';
2950cecf9d5Sandi    }
2960cecf9d5Sandi
2973dd5c225SAndreas Gohr    /**
2983dd5c225SAndreas Gohr     * Start emphasis (italics) formatting
2993dd5c225SAndreas Gohr     */
3000cecf9d5Sandi    function emphasis_open() {
301a2d649c4Sandi        $this->doc .= '<em>';
3020cecf9d5Sandi    }
3030cecf9d5Sandi
3043dd5c225SAndreas Gohr    /**
3053dd5c225SAndreas Gohr     * Stop emphasis (italics) formatting
3063dd5c225SAndreas Gohr     */
3070cecf9d5Sandi    function emphasis_close() {
308a2d649c4Sandi        $this->doc .= '</em>';
3090cecf9d5Sandi    }
3100cecf9d5Sandi
3113dd5c225SAndreas Gohr    /**
3123dd5c225SAndreas Gohr     * Start underline formatting
3133dd5c225SAndreas Gohr     */
3140cecf9d5Sandi    function underline_open() {
31502e51121SAnika Henke        $this->doc .= '<em class="u">';
3160cecf9d5Sandi    }
3170cecf9d5Sandi
3183dd5c225SAndreas Gohr    /**
3193dd5c225SAndreas Gohr     * Stop underline formatting
3203dd5c225SAndreas Gohr     */
3210cecf9d5Sandi    function underline_close() {
32202e51121SAnika Henke        $this->doc .= '</em>';
3230cecf9d5Sandi    }
3240cecf9d5Sandi
3253dd5c225SAndreas Gohr    /**
3263dd5c225SAndreas Gohr     * Start monospace formatting
3273dd5c225SAndreas Gohr     */
3280cecf9d5Sandi    function monospace_open() {
329a2d649c4Sandi        $this->doc .= '<code>';
3300cecf9d5Sandi    }
3310cecf9d5Sandi
3323dd5c225SAndreas Gohr    /**
3333dd5c225SAndreas Gohr     * Stop monospace formatting
3343dd5c225SAndreas Gohr     */
3350cecf9d5Sandi    function monospace_close() {
336a2d649c4Sandi        $this->doc .= '</code>';
3370cecf9d5Sandi    }
3380cecf9d5Sandi
3393dd5c225SAndreas Gohr    /**
3403dd5c225SAndreas Gohr     * Start a subscript
3413dd5c225SAndreas Gohr     */
3420cecf9d5Sandi    function subscript_open() {
343a2d649c4Sandi        $this->doc .= '<sub>';
3440cecf9d5Sandi    }
3450cecf9d5Sandi
3463dd5c225SAndreas Gohr    /**
3473dd5c225SAndreas Gohr     * Stop a subscript
3483dd5c225SAndreas Gohr     */
3490cecf9d5Sandi    function subscript_close() {
350a2d649c4Sandi        $this->doc .= '</sub>';
3510cecf9d5Sandi    }
3520cecf9d5Sandi
3533dd5c225SAndreas Gohr    /**
3543dd5c225SAndreas Gohr     * Start a superscript
3553dd5c225SAndreas Gohr     */
3560cecf9d5Sandi    function superscript_open() {
357a2d649c4Sandi        $this->doc .= '<sup>';
3580cecf9d5Sandi    }
3590cecf9d5Sandi
3603dd5c225SAndreas Gohr    /**
3613dd5c225SAndreas Gohr     * Stop a superscript
3623dd5c225SAndreas Gohr     */
3630cecf9d5Sandi    function superscript_close() {
364a2d649c4Sandi        $this->doc .= '</sup>';
3650cecf9d5Sandi    }
3660cecf9d5Sandi
3673dd5c225SAndreas Gohr    /**
3683dd5c225SAndreas Gohr     * Start deleted (strike-through) formatting
3693dd5c225SAndreas Gohr     */
3700cecf9d5Sandi    function deleted_open() {
371a2d649c4Sandi        $this->doc .= '<del>';
3720cecf9d5Sandi    }
3730cecf9d5Sandi
3743dd5c225SAndreas Gohr    /**
3753dd5c225SAndreas Gohr     * Stop deleted (strike-through) formatting
3763dd5c225SAndreas Gohr     */
3770cecf9d5Sandi    function deleted_close() {
378a2d649c4Sandi        $this->doc .= '</del>';
3790cecf9d5Sandi    }
3800cecf9d5Sandi
3813fd0b676Sandi    /**
3823fd0b676Sandi     * Callback for footnote start syntax
3833fd0b676Sandi     *
3843fd0b676Sandi     * All following content will go to the footnote instead of
385d74aace9Schris     * the document. To achieve this the previous rendered content
3863fd0b676Sandi     * is moved to $store and $doc is cleared
3873fd0b676Sandi     *
3883fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
3893fd0b676Sandi     */
3900cecf9d5Sandi    function footnote_open() {
3917764a90aSandi
3927764a90aSandi        // move current content to store and record footnote
3937764a90aSandi        $this->store = $this->doc;
3947764a90aSandi        $this->doc   = '';
3950cecf9d5Sandi    }
3960cecf9d5Sandi
3973fd0b676Sandi    /**
3983fd0b676Sandi     * Callback for footnote end syntax
3993fd0b676Sandi     *
4003fd0b676Sandi     * All rendered content is moved to the $footnotes array and the old
4013fd0b676Sandi     * content is restored from $store again
4023fd0b676Sandi     *
4033fd0b676Sandi     * @author Andreas Gohr
4043fd0b676Sandi     */
4050cecf9d5Sandi    function footnote_close() {
40616ec3e37SAndreas Gohr        /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
40716ec3e37SAndreas Gohr        static $fnid = 0;
40816ec3e37SAndreas Gohr        // assign new footnote id (we start at 1)
40916ec3e37SAndreas Gohr        $fnid++;
4107764a90aSandi
411d74aace9Schris        // recover footnote into the stack and restore old content
412d74aace9Schris        $footnote    = $this->doc;
4137764a90aSandi        $this->doc   = $this->store;
4147764a90aSandi        $this->store = '';
415d74aace9Schris
416d74aace9Schris        // check to see if this footnote has been seen before
417d74aace9Schris        $i = array_search($footnote, $this->footnotes);
418d74aace9Schris
419d74aace9Schris        if($i === false) {
420d74aace9Schris            // its a new footnote, add it to the $footnotes array
42116ec3e37SAndreas Gohr            $this->footnotes[$fnid] = $footnote;
422d74aace9Schris        } else {
42316ec3e37SAndreas Gohr            // seen this one before, save a placeholder
42416ec3e37SAndreas Gohr            $this->footnotes[$fnid] = "@@FNT".($i);
425d74aace9Schris        }
426d74aace9Schris
4276b379cbfSAndreas Gohr        // output the footnote reference and link
42816ec3e37SAndreas Gohr        $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
4290cecf9d5Sandi    }
4300cecf9d5Sandi
4313dd5c225SAndreas Gohr    /**
4323dd5c225SAndreas Gohr     * Open an unordered list
4330c4c0281SGerrit Uitslag     *
4347d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
4353dd5c225SAndreas Gohr     */
4360c4c0281SGerrit Uitslag    function listu_open($classes = null) {
4370c4c0281SGerrit Uitslag        $class = '';
4380c4c0281SGerrit Uitslag        if($classes !== null) {
4392e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
4400c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
4410c4c0281SGerrit Uitslag        }
4420c4c0281SGerrit Uitslag        $this->doc .= "<ul$class>".DOKU_LF;
4430cecf9d5Sandi    }
4440cecf9d5Sandi
4453dd5c225SAndreas Gohr    /**
4463dd5c225SAndreas Gohr     * Close an unordered list
4473dd5c225SAndreas Gohr     */
4480cecf9d5Sandi    function listu_close() {
449a2d649c4Sandi        $this->doc .= '</ul>'.DOKU_LF;
4500cecf9d5Sandi    }
4510cecf9d5Sandi
4523dd5c225SAndreas Gohr    /**
4533dd5c225SAndreas Gohr     * Open an ordered list
4540c4c0281SGerrit Uitslag     *
4557d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
4563dd5c225SAndreas Gohr     */
4570c4c0281SGerrit Uitslag    function listo_open($classes = null) {
4580c4c0281SGerrit Uitslag        $class = '';
4590c4c0281SGerrit Uitslag        if($classes !== null) {
4602e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
4610c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
4620c4c0281SGerrit Uitslag        }
4630c4c0281SGerrit Uitslag        $this->doc .= "<ol$class>".DOKU_LF;
4640cecf9d5Sandi    }
4650cecf9d5Sandi
4663dd5c225SAndreas Gohr    /**
4673dd5c225SAndreas Gohr     * Close an ordered list
4683dd5c225SAndreas Gohr     */
4690cecf9d5Sandi    function listo_close() {
470a2d649c4Sandi        $this->doc .= '</ol>'.DOKU_LF;
4710cecf9d5Sandi    }
4720cecf9d5Sandi
4733dd5c225SAndreas Gohr    /**
4743dd5c225SAndreas Gohr     * Open a list item
4753dd5c225SAndreas Gohr     *
4763dd5c225SAndreas Gohr     * @param int $level the nesting level
477e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
4783dd5c225SAndreas Gohr     */
479e3a24861SChristopher Smith    function listitem_open($level, $node=false) {
480e3a24861SChristopher Smith        $branching = $node ? ' node' : '';
481e3a24861SChristopher Smith        $this->doc .= '<li class="level'.$level.$branching.'">';
4820cecf9d5Sandi    }
4830cecf9d5Sandi
4843dd5c225SAndreas Gohr    /**
4853dd5c225SAndreas Gohr     * Close a list item
4863dd5c225SAndreas Gohr     */
4870cecf9d5Sandi    function listitem_close() {
488a2d649c4Sandi        $this->doc .= '</li>'.DOKU_LF;
4890cecf9d5Sandi    }
4900cecf9d5Sandi
4913dd5c225SAndreas Gohr    /**
4923dd5c225SAndreas Gohr     * Start the content of a list item
4933dd5c225SAndreas Gohr     */
4940cecf9d5Sandi    function listcontent_open() {
49590db23d7Schris        $this->doc .= '<div class="li">';
4960cecf9d5Sandi    }
4970cecf9d5Sandi
4983dd5c225SAndreas Gohr    /**
4993dd5c225SAndreas Gohr     * Stop the content of a list item
5003dd5c225SAndreas Gohr     */
5010cecf9d5Sandi    function listcontent_close() {
50259869a4bSAnika Henke        $this->doc .= '</div>'.DOKU_LF;
5030cecf9d5Sandi    }
5040cecf9d5Sandi
5053dd5c225SAndreas Gohr    /**
5063dd5c225SAndreas Gohr     * Output unformatted $text
5073dd5c225SAndreas Gohr     *
5083dd5c225SAndreas Gohr     * Defaults to $this->cdata()
5093dd5c225SAndreas Gohr     *
5103dd5c225SAndreas Gohr     * @param string $text
5113dd5c225SAndreas Gohr     */
5120cecf9d5Sandi    function unformatted($text) {
513a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
5140cecf9d5Sandi    }
5150cecf9d5Sandi
5160cecf9d5Sandi    /**
5173fd0b676Sandi     * Execute PHP code if allowed
5183fd0b676Sandi     *
519d9764001SMichael Hamann     * @param  string $text      PHP code that is either executed or printed
5205d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['phpok'] is okff
5215d568b99SChris Smith     *
5223fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5230cecf9d5Sandi     */
5245d568b99SChris Smith    function php($text, $wrapper = 'code') {
52535a56260SChris Smith        global $conf;
52635a56260SChris Smith
527d86d5af0SChris Smith        if($conf['phpok']) {
528bad0b545Sandi            ob_start();
5294de671bcSandi            eval($text);
5303fd0b676Sandi            $this->doc .= ob_get_contents();
531bad0b545Sandi            ob_end_clean();
532d86d5af0SChris Smith        } else {
5335d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'php', $wrapper);
534d86d5af0SChris Smith        }
5350cecf9d5Sandi    }
5360cecf9d5Sandi
5373dd5c225SAndreas Gohr    /**
5383dd5c225SAndreas Gohr     * Output block level PHP code
5393dd5c225SAndreas Gohr     *
5403dd5c225SAndreas Gohr     * If $conf['phpok'] is true this should evaluate the given code and append the result
5413dd5c225SAndreas Gohr     * to $doc
5423dd5c225SAndreas Gohr     *
5433dd5c225SAndreas Gohr     * @param string $text The PHP code
5443dd5c225SAndreas Gohr     */
54507f89c3cSAnika Henke    function phpblock($text) {
5465d568b99SChris Smith        $this->php($text, 'pre');
54707f89c3cSAnika Henke    }
54807f89c3cSAnika Henke
5490cecf9d5Sandi    /**
5503fd0b676Sandi     * Insert HTML if allowed
5513fd0b676Sandi     *
552d9764001SMichael Hamann     * @param  string $text      html text
5535d568b99SChris Smith     * @param  string $wrapper   html element to wrap result if $conf['htmlok'] is okff
5545d568b99SChris Smith     *
5553fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
5560cecf9d5Sandi     */
5575d568b99SChris Smith    function html($text, $wrapper = 'code') {
55835a56260SChris Smith        global $conf;
55935a56260SChris Smith
560d86d5af0SChris Smith        if($conf['htmlok']) {
561a2d649c4Sandi            $this->doc .= $text;
562d86d5af0SChris Smith        } else {
5635d568b99SChris Smith            $this->doc .= p_xhtml_cached_geshi($text, 'html4strict', $wrapper);
564d86d5af0SChris Smith        }
5654de671bcSandi    }
5660cecf9d5Sandi
5673dd5c225SAndreas Gohr    /**
5683dd5c225SAndreas Gohr     * Output raw block-level HTML
5693dd5c225SAndreas Gohr     *
5703dd5c225SAndreas Gohr     * If $conf['htmlok'] is true this should add the code as is to $doc
5713dd5c225SAndreas Gohr     *
5723dd5c225SAndreas Gohr     * @param string $text The HTML
5733dd5c225SAndreas Gohr     */
57407f89c3cSAnika Henke    function htmlblock($text) {
5755d568b99SChris Smith        $this->html($text, 'pre');
57607f89c3cSAnika Henke    }
57707f89c3cSAnika Henke
5783dd5c225SAndreas Gohr    /**
5793dd5c225SAndreas Gohr     * Start a block quote
5803dd5c225SAndreas Gohr     */
5810cecf9d5Sandi    function quote_open() {
58296331712SAnika Henke        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
5830cecf9d5Sandi    }
5840cecf9d5Sandi
5853dd5c225SAndreas Gohr    /**
5863dd5c225SAndreas Gohr     * Stop a block quote
5873dd5c225SAndreas Gohr     */
5880cecf9d5Sandi    function quote_close() {
58996331712SAnika Henke        $this->doc .= '</div></blockquote>'.DOKU_LF;
5900cecf9d5Sandi    }
5910cecf9d5Sandi
5923dd5c225SAndreas Gohr    /**
5933dd5c225SAndreas Gohr     * Output preformatted text
5943dd5c225SAndreas Gohr     *
5953dd5c225SAndreas Gohr     * @param string $text
5963dd5c225SAndreas Gohr     */
5973d491f75SAndreas Gohr    function preformatted($text) {
598c9250713SAnika Henke        $this->doc .= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF;
5993d491f75SAndreas Gohr    }
6003d491f75SAndreas Gohr
6013dd5c225SAndreas Gohr    /**
6023dd5c225SAndreas Gohr     * Display text as file content, optionally syntax highlighted
6033dd5c225SAndreas Gohr     *
6043dd5c225SAndreas Gohr     * @param string $text     text to show
6053dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6063dd5c225SAndreas Gohr     * @param string $filename file path label
6073dd5c225SAndreas Gohr     */
6083d491f75SAndreas Gohr    function file($text, $language = null, $filename = null) {
6093d491f75SAndreas Gohr        $this->_highlight('file', $text, $language, $filename);
6103d491f75SAndreas Gohr    }
6113d491f75SAndreas Gohr
6123dd5c225SAndreas Gohr    /**
6133dd5c225SAndreas Gohr     * Display text as code content, optionally syntax highlighted
6143dd5c225SAndreas Gohr     *
6153dd5c225SAndreas Gohr     * @param string $text     text to show
6163dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6173dd5c225SAndreas Gohr     * @param string $filename file path label
6183dd5c225SAndreas Gohr     */
6193d491f75SAndreas Gohr    function code($text, $language = null, $filename = null) {
6203d491f75SAndreas Gohr        $this->_highlight('code', $text, $language, $filename);
6213d491f75SAndreas Gohr    }
6223d491f75SAndreas Gohr
6230cecf9d5Sandi    /**
6243d491f75SAndreas Gohr     * Use GeSHi to highlight language syntax in code and file blocks
6253fd0b676Sandi     *
6263fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
6273dd5c225SAndreas Gohr     * @param string $type     code|file
6283dd5c225SAndreas Gohr     * @param string $text     text to show
6293dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6303dd5c225SAndreas Gohr     * @param string $filename file path label
6310cecf9d5Sandi     */
6323d491f75SAndreas Gohr    function _highlight($type, $text, $language = null, $filename = null) {
6333d491f75SAndreas Gohr        global $ID;
6343d491f75SAndreas Gohr        global $lang;
6353d491f75SAndreas Gohr
6363d491f75SAndreas Gohr        if($filename) {
637190c56e8SAndreas Gohr            // add icon
63827bf7924STom N Harris            list($ext) = mimetype($filename, false);
639190c56e8SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
640190c56e8SAndreas Gohr            $class = 'mediafile mf_'.$class;
641190c56e8SAndreas Gohr
6423d491f75SAndreas Gohr            $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
643190c56e8SAndreas Gohr            $this->doc .= '<dt><a href="'.exportlink($ID, 'code', array('codeblock' => $this->_codeblock)).'" title="'.$lang['download'].'" class="'.$class.'">';
6443d491f75SAndreas Gohr            $this->doc .= hsc($filename);
6453d491f75SAndreas Gohr            $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
6463d491f75SAndreas Gohr        }
6470cecf9d5Sandi
648d43aac1cSGina Haeussge        if($text{0} == "\n") {
649d43aac1cSGina Haeussge            $text = substr($text, 1);
650d43aac1cSGina Haeussge        }
651d43aac1cSGina Haeussge        if(substr($text, -1) == "\n") {
652d43aac1cSGina Haeussge            $text = substr($text, 0, -1);
653d43aac1cSGina Haeussge        }
654d43aac1cSGina Haeussge
6550cecf9d5Sandi        if(is_null($language)) {
6563d491f75SAndreas Gohr            $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
6570cecf9d5Sandi        } else {
6583d491f75SAndreas Gohr            $class = 'code'; //we always need the code class to make the syntax highlighting apply
6593d491f75SAndreas Gohr            if($type != 'code') $class .= ' '.$type;
6603d491f75SAndreas Gohr
6613d491f75SAndreas Gohr            $this->doc .= "<pre class=\"$class $language\">".p_xhtml_cached_geshi($text, $language, '').'</pre>'.DOKU_LF;
6620cecf9d5Sandi        }
6633d491f75SAndreas Gohr
6643d491f75SAndreas Gohr        if($filename) {
6653d491f75SAndreas Gohr            $this->doc .= '</dd></dl>'.DOKU_LF;
6663d491f75SAndreas Gohr        }
6673d491f75SAndreas Gohr
6683d491f75SAndreas Gohr        $this->_codeblock++;
6690cecf9d5Sandi    }
6700cecf9d5Sandi
6713dd5c225SAndreas Gohr    /**
6723dd5c225SAndreas Gohr     * Format an acronym
6733dd5c225SAndreas Gohr     *
6743dd5c225SAndreas Gohr     * Uses $this->acronyms
6753dd5c225SAndreas Gohr     *
6763dd5c225SAndreas Gohr     * @param string $acronym
6773dd5c225SAndreas Gohr     */
6780cecf9d5Sandi    function acronym($acronym) {
6790cecf9d5Sandi
6800cecf9d5Sandi        if(array_key_exists($acronym, $this->acronyms)) {
6810cecf9d5Sandi
682433bef32Sandi            $title = $this->_xmlEntities($this->acronyms[$acronym]);
6830cecf9d5Sandi
684940db3a3SAnika Henke            $this->doc .= '<abbr title="'.$title
685940db3a3SAnika Henke                .'">'.$this->_xmlEntities($acronym).'</abbr>';
6860cecf9d5Sandi
6870cecf9d5Sandi        } else {
688a2d649c4Sandi            $this->doc .= $this->_xmlEntities($acronym);
6890cecf9d5Sandi        }
6900cecf9d5Sandi    }
6910cecf9d5Sandi
6923dd5c225SAndreas Gohr    /**
6933dd5c225SAndreas Gohr     * Format a smiley
6943dd5c225SAndreas Gohr     *
6953dd5c225SAndreas Gohr     * Uses $this->smiley
6963dd5c225SAndreas Gohr     *
6973dd5c225SAndreas Gohr     * @param string $smiley
6983dd5c225SAndreas Gohr     */
6990cecf9d5Sandi    function smiley($smiley) {
7000cecf9d5Sandi        if(array_key_exists($smiley, $this->smileys)) {
701f62ea8a1Sandi            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
7028e38227fSAnika Henke                '" class="icon" alt="'.
703433bef32Sandi                $this->_xmlEntities($smiley).'" />';
7040cecf9d5Sandi        } else {
705a2d649c4Sandi            $this->doc .= $this->_xmlEntities($smiley);
7060cecf9d5Sandi        }
7070cecf9d5Sandi    }
7080cecf9d5Sandi
7093dd5c225SAndreas Gohr    /**
7103dd5c225SAndreas Gohr     * Format an entity
7113dd5c225SAndreas Gohr     *
7123dd5c225SAndreas Gohr     * Entities are basically small text replacements
7133dd5c225SAndreas Gohr     *
7143dd5c225SAndreas Gohr     * Uses $this->entities
7153dd5c225SAndreas Gohr     *
7163dd5c225SAndreas Gohr     * @param string $entity
7174de671bcSandi     */
7180cecf9d5Sandi    function entity($entity) {
7190cecf9d5Sandi        if(array_key_exists($entity, $this->entities)) {
720a2d649c4Sandi            $this->doc .= $this->entities[$entity];
7210cecf9d5Sandi        } else {
722a2d649c4Sandi            $this->doc .= $this->_xmlEntities($entity);
7230cecf9d5Sandi        }
7240cecf9d5Sandi    }
7250cecf9d5Sandi
7263dd5c225SAndreas Gohr    /**
7273dd5c225SAndreas Gohr     * Typographically format a multiply sign
7283dd5c225SAndreas Gohr     *
7293dd5c225SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
7303dd5c225SAndreas Gohr     *
7313dd5c225SAndreas Gohr     * @param string|int $x first value
7323dd5c225SAndreas Gohr     * @param string|int $y second value
7333dd5c225SAndreas Gohr     */
7340cecf9d5Sandi    function multiplyentity($x, $y) {
735a2d649c4Sandi        $this->doc .= "$x&times;$y";
7360cecf9d5Sandi    }
7370cecf9d5Sandi
7383dd5c225SAndreas Gohr    /**
7393dd5c225SAndreas Gohr     * Render an opening single quote char (language specific)
7403dd5c225SAndreas Gohr     */
7410cecf9d5Sandi    function singlequoteopening() {
74271b40da2SAnika Henke        global $lang;
74371b40da2SAnika Henke        $this->doc .= $lang['singlequoteopening'];
7440cecf9d5Sandi    }
7450cecf9d5Sandi
7463dd5c225SAndreas Gohr    /**
7473dd5c225SAndreas Gohr     * Render a closing single quote char (language specific)
7483dd5c225SAndreas Gohr     */
7490cecf9d5Sandi    function singlequoteclosing() {
75071b40da2SAnika Henke        global $lang;
75171b40da2SAnika Henke        $this->doc .= $lang['singlequoteclosing'];
7520cecf9d5Sandi    }
7530cecf9d5Sandi
7543dd5c225SAndreas Gohr    /**
7553dd5c225SAndreas Gohr     * Render an apostrophe char (language specific)
7563dd5c225SAndreas Gohr     */
75757d757d1SAndreas Gohr    function apostrophe() {
75857d757d1SAndreas Gohr        global $lang;
759a8bd192aSAndreas Gohr        $this->doc .= $lang['apostrophe'];
76057d757d1SAndreas Gohr    }
76157d757d1SAndreas Gohr
7623dd5c225SAndreas Gohr    /**
7633dd5c225SAndreas Gohr     * Render an opening double quote char (language specific)
7643dd5c225SAndreas Gohr     */
7650cecf9d5Sandi    function doublequoteopening() {
76671b40da2SAnika Henke        global $lang;
76771b40da2SAnika Henke        $this->doc .= $lang['doublequoteopening'];
7680cecf9d5Sandi    }
7690cecf9d5Sandi
7703dd5c225SAndreas Gohr    /**
7713dd5c225SAndreas Gohr     * Render an closinging double quote char (language specific)
7723dd5c225SAndreas Gohr     */
7730cecf9d5Sandi    function doublequoteclosing() {
77471b40da2SAnika Henke        global $lang;
77571b40da2SAnika Henke        $this->doc .= $lang['doublequoteclosing'];
7760cecf9d5Sandi    }
7770cecf9d5Sandi
7780cecf9d5Sandi    /**
7793dd5c225SAndreas Gohr     * Render a CamelCase link
7803dd5c225SAndreas Gohr     *
7813dd5c225SAndreas Gohr     * @param string $link       The link name
782122f2d46SAndreas Böhler     * @param bool   $returnonly whether to return html or write to doc attribute
7830c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
7840c4c0281SGerrit Uitslag     *
7853dd5c225SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
7860cecf9d5Sandi     */
787122f2d46SAndreas Böhler    function camelcaselink($link, $returnonly = false) {
788122f2d46SAndreas Böhler        if($returnonly) {
789122f2d46SAndreas Böhler          return $this->internallink($link, $link, null, true);
790122f2d46SAndreas Böhler        } else {
79111d0aa47Sandi          $this->internallink($link, $link);
7920cecf9d5Sandi        }
793122f2d46SAndreas Böhler    }
7940cecf9d5Sandi
7953dd5c225SAndreas Gohr    /**
7963dd5c225SAndreas Gohr     * Render a page local link
7973dd5c225SAndreas Gohr     *
7983dd5c225SAndreas Gohr     * @param string $hash       hash link identifier
7993dd5c225SAndreas Gohr     * @param string $name       name for the link
800122f2d46SAndreas Böhler     * @param bool   $returnonly whether to return html or write to doc attribute
8010c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
8023dd5c225SAndreas Gohr     */
803122f2d46SAndreas Böhler    function locallink($hash, $name = null, $returnonly = false) {
8040b7c14c2Sandi        global $ID;
8050b7c14c2Sandi        $name  = $this->_getLinkTitle($name, $hash, $isImage);
8060b7c14c2Sandi        $hash  = $this->_headerToLink($hash);
807e260f93bSAnika Henke        $title = $ID.' ↵';
808122f2d46SAndreas Böhler
809122f2d46SAndreas Böhler        $doc = '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
810122f2d46SAndreas Böhler        $doc .= $name;
811122f2d46SAndreas Böhler        $doc .= '</a>';
812122f2d46SAndreas Böhler
813122f2d46SAndreas Böhler        if($returnonly) {
814122f2d46SAndreas Böhler          return $doc;
815122f2d46SAndreas Böhler        } else {
816122f2d46SAndreas Böhler          $this->doc .= $doc;
817122f2d46SAndreas Böhler        }
8180b7c14c2Sandi    }
8190b7c14c2Sandi
820cffcc403Sandi    /**
8213fd0b676Sandi     * Render an internal Wiki Link
8223fd0b676Sandi     *
823fe9ec250SChris Smith     * $search,$returnonly & $linktype are not for the renderer but are used
824cffcc403Sandi     * elsewhere - no need to implement them in other renderers
8253fd0b676Sandi     *
8263dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
827f23eef27SGerrit Uitslag     * @param string      $id         pageid
828f23eef27SGerrit Uitslag     * @param string|null $name       link name
829f23eef27SGerrit Uitslag     * @param string|null $search     adds search url param
830f23eef27SGerrit Uitslag     * @param bool        $returnonly whether to return html or write to doc attribute
831f23eef27SGerrit Uitslag     * @param string      $linktype   type to set use of headings
832f23eef27SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
833cffcc403Sandi     */
8340ea51e63SMatt Perry    function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
835ba11bd29Sandi        global $conf;
83637e34a5eSandi        global $ID;
837c4dda6afSAnika Henke        global $INFO;
83844653a53SAdrian Lang
8393d5e07d9SAdrian Lang        $params = '';
8403d5e07d9SAdrian Lang        $parts  = explode('?', $id, 2);
8413d5e07d9SAdrian Lang        if(count($parts) === 2) {
8423d5e07d9SAdrian Lang            $id     = $parts[0];
8433d5e07d9SAdrian Lang            $params = $parts[1];
84444653a53SAdrian Lang        }
84544653a53SAdrian Lang
846fda14ffcSIzidor Matušov        // For empty $id we need to know the current $ID
847fda14ffcSIzidor Matušov        // We need this check because _simpleTitle needs
848fda14ffcSIzidor Matušov        // correct $id and resolve_pageid() use cleanID($id)
849fda14ffcSIzidor Matušov        // (some things could be lost)
850fda14ffcSIzidor Matušov        if($id === '') {
851fda14ffcSIzidor Matušov            $id = $ID;
852fda14ffcSIzidor Matušov        }
853fda14ffcSIzidor Matušov
8540339c872Sjan        // default name is based on $id as given
8550339c872Sjan        $default = $this->_simpleTitle($id);
856ad32e47eSAndreas Gohr
8570339c872Sjan        // now first resolve and clean up the $id
85890bee600Slisps        resolve_pageid(getNS($ID), $id, $exists, $this->date_at, true);
859fda14ffcSIzidor Matušov
86059bc3b48SGerrit Uitslag        $link = array();
861fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
8620e1c636eSandi        if(!$isImage) {
8630e1c636eSandi            if($exists) {
864ba11bd29Sandi                $class = 'wikilink1';
8650cecf9d5Sandi            } else {
866ba11bd29Sandi                $class       = 'wikilink2';
86744a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
8680cecf9d5Sandi            }
8690cecf9d5Sandi        } else {
870ba11bd29Sandi            $class = 'media';
8710cecf9d5Sandi        }
8720cecf9d5Sandi
873a1685bedSandi        //keep hash anchor
8746d2af55dSChristopher Smith        @list($id, $hash) = explode('#', $id, 2);
875943dedc6SAndreas Gohr        if(!empty($hash)) $hash = $this->_headerToLink($hash);
876a1685bedSandi
877ba11bd29Sandi        //prepare for formating
878ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
879ba11bd29Sandi        $link['style']  = '';
880ba11bd29Sandi        $link['pre']    = '';
881ba11bd29Sandi        $link['suf']    = '';
88240eb54bbSjan        // highlight link to current page
883c4dda6afSAnika Henke        if($id == $INFO['id']) {
88492795d04Sandi            $link['pre'] = '<span class="curid">';
88592795d04Sandi            $link['suf'] = '</span>';
88640eb54bbSjan        }
8875e163278SAndreas Gohr        $link['more']   = '';
888ba11bd29Sandi        $link['class']  = $class;
8895c2eed9aSlisps        if($this->date_at) {
8905c2eed9aSlisps            $params['at'] = $this->date_at;
8915c2eed9aSlisps        }
89244653a53SAdrian Lang        $link['url']    = wl($id, $params);
893ba11bd29Sandi        $link['name']   = $name;
894ba11bd29Sandi        $link['title']  = $id;
895723d78dbSandi        //add search string
896723d78dbSandi        if($search) {
897546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
898546d3a99SAndreas Gohr            if(is_array($search)) {
899546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
900546d3a99SAndreas Gohr                $link['url'] .= 's[]='.join('&amp;s[]=', $search);
901546d3a99SAndreas Gohr            } else {
902546d3a99SAndreas Gohr                $link['url'] .= 's='.rawurlencode($search);
903546d3a99SAndreas Gohr            }
904723d78dbSandi        }
905723d78dbSandi
906a1685bedSandi        //keep hash
907a1685bedSandi        if($hash) $link['url'] .= '#'.$hash;
908a1685bedSandi
909ba11bd29Sandi        //output formatted
910cffcc403Sandi        if($returnonly) {
911cffcc403Sandi            return $this->_formatLink($link);
912cffcc403Sandi        } else {
913a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
9140cecf9d5Sandi        }
915cffcc403Sandi    }
9160cecf9d5Sandi
9173dd5c225SAndreas Gohr    /**
9183dd5c225SAndreas Gohr     * Render an external link
9193dd5c225SAndreas Gohr     *
9203dd5c225SAndreas Gohr     * @param string       $url        full URL with scheme
9213dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
922122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
9230c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
9243dd5c225SAndreas Gohr     */
925122f2d46SAndreas Böhler    function externallink($url, $name = null, $returnonly = false) {
926b625487dSandi        global $conf;
9270cecf9d5Sandi
928433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
9296f0c5dbfSandi
930b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
931b52b1596SAndreas Gohr        if(is_null($this->schemes)) $this->schemes = getSchemes();
932b52b1596SAndreas Gohr        list($scheme) = explode('://', $url);
933b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
934b52b1596SAndreas Gohr        if(!in_array($scheme, $this->schemes)) $url = '';
935b52b1596SAndreas Gohr
936b52b1596SAndreas Gohr        // is there still an URL?
937b52b1596SAndreas Gohr        if(!$url) {
93849cef4fdSAndreas Böhler            if($returnonly) {
93949cef4fdSAndreas Böhler                return $name;
94049cef4fdSAndreas Böhler            } else {
941b52b1596SAndreas Gohr                $this->doc .= $name;
94249cef4fdSAndreas Böhler            }
943b52b1596SAndreas Gohr            return;
944b52b1596SAndreas Gohr        }
945b52b1596SAndreas Gohr
946b52b1596SAndreas Gohr        // set class
9470cecf9d5Sandi        if(!$isImage) {
948b625487dSandi            $class = 'urlextern';
9490cecf9d5Sandi        } else {
950b625487dSandi            $class = 'media';
9510cecf9d5Sandi        }
9520cecf9d5Sandi
953b625487dSandi        //prepare for formating
95459bc3b48SGerrit Uitslag        $link = array();
955b625487dSandi        $link['target'] = $conf['target']['extern'];
956b625487dSandi        $link['style']  = '';
957b625487dSandi        $link['pre']    = '';
958b625487dSandi        $link['suf']    = '';
9595e163278SAndreas Gohr        $link['more']   = '';
960b625487dSandi        $link['class']  = $class;
961b625487dSandi        $link['url']    = $url;
962914045f3SAndreas Gohr        $link['rel']    = '';
963e1c10e4dSchris
964b625487dSandi        $link['name']  = $name;
965433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
966914045f3SAndreas Gohr        if($conf['relnofollow']) $link['rel'] .= ' nofollow';
967914045f3SAndreas Gohr        if($conf['target']['extern']) $link['rel'] .= ' noopener';
9680cecf9d5Sandi
969b625487dSandi        //output formatted
970122f2d46SAndreas Böhler        if($returnonly) {
971122f2d46SAndreas Böhler            return $this->_formatLink($link);
972122f2d46SAndreas Böhler        } else {
973a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
9740cecf9d5Sandi        }
975122f2d46SAndreas Böhler    }
9760cecf9d5Sandi
9770cecf9d5Sandi    /**
9783dd5c225SAndreas Gohr     * Render an interwiki link
9793dd5c225SAndreas Gohr     *
9803dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
9813dd5c225SAndreas Gohr     *
9823dd5c225SAndreas Gohr     * @param string       $match      original link - probably not much use
9833dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
9843dd5c225SAndreas Gohr     * @param string       $wikiName   indentifier (shortcut) for the remote wiki
9853dd5c225SAndreas Gohr     * @param string       $wikiUri    the fragment parsed from the original link
986122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
9870c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
9880cecf9d5Sandi     */
989122f2d46SAndreas Böhler    function interwikilink($match, $name = null, $wikiName, $wikiUri, $returnonly = false) {
990b625487dSandi        global $conf;
9910cecf9d5Sandi
99297a3e4e3Sandi        $link           = array();
99397a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
99497a3e4e3Sandi        $link['pre']    = '';
99597a3e4e3Sandi        $link['suf']    = '';
9965e163278SAndreas Gohr        $link['more']   = '';
997433bef32Sandi        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
998914045f3SAndreas Gohr        $link['rel']    = '';
9990cecf9d5Sandi
100097a3e4e3Sandi        //get interwiki URL
10016496c33fSGerrit Uitslag        $exists = null;
10026496c33fSGerrit Uitslag        $url    = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
10030cecf9d5Sandi
100497a3e4e3Sandi        if(!$isImage) {
10059d2ddea4SAndreas Gohr            $class         = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
10069d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
10071c2d1019SAndreas Gohr        } else {
10081c2d1019SAndreas Gohr            $link['class'] = 'media';
100997a3e4e3Sandi        }
10100cecf9d5Sandi
101197a3e4e3Sandi        //do we stay at the same server? Use local target
10122345e871SGerrit Uitslag        if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
101397a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
101497a3e4e3Sandi        }
10156496c33fSGerrit Uitslag        if($exists !== null && !$isImage) {
10166496c33fSGerrit Uitslag            if($exists) {
10176496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
10186496c33fSGerrit Uitslag            } else {
10196496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
1020914045f3SAndreas Gohr                $link['rel'] .= ' nofollow';
10216496c33fSGerrit Uitslag            }
10226496c33fSGerrit Uitslag        }
1023914045f3SAndreas Gohr        if($conf['target']['interwiki']) $link['rel'] .= ' noopener';
10240cecf9d5Sandi
102597a3e4e3Sandi        $link['url']   = $url;
102697a3e4e3Sandi        $link['title'] = htmlspecialchars($link['url']);
102797a3e4e3Sandi
102897a3e4e3Sandi        //output formatted
1029122f2d46SAndreas Böhler        if($returnonly) {
1030122f2d46SAndreas Böhler            return $this->_formatLink($link);
1031122f2d46SAndreas Böhler        } else {
1032a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
10330cecf9d5Sandi        }
1034122f2d46SAndreas Böhler    }
10350cecf9d5Sandi
10360cecf9d5Sandi    /**
10373dd5c225SAndreas Gohr     * Link to windows share
10383dd5c225SAndreas Gohr     *
10393dd5c225SAndreas Gohr     * @param string       $url        the link
10403dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
1041122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
10420c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10430cecf9d5Sandi     */
1044122f2d46SAndreas Böhler    function windowssharelink($url, $name = null, $returnonly = false) {
10451d47afe1Sandi        global $conf;
10463dd5c225SAndreas Gohr
10471d47afe1Sandi        //simple setup
104859bc3b48SGerrit Uitslag        $link = array();
10491d47afe1Sandi        $link['target'] = $conf['target']['windows'];
10501d47afe1Sandi        $link['pre']    = '';
10511d47afe1Sandi        $link['suf']    = '';
10521d47afe1Sandi        $link['style']  = '';
10530cecf9d5Sandi
1054433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
10550cecf9d5Sandi        if(!$isImage) {
10561d47afe1Sandi            $link['class'] = 'windows';
10570cecf9d5Sandi        } else {
10581d47afe1Sandi            $link['class'] = 'media';
10590cecf9d5Sandi        }
10600cecf9d5Sandi
1061433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
10621d47afe1Sandi        $url           = str_replace('\\', '/', $url);
10631d47afe1Sandi        $url           = 'file:///'.$url;
10641d47afe1Sandi        $link['url']   = $url;
10650cecf9d5Sandi
10661d47afe1Sandi        //output formatted
1067122f2d46SAndreas Böhler        if($returnonly) {
1068122f2d46SAndreas Böhler            return $this->_formatLink($link);
1069122f2d46SAndreas Böhler        } else {
1070a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
10710cecf9d5Sandi        }
1072122f2d46SAndreas Böhler    }
10730cecf9d5Sandi
10743dd5c225SAndreas Gohr    /**
10753dd5c225SAndreas Gohr     * Render a linked E-Mail Address
10763dd5c225SAndreas Gohr     *
10773dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
10783dd5c225SAndreas Gohr     *
10793dd5c225SAndreas Gohr     * @param string       $address    Email-Address
10803dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
1081122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
10820c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10833dd5c225SAndreas Gohr     */
1084122f2d46SAndreas Böhler    function emaillink($address, $name = null, $returnonly = false) {
108571352defSandi        global $conf;
108671352defSandi        //simple setup
108771352defSandi        $link           = array();
108871352defSandi        $link['target'] = '';
108971352defSandi        $link['pre']    = '';
109071352defSandi        $link['suf']    = '';
109171352defSandi        $link['style']  = '';
109271352defSandi        $link['more']   = '';
10930cecf9d5Sandi
1094c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
10950cecf9d5Sandi        if(!$isImage) {
1096be96545cSAnika Henke            $link['class'] = 'mail';
10970cecf9d5Sandi        } else {
1098be96545cSAnika Henke            $link['class'] = 'media';
10990cecf9d5Sandi        }
11000cecf9d5Sandi
110107738714SAndreas Gohr        $address = $this->_xmlEntities($address);
110200a7b5adSEsther Brunner        $address = obfuscate($address);
110300a7b5adSEsther Brunner        $title   = $address;
11048c128049SAndreas Gohr
110571352defSandi        if(empty($name)) {
110600a7b5adSEsther Brunner            $name = $address;
110771352defSandi        }
11080cecf9d5Sandi
1109776b36ecSAndreas Gohr        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1110776b36ecSAndreas Gohr
1111776b36ecSAndreas Gohr        $link['url']   = 'mailto:'.$address;
111271352defSandi        $link['name']  = $name;
111371352defSandi        $link['title'] = $title;
11140cecf9d5Sandi
111571352defSandi        //output formatted
1116122f2d46SAndreas Böhler        if($returnonly) {
1117122f2d46SAndreas Böhler            return $this->_formatLink($link);
1118122f2d46SAndreas Böhler        } else {
1119a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
11200cecf9d5Sandi        }
1121122f2d46SAndreas Böhler    }
11220cecf9d5Sandi
11233dd5c225SAndreas Gohr    /**
11243dd5c225SAndreas Gohr     * Render an internal media file
11253dd5c225SAndreas Gohr     *
11263dd5c225SAndreas Gohr     * @param string $src       media ID
11273dd5c225SAndreas Gohr     * @param string $title     descriptive text
11283dd5c225SAndreas Gohr     * @param string $align     left|center|right
11293dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
11303dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
11313dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
11323dd5c225SAndreas Gohr     * @param string $linking   linkonly|detail|nolink
11333dd5c225SAndreas Gohr     * @param bool   $return    return HTML instead of adding to $doc
11340c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
11353dd5c225SAndreas Gohr     */
11360ea51e63SMatt Perry    function internalmedia($src, $title = null, $align = null, $width = null,
11373dd5c225SAndreas Gohr                           $height = null, $cache = null, $linking = null, $return = false) {
113837e34a5eSandi        global $ID;
113991df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
1140cdb5e961Slisps        resolve_mediaid(getNS($ID), $src, $exists, $this->date_at, true);
11410cecf9d5Sandi
1142d98d4540SBen Coburn        $noLink = false;
11438acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1144b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
11453685f775Sandi
11463dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1147b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
114852dc5eadSlisps            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src)), ($linking == 'direct'));
1149f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
11502a2a2ba2SAnika Henke            // don't link movies
115144881bd0Shenning.noren            $noLink = true;
115255efc227SAndreas Gohr        } else {
11532ca14335SEsther Brunner            // add file icons
11549d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
11559d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_'.$class;
115652dc5eadSlisps            $link['url'] = ml($src, array('id' => $ID, 'cache' => $cache , 'rev'=>$this->_getLastMediaRevisionAt($src)), true);
115791328684SMichael Hamann            if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
115855efc227SAndreas Gohr        }
11593685f775Sandi
116091df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
116191df343aSAndreas Gohr
11626fe20453SGina Haeussge        //markup non existing files
11634a24b459SKate Arzamastseva        if(!$exists) {
11646fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
11654a24b459SKate Arzamastseva        }
11666fe20453SGina Haeussge
11673685f775Sandi        //output formatted
1168f50634f0SAnika Henke        if($return) {
1169f50634f0SAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1170f50634f0SAnika Henke            else return $this->_formatLink($link);
1171f50634f0SAnika Henke        } else {
1172dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11732ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11740cecf9d5Sandi        }
1175f50634f0SAnika Henke    }
11760cecf9d5Sandi
11773dd5c225SAndreas Gohr    /**
11783dd5c225SAndreas Gohr     * Render an external media file
11793dd5c225SAndreas Gohr     *
11803dd5c225SAndreas Gohr     * @param string $src     full media URL
11813dd5c225SAndreas Gohr     * @param string $title   descriptive text
11823dd5c225SAndreas Gohr     * @param string $align   left|center|right
11833dd5c225SAndreas Gohr     * @param int    $width   width of media in pixel
11843dd5c225SAndreas Gohr     * @param int    $height  height of media in pixel
11853dd5c225SAndreas Gohr     * @param string $cache   cache|recache|nocache
11863dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1187410ee62aSAnika Henke     * @param bool   $return  return HTML instead of adding to $doc
11880c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
11893dd5c225SAndreas Gohr     */
11900ea51e63SMatt Perry    function externalmedia($src, $title = null, $align = null, $width = null,
1191410ee62aSAnika Henke                           $height = null, $cache = null, $linking = null, $return = false) {
11926efc45a2SDmitry Katsubo        if(link_isinterwiki($src)){
11936efc45a2SDmitry Katsubo            list($shortcut, $reference) = explode('>', $src, 2);
11946efc45a2SDmitry Katsubo            $exists = null;
11956efc45a2SDmitry Katsubo            $src = $this->_resolveInterWiki($shortcut, $reference, $exists);
11966efc45a2SDmitry Katsubo        }
119791df343aSAndreas Gohr        list($src, $hash) = explode('#', $src, 2);
1198d98d4540SBen Coburn        $noLink = false;
11998acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1200b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1201b739ff0fSPierre Spring
1202b739ff0fSPierre Spring        $link['url'] = ml($src, array('cache' => $cache));
12033685f775Sandi
12043dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1205b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
12062ca14335SEsther Brunner            // link only jpeg images
120744881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1208f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
12092a2a2ba2SAnika Henke            // don't link movies
121044881bd0Shenning.noren            $noLink = true;
12112ca14335SEsther Brunner        } else {
12122ca14335SEsther Brunner            // add file icons
121327bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
121427bf7924STom N Harris            $link['class'] .= ' mediafile mf_'.$class;
12152ca14335SEsther Brunner        }
12162ca14335SEsther Brunner
121791df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
121891df343aSAndreas Gohr
12193685f775Sandi        //output formatted
1220410ee62aSAnika Henke        if($return) {
1221410ee62aSAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1222410ee62aSAnika Henke            else return $this->_formatLink($link);
1223410ee62aSAnika Henke        } else {
1224dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
12252ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
12260cecf9d5Sandi        }
1227410ee62aSAnika Henke    }
12280cecf9d5Sandi
12294826ab45Sandi    /**
12303db95becSAndreas Gohr     * Renders an RSS feed
1231b625487dSandi     *
12320c4c0281SGerrit Uitslag     * @param string $url    URL of the feed
12330c4c0281SGerrit Uitslag     * @param array  $params Finetuning of the output
12340c4c0281SGerrit Uitslag     *
1235b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1236b625487dSandi     */
12373db95becSAndreas Gohr    function rss($url, $params) {
1238b625487dSandi        global $lang;
12393db95becSAndreas Gohr        global $conf;
12403db95becSAndreas Gohr
12413db95becSAndreas Gohr        require_once(DOKU_INC.'inc/FeedParser.php');
12423db95becSAndreas Gohr        $feed = new FeedParser();
124300077af8SAndreas Gohr        $feed->set_feed_url($url);
1244b625487dSandi
1245b625487dSandi        //disable warning while fetching
12463dd5c225SAndreas Gohr        if(!defined('DOKU_E_LEVEL')) {
12473dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
12483dd5c225SAndreas Gohr        }
12493db95becSAndreas Gohr        $rc = $feed->init();
12503dd5c225SAndreas Gohr        if(isset($elvl)) {
12513dd5c225SAndreas Gohr            error_reporting($elvl);
12523dd5c225SAndreas Gohr        }
1253b625487dSandi
125438c6f603SRobin H. Johnson        if($params['nosort']) $feed->enable_order_by_date(false);
125538c6f603SRobin H. Johnson
12563db95becSAndreas Gohr        //decide on start and end
12573db95becSAndreas Gohr        if($params['reverse']) {
12583db95becSAndreas Gohr            $mod   = -1;
12593db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
12603db95becSAndreas Gohr            $end   = $start - ($params['max']);
1261b2a412b0SAndreas Gohr            $end   = ($end < -1) ? -1 : $end;
12623db95becSAndreas Gohr        } else {
12633db95becSAndreas Gohr            $mod   = 1;
12643db95becSAndreas Gohr            $start = 0;
12653db95becSAndreas Gohr            $end   = $feed->get_item_quantity();
1266d91ab76fSMatt Perry            $end   = ($end > $params['max']) ? $params['max'] : $end;
12673db95becSAndreas Gohr        }
12683db95becSAndreas Gohr
1269a2d649c4Sandi        $this->doc .= '<ul class="rss">';
12703db95becSAndreas Gohr        if($rc) {
12713db95becSAndreas Gohr            for($x = $start; $x != $end; $x += $mod) {
12721bde1582SAndreas Gohr                $item = $feed->get_item($x);
12733db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
1274d2ea3363SAndreas Gohr                // support feeds without links
1275d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
1276d2ea3363SAndreas Gohr                if($lnkurl) {
1277793361f8SAndreas Gohr                    // title is escaped by SimplePie, we unescape here because it
1278793361f8SAndreas Gohr                    // is escaped again in externallink() FS#1705
12793dd5c225SAndreas Gohr                    $this->externallink(
12803dd5c225SAndreas Gohr                        $item->get_permalink(),
12813dd5c225SAndreas Gohr                        html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')
12823dd5c225SAndreas Gohr                    );
1283d2ea3363SAndreas Gohr                } else {
1284d2ea3363SAndreas Gohr                    $this->doc .= ' '.$item->get_title();
1285d2ea3363SAndreas Gohr                }
12863db95becSAndreas Gohr                if($params['author']) {
12871bde1582SAndreas Gohr                    $author = $item->get_author(0);
12881bde1582SAndreas Gohr                    if($author) {
12891bde1582SAndreas Gohr                        $name = $author->get_name();
12901bde1582SAndreas Gohr                        if(!$name) $name = $author->get_email();
12911bde1582SAndreas Gohr                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
12921bde1582SAndreas Gohr                    }
12933db95becSAndreas Gohr                }
12943db95becSAndreas Gohr                if($params['date']) {
12952e7e0c29SAndreas Gohr                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
12963db95becSAndreas Gohr                }
12971bde1582SAndreas Gohr                if($params['details']) {
12983db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
1299173dccb7STom N Harris                    if($conf['htmlok']) {
13001bde1582SAndreas Gohr                        $this->doc .= $item->get_description();
13013db95becSAndreas Gohr                    } else {
13021bde1582SAndreas Gohr                        $this->doc .= strip_tags($item->get_description());
13033db95becSAndreas Gohr                    }
13043db95becSAndreas Gohr                    $this->doc .= '</div>';
13053db95becSAndreas Gohr                }
13063db95becSAndreas Gohr
13073db95becSAndreas Gohr                $this->doc .= '</div></li>';
1308b625487dSandi            }
1309b625487dSandi        } else {
13103db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1311a2d649c4Sandi            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
1312b625487dSandi            $this->externallink($url);
131345e147ccSAndreas Gohr            if($conf['allowdebug']) {
131445e147ccSAndreas Gohr                $this->doc .= '<!--'.hsc($feed->error).'-->';
131545e147ccSAndreas Gohr            }
13163db95becSAndreas Gohr            $this->doc .= '</div></li>';
1317b625487dSandi        }
1318a2d649c4Sandi        $this->doc .= '</ul>';
1319b625487dSandi    }
1320b625487dSandi
13213dd5c225SAndreas Gohr    /**
13223dd5c225SAndreas Gohr     * Start a table
13233dd5c225SAndreas Gohr     *
13243dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
13253dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
13263dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
13277d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
13283dd5c225SAndreas Gohr     */
13290c4c0281SGerrit Uitslag    function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null) {
1330b5742cedSPierre Spring        // initialize the row counter used for classes
1331b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1332619736fdSAdrian Lang        $class                         = 'table';
13330c4c0281SGerrit Uitslag        if($classes !== null) {
13342e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
13350c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
13360c4c0281SGerrit Uitslag        }
1337619736fdSAdrian Lang        if($pos !== null) {
1338619736fdSAdrian Lang            $class .= ' '.$this->startSectionEdit($pos, 'table');
1339619736fdSAdrian Lang        }
1340619736fdSAdrian Lang        $this->doc .= '<div class="'.$class.'"><table class="inline">'.
1341619736fdSAdrian Lang            DOKU_LF;
13420cecf9d5Sandi    }
13430cecf9d5Sandi
13443dd5c225SAndreas Gohr    /**
13453dd5c225SAndreas Gohr     * Close a table
13463dd5c225SAndreas Gohr     *
13473dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
13483dd5c225SAndreas Gohr     */
1349619736fdSAdrian Lang    function table_close($pos = null) {
1350a8574918SAnika Henke        $this->doc .= '</table></div>'.DOKU_LF;
1351619736fdSAdrian Lang        if($pos !== null) {
135290df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
13530cecf9d5Sandi        }
1354619736fdSAdrian Lang    }
13550cecf9d5Sandi
13563dd5c225SAndreas Gohr    /**
13573dd5c225SAndreas Gohr     * Open a table header
13583dd5c225SAndreas Gohr     */
1359f05a1cc5SGerrit Uitslag    function tablethead_open() {
1360f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
1361f05a1cc5SGerrit Uitslag    }
1362f05a1cc5SGerrit Uitslag
13633dd5c225SAndreas Gohr    /**
13643dd5c225SAndreas Gohr     * Close a table header
13653dd5c225SAndreas Gohr     */
1366f05a1cc5SGerrit Uitslag    function tablethead_close() {
1367f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
1368f05a1cc5SGerrit Uitslag    }
1369f05a1cc5SGerrit Uitslag
13703dd5c225SAndreas Gohr    /**
13715a93f869SAnika Henke     * Open a table body
13725a93f869SAnika Henke     */
13735a93f869SAnika Henke    function tabletbody_open() {
13745a93f869SAnika Henke        $this->doc .= DOKU_TAB.'<tbody>'.DOKU_LF;
13755a93f869SAnika Henke    }
13765a93f869SAnika Henke
13775a93f869SAnika Henke    /**
13785a93f869SAnika Henke     * Close a table body
13795a93f869SAnika Henke     */
13805a93f869SAnika Henke    function tabletbody_close() {
13815a93f869SAnika Henke        $this->doc .= DOKU_TAB.'</tbody>'.DOKU_LF;
13825a93f869SAnika Henke    }
13835a93f869SAnika Henke
13845a93f869SAnika Henke    /**
1385d2a99739SAndreas Gohr     * Open a table footer
1386d2a99739SAndreas Gohr     */
1387d2a99739SAndreas Gohr    function tabletfoot_open() {
138844f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB.'<tfoot>'.DOKU_LF;
1389d2a99739SAndreas Gohr    }
1390d2a99739SAndreas Gohr
1391d2a99739SAndreas Gohr    /**
1392d2a99739SAndreas Gohr     * Close a table footer
1393d2a99739SAndreas Gohr     */
1394d2a99739SAndreas Gohr    function tabletfoot_close() {
139544f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB.'</tfoot>'.DOKU_LF;
1396d2a99739SAndreas Gohr    }
1397d2a99739SAndreas Gohr
1398d2a99739SAndreas Gohr    /**
13993dd5c225SAndreas Gohr     * Open a table row
14000c4c0281SGerrit Uitslag     *
14017d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
14023dd5c225SAndreas Gohr     */
14030c4c0281SGerrit Uitslag    function tablerow_open($classes = null) {
1404b5742cedSPierre Spring        // initialize the cell counter used for classes
1405b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1406b5742cedSPierre Spring        $class                          = 'row'.$this->_counter['row_counter']++;
14070c4c0281SGerrit Uitslag        if($classes !== null) {
14082e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
14090c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14100c4c0281SGerrit Uitslag        }
1411b5742cedSPierre Spring        $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
14120cecf9d5Sandi    }
14130cecf9d5Sandi
14143dd5c225SAndreas Gohr    /**
14153dd5c225SAndreas Gohr     * Close a table row
14163dd5c225SAndreas Gohr     */
14170cecf9d5Sandi    function tablerow_close() {
1418a2d649c4Sandi        $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
14190cecf9d5Sandi    }
14200cecf9d5Sandi
14213dd5c225SAndreas Gohr    /**
14223dd5c225SAndreas Gohr     * Open a table header cell
14233dd5c225SAndreas Gohr     *
14243dd5c225SAndreas Gohr     * @param int    $colspan
14253dd5c225SAndreas Gohr     * @param string $align left|center|right
14263dd5c225SAndreas Gohr     * @param int    $rowspan
14277d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
14283dd5c225SAndreas Gohr     */
14290c4c0281SGerrit Uitslag    function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1430b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
14310cecf9d5Sandi        if(!is_null($align)) {
1432b5742cedSPierre Spring            $class .= ' '.$align.'align';
14330cecf9d5Sandi        }
14340c4c0281SGerrit Uitslag        if($classes !== null) {
14352e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
14360c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14370c4c0281SGerrit Uitslag        }
1438b5742cedSPierre Spring        $class .= '"';
1439b5742cedSPierre Spring        $this->doc .= '<th '.$class;
14400cecf9d5Sandi        if($colspan > 1) {
1441a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1442a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
14430cecf9d5Sandi        }
144425b97867Shakan.sandell        if($rowspan > 1) {
144525b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
144625b97867Shakan.sandell        }
1447a2d649c4Sandi        $this->doc .= '>';
14480cecf9d5Sandi    }
14490cecf9d5Sandi
14503dd5c225SAndreas Gohr    /**
14513dd5c225SAndreas Gohr     * Close a table header cell
14523dd5c225SAndreas Gohr     */
14530cecf9d5Sandi    function tableheader_close() {
1454a2d649c4Sandi        $this->doc .= '</th>';
14550cecf9d5Sandi    }
14560cecf9d5Sandi
14573dd5c225SAndreas Gohr    /**
14583dd5c225SAndreas Gohr     * Open a table cell
14593dd5c225SAndreas Gohr     *
14603dd5c225SAndreas Gohr     * @param int       $colspan
14613dd5c225SAndreas Gohr     * @param string    $align left|center|right
14623dd5c225SAndreas Gohr     * @param int       $rowspan
14637d769f75SAndreas Gohr     * @param string|string[]    $classes css classes - have to be valid, do not pass unfiltered user input
14643dd5c225SAndreas Gohr     */
14650c4c0281SGerrit Uitslag    function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1466b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
14670cecf9d5Sandi        if(!is_null($align)) {
1468b5742cedSPierre Spring            $class .= ' '.$align.'align';
14690cecf9d5Sandi        }
14700c4c0281SGerrit Uitslag        if($classes !== null) {
14712e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
14720c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14730c4c0281SGerrit Uitslag        }
1474b5742cedSPierre Spring        $class .= '"';
1475b5742cedSPierre Spring        $this->doc .= '<td '.$class;
14760cecf9d5Sandi        if($colspan > 1) {
1477a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1478a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
14790cecf9d5Sandi        }
148025b97867Shakan.sandell        if($rowspan > 1) {
148125b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
148225b97867Shakan.sandell        }
1483a2d649c4Sandi        $this->doc .= '>';
14840cecf9d5Sandi    }
14850cecf9d5Sandi
14863dd5c225SAndreas Gohr    /**
14873dd5c225SAndreas Gohr     * Close a table cell
14883dd5c225SAndreas Gohr     */
14890cecf9d5Sandi    function tablecell_close() {
1490a2d649c4Sandi        $this->doc .= '</td>';
14910cecf9d5Sandi    }
14920cecf9d5Sandi
1493cea664bdSLarsDW223    /**
1494cea664bdSLarsDW223     * Returns the current header level.
1495cea664bdSLarsDW223     * (required e.g. by the filelist plugin)
1496cea664bdSLarsDW223     *
1497cea664bdSLarsDW223     * @return int The current header level
1498cea664bdSLarsDW223     */
1499cea664bdSLarsDW223    function getLastlevel() {
1500cea664bdSLarsDW223        return $this->lastlevel;
1501cea664bdSLarsDW223    }
1502cea664bdSLarsDW223
15033dd5c225SAndreas Gohr    #region Utility functions
15040cecf9d5Sandi
1505ba11bd29Sandi    /**
15063fd0b676Sandi     * Build a link
15073fd0b676Sandi     *
15083fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1509ba11bd29Sandi     *
15100c4c0281SGerrit Uitslag     * @param array $link attributes of a link
15110c4c0281SGerrit Uitslag     * @return string
15120c4c0281SGerrit Uitslag     *
1513ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1514ba11bd29Sandi     */
1515433bef32Sandi    function _formatLink($link) {
1516ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1517ba11bd29Sandi        if(substr($link['url'], 0, 7) != 'mailto:') {
1518ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1519ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1520ba11bd29Sandi        }
1521ba11bd29Sandi        //remove double encodings in titles
1522ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1523ba11bd29Sandi
1524453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1525453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1526453493f2SAndreas Gohr        $link['url']   = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1527453493f2SAndreas Gohr        $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
1528453493f2SAndreas Gohr
1529ba11bd29Sandi        $ret = '';
1530ba11bd29Sandi        $ret .= $link['pre'];
1531ba11bd29Sandi        $ret .= '<a href="'.$link['url'].'"';
1532bb4866bdSchris        if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1533bb4866bdSchris        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1534bb4866bdSchris        if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1535bb4866bdSchris        if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
1536914045f3SAndreas Gohr        if(!empty($link['rel'])) $ret .= ' rel="'.trim($link['rel']).'"';
1537bb4866bdSchris        if(!empty($link['more'])) $ret .= ' '.$link['more'];
1538ba11bd29Sandi        $ret .= '>';
1539ba11bd29Sandi        $ret .= $link['name'];
1540ba11bd29Sandi        $ret .= '</a>';
1541ba11bd29Sandi        $ret .= $link['suf'];
1542ba11bd29Sandi        return $ret;
1543ba11bd29Sandi    }
1544ba11bd29Sandi
1545ba11bd29Sandi    /**
15463fd0b676Sandi     * Renders internal and external media
15473fd0b676Sandi     *
15483fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
15493dd5c225SAndreas Gohr     * @param string $src       media ID
15503dd5c225SAndreas Gohr     * @param string $title     descriptive text
15513dd5c225SAndreas Gohr     * @param string $align     left|center|right
15523dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
15533dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
15543dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
15553dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
15563dd5c225SAndreas Gohr     * @return string
15573fd0b676Sandi     */
15580ea51e63SMatt Perry    function _media($src, $title = null, $align = null, $width = null,
15590ea51e63SMatt Perry                    $height = null, $cache = null, $render = true) {
15603fd0b676Sandi
15613fd0b676Sandi        $ret = '';
15623fd0b676Sandi
15633dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src);
15643fd0b676Sandi        if(substr($mime, 0, 5) == 'image') {
1565b739ff0fSPierre Spring            // first get the $title
1566b739ff0fSPierre Spring            if(!is_null($title)) {
1567b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1568b739ff0fSPierre Spring            } elseif($ext == 'jpg' || $ext == 'jpeg') {
1569b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1570b739ff0fSPierre Spring                require_once(DOKU_INC.'inc/JpegMeta.php');
157167f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1572b739ff0fSPierre Spring                if($jpeg !== false) $cap = $jpeg->getTitle();
15733dd5c225SAndreas Gohr                if(!empty($cap)) {
1574b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1575b739ff0fSPierre Spring                }
1576b739ff0fSPierre Spring            }
1577b739ff0fSPierre Spring            if(!$render) {
1578b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1579b739ff0fSPierre Spring                // return the title of the picture
1580b739ff0fSPierre Spring                if(!$title) {
1581b739ff0fSPierre Spring                    // just show the sourcename
15823009a773SAndreas Gohr                    $title = $this->_xmlEntities(utf8_basename(noNS($src)));
1583b739ff0fSPierre Spring                }
1584b739ff0fSPierre Spring                return $title;
1585b739ff0fSPierre Spring            }
15863fd0b676Sandi            //add image tag
158752dc5eadSlisps            $ret .= '<img src="'.ml($src, array('w' => $width, 'h' => $height, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src))).'"';
15883fd0b676Sandi            $ret .= ' class="media'.$align.'"';
15893fd0b676Sandi
1590b739ff0fSPierre Spring            if($title) {
1591b739ff0fSPierre Spring                $ret .= ' title="'.$title.'"';
1592b739ff0fSPierre Spring                $ret .= ' alt="'.$title.'"';
15933fd0b676Sandi            } else {
15943fd0b676Sandi                $ret .= ' alt=""';
15953fd0b676Sandi            }
15963fd0b676Sandi
15973fd0b676Sandi            if(!is_null($width))
15983fd0b676Sandi                $ret .= ' width="'.$this->_xmlEntities($width).'"';
15993fd0b676Sandi
16003fd0b676Sandi            if(!is_null($height))
16013fd0b676Sandi                $ret .= ' height="'.$this->_xmlEntities($height).'"';
16023fd0b676Sandi
16033fd0b676Sandi            $ret .= ' />';
16043fd0b676Sandi
160517954bb5SAnika Henke        } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
16062a2a2ba2SAnika Henke            // first get the $title
160717954bb5SAnika Henke            $title = !is_null($title) ? $this->_xmlEntities($title) : false;
16082a2a2ba2SAnika Henke            if(!$render) {
160917954bb5SAnika Henke                // if the file is not supposed to be rendered
161017954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
161117954bb5SAnika Henke                return $title ? $title : $this->_xmlEntities(utf8_basename(noNS($src)));
16122a2a2ba2SAnika Henke            }
16132a2a2ba2SAnika Henke
16142a2a2ba2SAnika Henke            $att          = array();
16152a2a2ba2SAnika Henke            $att['class'] = "media$align";
161617954bb5SAnika Henke            if($title) {
161717954bb5SAnika Henke                $att['title'] = $title;
161817954bb5SAnika Henke            }
16192a2a2ba2SAnika Henke
162017954bb5SAnika Henke            if(media_supportedav($mime, 'video')) {
162117954bb5SAnika Henke                //add video
162279e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1623b44a5dceSAnika Henke            }
162417954bb5SAnika Henke            if(media_supportedav($mime, 'audio')) {
1625b44a5dceSAnika Henke                //add audio
1626b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
162717954bb5SAnika Henke            }
1628b44a5dceSAnika Henke
16293fd0b676Sandi        } elseif($mime == 'application/x-shockwave-flash') {
16301c882ba8SAndreas Gohr            if(!$render) {
16311c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
16321c882ba8SAndreas Gohr                // return the title of the flash
16331c882ba8SAndreas Gohr                if(!$title) {
16341c882ba8SAndreas Gohr                    // just show the sourcename
16353009a773SAndreas Gohr                    $title = utf8_basename(noNS($src));
16361c882ba8SAndreas Gohr                }
163707bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
16381c882ba8SAndreas Gohr            }
16391c882ba8SAndreas Gohr
164007bf32b2SAndreas Gohr            $att          = array();
164107bf32b2SAndreas Gohr            $att['class'] = "media$align";
164207bf32b2SAndreas Gohr            if($align == 'right') $att['align'] = 'right';
164307bf32b2SAndreas Gohr            if($align == 'left') $att['align'] = 'left';
16443dd5c225SAndreas Gohr            $ret .= html_flashobject(
16453dd5c225SAndreas Gohr                ml($src, array('cache' => $cache), true, '&'), $width, $height,
164607bf32b2SAndreas Gohr                array('quality' => 'high'),
164707bf32b2SAndreas Gohr                null,
164807bf32b2SAndreas Gohr                $att,
16493dd5c225SAndreas Gohr                $this->_xmlEntities($title)
16503dd5c225SAndreas Gohr            );
16510f428d7dSAndreas Gohr        } elseif($title) {
16523fd0b676Sandi            // well at least we have a title to display
16533fd0b676Sandi            $ret .= $this->_xmlEntities($title);
16543fd0b676Sandi        } else {
16555291ca3aSAndreas Gohr            // just show the sourcename
16563009a773SAndreas Gohr            $ret .= $this->_xmlEntities(utf8_basename(noNS($src)));
16573fd0b676Sandi        }
16583fd0b676Sandi
16593fd0b676Sandi        return $ret;
16603fd0b676Sandi    }
16613fd0b676Sandi
16623dd5c225SAndreas Gohr    /**
16633dd5c225SAndreas Gohr     * Escape string for output
16643dd5c225SAndreas Gohr     *
16653dd5c225SAndreas Gohr     * @param $string
16663dd5c225SAndreas Gohr     * @return string
16673dd5c225SAndreas Gohr     */
1668433bef32Sandi    function _xmlEntities($string) {
1669de117061Schris        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
16700cecf9d5Sandi    }
16710cecf9d5Sandi
16728a831f2bSAndreas Gohr    /**
16738a831f2bSAndreas Gohr     * Creates a linkid from a headline
1674c5a8fd96SAndreas Gohr     *
16753dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
1676c5a8fd96SAndreas Gohr     * @param string  $title   The headline title
1677c5a8fd96SAndreas Gohr     * @param boolean $create  Create a new unique ID?
16783dd5c225SAndreas Gohr     * @return string
16798a831f2bSAndreas Gohr     */
1680c5a8fd96SAndreas Gohr    function _headerToLink($title, $create = false) {
1681c5a8fd96SAndreas Gohr        if($create) {
16824ceab83fSAndreas Gohr            return sectionID($title, $this->headers);
16834ceab83fSAndreas Gohr        } else {
1684443d207bSAndreas Gohr            $check = false;
1685443d207bSAndreas Gohr            return sectionID($title, $check);
1686c5a8fd96SAndreas Gohr        }
16870cecf9d5Sandi    }
16880cecf9d5Sandi
1689af587fa8Sandi    /**
16903fd0b676Sandi     * Construct a title and handle images in titles
16913fd0b676Sandi     *
16920b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
16933dd5c225SAndreas Gohr     * @param string|array $title    either string title or media array
16943dd5c225SAndreas Gohr     * @param string       $default  default title if nothing else is found
16953dd5c225SAndreas Gohr     * @param bool         $isImage  will be set to true if it's a media file
16963dd5c225SAndreas Gohr     * @param null|string  $id       linked page id (used to extract title from first heading)
16973dd5c225SAndreas Gohr     * @param string       $linktype content|navigation
16983dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
16993fd0b676Sandi     */
17000ea51e63SMatt Perry    function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
170144881bd0Shenning.noren        $isImage = false;
170229657f9eSAndreas Gohr        if(is_array($title)) {
170329657f9eSAndreas Gohr            $isImage = true;
170429657f9eSAndreas Gohr            return $this->_imageTitle($title);
170529657f9eSAndreas Gohr        } elseif(is_null($title) || trim($title) == '') {
1706fe9ec250SChris Smith            if(useHeading($linktype) && $id) {
170767c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1708f515db7fSAndreas Gohr                if(!blank($heading)) {
1709433bef32Sandi                    return $this->_xmlEntities($heading);
1710bb0a59d4Sjan                }
1711bb0a59d4Sjan            }
1712433bef32Sandi            return $this->_xmlEntities($default);
171368c26e6dSMichael Klier        } else {
171468c26e6dSMichael Klier            return $this->_xmlEntities($title);
17150cecf9d5Sandi        }
17160cecf9d5Sandi    }
17170cecf9d5Sandi
17180cecf9d5Sandi    /**
17193dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
17203fd0b676Sandi     *
17213fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1722e0c26282SGerrit Uitslag     * @param array $img
17233dd5c225SAndreas Gohr     * @return string HTML img tag or similar
17240cecf9d5Sandi     */
1725433bef32Sandi    function _imageTitle($img) {
1726d9baf1a7SKazutaka Miyasaka        global $ID;
1727d9baf1a7SKazutaka Miyasaka
1728d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1729d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
17303dd5c225SAndreas Gohr        list($img['src']) = explode('#', $img['src'], 2);
1731d9baf1a7SKazutaka Miyasaka        if($img['type'] == 'internalmedia') {
1732cdb5e961Slisps            resolve_mediaid(getNS($ID), $img['src'], $exists ,$this->date_at, true);
1733d9baf1a7SKazutaka Miyasaka        }
1734d9baf1a7SKazutaka Miyasaka
17353dd5c225SAndreas Gohr        return $this->_media(
17363dd5c225SAndreas Gohr            $img['src'],
17374826ab45Sandi            $img['title'],
17384826ab45Sandi            $img['align'],
17394826ab45Sandi            $img['width'],
17404826ab45Sandi            $img['height'],
17413dd5c225SAndreas Gohr            $img['cache']
17423dd5c225SAndreas Gohr        );
17430cecf9d5Sandi    }
1744b739ff0fSPierre Spring
1745b739ff0fSPierre Spring    /**
17463dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
17473dd5c225SAndreas Gohr     *
17483dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1749b739ff0fSPierre Spring     *
1750b739ff0fSPierre Spring     * @author   Pierre Spring <pierre.spring@liip.ch>
17513dd5c225SAndreas Gohr     * @param string $src       media ID
17523dd5c225SAndreas Gohr     * @param string $title     descriptive text
17533dd5c225SAndreas Gohr     * @param string $align     left|center|right
17543dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
17553dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
17563dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
17573dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
17583dd5c225SAndreas Gohr     * @return array associative array with link config
1759b739ff0fSPierre Spring     */
1760d91ab76fSMatt Perry    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1761b739ff0fSPierre Spring        global $conf;
1762b739ff0fSPierre Spring
1763b739ff0fSPierre Spring        $link           = array();
1764b739ff0fSPierre Spring        $link['class']  = 'media';
1765b739ff0fSPierre Spring        $link['style']  = '';
1766b739ff0fSPierre Spring        $link['pre']    = '';
1767b739ff0fSPierre Spring        $link['suf']    = '';
1768b739ff0fSPierre Spring        $link['more']   = '';
1769b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1770bc3d2252SAndreas Gohr        if($conf['target']['media']) $link['rel'] = 'noopener';
1771b739ff0fSPierre Spring        $link['title']  = $this->_xmlEntities($src);
1772b739ff0fSPierre Spring        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1773b739ff0fSPierre Spring
1774b739ff0fSPierre Spring        return $link;
1775b739ff0fSPierre Spring    }
177691459163SAnika Henke
17772a2a2ba2SAnika Henke    /**
17782a2a2ba2SAnika Henke     * Embed video(s) in HTML
17792a2a2ba2SAnika Henke     *
17802a2a2ba2SAnika Henke     * @author Anika Henke <anika@selfthinker.org>
17812a2a2ba2SAnika Henke     *
17822a2a2ba2SAnika Henke     * @param string $src         - ID of video to embed
17832a2a2ba2SAnika Henke     * @param int    $width       - width of the video in pixels
17842a2a2ba2SAnika Henke     * @param int    $height      - height of the video in pixels
17852a2a2ba2SAnika Henke     * @param array  $atts        - additional attributes for the <video> tag
1786f50634f0SAnika Henke     * @return string
17872a2a2ba2SAnika Henke     */
178879e53fe5SAnika Henke    function _video($src, $width, $height, $atts = null) {
17892a2a2ba2SAnika Henke        // prepare width and height
17902a2a2ba2SAnika Henke        if(is_null($atts)) $atts = array();
17912a2a2ba2SAnika Henke        $atts['width']  = (int) $width;
17922a2a2ba2SAnika Henke        $atts['height'] = (int) $height;
17932a2a2ba2SAnika Henke        if(!$atts['width']) $atts['width'] = 320;
17942a2a2ba2SAnika Henke        if(!$atts['height']) $atts['height'] = 240;
17952a2a2ba2SAnika Henke
1796410ee62aSAnika Henke        $posterUrl = '';
1797410ee62aSAnika Henke        $files = array();
1798410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1799410ee62aSAnika Henke
1800410ee62aSAnika Henke        if ($isExternal) {
1801410ee62aSAnika Henke            // take direct source for external files
1802702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1803410ee62aSAnika Henke            $files[$srcMime] = $src;
1804410ee62aSAnika Henke        } else {
18053d7a9e0aSAnika Henke            // prepare alternative formats
18063d7a9e0aSAnika Henke            $extensions   = array('webm', 'ogv', 'mp4');
1807410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
180859bc3b48SGerrit Uitslag            $poster       = media_alternativefiles($src, array('jpg', 'png'));
180999f943f6SAnika Henke            if(!empty($poster)) {
18102d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
181199f943f6SAnika Henke            }
1812410ee62aSAnika Henke        }
18132a2a2ba2SAnika Henke
1814f50634f0SAnika Henke        $out = '';
181579e53fe5SAnika Henke        // open video tag
1816f50634f0SAnika Henke        $out .= '<video '.buildAttributes($atts).' controls="controls"';
18173641199aSAnika Henke        if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1818f50634f0SAnika Henke        $out .= '>'.NL;
18193641199aSAnika Henke        $fallback = '';
182079e53fe5SAnika Henke
182179e53fe5SAnika Henke        // output source for each alternative video format
1822410ee62aSAnika Henke        foreach($files as $mime => $file) {
1823410ee62aSAnika Henke            if ($isExternal) {
1824410ee62aSAnika Henke                $url = $file;
1825410ee62aSAnika Henke                $linkType = 'externalmedia';
1826410ee62aSAnika Henke            } else {
18272d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1828410ee62aSAnika Henke                $linkType = 'internalmedia';
1829410ee62aSAnika Henke            }
183017954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
18313d7a9e0aSAnika Henke
1832f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
183379e53fe5SAnika Henke            // alternative content (just a link to the file)
1834410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
18353d7a9e0aSAnika Henke        }
18362a2a2ba2SAnika Henke
18372a2a2ba2SAnika Henke        // finish
18383641199aSAnika Henke        $out .= $fallback;
1839f50634f0SAnika Henke        $out .= '</video>'.NL;
1840f50634f0SAnika Henke        return $out;
18412a2a2ba2SAnika Henke    }
18422a2a2ba2SAnika Henke
1843b44a5dceSAnika Henke    /**
1844b44a5dceSAnika Henke     * Embed audio in HTML
1845b44a5dceSAnika Henke     *
1846b44a5dceSAnika Henke     * @author Anika Henke <anika@selfthinker.org>
1847b44a5dceSAnika Henke     *
1848b44a5dceSAnika Henke     * @param string $src       - ID of audio to embed
18496d4af72aSAnika Henke     * @param array  $atts      - additional attributes for the <audio> tag
1850f50634f0SAnika Henke     * @return string
1851b44a5dceSAnika Henke     */
185259bc3b48SGerrit Uitslag    function _audio($src, $atts = array()) {
1853702e97d3SAnika Henke        $files = array();
1854410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1855b44a5dceSAnika Henke
1856410ee62aSAnika Henke        if ($isExternal) {
1857410ee62aSAnika Henke            // take direct source for external files
1858702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1859410ee62aSAnika Henke            $files[$srcMime] = $src;
1860410ee62aSAnika Henke        } else {
1861b44a5dceSAnika Henke            // prepare alternative formats
1862b44a5dceSAnika Henke            $extensions   = array('ogg', 'mp3', 'wav');
1863410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
1864410ee62aSAnika Henke        }
1865b44a5dceSAnika Henke
1866f50634f0SAnika Henke        $out = '';
1867b44a5dceSAnika Henke        // open audio tag
1868f50634f0SAnika Henke        $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
18693641199aSAnika Henke        $fallback = '';
1870b44a5dceSAnika Henke
1871b44a5dceSAnika Henke        // output source for each alternative audio format
1872410ee62aSAnika Henke        foreach($files as $mime => $file) {
1873410ee62aSAnika Henke            if ($isExternal) {
1874410ee62aSAnika Henke                $url = $file;
1875410ee62aSAnika Henke                $linkType = 'externalmedia';
1876410ee62aSAnika Henke            } else {
18772d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1878410ee62aSAnika Henke                $linkType = 'internalmedia';
1879410ee62aSAnika Henke            }
188017954bb5SAnika Henke            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(utf8_basename(noNS($file)));
1881b44a5dceSAnika Henke
1882f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1883b44a5dceSAnika Henke            // alternative content (just a link to the file)
1884410ee62aSAnika Henke            $fallback .= $this->$linkType($file, $title, null, null, null, $cache = null, $linking = 'linkonly', $return = true);
1885b44a5dceSAnika Henke        }
1886b44a5dceSAnika Henke
1887b44a5dceSAnika Henke        // finish
18883641199aSAnika Henke        $out .= $fallback;
1889f50634f0SAnika Henke        $out .= '</audio>'.NL;
1890f50634f0SAnika Henke        return $out;
1891b44a5dceSAnika Henke    }
1892b44a5dceSAnika Henke
18935c2eed9aSlisps    /**
189452dc5eadSlisps     * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
18955c2eed9aSlisps     * which returns an existing media revision less or equal to rev or date_at
18965c2eed9aSlisps     *
18975c2eed9aSlisps     * @author lisps
18985c2eed9aSlisps     * @param string $media_id
18995c2eed9aSlisps     * @access protected
19005c2eed9aSlisps     * @return string revision ('' for current)
19015c2eed9aSlisps     */
190252dc5eadSlisps    function _getLastMediaRevisionAt($media_id){
190352dc5eadSlisps        if(!$this->date_at || media_isexternal($media_id)) return '';
190478b874e6Slisps        $pagelog = new MediaChangeLog($media_id);
190578b874e6Slisps        return $pagelog->getLastRevisionAt($this->date_at);
19065c2eed9aSlisps    }
19075c2eed9aSlisps
19083dd5c225SAndreas Gohr    #endregion
19090cecf9d5Sandi}
19100cecf9d5Sandi
1911e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1912