xref: /dokuwiki/inc/parser/xhtml.php (revision faf3f01b9af152b16aca96437c109c41b8250689)
10cecf9d5Sandi<?php
20c3a5702SAndreas Gohr
30c3a5702SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog;
42cd6cc0aSAndreas Gohruse dokuwiki\File\MediaResolver;
52cd6cc0aSAndreas Gohruse dokuwiki\File\PageResolver;
6*faf3f01bSAndreas Gohruse dokuwiki\Utf8\PhpString;
7*faf3f01bSAndreas Gohruse SimplePie\Author;
80c3a5702SAndreas Gohr
9b625487dSandi/**
10b625487dSandi * Renderer for XHTML output
11b625487dSandi *
12b4f2363aSAndreas Gohr * This is DokuWiki's main renderer used to display page content in the wiki
13b4f2363aSAndreas Gohr *
14b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
15b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
163dd5c225SAndreas Gohr *
170cecf9d5Sandi */
18*faf3f01bSAndreas Gohrclass Doku_Renderer_xhtml extends Doku_Renderer
19*faf3f01bSAndreas Gohr{
203dd5c225SAndreas Gohr    /** @var array store the table of contents */
21*faf3f01bSAndreas Gohr    public $toc = [];
220cecf9d5Sandi
233dd5c225SAndreas Gohr    /** @var array A stack of section edit data */
24*faf3f01bSAndreas Gohr    protected $sectionedits = [];
25de369923SAndreas 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! */
30*faf3f01bSAndreas Gohr    protected $footnotes = [];
317764a90aSandi
323dd5c225SAndreas Gohr    /** @var int current section level */
333dd5c225SAndreas Gohr    protected $lastlevel = 0;
343dd5c225SAndreas Gohr    /** @var array section node tracker */
35*faf3f01bSAndreas Gohr    protected $node = [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. */
41*faf3f01bSAndreas Gohr    protected $_counter = []; //
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 */
47*faf3f01bSAndreas Gohr    protected $schemes;
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     */
68*faf3f01bSAndreas Gohr    public function startSectionEdit($start, $data)
69*faf3f01bSAndreas Gohr    {
70ec57f119SLarsDW223        if (!is_array($data)) {
71ac025fdfSAndreas Gohr            msg(
72ac025fdfSAndreas Gohr                sprintf(
73ac025fdfSAndreas Gohr                    'startSectionEdit: $data "%s" is NOT an array! One of your plugins needs an update.',
74ac025fdfSAndreas Gohr                    hsc((string)$data)
75ac025fdfSAndreas Gohr                ), -1
76ac025fdfSAndreas Gohr            );
77ac025fdfSAndreas Gohr
78ac025fdfSAndreas Gohr            // @deprecated 2018-04-14, backward compatibility
79ac025fdfSAndreas Gohr            $args = func_get_args();
80*faf3f01bSAndreas Gohr            $data = [];
81ac025fdfSAndreas Gohr            if (isset($args[1])) $data['target'] = $args[1];
82ac025fdfSAndreas Gohr            if (isset($args[2])) $data['name'] = $args[2];
83ac025fdfSAndreas Gohr            if (isset($args[3])) $data['hid'] = $args[3];
84ec57f119SLarsDW223        }
85ec57f119SLarsDW223        $data['secid'] = ++$this->lastsecid;
86ec57f119SLarsDW223        $data['start'] = $start;
87ec57f119SLarsDW223        $this->sectionedits[] = $data;
88ec57f119SLarsDW223        return 'sectionedit' . $data['secid'];
8990df9a4dSAdrian Lang    }
9090df9a4dSAdrian Lang
9190df9a4dSAdrian Lang    /**
9290df9a4dSAdrian Lang     * Finish an edit section range
9390df9a4dSAdrian Lang     *
9442ea7f44SGerrit Uitslag     * @param int $end The byte position for the edit end; null for the rest of the page
9542ea7f44SGerrit Uitslag     *
9690df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
9790df9a4dSAdrian Lang     */
98*faf3f01bSAndreas Gohr    public function finishSectionEdit($end = null, $hid = null)
99*faf3f01bSAndreas Gohr    {
100ad43fdbfSasivery        if (count($this->sectionedits) == 0) {
1010d9f02ecSasivery            return;
1020d9f02ecSasivery        }
103ec57f119SLarsDW223        $data = array_pop($this->sectionedits);
104ec57f119SLarsDW223        if (!is_null($end) && $end <= $data['start']) {
10500c13053SAdrian Lang            return;
10600c13053SAdrian Lang        }
1072571786cSLarsDW223        if (!is_null($hid)) {
108ec57f119SLarsDW223            $data['hid'] .= $hid;
1092571786cSLarsDW223        }
110ec57f119SLarsDW223        $data['range'] = $data['start'] . '-' . (is_null($end) ? '' : $end);
111ec57f119SLarsDW223        unset($data['start']);
112*faf3f01bSAndreas Gohr        $this->doc .= '<!-- EDIT' . hsc(json_encode($data, JSON_THROW_ON_ERROR)) . ' -->';
11390df9a4dSAdrian Lang    }
11490df9a4dSAdrian Lang
1153dd5c225SAndreas Gohr    /**
1163dd5c225SAndreas Gohr     * Returns the format produced by this renderer.
1173dd5c225SAndreas Gohr     *
1183dd5c225SAndreas Gohr     * @return string always 'xhtml'
1193dd5c225SAndreas Gohr     */
120*faf3f01bSAndreas Gohr    public function getFormat()
121*faf3f01bSAndreas Gohr    {
1225f70445dSAndreas Gohr        return 'xhtml';
1235f70445dSAndreas Gohr    }
1245f70445dSAndreas Gohr
1253dd5c225SAndreas Gohr    /**
1263dd5c225SAndreas Gohr     * Initialize the document
1273dd5c225SAndreas Gohr     */
128*faf3f01bSAndreas Gohr    public function document_start()
129*faf3f01bSAndreas Gohr    {
130c5a8fd96SAndreas Gohr        //reset some internals
131*faf3f01bSAndreas Gohr        $this->toc = [];
1320cecf9d5Sandi    }
1330cecf9d5Sandi
1343dd5c225SAndreas Gohr    /**
1353dd5c225SAndreas Gohr     * Finalize the document
1363dd5c225SAndreas Gohr     */
137*faf3f01bSAndreas Gohr    public function document_end()
138*faf3f01bSAndreas Gohr    {
13990df9a4dSAdrian Lang        // Finish open section edits.
140*faf3f01bSAndreas Gohr        while ($this->sectionedits !== []) {
141ec57f119SLarsDW223            if ($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) {
14290df9a4dSAdrian Lang                // If there is only one section, do not write a section edit
14390df9a4dSAdrian Lang                // marker.
14490df9a4dSAdrian Lang                array_pop($this->sectionedits);
14590df9a4dSAdrian Lang            } else {
146d9e36cbeSAdrian Lang                $this->finishSectionEdit();
14790df9a4dSAdrian Lang            }
14890df9a4dSAdrian Lang        }
14990df9a4dSAdrian Lang
150*faf3f01bSAndreas Gohr        if ($this->footnotes !== []) {
151a2d649c4Sandi            $this->doc .= '<div class="footnotes">' . DOKU_LF;
152d74aace9Schris
15316ec3e37SAndreas Gohr            foreach ($this->footnotes as $id => $footnote) {
154d74aace9Schris                // check its not a placeholder that indicates actual footnote text is elsewhere
155d74aace9Schris                if (substr($footnote, 0, 5) != "@@FNT") {
156d74aace9Schris
157d74aace9Schris                    // open the footnote and set the anchor and backlink
158d74aace9Schris                    $this->doc .= '<div class="fn">';
15916cc7ed7SAnika Henke                    $this->doc .= '<sup><a href="#fnt__' . $id . '" id="fn__' . $id . '" class="fn_bot">';
16029bfcd16SAndreas Gohr                    $this->doc .= $id . ')</a></sup> ' . DOKU_LF;
161d74aace9Schris
162d74aace9Schris                    // get any other footnotes that use the same markup
163d74aace9Schris                    $alt = array_keys($this->footnotes, "@@FNT$id");
164d74aace9Schris
165d74aace9Schris                    foreach ($alt as $ref) {
166d74aace9Schris                        // set anchor and backlink for the other footnotes
16716ec3e37SAndreas Gohr                        $this->doc .= ', <sup><a href="#fnt__' . ($ref) . '" id="fn__' . ($ref) . '" class="fn_bot">';
16816ec3e37SAndreas Gohr                        $this->doc .= ($ref) . ')</a></sup> ' . DOKU_LF;
169d74aace9Schris                    }
170d74aace9Schris
171d74aace9Schris                    // add footnote markup and close this footnote
172694afa06SAnika Henke                    $this->doc .= '<div class="content">' . $footnote . '</div>';
173d74aace9Schris                    $this->doc .= '</div>' . DOKU_LF;
174d74aace9Schris                }
1750cecf9d5Sandi            }
176a2d649c4Sandi            $this->doc .= '</div>' . DOKU_LF;
1770cecf9d5Sandi        }
178c5a8fd96SAndreas Gohr
179b8595a66SAndreas Gohr        // Prepare the TOC
180851f2e89SAnika Henke        global $conf;
18164159a61SAndreas Gohr        if (
18264159a61SAndreas Gohr            $this->info['toc'] &&
18364159a61SAndreas Gohr            is_array($this->toc) &&
18464159a61SAndreas Gohr            $conf['tocminheads'] && count($this->toc) >= $conf['tocminheads']
18564159a61SAndreas Gohr        ) {
186b8595a66SAndreas Gohr            global $TOC;
187b8595a66SAndreas Gohr            $TOC = $this->toc;
1880cecf9d5Sandi        }
1893e55d035SAndreas Gohr
1903e55d035SAndreas Gohr        // make sure there are no empty paragraphs
19127918226Schris        $this->doc = preg_replace('#<p>\s*</p>#', '', $this->doc);
192e41c4da9SAndreas Gohr    }
1930cecf9d5Sandi
1943dd5c225SAndreas Gohr    /**
1953dd5c225SAndreas Gohr     * Add an item to the TOC
1963dd5c225SAndreas Gohr     *
1973dd5c225SAndreas Gohr     * @param string $id the hash link
1983dd5c225SAndreas Gohr     * @param string $text the text to display
1993dd5c225SAndreas Gohr     * @param int $level the nesting level
2003dd5c225SAndreas Gohr     */
201*faf3f01bSAndreas Gohr    public function toc_additem($id, $text, $level)
202*faf3f01bSAndreas Gohr    {
203af587fa8Sandi        global $conf;
204af587fa8Sandi
205c5a8fd96SAndreas Gohr        //handle TOC
206c5a8fd96SAndreas Gohr        if ($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']) {
2077d91652aSAndreas Gohr            $this->toc[] = html_mktocitem($id, $text, $level - $conf['toptoclevel'] + 1);
208c5a8fd96SAndreas Gohr        }
209e7856beaSchris    }
210e7856beaSchris
2113dd5c225SAndreas Gohr    /**
2123dd5c225SAndreas Gohr     * Render a heading
2133dd5c225SAndreas Gohr     *
2143dd5c225SAndreas Gohr     * @param string $text the text to display
2153dd5c225SAndreas Gohr     * @param int $level header level
2163dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
217e3c00e6eSIain Hallam     * @param bool $returnonly whether to return html or write to doc attribute
218e3c00e6eSIain Hallam     * @return void|string writes to doc attribute or returns html depends on $returnonly
2193dd5c225SAndreas Gohr     */
220*faf3f01bSAndreas Gohr    public function header($text, $level, $pos, $returnonly = false)
221*faf3f01bSAndreas Gohr    {
22290df9a4dSAdrian Lang        global $conf;
22390df9a4dSAdrian Lang
224f515db7fSAndreas Gohr        if (blank($text)) return; //skip empty headlines
225e7856beaSchris
226e7856beaSchris        $hid = $this->_headerToLink($text, true);
227e7856beaSchris
228e7856beaSchris        //only add items within configured levels
229e7856beaSchris        $this->toc_additem($hid, $text, $level);
230c5a8fd96SAndreas Gohr
23191459163SAnika Henke        // adjust $node to reflect hierarchy of levels
23291459163SAnika Henke        $this->node[$level - 1]++;
23391459163SAnika Henke        if ($level < $this->lastlevel) {
23491459163SAnika Henke            for ($i = 0; $i < $this->lastlevel - $level; $i++) {
23591459163SAnika Henke                $this->node[$this->lastlevel - $i - 1] = 0;
23691459163SAnika Henke            }
23791459163SAnika Henke        }
23891459163SAnika Henke        $this->lastlevel = $level;
23991459163SAnika Henke
24090df9a4dSAdrian Lang        if ($level <= $conf['maxseclevel'] &&
241*faf3f01bSAndreas Gohr            $this->sectionedits !== [] &&
242ec57f119SLarsDW223            $this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section'
2433dd5c225SAndreas Gohr        ) {
2446c1f778cSAdrian Lang            $this->finishSectionEdit($pos - 1);
24590df9a4dSAdrian Lang        }
24690df9a4dSAdrian Lang
247e3c00e6eSIain Hallam        // build the header
248e3c00e6eSIain Hallam        $header = DOKU_LF . '<h' . $level;
24990df9a4dSAdrian Lang        if ($level <= $conf['maxseclevel']) {
250*faf3f01bSAndreas Gohr            $data = [];
251ec57f119SLarsDW223            $data['target'] = 'section';
252ec57f119SLarsDW223            $data['name'] = $text;
253ec57f119SLarsDW223            $data['hid'] = $hid;
254ec57f119SLarsDW223            $data['codeblockOffset'] = $this->_codeblock;
255e3c00e6eSIain Hallam            $header .= ' class="' . $this->startSectionEdit($pos, $data) . '"';
25690df9a4dSAdrian Lang        }
257e3c00e6eSIain Hallam        $header .= ' id="' . $hid . '">';
258e3c00e6eSIain Hallam        $header .= $this->_xmlEntities($text);
259e3c00e6eSIain Hallam        $header .= "</h$level>" . DOKU_LF;
260e3c00e6eSIain Hallam
261e3c00e6eSIain Hallam        if ($returnonly) {
262e3c00e6eSIain Hallam            return $header;
263e3c00e6eSIain Hallam        } else {
264e3c00e6eSIain Hallam            $this->doc .= $header;
265e3c00e6eSIain Hallam        }
2660cecf9d5Sandi    }
2670cecf9d5Sandi
2683dd5c225SAndreas Gohr    /**
2693dd5c225SAndreas Gohr     * Open a new section
2703dd5c225SAndreas Gohr     *
2713dd5c225SAndreas Gohr     * @param int $level section level (as determined by the previous header)
2723dd5c225SAndreas Gohr     */
273*faf3f01bSAndreas Gohr    public function section_open($level)
274*faf3f01bSAndreas Gohr    {
2759864e7b1SAdrian Lang        $this->doc .= '<div class="level' . $level . '">' . DOKU_LF;
2760cecf9d5Sandi    }
2770cecf9d5Sandi
2783dd5c225SAndreas Gohr    /**
2793dd5c225SAndreas Gohr     * Close the current section
2803dd5c225SAndreas Gohr     */
281*faf3f01bSAndreas Gohr    public function section_close()
282*faf3f01bSAndreas Gohr    {
283a2d649c4Sandi        $this->doc .= DOKU_LF . '</div>' . DOKU_LF;
2840cecf9d5Sandi    }
2850cecf9d5Sandi
2863dd5c225SAndreas Gohr    /**
2873dd5c225SAndreas Gohr     * Render plain text data
2883dd5c225SAndreas Gohr     *
2893dd5c225SAndreas Gohr     * @param $text
2903dd5c225SAndreas Gohr     */
291*faf3f01bSAndreas Gohr    public function cdata($text)
292*faf3f01bSAndreas Gohr    {
293a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
2940cecf9d5Sandi    }
2950cecf9d5Sandi
2963dd5c225SAndreas Gohr    /**
2973dd5c225SAndreas Gohr     * Open a paragraph
2983dd5c225SAndreas Gohr     */
299*faf3f01bSAndreas Gohr    public function p_open()
300*faf3f01bSAndreas Gohr    {
30159869a4bSAnika Henke        $this->doc .= DOKU_LF . '<p>' . DOKU_LF;
3020cecf9d5Sandi    }
3030cecf9d5Sandi
3043dd5c225SAndreas Gohr    /**
3053dd5c225SAndreas Gohr     * Close a paragraph
3063dd5c225SAndreas Gohr     */
307*faf3f01bSAndreas Gohr    public function p_close()
308*faf3f01bSAndreas Gohr    {
30959869a4bSAnika Henke        $this->doc .= DOKU_LF . '</p>' . DOKU_LF;
3100cecf9d5Sandi    }
3110cecf9d5Sandi
3123dd5c225SAndreas Gohr    /**
3133dd5c225SAndreas Gohr     * Create a line break
3143dd5c225SAndreas Gohr     */
315*faf3f01bSAndreas Gohr    public function linebreak()
316*faf3f01bSAndreas Gohr    {
317a2d649c4Sandi        $this->doc .= '<br/>' . DOKU_LF;
3180cecf9d5Sandi    }
3190cecf9d5Sandi
3203dd5c225SAndreas Gohr    /**
3213dd5c225SAndreas Gohr     * Create a horizontal line
3223dd5c225SAndreas Gohr     */
323*faf3f01bSAndreas Gohr    public function hr()
324*faf3f01bSAndreas Gohr    {
3254beabca9SAnika Henke        $this->doc .= '<hr />' . DOKU_LF;
3260cecf9d5Sandi    }
3270cecf9d5Sandi
3283dd5c225SAndreas Gohr    /**
3293dd5c225SAndreas Gohr     * Start strong (bold) formatting
3303dd5c225SAndreas Gohr     */
331*faf3f01bSAndreas Gohr    public function strong_open()
332*faf3f01bSAndreas Gohr    {
333a2d649c4Sandi        $this->doc .= '<strong>';
3340cecf9d5Sandi    }
3350cecf9d5Sandi
3363dd5c225SAndreas Gohr    /**
3373dd5c225SAndreas Gohr     * Stop strong (bold) formatting
3383dd5c225SAndreas Gohr     */
339*faf3f01bSAndreas Gohr    public function strong_close()
340*faf3f01bSAndreas Gohr    {
341a2d649c4Sandi        $this->doc .= '</strong>';
3420cecf9d5Sandi    }
3430cecf9d5Sandi
3443dd5c225SAndreas Gohr    /**
3453dd5c225SAndreas Gohr     * Start emphasis (italics) formatting
3463dd5c225SAndreas Gohr     */
347*faf3f01bSAndreas Gohr    public function emphasis_open()
348*faf3f01bSAndreas Gohr    {
349a2d649c4Sandi        $this->doc .= '<em>';
3500cecf9d5Sandi    }
3510cecf9d5Sandi
3523dd5c225SAndreas Gohr    /**
3533dd5c225SAndreas Gohr     * Stop emphasis (italics) formatting
3543dd5c225SAndreas Gohr     */
355*faf3f01bSAndreas Gohr    public function emphasis_close()
356*faf3f01bSAndreas Gohr    {
357a2d649c4Sandi        $this->doc .= '</em>';
3580cecf9d5Sandi    }
3590cecf9d5Sandi
3603dd5c225SAndreas Gohr    /**
3613dd5c225SAndreas Gohr     * Start underline formatting
3623dd5c225SAndreas Gohr     */
363*faf3f01bSAndreas Gohr    public function underline_open()
364*faf3f01bSAndreas Gohr    {
36502e51121SAnika Henke        $this->doc .= '<em class="u">';
3660cecf9d5Sandi    }
3670cecf9d5Sandi
3683dd5c225SAndreas Gohr    /**
3693dd5c225SAndreas Gohr     * Stop underline formatting
3703dd5c225SAndreas Gohr     */
371*faf3f01bSAndreas Gohr    public function underline_close()
372*faf3f01bSAndreas Gohr    {
37302e51121SAnika Henke        $this->doc .= '</em>';
3740cecf9d5Sandi    }
3750cecf9d5Sandi
3763dd5c225SAndreas Gohr    /**
3773dd5c225SAndreas Gohr     * Start monospace formatting
3783dd5c225SAndreas Gohr     */
379*faf3f01bSAndreas Gohr    public function monospace_open()
380*faf3f01bSAndreas Gohr    {
381a2d649c4Sandi        $this->doc .= '<code>';
3820cecf9d5Sandi    }
3830cecf9d5Sandi
3843dd5c225SAndreas Gohr    /**
3853dd5c225SAndreas Gohr     * Stop monospace formatting
3863dd5c225SAndreas Gohr     */
387*faf3f01bSAndreas Gohr    public function monospace_close()
388*faf3f01bSAndreas Gohr    {
389a2d649c4Sandi        $this->doc .= '</code>';
3900cecf9d5Sandi    }
3910cecf9d5Sandi
3923dd5c225SAndreas Gohr    /**
3933dd5c225SAndreas Gohr     * Start a subscript
3943dd5c225SAndreas Gohr     */
395*faf3f01bSAndreas Gohr    public function subscript_open()
396*faf3f01bSAndreas Gohr    {
397a2d649c4Sandi        $this->doc .= '<sub>';
3980cecf9d5Sandi    }
3990cecf9d5Sandi
4003dd5c225SAndreas Gohr    /**
4013dd5c225SAndreas Gohr     * Stop a subscript
4023dd5c225SAndreas Gohr     */
403*faf3f01bSAndreas Gohr    public function subscript_close()
404*faf3f01bSAndreas Gohr    {
405a2d649c4Sandi        $this->doc .= '</sub>';
4060cecf9d5Sandi    }
4070cecf9d5Sandi
4083dd5c225SAndreas Gohr    /**
4093dd5c225SAndreas Gohr     * Start a superscript
4103dd5c225SAndreas Gohr     */
411*faf3f01bSAndreas Gohr    public function superscript_open()
412*faf3f01bSAndreas Gohr    {
413a2d649c4Sandi        $this->doc .= '<sup>';
4140cecf9d5Sandi    }
4150cecf9d5Sandi
4163dd5c225SAndreas Gohr    /**
4173dd5c225SAndreas Gohr     * Stop a superscript
4183dd5c225SAndreas Gohr     */
419*faf3f01bSAndreas Gohr    public function superscript_close()
420*faf3f01bSAndreas Gohr    {
421a2d649c4Sandi        $this->doc .= '</sup>';
4220cecf9d5Sandi    }
4230cecf9d5Sandi
4243dd5c225SAndreas Gohr    /**
4253dd5c225SAndreas Gohr     * Start deleted (strike-through) formatting
4263dd5c225SAndreas Gohr     */
427*faf3f01bSAndreas Gohr    public function deleted_open()
428*faf3f01bSAndreas Gohr    {
429a2d649c4Sandi        $this->doc .= '<del>';
4300cecf9d5Sandi    }
4310cecf9d5Sandi
4323dd5c225SAndreas Gohr    /**
4333dd5c225SAndreas Gohr     * Stop deleted (strike-through) formatting
4343dd5c225SAndreas Gohr     */
435*faf3f01bSAndreas Gohr    public function deleted_close()
436*faf3f01bSAndreas Gohr    {
437a2d649c4Sandi        $this->doc .= '</del>';
4380cecf9d5Sandi    }
4390cecf9d5Sandi
4403fd0b676Sandi    /**
4413fd0b676Sandi     * Callback for footnote start syntax
4423fd0b676Sandi     *
4433fd0b676Sandi     * All following content will go to the footnote instead of
444d74aace9Schris     * the document. To achieve this the previous rendered content
4453fd0b676Sandi     * is moved to $store and $doc is cleared
4463fd0b676Sandi     *
4473fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
4483fd0b676Sandi     */
449*faf3f01bSAndreas Gohr    public function footnote_open()
450*faf3f01bSAndreas Gohr    {
4517764a90aSandi
4527764a90aSandi        // move current content to store and record footnote
4537764a90aSandi        $this->store = $this->doc;
4547764a90aSandi        $this->doc = '';
4550cecf9d5Sandi    }
4560cecf9d5Sandi
4573fd0b676Sandi    /**
4583fd0b676Sandi     * Callback for footnote end syntax
4593fd0b676Sandi     *
4603fd0b676Sandi     * All rendered content is moved to the $footnotes array and the old
4613fd0b676Sandi     * content is restored from $store again
4623fd0b676Sandi     *
4633fd0b676Sandi     * @author Andreas Gohr
4643fd0b676Sandi     */
465*faf3f01bSAndreas Gohr    public function footnote_close()
466*faf3f01bSAndreas Gohr    {
46716ec3e37SAndreas Gohr        /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
46816ec3e37SAndreas Gohr        static $fnid = 0;
46916ec3e37SAndreas Gohr        // assign new footnote id (we start at 1)
47016ec3e37SAndreas Gohr        $fnid++;
4717764a90aSandi
472d74aace9Schris        // recover footnote into the stack and restore old content
473d74aace9Schris        $footnote = $this->doc;
4747764a90aSandi        $this->doc = $this->store;
4757764a90aSandi        $this->store = '';
476d74aace9Schris
477d74aace9Schris        // check to see if this footnote has been seen before
478d74aace9Schris        $i = array_search($footnote, $this->footnotes);
479d74aace9Schris
480d74aace9Schris        if ($i === false) {
481d74aace9Schris            // its a new footnote, add it to the $footnotes array
48216ec3e37SAndreas Gohr            $this->footnotes[$fnid] = $footnote;
483d74aace9Schris        } else {
48416ec3e37SAndreas Gohr            // seen this one before, save a placeholder
48516ec3e37SAndreas Gohr            $this->footnotes[$fnid] = "@@FNT" . ($i);
486d74aace9Schris        }
487d74aace9Schris
4886b379cbfSAndreas Gohr        // output the footnote reference and link
48916ec3e37SAndreas Gohr        $this->doc .= '<sup><a href="#fn__' . $fnid . '" id="fnt__' . $fnid . '" class="fn_top">' . $fnid . ')</a></sup>';
4900cecf9d5Sandi    }
4910cecf9d5Sandi
4923dd5c225SAndreas Gohr    /**
4933dd5c225SAndreas Gohr     * Open an unordered list
4940c4c0281SGerrit Uitslag     *
4957d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
4963dd5c225SAndreas Gohr     */
497*faf3f01bSAndreas Gohr    public function listu_open($classes = null)
498*faf3f01bSAndreas Gohr    {
4990c4c0281SGerrit Uitslag        $class = '';
5000c4c0281SGerrit Uitslag        if ($classes !== null) {
501*faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
5020c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
5030c4c0281SGerrit Uitslag        }
5040c4c0281SGerrit Uitslag        $this->doc .= "<ul$class>" . DOKU_LF;
5050cecf9d5Sandi    }
5060cecf9d5Sandi
5073dd5c225SAndreas Gohr    /**
5083dd5c225SAndreas Gohr     * Close an unordered list
5093dd5c225SAndreas Gohr     */
510*faf3f01bSAndreas Gohr    public function listu_close()
511*faf3f01bSAndreas Gohr    {
512a2d649c4Sandi        $this->doc .= '</ul>' . DOKU_LF;
5130cecf9d5Sandi    }
5140cecf9d5Sandi
5153dd5c225SAndreas Gohr    /**
5163dd5c225SAndreas Gohr     * Open an ordered list
5170c4c0281SGerrit Uitslag     *
5187d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
5193dd5c225SAndreas Gohr     */
520*faf3f01bSAndreas Gohr    public function listo_open($classes = null)
521*faf3f01bSAndreas Gohr    {
5220c4c0281SGerrit Uitslag        $class = '';
5230c4c0281SGerrit Uitslag        if ($classes !== null) {
524*faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
5250c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
5260c4c0281SGerrit Uitslag        }
5270c4c0281SGerrit Uitslag        $this->doc .= "<ol$class>" . DOKU_LF;
5280cecf9d5Sandi    }
5290cecf9d5Sandi
5303dd5c225SAndreas Gohr    /**
5313dd5c225SAndreas Gohr     * Close an ordered list
5323dd5c225SAndreas Gohr     */
533*faf3f01bSAndreas Gohr    public function listo_close()
534*faf3f01bSAndreas Gohr    {
535a2d649c4Sandi        $this->doc .= '</ol>' . DOKU_LF;
5360cecf9d5Sandi    }
5370cecf9d5Sandi
5383dd5c225SAndreas Gohr    /**
5393dd5c225SAndreas Gohr     * Open a list item
5403dd5c225SAndreas Gohr     *
5413dd5c225SAndreas Gohr     * @param int $level the nesting level
542e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
5433dd5c225SAndreas Gohr     */
544*faf3f01bSAndreas Gohr    public function listitem_open($level, $node = false)
545*faf3f01bSAndreas Gohr    {
546e3a24861SChristopher Smith        $branching = $node ? ' node' : '';
547e3a24861SChristopher Smith        $this->doc .= '<li class="level' . $level . $branching . '">';
5480cecf9d5Sandi    }
5490cecf9d5Sandi
5503dd5c225SAndreas Gohr    /**
5513dd5c225SAndreas Gohr     * Close a list item
5523dd5c225SAndreas Gohr     */
553*faf3f01bSAndreas Gohr    public function listitem_close()
554*faf3f01bSAndreas Gohr    {
555a2d649c4Sandi        $this->doc .= '</li>' . DOKU_LF;
5560cecf9d5Sandi    }
5570cecf9d5Sandi
5583dd5c225SAndreas Gohr    /**
5593dd5c225SAndreas Gohr     * Start the content of a list item
5603dd5c225SAndreas Gohr     */
561*faf3f01bSAndreas Gohr    public function listcontent_open()
562*faf3f01bSAndreas Gohr    {
56390db23d7Schris        $this->doc .= '<div class="li">';
5640cecf9d5Sandi    }
5650cecf9d5Sandi
5663dd5c225SAndreas Gohr    /**
5673dd5c225SAndreas Gohr     * Stop the content of a list item
5683dd5c225SAndreas Gohr     */
569*faf3f01bSAndreas Gohr    public function listcontent_close()
570*faf3f01bSAndreas Gohr    {
57159869a4bSAnika Henke        $this->doc .= '</div>' . DOKU_LF;
5720cecf9d5Sandi    }
5730cecf9d5Sandi
5743dd5c225SAndreas Gohr    /**
5753dd5c225SAndreas Gohr     * Output unformatted $text
5763dd5c225SAndreas Gohr     *
5773dd5c225SAndreas Gohr     * Defaults to $this->cdata()
5783dd5c225SAndreas Gohr     *
5793dd5c225SAndreas Gohr     * @param string $text
5803dd5c225SAndreas Gohr     */
581*faf3f01bSAndreas Gohr    public function unformatted($text)
582*faf3f01bSAndreas Gohr    {
583a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
5840cecf9d5Sandi    }
5850cecf9d5Sandi
5860cecf9d5Sandi    /**
5873dd5c225SAndreas Gohr     * Start a block quote
5883dd5c225SAndreas Gohr     */
589*faf3f01bSAndreas Gohr    public function quote_open()
590*faf3f01bSAndreas Gohr    {
59196331712SAnika Henke        $this->doc .= '<blockquote><div class="no">' . DOKU_LF;
5920cecf9d5Sandi    }
5930cecf9d5Sandi
5943dd5c225SAndreas Gohr    /**
5953dd5c225SAndreas Gohr     * Stop a block quote
5963dd5c225SAndreas Gohr     */
597*faf3f01bSAndreas Gohr    public function quote_close()
598*faf3f01bSAndreas Gohr    {
59996331712SAnika Henke        $this->doc .= '</div></blockquote>' . DOKU_LF;
6000cecf9d5Sandi    }
6010cecf9d5Sandi
6023dd5c225SAndreas Gohr    /**
6033dd5c225SAndreas Gohr     * Output preformatted text
6043dd5c225SAndreas Gohr     *
6053dd5c225SAndreas Gohr     * @param string $text
6063dd5c225SAndreas Gohr     */
607*faf3f01bSAndreas Gohr    public function preformatted($text)
608*faf3f01bSAndreas Gohr    {
609c9250713SAnika Henke        $this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text), "\n\r") . '</pre>' . DOKU_LF;
6103d491f75SAndreas Gohr    }
6113d491f75SAndreas Gohr
6123dd5c225SAndreas Gohr    /**
6133dd5c225SAndreas Gohr     * Display text as file content, optionally syntax highlighted
6143dd5c225SAndreas Gohr     *
6153dd5c225SAndreas Gohr     * @param string $text text to show
6163dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6173dd5c225SAndreas Gohr     * @param string $filename file path label
618e2d88156SLarsDW223     * @param array $options assoziative array with additional geshi options
6193dd5c225SAndreas Gohr     */
620*faf3f01bSAndreas Gohr    public function file($text, $language = null, $filename = null, $options = null)
621*faf3f01bSAndreas Gohr    {
622e2d88156SLarsDW223        $this->_highlight('file', $text, $language, $filename, $options);
6233d491f75SAndreas Gohr    }
6243d491f75SAndreas Gohr
6253dd5c225SAndreas Gohr    /**
6263dd5c225SAndreas Gohr     * Display text as code content, optionally syntax highlighted
6273dd5c225SAndreas Gohr     *
6283dd5c225SAndreas Gohr     * @param string $text text to show
6293dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6303dd5c225SAndreas Gohr     * @param string $filename file path label
631e2d88156SLarsDW223     * @param array $options assoziative array with additional geshi options
6323dd5c225SAndreas Gohr     */
633*faf3f01bSAndreas Gohr    public function code($text, $language = null, $filename = null, $options = null)
634*faf3f01bSAndreas Gohr    {
635e2d88156SLarsDW223        $this->_highlight('code', $text, $language, $filename, $options);
6363d491f75SAndreas Gohr    }
6373d491f75SAndreas Gohr
6380cecf9d5Sandi    /**
6393d491f75SAndreas Gohr     * Use GeSHi to highlight language syntax in code and file blocks
6403fd0b676Sandi     *
6413dd5c225SAndreas Gohr     * @param string $type code|file
6423dd5c225SAndreas Gohr     * @param string $text text to show
6433dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6443dd5c225SAndreas Gohr     * @param string $filename file path label
645e2d88156SLarsDW223     * @param array $options assoziative array with additional geshi options
646*faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
6470cecf9d5Sandi     */
648*faf3f01bSAndreas Gohr    public function _highlight($type, $text, $language = null, $filename = null, $options = null)
649*faf3f01bSAndreas Gohr    {
6503d491f75SAndreas Gohr        global $ID;
6513d491f75SAndreas Gohr        global $lang;
652ec57f119SLarsDW223        global $INPUT;
6533d491f75SAndreas Gohr
654bf8f8509SAndreas Gohr        $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language ?? '');
65556bd9509SPhy
6563d491f75SAndreas Gohr        if ($filename) {
657190c56e8SAndreas Gohr            // add icon
658*faf3f01bSAndreas Gohr            [$ext] = mimetype($filename, false);
659190c56e8SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
660190c56e8SAndreas Gohr            $class = 'mediafile mf_' . $class;
661190c56e8SAndreas Gohr
662ec57f119SLarsDW223            $offset = 0;
663ec57f119SLarsDW223            if ($INPUT->has('codeblockOffset')) {
664ec57f119SLarsDW223                $offset = $INPUT->str('codeblockOffset');
665ec57f119SLarsDW223            }
6663d491f75SAndreas Gohr            $this->doc .= '<dl class="' . $type . '">' . DOKU_LF;
66764159a61SAndreas Gohr            $this->doc .= '<dt><a href="' .
66864159a61SAndreas Gohr                exportlink(
66964159a61SAndreas Gohr                    $ID,
67064159a61SAndreas Gohr                    'code',
671*faf3f01bSAndreas Gohr                    ['codeblock' => $offset + $this->_codeblock]
67264159a61SAndreas Gohr                ) . '" title="' . $lang['download'] . '" class="' . $class . '">';
6733d491f75SAndreas Gohr            $this->doc .= hsc($filename);
6743d491f75SAndreas Gohr            $this->doc .= '</a></dt>' . DOKU_LF . '<dd>';
6753d491f75SAndreas Gohr        }
6760cecf9d5Sandi
6772401f18dSSyntaxseed        if ($text[0] == "\n") {
678d43aac1cSGina Haeussge            $text = substr($text, 1);
679d43aac1cSGina Haeussge        }
680d43aac1cSGina Haeussge        if (substr($text, -1) == "\n") {
681d43aac1cSGina Haeussge            $text = substr($text, 0, -1);
682d43aac1cSGina Haeussge        }
683d43aac1cSGina Haeussge
684a056e285SPhy        if (empty($language)) { // empty is faster than is_null and can prevent '' string
6853d491f75SAndreas Gohr            $this->doc .= '<pre class="' . $type . '">' . $this->_xmlEntities($text) . '</pre>' . DOKU_LF;
6860cecf9d5Sandi        } else {
6873d491f75SAndreas Gohr            $class = 'code'; //we always need the code class to make the syntax highlighting apply
6883d491f75SAndreas Gohr            if ($type != 'code') $class .= ' ' . $type;
6893d491f75SAndreas Gohr
69064159a61SAndreas Gohr            $this->doc .= "<pre class=\"$class $language\">" .
69164159a61SAndreas Gohr                p_xhtml_cached_geshi($text, $language, '', $options) .
69264159a61SAndreas Gohr                '</pre>' . DOKU_LF;
6930cecf9d5Sandi        }
6943d491f75SAndreas Gohr
6953d491f75SAndreas Gohr        if ($filename) {
6963d491f75SAndreas Gohr            $this->doc .= '</dd></dl>' . DOKU_LF;
6973d491f75SAndreas Gohr        }
6983d491f75SAndreas Gohr
6993d491f75SAndreas Gohr        $this->_codeblock++;
7000cecf9d5Sandi    }
7010cecf9d5Sandi
7023dd5c225SAndreas Gohr    /**
7033dd5c225SAndreas Gohr     * Format an acronym
7043dd5c225SAndreas Gohr     *
7053dd5c225SAndreas Gohr     * Uses $this->acronyms
7063dd5c225SAndreas Gohr     *
7073dd5c225SAndreas Gohr     * @param string $acronym
7083dd5c225SAndreas Gohr     */
709*faf3f01bSAndreas Gohr    public function acronym($acronym)
710*faf3f01bSAndreas Gohr    {
7110cecf9d5Sandi
7120cecf9d5Sandi        if (array_key_exists($acronym, $this->acronyms)) {
7130cecf9d5Sandi
714433bef32Sandi            $title = $this->_xmlEntities($this->acronyms[$acronym]);
7150cecf9d5Sandi
716940db3a3SAnika Henke            $this->doc .= '<abbr title="' . $title
717940db3a3SAnika Henke                . '">' . $this->_xmlEntities($acronym) . '</abbr>';
7180cecf9d5Sandi
7190cecf9d5Sandi        } else {
720a2d649c4Sandi            $this->doc .= $this->_xmlEntities($acronym);
7210cecf9d5Sandi        }
7220cecf9d5Sandi    }
7230cecf9d5Sandi
7243dd5c225SAndreas Gohr    /**
7253dd5c225SAndreas Gohr     * Format a smiley
7263dd5c225SAndreas Gohr     *
7273dd5c225SAndreas Gohr     * Uses $this->smiley
7283dd5c225SAndreas Gohr     *
7293dd5c225SAndreas Gohr     * @param string $smiley
7303dd5c225SAndreas Gohr     */
731*faf3f01bSAndreas Gohr    public function smiley($smiley)
732*faf3f01bSAndreas Gohr    {
733b09504a9SAndreas Gohr        if (isset($this->smileys[$smiley])) {
734f62ea8a1Sandi            $this->doc .= '<img src="' . DOKU_BASE . 'lib/images/smileys/' . $this->smileys[$smiley] .
735b09504a9SAndreas Gohr                '" class="icon smiley" alt="' . $this->_xmlEntities($smiley) . '" />';
7360cecf9d5Sandi        } else {
737a2d649c4Sandi            $this->doc .= $this->_xmlEntities($smiley);
7380cecf9d5Sandi        }
7390cecf9d5Sandi    }
7400cecf9d5Sandi
7413dd5c225SAndreas Gohr    /**
7423dd5c225SAndreas Gohr     * Format an entity
7433dd5c225SAndreas Gohr     *
7443dd5c225SAndreas Gohr     * Entities are basically small text replacements
7453dd5c225SAndreas Gohr     *
7463dd5c225SAndreas Gohr     * Uses $this->entities
7473dd5c225SAndreas Gohr     *
7483dd5c225SAndreas Gohr     * @param string $entity
7494de671bcSandi     */
750*faf3f01bSAndreas Gohr    public function entity($entity)
751*faf3f01bSAndreas Gohr    {
7520cecf9d5Sandi        if (array_key_exists($entity, $this->entities)) {
753a2d649c4Sandi            $this->doc .= $this->entities[$entity];
7540cecf9d5Sandi        } else {
755a2d649c4Sandi            $this->doc .= $this->_xmlEntities($entity);
7560cecf9d5Sandi        }
7570cecf9d5Sandi    }
7580cecf9d5Sandi
7593dd5c225SAndreas Gohr    /**
7603dd5c225SAndreas Gohr     * Typographically format a multiply sign
7613dd5c225SAndreas Gohr     *
7623dd5c225SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
7633dd5c225SAndreas Gohr     *
7643dd5c225SAndreas Gohr     * @param string|int $x first value
7653dd5c225SAndreas Gohr     * @param string|int $y second value
7663dd5c225SAndreas Gohr     */
767*faf3f01bSAndreas Gohr    public function multiplyentity($x, $y)
768*faf3f01bSAndreas Gohr    {
769a2d649c4Sandi        $this->doc .= "$x&times;$y";
7700cecf9d5Sandi    }
7710cecf9d5Sandi
7723dd5c225SAndreas Gohr    /**
7733dd5c225SAndreas Gohr     * Render an opening single quote char (language specific)
7743dd5c225SAndreas Gohr     */
775*faf3f01bSAndreas Gohr    public function singlequoteopening()
776*faf3f01bSAndreas Gohr    {
77771b40da2SAnika Henke        global $lang;
77871b40da2SAnika Henke        $this->doc .= $lang['singlequoteopening'];
7790cecf9d5Sandi    }
7800cecf9d5Sandi
7813dd5c225SAndreas Gohr    /**
7823dd5c225SAndreas Gohr     * Render a closing single quote char (language specific)
7833dd5c225SAndreas Gohr     */
784*faf3f01bSAndreas Gohr    public function singlequoteclosing()
785*faf3f01bSAndreas Gohr    {
78671b40da2SAnika Henke        global $lang;
78771b40da2SAnika Henke        $this->doc .= $lang['singlequoteclosing'];
7880cecf9d5Sandi    }
7890cecf9d5Sandi
7903dd5c225SAndreas Gohr    /**
7913dd5c225SAndreas Gohr     * Render an apostrophe char (language specific)
7923dd5c225SAndreas Gohr     */
793*faf3f01bSAndreas Gohr    public function apostrophe()
794*faf3f01bSAndreas Gohr    {
79557d757d1SAndreas Gohr        global $lang;
796a8bd192aSAndreas Gohr        $this->doc .= $lang['apostrophe'];
79757d757d1SAndreas Gohr    }
79857d757d1SAndreas Gohr
7993dd5c225SAndreas Gohr    /**
8003dd5c225SAndreas Gohr     * Render an opening double quote char (language specific)
8013dd5c225SAndreas Gohr     */
802*faf3f01bSAndreas Gohr    public function doublequoteopening()
803*faf3f01bSAndreas Gohr    {
80471b40da2SAnika Henke        global $lang;
80571b40da2SAnika Henke        $this->doc .= $lang['doublequoteopening'];
8060cecf9d5Sandi    }
8070cecf9d5Sandi
8083dd5c225SAndreas Gohr    /**
8093dd5c225SAndreas Gohr     * Render an closinging double quote char (language specific)
8103dd5c225SAndreas Gohr     */
811*faf3f01bSAndreas Gohr    public function doublequoteclosing()
812*faf3f01bSAndreas Gohr    {
81371b40da2SAnika Henke        global $lang;
81471b40da2SAnika Henke        $this->doc .= $lang['doublequoteclosing'];
8150cecf9d5Sandi    }
8160cecf9d5Sandi
8170cecf9d5Sandi    /**
8183dd5c225SAndreas Gohr     * Render a CamelCase link
8193dd5c225SAndreas Gohr     *
8203dd5c225SAndreas Gohr     * @param string $link The link name
821122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
8220c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
8230c4c0281SGerrit Uitslag     *
8243dd5c225SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
8250cecf9d5Sandi     */
826*faf3f01bSAndreas Gohr    public function camelcaselink($link, $returnonly = false)
827*faf3f01bSAndreas Gohr    {
828122f2d46SAndreas Böhler        if ($returnonly) {
829122f2d46SAndreas Böhler            return $this->internallink($link, $link, null, true);
830122f2d46SAndreas Böhler        } else {
83111d0aa47Sandi            $this->internallink($link, $link);
8320cecf9d5Sandi        }
833122f2d46SAndreas Böhler    }
8340cecf9d5Sandi
8353dd5c225SAndreas Gohr    /**
8363dd5c225SAndreas Gohr     * Render a page local link
8373dd5c225SAndreas Gohr     *
8383dd5c225SAndreas Gohr     * @param string $hash hash link identifier
8393dd5c225SAndreas Gohr     * @param string $name name for the link
840122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
8410c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
8423dd5c225SAndreas Gohr     */
843*faf3f01bSAndreas Gohr    public function locallink($hash, $name = null, $returnonly = false)
844*faf3f01bSAndreas Gohr    {
8450b7c14c2Sandi        global $ID;
8460b7c14c2Sandi        $name = $this->_getLinkTitle($name, $hash, $isImage);
8470b7c14c2Sandi        $hash = $this->_headerToLink($hash);
848e260f93bSAnika Henke        $title = $ID . ' ↵';
849122f2d46SAndreas Böhler
850122f2d46SAndreas Böhler        $doc = '<a href="#' . $hash . '" title="' . $title . '" class="wikilink1">';
851122f2d46SAndreas Böhler        $doc .= $name;
852122f2d46SAndreas Böhler        $doc .= '</a>';
853122f2d46SAndreas Böhler
854122f2d46SAndreas Böhler        if ($returnonly) {
855122f2d46SAndreas Böhler            return $doc;
856122f2d46SAndreas Böhler        } else {
857122f2d46SAndreas Böhler            $this->doc .= $doc;
858122f2d46SAndreas Böhler        }
8590b7c14c2Sandi    }
8600b7c14c2Sandi
861cffcc403Sandi    /**
8623fd0b676Sandi     * Render an internal Wiki Link
8633fd0b676Sandi     *
864fe9ec250SChris Smith     * $search,$returnonly & $linktype are not for the renderer but are used
865cffcc403Sandi     * elsewhere - no need to implement them in other renderers
8663fd0b676Sandi     *
867f23eef27SGerrit Uitslag     * @param string $id pageid
868f23eef27SGerrit Uitslag     * @param string|null $name link name
869f23eef27SGerrit Uitslag     * @param string|null $search adds search url param
870f23eef27SGerrit Uitslag     * @param bool $returnonly whether to return html or write to doc attribute
871f23eef27SGerrit Uitslag     * @param string $linktype type to set use of headings
872f23eef27SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
873*faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
874cffcc403Sandi     */
875*faf3f01bSAndreas Gohr    public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content')
876*faf3f01bSAndreas Gohr    {
877ba11bd29Sandi        global $conf;
87837e34a5eSandi        global $ID;
879c4dda6afSAnika Henke        global $INFO;
88044653a53SAdrian Lang
8813d5e07d9SAdrian Lang        $params = '';
8823d5e07d9SAdrian Lang        $parts = explode('?', $id, 2);
8833d5e07d9SAdrian Lang        if (count($parts) === 2) {
8843d5e07d9SAdrian Lang            $id = $parts[0];
8853d5e07d9SAdrian Lang            $params = $parts[1];
88644653a53SAdrian Lang        }
88744653a53SAdrian Lang
888fda14ffcSIzidor Matušov        // For empty $id we need to know the current $ID
889fda14ffcSIzidor Matušov        // We need this check because _simpleTitle needs
890fda14ffcSIzidor Matušov        // correct $id and resolve_pageid() use cleanID($id)
891fda14ffcSIzidor Matušov        // (some things could be lost)
892fda14ffcSIzidor Matušov        if ($id === '') {
893fda14ffcSIzidor Matušov            $id = $ID;
894fda14ffcSIzidor Matušov        }
895fda14ffcSIzidor Matušov
8960339c872Sjan        // default name is based on $id as given
8970339c872Sjan        $default = $this->_simpleTitle($id);
898ad32e47eSAndreas Gohr
8990339c872Sjan        // now first resolve and clean up the $id
9008c6be208SAndreas Gohr        $id = (new PageResolver($ID))->resolveId($id, $this->date_at, true);
9018c6be208SAndreas Gohr        $exists = page_exists($id, $this->date_at, false, true);
902fda14ffcSIzidor Matušov
903*faf3f01bSAndreas Gohr        $link = [];
904fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
9050e1c636eSandi        if (!$isImage) {
9060e1c636eSandi            if ($exists) {
907ba11bd29Sandi                $class = 'wikilink1';
9080cecf9d5Sandi            } else {
909ba11bd29Sandi                $class = 'wikilink2';
91044a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
9110cecf9d5Sandi            }
9120cecf9d5Sandi        } else {
913ba11bd29Sandi            $class = 'media';
9140cecf9d5Sandi        }
9150cecf9d5Sandi
916a1685bedSandi        //keep hash anchor
917*faf3f01bSAndreas Gohr        [$id, $hash] = sexplode('#', $id, 2);
918943dedc6SAndreas Gohr        if (!empty($hash)) $hash = $this->_headerToLink($hash);
919a1685bedSandi
920ba11bd29Sandi        //prepare for formating
921ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
922ba11bd29Sandi        $link['style'] = '';
923ba11bd29Sandi        $link['pre'] = '';
924ba11bd29Sandi        $link['suf'] = '';
925bbac1489SPhy        $link['more'] = 'data-wiki-id="' . $id . '"'; // id is already cleaned
926ba11bd29Sandi        $link['class'] = $class;
9275c2eed9aSlisps        if ($this->date_at) {
928912a6d48SPhy            $params = $params . '&at=' . rawurlencode($this->date_at);
9295c2eed9aSlisps        }
93044653a53SAdrian Lang        $link['url'] = wl($id, $params);
931ba11bd29Sandi        $link['name'] = $name;
932ba11bd29Sandi        $link['title'] = $id;
933723d78dbSandi        //add search string
934723d78dbSandi        if ($search) {
935546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
936546d3a99SAndreas Gohr            if (is_array($search)) {
937546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
938*faf3f01bSAndreas Gohr                $link['url'] .= 's[]=' . implode('&amp;s[]=', $search);
939546d3a99SAndreas Gohr            } else {
940546d3a99SAndreas Gohr                $link['url'] .= 's=' . rawurlencode($search);
941546d3a99SAndreas Gohr            }
942723d78dbSandi        }
943723d78dbSandi
944a1685bedSandi        //keep hash
945a1685bedSandi        if ($hash) $link['url'] .= '#' . $hash;
946a1685bedSandi
947ba11bd29Sandi        //output formatted
948cffcc403Sandi        if ($returnonly) {
949cffcc403Sandi            return $this->_formatLink($link);
950cffcc403Sandi        } else {
951a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
9520cecf9d5Sandi        }
953cffcc403Sandi    }
9540cecf9d5Sandi
9553dd5c225SAndreas Gohr    /**
9563dd5c225SAndreas Gohr     * Render an external link
9573dd5c225SAndreas Gohr     *
9583dd5c225SAndreas Gohr     * @param string $url full URL with scheme
9593dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
960122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
9610c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
9623dd5c225SAndreas Gohr     */
963*faf3f01bSAndreas Gohr    public function externallink($url, $name = null, $returnonly = false)
964*faf3f01bSAndreas Gohr    {
965b625487dSandi        global $conf;
9660cecf9d5Sandi
967433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
9686f0c5dbfSandi
969b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
970b52b1596SAndreas Gohr        if (is_null($this->schemes)) $this->schemes = getSchemes();
971*faf3f01bSAndreas Gohr        [$scheme] = explode('://', $url);
972b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
973b52b1596SAndreas Gohr        if (!in_array($scheme, $this->schemes)) $url = '';
974b52b1596SAndreas Gohr
975b52b1596SAndreas Gohr        // is there still an URL?
976b52b1596SAndreas Gohr        if (!$url) {
97749cef4fdSAndreas Böhler            if ($returnonly) {
97849cef4fdSAndreas Böhler                return $name;
97949cef4fdSAndreas Böhler            } else {
980b52b1596SAndreas Gohr                $this->doc .= $name;
98149cef4fdSAndreas Böhler            }
982b52b1596SAndreas Gohr            return;
983b52b1596SAndreas Gohr        }
984b52b1596SAndreas Gohr
985b52b1596SAndreas Gohr        // set class
9860cecf9d5Sandi        if (!$isImage) {
987b625487dSandi            $class = 'urlextern';
9880cecf9d5Sandi        } else {
989b625487dSandi            $class = 'media';
9900cecf9d5Sandi        }
9910cecf9d5Sandi
992b625487dSandi        //prepare for formating
993*faf3f01bSAndreas Gohr        $link = [];
994b625487dSandi        $link['target'] = $conf['target']['extern'];
995b625487dSandi        $link['style'] = '';
996b625487dSandi        $link['pre'] = '';
997b625487dSandi        $link['suf'] = '';
9985e163278SAndreas Gohr        $link['more'] = '';
999b625487dSandi        $link['class'] = $class;
1000b625487dSandi        $link['url'] = $url;
1001914045f3SAndreas Gohr        $link['rel'] = '';
1002e1c10e4dSchris
1003b625487dSandi        $link['name'] = $name;
1004433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
10055ddd0bbbSStarArmy        if ($conf['relnofollow']) $link['rel'] .= ' ugc nofollow';
1006914045f3SAndreas Gohr        if ($conf['target']['extern']) $link['rel'] .= ' noopener';
10070cecf9d5Sandi
1008b625487dSandi        //output formatted
1009122f2d46SAndreas Böhler        if ($returnonly) {
1010122f2d46SAndreas Böhler            return $this->_formatLink($link);
1011122f2d46SAndreas Böhler        } else {
1012a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
10130cecf9d5Sandi        }
1014122f2d46SAndreas Böhler    }
10150cecf9d5Sandi
10160cecf9d5Sandi    /**
10173dd5c225SAndreas Gohr     * Render an interwiki link
10183dd5c225SAndreas Gohr     *
10193dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
10203dd5c225SAndreas Gohr     *
10213dd5c225SAndreas Gohr     * @param string $match original link - probably not much use
10223dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
10233dd5c225SAndreas Gohr     * @param string $wikiName indentifier (shortcut) for the remote wiki
10243dd5c225SAndreas Gohr     * @param string $wikiUri the fragment parsed from the original link
1025122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
10260c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10270cecf9d5Sandi     */
1028*faf3f01bSAndreas Gohr    public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false)
1029*faf3f01bSAndreas Gohr    {
1030b625487dSandi        global $conf;
10310cecf9d5Sandi
1032*faf3f01bSAndreas Gohr        $link = [];
103397a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
103497a3e4e3Sandi        $link['pre'] = '';
103597a3e4e3Sandi        $link['suf'] = '';
10365e163278SAndreas Gohr        $link['more'] = '';
1037433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage);
1038914045f3SAndreas Gohr        $link['rel'] = '';
10390cecf9d5Sandi
104097a3e4e3Sandi        //get interwiki URL
10416496c33fSGerrit Uitslag        $exists = null;
10426496c33fSGerrit Uitslag        $url = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
10430cecf9d5Sandi
104497a3e4e3Sandi        if (!$isImage) {
10459d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
10469d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
10471c2d1019SAndreas Gohr        } else {
10481c2d1019SAndreas Gohr            $link['class'] = 'media';
104997a3e4e3Sandi        }
10500cecf9d5Sandi
105197a3e4e3Sandi        //do we stay at the same server? Use local target
1052*faf3f01bSAndreas Gohr        if (strpos($url, DOKU_URL) === 0 || strpos($url, DOKU_BASE) === 0) {
105397a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
105497a3e4e3Sandi        }
10556496c33fSGerrit Uitslag        if ($exists !== null && !$isImage) {
10566496c33fSGerrit Uitslag            if ($exists) {
10576496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
10586496c33fSGerrit Uitslag            } else {
10596496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
1060914045f3SAndreas Gohr                $link['rel'] .= ' nofollow';
10616496c33fSGerrit Uitslag            }
10626496c33fSGerrit Uitslag        }
1063914045f3SAndreas Gohr        if ($conf['target']['interwiki']) $link['rel'] .= ' noopener';
10640cecf9d5Sandi
106597a3e4e3Sandi        $link['url'] = $url;
1066f7711f2bSAndreas Gohr        $link['title'] = $this->_xmlEntities($link['url']);
106797a3e4e3Sandi
106897a3e4e3Sandi        // output formatted
1069122f2d46SAndreas Böhler        if ($returnonly) {
1070abde5980SPhy            if ($url == '') return $link['name'];
1071122f2d46SAndreas Böhler            return $this->_formatLink($link);
1072*faf3f01bSAndreas Gohr        } elseif ($url == '') {
1073*faf3f01bSAndreas Gohr            $this->doc .= $link['name'];
1074*faf3f01bSAndreas Gohr        } else $this->doc .= $this->_formatLink($link);
1075122f2d46SAndreas Böhler    }
10760cecf9d5Sandi
10770cecf9d5Sandi    /**
10783dd5c225SAndreas Gohr     * Link to windows share
10793dd5c225SAndreas Gohr     *
10803dd5c225SAndreas Gohr     * @param string $url the link
10813dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
1082122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
10830c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10840cecf9d5Sandi     */
1085*faf3f01bSAndreas Gohr    public function windowssharelink($url, $name = null, $returnonly = false)
1086*faf3f01bSAndreas Gohr    {
10871d47afe1Sandi        global $conf;
10883dd5c225SAndreas Gohr
10891d47afe1Sandi        //simple setup
1090*faf3f01bSAndreas Gohr        $link = [];
10911d47afe1Sandi        $link['target'] = $conf['target']['windows'];
10921d47afe1Sandi        $link['pre'] = '';
10931d47afe1Sandi        $link['suf'] = '';
10941d47afe1Sandi        $link['style'] = '';
10950cecf9d5Sandi
1096433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
10970cecf9d5Sandi        if (!$isImage) {
10981d47afe1Sandi            $link['class'] = 'windows';
10990cecf9d5Sandi        } else {
11001d47afe1Sandi            $link['class'] = 'media';
11010cecf9d5Sandi        }
11020cecf9d5Sandi
1103433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
11041d47afe1Sandi        $url = str_replace('\\', '/', $url);
11051d47afe1Sandi        $url = 'file:///' . $url;
11061d47afe1Sandi        $link['url'] = $url;
11070cecf9d5Sandi
11081d47afe1Sandi        //output formatted
1109122f2d46SAndreas Böhler        if ($returnonly) {
1110122f2d46SAndreas Böhler            return $this->_formatLink($link);
1111122f2d46SAndreas Böhler        } else {
1112a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
11130cecf9d5Sandi        }
1114122f2d46SAndreas Böhler    }
11150cecf9d5Sandi
11163dd5c225SAndreas Gohr    /**
11173dd5c225SAndreas Gohr     * Render a linked E-Mail Address
11183dd5c225SAndreas Gohr     *
11193dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
11203dd5c225SAndreas Gohr     *
11213dd5c225SAndreas Gohr     * @param string $address Email-Address
11223dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
1123122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
11240c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
11253dd5c225SAndreas Gohr     */
1126*faf3f01bSAndreas Gohr    public function emaillink($address, $name = null, $returnonly = false)
1127*faf3f01bSAndreas Gohr    {
112871352defSandi        global $conf;
112971352defSandi        //simple setup
1130*faf3f01bSAndreas Gohr        $link = [];
113171352defSandi        $link['target'] = '';
113271352defSandi        $link['pre'] = '';
113371352defSandi        $link['suf'] = '';
113471352defSandi        $link['style'] = '';
113571352defSandi        $link['more'] = '';
11360cecf9d5Sandi
1137c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
11380cecf9d5Sandi        if (!$isImage) {
1139be96545cSAnika Henke            $link['class'] = 'mail';
11400cecf9d5Sandi        } else {
1141be96545cSAnika Henke            $link['class'] = 'media';
11420cecf9d5Sandi        }
11430cecf9d5Sandi
114407738714SAndreas Gohr        $address = $this->_xmlEntities($address);
114500a7b5adSEsther Brunner        $address = obfuscate($address);
1146*faf3f01bSAndreas Gohr
114700a7b5adSEsther Brunner        $title = $address;
11488c128049SAndreas Gohr
114971352defSandi        if (empty($name)) {
115000a7b5adSEsther Brunner            $name = $address;
115171352defSandi        }
11520cecf9d5Sandi
1153776b36ecSAndreas Gohr        if ($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1154776b36ecSAndreas Gohr
1155776b36ecSAndreas Gohr        $link['url'] = 'mailto:' . $address;
115671352defSandi        $link['name'] = $name;
115771352defSandi        $link['title'] = $title;
11580cecf9d5Sandi
115971352defSandi        //output formatted
1160122f2d46SAndreas Böhler        if ($returnonly) {
1161122f2d46SAndreas Böhler            return $this->_formatLink($link);
1162122f2d46SAndreas Böhler        } else {
1163a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
11640cecf9d5Sandi        }
1165122f2d46SAndreas Böhler    }
11660cecf9d5Sandi
11673dd5c225SAndreas Gohr    /**
11683dd5c225SAndreas Gohr     * Render an internal media file
11693dd5c225SAndreas Gohr     *
11703dd5c225SAndreas Gohr     * @param string $src media ID
11713dd5c225SAndreas Gohr     * @param string $title descriptive text
11723dd5c225SAndreas Gohr     * @param string $align left|center|right
11733dd5c225SAndreas Gohr     * @param int $width width of media in pixel
11743dd5c225SAndreas Gohr     * @param int $height height of media in pixel
11753dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
11763dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
11773dd5c225SAndreas Gohr     * @param bool $return return HTML instead of adding to $doc
11780c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
11793dd5c225SAndreas Gohr     */
1180de369923SAndreas Gohr    public function internalmedia($src, $title = null, $align = null, $width = null,
1181*faf3f01bSAndreas Gohr                                  $height = null, $cache = null, $linking = null, $return = false)
1182*faf3f01bSAndreas Gohr    {
118337e34a5eSandi        global $ID;
11848f34cf3dSMichael Große        if (strpos($src, '#') !== false) {
1185*faf3f01bSAndreas Gohr            [$src, $hash] = sexplode('#', $src, 2);
11868f34cf3dSMichael Große        }
11878c6be208SAndreas Gohr        $src = (new MediaResolver($ID))->resolveId($src, $this->date_at, true);
11888c6be208SAndreas Gohr        $exists = media_exists($src);
11890cecf9d5Sandi
1190d98d4540SBen Coburn        $noLink = false;
1191*faf3f01bSAndreas Gohr        $render = $linking != 'linkonly';
1192b739ff0fSPierre Spring        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
11933685f775Sandi
1194*faf3f01bSAndreas Gohr        [$ext, $mime] = mimetype($src, false);
1195b739ff0fSPierre Spring        if (substr($mime, 0, 5) == 'image' && $render) {
119664159a61SAndreas Gohr            $link['url'] = ml(
119764159a61SAndreas Gohr                $src,
1198*faf3f01bSAndreas Gohr                [
119964159a61SAndreas Gohr                    'id' => $ID,
120064159a61SAndreas Gohr                    'cache' => $cache,
120164159a61SAndreas Gohr                    'rev' => $this->_getLastMediaRevisionAt($src)
1202*faf3f01bSAndreas Gohr                ],
120364159a61SAndreas Gohr                ($linking == 'direct')
120464159a61SAndreas Gohr            );
1205f50634f0SAnika Henke        } elseif (($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
12062a2a2ba2SAnika Henke            // don't link movies
120744881bd0Shenning.noren            $noLink = true;
120855efc227SAndreas Gohr        } else {
12092ca14335SEsther Brunner            // add file icons
12109d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
12119d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_' . $class;
121264159a61SAndreas Gohr            $link['url'] = ml(
121364159a61SAndreas Gohr                $src,
1214*faf3f01bSAndreas Gohr                [
121564159a61SAndreas Gohr                    'id' => $ID,
121664159a61SAndreas Gohr                    'cache' => $cache,
121764159a61SAndreas Gohr                    'rev' => $this->_getLastMediaRevisionAt($src)
1218*faf3f01bSAndreas Gohr                ],
121964159a61SAndreas Gohr                true
122064159a61SAndreas Gohr            );
122191328684SMichael Hamann            if ($exists) $link['title'] .= ' (' . filesize_h(filesize(mediaFN($src))) . ')';
122255efc227SAndreas Gohr        }
12233685f775Sandi
12248f34cf3dSMichael Große        if (!empty($hash)) $link['url'] .= '#' . $hash;
122591df343aSAndreas Gohr
12266fe20453SGina Haeussge        //markup non existing files
12274a24b459SKate Arzamastseva        if (!$exists) {
12286fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
12294a24b459SKate Arzamastseva        }
12306fe20453SGina Haeussge
12313685f775Sandi        //output formatted
1232f50634f0SAnika Henke        if ($return) {
1233*faf3f01bSAndreas Gohr            if ($linking == 'nolink' || $noLink) {
1234*faf3f01bSAndreas Gohr                return $link['name'];
1235f50634f0SAnika Henke            } else {
1236*faf3f01bSAndreas Gohr                return $this->_formatLink($link);
1237*faf3f01bSAndreas Gohr            }
1238*faf3f01bSAndreas Gohr        } elseif ($linking == 'nolink' || $noLink) {
1239*faf3f01bSAndreas Gohr            $this->doc .= $link['name'];
1240*faf3f01bSAndreas Gohr        } else {
1241*faf3f01bSAndreas Gohr            $this->doc .= $this->_formatLink($link);
12420cecf9d5Sandi        }
1243f50634f0SAnika Henke    }
12440cecf9d5Sandi
12453dd5c225SAndreas Gohr    /**
12463dd5c225SAndreas Gohr     * Render an external media file
12473dd5c225SAndreas Gohr     *
12483dd5c225SAndreas Gohr     * @param string $src full media URL
12493dd5c225SAndreas Gohr     * @param string $title descriptive text
12503dd5c225SAndreas Gohr     * @param string $align left|center|right
12513dd5c225SAndreas Gohr     * @param int $width width of media in pixel
12523dd5c225SAndreas Gohr     * @param int $height height of media in pixel
12533dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
12543dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1255410ee62aSAnika Henke     * @param bool $return return HTML instead of adding to $doc
12560c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
12573dd5c225SAndreas Gohr     */
1258de369923SAndreas Gohr    public function externalmedia($src, $title = null, $align = null, $width = null,
1259*faf3f01bSAndreas Gohr                                  $height = null, $cache = null, $linking = null, $return = false)
1260*faf3f01bSAndreas Gohr    {
12616efc45a2SDmitry Katsubo        if (link_isinterwiki($src)) {
1262*faf3f01bSAndreas Gohr            [$shortcut, $reference] = sexplode('>', $src, 2, '');
12636efc45a2SDmitry Katsubo            $exists = null;
12646efc45a2SDmitry Katsubo            $src = $this->_resolveInterWiki($shortcut, $reference, $exists);
1265abde5980SPhy            if ($src == '' && empty($title)) {
1266abde5980SPhy                // make sure at least something will be shown in this case
1267abde5980SPhy                $title = $reference;
1268abde5980SPhy            }
12696efc45a2SDmitry Katsubo        }
1270*faf3f01bSAndreas Gohr        [$src, $hash] = sexplode('#', $src, 2);
1271d98d4540SBen Coburn        $noLink = false;
1272abde5980SPhy        if ($src == '') {
1273abde5980SPhy            // only output plaintext without link if there is no src
1274abde5980SPhy            $noLink = true;
1275abde5980SPhy        }
1276*faf3f01bSAndreas Gohr        $render = $linking != 'linkonly';
1277b739ff0fSPierre Spring        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1278b739ff0fSPierre Spring
1279*faf3f01bSAndreas Gohr        $link['url'] = ml($src, ['cache' => $cache]);
12803685f775Sandi
1281*faf3f01bSAndreas Gohr        [$ext, $mime] = mimetype($src, false);
1282b739ff0fSPierre Spring        if (substr($mime, 0, 5) == 'image' && $render) {
12832ca14335SEsther Brunner            // link only jpeg images
128444881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1285f50634f0SAnika Henke        } elseif (($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
12862a2a2ba2SAnika Henke            // don't link movies
128744881bd0Shenning.noren            $noLink = true;
12882ca14335SEsther Brunner        } else {
12892ca14335SEsther Brunner            // add file icons
129027bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
129127bf7924STom N Harris            $link['class'] .= ' mediafile mf_' . $class;
12922ca14335SEsther Brunner        }
12932ca14335SEsther Brunner
129491df343aSAndreas Gohr        if ($hash) $link['url'] .= '#' . $hash;
129591df343aSAndreas Gohr
12963685f775Sandi        //output formatted
1297410ee62aSAnika Henke        if ($return) {
1298410ee62aSAnika Henke            if ($linking == 'nolink' || $noLink) return $link['name'];
1299410ee62aSAnika Henke            else return $this->_formatLink($link);
1300*faf3f01bSAndreas Gohr        } elseif ($linking == 'nolink' || $noLink) {
1301*faf3f01bSAndreas Gohr            $this->doc .= $link['name'];
1302*faf3f01bSAndreas Gohr        } else $this->doc .= $this->_formatLink($link);
1303410ee62aSAnika Henke    }
13040cecf9d5Sandi
13054826ab45Sandi    /**
13063db95becSAndreas Gohr     * Renders an RSS feed
1307b625487dSandi     *
13080c4c0281SGerrit Uitslag     * @param string $url URL of the feed
13090c4c0281SGerrit Uitslag     * @param array $params Finetuning of the output
13100c4c0281SGerrit Uitslag     *
1311b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1312b625487dSandi     */
1313*faf3f01bSAndreas Gohr    public function rss($url, $params)
1314*faf3f01bSAndreas Gohr    {
1315b625487dSandi        global $lang;
13163db95becSAndreas Gohr        global $conf;
13173db95becSAndreas Gohr
13183db95becSAndreas Gohr        require_once(DOKU_INC . 'inc/FeedParser.php');
13193db95becSAndreas Gohr        $feed = new FeedParser();
132000077af8SAndreas Gohr        $feed->set_feed_url($url);
1321b625487dSandi
1322b625487dSandi        //disable warning while fetching
13233dd5c225SAndreas Gohr        if (!defined('DOKU_E_LEVEL')) {
13243dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
13253dd5c225SAndreas Gohr        }
13263db95becSAndreas Gohr        $rc = $feed->init();
13273dd5c225SAndreas Gohr        if (isset($elvl)) {
13283dd5c225SAndreas Gohr            error_reporting($elvl);
13293dd5c225SAndreas Gohr        }
1330b625487dSandi
133138c6f603SRobin H. Johnson        if ($params['nosort']) $feed->enable_order_by_date(false);
133238c6f603SRobin H. Johnson
13333db95becSAndreas Gohr        //decide on start and end
13343db95becSAndreas Gohr        if ($params['reverse']) {
13353db95becSAndreas Gohr            $mod = -1;
13363db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
13373db95becSAndreas Gohr            $end = $start - ($params['max']);
1338b2a412b0SAndreas Gohr            $end = ($end < -1) ? -1 : $end;
13393db95becSAndreas Gohr        } else {
13403db95becSAndreas Gohr            $mod = 1;
13413db95becSAndreas Gohr            $start = 0;
13423db95becSAndreas Gohr            $end = $feed->get_item_quantity();
1343d91ab76fSMatt Perry            $end = ($end > $params['max']) ? $params['max'] : $end;
13443db95becSAndreas Gohr        }
13453db95becSAndreas Gohr
1346a2d649c4Sandi        $this->doc .= '<ul class="rss">';
13473db95becSAndreas Gohr        if ($rc) {
13483db95becSAndreas Gohr            for ($x = $start; $x != $end; $x += $mod) {
13491bde1582SAndreas Gohr                $item = $feed->get_item($x);
13503db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
135153df38b0SAndreas Gohr
1352d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
135353df38b0SAndreas Gohr                $title = html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8');
135453df38b0SAndreas Gohr
135553df38b0SAndreas Gohr                // support feeds without links
1356d2ea3363SAndreas Gohr                if ($lnkurl) {
135753df38b0SAndreas Gohr                    $this->externallink($item->get_permalink(), $title);
1358d2ea3363SAndreas Gohr                } else {
135953df38b0SAndreas Gohr                    $this->doc .= ' ' . hsc($item->get_title());
1360d2ea3363SAndreas Gohr                }
13613db95becSAndreas Gohr                if ($params['author']) {
13621bde1582SAndreas Gohr                    $author = $item->get_author(0);
1363*faf3f01bSAndreas Gohr                    if ($author instanceof Author) {
13641bde1582SAndreas Gohr                        $name = $author->get_name();
13651bde1582SAndreas Gohr                        if (!$name) $name = $author->get_email();
1366163c2842SPhy                        if ($name) $this->doc .= ' ' . $lang['by'] . ' ' . hsc($name);
13671bde1582SAndreas Gohr                    }
13683db95becSAndreas Gohr                }
13693db95becSAndreas Gohr                if ($params['date']) {
13702e7e0c29SAndreas Gohr                    $this->doc .= ' (' . $item->get_local_date($conf['dformat']) . ')';
13713db95becSAndreas Gohr                }
13721bde1582SAndreas Gohr                if ($params['details']) {
137353df38b0SAndreas Gohr                    $desc = $item->get_description();
137453df38b0SAndreas Gohr                    $desc = strip_tags($desc);
137553df38b0SAndreas Gohr                    $desc = html_entity_decode($desc, ENT_QUOTES, 'UTF-8');
13763db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
137753df38b0SAndreas Gohr                    $this->doc .= hsc($desc);
13783db95becSAndreas Gohr                    $this->doc .= '</div>';
13793db95becSAndreas Gohr                }
13803db95becSAndreas Gohr
13813db95becSAndreas Gohr                $this->doc .= '</div></li>';
1382b625487dSandi            }
1383b625487dSandi        } else {
13843db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1385a2d649c4Sandi            $this->doc .= '<em>' . $lang['rssfailed'] . '</em>';
1386b625487dSandi            $this->externallink($url);
138745e147ccSAndreas Gohr            if ($conf['allowdebug']) {
138845e147ccSAndreas Gohr                $this->doc .= '<!--' . hsc($feed->error) . '-->';
138945e147ccSAndreas Gohr            }
13903db95becSAndreas Gohr            $this->doc .= '</div></li>';
1391b625487dSandi        }
1392a2d649c4Sandi        $this->doc .= '</ul>';
1393b625487dSandi    }
1394b625487dSandi
13953dd5c225SAndreas Gohr    /**
13963dd5c225SAndreas Gohr     * Start a table
13973dd5c225SAndreas Gohr     *
13983dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
13993dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
14003dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
14017d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
14023dd5c225SAndreas Gohr     */
1403*faf3f01bSAndreas Gohr    public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null)
1404*faf3f01bSAndreas Gohr    {
1405b5742cedSPierre Spring        // initialize the row counter used for classes
1406b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1407619736fdSAdrian Lang        $class = 'table';
14080c4c0281SGerrit Uitslag        if ($classes !== null) {
1409*faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
14100c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14110c4c0281SGerrit Uitslag        }
1412619736fdSAdrian Lang        if ($pos !== null) {
141306917fceSMichael Große            $hid = $this->_headerToLink($class, true);
1414*faf3f01bSAndreas Gohr            $data = [];
1415ec57f119SLarsDW223            $data['target'] = 'table';
1416ec57f119SLarsDW223            $data['name'] = '';
1417ec57f119SLarsDW223            $data['hid'] = $hid;
1418ec57f119SLarsDW223            $class .= ' ' . $this->startSectionEdit($pos, $data);
1419619736fdSAdrian Lang        }
1420619736fdSAdrian Lang        $this->doc .= '<div class="' . $class . '"><table class="inline">' .
1421619736fdSAdrian Lang            DOKU_LF;
14220cecf9d5Sandi    }
14230cecf9d5Sandi
14243dd5c225SAndreas Gohr    /**
14253dd5c225SAndreas Gohr     * Close a table
14263dd5c225SAndreas Gohr     *
14273dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
14283dd5c225SAndreas Gohr     */
1429*faf3f01bSAndreas Gohr    public function table_close($pos = null)
1430*faf3f01bSAndreas Gohr    {
1431a8574918SAnika Henke        $this->doc .= '</table></div>' . DOKU_LF;
1432619736fdSAdrian Lang        if ($pos !== null) {
143390df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
14340cecf9d5Sandi        }
1435619736fdSAdrian Lang    }
14360cecf9d5Sandi
14373dd5c225SAndreas Gohr    /**
14383dd5c225SAndreas Gohr     * Open a table header
14393dd5c225SAndreas Gohr     */
1440*faf3f01bSAndreas Gohr    public function tablethead_open()
1441*faf3f01bSAndreas Gohr    {
1442f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB . '<thead>' . DOKU_LF;
1443f05a1cc5SGerrit Uitslag    }
1444f05a1cc5SGerrit Uitslag
14453dd5c225SAndreas Gohr    /**
14463dd5c225SAndreas Gohr     * Close a table header
14473dd5c225SAndreas Gohr     */
1448*faf3f01bSAndreas Gohr    public function tablethead_close()
1449*faf3f01bSAndreas Gohr    {
1450f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB . '</thead>' . DOKU_LF;
1451f05a1cc5SGerrit Uitslag    }
1452f05a1cc5SGerrit Uitslag
14533dd5c225SAndreas Gohr    /**
14545a93f869SAnika Henke     * Open a table body
14555a93f869SAnika Henke     */
1456*faf3f01bSAndreas Gohr    public function tabletbody_open()
1457*faf3f01bSAndreas Gohr    {
14585a93f869SAnika Henke        $this->doc .= DOKU_TAB . '<tbody>' . DOKU_LF;
14595a93f869SAnika Henke    }
14605a93f869SAnika Henke
14615a93f869SAnika Henke    /**
14625a93f869SAnika Henke     * Close a table body
14635a93f869SAnika Henke     */
1464*faf3f01bSAndreas Gohr    public function tabletbody_close()
1465*faf3f01bSAndreas Gohr    {
14665a93f869SAnika Henke        $this->doc .= DOKU_TAB . '</tbody>' . DOKU_LF;
14675a93f869SAnika Henke    }
14685a93f869SAnika Henke
14695a93f869SAnika Henke    /**
1470d2a99739SAndreas Gohr     * Open a table footer
1471d2a99739SAndreas Gohr     */
1472*faf3f01bSAndreas Gohr    public function tabletfoot_open()
1473*faf3f01bSAndreas Gohr    {
147444f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB . '<tfoot>' . DOKU_LF;
1475d2a99739SAndreas Gohr    }
1476d2a99739SAndreas Gohr
1477d2a99739SAndreas Gohr    /**
1478d2a99739SAndreas Gohr     * Close a table footer
1479d2a99739SAndreas Gohr     */
1480*faf3f01bSAndreas Gohr    public function tabletfoot_close()
1481*faf3f01bSAndreas Gohr    {
148244f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB . '</tfoot>' . DOKU_LF;
1483d2a99739SAndreas Gohr    }
1484d2a99739SAndreas Gohr
1485d2a99739SAndreas Gohr    /**
14863dd5c225SAndreas Gohr     * Open a table row
14870c4c0281SGerrit Uitslag     *
14887d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
14893dd5c225SAndreas Gohr     */
1490*faf3f01bSAndreas Gohr    public function tablerow_open($classes = null)
1491*faf3f01bSAndreas Gohr    {
1492b5742cedSPierre Spring        // initialize the cell counter used for classes
1493b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1494b5742cedSPierre Spring        $class = 'row' . $this->_counter['row_counter']++;
14950c4c0281SGerrit Uitslag        if ($classes !== null) {
1496*faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
14970c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14980c4c0281SGerrit Uitslag        }
1499b5742cedSPierre Spring        $this->doc .= DOKU_TAB . '<tr class="' . $class . '">' . DOKU_LF . DOKU_TAB . DOKU_TAB;
15000cecf9d5Sandi    }
15010cecf9d5Sandi
15023dd5c225SAndreas Gohr    /**
15033dd5c225SAndreas Gohr     * Close a table row
15043dd5c225SAndreas Gohr     */
1505*faf3f01bSAndreas Gohr    public function tablerow_close()
1506*faf3f01bSAndreas Gohr    {
1507a2d649c4Sandi        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
15080cecf9d5Sandi    }
15090cecf9d5Sandi
15103dd5c225SAndreas Gohr    /**
15113dd5c225SAndreas Gohr     * Open a table header cell
15123dd5c225SAndreas Gohr     *
15133dd5c225SAndreas Gohr     * @param int $colspan
15143dd5c225SAndreas Gohr     * @param string $align left|center|right
15153dd5c225SAndreas Gohr     * @param int $rowspan
15167d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
15173dd5c225SAndreas Gohr     */
1518*faf3f01bSAndreas Gohr    public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null)
1519*faf3f01bSAndreas Gohr    {
1520b5742cedSPierre Spring        $class = 'class="col' . $this->_counter['cell_counter']++;
15210cecf9d5Sandi        if (!is_null($align)) {
1522b5742cedSPierre Spring            $class .= ' ' . $align . 'align';
15230cecf9d5Sandi        }
15240c4c0281SGerrit Uitslag        if ($classes !== null) {
1525*faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
15260c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
15270c4c0281SGerrit Uitslag        }
1528b5742cedSPierre Spring        $class .= '"';
1529b5742cedSPierre Spring        $this->doc .= '<th ' . $class;
15300cecf9d5Sandi        if ($colspan > 1) {
1531a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1532a2d649c4Sandi            $this->doc .= ' colspan="' . $colspan . '"';
15330cecf9d5Sandi        }
153425b97867Shakan.sandell        if ($rowspan > 1) {
153525b97867Shakan.sandell            $this->doc .= ' rowspan="' . $rowspan . '"';
153625b97867Shakan.sandell        }
1537a2d649c4Sandi        $this->doc .= '>';
15380cecf9d5Sandi    }
15390cecf9d5Sandi
15403dd5c225SAndreas Gohr    /**
15413dd5c225SAndreas Gohr     * Close a table header cell
15423dd5c225SAndreas Gohr     */
1543*faf3f01bSAndreas Gohr    public function tableheader_close()
1544*faf3f01bSAndreas Gohr    {
1545a2d649c4Sandi        $this->doc .= '</th>';
15460cecf9d5Sandi    }
15470cecf9d5Sandi
15483dd5c225SAndreas Gohr    /**
15493dd5c225SAndreas Gohr     * Open a table cell
15503dd5c225SAndreas Gohr     *
15513dd5c225SAndreas Gohr     * @param int $colspan
15523dd5c225SAndreas Gohr     * @param string $align left|center|right
15533dd5c225SAndreas Gohr     * @param int $rowspan
15547d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
15553dd5c225SAndreas Gohr     */
1556*faf3f01bSAndreas Gohr    public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null)
1557*faf3f01bSAndreas Gohr    {
1558b5742cedSPierre Spring        $class = 'class="col' . $this->_counter['cell_counter']++;
15590cecf9d5Sandi        if (!is_null($align)) {
1560b5742cedSPierre Spring            $class .= ' ' . $align . 'align';
15610cecf9d5Sandi        }
15620c4c0281SGerrit Uitslag        if ($classes !== null) {
1563*faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
15640c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
15650c4c0281SGerrit Uitslag        }
1566b5742cedSPierre Spring        $class .= '"';
1567b5742cedSPierre Spring        $this->doc .= '<td ' . $class;
15680cecf9d5Sandi        if ($colspan > 1) {
1569a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1570a2d649c4Sandi            $this->doc .= ' colspan="' . $colspan . '"';
15710cecf9d5Sandi        }
157225b97867Shakan.sandell        if ($rowspan > 1) {
157325b97867Shakan.sandell            $this->doc .= ' rowspan="' . $rowspan . '"';
157425b97867Shakan.sandell        }
1575a2d649c4Sandi        $this->doc .= '>';
15760cecf9d5Sandi    }
15770cecf9d5Sandi
15783dd5c225SAndreas Gohr    /**
15793dd5c225SAndreas Gohr     * Close a table cell
15803dd5c225SAndreas Gohr     */
1581*faf3f01bSAndreas Gohr    public function tablecell_close()
1582*faf3f01bSAndreas Gohr    {
1583a2d649c4Sandi        $this->doc .= '</td>';
15840cecf9d5Sandi    }
15850cecf9d5Sandi
1586cea664bdSLarsDW223    /**
1587cea664bdSLarsDW223     * Returns the current header level.
1588cea664bdSLarsDW223     * (required e.g. by the filelist plugin)
1589cea664bdSLarsDW223     *
1590cea664bdSLarsDW223     * @return int The current header level
1591cea664bdSLarsDW223     */
1592*faf3f01bSAndreas Gohr    public function getLastlevel()
1593*faf3f01bSAndreas Gohr    {
1594cea664bdSLarsDW223        return $this->lastlevel;
1595cea664bdSLarsDW223    }
1596cea664bdSLarsDW223
15973dd5c225SAndreas Gohr    #region Utility functions
15980cecf9d5Sandi
1599ba11bd29Sandi    /**
16003fd0b676Sandi     * Build a link
16013fd0b676Sandi     *
16023fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1603ba11bd29Sandi     *
16040c4c0281SGerrit Uitslag     * @param array $link attributes of a link
16050c4c0281SGerrit Uitslag     * @return string
16060c4c0281SGerrit Uitslag     *
1607ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1608ba11bd29Sandi     */
1609*faf3f01bSAndreas Gohr    public function _formatLink($link)
1610*faf3f01bSAndreas Gohr    {
1611ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1612ba11bd29Sandi        if (substr($link['url'], 0, 7) != 'mailto:') {
1613ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1614ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1615ba11bd29Sandi        }
1616ba11bd29Sandi        //remove double encodings in titles
1617ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1618ba11bd29Sandi
1619453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1620453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1621*faf3f01bSAndreas Gohr        $link['url'] = strtr($link['url'], ['>' => '%3E', '<' => '%3C', '"' => '%22']);
1622*faf3f01bSAndreas Gohr        $link['title'] = strtr($link['title'], ['>' => '&gt;', '<' => '&lt;', '"' => '&quot;']);
1623453493f2SAndreas Gohr
1624ba11bd29Sandi        $ret = '';
1625ba11bd29Sandi        $ret .= $link['pre'];
1626ba11bd29Sandi        $ret .= '<a href="' . $link['url'] . '"';
1627bb4866bdSchris        if (!empty($link['class'])) $ret .= ' class="' . $link['class'] . '"';
1628bb4866bdSchris        if (!empty($link['target'])) $ret .= ' target="' . $link['target'] . '"';
1629bb4866bdSchris        if (!empty($link['title'])) $ret .= ' title="' . $link['title'] . '"';
1630bb4866bdSchris        if (!empty($link['style'])) $ret .= ' style="' . $link['style'] . '"';
1631914045f3SAndreas Gohr        if (!empty($link['rel'])) $ret .= ' rel="' . trim($link['rel']) . '"';
1632bb4866bdSchris        if (!empty($link['more'])) $ret .= ' ' . $link['more'];
1633ba11bd29Sandi        $ret .= '>';
1634ba11bd29Sandi        $ret .= $link['name'];
1635ba11bd29Sandi        $ret .= '</a>';
1636ba11bd29Sandi        $ret .= $link['suf'];
1637ba11bd29Sandi        return $ret;
1638ba11bd29Sandi    }
1639ba11bd29Sandi
1640ba11bd29Sandi    /**
16413fd0b676Sandi     * Renders internal and external media
16423fd0b676Sandi     *
16433dd5c225SAndreas Gohr     * @param string $src media ID
16443dd5c225SAndreas Gohr     * @param string $title descriptive text
16453dd5c225SAndreas Gohr     * @param string $align left|center|right
16463dd5c225SAndreas Gohr     * @param int $width width of media in pixel
16473dd5c225SAndreas Gohr     * @param int $height height of media in pixel
16483dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
16493dd5c225SAndreas Gohr     * @param bool $render should the media be embedded inline or just linked
16503dd5c225SAndreas Gohr     * @return string
1651*faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
16523fd0b676Sandi     */
1653de369923SAndreas Gohr    public function _media($src, $title = null, $align = null, $width = null,
1654*faf3f01bSAndreas Gohr                           $height = null, $cache = null, $render = true)
1655*faf3f01bSAndreas Gohr    {
16563fd0b676Sandi
16573fd0b676Sandi        $ret = '';
16583fd0b676Sandi
1659*faf3f01bSAndreas Gohr        [$ext, $mime] = mimetype($src);
16603fd0b676Sandi        if (substr($mime, 0, 5) == 'image') {
1661b739ff0fSPierre Spring            // first get the $title
1662b739ff0fSPierre Spring            if (!is_null($title)) {
1663b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1664b739ff0fSPierre Spring            } elseif ($ext == 'jpg' || $ext == 'jpeg') {
1665b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1666b739ff0fSPierre Spring                require_once(DOKU_INC . 'inc/JpegMeta.php');
166767f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1668*faf3f01bSAndreas Gohr                $cap = $jpeg->getTitle();
16693dd5c225SAndreas Gohr                if (!empty($cap)) {
1670b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1671b739ff0fSPierre Spring                }
1672b739ff0fSPierre Spring            }
1673b739ff0fSPierre Spring            if (!$render) {
1674b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1675b739ff0fSPierre Spring                // return the title of the picture
1676768be5a3SPhy                if ($title === null || $title === "") {
1677b739ff0fSPierre Spring                    // just show the sourcename
1678*faf3f01bSAndreas Gohr                    $title = $this->_xmlEntities(PhpString::basename(noNS($src)));
1679b739ff0fSPierre Spring                }
1680b739ff0fSPierre Spring                return $title;
1681b739ff0fSPierre Spring            }
16823fd0b676Sandi            //add image tag
168364159a61SAndreas Gohr            $ret .= '<img src="' . ml(
168464159a61SAndreas Gohr                    $src,
1685*faf3f01bSAndreas Gohr                    [
1686*faf3f01bSAndreas Gohr                        'w' => $width,
1687*faf3f01bSAndreas Gohr                        'h' => $height,
168864159a61SAndreas Gohr                        'cache' => $cache,
168964159a61SAndreas Gohr                        'rev' => $this->_getLastMediaRevisionAt($src)
1690*faf3f01bSAndreas Gohr                    ]
169164159a61SAndreas Gohr                ) . '"';
16923fd0b676Sandi            $ret .= ' class="media' . $align . '"';
16934732b197SAndreas Gohr            $ret .= ' loading="lazy"';
16943fd0b676Sandi
1695b739ff0fSPierre Spring            if ($title) {
1696b739ff0fSPierre Spring                $ret .= ' title="' . $title . '"';
1697b739ff0fSPierre Spring                $ret .= ' alt="' . $title . '"';
16983fd0b676Sandi            } else {
16993fd0b676Sandi                $ret .= ' alt=""';
17003fd0b676Sandi            }
17013fd0b676Sandi
17023fd0b676Sandi            if (!is_null($width))
17033fd0b676Sandi                $ret .= ' width="' . $this->_xmlEntities($width) . '"';
17043fd0b676Sandi
17053fd0b676Sandi            if (!is_null($height))
17063fd0b676Sandi                $ret .= ' height="' . $this->_xmlEntities($height) . '"';
17073fd0b676Sandi
17083fd0b676Sandi            $ret .= ' />';
17093fd0b676Sandi
171017954bb5SAnika Henke        } elseif (media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
17112a2a2ba2SAnika Henke            // first get the $title
1712*faf3f01bSAndreas Gohr            $title = is_null($title) ? false : $title;
17132a2a2ba2SAnika Henke            if (!$render) {
171417954bb5SAnika Henke                // if the file is not supposed to be rendered
171517954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
1716*faf3f01bSAndreas Gohr                return $this->_xmlEntities($title ?: PhpString::basename(noNS($src)));
17172a2a2ba2SAnika Henke            }
17182a2a2ba2SAnika Henke
1719*faf3f01bSAndreas Gohr            $att = [];
17202a2a2ba2SAnika Henke            $att['class'] = "media$align";
172117954bb5SAnika Henke            if ($title) {
172217954bb5SAnika Henke                $att['title'] = $title;
172317954bb5SAnika Henke            }
17242a2a2ba2SAnika Henke
172517954bb5SAnika Henke            if (media_supportedav($mime, 'video')) {
172617954bb5SAnika Henke                //add video
172779e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1728b44a5dceSAnika Henke            }
172917954bb5SAnika Henke            if (media_supportedav($mime, 'audio')) {
1730b44a5dceSAnika Henke                //add audio
1731b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
173217954bb5SAnika Henke            }
1733b44a5dceSAnika Henke
17343fd0b676Sandi        } elseif ($mime == 'application/x-shockwave-flash') {
17351c882ba8SAndreas Gohr            if (!$render) {
17361c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
17371c882ba8SAndreas Gohr                // return the title of the flash
17381c882ba8SAndreas Gohr                if (!$title) {
17391c882ba8SAndreas Gohr                    // just show the sourcename
1740*faf3f01bSAndreas Gohr                    $title = PhpString::basename(noNS($src));
17411c882ba8SAndreas Gohr                }
174207bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
17431c882ba8SAndreas Gohr            }
17441c882ba8SAndreas Gohr
1745*faf3f01bSAndreas Gohr            $att = [];
174607bf32b2SAndreas Gohr            $att['class'] = "media$align";
174707bf32b2SAndreas Gohr            if ($align == 'right') $att['align'] = 'right';
174807bf32b2SAndreas Gohr            if ($align == 'left') $att['align'] = 'left';
17493dd5c225SAndreas Gohr            $ret .= html_flashobject(
1750*faf3f01bSAndreas Gohr                ml($src, ['cache' => $cache], true, '&'), $width, $height,
1751*faf3f01bSAndreas Gohr                ['quality' => 'high'],
175207bf32b2SAndreas Gohr                null,
175307bf32b2SAndreas Gohr                $att,
17543dd5c225SAndreas Gohr                $this->_xmlEntities($title)
17553dd5c225SAndreas Gohr            );
17560f428d7dSAndreas Gohr        } elseif ($title) {
17573fd0b676Sandi            // well at least we have a title to display
17583fd0b676Sandi            $ret .= $this->_xmlEntities($title);
17593fd0b676Sandi        } else {
17605291ca3aSAndreas Gohr            // just show the sourcename
1761*faf3f01bSAndreas Gohr            $ret .= $this->_xmlEntities(PhpString::basename(noNS($src)));
17623fd0b676Sandi        }
17633fd0b676Sandi
17643fd0b676Sandi        return $ret;
17653fd0b676Sandi    }
17663fd0b676Sandi
17673dd5c225SAndreas Gohr    /**
17683dd5c225SAndreas Gohr     * Escape string for output
17693dd5c225SAndreas Gohr     *
17703dd5c225SAndreas Gohr     * @param $string
17713dd5c225SAndreas Gohr     * @return string
17723dd5c225SAndreas Gohr     */
1773*faf3f01bSAndreas Gohr    public function _xmlEntities($string)
1774*faf3f01bSAndreas Gohr    {
1775f7711f2bSAndreas Gohr        return hsc($string);
17760cecf9d5Sandi    }
17770cecf9d5Sandi
1778de369923SAndreas Gohr
1779af587fa8Sandi    /**
17803fd0b676Sandi     * Construct a title and handle images in titles
17813fd0b676Sandi     *
17823dd5c225SAndreas Gohr     * @param string|array $title either string title or media array
17833dd5c225SAndreas Gohr     * @param string $default default title if nothing else is found
17843dd5c225SAndreas Gohr     * @param bool $isImage will be set to true if it's a media file
17853dd5c225SAndreas Gohr     * @param null|string $id linked page id (used to extract title from first heading)
17863dd5c225SAndreas Gohr     * @param string $linktype content|navigation
17873dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
1788*faf3f01bSAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
17893fd0b676Sandi     */
1790*faf3f01bSAndreas Gohr    public function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content')
1791*faf3f01bSAndreas Gohr    {
179244881bd0Shenning.noren        $isImage = false;
179329657f9eSAndreas Gohr        if (is_array($title)) {
179429657f9eSAndreas Gohr            $isImage = true;
179529657f9eSAndreas Gohr            return $this->_imageTitle($title);
179629657f9eSAndreas Gohr        } elseif (is_null($title) || trim($title) == '') {
1797fe9ec250SChris Smith            if (useHeading($linktype) && $id) {
179867c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1799f515db7fSAndreas Gohr                if (!blank($heading)) {
1800433bef32Sandi                    return $this->_xmlEntities($heading);
1801bb0a59d4Sjan                }
1802bb0a59d4Sjan            }
1803433bef32Sandi            return $this->_xmlEntities($default);
180468c26e6dSMichael Klier        } else {
180568c26e6dSMichael Klier            return $this->_xmlEntities($title);
18060cecf9d5Sandi        }
18070cecf9d5Sandi    }
18080cecf9d5Sandi
18090cecf9d5Sandi    /**
18103dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
18113fd0b676Sandi     *
1812e0c26282SGerrit Uitslag     * @param array $img
18133dd5c225SAndreas Gohr     * @return string HTML img tag or similar
1814*faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
18150cecf9d5Sandi     */
1816*faf3f01bSAndreas Gohr    public function _imageTitle($img)
1817*faf3f01bSAndreas Gohr    {
1818d9baf1a7SKazutaka Miyasaka        global $ID;
1819d9baf1a7SKazutaka Miyasaka
1820d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1821d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
1822*faf3f01bSAndreas Gohr        [$img['src']] = explode('#', $img['src'], 2);
1823d9baf1a7SKazutaka Miyasaka        if ($img['type'] == 'internalmedia') {
18248c6be208SAndreas Gohr            $img['src'] = (new MediaResolver($ID))->resolveId($img['src'], $this->date_at, true);
1825d9baf1a7SKazutaka Miyasaka        }
1826d9baf1a7SKazutaka Miyasaka
18273dd5c225SAndreas Gohr        return $this->_media(
18283dd5c225SAndreas Gohr            $img['src'],
18294826ab45Sandi            $img['title'],
18304826ab45Sandi            $img['align'],
18314826ab45Sandi            $img['width'],
18324826ab45Sandi            $img['height'],
18333dd5c225SAndreas Gohr            $img['cache']
18343dd5c225SAndreas Gohr        );
18350cecf9d5Sandi    }
1836b739ff0fSPierre Spring
1837b739ff0fSPierre Spring    /**
18383dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
18393dd5c225SAndreas Gohr     *
18403dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1841b739ff0fSPierre Spring     *
18423dd5c225SAndreas Gohr     * @param string $src media ID
18433dd5c225SAndreas Gohr     * @param string $title descriptive text
18443dd5c225SAndreas Gohr     * @param string $align left|center|right
18453dd5c225SAndreas Gohr     * @param int $width width of media in pixel
18463dd5c225SAndreas Gohr     * @param int $height height of media in pixel
18473dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
18483dd5c225SAndreas Gohr     * @param bool $render should the media be embedded inline or just linked
18493dd5c225SAndreas Gohr     * @return array associative array with link config
1850*faf3f01bSAndreas Gohr     * @author   Pierre Spring <pierre.spring@liip.ch>
1851b739ff0fSPierre Spring     */
1852*faf3f01bSAndreas Gohr    public function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render)
1853*faf3f01bSAndreas Gohr    {
1854b739ff0fSPierre Spring        global $conf;
1855b739ff0fSPierre Spring
1856*faf3f01bSAndreas Gohr        $link = [];
1857b739ff0fSPierre Spring        $link['class'] = 'media';
1858b739ff0fSPierre Spring        $link['style'] = '';
1859b739ff0fSPierre Spring        $link['pre'] = '';
1860b739ff0fSPierre Spring        $link['suf'] = '';
1861b739ff0fSPierre Spring        $link['more'] = '';
1862b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1863bc3d2252SAndreas Gohr        if ($conf['target']['media']) $link['rel'] = 'noopener';
1864b739ff0fSPierre Spring        $link['title'] = $this->_xmlEntities($src);
1865b739ff0fSPierre Spring        $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1866b739ff0fSPierre Spring
1867b739ff0fSPierre Spring        return $link;
1868b739ff0fSPierre Spring    }
186991459163SAnika Henke
18702a2a2ba2SAnika Henke    /**
18712a2a2ba2SAnika Henke     * Embed video(s) in HTML
18722a2a2ba2SAnika Henke     *
18732a2a2ba2SAnika Henke     * @param string $src - ID of video to embed
18742a2a2ba2SAnika Henke     * @param int $width - width of the video in pixels
18752a2a2ba2SAnika Henke     * @param int $height - height of the video in pixels
18762a2a2ba2SAnika Henke     * @param array $atts - additional attributes for the <video> tag
1877f50634f0SAnika Henke     * @return string
1878*faf3f01bSAndreas Gohr     * @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
1879*faf3f01bSAndreas Gohr     *
1880*faf3f01bSAndreas Gohr     * @author Anika Henke <anika@selfthinker.org>
18812a2a2ba2SAnika Henke     */
1882*faf3f01bSAndreas Gohr    public function _video($src, $width, $height, $atts = null)
1883*faf3f01bSAndreas Gohr    {
18842a2a2ba2SAnika Henke        // prepare width and height
1885*faf3f01bSAndreas Gohr        if (is_null($atts)) $atts = [];
18862a2a2ba2SAnika Henke        $atts['width'] = (int)$width;
18872a2a2ba2SAnika Henke        $atts['height'] = (int)$height;
18882a2a2ba2SAnika Henke        if (!$atts['width']) $atts['width'] = 320;
18892a2a2ba2SAnika Henke        if (!$atts['height']) $atts['height'] = 240;
18902a2a2ba2SAnika Henke
1891410ee62aSAnika Henke        $posterUrl = '';
1892*faf3f01bSAndreas Gohr        $files = [];
1893*faf3f01bSAndreas Gohr        $tracks = [];
1894410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1895410ee62aSAnika Henke
1896410ee62aSAnika Henke        if ($isExternal) {
1897410ee62aSAnika Henke            // take direct source for external files
1898*faf3f01bSAndreas Gohr            [, $srcMime] = mimetype($src);
1899410ee62aSAnika Henke            $files[$srcMime] = $src;
1900410ee62aSAnika Henke        } else {
19013d7a9e0aSAnika Henke            // prepare alternative formats
1902*faf3f01bSAndreas Gohr            $extensions = ['webm', 'ogv', 'mp4'];
1903410ee62aSAnika Henke            $files = media_alternativefiles($src, $extensions);
1904*faf3f01bSAndreas Gohr            $poster = media_alternativefiles($src, ['jpg', 'png']);
19050877a1f1SSchplurtz le Déboulonné            $tracks = media_trackfiles($src);
190699f943f6SAnika Henke            if (!empty($poster)) {
19072d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
190899f943f6SAnika Henke            }
1909410ee62aSAnika Henke        }
19102a2a2ba2SAnika Henke
1911f50634f0SAnika Henke        $out = '';
191279e53fe5SAnika Henke        // open video tag
1913f50634f0SAnika Henke        $out .= '<video ' . buildAttributes($atts) . ' controls="controls"';
19143641199aSAnika Henke        if ($posterUrl) $out .= ' poster="' . hsc($posterUrl) . '"';
1915f50634f0SAnika Henke        $out .= '>' . NL;
19163641199aSAnika Henke        $fallback = '';
191779e53fe5SAnika Henke
191879e53fe5SAnika Henke        // output source for each alternative video format
1919410ee62aSAnika Henke        foreach ($files as $mime => $file) {
1920410ee62aSAnika Henke            if ($isExternal) {
1921410ee62aSAnika Henke                $url = $file;
1922410ee62aSAnika Henke                $linkType = 'externalmedia';
1923410ee62aSAnika Henke            } else {
19242d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1925410ee62aSAnika Henke                $linkType = 'internalmedia';
1926410ee62aSAnika Henke            }
1927*faf3f01bSAndreas Gohr            $title = empty($atts['title'])
1928*faf3f01bSAndreas Gohr                ? $this->_xmlEntities(PhpString::basename(noNS($file)))
1929*faf3f01bSAndreas Gohr                : $atts['title'];
19303d7a9e0aSAnika Henke
1931f50634f0SAnika Henke            $out .= '<source src="' . hsc($url) . '" type="' . $mime . '" />' . NL;
193279e53fe5SAnika Henke            // alternative content (just a link to the file)
193364159a61SAndreas Gohr            $fallback .= $this->$linkType(
193464159a61SAndreas Gohr                $file,
193564159a61SAndreas Gohr                $title,
193664159a61SAndreas Gohr                null,
193764159a61SAndreas Gohr                null,
193864159a61SAndreas Gohr                null,
193964159a61SAndreas Gohr                $cache = null,
194064159a61SAndreas Gohr                $linking = 'linkonly',
194164159a61SAndreas Gohr                $return = true
194264159a61SAndreas Gohr            );
19433d7a9e0aSAnika Henke        }
19442a2a2ba2SAnika Henke
19450877a1f1SSchplurtz le Déboulonné        // output each track if any
19460877a1f1SSchplurtz le Déboulonné        foreach ($tracks as $trackid => $info) {
1947*faf3f01bSAndreas Gohr            [$kind, $srclang] = array_map('hsc', $info);
194823c61bbeSSchplurtz le Déboulonné            $out .= "<track kind=\"$kind\" srclang=\"$srclang\" ";
194923c61bbeSSchplurtz le Déboulonné            $out .= "label=\"$srclang\" ";
19500877a1f1SSchplurtz le Déboulonné            $out .= 'src="' . ml($trackid, '', true) . '">' . NL;
19510877a1f1SSchplurtz le Déboulonné        }
19520877a1f1SSchplurtz le Déboulonné
19532a2a2ba2SAnika Henke        // finish
19543641199aSAnika Henke        $out .= $fallback;
1955f50634f0SAnika Henke        $out .= '</video>' . NL;
1956f50634f0SAnika Henke        return $out;
19572a2a2ba2SAnika Henke    }
19582a2a2ba2SAnika Henke
1959b44a5dceSAnika Henke    /**
1960b44a5dceSAnika Henke     * Embed audio in HTML
1961b44a5dceSAnika Henke     *
1962b44a5dceSAnika Henke     * @param string $src - ID of audio to embed
19636d4af72aSAnika Henke     * @param array $atts - additional attributes for the <audio> tag
1964f50634f0SAnika Henke     * @return string
1965*faf3f01bSAndreas Gohr     * @author Anika Henke <anika@selfthinker.org>
1966*faf3f01bSAndreas Gohr     *
1967b44a5dceSAnika Henke     */
1968*faf3f01bSAndreas Gohr    public function _audio($src, $atts = [])
1969*faf3f01bSAndreas Gohr    {
1970*faf3f01bSAndreas Gohr        $files = [];
1971410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1972b44a5dceSAnika Henke
1973410ee62aSAnika Henke        if ($isExternal) {
1974410ee62aSAnika Henke            // take direct source for external files
1975*faf3f01bSAndreas Gohr            [, $srcMime] = mimetype($src);
1976410ee62aSAnika Henke            $files[$srcMime] = $src;
1977410ee62aSAnika Henke        } else {
1978b44a5dceSAnika Henke            // prepare alternative formats
1979*faf3f01bSAndreas Gohr            $extensions = ['ogg', 'mp3', 'wav'];
1980410ee62aSAnika Henke            $files = media_alternativefiles($src, $extensions);
1981410ee62aSAnika Henke        }
1982b44a5dceSAnika Henke
1983f50634f0SAnika Henke        $out = '';
1984b44a5dceSAnika Henke        // open audio tag
1985f50634f0SAnika Henke        $out .= '<audio ' . buildAttributes($atts) . ' controls="controls">' . NL;
19863641199aSAnika Henke        $fallback = '';
1987b44a5dceSAnika Henke
1988b44a5dceSAnika Henke        // output source for each alternative audio format
1989410ee62aSAnika Henke        foreach ($files as $mime => $file) {
1990410ee62aSAnika Henke            if ($isExternal) {
1991410ee62aSAnika Henke                $url = $file;
1992410ee62aSAnika Henke                $linkType = 'externalmedia';
1993410ee62aSAnika Henke            } else {
19942d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1995410ee62aSAnika Henke                $linkType = 'internalmedia';
1996410ee62aSAnika Henke            }
1997*faf3f01bSAndreas Gohr            $title = $atts['title'] ?: $this->_xmlEntities(PhpString::basename(noNS($file)));
1998b44a5dceSAnika Henke
1999f50634f0SAnika Henke            $out .= '<source src="' . hsc($url) . '" type="' . $mime . '" />' . NL;
2000b44a5dceSAnika Henke            // alternative content (just a link to the file)
200164159a61SAndreas Gohr            $fallback .= $this->$linkType(
200264159a61SAndreas Gohr                $file,
200364159a61SAndreas Gohr                $title,
200464159a61SAndreas Gohr                null,
200564159a61SAndreas Gohr                null,
200664159a61SAndreas Gohr                null,
200764159a61SAndreas Gohr                $cache = null,
200864159a61SAndreas Gohr                $linking = 'linkonly',
200964159a61SAndreas Gohr                $return = true
201064159a61SAndreas Gohr            );
2011b44a5dceSAnika Henke        }
2012b44a5dceSAnika Henke
2013b44a5dceSAnika Henke        // finish
20143641199aSAnika Henke        $out .= $fallback;
2015f50634f0SAnika Henke        $out .= '</audio>' . NL;
2016f50634f0SAnika Henke        return $out;
2017b44a5dceSAnika Henke    }
2018b44a5dceSAnika Henke
20195c2eed9aSlisps    /**
202052dc5eadSlisps     * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
20215c2eed9aSlisps     * which returns an existing media revision less or equal to rev or date_at
20225c2eed9aSlisps     *
20235c2eed9aSlisps     * @param string $media_id
20245c2eed9aSlisps     * @access protected
20255c2eed9aSlisps     * @return string revision ('' for current)
2026*faf3f01bSAndreas Gohr     * @author lisps
20275c2eed9aSlisps     */
2028*faf3f01bSAndreas Gohr    protected function _getLastMediaRevisionAt($media_id)
2029*faf3f01bSAndreas Gohr    {
203052dc5eadSlisps        if (!$this->date_at || media_isexternal($media_id)) return '';
2031252acce3SSatoshi Sahara        $changelog = new MediaChangeLog($media_id);
2032252acce3SSatoshi Sahara        return $changelog->getLastRevisionAt($this->date_at);
20335c2eed9aSlisps    }
20345c2eed9aSlisps
20353dd5c225SAndreas Gohr    #endregion
20360cecf9d5Sandi}
20370cecf9d5Sandi
2038e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
2039