xref: /plugin/diagrams/syntax/embed.php (revision 2aa4335d54333a6ce998c788e418576adca3450f)
18a822cc1SAndreas Gohr<?php
28a822cc1SAndreas Gohr
3bc39777fSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
4bc39777fSAndreas Gohr
58a822cc1SAndreas Gohr/**
68c8c7007SAndreas Gohr * DokuWiki Plugin diagrams (Syntax Component)
78c8c7007SAndreas Gohr *
88c8c7007SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
98c8c7007SAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
108a822cc1SAndreas Gohr */
11046ca144SAndreas Gohrclass syntax_plugin_diagrams_embed extends syntax_plugin_diagrams_mediafile
128c8c7007SAndreas Gohr{
13046ca144SAndreas Gohr    /** @var int count the current embedded diagram */
14046ca144SAndreas Gohr    protected $count = 0;
158a822cc1SAndreas Gohr
168c8c7007SAndreas Gohr    /** @inheritDoc */
178a822cc1SAndreas Gohr    public function connectTo($mode)
188a822cc1SAndreas Gohr    {
19bc39777fSAndreas Gohr        // only register if embed mode is enabled
2059e7180eSAndreas Gohr        if (!($this->getConf('mode') & Diagrams::MODE_EMBED)) return;
21baa90558SAnna Dabrowska        $this->Lexer->addSpecialPattern('<diagram(?: .*?)?>.*?(?:</diagram>)', $mode, 'plugin_diagrams_embed');
228a822cc1SAndreas Gohr    }
238a822cc1SAndreas Gohr
248c8c7007SAndreas Gohr    /** @inheritDoc */
258a822cc1SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
268a822cc1SAndreas Gohr    {
278c8c7007SAndreas Gohr        [$open, $rest] = sexplode('>', $match, 2);
288c8c7007SAndreas Gohr        $params = substr($open, 9);
298c8c7007SAndreas Gohr        $svg = substr($rest, 0, -10);
308c8c7007SAndreas Gohr
31317bdfc2SAndreas Gohr        // embed positions
32317bdfc2SAndreas Gohr        $svglen = strlen($svg);
33317bdfc2SAndreas Gohr        $svgpos = $pos + strpos($match, '>');
34317bdfc2SAndreas Gohr
3595ed8ca0SAndreas Gohr        /** @var helper_plugin_diagrams $helper */
3695ed8ca0SAndreas Gohr        $helper = plugin_load('helper', 'diagrams');
3795ed8ca0SAndreas Gohr        if (!$helper->isDiagram($svg)) return false;
3895ed8ca0SAndreas Gohr
398c8c7007SAndreas Gohr        $data = [
408c8c7007SAndreas Gohr            'svg' => $svg,
418c8c7007SAndreas Gohr            'align' => '',
428c8c7007SAndreas Gohr            'width' => '',
438c8c7007SAndreas Gohr            'height' => '',
44317bdfc2SAndreas Gohr            'pos' => $svgpos,
45317bdfc2SAndreas Gohr            'len' => $svglen,
468c8c7007SAndreas Gohr        ];
478c8c7007SAndreas Gohr
488c8c7007SAndreas Gohr        if (preg_match('/\b(left|right|center)\b/', $params, $matches)) {
498c8c7007SAndreas Gohr            $data['align'] = $matches[1];
508c8c7007SAndreas Gohr        }
518c8c7007SAndreas Gohr        if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) {
528c8c7007SAndreas Gohr            $data['width'] = (int)$matches[1];
538c8c7007SAndreas Gohr            $data['height'] = (int)$matches[2];
548a822cc1SAndreas Gohr        }
558a822cc1SAndreas Gohr
568c8c7007SAndreas Gohr        return $data;
578c8c7007SAndreas Gohr    }
588c8c7007SAndreas Gohr
598c8c7007SAndreas Gohr    /** @inheritDoc */
608a822cc1SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $data)
618a822cc1SAndreas Gohr    {
62100ff2a5SAndreas Gohr        if (!$data) return false;
63046ca144SAndreas Gohr        global $INPUT;
648c8c7007SAndreas Gohr
65046ca144SAndreas Gohr        switch ($format) {
66046ca144SAndreas Gohr            case 'xhtml':
67*2aa4335dSAndreas Gohr                // this references the diagram as data uri to prevent cross-origin access XSS
68*2aa4335dSAndreas Gohr                // we use this instead of the export_method below (which would also apply CSP headers)
69*2aa4335dSAndreas Gohr                // because this needs to work in preview, before the page has been saved
70*2aa4335dSAndreas Gohr                $data['url'] = 'data:image/svg+xml;base64,' . base64_encode($data['svg']);
71046ca144SAndreas Gohr                parent::render($format, $renderer, $data);
72046ca144SAndreas Gohr                return true;
73046ca144SAndreas Gohr            case 'diagrams':
74046ca144SAndreas Gohr                // This exports a single SVG during the export_diagrams action
75046ca144SAndreas Gohr                if ($INPUT->int('svg') === $this->count++) {
76046ca144SAndreas Gohr                    $renderer->doc = $data['svg'];
77100ff2a5SAndreas Gohr                }
788a822cc1SAndreas Gohr                return true;
798a822cc1SAndreas Gohr        }
80046ca144SAndreas Gohr        return false;
81046ca144SAndreas Gohr    }
82046ca144SAndreas Gohr
83046ca144SAndreas Gohr
848a822cc1SAndreas Gohr}
858c8c7007SAndreas Gohr
86