xref: /plugin/diagrams/syntax/embed.php (revision fc9883e98206ae9a5f4fa3ad7aa06bd435c1ab07)
1<?php
2
3use dokuwiki\plugin\diagrams\Diagrams;
4
5/**
6 * DokuWiki Plugin diagrams (Syntax Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
10 */
11class syntax_plugin_diagrams_embed extends syntax_plugin_diagrams_mediafile
12{
13    /** @var int count the current embedded diagram */
14    protected $count = 0;
15
16    /** @inheritDoc */
17    public function connectTo($mode)
18    {
19        // only register if embed mode is enabled
20        if (!($this->getConf('mode') & Diagrams::MODE_EMBED)) return;
21        $this->Lexer->addSpecialPattern('<diagram(?: .*?)?>.*?(?:</diagram>)', $mode, 'plugin_diagrams_embed');
22    }
23
24    /** @inheritDoc */
25    public function handle($match, $state, $pos, Doku_Handler $handler)
26    {
27        [$open, $rest] = sexplode('>', $match, 2);
28        $params = substr($open, 9);
29        $svg = substr($rest, 0, -10);
30
31        // embed positions
32        $svglen = strlen($svg);
33        $svgpos = $pos + strpos($match, '>');
34
35        /** @var helper_plugin_diagrams $helper */
36        $helper = plugin_load('helper', 'diagrams');
37        if (!$helper->isDiagram($svg)) return false;
38
39        $data = [
40            'svg' => $svg,
41            'align' => '',
42            'width' => '',
43            'height' => '',
44            'pos' => $svgpos,
45            'len' => $svglen,
46        ];
47
48        if (preg_match('/\b(left|right|center)\b/', $params, $matches)) {
49            $data['align'] = $matches[1];
50        }
51        if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) {
52            $data['width'] = (int)$matches[1];
53            $data['height'] = (int)$matches[2];
54        }
55
56        return $data;
57    }
58
59    /** @inheritDoc */
60    public function render($format, Doku_Renderer $renderer, $data)
61    {
62        if (!$data) return false;
63        global $INPUT;
64
65        switch ($format) {
66            case 'xhtml':
67                // this references the diagram as data uri to prevent cross-origin access XSS
68                // we use this instead of the export_method below (which would also apply CSP headers)
69                // because this needs to work in preview, before the page has been saved
70                $data['url'] = 'data:image/svg+xml;base64,' . base64_encode($data['svg']);
71                parent::render($format, $renderer, $data);
72                return true;
73            case 'diagrams':
74                // This exports a single SVG during the export_diagrams action
75                if ($INPUT->int('svg') === $this->count++) {
76                    $renderer->doc = $data['svg'];
77                }
78                return true;
79        }
80        return false;
81    }
82
83
84}
85
86