1<?php
2
3use dokuwiki\plugin\diagrams\Diagrams;
4
5/**
6 * DokuWiki Plugin diagrams (Renderer Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
10 */
11class renderer_plugin_diagrams extends Doku_Renderer
12{
13
14    /** @inheritDoc */
15    public function getFormat()
16    {
17        return 'diagrams';
18    }
19
20    /**
21     * Set proper headers
22     */
23    public function document_start()
24    {
25        global $ID;
26        $headers = [
27            'Content-Type' => 'image/svg+xml',
28            'Content-Security-Policy' => $this->getCSP(),
29        ];
30        p_set_metadata($ID, ['format' => ['diagrams' => $headers]]);
31        // don't cache
32        $this->nocache();
33    }
34
35    /**
36     * Create the content security policy
37     * @return string
38     */
39    protected function getCSP() {
40        $policy = Diagrams::CSP;
41
42        /** @noinspection DuplicatedCode from dokuwiki\HTTP\Headers::contentSecurityPolicy() */
43        foreach ($policy as $key => $values) {
44            // if the value is not an array, we also accept newline terminated strings
45            if (!is_array($values)) $values = explode("\n", $values);
46            $values = array_map('trim', $values);
47            $values = array_unique($values);
48            $values = array_filter($values);
49            $policy[$key] = $values;
50        }
51
52        $cspheader = '';
53        foreach ($policy as $key => $values) {
54            if ($values) {
55                $cspheader .= " $key " . join(' ', $values) . ';';
56            } else {
57                $cspheader .= " $key;";
58            }
59        }
60
61        return $cspheader;
62    }
63}
64
65