xref: /plugin/diagrams/renderer.php (revision 27b2367e0e899c7a18667673704a68784b2c5652)
1046ca144SAndreas Gohr<?php
2*27b2367eSAndreas Gohr
3*27b2367eSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
4*27b2367eSAndreas Gohr
5046ca144SAndreas Gohr/**
6046ca144SAndreas Gohr * DokuWiki Plugin diagrams (Renderer Component)
7046ca144SAndreas Gohr *
8046ca144SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9046ca144SAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
10046ca144SAndreas Gohr */
11046ca144SAndreas Gohrclass renderer_plugin_diagrams extends Doku_Renderer
12046ca144SAndreas Gohr{
13046ca144SAndreas Gohr
14046ca144SAndreas Gohr    /** @inheritDoc */
15046ca144SAndreas Gohr    public function getFormat()
16046ca144SAndreas Gohr    {
17046ca144SAndreas Gohr        return 'diagrams';
18046ca144SAndreas Gohr    }
19046ca144SAndreas Gohr
20046ca144SAndreas Gohr    /**
21046ca144SAndreas Gohr     * Set proper headers
22046ca144SAndreas Gohr     */
23046ca144SAndreas Gohr    public function document_start()
24046ca144SAndreas Gohr    {
25046ca144SAndreas Gohr        global $ID;
26046ca144SAndreas Gohr        $headers = [
27046ca144SAndreas Gohr            'Content-Type' => 'image/svg+xml',
28046ca144SAndreas Gohr            'Content-Security-Policy' => $this->getCSP(),
29046ca144SAndreas Gohr        ];
30046ca144SAndreas Gohr        p_set_metadata($ID, ['format' => ['diagrams' => $headers]]);
31046ca144SAndreas Gohr        // don't cache
32046ca144SAndreas Gohr        $this->nocache();
33046ca144SAndreas Gohr    }
34046ca144SAndreas Gohr
35046ca144SAndreas Gohr    /**
36046ca144SAndreas Gohr     * Create the content security policy
37046ca144SAndreas Gohr     * @return string
38046ca144SAndreas Gohr     */
39046ca144SAndreas Gohr    protected function getCSP() {
40*27b2367eSAndreas Gohr        $policy = Diagrams::CSP;
41046ca144SAndreas Gohr
42046ca144SAndreas Gohr        /** @noinspection DuplicatedCode from dokuwiki\HTTP\Headers::contentSecurityPolicy() */
43046ca144SAndreas Gohr        foreach ($policy as $key => $values) {
44046ca144SAndreas Gohr            // if the value is not an array, we also accept newline terminated strings
45046ca144SAndreas Gohr            if (!is_array($values)) $values = explode("\n", $values);
46046ca144SAndreas Gohr            $values = array_map('trim', $values);
47046ca144SAndreas Gohr            $values = array_unique($values);
48046ca144SAndreas Gohr            $values = array_filter($values);
49046ca144SAndreas Gohr            $policy[$key] = $values;
50046ca144SAndreas Gohr        }
51046ca144SAndreas Gohr
52046ca144SAndreas Gohr        $cspheader = '';
53046ca144SAndreas Gohr        foreach ($policy as $key => $values) {
54046ca144SAndreas Gohr            if ($values) {
55046ca144SAndreas Gohr                $cspheader .= " $key " . join(' ', $values) . ';';
56046ca144SAndreas Gohr            } else {
57046ca144SAndreas Gohr                $cspheader .= " $key;";
58046ca144SAndreas Gohr            }
59046ca144SAndreas Gohr        }
60046ca144SAndreas Gohr
61046ca144SAndreas Gohr        return $cspheader;
62046ca144SAndreas Gohr    }
63046ca144SAndreas Gohr}
64046ca144SAndreas Gohr
65