xref: /plugin/diagrams/syntax/embed.php (revision 0e8c676d43af9323e66c3e8f386c1b5f978b7ba1)
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 $ID;
64        global $ACT;
65        global $INPUT;
66
67        switch ($format) {
68            case 'xhtml':
69                if(act_clean($ACT) !== 'preview' && page_exists($ID)) {
70                    // this is "normal" rendering, we reference the diagram through the export
71                    // this applies the same CSP as mediafiles and will also show the exact behaviours
72                    $data['url'] = wl($ID, ['do' => 'export_diagrams', 'svg' => $this->count++], true, '&');
73                } else {
74                    // we're in preview and the diagram may not have been saved, yet. So we
75                    // reference it as data uri to prevent cross-origin access XSS
76                    $data['url'] = 'data:image/svg+xml;base64,' . base64_encode($data['svg']);
77                }
78
79                parent::render($format, $renderer, $data);
80                return true;
81            case 'diagrams':
82                // This exports a single SVG during the export_diagrams action
83                if ($INPUT->int('svg') === $this->count++) {
84                    $renderer->doc = $data['svg'];
85                }
86                return true;
87        }
88        return false;
89    }
90
91
92}
93
94