xref: /dokuwiki/inc/parser/xhtml.php (revision ad43fdbf71534d013c5204fdb0e4203acfcf9a95)
10cecf9d5Sandi<?php
20c3a5702SAndreas Gohr
30c3a5702SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog;
42cd6cc0aSAndreas Gohruse dokuwiki\File\MediaResolver;
52cd6cc0aSAndreas Gohruse dokuwiki\File\PageResolver;
60c3a5702SAndreas Gohr
7b625487dSandi/**
8b625487dSandi * Renderer for XHTML output
9b625487dSandi *
10b4f2363aSAndreas Gohr * This is DokuWiki's main renderer used to display page content in the wiki
11b4f2363aSAndreas Gohr *
12b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
13b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
143dd5c225SAndreas Gohr *
150cecf9d5Sandi */
16ac83b9d8Sandiclass Doku_Renderer_xhtml extends Doku_Renderer {
173dd5c225SAndreas Gohr    /** @var array store the table of contents */
183dd5c225SAndreas Gohr    public $toc = array();
190cecf9d5Sandi
203dd5c225SAndreas Gohr    /** @var array A stack of section edit data */
213dd5c225SAndreas Gohr    protected $sectionedits = array();
22de369923SAndreas Gohr
23de369923SAndreas Gohr    /** @var string|int link pages and media against this revision */
24de369923SAndreas Gohr    public $date_at = '';
25c5a8fd96SAndreas Gohr
263dd5c225SAndreas Gohr    /** @var int last section edit id, used by startSectionEdit */
273dd5c225SAndreas Gohr    protected $lastsecid = 0;
280cecf9d5Sandi
2916ec3e37SAndreas Gohr    /** @var array a list of footnotes, list starts at 1! */
303dd5c225SAndreas Gohr    protected $footnotes = array();
317764a90aSandi
323dd5c225SAndreas Gohr    /** @var int current section level */
333dd5c225SAndreas Gohr    protected $lastlevel = 0;
343dd5c225SAndreas Gohr    /** @var array section node tracker */
353dd5c225SAndreas Gohr    protected $node = array(0, 0, 0, 0, 0);
363dd5c225SAndreas Gohr
373dd5c225SAndreas Gohr    /** @var string temporary $doc store */
383dd5c225SAndreas Gohr    protected $store = '';
393dd5c225SAndreas Gohr
403dd5c225SAndreas Gohr    /** @var array global counter, for table classes etc. */
413dd5c225SAndreas Gohr    protected $_counter = array(); //
423dd5c225SAndreas Gohr
433dd5c225SAndreas Gohr    /** @var int counts the code and file blocks, used to provide download links */
443dd5c225SAndreas Gohr    protected $_codeblock = 0;
453dd5c225SAndreas Gohr
463dd5c225SAndreas Gohr    /** @var array list of allowed URL schemes */
473dd5c225SAndreas Gohr    protected $schemes = null;
48b5742cedSPierre Spring
4990df9a4dSAdrian Lang    /**
5090df9a4dSAdrian Lang     * Register a new edit section range
5190df9a4dSAdrian Lang     *
5242ea7f44SGerrit Uitslag     * @param int    $start  The byte position for the edit start
53ec57f119SLarsDW223     * @param array  $data   Associative array with section data:
54ec57f119SLarsDW223     *                       Key 'name': the section name/title
55ec57f119SLarsDW223     *                       Key 'target': the target for the section edit,
56ec57f119SLarsDW223     *                                     e.g. 'section' or 'table'
57ec57f119SLarsDW223     *                       Key 'hid': header id
58ec57f119SLarsDW223     *                       Key 'codeblockOffset': actual code block index
59ec57f119SLarsDW223     *                       Key 'start': set in startSectionEdit(),
60ec57f119SLarsDW223     *                                    do not set yourself
61ec57f119SLarsDW223     *                       Key 'range': calculated from 'start' and
62ec57f119SLarsDW223     *                                    $key in finishSectionEdit(),
63ec57f119SLarsDW223     *                                    do not set yourself
6490df9a4dSAdrian Lang     * @return string  A marker class for the starting HTML element
6542ea7f44SGerrit Uitslag     *
6690df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
6790df9a4dSAdrian Lang     */
68ec57f119SLarsDW223    public function startSectionEdit($start, $data) {
69ec57f119SLarsDW223        if (!is_array($data)) {
70ac025fdfSAndreas Gohr            msg(
71ac025fdfSAndreas Gohr                sprintf(
72ac025fdfSAndreas Gohr                    'startSectionEdit: $data "%s" is NOT an array! One of your plugins needs an update.',
73ac025fdfSAndreas Gohr                    hsc((string) $data)
74ac025fdfSAndreas Gohr                ), -1
75ac025fdfSAndreas Gohr            );
76ac025fdfSAndreas Gohr
77ac025fdfSAndreas Gohr            // @deprecated 2018-04-14, backward compatibility
78ac025fdfSAndreas Gohr            $args = func_get_args();
79ac025fdfSAndreas Gohr            $data = array();
80ac025fdfSAndreas Gohr            if(isset($args[1])) $data['target'] = $args[1];
81ac025fdfSAndreas Gohr            if(isset($args[2])) $data['name'] = $args[2];
82ac025fdfSAndreas Gohr            if(isset($args[3])) $data['hid'] = $args[3];
83ec57f119SLarsDW223        }
84ec57f119SLarsDW223        $data['secid'] = ++$this->lastsecid;
85ec57f119SLarsDW223        $data['start'] = $start;
86ec57f119SLarsDW223        $this->sectionedits[] = $data;
87ec57f119SLarsDW223        return 'sectionedit'.$data['secid'];
8890df9a4dSAdrian Lang    }
8990df9a4dSAdrian Lang
9090df9a4dSAdrian Lang    /**
9190df9a4dSAdrian Lang     * Finish an edit section range
9290df9a4dSAdrian Lang     *
9342ea7f44SGerrit Uitslag     * @param int  $end     The byte position for the edit end; null for the rest of the page
9442ea7f44SGerrit Uitslag     *
9590df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
9690df9a4dSAdrian Lang     */
972571786cSLarsDW223    public function finishSectionEdit($end = null, $hid = null) {
98*ad43fdbfSasivery        if(count($this->sectionedits) == 0) {
990d9f02ecSasivery            return;
1000d9f02ecSasivery        }
101*ad43fdbfSasivery        $data = array_pop($this->sectionedits);
102ec57f119SLarsDW223        if(!is_null($end) && $end <= $data['start']) {
10300c13053SAdrian Lang            return;
10400c13053SAdrian Lang        }
1052571786cSLarsDW223        if(!is_null($hid)) {
106ec57f119SLarsDW223            $data['hid'] .= $hid;
1072571786cSLarsDW223        }
108ec57f119SLarsDW223        $data['range'] = $data['start'].'-'.(is_null($end) ? '' : $end);
109ec57f119SLarsDW223        unset($data['start']);
110ada0d779SMichael Hamann        $this->doc .= '<!-- EDIT'.hsc(json_encode ($data)).' -->';
11190df9a4dSAdrian Lang    }
11290df9a4dSAdrian Lang
1133dd5c225SAndreas Gohr    /**
1143dd5c225SAndreas Gohr     * Returns the format produced by this renderer.
1153dd5c225SAndreas Gohr     *
1163dd5c225SAndreas Gohr     * @return string always 'xhtml'
1173dd5c225SAndreas Gohr     */
118de369923SAndreas Gohr    public function getFormat() {
1195f70445dSAndreas Gohr        return 'xhtml';
1205f70445dSAndreas Gohr    }
1215f70445dSAndreas Gohr
1223dd5c225SAndreas Gohr    /**
1233dd5c225SAndreas Gohr     * Initialize the document
1243dd5c225SAndreas Gohr     */
125de369923SAndreas Gohr    public function document_start() {
126c5a8fd96SAndreas Gohr        //reset some internals
127c5a8fd96SAndreas Gohr        $this->toc     = array();
1280cecf9d5Sandi    }
1290cecf9d5Sandi
1303dd5c225SAndreas Gohr    /**
1313dd5c225SAndreas Gohr     * Finalize the document
1323dd5c225SAndreas Gohr     */
133de369923SAndreas Gohr    public function document_end() {
13490df9a4dSAdrian Lang        // Finish open section edits.
13590df9a4dSAdrian Lang        while(count($this->sectionedits) > 0) {
136ec57f119SLarsDW223            if($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) {
13790df9a4dSAdrian Lang                // If there is only one section, do not write a section edit
13890df9a4dSAdrian Lang                // marker.
13990df9a4dSAdrian Lang                array_pop($this->sectionedits);
14090df9a4dSAdrian Lang            } else {
141d9e36cbeSAdrian Lang                $this->finishSectionEdit();
14290df9a4dSAdrian Lang            }
14390df9a4dSAdrian Lang        }
14490df9a4dSAdrian Lang
1450cecf9d5Sandi        if(count($this->footnotes) > 0) {
146a2d649c4Sandi            $this->doc .= '<div class="footnotes">'.DOKU_LF;
147d74aace9Schris
14816ec3e37SAndreas Gohr            foreach($this->footnotes as $id => $footnote) {
149d74aace9Schris                // check its not a placeholder that indicates actual footnote text is elsewhere
150d74aace9Schris                if(substr($footnote, 0, 5) != "@@FNT") {
151d74aace9Schris
152d74aace9Schris                    // open the footnote and set the anchor and backlink
153d74aace9Schris                    $this->doc .= '<div class="fn">';
15416cc7ed7SAnika Henke                    $this->doc .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" class="fn_bot">';
15529bfcd16SAndreas Gohr                    $this->doc .= $id.')</a></sup> '.DOKU_LF;
156d74aace9Schris
157d74aace9Schris                    // get any other footnotes that use the same markup
158d74aace9Schris                    $alt = array_keys($this->footnotes, "@@FNT$id");
159d74aace9Schris
160d74aace9Schris                    if(count($alt)) {
161d74aace9Schris                        foreach($alt as $ref) {
162d74aace9Schris                            // set anchor and backlink for the other footnotes
16316ec3e37SAndreas Gohr                            $this->doc .= ', <sup><a href="#fnt__'.($ref).'" id="fn__'.($ref).'" class="fn_bot">';
16416ec3e37SAndreas Gohr                            $this->doc .= ($ref).')</a></sup> '.DOKU_LF;
165d74aace9Schris                        }
166d74aace9Schris                    }
167d74aace9Schris
168d74aace9Schris                    // add footnote markup and close this footnote
169694afa06SAnika Henke                    $this->doc .= '<div class="content">'.$footnote.'</div>';
170d74aace9Schris                    $this->doc .= '</div>'.DOKU_LF;
171d74aace9Schris                }
1720cecf9d5Sandi            }
173a2d649c4Sandi            $this->doc .= '</div>'.DOKU_LF;
1740cecf9d5Sandi        }
175c5a8fd96SAndreas Gohr
176b8595a66SAndreas Gohr        // Prepare the TOC
177851f2e89SAnika Henke        global $conf;
17864159a61SAndreas Gohr        if(
17964159a61SAndreas Gohr            $this->info['toc'] &&
18064159a61SAndreas Gohr            is_array($this->toc) &&
18164159a61SAndreas Gohr            $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']
18264159a61SAndreas Gohr        ) {
183b8595a66SAndreas Gohr            global $TOC;
184b8595a66SAndreas Gohr            $TOC = $this->toc;
1850cecf9d5Sandi        }
1863e55d035SAndreas Gohr
1873e55d035SAndreas Gohr        // make sure there are no empty paragraphs
18827918226Schris        $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
189e41c4da9SAndreas Gohr    }
1900cecf9d5Sandi
1913dd5c225SAndreas Gohr    /**
1923dd5c225SAndreas Gohr     * Add an item to the TOC
1933dd5c225SAndreas Gohr     *
1943dd5c225SAndreas Gohr     * @param string $id       the hash link
1953dd5c225SAndreas Gohr     * @param string $text     the text to display
1963dd5c225SAndreas Gohr     * @param int    $level    the nesting level
1973dd5c225SAndreas Gohr     */
198de369923SAndreas Gohr    public function toc_additem($id, $text, $level) {
199af587fa8Sandi        global $conf;
200af587fa8Sandi
201c5a8fd96SAndreas Gohr        //handle TOC
202c5a8fd96SAndreas Gohr        if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
2037d91652aSAndreas Gohr            $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
204c5a8fd96SAndreas Gohr        }
205e7856beaSchris    }
206e7856beaSchris
2073dd5c225SAndreas Gohr    /**
2083dd5c225SAndreas Gohr     * Render a heading
2093dd5c225SAndreas Gohr     *
2103dd5c225SAndreas Gohr     * @param string $text       the text to display
2113dd5c225SAndreas Gohr     * @param int    $level      header level
2123dd5c225SAndreas Gohr     * @param int    $pos        byte position in the original source
213e3c00e6eSIain Hallam     * @param bool   $returnonly whether to return html or write to doc attribute
214e3c00e6eSIain Hallam     * @return void|string writes to doc attribute or returns html depends on $returnonly
2153dd5c225SAndreas Gohr     */
216e3c00e6eSIain Hallam    public function header($text, $level, $pos, $returnonly = false) {
21790df9a4dSAdrian Lang        global $conf;
21890df9a4dSAdrian Lang
219f515db7fSAndreas Gohr        if(blank($text)) return; //skip empty headlines
220e7856beaSchris
221e7856beaSchris        $hid = $this->_headerToLink($text, true);
222e7856beaSchris
223e7856beaSchris        //only add items within configured levels
224e7856beaSchris        $this->toc_additem($hid, $text, $level);
225c5a8fd96SAndreas Gohr
22691459163SAnika Henke        // adjust $node to reflect hierarchy of levels
22791459163SAnika Henke        $this->node[$level - 1]++;
22891459163SAnika Henke        if($level < $this->lastlevel) {
22991459163SAnika Henke            for($i = 0; $i < $this->lastlevel - $level; $i++) {
23091459163SAnika Henke                $this->node[$this->lastlevel - $i - 1] = 0;
23191459163SAnika Henke            }
23291459163SAnika Henke        }
23391459163SAnika Henke        $this->lastlevel = $level;
23491459163SAnika Henke
23590df9a4dSAdrian Lang        if($level <= $conf['maxseclevel'] &&
23690df9a4dSAdrian Lang            count($this->sectionedits) > 0 &&
237ec57f119SLarsDW223            $this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section'
2383dd5c225SAndreas Gohr        ) {
2396c1f778cSAdrian Lang            $this->finishSectionEdit($pos - 1);
24090df9a4dSAdrian Lang        }
24190df9a4dSAdrian Lang
242e3c00e6eSIain Hallam        // build the header
243e3c00e6eSIain Hallam        $header = DOKU_LF.'<h'.$level;
24490df9a4dSAdrian Lang        if($level <= $conf['maxseclevel']) {
245ec57f119SLarsDW223            $data = array();
246ec57f119SLarsDW223            $data['target'] = 'section';
247ec57f119SLarsDW223            $data['name'] = $text;
248ec57f119SLarsDW223            $data['hid'] = $hid;
249ec57f119SLarsDW223            $data['codeblockOffset'] = $this->_codeblock;
250e3c00e6eSIain Hallam            $header .= ' class="'.$this->startSectionEdit($pos, $data).'"';
25190df9a4dSAdrian Lang        }
252e3c00e6eSIain Hallam        $header .= ' id="'.$hid.'">';
253e3c00e6eSIain Hallam        $header .= $this->_xmlEntities($text);
254e3c00e6eSIain Hallam        $header .= "</h$level>".DOKU_LF;
255e3c00e6eSIain Hallam
256e3c00e6eSIain Hallam        if ($returnonly) {
257e3c00e6eSIain Hallam            return $header;
258e3c00e6eSIain Hallam        } else {
259e3c00e6eSIain Hallam            $this->doc .= $header;
260e3c00e6eSIain Hallam        }
2610cecf9d5Sandi    }
2620cecf9d5Sandi
2633dd5c225SAndreas Gohr    /**
2643dd5c225SAndreas Gohr     * Open a new section
2653dd5c225SAndreas Gohr     *
2663dd5c225SAndreas Gohr     * @param int $level section level (as determined by the previous header)
2673dd5c225SAndreas Gohr     */
268de369923SAndreas Gohr    public function section_open($level) {
2699864e7b1SAdrian Lang        $this->doc .= '<div class="level'.$level.'">'.DOKU_LF;
2700cecf9d5Sandi    }
2710cecf9d5Sandi
2723dd5c225SAndreas Gohr    /**
2733dd5c225SAndreas Gohr     * Close the current section
2743dd5c225SAndreas Gohr     */
275de369923SAndreas Gohr    public function section_close() {
276a2d649c4Sandi        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
2770cecf9d5Sandi    }
2780cecf9d5Sandi
2793dd5c225SAndreas Gohr    /**
2803dd5c225SAndreas Gohr     * Render plain text data
2813dd5c225SAndreas Gohr     *
2823dd5c225SAndreas Gohr     * @param $text
2833dd5c225SAndreas Gohr     */
284de369923SAndreas Gohr    public function cdata($text) {
285a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
2860cecf9d5Sandi    }
2870cecf9d5Sandi
2883dd5c225SAndreas Gohr    /**
2893dd5c225SAndreas Gohr     * Open a paragraph
2903dd5c225SAndreas Gohr     */
291de369923SAndreas Gohr    public function p_open() {
29259869a4bSAnika Henke        $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
2930cecf9d5Sandi    }
2940cecf9d5Sandi
2953dd5c225SAndreas Gohr    /**
2963dd5c225SAndreas Gohr     * Close a paragraph
2973dd5c225SAndreas Gohr     */
298de369923SAndreas Gohr    public function p_close() {
29959869a4bSAnika Henke        $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
3000cecf9d5Sandi    }
3010cecf9d5Sandi
3023dd5c225SAndreas Gohr    /**
3033dd5c225SAndreas Gohr     * Create a line break
3043dd5c225SAndreas Gohr     */
305de369923SAndreas Gohr    public function linebreak() {
306a2d649c4Sandi        $this->doc .= '<br/>'.DOKU_LF;
3070cecf9d5Sandi    }
3080cecf9d5Sandi
3093dd5c225SAndreas Gohr    /**
3103dd5c225SAndreas Gohr     * Create a horizontal line
3113dd5c225SAndreas Gohr     */
312de369923SAndreas Gohr    public function hr() {
3134beabca9SAnika Henke        $this->doc .= '<hr />'.DOKU_LF;
3140cecf9d5Sandi    }
3150cecf9d5Sandi
3163dd5c225SAndreas Gohr    /**
3173dd5c225SAndreas Gohr     * Start strong (bold) formatting
3183dd5c225SAndreas Gohr     */
319de369923SAndreas Gohr    public function strong_open() {
320a2d649c4Sandi        $this->doc .= '<strong>';
3210cecf9d5Sandi    }
3220cecf9d5Sandi
3233dd5c225SAndreas Gohr    /**
3243dd5c225SAndreas Gohr     * Stop strong (bold) formatting
3253dd5c225SAndreas Gohr     */
326de369923SAndreas Gohr    public function strong_close() {
327a2d649c4Sandi        $this->doc .= '</strong>';
3280cecf9d5Sandi    }
3290cecf9d5Sandi
3303dd5c225SAndreas Gohr    /**
3313dd5c225SAndreas Gohr     * Start emphasis (italics) formatting
3323dd5c225SAndreas Gohr     */
333de369923SAndreas Gohr    public function emphasis_open() {
334a2d649c4Sandi        $this->doc .= '<em>';
3350cecf9d5Sandi    }
3360cecf9d5Sandi
3373dd5c225SAndreas Gohr    /**
3383dd5c225SAndreas Gohr     * Stop emphasis (italics) formatting
3393dd5c225SAndreas Gohr     */
340de369923SAndreas Gohr    public function emphasis_close() {
341a2d649c4Sandi        $this->doc .= '</em>';
3420cecf9d5Sandi    }
3430cecf9d5Sandi
3443dd5c225SAndreas Gohr    /**
3453dd5c225SAndreas Gohr     * Start underline formatting
3463dd5c225SAndreas Gohr     */
347de369923SAndreas Gohr    public function underline_open() {
34802e51121SAnika Henke        $this->doc .= '<em class="u">';
3490cecf9d5Sandi    }
3500cecf9d5Sandi
3513dd5c225SAndreas Gohr    /**
3523dd5c225SAndreas Gohr     * Stop underline formatting
3533dd5c225SAndreas Gohr     */
354de369923SAndreas Gohr    public function underline_close() {
35502e51121SAnika Henke        $this->doc .= '</em>';
3560cecf9d5Sandi    }
3570cecf9d5Sandi
3583dd5c225SAndreas Gohr    /**
3593dd5c225SAndreas Gohr     * Start monospace formatting
3603dd5c225SAndreas Gohr     */
361de369923SAndreas Gohr    public function monospace_open() {
362a2d649c4Sandi        $this->doc .= '<code>';
3630cecf9d5Sandi    }
3640cecf9d5Sandi
3653dd5c225SAndreas Gohr    /**
3663dd5c225SAndreas Gohr     * Stop monospace formatting
3673dd5c225SAndreas Gohr     */
368de369923SAndreas Gohr    public function monospace_close() {
369a2d649c4Sandi        $this->doc .= '</code>';
3700cecf9d5Sandi    }
3710cecf9d5Sandi
3723dd5c225SAndreas Gohr    /**
3733dd5c225SAndreas Gohr     * Start a subscript
3743dd5c225SAndreas Gohr     */
375de369923SAndreas Gohr    public function subscript_open() {
376a2d649c4Sandi        $this->doc .= '<sub>';
3770cecf9d5Sandi    }
3780cecf9d5Sandi
3793dd5c225SAndreas Gohr    /**
3803dd5c225SAndreas Gohr     * Stop a subscript
3813dd5c225SAndreas Gohr     */
382de369923SAndreas Gohr    public function subscript_close() {
383a2d649c4Sandi        $this->doc .= '</sub>';
3840cecf9d5Sandi    }
3850cecf9d5Sandi
3863dd5c225SAndreas Gohr    /**
3873dd5c225SAndreas Gohr     * Start a superscript
3883dd5c225SAndreas Gohr     */
389de369923SAndreas Gohr    public function superscript_open() {
390a2d649c4Sandi        $this->doc .= '<sup>';
3910cecf9d5Sandi    }
3920cecf9d5Sandi
3933dd5c225SAndreas Gohr    /**
3943dd5c225SAndreas Gohr     * Stop a superscript
3953dd5c225SAndreas Gohr     */
396de369923SAndreas Gohr    public function superscript_close() {
397a2d649c4Sandi        $this->doc .= '</sup>';
3980cecf9d5Sandi    }
3990cecf9d5Sandi
4003dd5c225SAndreas Gohr    /**
4013dd5c225SAndreas Gohr     * Start deleted (strike-through) formatting
4023dd5c225SAndreas Gohr     */
403de369923SAndreas Gohr    public function deleted_open() {
404a2d649c4Sandi        $this->doc .= '<del>';
4050cecf9d5Sandi    }
4060cecf9d5Sandi
4073dd5c225SAndreas Gohr    /**
4083dd5c225SAndreas Gohr     * Stop deleted (strike-through) formatting
4093dd5c225SAndreas Gohr     */
410de369923SAndreas Gohr    public function deleted_close() {
411a2d649c4Sandi        $this->doc .= '</del>';
4120cecf9d5Sandi    }
4130cecf9d5Sandi
4143fd0b676Sandi    /**
4153fd0b676Sandi     * Callback for footnote start syntax
4163fd0b676Sandi     *
4173fd0b676Sandi     * All following content will go to the footnote instead of
418d74aace9Schris     * the document. To achieve this the previous rendered content
4193fd0b676Sandi     * is moved to $store and $doc is cleared
4203fd0b676Sandi     *
4213fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
4223fd0b676Sandi     */
423de369923SAndreas Gohr    public function footnote_open() {
4247764a90aSandi
4257764a90aSandi        // move current content to store and record footnote
4267764a90aSandi        $this->store = $this->doc;
4277764a90aSandi        $this->doc   = '';
4280cecf9d5Sandi    }
4290cecf9d5Sandi
4303fd0b676Sandi    /**
4313fd0b676Sandi     * Callback for footnote end syntax
4323fd0b676Sandi     *
4333fd0b676Sandi     * All rendered content is moved to the $footnotes array and the old
4343fd0b676Sandi     * content is restored from $store again
4353fd0b676Sandi     *
4363fd0b676Sandi     * @author Andreas Gohr
4373fd0b676Sandi     */
438de369923SAndreas Gohr    public function footnote_close() {
43916ec3e37SAndreas Gohr        /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
44016ec3e37SAndreas Gohr        static $fnid = 0;
44116ec3e37SAndreas Gohr        // assign new footnote id (we start at 1)
44216ec3e37SAndreas Gohr        $fnid++;
4437764a90aSandi
444d74aace9Schris        // recover footnote into the stack and restore old content
445d74aace9Schris        $footnote    = $this->doc;
4467764a90aSandi        $this->doc   = $this->store;
4477764a90aSandi        $this->store = '';
448d74aace9Schris
449d74aace9Schris        // check to see if this footnote has been seen before
450d74aace9Schris        $i = array_search($footnote, $this->footnotes);
451d74aace9Schris
452d74aace9Schris        if($i === false) {
453d74aace9Schris            // its a new footnote, add it to the $footnotes array
45416ec3e37SAndreas Gohr            $this->footnotes[$fnid] = $footnote;
455d74aace9Schris        } else {
45616ec3e37SAndreas Gohr            // seen this one before, save a placeholder
45716ec3e37SAndreas Gohr            $this->footnotes[$fnid] = "@@FNT".($i);
458d74aace9Schris        }
459d74aace9Schris
4606b379cbfSAndreas Gohr        // output the footnote reference and link
46116ec3e37SAndreas Gohr        $this->doc .= '<sup><a href="#fn__'.$fnid.'" id="fnt__'.$fnid.'" class="fn_top">'.$fnid.')</a></sup>';
4620cecf9d5Sandi    }
4630cecf9d5Sandi
4643dd5c225SAndreas Gohr    /**
4653dd5c225SAndreas Gohr     * Open an unordered list
4660c4c0281SGerrit Uitslag     *
4677d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
4683dd5c225SAndreas Gohr     */
469de369923SAndreas Gohr    public function listu_open($classes = null) {
4700c4c0281SGerrit Uitslag        $class = '';
4710c4c0281SGerrit Uitslag        if($classes !== null) {
4722e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
4730c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
4740c4c0281SGerrit Uitslag        }
4750c4c0281SGerrit Uitslag        $this->doc .= "<ul$class>".DOKU_LF;
4760cecf9d5Sandi    }
4770cecf9d5Sandi
4783dd5c225SAndreas Gohr    /**
4793dd5c225SAndreas Gohr     * Close an unordered list
4803dd5c225SAndreas Gohr     */
481de369923SAndreas Gohr    public function listu_close() {
482a2d649c4Sandi        $this->doc .= '</ul>'.DOKU_LF;
4830cecf9d5Sandi    }
4840cecf9d5Sandi
4853dd5c225SAndreas Gohr    /**
4863dd5c225SAndreas Gohr     * Open an ordered list
4870c4c0281SGerrit Uitslag     *
4887d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
4893dd5c225SAndreas Gohr     */
490de369923SAndreas Gohr    public function listo_open($classes = null) {
4910c4c0281SGerrit Uitslag        $class = '';
4920c4c0281SGerrit Uitslag        if($classes !== null) {
4932e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
4940c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
4950c4c0281SGerrit Uitslag        }
4960c4c0281SGerrit Uitslag        $this->doc .= "<ol$class>".DOKU_LF;
4970cecf9d5Sandi    }
4980cecf9d5Sandi
4993dd5c225SAndreas Gohr    /**
5003dd5c225SAndreas Gohr     * Close an ordered list
5013dd5c225SAndreas Gohr     */
502de369923SAndreas Gohr    public function listo_close() {
503a2d649c4Sandi        $this->doc .= '</ol>'.DOKU_LF;
5040cecf9d5Sandi    }
5050cecf9d5Sandi
5063dd5c225SAndreas Gohr    /**
5073dd5c225SAndreas Gohr     * Open a list item
5083dd5c225SAndreas Gohr     *
5093dd5c225SAndreas Gohr     * @param int $level the nesting level
510e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
5113dd5c225SAndreas Gohr     */
512de369923SAndreas Gohr    public function listitem_open($level, $node=false) {
513e3a24861SChristopher Smith        $branching = $node ? ' node' : '';
514e3a24861SChristopher Smith        $this->doc .= '<li class="level'.$level.$branching.'">';
5150cecf9d5Sandi    }
5160cecf9d5Sandi
5173dd5c225SAndreas Gohr    /**
5183dd5c225SAndreas Gohr     * Close a list item
5193dd5c225SAndreas Gohr     */
520de369923SAndreas Gohr    public function listitem_close() {
521a2d649c4Sandi        $this->doc .= '</li>'.DOKU_LF;
5220cecf9d5Sandi    }
5230cecf9d5Sandi
5243dd5c225SAndreas Gohr    /**
5253dd5c225SAndreas Gohr     * Start the content of a list item
5263dd5c225SAndreas Gohr     */
527de369923SAndreas Gohr    public function listcontent_open() {
52890db23d7Schris        $this->doc .= '<div class="li">';
5290cecf9d5Sandi    }
5300cecf9d5Sandi
5313dd5c225SAndreas Gohr    /**
5323dd5c225SAndreas Gohr     * Stop the content of a list item
5333dd5c225SAndreas Gohr     */
534de369923SAndreas Gohr    public function listcontent_close() {
53559869a4bSAnika Henke        $this->doc .= '</div>'.DOKU_LF;
5360cecf9d5Sandi    }
5370cecf9d5Sandi
5383dd5c225SAndreas Gohr    /**
5393dd5c225SAndreas Gohr     * Output unformatted $text
5403dd5c225SAndreas Gohr     *
5413dd5c225SAndreas Gohr     * Defaults to $this->cdata()
5423dd5c225SAndreas Gohr     *
5433dd5c225SAndreas Gohr     * @param string $text
5443dd5c225SAndreas Gohr     */
545de369923SAndreas Gohr    public function unformatted($text) {
546a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
5470cecf9d5Sandi    }
5480cecf9d5Sandi
5490cecf9d5Sandi    /**
5503dd5c225SAndreas Gohr     * Start a block quote
5513dd5c225SAndreas Gohr     */
552de369923SAndreas Gohr    public function quote_open() {
55396331712SAnika Henke        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
5540cecf9d5Sandi    }
5550cecf9d5Sandi
5563dd5c225SAndreas Gohr    /**
5573dd5c225SAndreas Gohr     * Stop a block quote
5583dd5c225SAndreas Gohr     */
559de369923SAndreas Gohr    public function quote_close() {
56096331712SAnika Henke        $this->doc .= '</div></blockquote>'.DOKU_LF;
5610cecf9d5Sandi    }
5620cecf9d5Sandi
5633dd5c225SAndreas Gohr    /**
5643dd5c225SAndreas Gohr     * Output preformatted text
5653dd5c225SAndreas Gohr     *
5663dd5c225SAndreas Gohr     * @param string $text
5673dd5c225SAndreas Gohr     */
568de369923SAndreas Gohr    public function preformatted($text) {
569c9250713SAnika Henke        $this->doc .= '<pre class="code">'.trim($this->_xmlEntities($text), "\n\r").'</pre>'.DOKU_LF;
5703d491f75SAndreas Gohr    }
5713d491f75SAndreas Gohr
5723dd5c225SAndreas Gohr    /**
5733dd5c225SAndreas Gohr     * Display text as file content, optionally syntax highlighted
5743dd5c225SAndreas Gohr     *
5753dd5c225SAndreas Gohr     * @param string $text     text to show
5763dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5773dd5c225SAndreas Gohr     * @param string $filename file path label
578e2d88156SLarsDW223     * @param array  $options  assoziative array with additional geshi options
5793dd5c225SAndreas Gohr     */
580de369923SAndreas Gohr    public function file($text, $language = null, $filename = null, $options=null) {
581e2d88156SLarsDW223        $this->_highlight('file', $text, $language, $filename, $options);
5823d491f75SAndreas Gohr    }
5833d491f75SAndreas Gohr
5843dd5c225SAndreas Gohr    /**
5853dd5c225SAndreas Gohr     * Display text as code content, optionally syntax highlighted
5863dd5c225SAndreas Gohr     *
5873dd5c225SAndreas Gohr     * @param string $text     text to show
5883dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
5893dd5c225SAndreas Gohr     * @param string $filename file path label
590e2d88156SLarsDW223     * @param array  $options  assoziative array with additional geshi options
5913dd5c225SAndreas Gohr     */
592de369923SAndreas Gohr    public function code($text, $language = null, $filename = null, $options=null) {
593e2d88156SLarsDW223        $this->_highlight('code', $text, $language, $filename, $options);
5943d491f75SAndreas Gohr    }
5953d491f75SAndreas Gohr
5960cecf9d5Sandi    /**
5973d491f75SAndreas Gohr     * Use GeSHi to highlight language syntax in code and file blocks
5983fd0b676Sandi     *
5993fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
6003dd5c225SAndreas Gohr     * @param string $type     code|file
6013dd5c225SAndreas Gohr     * @param string $text     text to show
6023dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6033dd5c225SAndreas Gohr     * @param string $filename file path label
604e2d88156SLarsDW223     * @param array  $options  assoziative array with additional geshi options
6050cecf9d5Sandi     */
606de369923SAndreas Gohr    public function _highlight($type, $text, $language = null, $filename = null, $options = null) {
6073d491f75SAndreas Gohr        global $ID;
6083d491f75SAndreas Gohr        global $lang;
609ec57f119SLarsDW223        global $INPUT;
6103d491f75SAndreas Gohr
611bf8f8509SAndreas Gohr        $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language ?? '');
61256bd9509SPhy
6133d491f75SAndreas Gohr        if($filename) {
614190c56e8SAndreas Gohr            // add icon
61527bf7924STom N Harris            list($ext) = mimetype($filename, false);
616190c56e8SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
617190c56e8SAndreas Gohr            $class = 'mediafile mf_'.$class;
618190c56e8SAndreas Gohr
619ec57f119SLarsDW223            $offset = 0;
620ec57f119SLarsDW223            if ($INPUT->has('codeblockOffset')) {
621ec57f119SLarsDW223                $offset = $INPUT->str('codeblockOffset');
622ec57f119SLarsDW223            }
6233d491f75SAndreas Gohr            $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
62464159a61SAndreas Gohr            $this->doc .= '<dt><a href="' .
62564159a61SAndreas Gohr                exportlink(
62664159a61SAndreas Gohr                    $ID,
62764159a61SAndreas Gohr                    'code',
62864159a61SAndreas Gohr                    array('codeblock' => $offset + $this->_codeblock)
62964159a61SAndreas Gohr                ) . '" title="' . $lang['download'] . '" class="' . $class . '">';
6303d491f75SAndreas Gohr            $this->doc .= hsc($filename);
6313d491f75SAndreas Gohr            $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
6323d491f75SAndreas Gohr        }
6330cecf9d5Sandi
6342401f18dSSyntaxseed        if($text[0] == "\n") {
635d43aac1cSGina Haeussge            $text = substr($text, 1);
636d43aac1cSGina Haeussge        }
637d43aac1cSGina Haeussge        if(substr($text, -1) == "\n") {
638d43aac1cSGina Haeussge            $text = substr($text, 0, -1);
639d43aac1cSGina Haeussge        }
640d43aac1cSGina Haeussge
641a056e285SPhy        if(empty($language)) { // empty is faster than is_null and can prevent '' string
6423d491f75SAndreas Gohr            $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
6430cecf9d5Sandi        } else {
6443d491f75SAndreas Gohr            $class = 'code'; //we always need the code class to make the syntax highlighting apply
6453d491f75SAndreas Gohr            if($type != 'code') $class .= ' '.$type;
6463d491f75SAndreas Gohr
64764159a61SAndreas Gohr            $this->doc .= "<pre class=\"$class $language\">" .
64864159a61SAndreas Gohr                p_xhtml_cached_geshi($text, $language, '', $options) .
64964159a61SAndreas Gohr                '</pre>' . DOKU_LF;
6500cecf9d5Sandi        }
6513d491f75SAndreas Gohr
6523d491f75SAndreas Gohr        if($filename) {
6533d491f75SAndreas Gohr            $this->doc .= '</dd></dl>'.DOKU_LF;
6543d491f75SAndreas Gohr        }
6553d491f75SAndreas Gohr
6563d491f75SAndreas Gohr        $this->_codeblock++;
6570cecf9d5Sandi    }
6580cecf9d5Sandi
6593dd5c225SAndreas Gohr    /**
6603dd5c225SAndreas Gohr     * Format an acronym
6613dd5c225SAndreas Gohr     *
6623dd5c225SAndreas Gohr     * Uses $this->acronyms
6633dd5c225SAndreas Gohr     *
6643dd5c225SAndreas Gohr     * @param string $acronym
6653dd5c225SAndreas Gohr     */
666de369923SAndreas Gohr    public function acronym($acronym) {
6670cecf9d5Sandi
6680cecf9d5Sandi        if(array_key_exists($acronym, $this->acronyms)) {
6690cecf9d5Sandi
670433bef32Sandi            $title = $this->_xmlEntities($this->acronyms[$acronym]);
6710cecf9d5Sandi
672940db3a3SAnika Henke            $this->doc .= '<abbr title="'.$title
673940db3a3SAnika Henke                .'">'.$this->_xmlEntities($acronym).'</abbr>';
6740cecf9d5Sandi
6750cecf9d5Sandi        } else {
676a2d649c4Sandi            $this->doc .= $this->_xmlEntities($acronym);
6770cecf9d5Sandi        }
6780cecf9d5Sandi    }
6790cecf9d5Sandi
6803dd5c225SAndreas Gohr    /**
6813dd5c225SAndreas Gohr     * Format a smiley
6823dd5c225SAndreas Gohr     *
6833dd5c225SAndreas Gohr     * Uses $this->smiley
6843dd5c225SAndreas Gohr     *
6853dd5c225SAndreas Gohr     * @param string $smiley
6863dd5c225SAndreas Gohr     */
687de369923SAndreas Gohr    public function smiley($smiley) {
688b09504a9SAndreas Gohr        if (isset($this->smileys[$smiley])) {
689f62ea8a1Sandi            $this->doc .= '<img src="' . DOKU_BASE . 'lib/images/smileys/' . $this->smileys[$smiley] .
690b09504a9SAndreas Gohr                '" class="icon smiley" alt="' . $this->_xmlEntities($smiley) . '" />';
6910cecf9d5Sandi        } else {
692a2d649c4Sandi            $this->doc .= $this->_xmlEntities($smiley);
6930cecf9d5Sandi        }
6940cecf9d5Sandi    }
6950cecf9d5Sandi
6963dd5c225SAndreas Gohr    /**
6973dd5c225SAndreas Gohr     * Format an entity
6983dd5c225SAndreas Gohr     *
6993dd5c225SAndreas Gohr     * Entities are basically small text replacements
7003dd5c225SAndreas Gohr     *
7013dd5c225SAndreas Gohr     * Uses $this->entities
7023dd5c225SAndreas Gohr     *
7033dd5c225SAndreas Gohr     * @param string $entity
7044de671bcSandi     */
705de369923SAndreas Gohr    public function entity($entity) {
7060cecf9d5Sandi        if(array_key_exists($entity, $this->entities)) {
707a2d649c4Sandi            $this->doc .= $this->entities[$entity];
7080cecf9d5Sandi        } else {
709a2d649c4Sandi            $this->doc .= $this->_xmlEntities($entity);
7100cecf9d5Sandi        }
7110cecf9d5Sandi    }
7120cecf9d5Sandi
7133dd5c225SAndreas Gohr    /**
7143dd5c225SAndreas Gohr     * Typographically format a multiply sign
7153dd5c225SAndreas Gohr     *
7163dd5c225SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
7173dd5c225SAndreas Gohr     *
7183dd5c225SAndreas Gohr     * @param string|int $x first value
7193dd5c225SAndreas Gohr     * @param string|int $y second value
7203dd5c225SAndreas Gohr     */
721de369923SAndreas Gohr    public function multiplyentity($x, $y) {
722a2d649c4Sandi        $this->doc .= "$x&times;$y";
7230cecf9d5Sandi    }
7240cecf9d5Sandi
7253dd5c225SAndreas Gohr    /**
7263dd5c225SAndreas Gohr     * Render an opening single quote char (language specific)
7273dd5c225SAndreas Gohr     */
728de369923SAndreas Gohr    public function singlequoteopening() {
72971b40da2SAnika Henke        global $lang;
73071b40da2SAnika Henke        $this->doc .= $lang['singlequoteopening'];
7310cecf9d5Sandi    }
7320cecf9d5Sandi
7333dd5c225SAndreas Gohr    /**
7343dd5c225SAndreas Gohr     * Render a closing single quote char (language specific)
7353dd5c225SAndreas Gohr     */
736de369923SAndreas Gohr    public function singlequoteclosing() {
73771b40da2SAnika Henke        global $lang;
73871b40da2SAnika Henke        $this->doc .= $lang['singlequoteclosing'];
7390cecf9d5Sandi    }
7400cecf9d5Sandi
7413dd5c225SAndreas Gohr    /**
7423dd5c225SAndreas Gohr     * Render an apostrophe char (language specific)
7433dd5c225SAndreas Gohr     */
744de369923SAndreas Gohr    public function apostrophe() {
74557d757d1SAndreas Gohr        global $lang;
746a8bd192aSAndreas Gohr        $this->doc .= $lang['apostrophe'];
74757d757d1SAndreas Gohr    }
74857d757d1SAndreas Gohr
7493dd5c225SAndreas Gohr    /**
7503dd5c225SAndreas Gohr     * Render an opening double quote char (language specific)
7513dd5c225SAndreas Gohr     */
752de369923SAndreas Gohr    public function doublequoteopening() {
75371b40da2SAnika Henke        global $lang;
75471b40da2SAnika Henke        $this->doc .= $lang['doublequoteopening'];
7550cecf9d5Sandi    }
7560cecf9d5Sandi
7573dd5c225SAndreas Gohr    /**
7583dd5c225SAndreas Gohr     * Render an closinging double quote char (language specific)
7593dd5c225SAndreas Gohr     */
760de369923SAndreas Gohr    public function doublequoteclosing() {
76171b40da2SAnika Henke        global $lang;
76271b40da2SAnika Henke        $this->doc .= $lang['doublequoteclosing'];
7630cecf9d5Sandi    }
7640cecf9d5Sandi
7650cecf9d5Sandi    /**
7663dd5c225SAndreas Gohr     * Render a CamelCase link
7673dd5c225SAndreas Gohr     *
7683dd5c225SAndreas Gohr     * @param string $link       The link name
769122f2d46SAndreas Böhler     * @param bool   $returnonly whether to return html or write to doc attribute
7700c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
7710c4c0281SGerrit Uitslag     *
7723dd5c225SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
7730cecf9d5Sandi     */
774de369923SAndreas Gohr    public function camelcaselink($link, $returnonly = false) {
775122f2d46SAndreas Böhler        if($returnonly) {
776122f2d46SAndreas Böhler          return $this->internallink($link, $link, null, true);
777122f2d46SAndreas Böhler        } else {
77811d0aa47Sandi          $this->internallink($link, $link);
7790cecf9d5Sandi        }
780122f2d46SAndreas Böhler    }
7810cecf9d5Sandi
7823dd5c225SAndreas Gohr    /**
7833dd5c225SAndreas Gohr     * Render a page local link
7843dd5c225SAndreas Gohr     *
7853dd5c225SAndreas Gohr     * @param string $hash       hash link identifier
7863dd5c225SAndreas Gohr     * @param string $name       name for the link
787122f2d46SAndreas Böhler     * @param bool   $returnonly whether to return html or write to doc attribute
7880c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
7893dd5c225SAndreas Gohr     */
790de369923SAndreas Gohr    public function locallink($hash, $name = null, $returnonly = false) {
7910b7c14c2Sandi        global $ID;
7920b7c14c2Sandi        $name  = $this->_getLinkTitle($name, $hash, $isImage);
7930b7c14c2Sandi        $hash  = $this->_headerToLink($hash);
794e260f93bSAnika Henke        $title = $ID.' ↵';
795122f2d46SAndreas Böhler
796122f2d46SAndreas Böhler        $doc = '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
797122f2d46SAndreas Böhler        $doc .= $name;
798122f2d46SAndreas Böhler        $doc .= '</a>';
799122f2d46SAndreas Böhler
800122f2d46SAndreas Böhler        if($returnonly) {
801122f2d46SAndreas Böhler          return $doc;
802122f2d46SAndreas Böhler        } else {
803122f2d46SAndreas Böhler          $this->doc .= $doc;
804122f2d46SAndreas Böhler        }
8050b7c14c2Sandi    }
8060b7c14c2Sandi
807cffcc403Sandi    /**
8083fd0b676Sandi     * Render an internal Wiki Link
8093fd0b676Sandi     *
810fe9ec250SChris Smith     * $search,$returnonly & $linktype are not for the renderer but are used
811cffcc403Sandi     * elsewhere - no need to implement them in other renderers
8123fd0b676Sandi     *
8133dd5c225SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
814f23eef27SGerrit Uitslag     * @param string      $id         pageid
815f23eef27SGerrit Uitslag     * @param string|null $name       link name
816f23eef27SGerrit Uitslag     * @param string|null $search     adds search url param
817f23eef27SGerrit Uitslag     * @param bool        $returnonly whether to return html or write to doc attribute
818f23eef27SGerrit Uitslag     * @param string      $linktype   type to set use of headings
819f23eef27SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
820cffcc403Sandi     */
821de369923SAndreas Gohr    public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') {
822ba11bd29Sandi        global $conf;
82337e34a5eSandi        global $ID;
824c4dda6afSAnika Henke        global $INFO;
82544653a53SAdrian Lang
8263d5e07d9SAdrian Lang        $params = '';
8273d5e07d9SAdrian Lang        $parts  = explode('?', $id, 2);
8283d5e07d9SAdrian Lang        if(count($parts) === 2) {
8293d5e07d9SAdrian Lang            $id     = $parts[0];
8303d5e07d9SAdrian Lang            $params = $parts[1];
83144653a53SAdrian Lang        }
83244653a53SAdrian Lang
833fda14ffcSIzidor Matušov        // For empty $id we need to know the current $ID
834fda14ffcSIzidor Matušov        // We need this check because _simpleTitle needs
835fda14ffcSIzidor Matušov        // correct $id and resolve_pageid() use cleanID($id)
836fda14ffcSIzidor Matušov        // (some things could be lost)
837fda14ffcSIzidor Matušov        if($id === '') {
838fda14ffcSIzidor Matušov            $id = $ID;
839fda14ffcSIzidor Matušov        }
840fda14ffcSIzidor Matušov
8410339c872Sjan        // default name is based on $id as given
8420339c872Sjan        $default = $this->_simpleTitle($id);
843ad32e47eSAndreas Gohr
8440339c872Sjan        // now first resolve and clean up the $id
8458c6be208SAndreas Gohr        $id = (new PageResolver($ID))->resolveId($id, $this->date_at, true);
8468c6be208SAndreas Gohr        $exists = page_exists($id, $this->date_at, false, true);
847fda14ffcSIzidor Matušov
84859bc3b48SGerrit Uitslag        $link = array();
849fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
8500e1c636eSandi        if(!$isImage) {
8510e1c636eSandi            if($exists) {
852ba11bd29Sandi                $class = 'wikilink1';
8530cecf9d5Sandi            } else {
854ba11bd29Sandi                $class       = 'wikilink2';
85544a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
8560cecf9d5Sandi            }
8570cecf9d5Sandi        } else {
858ba11bd29Sandi            $class = 'media';
8590cecf9d5Sandi        }
8600cecf9d5Sandi
861a1685bedSandi        //keep hash anchor
862ec34bb30SAndreas Gohr        list($id, $hash) = sexplode('#', $id, 2);
863943dedc6SAndreas Gohr        if(!empty($hash)) $hash = $this->_headerToLink($hash);
864a1685bedSandi
865ba11bd29Sandi        //prepare for formating
866ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
867ba11bd29Sandi        $link['style']  = '';
868ba11bd29Sandi        $link['pre']    = '';
869ba11bd29Sandi        $link['suf']    = '';
870bbac1489SPhy        $link['more']   = 'data-wiki-id="'.$id.'"'; // id is already cleaned
871ba11bd29Sandi        $link['class']  = $class;
8725c2eed9aSlisps        if($this->date_at) {
873912a6d48SPhy            $params = $params.'&at='.rawurlencode($this->date_at);
8745c2eed9aSlisps        }
87544653a53SAdrian Lang        $link['url']    = wl($id, $params);
876ba11bd29Sandi        $link['name']   = $name;
877ba11bd29Sandi        $link['title']  = $id;
878723d78dbSandi        //add search string
879723d78dbSandi        if($search) {
880546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
881546d3a99SAndreas Gohr            if(is_array($search)) {
882546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
883546d3a99SAndreas Gohr                $link['url'] .= 's[]='.join('&amp;s[]=', $search);
884546d3a99SAndreas Gohr            } else {
885546d3a99SAndreas Gohr                $link['url'] .= 's='.rawurlencode($search);
886546d3a99SAndreas Gohr            }
887723d78dbSandi        }
888723d78dbSandi
889a1685bedSandi        //keep hash
890a1685bedSandi        if($hash) $link['url'] .= '#'.$hash;
891a1685bedSandi
892ba11bd29Sandi        //output formatted
893cffcc403Sandi        if($returnonly) {
894cffcc403Sandi            return $this->_formatLink($link);
895cffcc403Sandi        } else {
896a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
8970cecf9d5Sandi        }
898cffcc403Sandi    }
8990cecf9d5Sandi
9003dd5c225SAndreas Gohr    /**
9013dd5c225SAndreas Gohr     * Render an external link
9023dd5c225SAndreas Gohr     *
9033dd5c225SAndreas Gohr     * @param string       $url        full URL with scheme
9043dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
905122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
9060c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
9073dd5c225SAndreas Gohr     */
908de369923SAndreas Gohr    public function externallink($url, $name = null, $returnonly = false) {
909b625487dSandi        global $conf;
9100cecf9d5Sandi
911433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
9126f0c5dbfSandi
913b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
914b52b1596SAndreas Gohr        if(is_null($this->schemes)) $this->schemes = getSchemes();
915b52b1596SAndreas Gohr        list($scheme) = explode('://', $url);
916b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
917b52b1596SAndreas Gohr        if(!in_array($scheme, $this->schemes)) $url = '';
918b52b1596SAndreas Gohr
919b52b1596SAndreas Gohr        // is there still an URL?
920b52b1596SAndreas Gohr        if(!$url) {
92149cef4fdSAndreas Böhler            if($returnonly) {
92249cef4fdSAndreas Böhler                return $name;
92349cef4fdSAndreas Böhler            } else {
924b52b1596SAndreas Gohr                $this->doc .= $name;
92549cef4fdSAndreas Böhler            }
926b52b1596SAndreas Gohr            return;
927b52b1596SAndreas Gohr        }
928b52b1596SAndreas Gohr
929b52b1596SAndreas Gohr        // set class
9300cecf9d5Sandi        if(!$isImage) {
931b625487dSandi            $class = 'urlextern';
9320cecf9d5Sandi        } else {
933b625487dSandi            $class = 'media';
9340cecf9d5Sandi        }
9350cecf9d5Sandi
936b625487dSandi        //prepare for formating
93759bc3b48SGerrit Uitslag        $link = array();
938b625487dSandi        $link['target'] = $conf['target']['extern'];
939b625487dSandi        $link['style']  = '';
940b625487dSandi        $link['pre']    = '';
941b625487dSandi        $link['suf']    = '';
9425e163278SAndreas Gohr        $link['more']   = '';
943b625487dSandi        $link['class']  = $class;
944b625487dSandi        $link['url']    = $url;
945914045f3SAndreas Gohr        $link['rel']    = '';
946e1c10e4dSchris
947b625487dSandi        $link['name']  = $name;
948433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
9495ddd0bbbSStarArmy        if($conf['relnofollow']) $link['rel'] .= ' ugc nofollow';
950914045f3SAndreas Gohr        if($conf['target']['extern']) $link['rel'] .= ' noopener';
9510cecf9d5Sandi
952b625487dSandi        //output formatted
953122f2d46SAndreas Böhler        if($returnonly) {
954122f2d46SAndreas Böhler            return $this->_formatLink($link);
955122f2d46SAndreas Böhler        } else {
956a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
9570cecf9d5Sandi        }
958122f2d46SAndreas Böhler    }
9590cecf9d5Sandi
9600cecf9d5Sandi    /**
9613dd5c225SAndreas Gohr     * Render an interwiki link
9623dd5c225SAndreas Gohr     *
9633dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
9643dd5c225SAndreas Gohr     *
9653dd5c225SAndreas Gohr     * @param string       $match      original link - probably not much use
9663dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
9673dd5c225SAndreas Gohr     * @param string       $wikiName   indentifier (shortcut) for the remote wiki
9683dd5c225SAndreas Gohr     * @param string       $wikiUri    the fragment parsed from the original link
969122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
9700c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
9710cecf9d5Sandi     */
972de369923SAndreas Gohr    public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false) {
973b625487dSandi        global $conf;
9740cecf9d5Sandi
97597a3e4e3Sandi        $link           = array();
97697a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
97797a3e4e3Sandi        $link['pre']    = '';
97897a3e4e3Sandi        $link['suf']    = '';
9795e163278SAndreas Gohr        $link['more']   = '';
980433bef32Sandi        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
981914045f3SAndreas Gohr        $link['rel']    = '';
9820cecf9d5Sandi
98397a3e4e3Sandi        //get interwiki URL
9846496c33fSGerrit Uitslag        $exists = null;
9856496c33fSGerrit Uitslag        $url    = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
9860cecf9d5Sandi
98797a3e4e3Sandi        if(!$isImage) {
9889d2ddea4SAndreas Gohr            $class         = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
9899d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
9901c2d1019SAndreas Gohr        } else {
9911c2d1019SAndreas Gohr            $link['class'] = 'media';
99297a3e4e3Sandi        }
9930cecf9d5Sandi
99497a3e4e3Sandi        //do we stay at the same server? Use local target
9952345e871SGerrit Uitslag        if(strpos($url, DOKU_URL) === 0 OR strpos($url, DOKU_BASE) === 0) {
99697a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
99797a3e4e3Sandi        }
9986496c33fSGerrit Uitslag        if($exists !== null && !$isImage) {
9996496c33fSGerrit Uitslag            if($exists) {
10006496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
10016496c33fSGerrit Uitslag            } else {
10026496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
1003914045f3SAndreas Gohr                $link['rel'] .= ' nofollow';
10046496c33fSGerrit Uitslag            }
10056496c33fSGerrit Uitslag        }
1006914045f3SAndreas Gohr        if($conf['target']['interwiki']) $link['rel'] .= ' noopener';
10070cecf9d5Sandi
100897a3e4e3Sandi        $link['url']   = $url;
1009f7711f2bSAndreas Gohr        $link['title'] = $this->_xmlEntities($link['url']);
101097a3e4e3Sandi
101197a3e4e3Sandi        // output formatted
1012122f2d46SAndreas Böhler        if($returnonly) {
1013abde5980SPhy            if($url == '') return $link['name'];
1014122f2d46SAndreas Böhler            return $this->_formatLink($link);
1015122f2d46SAndreas Böhler        } else {
1016abde5980SPhy            if($url == '') $this->doc .= $link['name'];
1017abde5980SPhy            else $this->doc .= $this->_formatLink($link);
10180cecf9d5Sandi        }
1019122f2d46SAndreas Böhler    }
10200cecf9d5Sandi
10210cecf9d5Sandi    /**
10223dd5c225SAndreas Gohr     * Link to windows share
10233dd5c225SAndreas Gohr     *
10243dd5c225SAndreas Gohr     * @param string       $url        the link
10253dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
1026122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
10270c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10280cecf9d5Sandi     */
1029de369923SAndreas Gohr    public function windowssharelink($url, $name = null, $returnonly = false) {
10301d47afe1Sandi        global $conf;
10313dd5c225SAndreas Gohr
10321d47afe1Sandi        //simple setup
103359bc3b48SGerrit Uitslag        $link = array();
10341d47afe1Sandi        $link['target'] = $conf['target']['windows'];
10351d47afe1Sandi        $link['pre']    = '';
10361d47afe1Sandi        $link['suf']    = '';
10371d47afe1Sandi        $link['style']  = '';
10380cecf9d5Sandi
1039433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
10400cecf9d5Sandi        if(!$isImage) {
10411d47afe1Sandi            $link['class'] = 'windows';
10420cecf9d5Sandi        } else {
10431d47afe1Sandi            $link['class'] = 'media';
10440cecf9d5Sandi        }
10450cecf9d5Sandi
1046433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
10471d47afe1Sandi        $url           = str_replace('\\', '/', $url);
10481d47afe1Sandi        $url           = 'file:///'.$url;
10491d47afe1Sandi        $link['url']   = $url;
10500cecf9d5Sandi
10511d47afe1Sandi        //output formatted
1052122f2d46SAndreas Böhler        if($returnonly) {
1053122f2d46SAndreas Böhler            return $this->_formatLink($link);
1054122f2d46SAndreas Böhler        } else {
1055a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
10560cecf9d5Sandi        }
1057122f2d46SAndreas Böhler    }
10580cecf9d5Sandi
10593dd5c225SAndreas Gohr    /**
10603dd5c225SAndreas Gohr     * Render a linked E-Mail Address
10613dd5c225SAndreas Gohr     *
10623dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
10633dd5c225SAndreas Gohr     *
10643dd5c225SAndreas Gohr     * @param string       $address    Email-Address
10653dd5c225SAndreas Gohr     * @param string|array $name       name for the link, array for media file
1066122f2d46SAndreas Böhler     * @param bool         $returnonly whether to return html or write to doc attribute
10670c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10683dd5c225SAndreas Gohr     */
1069de369923SAndreas Gohr    public function emaillink($address, $name = null, $returnonly = false) {
107071352defSandi        global $conf;
107171352defSandi        //simple setup
107271352defSandi        $link           = array();
107371352defSandi        $link['target'] = '';
107471352defSandi        $link['pre']    = '';
107571352defSandi        $link['suf']    = '';
107671352defSandi        $link['style']  = '';
107771352defSandi        $link['more']   = '';
10780cecf9d5Sandi
1079c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
10800cecf9d5Sandi        if(!$isImage) {
1081be96545cSAnika Henke            $link['class'] = 'mail';
10820cecf9d5Sandi        } else {
1083be96545cSAnika Henke            $link['class'] = 'media';
10840cecf9d5Sandi        }
10850cecf9d5Sandi
108607738714SAndreas Gohr        $address = $this->_xmlEntities($address);
108700a7b5adSEsther Brunner        $address = obfuscate($address);
108800a7b5adSEsther Brunner        $title   = $address;
10898c128049SAndreas Gohr
109071352defSandi        if(empty($name)) {
109100a7b5adSEsther Brunner            $name = $address;
109271352defSandi        }
10930cecf9d5Sandi
1094776b36ecSAndreas Gohr        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1095776b36ecSAndreas Gohr
1096776b36ecSAndreas Gohr        $link['url']   = 'mailto:'.$address;
109771352defSandi        $link['name']  = $name;
109871352defSandi        $link['title'] = $title;
10990cecf9d5Sandi
110071352defSandi        //output formatted
1101122f2d46SAndreas Böhler        if($returnonly) {
1102122f2d46SAndreas Böhler            return $this->_formatLink($link);
1103122f2d46SAndreas Böhler        } else {
1104a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
11050cecf9d5Sandi        }
1106122f2d46SAndreas Böhler    }
11070cecf9d5Sandi
11083dd5c225SAndreas Gohr    /**
11093dd5c225SAndreas Gohr     * Render an internal media file
11103dd5c225SAndreas Gohr     *
11113dd5c225SAndreas Gohr     * @param string $src       media ID
11123dd5c225SAndreas Gohr     * @param string $title     descriptive text
11133dd5c225SAndreas Gohr     * @param string $align     left|center|right
11143dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
11153dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
11163dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
11173dd5c225SAndreas Gohr     * @param string $linking   linkonly|detail|nolink
11183dd5c225SAndreas Gohr     * @param bool   $return    return HTML instead of adding to $doc
11190c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
11203dd5c225SAndreas Gohr     */
1121de369923SAndreas Gohr    public function internalmedia($src, $title = null, $align = null, $width = null,
11223dd5c225SAndreas Gohr                           $height = null, $cache = null, $linking = null, $return = false) {
112337e34a5eSandi        global $ID;
11248f34cf3dSMichael Große        if (strpos($src, '#') !== false) {
1125ec34bb30SAndreas Gohr            list($src, $hash) = sexplode('#', $src, 2);
11268f34cf3dSMichael Große        }
11278c6be208SAndreas Gohr        $src = (new MediaResolver($ID))->resolveId($src,$this->date_at,true);
11288c6be208SAndreas Gohr        $exists = media_exists($src);
11290cecf9d5Sandi
1130d98d4540SBen Coburn        $noLink = false;
11318acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1132b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
11333685f775Sandi
11343dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1135b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
113664159a61SAndreas Gohr            $link['url'] = ml(
113764159a61SAndreas Gohr                $src,
113864159a61SAndreas Gohr                array(
113964159a61SAndreas Gohr                    'id' => $ID,
114064159a61SAndreas Gohr                    'cache' => $cache,
114164159a61SAndreas Gohr                    'rev' => $this->_getLastMediaRevisionAt($src)
114264159a61SAndreas Gohr                ),
114364159a61SAndreas Gohr                ($linking == 'direct')
114464159a61SAndreas Gohr            );
1145f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
11462a2a2ba2SAnika Henke            // don't link movies
114744881bd0Shenning.noren            $noLink = true;
114855efc227SAndreas Gohr        } else {
11492ca14335SEsther Brunner            // add file icons
11509d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
11519d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_'.$class;
115264159a61SAndreas Gohr            $link['url'] = ml(
115364159a61SAndreas Gohr                $src,
115464159a61SAndreas Gohr                array(
115564159a61SAndreas Gohr                    'id' => $ID,
115664159a61SAndreas Gohr                    'cache' => $cache,
115764159a61SAndreas Gohr                    'rev' => $this->_getLastMediaRevisionAt($src)
115864159a61SAndreas Gohr                ),
115964159a61SAndreas Gohr                true
116064159a61SAndreas Gohr            );
116191328684SMichael Hamann            if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
116255efc227SAndreas Gohr        }
11633685f775Sandi
11648f34cf3dSMichael Große        if (!empty($hash)) $link['url'] .= '#'.$hash;
116591df343aSAndreas Gohr
11666fe20453SGina Haeussge        //markup non existing files
11674a24b459SKate Arzamastseva        if(!$exists) {
11686fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
11694a24b459SKate Arzamastseva        }
11706fe20453SGina Haeussge
11713685f775Sandi        //output formatted
1172f50634f0SAnika Henke        if($return) {
1173f50634f0SAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1174f50634f0SAnika Henke            else return $this->_formatLink($link);
1175f50634f0SAnika Henke        } else {
1176dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
11772ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
11780cecf9d5Sandi        }
1179f50634f0SAnika Henke    }
11800cecf9d5Sandi
11813dd5c225SAndreas Gohr    /**
11823dd5c225SAndreas Gohr     * Render an external media file
11833dd5c225SAndreas Gohr     *
11843dd5c225SAndreas Gohr     * @param string $src     full media URL
11853dd5c225SAndreas Gohr     * @param string $title   descriptive text
11863dd5c225SAndreas Gohr     * @param string $align   left|center|right
11873dd5c225SAndreas Gohr     * @param int    $width   width of media in pixel
11883dd5c225SAndreas Gohr     * @param int    $height  height of media in pixel
11893dd5c225SAndreas Gohr     * @param string $cache   cache|recache|nocache
11903dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1191410ee62aSAnika Henke     * @param bool   $return  return HTML instead of adding to $doc
11920c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
11933dd5c225SAndreas Gohr     */
1194de369923SAndreas Gohr    public function externalmedia($src, $title = null, $align = null, $width = null,
1195410ee62aSAnika Henke                           $height = null, $cache = null, $linking = null, $return = false) {
11966efc45a2SDmitry Katsubo        if(link_isinterwiki($src)){
1197ec34bb30SAndreas Gohr            list($shortcut, $reference) = sexplode('>', $src, 2, '');
11986efc45a2SDmitry Katsubo            $exists = null;
11996efc45a2SDmitry Katsubo            $src = $this->_resolveInterWiki($shortcut, $reference, $exists);
1200abde5980SPhy            if($src == '' && empty($title)){
1201abde5980SPhy                // make sure at least something will be shown in this case
1202abde5980SPhy                $title = $reference;
1203abde5980SPhy            }
12046efc45a2SDmitry Katsubo        }
1205ec34bb30SAndreas Gohr        list($src, $hash) = sexplode('#', $src, 2);
1206d98d4540SBen Coburn        $noLink = false;
1207abde5980SPhy        if($src == '') {
1208abde5980SPhy            // only output plaintext without link if there is no src
1209abde5980SPhy            $noLink = true;
1210abde5980SPhy        }
12118acb3108SAndreas Gohr        $render = ($linking == 'linkonly') ? false : true;
1212b739ff0fSPierre Spring        $link   = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1213b739ff0fSPierre Spring
1214b739ff0fSPierre Spring        $link['url'] = ml($src, array('cache' => $cache));
12153685f775Sandi
12163dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src, false);
1217b739ff0fSPierre Spring        if(substr($mime, 0, 5) == 'image' && $render) {
12182ca14335SEsther Brunner            // link only jpeg images
121944881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1220f50634f0SAnika Henke        } elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
12212a2a2ba2SAnika Henke            // don't link movies
122244881bd0Shenning.noren            $noLink = true;
12232ca14335SEsther Brunner        } else {
12242ca14335SEsther Brunner            // add file icons
122527bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
122627bf7924STom N Harris            $link['class'] .= ' mediafile mf_'.$class;
12272ca14335SEsther Brunner        }
12282ca14335SEsther Brunner
122991df343aSAndreas Gohr        if($hash) $link['url'] .= '#'.$hash;
123091df343aSAndreas Gohr
12313685f775Sandi        //output formatted
1232410ee62aSAnika Henke        if($return) {
1233410ee62aSAnika Henke            if($linking == 'nolink' || $noLink) return $link['name'];
1234410ee62aSAnika Henke            else return $this->_formatLink($link);
1235410ee62aSAnika Henke        } else {
1236dc673a5bSjoe.lapp            if($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
12372ca14335SEsther Brunner            else $this->doc .= $this->_formatLink($link);
12380cecf9d5Sandi        }
1239410ee62aSAnika Henke    }
12400cecf9d5Sandi
12414826ab45Sandi    /**
12423db95becSAndreas Gohr     * Renders an RSS feed
1243b625487dSandi     *
12440c4c0281SGerrit Uitslag     * @param string $url    URL of the feed
12450c4c0281SGerrit Uitslag     * @param array  $params Finetuning of the output
12460c4c0281SGerrit Uitslag     *
1247b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1248b625487dSandi     */
1249de369923SAndreas Gohr    public function rss($url, $params) {
1250b625487dSandi        global $lang;
12513db95becSAndreas Gohr        global $conf;
12523db95becSAndreas Gohr
12533db95becSAndreas Gohr        require_once(DOKU_INC.'inc/FeedParser.php');
12543db95becSAndreas Gohr        $feed = new FeedParser();
125500077af8SAndreas Gohr        $feed->set_feed_url($url);
1256b625487dSandi
1257b625487dSandi        //disable warning while fetching
12583dd5c225SAndreas Gohr        if(!defined('DOKU_E_LEVEL')) {
12593dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
12603dd5c225SAndreas Gohr        }
12613db95becSAndreas Gohr        $rc = $feed->init();
12623dd5c225SAndreas Gohr        if(isset($elvl)) {
12633dd5c225SAndreas Gohr            error_reporting($elvl);
12643dd5c225SAndreas Gohr        }
1265b625487dSandi
126638c6f603SRobin H. Johnson        if($params['nosort']) $feed->enable_order_by_date(false);
126738c6f603SRobin H. Johnson
12683db95becSAndreas Gohr        //decide on start and end
12693db95becSAndreas Gohr        if($params['reverse']) {
12703db95becSAndreas Gohr            $mod   = -1;
12713db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
12723db95becSAndreas Gohr            $end   = $start - ($params['max']);
1273b2a412b0SAndreas Gohr            $end   = ($end < -1) ? -1 : $end;
12743db95becSAndreas Gohr        } else {
12753db95becSAndreas Gohr            $mod   = 1;
12763db95becSAndreas Gohr            $start = 0;
12773db95becSAndreas Gohr            $end   = $feed->get_item_quantity();
1278d91ab76fSMatt Perry            $end   = ($end > $params['max']) ? $params['max'] : $end;
12793db95becSAndreas Gohr        }
12803db95becSAndreas Gohr
1281a2d649c4Sandi        $this->doc .= '<ul class="rss">';
12823db95becSAndreas Gohr        if($rc) {
12833db95becSAndreas Gohr            for($x = $start; $x != $end; $x += $mod) {
12841bde1582SAndreas Gohr                $item = $feed->get_item($x);
12853db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
1286d2ea3363SAndreas Gohr                // support feeds without links
1287d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
1288d2ea3363SAndreas Gohr                if($lnkurl) {
1289793361f8SAndreas Gohr                    // title is escaped by SimplePie, we unescape here because it
1290793361f8SAndreas Gohr                    // is escaped again in externallink() FS#1705
12913dd5c225SAndreas Gohr                    $this->externallink(
12923dd5c225SAndreas Gohr                        $item->get_permalink(),
12933dd5c225SAndreas Gohr                        html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8')
12943dd5c225SAndreas Gohr                    );
1295d2ea3363SAndreas Gohr                } else {
1296d2ea3363SAndreas Gohr                    $this->doc .= ' '.$item->get_title();
1297d2ea3363SAndreas Gohr                }
12983db95becSAndreas Gohr                if($params['author']) {
12991bde1582SAndreas Gohr                    $author = $item->get_author(0);
13001bde1582SAndreas Gohr                    if($author) {
13011bde1582SAndreas Gohr                        $name = $author->get_name();
13021bde1582SAndreas Gohr                        if(!$name) $name = $author->get_email();
1303163c2842SPhy                        if($name) $this->doc .= ' '.$lang['by'].' '.hsc($name);
13041bde1582SAndreas Gohr                    }
13053db95becSAndreas Gohr                }
13063db95becSAndreas Gohr                if($params['date']) {
13072e7e0c29SAndreas Gohr                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
13083db95becSAndreas Gohr                }
13091bde1582SAndreas Gohr                if($params['details']) {
13103db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
13111bde1582SAndreas Gohr                    $this->doc .= strip_tags($item->get_description());
13123db95becSAndreas Gohr                    $this->doc .= '</div>';
13133db95becSAndreas Gohr                }
13143db95becSAndreas Gohr
13153db95becSAndreas Gohr                $this->doc .= '</div></li>';
1316b625487dSandi            }
1317b625487dSandi        } else {
13183db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1319a2d649c4Sandi            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
1320b625487dSandi            $this->externallink($url);
132145e147ccSAndreas Gohr            if($conf['allowdebug']) {
132245e147ccSAndreas Gohr                $this->doc .= '<!--'.hsc($feed->error).'-->';
132345e147ccSAndreas Gohr            }
13243db95becSAndreas Gohr            $this->doc .= '</div></li>';
1325b625487dSandi        }
1326a2d649c4Sandi        $this->doc .= '</ul>';
1327b625487dSandi    }
1328b625487dSandi
13293dd5c225SAndreas Gohr    /**
13303dd5c225SAndreas Gohr     * Start a table
13313dd5c225SAndreas Gohr     *
13323dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
13333dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
13343dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
13357d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
13363dd5c225SAndreas Gohr     */
1337de369923SAndreas Gohr    public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null) {
1338b5742cedSPierre Spring        // initialize the row counter used for classes
1339b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1340619736fdSAdrian Lang        $class                         = 'table';
13410c4c0281SGerrit Uitslag        if($classes !== null) {
13422e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
13430c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
13440c4c0281SGerrit Uitslag        }
1345619736fdSAdrian Lang        if($pos !== null) {
134606917fceSMichael Große            $hid = $this->_headerToLink($class, true);
1347ec57f119SLarsDW223            $data = array();
1348ec57f119SLarsDW223            $data['target'] = 'table';
1349ec57f119SLarsDW223            $data['name'] = '';
1350ec57f119SLarsDW223            $data['hid'] = $hid;
1351ec57f119SLarsDW223            $class .= ' '.$this->startSectionEdit($pos, $data);
1352619736fdSAdrian Lang        }
1353619736fdSAdrian Lang        $this->doc .= '<div class="'.$class.'"><table class="inline">'.
1354619736fdSAdrian Lang            DOKU_LF;
13550cecf9d5Sandi    }
13560cecf9d5Sandi
13573dd5c225SAndreas Gohr    /**
13583dd5c225SAndreas Gohr     * Close a table
13593dd5c225SAndreas Gohr     *
13603dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
13613dd5c225SAndreas Gohr     */
1362de369923SAndreas Gohr    public function table_close($pos = null) {
1363a8574918SAnika Henke        $this->doc .= '</table></div>'.DOKU_LF;
1364619736fdSAdrian Lang        if($pos !== null) {
136590df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
13660cecf9d5Sandi        }
1367619736fdSAdrian Lang    }
13680cecf9d5Sandi
13693dd5c225SAndreas Gohr    /**
13703dd5c225SAndreas Gohr     * Open a table header
13713dd5c225SAndreas Gohr     */
1372de369923SAndreas Gohr    public function tablethead_open() {
1373f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'<thead>'.DOKU_LF;
1374f05a1cc5SGerrit Uitslag    }
1375f05a1cc5SGerrit Uitslag
13763dd5c225SAndreas Gohr    /**
13773dd5c225SAndreas Gohr     * Close a table header
13783dd5c225SAndreas Gohr     */
1379de369923SAndreas Gohr    public function tablethead_close() {
1380f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB.'</thead>'.DOKU_LF;
1381f05a1cc5SGerrit Uitslag    }
1382f05a1cc5SGerrit Uitslag
13833dd5c225SAndreas Gohr    /**
13845a93f869SAnika Henke     * Open a table body
13855a93f869SAnika Henke     */
1386de369923SAndreas Gohr    public function tabletbody_open() {
13875a93f869SAnika Henke        $this->doc .= DOKU_TAB.'<tbody>'.DOKU_LF;
13885a93f869SAnika Henke    }
13895a93f869SAnika Henke
13905a93f869SAnika Henke    /**
13915a93f869SAnika Henke     * Close a table body
13925a93f869SAnika Henke     */
1393de369923SAndreas Gohr    public function tabletbody_close() {
13945a93f869SAnika Henke        $this->doc .= DOKU_TAB.'</tbody>'.DOKU_LF;
13955a93f869SAnika Henke    }
13965a93f869SAnika Henke
13975a93f869SAnika Henke    /**
1398d2a99739SAndreas Gohr     * Open a table footer
1399d2a99739SAndreas Gohr     */
1400de369923SAndreas Gohr    public function tabletfoot_open() {
140144f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB.'<tfoot>'.DOKU_LF;
1402d2a99739SAndreas Gohr    }
1403d2a99739SAndreas Gohr
1404d2a99739SAndreas Gohr    /**
1405d2a99739SAndreas Gohr     * Close a table footer
1406d2a99739SAndreas Gohr     */
1407de369923SAndreas Gohr    public function tabletfoot_close() {
140844f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB.'</tfoot>'.DOKU_LF;
1409d2a99739SAndreas Gohr    }
1410d2a99739SAndreas Gohr
1411d2a99739SAndreas Gohr    /**
14123dd5c225SAndreas Gohr     * Open a table row
14130c4c0281SGerrit Uitslag     *
14147d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
14153dd5c225SAndreas Gohr     */
1416de369923SAndreas Gohr    public function tablerow_open($classes = null) {
1417b5742cedSPierre Spring        // initialize the cell counter used for classes
1418b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1419b5742cedSPierre Spring        $class                          = 'row'.$this->_counter['row_counter']++;
14200c4c0281SGerrit Uitslag        if($classes !== null) {
14212e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
14220c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14230c4c0281SGerrit Uitslag        }
1424b5742cedSPierre Spring        $this->doc .= DOKU_TAB.'<tr class="'.$class.'">'.DOKU_LF.DOKU_TAB.DOKU_TAB;
14250cecf9d5Sandi    }
14260cecf9d5Sandi
14273dd5c225SAndreas Gohr    /**
14283dd5c225SAndreas Gohr     * Close a table row
14293dd5c225SAndreas Gohr     */
1430de369923SAndreas Gohr    public function tablerow_close() {
1431a2d649c4Sandi        $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
14320cecf9d5Sandi    }
14330cecf9d5Sandi
14343dd5c225SAndreas Gohr    /**
14353dd5c225SAndreas Gohr     * Open a table header cell
14363dd5c225SAndreas Gohr     *
14373dd5c225SAndreas Gohr     * @param int    $colspan
14383dd5c225SAndreas Gohr     * @param string $align left|center|right
14393dd5c225SAndreas Gohr     * @param int    $rowspan
14407d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
14413dd5c225SAndreas Gohr     */
1442de369923SAndreas Gohr    public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1443b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
14440cecf9d5Sandi        if(!is_null($align)) {
1445b5742cedSPierre Spring            $class .= ' '.$align.'align';
14460cecf9d5Sandi        }
14470c4c0281SGerrit Uitslag        if($classes !== null) {
14482e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
14490c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14500c4c0281SGerrit Uitslag        }
1451b5742cedSPierre Spring        $class .= '"';
1452b5742cedSPierre Spring        $this->doc .= '<th '.$class;
14530cecf9d5Sandi        if($colspan > 1) {
1454a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1455a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
14560cecf9d5Sandi        }
145725b97867Shakan.sandell        if($rowspan > 1) {
145825b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
145925b97867Shakan.sandell        }
1460a2d649c4Sandi        $this->doc .= '>';
14610cecf9d5Sandi    }
14620cecf9d5Sandi
14633dd5c225SAndreas Gohr    /**
14643dd5c225SAndreas Gohr     * Close a table header cell
14653dd5c225SAndreas Gohr     */
1466de369923SAndreas Gohr    public function tableheader_close() {
1467a2d649c4Sandi        $this->doc .= '</th>';
14680cecf9d5Sandi    }
14690cecf9d5Sandi
14703dd5c225SAndreas Gohr    /**
14713dd5c225SAndreas Gohr     * Open a table cell
14723dd5c225SAndreas Gohr     *
14733dd5c225SAndreas Gohr     * @param int       $colspan
14743dd5c225SAndreas Gohr     * @param string    $align left|center|right
14753dd5c225SAndreas Gohr     * @param int       $rowspan
14767d769f75SAndreas Gohr     * @param string|string[]    $classes css classes - have to be valid, do not pass unfiltered user input
14773dd5c225SAndreas Gohr     */
1478de369923SAndreas Gohr    public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) {
1479b5742cedSPierre Spring        $class = 'class="col'.$this->_counter['cell_counter']++;
14800cecf9d5Sandi        if(!is_null($align)) {
1481b5742cedSPierre Spring            $class .= ' '.$align.'align';
14820cecf9d5Sandi        }
14830c4c0281SGerrit Uitslag        if($classes !== null) {
14842e0ebe60SAndreas Gohr            if(is_array($classes)) $classes = join(' ', $classes);
14850c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14860c4c0281SGerrit Uitslag        }
1487b5742cedSPierre Spring        $class .= '"';
1488b5742cedSPierre Spring        $this->doc .= '<td '.$class;
14890cecf9d5Sandi        if($colspan > 1) {
1490a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1491a2d649c4Sandi            $this->doc .= ' colspan="'.$colspan.'"';
14920cecf9d5Sandi        }
149325b97867Shakan.sandell        if($rowspan > 1) {
149425b97867Shakan.sandell            $this->doc .= ' rowspan="'.$rowspan.'"';
149525b97867Shakan.sandell        }
1496a2d649c4Sandi        $this->doc .= '>';
14970cecf9d5Sandi    }
14980cecf9d5Sandi
14993dd5c225SAndreas Gohr    /**
15003dd5c225SAndreas Gohr     * Close a table cell
15013dd5c225SAndreas Gohr     */
1502de369923SAndreas Gohr    public function tablecell_close() {
1503a2d649c4Sandi        $this->doc .= '</td>';
15040cecf9d5Sandi    }
15050cecf9d5Sandi
1506cea664bdSLarsDW223    /**
1507cea664bdSLarsDW223     * Returns the current header level.
1508cea664bdSLarsDW223     * (required e.g. by the filelist plugin)
1509cea664bdSLarsDW223     *
1510cea664bdSLarsDW223     * @return int The current header level
1511cea664bdSLarsDW223     */
1512de369923SAndreas Gohr    public function getLastlevel() {
1513cea664bdSLarsDW223        return $this->lastlevel;
1514cea664bdSLarsDW223    }
1515cea664bdSLarsDW223
15163dd5c225SAndreas Gohr    #region Utility functions
15170cecf9d5Sandi
1518ba11bd29Sandi    /**
15193fd0b676Sandi     * Build a link
15203fd0b676Sandi     *
15213fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1522ba11bd29Sandi     *
15230c4c0281SGerrit Uitslag     * @param array $link attributes of a link
15240c4c0281SGerrit Uitslag     * @return string
15250c4c0281SGerrit Uitslag     *
1526ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1527ba11bd29Sandi     */
1528de369923SAndreas Gohr    public function _formatLink($link) {
1529ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1530ba11bd29Sandi        if(substr($link['url'], 0, 7) != 'mailto:') {
1531ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1532ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1533ba11bd29Sandi        }
1534ba11bd29Sandi        //remove double encodings in titles
1535ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1536ba11bd29Sandi
1537453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1538453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1539453493f2SAndreas Gohr        $link['url']   = strtr($link['url'], array('>' => '%3E', '<' => '%3C', '"' => '%22'));
1540453493f2SAndreas Gohr        $link['title'] = strtr($link['title'], array('>' => '&gt;', '<' => '&lt;', '"' => '&quot;'));
1541453493f2SAndreas Gohr
1542ba11bd29Sandi        $ret = '';
1543ba11bd29Sandi        $ret .= $link['pre'];
1544ba11bd29Sandi        $ret .= '<a href="'.$link['url'].'"';
1545bb4866bdSchris        if(!empty($link['class'])) $ret .= ' class="'.$link['class'].'"';
1546bb4866bdSchris        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
1547bb4866bdSchris        if(!empty($link['title'])) $ret .= ' title="'.$link['title'].'"';
1548bb4866bdSchris        if(!empty($link['style'])) $ret .= ' style="'.$link['style'].'"';
1549914045f3SAndreas Gohr        if(!empty($link['rel'])) $ret .= ' rel="'.trim($link['rel']).'"';
1550bb4866bdSchris        if(!empty($link['more'])) $ret .= ' '.$link['more'];
1551ba11bd29Sandi        $ret .= '>';
1552ba11bd29Sandi        $ret .= $link['name'];
1553ba11bd29Sandi        $ret .= '</a>';
1554ba11bd29Sandi        $ret .= $link['suf'];
1555ba11bd29Sandi        return $ret;
1556ba11bd29Sandi    }
1557ba11bd29Sandi
1558ba11bd29Sandi    /**
15593fd0b676Sandi     * Renders internal and external media
15603fd0b676Sandi     *
15613fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
15623dd5c225SAndreas Gohr     * @param string $src       media ID
15633dd5c225SAndreas Gohr     * @param string $title     descriptive text
15643dd5c225SAndreas Gohr     * @param string $align     left|center|right
15653dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
15663dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
15673dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
15683dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
15693dd5c225SAndreas Gohr     * @return string
15703fd0b676Sandi     */
1571de369923SAndreas Gohr    public function _media($src, $title = null, $align = null, $width = null,
15720ea51e63SMatt Perry                    $height = null, $cache = null, $render = true) {
15733fd0b676Sandi
15743fd0b676Sandi        $ret = '';
15753fd0b676Sandi
15763dd5c225SAndreas Gohr        list($ext, $mime) = mimetype($src);
15773fd0b676Sandi        if(substr($mime, 0, 5) == 'image') {
1578b739ff0fSPierre Spring            // first get the $title
1579b739ff0fSPierre Spring            if(!is_null($title)) {
1580b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1581b739ff0fSPierre Spring            } elseif($ext == 'jpg' || $ext == 'jpeg') {
1582b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1583b739ff0fSPierre Spring                require_once(DOKU_INC.'inc/JpegMeta.php');
158467f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1585b739ff0fSPierre Spring                if($jpeg !== false) $cap = $jpeg->getTitle();
15863dd5c225SAndreas Gohr                if(!empty($cap)) {
1587b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1588b739ff0fSPierre Spring                }
1589b739ff0fSPierre Spring            }
1590b739ff0fSPierre Spring            if(!$render) {
1591b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1592b739ff0fSPierre Spring                // return the title of the picture
1593768be5a3SPhy                if($title === null || $title === "") {
1594b739ff0fSPierre Spring                    // just show the sourcename
15958cbc5ee8SAndreas Gohr                    $title = $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($src)));
1596b739ff0fSPierre Spring                }
1597b739ff0fSPierre Spring                return $title;
1598b739ff0fSPierre Spring            }
15993fd0b676Sandi            //add image tag
160064159a61SAndreas Gohr            $ret .= '<img src="' . ml(
160164159a61SAndreas Gohr                    $src,
160264159a61SAndreas Gohr                    array(
160364159a61SAndreas Gohr                        'w' => $width, 'h' => $height,
160464159a61SAndreas Gohr                        'cache' => $cache,
160564159a61SAndreas Gohr                        'rev' => $this->_getLastMediaRevisionAt($src)
160664159a61SAndreas Gohr                    )
160764159a61SAndreas Gohr                ) . '"';
16083fd0b676Sandi            $ret .= ' class="media'.$align.'"';
16094732b197SAndreas Gohr            $ret .= ' loading="lazy"';
16103fd0b676Sandi
1611b739ff0fSPierre Spring            if($title) {
1612b739ff0fSPierre Spring                $ret .= ' title="'.$title.'"';
1613b739ff0fSPierre Spring                $ret .= ' alt="'.$title.'"';
16143fd0b676Sandi            } else {
16153fd0b676Sandi                $ret .= ' alt=""';
16163fd0b676Sandi            }
16173fd0b676Sandi
16183fd0b676Sandi            if(!is_null($width))
16193fd0b676Sandi                $ret .= ' width="'.$this->_xmlEntities($width).'"';
16203fd0b676Sandi
16213fd0b676Sandi            if(!is_null($height))
16223fd0b676Sandi                $ret .= ' height="'.$this->_xmlEntities($height).'"';
16233fd0b676Sandi
16243fd0b676Sandi            $ret .= ' />';
16253fd0b676Sandi
162617954bb5SAnika Henke        } elseif(media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
16272a2a2ba2SAnika Henke            // first get the $title
1628a8dcb874Sbleistivt            $title = !is_null($title) ? $title : false;
16292a2a2ba2SAnika Henke            if(!$render) {
163017954bb5SAnika Henke                // if the file is not supposed to be rendered
163117954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
1632a8dcb874Sbleistivt                return $this->_xmlEntities($title ? $title : \dokuwiki\Utf8\PhpString::basename(noNS($src)));
16332a2a2ba2SAnika Henke            }
16342a2a2ba2SAnika Henke
16352a2a2ba2SAnika Henke            $att          = array();
16362a2a2ba2SAnika Henke            $att['class'] = "media$align";
163717954bb5SAnika Henke            if($title) {
163817954bb5SAnika Henke                $att['title'] = $title;
163917954bb5SAnika Henke            }
16402a2a2ba2SAnika Henke
164117954bb5SAnika Henke            if(media_supportedav($mime, 'video')) {
164217954bb5SAnika Henke                //add video
164379e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1644b44a5dceSAnika Henke            }
164517954bb5SAnika Henke            if(media_supportedav($mime, 'audio')) {
1646b44a5dceSAnika Henke                //add audio
1647b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
164817954bb5SAnika Henke            }
1649b44a5dceSAnika Henke
16503fd0b676Sandi        } elseif($mime == 'application/x-shockwave-flash') {
16511c882ba8SAndreas Gohr            if(!$render) {
16521c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
16531c882ba8SAndreas Gohr                // return the title of the flash
16541c882ba8SAndreas Gohr                if(!$title) {
16551c882ba8SAndreas Gohr                    // just show the sourcename
16568cbc5ee8SAndreas Gohr                    $title = \dokuwiki\Utf8\PhpString::basename(noNS($src));
16571c882ba8SAndreas Gohr                }
165807bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
16591c882ba8SAndreas Gohr            }
16601c882ba8SAndreas Gohr
166107bf32b2SAndreas Gohr            $att          = array();
166207bf32b2SAndreas Gohr            $att['class'] = "media$align";
166307bf32b2SAndreas Gohr            if($align == 'right') $att['align'] = 'right';
166407bf32b2SAndreas Gohr            if($align == 'left') $att['align'] = 'left';
16653dd5c225SAndreas Gohr            $ret .= html_flashobject(
16663dd5c225SAndreas Gohr                ml($src, array('cache' => $cache), true, '&'), $width, $height,
166707bf32b2SAndreas Gohr                array('quality' => 'high'),
166807bf32b2SAndreas Gohr                null,
166907bf32b2SAndreas Gohr                $att,
16703dd5c225SAndreas Gohr                $this->_xmlEntities($title)
16713dd5c225SAndreas Gohr            );
16720f428d7dSAndreas Gohr        } elseif($title) {
16733fd0b676Sandi            // well at least we have a title to display
16743fd0b676Sandi            $ret .= $this->_xmlEntities($title);
16753fd0b676Sandi        } else {
16765291ca3aSAndreas Gohr            // just show the sourcename
16778cbc5ee8SAndreas Gohr            $ret .= $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($src)));
16783fd0b676Sandi        }
16793fd0b676Sandi
16803fd0b676Sandi        return $ret;
16813fd0b676Sandi    }
16823fd0b676Sandi
16833dd5c225SAndreas Gohr    /**
16843dd5c225SAndreas Gohr     * Escape string for output
16853dd5c225SAndreas Gohr     *
16863dd5c225SAndreas Gohr     * @param $string
16873dd5c225SAndreas Gohr     * @return string
16883dd5c225SAndreas Gohr     */
1689de369923SAndreas Gohr    public function _xmlEntities($string) {
1690f7711f2bSAndreas Gohr        return hsc($string);
16910cecf9d5Sandi    }
16920cecf9d5Sandi
1693de369923SAndreas Gohr
16940cecf9d5Sandi
1695af587fa8Sandi    /**
16963fd0b676Sandi     * Construct a title and handle images in titles
16973fd0b676Sandi     *
16980b7c14c2Sandi     * @author Harry Fuecks <hfuecks@gmail.com>
16993dd5c225SAndreas Gohr     * @param string|array $title    either string title or media array
17003dd5c225SAndreas Gohr     * @param string       $default  default title if nothing else is found
17013dd5c225SAndreas Gohr     * @param bool         $isImage  will be set to true if it's a media file
17023dd5c225SAndreas Gohr     * @param null|string  $id       linked page id (used to extract title from first heading)
17033dd5c225SAndreas Gohr     * @param string       $linktype content|navigation
17043dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
17053fd0b676Sandi     */
1706de369923SAndreas Gohr    public function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content') {
170744881bd0Shenning.noren        $isImage = false;
170829657f9eSAndreas Gohr        if(is_array($title)) {
170929657f9eSAndreas Gohr            $isImage = true;
171029657f9eSAndreas Gohr            return $this->_imageTitle($title);
171129657f9eSAndreas Gohr        } elseif(is_null($title) || trim($title) == '') {
1712fe9ec250SChris Smith            if(useHeading($linktype) && $id) {
171367c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1714f515db7fSAndreas Gohr                if(!blank($heading)) {
1715433bef32Sandi                    return $this->_xmlEntities($heading);
1716bb0a59d4Sjan                }
1717bb0a59d4Sjan            }
1718433bef32Sandi            return $this->_xmlEntities($default);
171968c26e6dSMichael Klier        } else {
172068c26e6dSMichael Klier            return $this->_xmlEntities($title);
17210cecf9d5Sandi        }
17220cecf9d5Sandi    }
17230cecf9d5Sandi
17240cecf9d5Sandi    /**
17253dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
17263fd0b676Sandi     *
17273fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1728e0c26282SGerrit Uitslag     * @param array $img
17293dd5c225SAndreas Gohr     * @return string HTML img tag or similar
17300cecf9d5Sandi     */
1731de369923SAndreas Gohr    public function _imageTitle($img) {
1732d9baf1a7SKazutaka Miyasaka        global $ID;
1733d9baf1a7SKazutaka Miyasaka
1734d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1735d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
17363dd5c225SAndreas Gohr        list($img['src']) = explode('#', $img['src'], 2);
1737d9baf1a7SKazutaka Miyasaka        if($img['type'] == 'internalmedia') {
17388c6be208SAndreas Gohr            $img['src'] = (new MediaResolver($ID))->resolveId($img['src'], $this->date_at, true);
1739d9baf1a7SKazutaka Miyasaka        }
1740d9baf1a7SKazutaka Miyasaka
17413dd5c225SAndreas Gohr        return $this->_media(
17423dd5c225SAndreas Gohr            $img['src'],
17434826ab45Sandi            $img['title'],
17444826ab45Sandi            $img['align'],
17454826ab45Sandi            $img['width'],
17464826ab45Sandi            $img['height'],
17473dd5c225SAndreas Gohr            $img['cache']
17483dd5c225SAndreas Gohr        );
17490cecf9d5Sandi    }
1750b739ff0fSPierre Spring
1751b739ff0fSPierre Spring    /**
17523dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
17533dd5c225SAndreas Gohr     *
17543dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1755b739ff0fSPierre Spring     *
1756b739ff0fSPierre Spring     * @author   Pierre Spring <pierre.spring@liip.ch>
17573dd5c225SAndreas Gohr     * @param string $src       media ID
17583dd5c225SAndreas Gohr     * @param string $title     descriptive text
17593dd5c225SAndreas Gohr     * @param string $align     left|center|right
17603dd5c225SAndreas Gohr     * @param int    $width     width of media in pixel
17613dd5c225SAndreas Gohr     * @param int    $height    height of media in pixel
17623dd5c225SAndreas Gohr     * @param string $cache     cache|recache|nocache
17633dd5c225SAndreas Gohr     * @param bool   $render    should the media be embedded inline or just linked
17643dd5c225SAndreas Gohr     * @return array associative array with link config
1765b739ff0fSPierre Spring     */
1766de369923SAndreas Gohr    public function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render) {
1767b739ff0fSPierre Spring        global $conf;
1768b739ff0fSPierre Spring
1769b739ff0fSPierre Spring        $link           = array();
1770b739ff0fSPierre Spring        $link['class']  = 'media';
1771b739ff0fSPierre Spring        $link['style']  = '';
1772b739ff0fSPierre Spring        $link['pre']    = '';
1773b739ff0fSPierre Spring        $link['suf']    = '';
1774b739ff0fSPierre Spring        $link['more']   = '';
1775b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1776bc3d2252SAndreas Gohr        if($conf['target']['media']) $link['rel'] = 'noopener';
1777b739ff0fSPierre Spring        $link['title']  = $this->_xmlEntities($src);
1778b739ff0fSPierre Spring        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1779b739ff0fSPierre Spring
1780b739ff0fSPierre Spring        return $link;
1781b739ff0fSPierre Spring    }
178291459163SAnika Henke
17832a2a2ba2SAnika Henke    /**
17842a2a2ba2SAnika Henke     * Embed video(s) in HTML
17852a2a2ba2SAnika Henke     *
17862a2a2ba2SAnika Henke     * @author Anika Henke <anika@selfthinker.org>
17870877a1f1SSchplurtz le Déboulonné     * @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
17882a2a2ba2SAnika Henke     *
17892a2a2ba2SAnika Henke     * @param string $src         - ID of video to embed
17902a2a2ba2SAnika Henke     * @param int    $width       - width of the video in pixels
17912a2a2ba2SAnika Henke     * @param int    $height      - height of the video in pixels
17922a2a2ba2SAnika Henke     * @param array  $atts        - additional attributes for the <video> tag
1793f50634f0SAnika Henke     * @return string
17942a2a2ba2SAnika Henke     */
1795de369923SAndreas Gohr    public function _video($src, $width, $height, $atts = null) {
17962a2a2ba2SAnika Henke        // prepare width and height
17972a2a2ba2SAnika Henke        if(is_null($atts)) $atts = array();
17982a2a2ba2SAnika Henke        $atts['width']  = (int) $width;
17992a2a2ba2SAnika Henke        $atts['height'] = (int) $height;
18002a2a2ba2SAnika Henke        if(!$atts['width']) $atts['width'] = 320;
18012a2a2ba2SAnika Henke        if(!$atts['height']) $atts['height'] = 240;
18022a2a2ba2SAnika Henke
1803410ee62aSAnika Henke        $posterUrl = '';
1804410ee62aSAnika Henke        $files = array();
18050877a1f1SSchplurtz le Déboulonné        $tracks = array();
1806410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1807410ee62aSAnika Henke
1808410ee62aSAnika Henke        if ($isExternal) {
1809410ee62aSAnika Henke            // take direct source for external files
1810702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1811410ee62aSAnika Henke            $files[$srcMime] = $src;
1812410ee62aSAnika Henke        } else {
18133d7a9e0aSAnika Henke            // prepare alternative formats
18143d7a9e0aSAnika Henke            $extensions   = array('webm', 'ogv', 'mp4');
1815410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
181659bc3b48SGerrit Uitslag            $poster       = media_alternativefiles($src, array('jpg', 'png'));
18170877a1f1SSchplurtz le Déboulonné            $tracks       = media_trackfiles($src);
181899f943f6SAnika Henke            if(!empty($poster)) {
18192d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
182099f943f6SAnika Henke            }
1821410ee62aSAnika Henke        }
18222a2a2ba2SAnika Henke
1823f50634f0SAnika Henke        $out = '';
182479e53fe5SAnika Henke        // open video tag
1825f50634f0SAnika Henke        $out .= '<video '.buildAttributes($atts).' controls="controls"';
18263641199aSAnika Henke        if($posterUrl) $out .= ' poster="'.hsc($posterUrl).'"';
1827f50634f0SAnika Henke        $out .= '>'.NL;
18283641199aSAnika Henke        $fallback = '';
182979e53fe5SAnika Henke
183079e53fe5SAnika Henke        // output source for each alternative video format
1831410ee62aSAnika Henke        foreach($files as $mime => $file) {
1832410ee62aSAnika Henke            if ($isExternal) {
1833410ee62aSAnika Henke                $url = $file;
1834410ee62aSAnika Henke                $linkType = 'externalmedia';
1835410ee62aSAnika Henke            } else {
18362d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1837410ee62aSAnika Henke                $linkType = 'internalmedia';
1838410ee62aSAnika Henke            }
1839056bf31fSDamien Regad            $title = !empty($atts['title'])
1840056bf31fSDamien Regad                ? $atts['title']
1841056bf31fSDamien Regad                : $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($file)));
18423d7a9e0aSAnika Henke
1843f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
184479e53fe5SAnika Henke            // alternative content (just a link to the file)
184564159a61SAndreas Gohr            $fallback .= $this->$linkType(
184664159a61SAndreas Gohr                $file,
184764159a61SAndreas Gohr                $title,
184864159a61SAndreas Gohr                null,
184964159a61SAndreas Gohr                null,
185064159a61SAndreas Gohr                null,
185164159a61SAndreas Gohr                $cache = null,
185264159a61SAndreas Gohr                $linking = 'linkonly',
185364159a61SAndreas Gohr                $return = true
185464159a61SAndreas Gohr            );
18553d7a9e0aSAnika Henke        }
18562a2a2ba2SAnika Henke
18570877a1f1SSchplurtz le Déboulonné        // output each track if any
18580877a1f1SSchplurtz le Déboulonné        foreach( $tracks as $trackid => $info ) {
185923c61bbeSSchplurtz le Déboulonné            list( $kind, $srclang ) = array_map( 'hsc', $info );
186023c61bbeSSchplurtz le Déboulonné            $out .= "<track kind=\"$kind\" srclang=\"$srclang\" ";
186123c61bbeSSchplurtz le Déboulonné            $out .= "label=\"$srclang\" ";
18620877a1f1SSchplurtz le Déboulonné            $out .= 'src="'.ml($trackid, '', true).'">'.NL;
18630877a1f1SSchplurtz le Déboulonné        }
18640877a1f1SSchplurtz le Déboulonné
18652a2a2ba2SAnika Henke        // finish
18663641199aSAnika Henke        $out .= $fallback;
1867f50634f0SAnika Henke        $out .= '</video>'.NL;
1868f50634f0SAnika Henke        return $out;
18692a2a2ba2SAnika Henke    }
18702a2a2ba2SAnika Henke
1871b44a5dceSAnika Henke    /**
1872b44a5dceSAnika Henke     * Embed audio in HTML
1873b44a5dceSAnika Henke     *
1874b44a5dceSAnika Henke     * @author Anika Henke <anika@selfthinker.org>
1875b44a5dceSAnika Henke     *
1876b44a5dceSAnika Henke     * @param string $src       - ID of audio to embed
18776d4af72aSAnika Henke     * @param array  $atts      - additional attributes for the <audio> tag
1878f50634f0SAnika Henke     * @return string
1879b44a5dceSAnika Henke     */
1880de369923SAndreas Gohr    public function _audio($src, $atts = array()) {
1881702e97d3SAnika Henke        $files = array();
1882410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1883b44a5dceSAnika Henke
1884410ee62aSAnika Henke        if ($isExternal) {
1885410ee62aSAnika Henke            // take direct source for external files
1886702e97d3SAnika Henke            list(/*ext*/, $srcMime) = mimetype($src);
1887410ee62aSAnika Henke            $files[$srcMime] = $src;
1888410ee62aSAnika Henke        } else {
1889b44a5dceSAnika Henke            // prepare alternative formats
1890b44a5dceSAnika Henke            $extensions   = array('ogg', 'mp3', 'wav');
1891410ee62aSAnika Henke            $files        = media_alternativefiles($src, $extensions);
1892410ee62aSAnika Henke        }
1893b44a5dceSAnika Henke
1894f50634f0SAnika Henke        $out = '';
1895b44a5dceSAnika Henke        // open audio tag
1896f50634f0SAnika Henke        $out .= '<audio '.buildAttributes($atts).' controls="controls">'.NL;
18973641199aSAnika Henke        $fallback = '';
1898b44a5dceSAnika Henke
1899b44a5dceSAnika Henke        // output source for each alternative audio format
1900410ee62aSAnika Henke        foreach($files as $mime => $file) {
1901410ee62aSAnika Henke            if ($isExternal) {
1902410ee62aSAnika Henke                $url = $file;
1903410ee62aSAnika Henke                $linkType = 'externalmedia';
1904410ee62aSAnika Henke            } else {
19052d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1906410ee62aSAnika Henke                $linkType = 'internalmedia';
1907410ee62aSAnika Henke            }
19088cbc5ee8SAndreas Gohr            $title = $atts['title'] ? $atts['title'] : $this->_xmlEntities(\dokuwiki\Utf8\PhpString::basename(noNS($file)));
1909b44a5dceSAnika Henke
1910f50634f0SAnika Henke            $out .= '<source src="'.hsc($url).'" type="'.$mime.'" />'.NL;
1911b44a5dceSAnika Henke            // alternative content (just a link to the file)
191264159a61SAndreas Gohr            $fallback .= $this->$linkType(
191364159a61SAndreas Gohr                $file,
191464159a61SAndreas Gohr                $title,
191564159a61SAndreas Gohr                null,
191664159a61SAndreas Gohr                null,
191764159a61SAndreas Gohr                null,
191864159a61SAndreas Gohr                $cache = null,
191964159a61SAndreas Gohr                $linking = 'linkonly',
192064159a61SAndreas Gohr                $return = true
192164159a61SAndreas Gohr            );
1922b44a5dceSAnika Henke        }
1923b44a5dceSAnika Henke
1924b44a5dceSAnika Henke        // finish
19253641199aSAnika Henke        $out .= $fallback;
1926f50634f0SAnika Henke        $out .= '</audio>'.NL;
1927f50634f0SAnika Henke        return $out;
1928b44a5dceSAnika Henke    }
1929b44a5dceSAnika Henke
19305c2eed9aSlisps    /**
193152dc5eadSlisps     * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
19325c2eed9aSlisps     * which returns an existing media revision less or equal to rev or date_at
19335c2eed9aSlisps     *
19345c2eed9aSlisps     * @author lisps
19355c2eed9aSlisps     * @param string $media_id
19365c2eed9aSlisps     * @access protected
19375c2eed9aSlisps     * @return string revision ('' for current)
19385c2eed9aSlisps     */
1939de369923SAndreas Gohr    protected function _getLastMediaRevisionAt($media_id) {
194052dc5eadSlisps        if (!$this->date_at || media_isexternal($media_id)) return '';
1941252acce3SSatoshi Sahara        $changelog = new MediaChangeLog($media_id);
1942252acce3SSatoshi Sahara        return $changelog->getLastRevisionAt($this->date_at);
19435c2eed9aSlisps    }
19445c2eed9aSlisps
19453dd5c225SAndreas Gohr    #endregion
19460cecf9d5Sandi}
19470cecf9d5Sandi
1948e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
1949