xref: /plugin/diagrams/syntax/embed.php (revision 046ca1447d01a47b6caa55952ebefa80c90967a9)
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 */
11*046ca144SAndreas Gohrclass syntax_plugin_diagrams_embed extends syntax_plugin_diagrams_mediafile
128c8c7007SAndreas Gohr{
13*046ca144SAndreas Gohr    /** @var int count the current embedded diagram */
14*046ca144SAndreas 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;
63*046ca144SAndreas Gohr        global $ID;
64*046ca144SAndreas Gohr        global $INPUT;
658c8c7007SAndreas Gohr
66*046ca144SAndreas Gohr        switch ($format) {
67*046ca144SAndreas Gohr            case 'xhtml':
68*046ca144SAndreas Gohr                // this references the diagram via the export_diagrams URL
69*046ca144SAndreas Gohr                // this is used instead of inlining the SVG to be able to use a CSP header to prevent XSS
70*046ca144SAndreas Gohr                $data['url'] = wl($ID, ['do' => 'export_diagrams', 'svg' => $this->count++], true, '&');
71*046ca144SAndreas Gohr                parent::render($format, $renderer, $data);
72*046ca144SAndreas Gohr                return true;
73*046ca144SAndreas Gohr            case 'diagrams':
74*046ca144SAndreas Gohr                // This exports a single SVG during the export_diagrams action
75*046ca144SAndreas Gohr                if ($INPUT->int('svg') === $this->count++) {
76*046ca144SAndreas Gohr                    $renderer->doc = $data['svg'];
77100ff2a5SAndreas Gohr                }
788a822cc1SAndreas Gohr                return true;
798a822cc1SAndreas Gohr        }
80*046ca144SAndreas Gohr        return false;
81*046ca144SAndreas Gohr    }
82*046ca144SAndreas Gohr
83*046ca144SAndreas Gohr
848a822cc1SAndreas Gohr}
858c8c7007SAndreas Gohr
86