xref: /plugin/diagrams/action/embed.php (revision 54b7a78ab0ccd620e965bf709a938d44c0f563b9)
18c8c7007SAndreas Gohr<?php
2bc39777fSAndreas Gohr
3bc39777fSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
4bc39777fSAndreas Gohr
58c8c7007SAndreas Gohr/**
68c8c7007SAndreas Gohr * DokuWiki Plugin diagrams (Action Component)
78c8c7007SAndreas Gohr *
88c8c7007SAndreas Gohr * This handles loading and saving embedded diagrams
98c8c7007SAndreas Gohr *
108c8c7007SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
118c8c7007SAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
128c8c7007SAndreas Gohr */
138c8c7007SAndreas Gohrclass action_plugin_diagrams_embed extends \dokuwiki\Extension\ActionPlugin
148c8c7007SAndreas Gohr{
1595ed8ca0SAndreas Gohr    /** @var helper_plugin_diagrams */
1695ed8ca0SAndreas Gohr    protected $helper;
178c8c7007SAndreas Gohr
188c8c7007SAndreas Gohr    /** @inheritDoc */
198c8c7007SAndreas Gohr    public function register(Doku_Event_Handler $controller)
208c8c7007SAndreas Gohr    {
21bc39777fSAndreas Gohr        // only register if embed mode is enabled
2259e7180eSAndreas Gohr        if (!($this->getConf('mode') & Diagrams::MODE_EMBED)) return;
23bc39777fSAndreas Gohr
248c8c7007SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleLoad');
25bc39777fSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleSave');
2695ed8ca0SAndreas Gohr
2795ed8ca0SAndreas Gohr        $this->helper = plugin_load('helper', 'diagrams');
288c8c7007SAndreas Gohr    }
298c8c7007SAndreas Gohr
308c8c7007SAndreas Gohr    /**
318c8c7007SAndreas Gohr     * Load the SVG for an embedded diagram
328c8c7007SAndreas Gohr     *
338c8c7007SAndreas Gohr     * This locks the page for editing
348c8c7007SAndreas Gohr     *
3559e7180eSAndreas Gohr     * @param Doku_Event $event Event object AJAX_CALL_UNKNOWN
368c8c7007SAndreas Gohr     */
3759e7180eSAndreas Gohr    public function handleLoad(Doku_Event $event)
38146e874bSAndreas Gohr    {
398c8c7007SAndreas Gohr        if ($event->data !== 'plugin_diagrams_embed_load') return;
408c8c7007SAndreas Gohr        $event->preventDefault();
418c8c7007SAndreas Gohr        $event->stopPropagation();
428c8c7007SAndreas Gohr
438c8c7007SAndreas Gohr        global $INPUT;
448c8c7007SAndreas Gohr
458c8c7007SAndreas Gohr        $id = $INPUT->str('id');
468c8c7007SAndreas Gohr        $pos = $INPUT->int('pos');
478c8c7007SAndreas Gohr        $len = $INPUT->int('len');
488c8c7007SAndreas Gohr
49ca5b8841SAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_EDIT) {
508c8c7007SAndreas Gohr            http_status(403);
518c8c7007SAndreas Gohr            return;
528c8c7007SAndreas Gohr        }
538c8c7007SAndreas Gohr
548c8c7007SAndreas Gohr        if (!page_exists($id)) {
558c8c7007SAndreas Gohr            http_status(404);
568c8c7007SAndreas Gohr            return;
578c8c7007SAndreas Gohr        }
588c8c7007SAndreas Gohr
598c8c7007SAndreas Gohr        if (checklock($id)) {
608c8c7007SAndreas Gohr            http_status(423, 'Page Locked');
618c8c7007SAndreas Gohr            return;
628c8c7007SAndreas Gohr        }
638c8c7007SAndreas Gohr
648c8c7007SAndreas Gohr        $svg = rawWiki($id);
65*54b7a78aSAndreas Gohr        $svg = substr($svg, $pos, $len);
6695ed8ca0SAndreas Gohr        if (!$this->helper->isDiagram($svg)) {
6795ed8ca0SAndreas Gohr            http_status(400);
6895ed8ca0SAndreas Gohr            return;
6995ed8ca0SAndreas Gohr        }
7095ed8ca0SAndreas Gohr
7195ed8ca0SAndreas Gohr        lock($id); // FIXME we probably need some periodic lock renewal while editing?
7295ed8ca0SAndreas Gohr        header('Content-Type: image/svg+xml');
73*54b7a78aSAndreas Gohr        echo $svg;
748c8c7007SAndreas Gohr    }
758c8c7007SAndreas Gohr
768c8c7007SAndreas Gohr    /**
778c8c7007SAndreas Gohr     * Save a new embedded diagram
788c8c7007SAndreas Gohr     *
7959e7180eSAndreas Gohr     * @param Doku_Event $event AJAX_CALL_UNKNOWN
808c8c7007SAndreas Gohr     */
8159e7180eSAndreas Gohr    public function handleSave(Doku_Event $event)
828c8c7007SAndreas Gohr    {
83317bdfc2SAndreas Gohr        if ($event->data !== 'plugin_diagrams_embed_save') return;
848c8c7007SAndreas Gohr        $event->preventDefault();
858c8c7007SAndreas Gohr        $event->stopPropagation();
868c8c7007SAndreas Gohr
878c8c7007SAndreas Gohr        global $INPUT;
888c8c7007SAndreas Gohr
898c8c7007SAndreas Gohr        $id = $INPUT->str('id');
90317bdfc2SAndreas Gohr        $svg = $INPUT->str('svg');
918c8c7007SAndreas Gohr        $pos = $INPUT->int('pos');
928c8c7007SAndreas Gohr        $len = $INPUT->int('len');
938c8c7007SAndreas Gohr
948c8c7007SAndreas Gohr
958c8c7007SAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_EDIT) {
968c8c7007SAndreas Gohr            http_status(403);
978c8c7007SAndreas Gohr            return;
988c8c7007SAndreas Gohr        }
998c8c7007SAndreas Gohr
1008c8c7007SAndreas Gohr        if (!page_exists($id)) {
1018c8c7007SAndreas Gohr            http_status(404);
1028c8c7007SAndreas Gohr            return;
1038c8c7007SAndreas Gohr        }
1048c8c7007SAndreas Gohr
1058c8c7007SAndreas Gohr        if (!checkSecurityToken()) {
1068c8c7007SAndreas Gohr            http_status(403);
1078c8c7007SAndreas Gohr            return;
1088c8c7007SAndreas Gohr        }
1098c8c7007SAndreas Gohr
11095ed8ca0SAndreas Gohr        if (!$this->helper->isDiagram($svg)) {
11195ed8ca0SAndreas Gohr            http_status(400);
11295ed8ca0SAndreas Gohr            return;
11395ed8ca0SAndreas Gohr        }
11495ed8ca0SAndreas Gohr
1158c8c7007SAndreas Gohr        $original = rawWiki($id);
1168c8c7007SAndreas Gohr        $new = substr($original, 0, $pos) . $svg . substr($original, $pos + $len);
1178c8c7007SAndreas Gohr        saveWikiText($id, $new, $this->getLang('embedSaveSummary'));
1188c8c7007SAndreas Gohr        unlock($id);
1198c8c7007SAndreas Gohr        echo 'OK';
1208c8c7007SAndreas Gohr    }
1218c8c7007SAndreas Gohr
1228c8c7007SAndreas Gohr}
1238c8c7007SAndreas Gohr
124