1<?php
2
3use dokuwiki\plugin\diagrams\Diagrams;
4
5/**
6 * DokuWiki Plugin diagrams (Action Component)
7 *
8 * This handles loading and saving embedded diagrams
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
12 */
13class action_plugin_diagrams_embed extends \dokuwiki\Extension\ActionPlugin
14{
15    /** @var helper_plugin_diagrams */
16    protected $helper;
17
18    /** @inheritDoc */
19    public function register(Doku_Event_Handler $controller)
20    {
21        // only register if embed mode is enabled
22        if (!($this->getConf('mode') & Diagrams::MODE_EMBED)) return;
23
24        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleLoad');
25        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleSave');
26
27        $this->helper = plugin_load('helper', 'diagrams');
28    }
29
30    /**
31     * Load the SVG for an embedded diagram
32     *
33     * This locks the page for editing
34     *
35     * @param Doku_Event $event Event object AJAX_CALL_UNKNOWN
36     */
37    public function handleLoad(Doku_Event $event)
38    {
39        if ($event->data !== 'plugin_diagrams_embed_load') return;
40        $event->preventDefault();
41        $event->stopPropagation();
42
43        global $INPUT;
44
45        $id = $INPUT->str('id');
46        $pos = $INPUT->int('pos');
47        $len = $INPUT->int('len');
48
49        if (auth_quickaclcheck($id) < AUTH_EDIT) {
50            http_status(403);
51            return;
52        }
53
54        if (!page_exists($id)) {
55            http_status(404);
56            return;
57        }
58
59        if (checklock($id)) {
60            http_status(423, 'Page Locked');
61            return;
62        }
63
64        $svg = rawWiki($id);
65        $svg = substr($svg, $pos, $len);
66        if (!$this->helper->isDiagram($svg)) {
67            http_status(400);
68            return;
69        }
70
71        lock($id); // FIXME we probably need some periodic lock renewal while editing?
72        header('Content-Type: image/svg+xml');
73        echo $svg;
74    }
75
76    /**
77     * Save a new embedded diagram
78     *
79     * @param Doku_Event $event AJAX_CALL_UNKNOWN
80     */
81    public function handleSave(Doku_Event $event)
82    {
83        if ($event->data !== 'plugin_diagrams_embed_save') return;
84        $event->preventDefault();
85        $event->stopPropagation();
86
87        global $INPUT;
88
89        $id = $INPUT->str('id');
90        $svg = $INPUT->str('svg');
91        $pos = $INPUT->int('pos');
92        $len = $INPUT->int('len');
93
94
95        if (auth_quickaclcheck($id) < AUTH_EDIT) {
96            http_status(403);
97            return;
98        }
99
100        if (!page_exists($id)) {
101            http_status(404);
102            return;
103        }
104
105        if (!checkSecurityToken()) {
106            http_status(403);
107            return;
108        }
109
110        if (!$this->helper->isDiagram($svg)) {
111            http_status(400);
112            return;
113        }
114
115        $original = rawWiki($id);
116        $new = substr($original, 0, $pos) . $svg . substr($original, $pos + $len);
117        saveWikiText($id, $new, $this->getLang('embedSaveSummary'));
118        unlock($id);
119        echo 'OK';
120    }
121
122}
123
124