xref: /plugin/diagrams/action/action.php (revision 2bec1a225a06bb4c356005e6a5e76e5d4d346b20)
1<?php
2
3/**
4 * Action component of diagrams plugin
5 *
6 * This handles general operations independent of the configured mode
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
10 */
11class action_plugin_diagrams_action extends DokuWiki_Action_Plugin
12{
13    /** @var helper_plugin_diagrams */
14    protected $helper;
15
16    /**@inheritDoc */
17    public function register(Doku_Event_Handler $controller)
18    {
19        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addJsinfo');
20        $controller->register_hook('MEDIAMANAGER_STARTED', 'AFTER', $this, 'addJsinfo');
21        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'checkConf');
22        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleCache');
23
24        $this->helper = plugin_load('helper', 'diagrams');
25    }
26
27    /**
28     * Add data to JSINFO
29     *
30     * full service URL
31     * digram mode
32     * security token used for uploading
33     *
34     * @param Doku_Event $event DOKUWIKI_STARTED|MEDIAMANAGER_STARTED
35     */
36    public function addJsinfo(Doku_Event $event)
37    {
38        global $JSINFO;
39        $JSINFO['sectok'] = getSecurityToken();
40        $JSINFO['plugins']['diagrams'] = [
41            'service_url' => $this->getConf('service_url'),
42            'mode' => $this->getConf('mode'),
43        ];
44    }
45
46    /**
47     * Check if DokuWiki is properly configured to handle SVG diagrams
48     *
49     * @param Doku_Event $event DOKUWIKI_STARTED
50     */
51    public function checkConf(Doku_Event $event)
52    {
53        $mime = getMimeTypes();
54        if (!array_key_exists('svg', $mime) || $mime['svg'] !== 'image/svg+xml') {
55            msg($this->getLang('missingConfig'), -1);
56        }
57    }
58
59    /**
60     * Save the PNG cache of a diagram
61     *
62     * @param Doku_Event $event AJAX_CALL_UNKNOWN
63     */
64    public function handleCache(Doku_Event $event)
65    {
66        if ($event->data !== 'plugin_diagrams_savecache') return;
67        $event->preventDefault();
68        $event->stopPropagation();
69
70        global $INPUT;
71
72        $svg = $INPUT->str('svg'); // raw svg
73        $png = $INPUT->str('png'); // data uri
74
75        if (!checkSecurityToken()) {
76            http_status(403);
77            return;
78        }
79
80        if (!$this->helper->isDiagram($svg)) {
81            http_status(400);
82            return;
83        }
84
85        if(!preg_match('/^data:image\/png;base64,/', $png)) {
86            http_status(400);
87            return;
88        }
89        $png = base64_decode(explode(',',$png)[1]);
90
91        if(substr($png, 1, 3) !== 'PNG') {
92            http_status(400);
93            return;
94        }
95
96        $cacheName = getCacheName($svg, '.diagrams.png');
97        if(io_saveFile($cacheName, $png)) {
98            echo 'OK';
99        } else {
100            http_status(500);
101        }
102    }
103}
104