xref: /dokuwiki/inc/parser/xhtml.php (revision 749bc7f13d5fc213945d96ad93feaab8f74a1e03)
10cecf9d5Sandi<?php
20c3a5702SAndreas Gohr
30c3a5702SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog;
42cd6cc0aSAndreas Gohruse dokuwiki\File\MediaResolver;
52cd6cc0aSAndreas Gohruse dokuwiki\File\PageResolver;
6faf3f01bSAndreas Gohruse dokuwiki\Utf8\PhpString;
7faf3f01bSAndreas 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 */
18faf3f01bSAndreas Gohrclass Doku_Renderer_xhtml extends Doku_Renderer
19faf3f01bSAndreas Gohr{
203dd5c225SAndreas Gohr    /** @var array store the table of contents */
21faf3f01bSAndreas Gohr    public $toc = [];
220cecf9d5Sandi
233dd5c225SAndreas Gohr    /** @var array A stack of section edit data */
24faf3f01bSAndreas 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! */
30faf3f01bSAndreas Gohr    protected $footnotes = [];
317764a90aSandi
323dd5c225SAndreas Gohr    /** @var int current section level */
333dd5c225SAndreas Gohr    protected $lastlevel = 0;
343dd5c225SAndreas Gohr    /** @var array section node tracker */
35faf3f01bSAndreas 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. */
41faf3f01bSAndreas 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 */
47faf3f01bSAndreas 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     */
68faf3f01bSAndreas Gohr    public function startSectionEdit($start, $data)
69faf3f01bSAndreas 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)
7595078f23SAndreas Gohr                ),
7695078f23SAndreas Gohr                -1
77ac025fdfSAndreas Gohr            );
78ac025fdfSAndreas Gohr
79ac025fdfSAndreas Gohr            // @deprecated 2018-04-14, backward compatibility
80ac025fdfSAndreas Gohr            $args = func_get_args();
81faf3f01bSAndreas Gohr            $data = [];
82ac025fdfSAndreas Gohr            if (isset($args[1])) $data['target'] = $args[1];
83ac025fdfSAndreas Gohr            if (isset($args[2])) $data['name'] = $args[2];
84ac025fdfSAndreas Gohr            if (isset($args[3])) $data['hid'] = $args[3];
85ec57f119SLarsDW223        }
86ec57f119SLarsDW223        $data['secid'] = ++$this->lastsecid;
87ec57f119SLarsDW223        $data['start'] = $start;
88ec57f119SLarsDW223        $this->sectionedits[] = $data;
89ec57f119SLarsDW223        return 'sectionedit' . $data['secid'];
9090df9a4dSAdrian Lang    }
9190df9a4dSAdrian Lang
9290df9a4dSAdrian Lang    /**
9390df9a4dSAdrian Lang     * Finish an edit section range
9490df9a4dSAdrian Lang     *
9542ea7f44SGerrit Uitslag     * @param int $end The byte position for the edit end; null for the rest of the page
9642ea7f44SGerrit Uitslag     *
9790df9a4dSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
9890df9a4dSAdrian Lang     */
99faf3f01bSAndreas Gohr    public function finishSectionEdit($end = null, $hid = null)
100faf3f01bSAndreas Gohr    {
101ad43fdbfSasivery        if (count($this->sectionedits) == 0) {
1020d9f02ecSasivery            return;
1030d9f02ecSasivery        }
104ec57f119SLarsDW223        $data = array_pop($this->sectionedits);
105ec57f119SLarsDW223        if (!is_null($end) && $end <= $data['start']) {
10600c13053SAdrian Lang            return;
10700c13053SAdrian Lang        }
1082571786cSLarsDW223        if (!is_null($hid)) {
109ec57f119SLarsDW223            $data['hid'] .= $hid;
1102571786cSLarsDW223        }
111ec57f119SLarsDW223        $data['range'] = $data['start'] . '-' . (is_null($end) ? '' : $end);
112ec57f119SLarsDW223        unset($data['start']);
113faf3f01bSAndreas Gohr        $this->doc .= '<!-- EDIT' . hsc(json_encode($data, JSON_THROW_ON_ERROR)) . ' -->';
11490df9a4dSAdrian Lang    }
11590df9a4dSAdrian Lang
1163dd5c225SAndreas Gohr    /**
1173dd5c225SAndreas Gohr     * Returns the format produced by this renderer.
1183dd5c225SAndreas Gohr     *
1193dd5c225SAndreas Gohr     * @return string always 'xhtml'
1203dd5c225SAndreas Gohr     */
121faf3f01bSAndreas Gohr    public function getFormat()
122faf3f01bSAndreas Gohr    {
1235f70445dSAndreas Gohr        return 'xhtml';
1245f70445dSAndreas Gohr    }
1255f70445dSAndreas Gohr
1263dd5c225SAndreas Gohr    /**
1273dd5c225SAndreas Gohr     * Initialize the document
1283dd5c225SAndreas Gohr     */
129faf3f01bSAndreas Gohr    public function document_start()
130faf3f01bSAndreas Gohr    {
131c5a8fd96SAndreas Gohr        //reset some internals
132faf3f01bSAndreas Gohr        $this->toc = [];
1330cecf9d5Sandi    }
1340cecf9d5Sandi
1353dd5c225SAndreas Gohr    /**
1363dd5c225SAndreas Gohr     * Finalize the document
1373dd5c225SAndreas Gohr     */
138faf3f01bSAndreas Gohr    public function document_end()
139faf3f01bSAndreas Gohr    {
14090df9a4dSAdrian Lang        // Finish open section edits.
141faf3f01bSAndreas Gohr        while ($this->sectionedits !== []) {
142ec57f119SLarsDW223            if ($this->sectionedits[count($this->sectionedits) - 1]['start'] <= 1) {
14390df9a4dSAdrian Lang                // If there is only one section, do not write a section edit
14490df9a4dSAdrian Lang                // marker.
14590df9a4dSAdrian Lang                array_pop($this->sectionedits);
14690df9a4dSAdrian Lang            } else {
147d9e36cbeSAdrian Lang                $this->finishSectionEdit();
14890df9a4dSAdrian Lang            }
14990df9a4dSAdrian Lang        }
15090df9a4dSAdrian Lang
151faf3f01bSAndreas Gohr        if ($this->footnotes !== []) {
152a2d649c4Sandi            $this->doc .= '<div class="footnotes">' . DOKU_LF;
153d74aace9Schris
15416ec3e37SAndreas Gohr            foreach ($this->footnotes as $id => $footnote) {
155d74aace9Schris                // check its not a placeholder that indicates actual footnote text is elsewhere
156d74aace9Schris                if (substr($footnote, 0, 5) != "@@FNT") {
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     */
201faf3f01bSAndreas Gohr    public function toc_additem($id, $text, $level)
202faf3f01bSAndreas 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     */
220faf3f01bSAndreas Gohr    public function header($text, $level, $pos, $returnonly = false)
221faf3f01bSAndreas 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
24095078f23SAndreas Gohr        if (
24195078f23SAndreas Gohr            $level <= $conf['maxseclevel'] &&
242faf3f01bSAndreas Gohr            $this->sectionedits !== [] &&
243ec57f119SLarsDW223            $this->sectionedits[count($this->sectionedits) - 1]['target'] === 'section'
2443dd5c225SAndreas Gohr        ) {
2456c1f778cSAdrian Lang            $this->finishSectionEdit($pos - 1);
24690df9a4dSAdrian Lang        }
24790df9a4dSAdrian Lang
248e3c00e6eSIain Hallam        // build the header
249e3c00e6eSIain Hallam        $header = DOKU_LF . '<h' . $level;
25090df9a4dSAdrian Lang        if ($level <= $conf['maxseclevel']) {
251faf3f01bSAndreas Gohr            $data = [];
252ec57f119SLarsDW223            $data['target'] = 'section';
253ec57f119SLarsDW223            $data['name'] = $text;
254ec57f119SLarsDW223            $data['hid'] = $hid;
255ec57f119SLarsDW223            $data['codeblockOffset'] = $this->_codeblock;
256e3c00e6eSIain Hallam            $header .= ' class="' . $this->startSectionEdit($pos, $data) . '"';
25790df9a4dSAdrian Lang        }
258e3c00e6eSIain Hallam        $header .= ' id="' . $hid . '">';
259e3c00e6eSIain Hallam        $header .= $this->_xmlEntities($text);
260e3c00e6eSIain Hallam        $header .= "</h$level>" . DOKU_LF;
261e3c00e6eSIain Hallam
262e3c00e6eSIain Hallam        if ($returnonly) {
263e3c00e6eSIain Hallam            return $header;
264e3c00e6eSIain Hallam        } else {
265e3c00e6eSIain Hallam            $this->doc .= $header;
266e3c00e6eSIain Hallam        }
2670cecf9d5Sandi    }
2680cecf9d5Sandi
2693dd5c225SAndreas Gohr    /**
2703dd5c225SAndreas Gohr     * Open a new section
2713dd5c225SAndreas Gohr     *
2723dd5c225SAndreas Gohr     * @param int $level section level (as determined by the previous header)
2733dd5c225SAndreas Gohr     */
274faf3f01bSAndreas Gohr    public function section_open($level)
275faf3f01bSAndreas Gohr    {
2769864e7b1SAdrian Lang        $this->doc .= '<div class="level' . $level . '">' . DOKU_LF;
2770cecf9d5Sandi    }
2780cecf9d5Sandi
2793dd5c225SAndreas Gohr    /**
2803dd5c225SAndreas Gohr     * Close the current section
2813dd5c225SAndreas Gohr     */
282faf3f01bSAndreas Gohr    public function section_close()
283faf3f01bSAndreas Gohr    {
284a2d649c4Sandi        $this->doc .= DOKU_LF . '</div>' . DOKU_LF;
2850cecf9d5Sandi    }
2860cecf9d5Sandi
2873dd5c225SAndreas Gohr    /**
2883dd5c225SAndreas Gohr     * Render plain text data
2893dd5c225SAndreas Gohr     *
2903dd5c225SAndreas Gohr     * @param $text
2913dd5c225SAndreas Gohr     */
292faf3f01bSAndreas Gohr    public function cdata($text)
293faf3f01bSAndreas Gohr    {
294a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
2950cecf9d5Sandi    }
2960cecf9d5Sandi
2973dd5c225SAndreas Gohr    /**
2983dd5c225SAndreas Gohr     * Open a paragraph
2993dd5c225SAndreas Gohr     */
300faf3f01bSAndreas Gohr    public function p_open()
301faf3f01bSAndreas Gohr    {
30259869a4bSAnika Henke        $this->doc .= DOKU_LF . '<p>' . DOKU_LF;
3030cecf9d5Sandi    }
3040cecf9d5Sandi
3053dd5c225SAndreas Gohr    /**
3063dd5c225SAndreas Gohr     * Close a paragraph
3073dd5c225SAndreas Gohr     */
308faf3f01bSAndreas Gohr    public function p_close()
309faf3f01bSAndreas Gohr    {
31059869a4bSAnika Henke        $this->doc .= DOKU_LF . '</p>' . DOKU_LF;
3110cecf9d5Sandi    }
3120cecf9d5Sandi
3133dd5c225SAndreas Gohr    /**
3143dd5c225SAndreas Gohr     * Create a line break
3153dd5c225SAndreas Gohr     */
316faf3f01bSAndreas Gohr    public function linebreak()
317faf3f01bSAndreas Gohr    {
318a2d649c4Sandi        $this->doc .= '<br/>' . DOKU_LF;
3190cecf9d5Sandi    }
3200cecf9d5Sandi
3213dd5c225SAndreas Gohr    /**
3223dd5c225SAndreas Gohr     * Create a horizontal line
3233dd5c225SAndreas Gohr     */
324faf3f01bSAndreas Gohr    public function hr()
325faf3f01bSAndreas Gohr    {
3264beabca9SAnika Henke        $this->doc .= '<hr />' . DOKU_LF;
3270cecf9d5Sandi    }
3280cecf9d5Sandi
3293dd5c225SAndreas Gohr    /**
3303dd5c225SAndreas Gohr     * Start strong (bold) formatting
3313dd5c225SAndreas Gohr     */
332faf3f01bSAndreas Gohr    public function strong_open()
333faf3f01bSAndreas Gohr    {
334a2d649c4Sandi        $this->doc .= '<strong>';
3350cecf9d5Sandi    }
3360cecf9d5Sandi
3373dd5c225SAndreas Gohr    /**
3383dd5c225SAndreas Gohr     * Stop strong (bold) formatting
3393dd5c225SAndreas Gohr     */
340faf3f01bSAndreas Gohr    public function strong_close()
341faf3f01bSAndreas Gohr    {
342a2d649c4Sandi        $this->doc .= '</strong>';
3430cecf9d5Sandi    }
3440cecf9d5Sandi
3453dd5c225SAndreas Gohr    /**
3463dd5c225SAndreas Gohr     * Start emphasis (italics) formatting
3473dd5c225SAndreas Gohr     */
348faf3f01bSAndreas Gohr    public function emphasis_open()
349faf3f01bSAndreas Gohr    {
350a2d649c4Sandi        $this->doc .= '<em>';
3510cecf9d5Sandi    }
3520cecf9d5Sandi
3533dd5c225SAndreas Gohr    /**
3543dd5c225SAndreas Gohr     * Stop emphasis (italics) formatting
3553dd5c225SAndreas Gohr     */
356faf3f01bSAndreas Gohr    public function emphasis_close()
357faf3f01bSAndreas Gohr    {
358a2d649c4Sandi        $this->doc .= '</em>';
3590cecf9d5Sandi    }
3600cecf9d5Sandi
3613dd5c225SAndreas Gohr    /**
3623dd5c225SAndreas Gohr     * Start underline formatting
3633dd5c225SAndreas Gohr     */
364faf3f01bSAndreas Gohr    public function underline_open()
365faf3f01bSAndreas Gohr    {
36602e51121SAnika Henke        $this->doc .= '<em class="u">';
3670cecf9d5Sandi    }
3680cecf9d5Sandi
3693dd5c225SAndreas Gohr    /**
3703dd5c225SAndreas Gohr     * Stop underline formatting
3713dd5c225SAndreas Gohr     */
372faf3f01bSAndreas Gohr    public function underline_close()
373faf3f01bSAndreas Gohr    {
37402e51121SAnika Henke        $this->doc .= '</em>';
3750cecf9d5Sandi    }
3760cecf9d5Sandi
3773dd5c225SAndreas Gohr    /**
3783dd5c225SAndreas Gohr     * Start monospace formatting
3793dd5c225SAndreas Gohr     */
380faf3f01bSAndreas Gohr    public function monospace_open()
381faf3f01bSAndreas Gohr    {
382a2d649c4Sandi        $this->doc .= '<code>';
3830cecf9d5Sandi    }
3840cecf9d5Sandi
3853dd5c225SAndreas Gohr    /**
3863dd5c225SAndreas Gohr     * Stop monospace formatting
3873dd5c225SAndreas Gohr     */
388faf3f01bSAndreas Gohr    public function monospace_close()
389faf3f01bSAndreas Gohr    {
390a2d649c4Sandi        $this->doc .= '</code>';
3910cecf9d5Sandi    }
3920cecf9d5Sandi
3933dd5c225SAndreas Gohr    /**
3943dd5c225SAndreas Gohr     * Start a subscript
3953dd5c225SAndreas Gohr     */
396faf3f01bSAndreas Gohr    public function subscript_open()
397faf3f01bSAndreas Gohr    {
398a2d649c4Sandi        $this->doc .= '<sub>';
3990cecf9d5Sandi    }
4000cecf9d5Sandi
4013dd5c225SAndreas Gohr    /**
4023dd5c225SAndreas Gohr     * Stop a subscript
4033dd5c225SAndreas Gohr     */
404faf3f01bSAndreas Gohr    public function subscript_close()
405faf3f01bSAndreas Gohr    {
406a2d649c4Sandi        $this->doc .= '</sub>';
4070cecf9d5Sandi    }
4080cecf9d5Sandi
4093dd5c225SAndreas Gohr    /**
4103dd5c225SAndreas Gohr     * Start a superscript
4113dd5c225SAndreas Gohr     */
412faf3f01bSAndreas Gohr    public function superscript_open()
413faf3f01bSAndreas Gohr    {
414a2d649c4Sandi        $this->doc .= '<sup>';
4150cecf9d5Sandi    }
4160cecf9d5Sandi
4173dd5c225SAndreas Gohr    /**
4183dd5c225SAndreas Gohr     * Stop a superscript
4193dd5c225SAndreas Gohr     */
420faf3f01bSAndreas Gohr    public function superscript_close()
421faf3f01bSAndreas Gohr    {
422a2d649c4Sandi        $this->doc .= '</sup>';
4230cecf9d5Sandi    }
4240cecf9d5Sandi
4253dd5c225SAndreas Gohr    /**
4263dd5c225SAndreas Gohr     * Start deleted (strike-through) formatting
4273dd5c225SAndreas Gohr     */
428faf3f01bSAndreas Gohr    public function deleted_open()
429faf3f01bSAndreas Gohr    {
430a2d649c4Sandi        $this->doc .= '<del>';
4310cecf9d5Sandi    }
4320cecf9d5Sandi
4333dd5c225SAndreas Gohr    /**
4343dd5c225SAndreas Gohr     * Stop deleted (strike-through) formatting
4353dd5c225SAndreas Gohr     */
436faf3f01bSAndreas Gohr    public function deleted_close()
437faf3f01bSAndreas Gohr    {
438a2d649c4Sandi        $this->doc .= '</del>';
4390cecf9d5Sandi    }
4400cecf9d5Sandi
4413fd0b676Sandi    /**
4423fd0b676Sandi     * Callback for footnote start syntax
4433fd0b676Sandi     *
4443fd0b676Sandi     * All following content will go to the footnote instead of
445d74aace9Schris     * the document. To achieve this the previous rendered content
4463fd0b676Sandi     * is moved to $store and $doc is cleared
4473fd0b676Sandi     *
4483fd0b676Sandi     * @author Andreas Gohr <andi@splitbrain.org>
4493fd0b676Sandi     */
450faf3f01bSAndreas Gohr    public function footnote_open()
451faf3f01bSAndreas Gohr    {
4527764a90aSandi
4537764a90aSandi        // move current content to store and record footnote
4547764a90aSandi        $this->store = $this->doc;
4557764a90aSandi        $this->doc = '';
4560cecf9d5Sandi    }
4570cecf9d5Sandi
4583fd0b676Sandi    /**
4593fd0b676Sandi     * Callback for footnote end syntax
4603fd0b676Sandi     *
4613fd0b676Sandi     * All rendered content is moved to the $footnotes array and the old
4623fd0b676Sandi     * content is restored from $store again
4633fd0b676Sandi     *
4643fd0b676Sandi     * @author Andreas Gohr
4653fd0b676Sandi     */
466faf3f01bSAndreas Gohr    public function footnote_close()
467faf3f01bSAndreas Gohr    {
46816ec3e37SAndreas Gohr        /** @var $fnid int takes track of seen footnotes, assures they are unique even across multiple docs FS#2841 */
46916ec3e37SAndreas Gohr        static $fnid = 0;
47016ec3e37SAndreas Gohr        // assign new footnote id (we start at 1)
47116ec3e37SAndreas Gohr        $fnid++;
4727764a90aSandi
473d74aace9Schris        // recover footnote into the stack and restore old content
474d74aace9Schris        $footnote = $this->doc;
4757764a90aSandi        $this->doc = $this->store;
4767764a90aSandi        $this->store = '';
477d74aace9Schris
478d74aace9Schris        // check to see if this footnote has been seen before
479d74aace9Schris        $i = array_search($footnote, $this->footnotes);
480d74aace9Schris
481d74aace9Schris        if ($i === false) {
482d74aace9Schris            // its a new footnote, add it to the $footnotes array
48316ec3e37SAndreas Gohr            $this->footnotes[$fnid] = $footnote;
484d74aace9Schris        } else {
48516ec3e37SAndreas Gohr            // seen this one before, save a placeholder
48616ec3e37SAndreas Gohr            $this->footnotes[$fnid] = "@@FNT" . ($i);
487d74aace9Schris        }
488d74aace9Schris
4896b379cbfSAndreas Gohr        // output the footnote reference and link
49095078f23SAndreas Gohr        $this->doc .= sprintf(
49195078f23SAndreas Gohr            '<sup><a href="#fn__%d" id="fnt__%d" class="fn_top">%d)</a></sup>',
49295078f23SAndreas Gohr            $fnid,
49395078f23SAndreas Gohr            $fnid,
49495078f23SAndreas Gohr            $fnid
49595078f23SAndreas Gohr        );
4960cecf9d5Sandi    }
4970cecf9d5Sandi
4983dd5c225SAndreas Gohr    /**
4993dd5c225SAndreas Gohr     * Open an unordered list
5000c4c0281SGerrit Uitslag     *
5017d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
5023dd5c225SAndreas Gohr     */
503faf3f01bSAndreas Gohr    public function listu_open($classes = null)
504faf3f01bSAndreas Gohr    {
5050c4c0281SGerrit Uitslag        $class = '';
5060c4c0281SGerrit Uitslag        if ($classes !== null) {
507faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
5080c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
5090c4c0281SGerrit Uitslag        }
5100c4c0281SGerrit Uitslag        $this->doc .= "<ul$class>" . DOKU_LF;
5110cecf9d5Sandi    }
5120cecf9d5Sandi
5133dd5c225SAndreas Gohr    /**
5143dd5c225SAndreas Gohr     * Close an unordered list
5153dd5c225SAndreas Gohr     */
516faf3f01bSAndreas Gohr    public function listu_close()
517faf3f01bSAndreas Gohr    {
518a2d649c4Sandi        $this->doc .= '</ul>' . DOKU_LF;
5190cecf9d5Sandi    }
5200cecf9d5Sandi
5213dd5c225SAndreas Gohr    /**
5223dd5c225SAndreas Gohr     * Open an ordered list
5230c4c0281SGerrit Uitslag     *
5247d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
5253dd5c225SAndreas Gohr     */
526faf3f01bSAndreas Gohr    public function listo_open($classes = null)
527faf3f01bSAndreas Gohr    {
5280c4c0281SGerrit Uitslag        $class = '';
5290c4c0281SGerrit Uitslag        if ($classes !== null) {
530faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
5310c4c0281SGerrit Uitslag            $class = " class=\"$classes\"";
5320c4c0281SGerrit Uitslag        }
5330c4c0281SGerrit Uitslag        $this->doc .= "<ol$class>" . DOKU_LF;
5340cecf9d5Sandi    }
5350cecf9d5Sandi
5363dd5c225SAndreas Gohr    /**
5373dd5c225SAndreas Gohr     * Close an ordered list
5383dd5c225SAndreas Gohr     */
539faf3f01bSAndreas Gohr    public function listo_close()
540faf3f01bSAndreas Gohr    {
541a2d649c4Sandi        $this->doc .= '</ol>' . DOKU_LF;
5420cecf9d5Sandi    }
5430cecf9d5Sandi
5443dd5c225SAndreas Gohr    /**
5453dd5c225SAndreas Gohr     * Open a list item
5463dd5c225SAndreas Gohr     *
5473dd5c225SAndreas Gohr     * @param int $level the nesting level
548e3a24861SChristopher Smith     * @param bool $node true when a node; false when a leaf
5493dd5c225SAndreas Gohr     */
550faf3f01bSAndreas Gohr    public function listitem_open($level, $node = false)
551faf3f01bSAndreas Gohr    {
552e3a24861SChristopher Smith        $branching = $node ? ' node' : '';
553e3a24861SChristopher Smith        $this->doc .= '<li class="level' . $level . $branching . '">';
5540cecf9d5Sandi    }
5550cecf9d5Sandi
5563dd5c225SAndreas Gohr    /**
5573dd5c225SAndreas Gohr     * Close a list item
5583dd5c225SAndreas Gohr     */
559faf3f01bSAndreas Gohr    public function listitem_close()
560faf3f01bSAndreas Gohr    {
561a2d649c4Sandi        $this->doc .= '</li>' . DOKU_LF;
5620cecf9d5Sandi    }
5630cecf9d5Sandi
5643dd5c225SAndreas Gohr    /**
5653dd5c225SAndreas Gohr     * Start the content of a list item
5663dd5c225SAndreas Gohr     */
567faf3f01bSAndreas Gohr    public function listcontent_open()
568faf3f01bSAndreas Gohr    {
56990db23d7Schris        $this->doc .= '<div class="li">';
5700cecf9d5Sandi    }
5710cecf9d5Sandi
5723dd5c225SAndreas Gohr    /**
5733dd5c225SAndreas Gohr     * Stop the content of a list item
5743dd5c225SAndreas Gohr     */
575faf3f01bSAndreas Gohr    public function listcontent_close()
576faf3f01bSAndreas Gohr    {
57759869a4bSAnika Henke        $this->doc .= '</div>' . DOKU_LF;
5780cecf9d5Sandi    }
5790cecf9d5Sandi
5803dd5c225SAndreas Gohr    /**
5813dd5c225SAndreas Gohr     * Output unformatted $text
5823dd5c225SAndreas Gohr     *
5833dd5c225SAndreas Gohr     * Defaults to $this->cdata()
5843dd5c225SAndreas Gohr     *
5853dd5c225SAndreas Gohr     * @param string $text
5863dd5c225SAndreas Gohr     */
587faf3f01bSAndreas Gohr    public function unformatted($text)
588faf3f01bSAndreas Gohr    {
589a2d649c4Sandi        $this->doc .= $this->_xmlEntities($text);
5900cecf9d5Sandi    }
5910cecf9d5Sandi
5920cecf9d5Sandi    /**
5933dd5c225SAndreas Gohr     * Start a block quote
5943dd5c225SAndreas Gohr     */
595faf3f01bSAndreas Gohr    public function quote_open()
596faf3f01bSAndreas Gohr    {
59796331712SAnika Henke        $this->doc .= '<blockquote><div class="no">' . DOKU_LF;
5980cecf9d5Sandi    }
5990cecf9d5Sandi
6003dd5c225SAndreas Gohr    /**
6013dd5c225SAndreas Gohr     * Stop a block quote
6023dd5c225SAndreas Gohr     */
603faf3f01bSAndreas Gohr    public function quote_close()
604faf3f01bSAndreas Gohr    {
60596331712SAnika Henke        $this->doc .= '</div></blockquote>' . DOKU_LF;
6060cecf9d5Sandi    }
6070cecf9d5Sandi
6083dd5c225SAndreas Gohr    /**
6093dd5c225SAndreas Gohr     * Output preformatted text
6103dd5c225SAndreas Gohr     *
6113dd5c225SAndreas Gohr     * @param string $text
6123dd5c225SAndreas Gohr     */
613faf3f01bSAndreas Gohr    public function preformatted($text)
614faf3f01bSAndreas Gohr    {
615c9250713SAnika Henke        $this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text), "\n\r") . '</pre>' . DOKU_LF;
6163d491f75SAndreas Gohr    }
6173d491f75SAndreas Gohr
6183dd5c225SAndreas Gohr    /**
6193dd5c225SAndreas Gohr     * Display text as file content, optionally syntax highlighted
6203dd5c225SAndreas Gohr     *
6213dd5c225SAndreas Gohr     * @param string $text text to show
6223dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6233dd5c225SAndreas Gohr     * @param string $filename file path label
624e2d88156SLarsDW223     * @param array $options assoziative array with additional geshi options
6253dd5c225SAndreas Gohr     */
626faf3f01bSAndreas Gohr    public function file($text, $language = null, $filename = null, $options = null)
627faf3f01bSAndreas Gohr    {
628e2d88156SLarsDW223        $this->_highlight('file', $text, $language, $filename, $options);
6293d491f75SAndreas Gohr    }
6303d491f75SAndreas Gohr
6313dd5c225SAndreas Gohr    /**
6323dd5c225SAndreas Gohr     * Display text as code content, optionally syntax highlighted
6333dd5c225SAndreas Gohr     *
6343dd5c225SAndreas Gohr     * @param string $text text to show
6353dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6363dd5c225SAndreas Gohr     * @param string $filename file path label
637e2d88156SLarsDW223     * @param array $options assoziative array with additional geshi options
6383dd5c225SAndreas Gohr     */
639faf3f01bSAndreas Gohr    public function code($text, $language = null, $filename = null, $options = null)
640faf3f01bSAndreas Gohr    {
641e2d88156SLarsDW223        $this->_highlight('code', $text, $language, $filename, $options);
6423d491f75SAndreas Gohr    }
6433d491f75SAndreas Gohr
6440cecf9d5Sandi    /**
6453d491f75SAndreas Gohr     * Use GeSHi to highlight language syntax in code and file blocks
6463fd0b676Sandi     *
6473dd5c225SAndreas Gohr     * @param string $type code|file
6483dd5c225SAndreas Gohr     * @param string $text text to show
6493dd5c225SAndreas Gohr     * @param string $language programming language to use for syntax highlighting
6503dd5c225SAndreas Gohr     * @param string $filename file path label
651e2d88156SLarsDW223     * @param array $options assoziative array with additional geshi options
652faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
6530cecf9d5Sandi     */
654faf3f01bSAndreas Gohr    public function _highlight($type, $text, $language = null, $filename = null, $options = null)
655faf3f01bSAndreas Gohr    {
6563d491f75SAndreas Gohr        global $ID;
6573d491f75SAndreas Gohr        global $lang;
658ec57f119SLarsDW223        global $INPUT;
6593d491f75SAndreas Gohr
660bf8f8509SAndreas Gohr        $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language ?? '');
66156bd9509SPhy
6623d491f75SAndreas Gohr        if ($filename) {
663190c56e8SAndreas Gohr            // add icon
664faf3f01bSAndreas Gohr            [$ext] = mimetype($filename, false);
665190c56e8SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
666190c56e8SAndreas Gohr            $class = 'mediafile mf_' . $class;
667190c56e8SAndreas Gohr
668ec57f119SLarsDW223            $offset = 0;
669ec57f119SLarsDW223            if ($INPUT->has('codeblockOffset')) {
670ec57f119SLarsDW223                $offset = $INPUT->str('codeblockOffset');
671ec57f119SLarsDW223            }
6723d491f75SAndreas Gohr            $this->doc .= '<dl class="' . $type . '">' . DOKU_LF;
67364159a61SAndreas Gohr            $this->doc .= '<dt><a href="' .
67464159a61SAndreas Gohr                exportlink(
67564159a61SAndreas Gohr                    $ID,
67664159a61SAndreas Gohr                    'code',
677faf3f01bSAndreas Gohr                    ['codeblock' => $offset + $this->_codeblock]
67864159a61SAndreas Gohr                ) . '" title="' . $lang['download'] . '" class="' . $class . '">';
6793d491f75SAndreas Gohr            $this->doc .= hsc($filename);
6803d491f75SAndreas Gohr            $this->doc .= '</a></dt>' . DOKU_LF . '<dd>';
6813d491f75SAndreas Gohr        }
6820cecf9d5Sandi
6832401f18dSSyntaxseed        if ($text[0] == "\n") {
684d43aac1cSGina Haeussge            $text = substr($text, 1);
685d43aac1cSGina Haeussge        }
686d43aac1cSGina Haeussge        if (substr($text, -1) == "\n") {
687d43aac1cSGina Haeussge            $text = substr($text, 0, -1);
688d43aac1cSGina Haeussge        }
689d43aac1cSGina Haeussge
690a056e285SPhy        if (empty($language)) { // empty is faster than is_null and can prevent '' string
6913d491f75SAndreas Gohr            $this->doc .= '<pre class="' . $type . '">' . $this->_xmlEntities($text) . '</pre>' . DOKU_LF;
6920cecf9d5Sandi        } else {
6933d491f75SAndreas Gohr            $class = 'code'; //we always need the code class to make the syntax highlighting apply
6943d491f75SAndreas Gohr            if ($type != 'code') $class .= ' ' . $type;
6953d491f75SAndreas Gohr
69664159a61SAndreas Gohr            $this->doc .= "<pre class=\"$class $language\">" .
69764159a61SAndreas Gohr                p_xhtml_cached_geshi($text, $language, '', $options) .
69864159a61SAndreas Gohr                '</pre>' . DOKU_LF;
6990cecf9d5Sandi        }
7003d491f75SAndreas Gohr
7013d491f75SAndreas Gohr        if ($filename) {
7023d491f75SAndreas Gohr            $this->doc .= '</dd></dl>' . DOKU_LF;
7033d491f75SAndreas Gohr        }
7043d491f75SAndreas Gohr
7053d491f75SAndreas Gohr        $this->_codeblock++;
7060cecf9d5Sandi    }
7070cecf9d5Sandi
7083dd5c225SAndreas Gohr    /**
7093dd5c225SAndreas Gohr     * Format an acronym
7103dd5c225SAndreas Gohr     *
7113dd5c225SAndreas Gohr     * Uses $this->acronyms
7123dd5c225SAndreas Gohr     *
7133dd5c225SAndreas Gohr     * @param string $acronym
7143dd5c225SAndreas Gohr     */
715faf3f01bSAndreas Gohr    public function acronym($acronym)
716faf3f01bSAndreas Gohr    {
7170cecf9d5Sandi
7180cecf9d5Sandi        if (array_key_exists($acronym, $this->acronyms)) {
719433bef32Sandi            $title = $this->_xmlEntities($this->acronyms[$acronym]);
7200cecf9d5Sandi
721940db3a3SAnika Henke            $this->doc .= '<abbr title="' . $title
722940db3a3SAnika Henke                . '">' . $this->_xmlEntities($acronym) . '</abbr>';
7230cecf9d5Sandi        } else {
724a2d649c4Sandi            $this->doc .= $this->_xmlEntities($acronym);
7250cecf9d5Sandi        }
7260cecf9d5Sandi    }
7270cecf9d5Sandi
7283dd5c225SAndreas Gohr    /**
7293dd5c225SAndreas Gohr     * Format a smiley
7303dd5c225SAndreas Gohr     *
7313dd5c225SAndreas Gohr     * Uses $this->smiley
7323dd5c225SAndreas Gohr     *
7333dd5c225SAndreas Gohr     * @param string $smiley
7343dd5c225SAndreas Gohr     */
735faf3f01bSAndreas Gohr    public function smiley($smiley)
736faf3f01bSAndreas Gohr    {
737b09504a9SAndreas Gohr        if (isset($this->smileys[$smiley])) {
738f62ea8a1Sandi            $this->doc .= '<img src="' . DOKU_BASE . 'lib/images/smileys/' . $this->smileys[$smiley] .
739b09504a9SAndreas Gohr                '" class="icon smiley" alt="' . $this->_xmlEntities($smiley) . '" />';
7400cecf9d5Sandi        } else {
741a2d649c4Sandi            $this->doc .= $this->_xmlEntities($smiley);
7420cecf9d5Sandi        }
7430cecf9d5Sandi    }
7440cecf9d5Sandi
7453dd5c225SAndreas Gohr    /**
7463dd5c225SAndreas Gohr     * Format an entity
7473dd5c225SAndreas Gohr     *
7483dd5c225SAndreas Gohr     * Entities are basically small text replacements
7493dd5c225SAndreas Gohr     *
7503dd5c225SAndreas Gohr     * Uses $this->entities
7513dd5c225SAndreas Gohr     *
7523dd5c225SAndreas Gohr     * @param string $entity
7534de671bcSandi     */
754faf3f01bSAndreas Gohr    public function entity($entity)
755faf3f01bSAndreas Gohr    {
7560cecf9d5Sandi        if (array_key_exists($entity, $this->entities)) {
757a2d649c4Sandi            $this->doc .= $this->entities[$entity];
7580cecf9d5Sandi        } else {
759a2d649c4Sandi            $this->doc .= $this->_xmlEntities($entity);
7600cecf9d5Sandi        }
7610cecf9d5Sandi    }
7620cecf9d5Sandi
7633dd5c225SAndreas Gohr    /**
7643dd5c225SAndreas Gohr     * Typographically format a multiply sign
7653dd5c225SAndreas Gohr     *
7663dd5c225SAndreas Gohr     * Example: ($x=640, $y=480) should result in "640×480"
7673dd5c225SAndreas Gohr     *
7683dd5c225SAndreas Gohr     * @param string|int $x first value
7693dd5c225SAndreas Gohr     * @param string|int $y second value
7703dd5c225SAndreas Gohr     */
771faf3f01bSAndreas Gohr    public function multiplyentity($x, $y)
772faf3f01bSAndreas Gohr    {
773a2d649c4Sandi        $this->doc .= "$x&times;$y";
7740cecf9d5Sandi    }
7750cecf9d5Sandi
7763dd5c225SAndreas Gohr    /**
7773dd5c225SAndreas Gohr     * Render an opening single quote char (language specific)
7783dd5c225SAndreas Gohr     */
779faf3f01bSAndreas Gohr    public function singlequoteopening()
780faf3f01bSAndreas Gohr    {
78171b40da2SAnika Henke        global $lang;
78271b40da2SAnika Henke        $this->doc .= $lang['singlequoteopening'];
7830cecf9d5Sandi    }
7840cecf9d5Sandi
7853dd5c225SAndreas Gohr    /**
7863dd5c225SAndreas Gohr     * Render a closing single quote char (language specific)
7873dd5c225SAndreas Gohr     */
788faf3f01bSAndreas Gohr    public function singlequoteclosing()
789faf3f01bSAndreas Gohr    {
79071b40da2SAnika Henke        global $lang;
79171b40da2SAnika Henke        $this->doc .= $lang['singlequoteclosing'];
7920cecf9d5Sandi    }
7930cecf9d5Sandi
7943dd5c225SAndreas Gohr    /**
7953dd5c225SAndreas Gohr     * Render an apostrophe char (language specific)
7963dd5c225SAndreas Gohr     */
797faf3f01bSAndreas Gohr    public function apostrophe()
798faf3f01bSAndreas Gohr    {
79957d757d1SAndreas Gohr        global $lang;
800a8bd192aSAndreas Gohr        $this->doc .= $lang['apostrophe'];
80157d757d1SAndreas Gohr    }
80257d757d1SAndreas Gohr
8033dd5c225SAndreas Gohr    /**
8043dd5c225SAndreas Gohr     * Render an opening double quote char (language specific)
8053dd5c225SAndreas Gohr     */
806faf3f01bSAndreas Gohr    public function doublequoteopening()
807faf3f01bSAndreas Gohr    {
80871b40da2SAnika Henke        global $lang;
80971b40da2SAnika Henke        $this->doc .= $lang['doublequoteopening'];
8100cecf9d5Sandi    }
8110cecf9d5Sandi
8123dd5c225SAndreas Gohr    /**
8133dd5c225SAndreas Gohr     * Render an closinging double quote char (language specific)
8143dd5c225SAndreas Gohr     */
815faf3f01bSAndreas Gohr    public function doublequoteclosing()
816faf3f01bSAndreas Gohr    {
81771b40da2SAnika Henke        global $lang;
81871b40da2SAnika Henke        $this->doc .= $lang['doublequoteclosing'];
8190cecf9d5Sandi    }
8200cecf9d5Sandi
8210cecf9d5Sandi    /**
8223dd5c225SAndreas Gohr     * Render a CamelCase link
8233dd5c225SAndreas Gohr     *
8243dd5c225SAndreas Gohr     * @param string $link The link name
825122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
8260c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
8270c4c0281SGerrit Uitslag     *
8283dd5c225SAndreas Gohr     * @see http://en.wikipedia.org/wiki/CamelCase
8290cecf9d5Sandi     */
830faf3f01bSAndreas Gohr    public function camelcaselink($link, $returnonly = false)
831faf3f01bSAndreas Gohr    {
832122f2d46SAndreas Böhler        if ($returnonly) {
833122f2d46SAndreas Böhler            return $this->internallink($link, $link, null, true);
834122f2d46SAndreas Böhler        } else {
83511d0aa47Sandi            $this->internallink($link, $link);
8360cecf9d5Sandi        }
837122f2d46SAndreas Böhler    }
8380cecf9d5Sandi
8393dd5c225SAndreas Gohr    /**
8403dd5c225SAndreas Gohr     * Render a page local link
8413dd5c225SAndreas Gohr     *
8423dd5c225SAndreas Gohr     * @param string $hash hash link identifier
8433dd5c225SAndreas Gohr     * @param string $name name for the link
844122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
8450c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
8463dd5c225SAndreas Gohr     */
847faf3f01bSAndreas Gohr    public function locallink($hash, $name = null, $returnonly = false)
848faf3f01bSAndreas Gohr    {
8490b7c14c2Sandi        global $ID;
8500b7c14c2Sandi        $name = $this->_getLinkTitle($name, $hash, $isImage);
8510b7c14c2Sandi        $hash = $this->_headerToLink($hash);
852e260f93bSAnika Henke        $title = $ID . ' ↵';
853122f2d46SAndreas Böhler
854122f2d46SAndreas Böhler        $doc = '<a href="#' . $hash . '" title="' . $title . '" class="wikilink1">';
855122f2d46SAndreas Böhler        $doc .= $name;
856122f2d46SAndreas Böhler        $doc .= '</a>';
857122f2d46SAndreas Böhler
858122f2d46SAndreas Böhler        if ($returnonly) {
859122f2d46SAndreas Böhler            return $doc;
860122f2d46SAndreas Böhler        } else {
861122f2d46SAndreas Böhler            $this->doc .= $doc;
862122f2d46SAndreas Böhler        }
8630b7c14c2Sandi    }
8640b7c14c2Sandi
865cffcc403Sandi    /**
8663fd0b676Sandi     * Render an internal Wiki Link
8673fd0b676Sandi     *
868fe9ec250SChris Smith     * $search,$returnonly & $linktype are not for the renderer but are used
869cffcc403Sandi     * elsewhere - no need to implement them in other renderers
8703fd0b676Sandi     *
871f23eef27SGerrit Uitslag     * @param string $id pageid
872f23eef27SGerrit Uitslag     * @param string|null $name link name
873f23eef27SGerrit Uitslag     * @param string|null $search adds search url param
874f23eef27SGerrit Uitslag     * @param bool $returnonly whether to return html or write to doc attribute
875f23eef27SGerrit Uitslag     * @param string $linktype type to set use of headings
876f23eef27SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
877faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
878cffcc403Sandi     */
879faf3f01bSAndreas Gohr    public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content')
880faf3f01bSAndreas Gohr    {
881ba11bd29Sandi        global $conf;
88237e34a5eSandi        global $ID;
883c4dda6afSAnika Henke        global $INFO;
88444653a53SAdrian Lang
8853d5e07d9SAdrian Lang        $params = '';
8863d5e07d9SAdrian Lang        $parts = explode('?', $id, 2);
8873d5e07d9SAdrian Lang        if (count($parts) === 2) {
8883d5e07d9SAdrian Lang            $id = $parts[0];
8893d5e07d9SAdrian Lang            $params = $parts[1];
89044653a53SAdrian Lang        }
89144653a53SAdrian Lang
892fda14ffcSIzidor Matušov        // For empty $id we need to know the current $ID
893fda14ffcSIzidor Matušov        // We need this check because _simpleTitle needs
894fda14ffcSIzidor Matušov        // correct $id and resolve_pageid() use cleanID($id)
895fda14ffcSIzidor Matušov        // (some things could be lost)
896fda14ffcSIzidor Matušov        if ($id === '') {
897fda14ffcSIzidor Matušov            $id = $ID;
898fda14ffcSIzidor Matušov        }
899fda14ffcSIzidor Matušov
9000339c872Sjan        // default name is based on $id as given
9010339c872Sjan        $default = $this->_simpleTitle($id);
902ad32e47eSAndreas Gohr
9030339c872Sjan        // now first resolve and clean up the $id
9048c6be208SAndreas Gohr        $id = (new PageResolver($ID))->resolveId($id, $this->date_at, true);
9058c6be208SAndreas Gohr        $exists = page_exists($id, $this->date_at, false, true);
906fda14ffcSIzidor Matušov
907faf3f01bSAndreas Gohr        $link = [];
908fe9ec250SChris Smith        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
9090e1c636eSandi        if (!$isImage) {
9100e1c636eSandi            if ($exists) {
911ba11bd29Sandi                $class = 'wikilink1';
9120cecf9d5Sandi            } else {
913ba11bd29Sandi                $class = 'wikilink2';
91444a6b4c7SAndreas Gohr                $link['rel'] = 'nofollow';
9150cecf9d5Sandi            }
9160cecf9d5Sandi        } else {
917ba11bd29Sandi            $class = 'media';
9180cecf9d5Sandi        }
9190cecf9d5Sandi
920a1685bedSandi        //keep hash anchor
921faf3f01bSAndreas Gohr        [$id, $hash] = sexplode('#', $id, 2);
922943dedc6SAndreas Gohr        if (!empty($hash)) $hash = $this->_headerToLink($hash);
923a1685bedSandi
924ba11bd29Sandi        //prepare for formating
925ba11bd29Sandi        $link['target'] = $conf['target']['wiki'];
926ba11bd29Sandi        $link['style'] = '';
927ba11bd29Sandi        $link['pre'] = '';
928ba11bd29Sandi        $link['suf'] = '';
929bbac1489SPhy        $link['more'] = 'data-wiki-id="' . $id . '"'; // id is already cleaned
930ba11bd29Sandi        $link['class'] = $class;
9315c2eed9aSlisps        if ($this->date_at) {
932912a6d48SPhy            $params = $params . '&at=' . rawurlencode($this->date_at);
9335c2eed9aSlisps        }
93444653a53SAdrian Lang        $link['url'] = wl($id, $params);
935ba11bd29Sandi        $link['name'] = $name;
936ba11bd29Sandi        $link['title'] = $id;
937723d78dbSandi        //add search string
938723d78dbSandi        if ($search) {
939546d3a99SAndreas Gohr            ($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
940546d3a99SAndreas Gohr            if (is_array($search)) {
941546d3a99SAndreas Gohr                $search = array_map('rawurlencode', $search);
942faf3f01bSAndreas Gohr                $link['url'] .= 's[]=' . implode('&amp;s[]=', $search);
943546d3a99SAndreas Gohr            } else {
944546d3a99SAndreas Gohr                $link['url'] .= 's=' . rawurlencode($search);
945546d3a99SAndreas Gohr            }
946723d78dbSandi        }
947723d78dbSandi
948a1685bedSandi        //keep hash
949a1685bedSandi        if ($hash) $link['url'] .= '#' . $hash;
950a1685bedSandi
951ba11bd29Sandi        //output formatted
952cffcc403Sandi        if ($returnonly) {
953cffcc403Sandi            return $this->_formatLink($link);
954cffcc403Sandi        } else {
955a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
9560cecf9d5Sandi        }
957cffcc403Sandi    }
9580cecf9d5Sandi
9593dd5c225SAndreas Gohr    /**
9603dd5c225SAndreas Gohr     * Render an external link
9613dd5c225SAndreas Gohr     *
9623dd5c225SAndreas Gohr     * @param string $url full URL with scheme
9633dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
964122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
9650c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
9663dd5c225SAndreas Gohr     */
967faf3f01bSAndreas Gohr    public function externallink($url, $name = null, $returnonly = false)
968faf3f01bSAndreas Gohr    {
969b625487dSandi        global $conf;
9700cecf9d5Sandi
971433bef32Sandi        $name = $this->_getLinkTitle($name, $url, $isImage);
9726f0c5dbfSandi
973b52b1596SAndreas Gohr        // url might be an attack vector, only allow registered protocols
974b52b1596SAndreas Gohr        if (is_null($this->schemes)) $this->schemes = getSchemes();
975faf3f01bSAndreas Gohr        [$scheme] = explode('://', $url);
976b52b1596SAndreas Gohr        $scheme = strtolower($scheme);
977b52b1596SAndreas Gohr        if (!in_array($scheme, $this->schemes)) $url = '';
978b52b1596SAndreas Gohr
979b52b1596SAndreas Gohr        // is there still an URL?
980b52b1596SAndreas Gohr        if (!$url) {
98149cef4fdSAndreas Böhler            if ($returnonly) {
98249cef4fdSAndreas Böhler                return $name;
98349cef4fdSAndreas Böhler            } else {
984b52b1596SAndreas Gohr                $this->doc .= $name;
98549cef4fdSAndreas Böhler            }
986b52b1596SAndreas Gohr            return;
987b52b1596SAndreas Gohr        }
988b52b1596SAndreas Gohr
989b52b1596SAndreas Gohr        // set class
9900cecf9d5Sandi        if (!$isImage) {
991b625487dSandi            $class = 'urlextern';
9920cecf9d5Sandi        } else {
993b625487dSandi            $class = 'media';
9940cecf9d5Sandi        }
9950cecf9d5Sandi
996b625487dSandi        //prepare for formating
997faf3f01bSAndreas Gohr        $link = [];
998b625487dSandi        $link['target'] = $conf['target']['extern'];
999b625487dSandi        $link['style'] = '';
1000b625487dSandi        $link['pre'] = '';
1001b625487dSandi        $link['suf'] = '';
10025e163278SAndreas Gohr        $link['more'] = '';
1003b625487dSandi        $link['class'] = $class;
1004b625487dSandi        $link['url'] = $url;
1005914045f3SAndreas Gohr        $link['rel'] = '';
1006e1c10e4dSchris
1007b625487dSandi        $link['name'] = $name;
1008433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
10095ddd0bbbSStarArmy        if ($conf['relnofollow']) $link['rel'] .= ' ugc nofollow';
1010914045f3SAndreas Gohr        if ($conf['target']['extern']) $link['rel'] .= ' noopener';
10110cecf9d5Sandi
1012b625487dSandi        //output formatted
1013122f2d46SAndreas Böhler        if ($returnonly) {
1014122f2d46SAndreas Böhler            return $this->_formatLink($link);
1015122f2d46SAndreas Böhler        } else {
1016a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
10170cecf9d5Sandi        }
1018122f2d46SAndreas Böhler    }
10190cecf9d5Sandi
10200cecf9d5Sandi    /**
10213dd5c225SAndreas Gohr     * Render an interwiki link
10223dd5c225SAndreas Gohr     *
10233dd5c225SAndreas Gohr     * You may want to use $this->_resolveInterWiki() here
10243dd5c225SAndreas Gohr     *
10253dd5c225SAndreas Gohr     * @param string $match original link - probably not much use
10263dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
10273dd5c225SAndreas Gohr     * @param string $wikiName indentifier (shortcut) for the remote wiki
10283dd5c225SAndreas Gohr     * @param string $wikiUri the fragment parsed from the original link
1029122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
10300c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10310cecf9d5Sandi     */
1032faf3f01bSAndreas Gohr    public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false)
1033faf3f01bSAndreas Gohr    {
1034b625487dSandi        global $conf;
10350cecf9d5Sandi
1036faf3f01bSAndreas Gohr        $link = [];
103797a3e4e3Sandi        $link['target'] = $conf['target']['interwiki'];
103897a3e4e3Sandi        $link['pre'] = '';
103997a3e4e3Sandi        $link['suf'] = '';
10405e163278SAndreas Gohr        $link['more'] = '';
1041433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $wikiUri, $isImage);
1042914045f3SAndreas Gohr        $link['rel'] = '';
10430cecf9d5Sandi
104497a3e4e3Sandi        //get interwiki URL
10456496c33fSGerrit Uitslag        $exists = null;
10466496c33fSGerrit Uitslag        $url = $this->_resolveInterWiki($wikiName, $wikiUri, $exists);
10470cecf9d5Sandi
104897a3e4e3Sandi        if (!$isImage) {
10499d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $wikiName);
10509d2ddea4SAndreas Gohr            $link['class'] = "interwiki iw_$class";
10511c2d1019SAndreas Gohr        } else {
10521c2d1019SAndreas Gohr            $link['class'] = 'media';
105397a3e4e3Sandi        }
10540cecf9d5Sandi
105597a3e4e3Sandi        //do we stay at the same server? Use local target
1056faf3f01bSAndreas Gohr        if (strpos($url, DOKU_URL) === 0 || strpos($url, DOKU_BASE) === 0) {
105797a3e4e3Sandi            $link['target'] = $conf['target']['wiki'];
105897a3e4e3Sandi        }
10596496c33fSGerrit Uitslag        if ($exists !== null && !$isImage) {
10606496c33fSGerrit Uitslag            if ($exists) {
10616496c33fSGerrit Uitslag                $link['class'] .= ' wikilink1';
10626496c33fSGerrit Uitslag            } else {
10636496c33fSGerrit Uitslag                $link['class'] .= ' wikilink2';
1064914045f3SAndreas Gohr                $link['rel'] .= ' nofollow';
10656496c33fSGerrit Uitslag            }
10666496c33fSGerrit Uitslag        }
1067914045f3SAndreas Gohr        if ($conf['target']['interwiki']) $link['rel'] .= ' noopener';
10680cecf9d5Sandi
106997a3e4e3Sandi        $link['url'] = $url;
1070f7711f2bSAndreas Gohr        $link['title'] = $this->_xmlEntities($link['url']);
107197a3e4e3Sandi
107297a3e4e3Sandi        // output formatted
1073122f2d46SAndreas Böhler        if ($returnonly) {
1074abde5980SPhy            if ($url == '') return $link['name'];
1075122f2d46SAndreas Böhler            return $this->_formatLink($link);
1076faf3f01bSAndreas Gohr        } elseif ($url == '') {
1077faf3f01bSAndreas Gohr            $this->doc .= $link['name'];
1078faf3f01bSAndreas Gohr        } else $this->doc .= $this->_formatLink($link);
1079122f2d46SAndreas Böhler    }
10800cecf9d5Sandi
10810cecf9d5Sandi    /**
10823dd5c225SAndreas Gohr     * Link to windows share
10833dd5c225SAndreas Gohr     *
10843dd5c225SAndreas Gohr     * @param string $url the link
10853dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
1086122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
10870c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
10880cecf9d5Sandi     */
1089faf3f01bSAndreas Gohr    public function windowssharelink($url, $name = null, $returnonly = false)
1090faf3f01bSAndreas Gohr    {
10911d47afe1Sandi        global $conf;
10923dd5c225SAndreas Gohr
10931d47afe1Sandi        //simple setup
1094faf3f01bSAndreas Gohr        $link = [];
10951d47afe1Sandi        $link['target'] = $conf['target']['windows'];
10961d47afe1Sandi        $link['pre'] = '';
10971d47afe1Sandi        $link['suf'] = '';
10981d47afe1Sandi        $link['style'] = '';
10990cecf9d5Sandi
1100433bef32Sandi        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
11010cecf9d5Sandi        if (!$isImage) {
11021d47afe1Sandi            $link['class'] = 'windows';
11030cecf9d5Sandi        } else {
11041d47afe1Sandi            $link['class'] = 'media';
11050cecf9d5Sandi        }
11060cecf9d5Sandi
1107433bef32Sandi        $link['title'] = $this->_xmlEntities($url);
11081d47afe1Sandi        $url = str_replace('\\', '/', $url);
11091d47afe1Sandi        $url = 'file:///' . $url;
11101d47afe1Sandi        $link['url'] = $url;
11110cecf9d5Sandi
11121d47afe1Sandi        //output formatted
1113122f2d46SAndreas Böhler        if ($returnonly) {
1114122f2d46SAndreas Böhler            return $this->_formatLink($link);
1115122f2d46SAndreas Böhler        } else {
1116a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
11170cecf9d5Sandi        }
1118122f2d46SAndreas Böhler    }
11190cecf9d5Sandi
11203dd5c225SAndreas Gohr    /**
11213dd5c225SAndreas Gohr     * Render a linked E-Mail Address
11223dd5c225SAndreas Gohr     *
11233dd5c225SAndreas Gohr     * Honors $conf['mailguard'] setting
11243dd5c225SAndreas Gohr     *
11253dd5c225SAndreas Gohr     * @param string $address Email-Address
11263dd5c225SAndreas Gohr     * @param string|array $name name for the link, array for media file
1127122f2d46SAndreas Böhler     * @param bool $returnonly whether to return html or write to doc attribute
11280c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $returnonly
11293dd5c225SAndreas Gohr     */
1130faf3f01bSAndreas Gohr    public function emaillink($address, $name = null, $returnonly = false)
1131faf3f01bSAndreas Gohr    {
113271352defSandi        global $conf;
113371352defSandi        //simple setup
1134faf3f01bSAndreas Gohr        $link = [];
113571352defSandi        $link['target'] = '';
113671352defSandi        $link['pre'] = '';
113771352defSandi        $link['suf'] = '';
113871352defSandi        $link['style'] = '';
113971352defSandi        $link['more'] = '';
11400cecf9d5Sandi
1141c078fc55SAndreas Gohr        $name = $this->_getLinkTitle($name, '', $isImage);
11420cecf9d5Sandi        if (!$isImage) {
1143be96545cSAnika Henke            $link['class'] = 'mail';
11440cecf9d5Sandi        } else {
1145be96545cSAnika Henke            $link['class'] = 'media';
11460cecf9d5Sandi        }
11470cecf9d5Sandi
114807738714SAndreas Gohr        $address = $this->_xmlEntities($address);
114900a7b5adSEsther Brunner        $address = obfuscate($address);
1150faf3f01bSAndreas Gohr
115100a7b5adSEsther Brunner        $title = $address;
11528c128049SAndreas Gohr
115371352defSandi        if (empty($name)) {
115400a7b5adSEsther Brunner            $name = $address;
115571352defSandi        }
11560cecf9d5Sandi
1157776b36ecSAndreas Gohr        if ($conf['mailguard'] == 'visible') $address = rawurlencode($address);
1158776b36ecSAndreas Gohr
1159776b36ecSAndreas Gohr        $link['url'] = 'mailto:' . $address;
116071352defSandi        $link['name'] = $name;
116171352defSandi        $link['title'] = $title;
11620cecf9d5Sandi
116371352defSandi        //output formatted
1164122f2d46SAndreas Böhler        if ($returnonly) {
1165122f2d46SAndreas Böhler            return $this->_formatLink($link);
1166122f2d46SAndreas Böhler        } else {
1167a2d649c4Sandi            $this->doc .= $this->_formatLink($link);
11680cecf9d5Sandi        }
1169122f2d46SAndreas Böhler    }
11700cecf9d5Sandi
11713dd5c225SAndreas Gohr    /**
11723dd5c225SAndreas Gohr     * Render an internal media file
11733dd5c225SAndreas Gohr     *
11743dd5c225SAndreas Gohr     * @param string $src media ID
11753dd5c225SAndreas Gohr     * @param string $title descriptive text
11763dd5c225SAndreas Gohr     * @param string $align left|center|right
11773dd5c225SAndreas Gohr     * @param int $width width of media in pixel
11783dd5c225SAndreas Gohr     * @param int $height height of media in pixel
11793dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
11803dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
11813dd5c225SAndreas Gohr     * @param bool $return return HTML instead of adding to $doc
11820c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
11833dd5c225SAndreas Gohr     */
118495078f23SAndreas Gohr    public function internalmedia(
118595078f23SAndreas Gohr        $src,
118695078f23SAndreas Gohr        $title = null,
118795078f23SAndreas Gohr        $align = null,
118895078f23SAndreas Gohr        $width = null,
118995078f23SAndreas Gohr        $height = null,
119095078f23SAndreas Gohr        $cache = null,
119195078f23SAndreas Gohr        $linking = null,
119295078f23SAndreas Gohr        $return = false
119395078f23SAndreas Gohr    ) {
119437e34a5eSandi        global $ID;
11958f34cf3dSMichael Große        if (strpos($src, '#') !== false) {
1196faf3f01bSAndreas Gohr            [$src, $hash] = sexplode('#', $src, 2);
11978f34cf3dSMichael Große        }
11988c6be208SAndreas Gohr        $src = (new MediaResolver($ID))->resolveId($src, $this->date_at, true);
11998c6be208SAndreas Gohr        $exists = media_exists($src);
12000cecf9d5Sandi
1201d98d4540SBen Coburn        $noLink = false;
1202faf3f01bSAndreas Gohr        $render = $linking != 'linkonly';
1203b739ff0fSPierre Spring        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
12043685f775Sandi
1205faf3f01bSAndreas Gohr        [$ext, $mime] = mimetype($src, false);
1206b739ff0fSPierre Spring        if (substr($mime, 0, 5) == 'image' && $render) {
120764159a61SAndreas Gohr            $link['url'] = ml(
120864159a61SAndreas Gohr                $src,
1209faf3f01bSAndreas Gohr                [
121064159a61SAndreas Gohr                    'id' => $ID,
121164159a61SAndreas Gohr                    'cache' => $cache,
121264159a61SAndreas Gohr                    'rev' => $this->_getLastMediaRevisionAt($src)
1213faf3f01bSAndreas Gohr                ],
121464159a61SAndreas Gohr                ($linking == 'direct')
121564159a61SAndreas Gohr            );
1216f50634f0SAnika Henke        } elseif (($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
12172a2a2ba2SAnika Henke            // don't link movies
121844881bd0Shenning.noren            $noLink = true;
121955efc227SAndreas Gohr        } else {
12202ca14335SEsther Brunner            // add file icons
12219d2ddea4SAndreas Gohr            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
12229d2ddea4SAndreas Gohr            $link['class'] .= ' mediafile mf_' . $class;
122364159a61SAndreas Gohr            $link['url'] = ml(
122464159a61SAndreas Gohr                $src,
1225faf3f01bSAndreas Gohr                [
122664159a61SAndreas Gohr                    'id' => $ID,
122764159a61SAndreas Gohr                    'cache' => $cache,
122864159a61SAndreas Gohr                    'rev' => $this->_getLastMediaRevisionAt($src)
1229faf3f01bSAndreas Gohr                ],
123064159a61SAndreas Gohr                true
123164159a61SAndreas Gohr            );
123291328684SMichael Hamann            if ($exists) $link['title'] .= ' (' . filesize_h(filesize(mediaFN($src))) . ')';
123355efc227SAndreas Gohr        }
12343685f775Sandi
12358f34cf3dSMichael Große        if (!empty($hash)) $link['url'] .= '#' . $hash;
123691df343aSAndreas Gohr
12376fe20453SGina Haeussge        //markup non existing files
12384a24b459SKate Arzamastseva        if (!$exists) {
12396fe20453SGina Haeussge            $link['class'] .= ' wikilink2';
12404a24b459SKate Arzamastseva        }
12416fe20453SGina Haeussge
12423685f775Sandi        //output formatted
1243f50634f0SAnika Henke        if ($return) {
1244faf3f01bSAndreas Gohr            if ($linking == 'nolink' || $noLink) {
1245faf3f01bSAndreas Gohr                return $link['name'];
1246f50634f0SAnika Henke            } else {
1247faf3f01bSAndreas Gohr                return $this->_formatLink($link);
1248faf3f01bSAndreas Gohr            }
1249faf3f01bSAndreas Gohr        } elseif ($linking == 'nolink' || $noLink) {
1250faf3f01bSAndreas Gohr            $this->doc .= $link['name'];
1251faf3f01bSAndreas Gohr        } else {
1252faf3f01bSAndreas Gohr            $this->doc .= $this->_formatLink($link);
12530cecf9d5Sandi        }
1254f50634f0SAnika Henke    }
12550cecf9d5Sandi
12563dd5c225SAndreas Gohr    /**
12573dd5c225SAndreas Gohr     * Render an external media file
12583dd5c225SAndreas Gohr     *
12593dd5c225SAndreas Gohr     * @param string $src full media URL
12603dd5c225SAndreas Gohr     * @param string $title descriptive text
12613dd5c225SAndreas Gohr     * @param string $align left|center|right
12623dd5c225SAndreas Gohr     * @param int $width width of media in pixel
12633dd5c225SAndreas Gohr     * @param int $height height of media in pixel
12643dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
12653dd5c225SAndreas Gohr     * @param string $linking linkonly|detail|nolink
1266410ee62aSAnika Henke     * @param bool $return return HTML instead of adding to $doc
12670c4c0281SGerrit Uitslag     * @return void|string writes to doc attribute or returns html depends on $return
12683dd5c225SAndreas Gohr     */
126995078f23SAndreas Gohr    public function externalmedia(
127095078f23SAndreas Gohr        $src,
127195078f23SAndreas Gohr        $title = null,
127295078f23SAndreas Gohr        $align = null,
127395078f23SAndreas Gohr        $width = null,
127495078f23SAndreas Gohr        $height = null,
127595078f23SAndreas Gohr        $cache = null,
127695078f23SAndreas Gohr        $linking = null,
127795078f23SAndreas Gohr        $return = false
127895078f23SAndreas Gohr    ) {
12796efc45a2SDmitry Katsubo        if (link_isinterwiki($src)) {
1280faf3f01bSAndreas Gohr            [$shortcut, $reference] = sexplode('>', $src, 2, '');
12816efc45a2SDmitry Katsubo            $exists = null;
12826efc45a2SDmitry Katsubo            $src = $this->_resolveInterWiki($shortcut, $reference, $exists);
1283abde5980SPhy            if ($src == '' && empty($title)) {
1284abde5980SPhy                // make sure at least something will be shown in this case
1285abde5980SPhy                $title = $reference;
1286abde5980SPhy            }
12876efc45a2SDmitry Katsubo        }
1288faf3f01bSAndreas Gohr        [$src, $hash] = sexplode('#', $src, 2);
1289d98d4540SBen Coburn        $noLink = false;
1290abde5980SPhy        if ($src == '') {
1291abde5980SPhy            // only output plaintext without link if there is no src
1292abde5980SPhy            $noLink = true;
1293abde5980SPhy        }
1294faf3f01bSAndreas Gohr        $render = $linking != 'linkonly';
1295b739ff0fSPierre Spring        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
1296b739ff0fSPierre Spring
1297faf3f01bSAndreas Gohr        $link['url'] = ml($src, ['cache' => $cache]);
12983685f775Sandi
1299faf3f01bSAndreas Gohr        [$ext, $mime] = mimetype($src, false);
1300b739ff0fSPierre Spring        if (substr($mime, 0, 5) == 'image' && $render) {
13012ca14335SEsther Brunner            // link only jpeg images
130244881bd0Shenning.noren            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
1303f50634f0SAnika Henke        } elseif (($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
13042a2a2ba2SAnika Henke            // don't link movies
130544881bd0Shenning.noren            $noLink = true;
13062ca14335SEsther Brunner        } else {
13072ca14335SEsther Brunner            // add file icons
130827bf7924STom N Harris            $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
130927bf7924STom N Harris            $link['class'] .= ' mediafile mf_' . $class;
13102ca14335SEsther Brunner        }
13112ca14335SEsther Brunner
131291df343aSAndreas Gohr        if ($hash) $link['url'] .= '#' . $hash;
131391df343aSAndreas Gohr
13143685f775Sandi        //output formatted
1315410ee62aSAnika Henke        if ($return) {
1316410ee62aSAnika Henke            if ($linking == 'nolink' || $noLink) return $link['name'];
1317410ee62aSAnika Henke            else return $this->_formatLink($link);
1318faf3f01bSAndreas Gohr        } elseif ($linking == 'nolink' || $noLink) {
1319faf3f01bSAndreas Gohr            $this->doc .= $link['name'];
1320faf3f01bSAndreas Gohr        } else $this->doc .= $this->_formatLink($link);
1321410ee62aSAnika Henke    }
13220cecf9d5Sandi
13234826ab45Sandi    /**
13243db95becSAndreas Gohr     * Renders an RSS feed
1325b625487dSandi     *
13260c4c0281SGerrit Uitslag     * @param string $url URL of the feed
13270c4c0281SGerrit Uitslag     * @param array $params Finetuning of the output
13280c4c0281SGerrit Uitslag     *
1329b625487dSandi     * @author Andreas Gohr <andi@splitbrain.org>
1330b625487dSandi     */
1331faf3f01bSAndreas Gohr    public function rss($url, $params)
1332faf3f01bSAndreas Gohr    {
1333b625487dSandi        global $lang;
13343db95becSAndreas Gohr        global $conf;
13353db95becSAndreas Gohr
13363db95becSAndreas Gohr        require_once(DOKU_INC . 'inc/FeedParser.php');
13373db95becSAndreas Gohr        $feed = new FeedParser();
133800077af8SAndreas Gohr        $feed->set_feed_url($url);
1339b625487dSandi
1340b625487dSandi        //disable warning while fetching
13413dd5c225SAndreas Gohr        if (!defined('DOKU_E_LEVEL')) {
13423dd5c225SAndreas Gohr            $elvl = error_reporting(E_ERROR);
13433dd5c225SAndreas Gohr        }
13443db95becSAndreas Gohr        $rc = $feed->init();
13453dd5c225SAndreas Gohr        if (isset($elvl)) {
13463dd5c225SAndreas Gohr            error_reporting($elvl);
13473dd5c225SAndreas Gohr        }
1348b625487dSandi
134938c6f603SRobin H. Johnson        if ($params['nosort']) $feed->enable_order_by_date(false);
135038c6f603SRobin H. Johnson
13513db95becSAndreas Gohr        //decide on start and end
13523db95becSAndreas Gohr        if ($params['reverse']) {
13533db95becSAndreas Gohr            $mod = -1;
13543db95becSAndreas Gohr            $start = $feed->get_item_quantity() - 1;
13553db95becSAndreas Gohr            $end = $start - ($params['max']);
1356b2a412b0SAndreas Gohr            $end = ($end < -1) ? -1 : $end;
13573db95becSAndreas Gohr        } else {
13583db95becSAndreas Gohr            $mod = 1;
13593db95becSAndreas Gohr            $start = 0;
13603db95becSAndreas Gohr            $end = $feed->get_item_quantity();
1361d91ab76fSMatt Perry            $end = ($end > $params['max']) ? $params['max'] : $end;
13623db95becSAndreas Gohr        }
13633db95becSAndreas Gohr
1364a2d649c4Sandi        $this->doc .= '<ul class="rss">';
13653db95becSAndreas Gohr        if ($rc) {
13663db95becSAndreas Gohr            for ($x = $start; $x != $end; $x += $mod) {
13671bde1582SAndreas Gohr                $item = $feed->get_item($x);
13683db95becSAndreas Gohr                $this->doc .= '<li><div class="li">';
136953df38b0SAndreas Gohr
1370d2ea3363SAndreas Gohr                $lnkurl = $item->get_permalink();
137153df38b0SAndreas Gohr                $title = html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8');
137253df38b0SAndreas Gohr
137353df38b0SAndreas Gohr                // support feeds without links
1374d2ea3363SAndreas Gohr                if ($lnkurl) {
137553df38b0SAndreas Gohr                    $this->externallink($item->get_permalink(), $title);
1376d2ea3363SAndreas Gohr                } else {
137753df38b0SAndreas Gohr                    $this->doc .= ' ' . hsc($item->get_title());
1378d2ea3363SAndreas Gohr                }
13793db95becSAndreas Gohr                if ($params['author']) {
13801bde1582SAndreas Gohr                    $author = $item->get_author(0);
1381faf3f01bSAndreas Gohr                    if ($author instanceof Author) {
13821bde1582SAndreas Gohr                        $name = $author->get_name();
13831bde1582SAndreas Gohr                        if (!$name) $name = $author->get_email();
1384163c2842SPhy                        if ($name) $this->doc .= ' ' . $lang['by'] . ' ' . hsc($name);
13851bde1582SAndreas Gohr                    }
13863db95becSAndreas Gohr                }
13873db95becSAndreas Gohr                if ($params['date']) {
13882e7e0c29SAndreas Gohr                    $this->doc .= ' (' . $item->get_local_date($conf['dformat']) . ')';
13893db95becSAndreas Gohr                }
13901bde1582SAndreas Gohr                if ($params['details']) {
139153df38b0SAndreas Gohr                    $desc = $item->get_description();
139253df38b0SAndreas Gohr                    $desc = strip_tags($desc);
139353df38b0SAndreas Gohr                    $desc = html_entity_decode($desc, ENT_QUOTES, 'UTF-8');
13943db95becSAndreas Gohr                    $this->doc .= '<div class="detail">';
139553df38b0SAndreas Gohr                    $this->doc .= hsc($desc);
13963db95becSAndreas Gohr                    $this->doc .= '</div>';
13973db95becSAndreas Gohr                }
13983db95becSAndreas Gohr
13993db95becSAndreas Gohr                $this->doc .= '</div></li>';
1400b625487dSandi            }
1401b625487dSandi        } else {
14023db95becSAndreas Gohr            $this->doc .= '<li><div class="li">';
1403a2d649c4Sandi            $this->doc .= '<em>' . $lang['rssfailed'] . '</em>';
1404b625487dSandi            $this->externallink($url);
140545e147ccSAndreas Gohr            if ($conf['allowdebug']) {
140645e147ccSAndreas Gohr                $this->doc .= '<!--' . hsc($feed->error) . '-->';
140745e147ccSAndreas Gohr            }
14083db95becSAndreas Gohr            $this->doc .= '</div></li>';
1409b625487dSandi        }
1410a2d649c4Sandi        $this->doc .= '</ul>';
1411b625487dSandi    }
1412b625487dSandi
14133dd5c225SAndreas Gohr    /**
14143dd5c225SAndreas Gohr     * Start a table
14153dd5c225SAndreas Gohr     *
14163dd5c225SAndreas Gohr     * @param int $maxcols maximum number of columns
14173dd5c225SAndreas Gohr     * @param int $numrows NOT IMPLEMENTED
14183dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
14197d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
14203dd5c225SAndreas Gohr     */
1421faf3f01bSAndreas Gohr    public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null)
1422faf3f01bSAndreas Gohr    {
1423b5742cedSPierre Spring        // initialize the row counter used for classes
1424b5742cedSPierre Spring        $this->_counter['row_counter'] = 0;
1425619736fdSAdrian Lang        $class = 'table';
14260c4c0281SGerrit Uitslag        if ($classes !== null) {
1427faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
14280c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
14290c4c0281SGerrit Uitslag        }
1430619736fdSAdrian Lang        if ($pos !== null) {
143106917fceSMichael Große            $hid = $this->_headerToLink($class, true);
1432faf3f01bSAndreas Gohr            $data = [];
1433ec57f119SLarsDW223            $data['target'] = 'table';
1434ec57f119SLarsDW223            $data['name'] = '';
1435ec57f119SLarsDW223            $data['hid'] = $hid;
1436ec57f119SLarsDW223            $class .= ' ' . $this->startSectionEdit($pos, $data);
1437619736fdSAdrian Lang        }
1438619736fdSAdrian Lang        $this->doc .= '<div class="' . $class . '"><table class="inline">' .
1439619736fdSAdrian Lang            DOKU_LF;
14400cecf9d5Sandi    }
14410cecf9d5Sandi
14423dd5c225SAndreas Gohr    /**
14433dd5c225SAndreas Gohr     * Close a table
14443dd5c225SAndreas Gohr     *
14453dd5c225SAndreas Gohr     * @param int $pos byte position in the original source
14463dd5c225SAndreas Gohr     */
1447faf3f01bSAndreas Gohr    public function table_close($pos = null)
1448faf3f01bSAndreas Gohr    {
1449a8574918SAnika Henke        $this->doc .= '</table></div>' . DOKU_LF;
1450619736fdSAdrian Lang        if ($pos !== null) {
145190df9a4dSAdrian Lang            $this->finishSectionEdit($pos);
14520cecf9d5Sandi        }
1453619736fdSAdrian Lang    }
14540cecf9d5Sandi
14553dd5c225SAndreas Gohr    /**
14563dd5c225SAndreas Gohr     * Open a table header
14573dd5c225SAndreas Gohr     */
1458faf3f01bSAndreas Gohr    public function tablethead_open()
1459faf3f01bSAndreas Gohr    {
1460f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB . '<thead>' . DOKU_LF;
1461f05a1cc5SGerrit Uitslag    }
1462f05a1cc5SGerrit Uitslag
14633dd5c225SAndreas Gohr    /**
14643dd5c225SAndreas Gohr     * Close a table header
14653dd5c225SAndreas Gohr     */
1466faf3f01bSAndreas Gohr    public function tablethead_close()
1467faf3f01bSAndreas Gohr    {
1468f05a1cc5SGerrit Uitslag        $this->doc .= DOKU_TAB . '</thead>' . DOKU_LF;
1469f05a1cc5SGerrit Uitslag    }
1470f05a1cc5SGerrit Uitslag
14713dd5c225SAndreas Gohr    /**
14725a93f869SAnika Henke     * Open a table body
14735a93f869SAnika Henke     */
1474faf3f01bSAndreas Gohr    public function tabletbody_open()
1475faf3f01bSAndreas Gohr    {
14765a93f869SAnika Henke        $this->doc .= DOKU_TAB . '<tbody>' . DOKU_LF;
14775a93f869SAnika Henke    }
14785a93f869SAnika Henke
14795a93f869SAnika Henke    /**
14805a93f869SAnika Henke     * Close a table body
14815a93f869SAnika Henke     */
1482faf3f01bSAndreas Gohr    public function tabletbody_close()
1483faf3f01bSAndreas Gohr    {
14845a93f869SAnika Henke        $this->doc .= DOKU_TAB . '</tbody>' . DOKU_LF;
14855a93f869SAnika Henke    }
14865a93f869SAnika Henke
14875a93f869SAnika Henke    /**
1488d2a99739SAndreas Gohr     * Open a table footer
1489d2a99739SAndreas Gohr     */
1490faf3f01bSAndreas Gohr    public function tabletfoot_open()
1491faf3f01bSAndreas Gohr    {
149244f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB . '<tfoot>' . DOKU_LF;
1493d2a99739SAndreas Gohr    }
1494d2a99739SAndreas Gohr
1495d2a99739SAndreas Gohr    /**
1496d2a99739SAndreas Gohr     * Close a table footer
1497d2a99739SAndreas Gohr     */
1498faf3f01bSAndreas Gohr    public function tabletfoot_close()
1499faf3f01bSAndreas Gohr    {
150044f5d1c1SAndreas Gohr        $this->doc .= DOKU_TAB . '</tfoot>' . DOKU_LF;
1501d2a99739SAndreas Gohr    }
1502d2a99739SAndreas Gohr
1503d2a99739SAndreas Gohr    /**
15043dd5c225SAndreas Gohr     * Open a table row
15050c4c0281SGerrit Uitslag     *
15067d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
15073dd5c225SAndreas Gohr     */
1508faf3f01bSAndreas Gohr    public function tablerow_open($classes = null)
1509faf3f01bSAndreas Gohr    {
1510b5742cedSPierre Spring        // initialize the cell counter used for classes
1511b5742cedSPierre Spring        $this->_counter['cell_counter'] = 0;
1512b5742cedSPierre Spring        $class = 'row' . $this->_counter['row_counter']++;
15130c4c0281SGerrit Uitslag        if ($classes !== null) {
1514faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
15150c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
15160c4c0281SGerrit Uitslag        }
1517b5742cedSPierre Spring        $this->doc .= DOKU_TAB . '<tr class="' . $class . '">' . DOKU_LF . DOKU_TAB . DOKU_TAB;
15180cecf9d5Sandi    }
15190cecf9d5Sandi
15203dd5c225SAndreas Gohr    /**
15213dd5c225SAndreas Gohr     * Close a table row
15223dd5c225SAndreas Gohr     */
1523faf3f01bSAndreas Gohr    public function tablerow_close()
1524faf3f01bSAndreas Gohr    {
1525a2d649c4Sandi        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
15260cecf9d5Sandi    }
15270cecf9d5Sandi
15283dd5c225SAndreas Gohr    /**
15293dd5c225SAndreas Gohr     * Open a table header cell
15303dd5c225SAndreas Gohr     *
15313dd5c225SAndreas Gohr     * @param int $colspan
15323dd5c225SAndreas Gohr     * @param string $align left|center|right
15333dd5c225SAndreas Gohr     * @param int $rowspan
15347d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
15353dd5c225SAndreas Gohr     */
1536faf3f01bSAndreas Gohr    public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null)
1537faf3f01bSAndreas Gohr    {
1538b5742cedSPierre Spring        $class = 'class="col' . $this->_counter['cell_counter']++;
15390cecf9d5Sandi        if (!is_null($align)) {
1540b5742cedSPierre Spring            $class .= ' ' . $align . 'align';
15410cecf9d5Sandi        }
15420c4c0281SGerrit Uitslag        if ($classes !== null) {
1543faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
15440c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
15450c4c0281SGerrit Uitslag        }
1546b5742cedSPierre Spring        $class .= '"';
1547b5742cedSPierre Spring        $this->doc .= '<th ' . $class;
15480cecf9d5Sandi        if ($colspan > 1) {
1549a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1550a2d649c4Sandi            $this->doc .= ' colspan="' . $colspan . '"';
15510cecf9d5Sandi        }
155225b97867Shakan.sandell        if ($rowspan > 1) {
155325b97867Shakan.sandell            $this->doc .= ' rowspan="' . $rowspan . '"';
155425b97867Shakan.sandell        }
1555a2d649c4Sandi        $this->doc .= '>';
15560cecf9d5Sandi    }
15570cecf9d5Sandi
15583dd5c225SAndreas Gohr    /**
15593dd5c225SAndreas Gohr     * Close a table header cell
15603dd5c225SAndreas Gohr     */
1561faf3f01bSAndreas Gohr    public function tableheader_close()
1562faf3f01bSAndreas Gohr    {
1563a2d649c4Sandi        $this->doc .= '</th>';
15640cecf9d5Sandi    }
15650cecf9d5Sandi
15663dd5c225SAndreas Gohr    /**
15673dd5c225SAndreas Gohr     * Open a table cell
15683dd5c225SAndreas Gohr     *
15693dd5c225SAndreas Gohr     * @param int $colspan
15703dd5c225SAndreas Gohr     * @param string $align left|center|right
15713dd5c225SAndreas Gohr     * @param int $rowspan
15727d769f75SAndreas Gohr     * @param string|string[] $classes css classes - have to be valid, do not pass unfiltered user input
15733dd5c225SAndreas Gohr     */
1574faf3f01bSAndreas Gohr    public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null)
1575faf3f01bSAndreas Gohr    {
1576b5742cedSPierre Spring        $class = 'class="col' . $this->_counter['cell_counter']++;
15770cecf9d5Sandi        if (!is_null($align)) {
1578b5742cedSPierre Spring            $class .= ' ' . $align . 'align';
15790cecf9d5Sandi        }
15800c4c0281SGerrit Uitslag        if ($classes !== null) {
1581faf3f01bSAndreas Gohr            if (is_array($classes)) $classes = implode(' ', $classes);
15820c4c0281SGerrit Uitslag            $class .= ' ' . $classes;
15830c4c0281SGerrit Uitslag        }
1584b5742cedSPierre Spring        $class .= '"';
1585b5742cedSPierre Spring        $this->doc .= '<td ' . $class;
15860cecf9d5Sandi        if ($colspan > 1) {
1587a28fd914SAndreas Gohr            $this->_counter['cell_counter'] += $colspan - 1;
1588a2d649c4Sandi            $this->doc .= ' colspan="' . $colspan . '"';
15890cecf9d5Sandi        }
159025b97867Shakan.sandell        if ($rowspan > 1) {
159125b97867Shakan.sandell            $this->doc .= ' rowspan="' . $rowspan . '"';
159225b97867Shakan.sandell        }
1593a2d649c4Sandi        $this->doc .= '>';
15940cecf9d5Sandi    }
15950cecf9d5Sandi
15963dd5c225SAndreas Gohr    /**
15973dd5c225SAndreas Gohr     * Close a table cell
15983dd5c225SAndreas Gohr     */
1599faf3f01bSAndreas Gohr    public function tablecell_close()
1600faf3f01bSAndreas Gohr    {
1601a2d649c4Sandi        $this->doc .= '</td>';
16020cecf9d5Sandi    }
16030cecf9d5Sandi
1604cea664bdSLarsDW223    /**
1605cea664bdSLarsDW223     * Returns the current header level.
1606cea664bdSLarsDW223     * (required e.g. by the filelist plugin)
1607cea664bdSLarsDW223     *
1608cea664bdSLarsDW223     * @return int The current header level
1609cea664bdSLarsDW223     */
1610faf3f01bSAndreas Gohr    public function getLastlevel()
1611faf3f01bSAndreas Gohr    {
1612cea664bdSLarsDW223        return $this->lastlevel;
1613cea664bdSLarsDW223    }
1614cea664bdSLarsDW223
16153dd5c225SAndreas Gohr    #region Utility functions
16160cecf9d5Sandi
1617ba11bd29Sandi    /**
16183fd0b676Sandi     * Build a link
16193fd0b676Sandi     *
16203fd0b676Sandi     * Assembles all parts defined in $link returns HTML for the link
1621ba11bd29Sandi     *
16220c4c0281SGerrit Uitslag     * @param array $link attributes of a link
16230c4c0281SGerrit Uitslag     * @return string
16240c4c0281SGerrit Uitslag     *
1625ba11bd29Sandi     * @author Andreas Gohr <andi@splitbrain.org>
1626ba11bd29Sandi     */
1627faf3f01bSAndreas Gohr    public function _formatLink($link)
1628faf3f01bSAndreas Gohr    {
1629ba11bd29Sandi        //make sure the url is XHTML compliant (skip mailto)
1630ba11bd29Sandi        if (substr($link['url'], 0, 7) != 'mailto:') {
1631ba11bd29Sandi            $link['url'] = str_replace('&', '&amp;', $link['url']);
1632ba11bd29Sandi            $link['url'] = str_replace('&amp;amp;', '&amp;', $link['url']);
1633ba11bd29Sandi        }
1634ba11bd29Sandi        //remove double encodings in titles
1635ba11bd29Sandi        $link['title'] = str_replace('&amp;amp;', '&amp;', $link['title']);
1636ba11bd29Sandi
1637453493f2SAndreas Gohr        // be sure there are no bad chars in url or title
1638453493f2SAndreas Gohr        // (we can't do this for name because it can contain an img tag)
1639faf3f01bSAndreas Gohr        $link['url'] = strtr($link['url'], ['>' => '%3E', '<' => '%3C', '"' => '%22']);
1640faf3f01bSAndreas Gohr        $link['title'] = strtr($link['title'], ['>' => '&gt;', '<' => '&lt;', '"' => '&quot;']);
1641453493f2SAndreas Gohr
1642ba11bd29Sandi        $ret = '';
1643ba11bd29Sandi        $ret .= $link['pre'];
1644ba11bd29Sandi        $ret .= '<a href="' . $link['url'] . '"';
1645bb4866bdSchris        if (!empty($link['class'])) $ret .= ' class="' . $link['class'] . '"';
1646bb4866bdSchris        if (!empty($link['target'])) $ret .= ' target="' . $link['target'] . '"';
1647bb4866bdSchris        if (!empty($link['title'])) $ret .= ' title="' . $link['title'] . '"';
1648bb4866bdSchris        if (!empty($link['style'])) $ret .= ' style="' . $link['style'] . '"';
1649914045f3SAndreas Gohr        if (!empty($link['rel'])) $ret .= ' rel="' . trim($link['rel']) . '"';
1650bb4866bdSchris        if (!empty($link['more'])) $ret .= ' ' . $link['more'];
1651ba11bd29Sandi        $ret .= '>';
1652ba11bd29Sandi        $ret .= $link['name'];
1653ba11bd29Sandi        $ret .= '</a>';
1654ba11bd29Sandi        $ret .= $link['suf'];
1655ba11bd29Sandi        return $ret;
1656ba11bd29Sandi    }
1657ba11bd29Sandi
1658ba11bd29Sandi    /**
16593fd0b676Sandi     * Renders internal and external media
16603fd0b676Sandi     *
16613dd5c225SAndreas Gohr     * @param string $src media ID
16623dd5c225SAndreas Gohr     * @param string $title descriptive text
16633dd5c225SAndreas Gohr     * @param string $align left|center|right
16643dd5c225SAndreas Gohr     * @param int $width width of media in pixel
16653dd5c225SAndreas Gohr     * @param int $height height of media in pixel
16663dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
16673dd5c225SAndreas Gohr     * @param bool $render should the media be embedded inline or just linked
16683dd5c225SAndreas Gohr     * @return string
1669faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
16703fd0b676Sandi     */
167195078f23SAndreas Gohr    public function _media(
167295078f23SAndreas Gohr        $src,
167395078f23SAndreas Gohr        $title = null,
167495078f23SAndreas Gohr        $align = null,
167595078f23SAndreas Gohr        $width = null,
167695078f23SAndreas Gohr        $height = null,
167795078f23SAndreas Gohr        $cache = null,
167895078f23SAndreas Gohr        $render = true
167995078f23SAndreas Gohr    ) {
16803fd0b676Sandi
16813fd0b676Sandi        $ret = '';
16823fd0b676Sandi
1683faf3f01bSAndreas Gohr        [$ext, $mime] = mimetype($src);
16843fd0b676Sandi        if (substr($mime, 0, 5) == 'image') {
1685b739ff0fSPierre Spring            // first get the $title
1686b739ff0fSPierre Spring            if (!is_null($title)) {
1687b739ff0fSPierre Spring                $title = $this->_xmlEntities($title);
1688b739ff0fSPierre Spring            } elseif ($ext == 'jpg' || $ext == 'jpeg') {
1689b739ff0fSPierre Spring                //try to use the caption from IPTC/EXIF
1690b739ff0fSPierre Spring                require_once(DOKU_INC . 'inc/JpegMeta.php');
169167f9913dSAndreas Gohr                $jpeg = new JpegMeta(mediaFN($src));
1692faf3f01bSAndreas Gohr                $cap = $jpeg->getTitle();
16933dd5c225SAndreas Gohr                if (!empty($cap)) {
1694b739ff0fSPierre Spring                    $title = $this->_xmlEntities($cap);
1695b739ff0fSPierre Spring                }
1696b739ff0fSPierre Spring            }
1697b739ff0fSPierre Spring            if (!$render) {
1698b739ff0fSPierre Spring                // if the picture is not supposed to be rendered
1699b739ff0fSPierre Spring                // return the title of the picture
1700768be5a3SPhy                if ($title === null || $title === "") {
1701b739ff0fSPierre Spring                    // just show the sourcename
1702faf3f01bSAndreas Gohr                    $title = $this->_xmlEntities(PhpString::basename(noNS($src)));
1703b739ff0fSPierre Spring                }
1704b739ff0fSPierre Spring                return $title;
1705b739ff0fSPierre Spring            }
17063fd0b676Sandi            //add image tag
170764159a61SAndreas Gohr            $ret .= '<img src="' . ml(
170864159a61SAndreas Gohr                $src,
1709faf3f01bSAndreas Gohr                [
1710faf3f01bSAndreas Gohr                        'w' => $width,
1711faf3f01bSAndreas Gohr                        'h' => $height,
171264159a61SAndreas Gohr                        'cache' => $cache,
171364159a61SAndreas Gohr                        'rev' => $this->_getLastMediaRevisionAt($src)
1714faf3f01bSAndreas Gohr                    ]
171564159a61SAndreas Gohr            ) . '"';
17163fd0b676Sandi            $ret .= ' class="media' . $align . '"';
17174732b197SAndreas Gohr            $ret .= ' loading="lazy"';
17183fd0b676Sandi
1719b739ff0fSPierre Spring            if ($title) {
1720b739ff0fSPierre Spring                $ret .= ' title="' . $title . '"';
1721b739ff0fSPierre Spring                $ret .= ' alt="' . $title . '"';
17223fd0b676Sandi            } else {
17233fd0b676Sandi                $ret .= ' alt=""';
17243fd0b676Sandi            }
17253fd0b676Sandi
1726*749bc7f1SAndreas Gohr            if (!is_null($width)) {
17273fd0b676Sandi                $ret .= ' width="' . $this->_xmlEntities($width) . '"';
1728*749bc7f1SAndreas Gohr            }
17293fd0b676Sandi
1730*749bc7f1SAndreas Gohr            if (!is_null($height)) {
17313fd0b676Sandi                $ret .= ' height="' . $this->_xmlEntities($height) . '"';
1732*749bc7f1SAndreas Gohr            }
17333fd0b676Sandi
17343fd0b676Sandi            $ret .= ' />';
173517954bb5SAnika Henke        } elseif (media_supportedav($mime, 'video') || media_supportedav($mime, 'audio')) {
17362a2a2ba2SAnika Henke            // first get the $title
1737*749bc7f1SAndreas Gohr            $title ??= false;
17382a2a2ba2SAnika Henke            if (!$render) {
173917954bb5SAnika Henke                // if the file is not supposed to be rendered
174017954bb5SAnika Henke                // return the title of the file (just the sourcename if there is no title)
1741faf3f01bSAndreas Gohr                return $this->_xmlEntities($title ?: PhpString::basename(noNS($src)));
17422a2a2ba2SAnika Henke            }
17432a2a2ba2SAnika Henke
1744faf3f01bSAndreas Gohr            $att = [];
17452a2a2ba2SAnika Henke            $att['class'] = "media$align";
174617954bb5SAnika Henke            if ($title) {
174717954bb5SAnika Henke                $att['title'] = $title;
174817954bb5SAnika Henke            }
17492a2a2ba2SAnika Henke
175017954bb5SAnika Henke            if (media_supportedav($mime, 'video')) {
175117954bb5SAnika Henke                //add video
175279e53fe5SAnika Henke                $ret .= $this->_video($src, $width, $height, $att);
1753b44a5dceSAnika Henke            }
175417954bb5SAnika Henke            if (media_supportedav($mime, 'audio')) {
1755b44a5dceSAnika Henke                //add audio
1756b44a5dceSAnika Henke                $ret .= $this->_audio($src, $att);
175717954bb5SAnika Henke            }
17583fd0b676Sandi        } elseif ($mime == 'application/x-shockwave-flash') {
17591c882ba8SAndreas Gohr            if (!$render) {
17601c882ba8SAndreas Gohr                // if the flash is not supposed to be rendered
17611c882ba8SAndreas Gohr                // return the title of the flash
17621c882ba8SAndreas Gohr                if (!$title) {
17631c882ba8SAndreas Gohr                    // just show the sourcename
1764faf3f01bSAndreas Gohr                    $title = PhpString::basename(noNS($src));
17651c882ba8SAndreas Gohr                }
176607bf32b2SAndreas Gohr                return $this->_xmlEntities($title);
17671c882ba8SAndreas Gohr            }
17681c882ba8SAndreas Gohr
1769faf3f01bSAndreas Gohr            $att = [];
177007bf32b2SAndreas Gohr            $att['class'] = "media$align";
177107bf32b2SAndreas Gohr            if ($align == 'right') $att['align'] = 'right';
177207bf32b2SAndreas Gohr            if ($align == 'left') $att['align'] = 'left';
17733dd5c225SAndreas Gohr            $ret .= html_flashobject(
177495078f23SAndreas Gohr                ml($src, ['cache' => $cache], true, '&'),
177595078f23SAndreas Gohr                $width,
177695078f23SAndreas Gohr                $height,
1777faf3f01bSAndreas Gohr                ['quality' => 'high'],
177807bf32b2SAndreas Gohr                null,
177907bf32b2SAndreas Gohr                $att,
17803dd5c225SAndreas Gohr                $this->_xmlEntities($title)
17813dd5c225SAndreas Gohr            );
17820f428d7dSAndreas Gohr        } elseif ($title) {
17833fd0b676Sandi            // well at least we have a title to display
17843fd0b676Sandi            $ret .= $this->_xmlEntities($title);
17853fd0b676Sandi        } else {
17865291ca3aSAndreas Gohr            // just show the sourcename
1787faf3f01bSAndreas Gohr            $ret .= $this->_xmlEntities(PhpString::basename(noNS($src)));
17883fd0b676Sandi        }
17893fd0b676Sandi
17903fd0b676Sandi        return $ret;
17913fd0b676Sandi    }
17923fd0b676Sandi
17933dd5c225SAndreas Gohr    /**
17943dd5c225SAndreas Gohr     * Escape string for output
17953dd5c225SAndreas Gohr     *
17963dd5c225SAndreas Gohr     * @param $string
17973dd5c225SAndreas Gohr     * @return string
17983dd5c225SAndreas Gohr     */
1799faf3f01bSAndreas Gohr    public function _xmlEntities($string)
1800faf3f01bSAndreas Gohr    {
1801f7711f2bSAndreas Gohr        return hsc($string);
18020cecf9d5Sandi    }
18030cecf9d5Sandi
1804de369923SAndreas Gohr
1805af587fa8Sandi    /**
18063fd0b676Sandi     * Construct a title and handle images in titles
18073fd0b676Sandi     *
18083dd5c225SAndreas Gohr     * @param string|array $title either string title or media array
18093dd5c225SAndreas Gohr     * @param string $default default title if nothing else is found
18103dd5c225SAndreas Gohr     * @param bool $isImage will be set to true if it's a media file
18113dd5c225SAndreas Gohr     * @param null|string $id linked page id (used to extract title from first heading)
18123dd5c225SAndreas Gohr     * @param string $linktype content|navigation
18133dd5c225SAndreas Gohr     * @return string      HTML of the title, might be full image tag or just escaped text
1814faf3f01bSAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
18153fd0b676Sandi     */
1816faf3f01bSAndreas Gohr    public function _getLinkTitle($title, $default, &$isImage, $id = null, $linktype = 'content')
1817faf3f01bSAndreas Gohr    {
181844881bd0Shenning.noren        $isImage = false;
181929657f9eSAndreas Gohr        if (is_array($title)) {
182029657f9eSAndreas Gohr            $isImage = true;
182129657f9eSAndreas Gohr            return $this->_imageTitle($title);
182229657f9eSAndreas Gohr        } elseif (is_null($title) || trim($title) == '') {
1823fe9ec250SChris Smith            if (useHeading($linktype) && $id) {
182467c15eceSMichael Hamann                $heading = p_get_first_heading($id);
1825f515db7fSAndreas Gohr                if (!blank($heading)) {
1826433bef32Sandi                    return $this->_xmlEntities($heading);
1827bb0a59d4Sjan                }
1828bb0a59d4Sjan            }
1829433bef32Sandi            return $this->_xmlEntities($default);
183068c26e6dSMichael Klier        } else {
183168c26e6dSMichael Klier            return $this->_xmlEntities($title);
18320cecf9d5Sandi        }
18330cecf9d5Sandi    }
18340cecf9d5Sandi
18350cecf9d5Sandi    /**
18363dd5c225SAndreas Gohr     * Returns HTML code for images used in link titles
18373fd0b676Sandi     *
1838e0c26282SGerrit Uitslag     * @param array $img
18393dd5c225SAndreas Gohr     * @return string HTML img tag or similar
1840faf3f01bSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
18410cecf9d5Sandi     */
1842faf3f01bSAndreas Gohr    public function _imageTitle($img)
1843faf3f01bSAndreas Gohr    {
1844d9baf1a7SKazutaka Miyasaka        global $ID;
1845d9baf1a7SKazutaka Miyasaka
1846d9baf1a7SKazutaka Miyasaka        // some fixes on $img['src']
1847d9baf1a7SKazutaka Miyasaka        // see internalmedia() and externalmedia()
1848faf3f01bSAndreas Gohr        [$img['src']] = explode('#', $img['src'], 2);
1849d9baf1a7SKazutaka Miyasaka        if ($img['type'] == 'internalmedia') {
18508c6be208SAndreas Gohr            $img['src'] = (new MediaResolver($ID))->resolveId($img['src'], $this->date_at, true);
1851d9baf1a7SKazutaka Miyasaka        }
1852d9baf1a7SKazutaka Miyasaka
18533dd5c225SAndreas Gohr        return $this->_media(
18543dd5c225SAndreas Gohr            $img['src'],
18554826ab45Sandi            $img['title'],
18564826ab45Sandi            $img['align'],
18574826ab45Sandi            $img['width'],
18584826ab45Sandi            $img['height'],
18593dd5c225SAndreas Gohr            $img['cache']
18603dd5c225SAndreas Gohr        );
18610cecf9d5Sandi    }
1862b739ff0fSPierre Spring
1863b739ff0fSPierre Spring    /**
18643dd5c225SAndreas Gohr     * helperfunction to return a basic link to a media
18653dd5c225SAndreas Gohr     *
18663dd5c225SAndreas Gohr     * used in internalmedia() and externalmedia()
1867b739ff0fSPierre Spring     *
18683dd5c225SAndreas Gohr     * @param string $src media ID
18693dd5c225SAndreas Gohr     * @param string $title descriptive text
18703dd5c225SAndreas Gohr     * @param string $align left|center|right
18713dd5c225SAndreas Gohr     * @param int $width width of media in pixel
18723dd5c225SAndreas Gohr     * @param int $height height of media in pixel
18733dd5c225SAndreas Gohr     * @param string $cache cache|recache|nocache
18743dd5c225SAndreas Gohr     * @param bool $render should the media be embedded inline or just linked
18753dd5c225SAndreas Gohr     * @return array associative array with link config
1876faf3f01bSAndreas Gohr     * @author   Pierre Spring <pierre.spring@liip.ch>
1877b739ff0fSPierre Spring     */
1878faf3f01bSAndreas Gohr    public function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render)
1879faf3f01bSAndreas Gohr    {
1880b739ff0fSPierre Spring        global $conf;
1881b739ff0fSPierre Spring
1882faf3f01bSAndreas Gohr        $link = [];
1883b739ff0fSPierre Spring        $link['class'] = 'media';
1884b739ff0fSPierre Spring        $link['style'] = '';
1885b739ff0fSPierre Spring        $link['pre'] = '';
1886b739ff0fSPierre Spring        $link['suf'] = '';
1887b739ff0fSPierre Spring        $link['more'] = '';
1888b739ff0fSPierre Spring        $link['target'] = $conf['target']['media'];
1889bc3d2252SAndreas Gohr        if ($conf['target']['media']) $link['rel'] = 'noopener';
1890b739ff0fSPierre Spring        $link['title'] = $this->_xmlEntities($src);
1891b739ff0fSPierre Spring        $link['name'] = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1892b739ff0fSPierre Spring
1893b739ff0fSPierre Spring        return $link;
1894b739ff0fSPierre Spring    }
189591459163SAnika Henke
18962a2a2ba2SAnika Henke    /**
18972a2a2ba2SAnika Henke     * Embed video(s) in HTML
18982a2a2ba2SAnika Henke     *
18992a2a2ba2SAnika Henke     * @param string $src - ID of video to embed
19002a2a2ba2SAnika Henke     * @param int $width - width of the video in pixels
19012a2a2ba2SAnika Henke     * @param int $height - height of the video in pixels
19022a2a2ba2SAnika Henke     * @param array $atts - additional attributes for the <video> tag
1903f50634f0SAnika Henke     * @return string
1904faf3f01bSAndreas Gohr     * @author Schplurtz le Déboulonné <Schplurtz@laposte.net>
1905faf3f01bSAndreas Gohr     *
1906faf3f01bSAndreas Gohr     * @author Anika Henke <anika@selfthinker.org>
19072a2a2ba2SAnika Henke     */
1908faf3f01bSAndreas Gohr    public function _video($src, $width, $height, $atts = null)
1909faf3f01bSAndreas Gohr    {
19102a2a2ba2SAnika Henke        // prepare width and height
1911faf3f01bSAndreas Gohr        if (is_null($atts)) $atts = [];
19122a2a2ba2SAnika Henke        $atts['width'] = (int)$width;
19132a2a2ba2SAnika Henke        $atts['height'] = (int)$height;
19142a2a2ba2SAnika Henke        if (!$atts['width']) $atts['width'] = 320;
19152a2a2ba2SAnika Henke        if (!$atts['height']) $atts['height'] = 240;
19162a2a2ba2SAnika Henke
1917410ee62aSAnika Henke        $posterUrl = '';
1918faf3f01bSAndreas Gohr        $files = [];
1919faf3f01bSAndreas Gohr        $tracks = [];
1920410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1921410ee62aSAnika Henke
1922410ee62aSAnika Henke        if ($isExternal) {
1923410ee62aSAnika Henke            // take direct source for external files
1924a19c9aa0SGerrit Uitslag            [/* ext */, $srcMime] = mimetype($src);
1925410ee62aSAnika Henke            $files[$srcMime] = $src;
1926410ee62aSAnika Henke        } else {
19273d7a9e0aSAnika Henke            // prepare alternative formats
1928faf3f01bSAndreas Gohr            $extensions = ['webm', 'ogv', 'mp4'];
1929410ee62aSAnika Henke            $files = media_alternativefiles($src, $extensions);
1930faf3f01bSAndreas Gohr            $poster = media_alternativefiles($src, ['jpg', 'png']);
19310877a1f1SSchplurtz le Déboulonné            $tracks = media_trackfiles($src);
193299f943f6SAnika Henke            if (!empty($poster)) {
19332d338eabSAndreas Gohr                $posterUrl = ml(reset($poster), '', true, '&');
193499f943f6SAnika Henke            }
1935410ee62aSAnika Henke        }
19362a2a2ba2SAnika Henke
1937f50634f0SAnika Henke        $out = '';
193879e53fe5SAnika Henke        // open video tag
1939f50634f0SAnika Henke        $out .= '<video ' . buildAttributes($atts) . ' controls="controls"';
19403641199aSAnika Henke        if ($posterUrl) $out .= ' poster="' . hsc($posterUrl) . '"';
1941f50634f0SAnika Henke        $out .= '>' . NL;
19423641199aSAnika Henke        $fallback = '';
194379e53fe5SAnika Henke
194479e53fe5SAnika Henke        // output source for each alternative video format
1945410ee62aSAnika Henke        foreach ($files as $mime => $file) {
1946410ee62aSAnika Henke            if ($isExternal) {
1947410ee62aSAnika Henke                $url = $file;
1948410ee62aSAnika Henke                $linkType = 'externalmedia';
1949410ee62aSAnika Henke            } else {
19502d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
1951410ee62aSAnika Henke                $linkType = 'internalmedia';
1952410ee62aSAnika Henke            }
1953faf3f01bSAndreas Gohr            $title = empty($atts['title'])
1954faf3f01bSAndreas Gohr                ? $this->_xmlEntities(PhpString::basename(noNS($file)))
1955faf3f01bSAndreas Gohr                : $atts['title'];
19563d7a9e0aSAnika Henke
1957f50634f0SAnika Henke            $out .= '<source src="' . hsc($url) . '" type="' . $mime . '" />' . NL;
195879e53fe5SAnika Henke            // alternative content (just a link to the file)
195964159a61SAndreas Gohr            $fallback .= $this->$linkType(
196064159a61SAndreas Gohr                $file,
196164159a61SAndreas Gohr                $title,
196264159a61SAndreas Gohr                null,
196364159a61SAndreas Gohr                null,
196464159a61SAndreas Gohr                null,
196564159a61SAndreas Gohr                $cache = null,
196664159a61SAndreas Gohr                $linking = 'linkonly',
196764159a61SAndreas Gohr                $return = true
196864159a61SAndreas Gohr            );
19693d7a9e0aSAnika Henke        }
19702a2a2ba2SAnika Henke
19710877a1f1SSchplurtz le Déboulonné        // output each track if any
19720877a1f1SSchplurtz le Déboulonné        foreach ($tracks as $trackid => $info) {
1973faf3f01bSAndreas Gohr            [$kind, $srclang] = array_map('hsc', $info);
197423c61bbeSSchplurtz le Déboulonné            $out .= "<track kind=\"$kind\" srclang=\"$srclang\" ";
197523c61bbeSSchplurtz le Déboulonné            $out .= "label=\"$srclang\" ";
19760877a1f1SSchplurtz le Déboulonné            $out .= 'src="' . ml($trackid, '', true) . '">' . NL;
19770877a1f1SSchplurtz le Déboulonné        }
19780877a1f1SSchplurtz le Déboulonné
19792a2a2ba2SAnika Henke        // finish
19803641199aSAnika Henke        $out .= $fallback;
1981f50634f0SAnika Henke        $out .= '</video>' . NL;
1982f50634f0SAnika Henke        return $out;
19832a2a2ba2SAnika Henke    }
19842a2a2ba2SAnika Henke
1985b44a5dceSAnika Henke    /**
1986b44a5dceSAnika Henke     * Embed audio in HTML
1987b44a5dceSAnika Henke     *
1988b44a5dceSAnika Henke     * @param string $src - ID of audio to embed
19896d4af72aSAnika Henke     * @param array $atts - additional attributes for the <audio> tag
1990f50634f0SAnika Henke     * @return string
1991faf3f01bSAndreas Gohr     * @author Anika Henke <anika@selfthinker.org>
1992faf3f01bSAndreas Gohr     *
1993b44a5dceSAnika Henke     */
1994faf3f01bSAndreas Gohr    public function _audio($src, $atts = [])
1995faf3f01bSAndreas Gohr    {
1996faf3f01bSAndreas Gohr        $files = [];
1997410ee62aSAnika Henke        $isExternal = media_isexternal($src);
1998b44a5dceSAnika Henke
1999410ee62aSAnika Henke        if ($isExternal) {
2000410ee62aSAnika Henke            // take direct source for external files
2001a19c9aa0SGerrit Uitslag            [/* ext */, $srcMime] = mimetype($src);
2002410ee62aSAnika Henke            $files[$srcMime] = $src;
2003410ee62aSAnika Henke        } else {
2004b44a5dceSAnika Henke            // prepare alternative formats
2005faf3f01bSAndreas Gohr            $extensions = ['ogg', 'mp3', 'wav'];
2006410ee62aSAnika Henke            $files = media_alternativefiles($src, $extensions);
2007410ee62aSAnika Henke        }
2008b44a5dceSAnika Henke
2009f50634f0SAnika Henke        $out = '';
2010b44a5dceSAnika Henke        // open audio tag
2011f50634f0SAnika Henke        $out .= '<audio ' . buildAttributes($atts) . ' controls="controls">' . NL;
20123641199aSAnika Henke        $fallback = '';
2013b44a5dceSAnika Henke
2014b44a5dceSAnika Henke        // output source for each alternative audio format
2015410ee62aSAnika Henke        foreach ($files as $mime => $file) {
2016410ee62aSAnika Henke            if ($isExternal) {
2017410ee62aSAnika Henke                $url = $file;
2018410ee62aSAnika Henke                $linkType = 'externalmedia';
2019410ee62aSAnika Henke            } else {
20202d338eabSAndreas Gohr                $url = ml($file, '', true, '&');
2021410ee62aSAnika Henke                $linkType = 'internalmedia';
2022410ee62aSAnika Henke            }
2023faf3f01bSAndreas Gohr            $title = $atts['title'] ?: $this->_xmlEntities(PhpString::basename(noNS($file)));
2024b44a5dceSAnika Henke
2025f50634f0SAnika Henke            $out .= '<source src="' . hsc($url) . '" type="' . $mime . '" />' . NL;
2026b44a5dceSAnika Henke            // alternative content (just a link to the file)
202764159a61SAndreas Gohr            $fallback .= $this->$linkType(
202864159a61SAndreas Gohr                $file,
202964159a61SAndreas Gohr                $title,
203064159a61SAndreas Gohr                null,
203164159a61SAndreas Gohr                null,
203264159a61SAndreas Gohr                null,
203364159a61SAndreas Gohr                $cache = null,
203464159a61SAndreas Gohr                $linking = 'linkonly',
203564159a61SAndreas Gohr                $return = true
203664159a61SAndreas Gohr            );
2037b44a5dceSAnika Henke        }
2038b44a5dceSAnika Henke
2039b44a5dceSAnika Henke        // finish
20403641199aSAnika Henke        $out .= $fallback;
2041f50634f0SAnika Henke        $out .= '</audio>' . NL;
2042f50634f0SAnika Henke        return $out;
2043b44a5dceSAnika Henke    }
2044b44a5dceSAnika Henke
20455c2eed9aSlisps    /**
204652dc5eadSlisps     * _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
20475c2eed9aSlisps     * which returns an existing media revision less or equal to rev or date_at
20485c2eed9aSlisps     *
20495c2eed9aSlisps     * @param string $media_id
20505c2eed9aSlisps     * @access protected
20515c2eed9aSlisps     * @return string revision ('' for current)
2052faf3f01bSAndreas Gohr     * @author lisps
20535c2eed9aSlisps     */
2054faf3f01bSAndreas Gohr    protected function _getLastMediaRevisionAt($media_id)
2055faf3f01bSAndreas Gohr    {
205652dc5eadSlisps        if (!$this->date_at || media_isexternal($media_id)) return '';
2057252acce3SSatoshi Sahara        $changelog = new MediaChangeLog($media_id);
2058252acce3SSatoshi Sahara        return $changelog->getLastRevisionAt($this->date_at);
20595c2eed9aSlisps    }
20605c2eed9aSlisps
20613dd5c225SAndreas Gohr    #endregion
20620cecf9d5Sandi}
20630cecf9d5Sandi
2064e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
2065