xref: /plugin/diagrams/action/mediafile.php (revision 27b2367e0e899c7a18667673704a68784b2c5652)
15f757686SAndreas Gohr<?php
25f757686SAndreas Gohr
359e7180eSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams;
459e7180eSAndreas Gohr
55f757686SAndreas Gohr/**
65f757686SAndreas Gohr * Action component of diagrams plugin
75f757686SAndreas Gohr *
859e7180eSAndreas Gohr * This handles operations related to mediafile based diagrams
959e7180eSAndreas Gohr *
1059e7180eSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1159e7180eSAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
125f757686SAndreas Gohr */
135f757686SAndreas Gohrclass action_plugin_diagrams_mediafile extends DokuWiki_Action_Plugin
145f757686SAndreas Gohr{
155f757686SAndreas Gohr
1659e7180eSAndreas Gohr    /** @var helper_plugin_diagrams */
1759e7180eSAndreas Gohr    protected $helper;
1859e7180eSAndreas Gohr
1959e7180eSAndreas Gohr    /** @inheritDoc */
205f757686SAndreas Gohr    public function register(Doku_Event_Handler $controller)
215f757686SAndreas Gohr    {
2259e7180eSAndreas Gohr        // only register if mediafile mode is enabled
2359e7180eSAndreas Gohr        if (!($this->getConf('mode') & Diagrams::MODE_MEDIA)) return;
2459e7180eSAndreas Gohr
255f757686SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleEditCheck');
2659e7180eSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleNamespaceCheck');
27ca5b8841SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleIsDiagramCheck');
2859e7180eSAndreas Gohr        $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'handleCSP');
29b7bd8fa5SAnna Dabrowska        $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'registerMoveHandler');
3059e7180eSAndreas Gohr
3159e7180eSAndreas Gohr        $this->helper = plugin_load('helper', 'diagrams');
325f757686SAndreas Gohr    }
335f757686SAndreas Gohr
345f757686SAndreas Gohr    /**
355f757686SAndreas Gohr     * Check all supplied diagrams and return only editable diagrams
365f757686SAndreas Gohr     *
3759e7180eSAndreas Gohr     * @param Doku_Event $event AJAX_CALL_UNKNOWN
385f757686SAndreas Gohr     */
395f757686SAndreas Gohr    public function handleEditCheck(Doku_Event $event)
405f757686SAndreas Gohr    {
415f757686SAndreas Gohr        if ($event->data !== 'plugin_diagrams_mediafile_editcheck') return;
425f757686SAndreas Gohr        $event->preventDefault();
435f757686SAndreas Gohr        $event->stopPropagation();
445f757686SAndreas Gohr
455f757686SAndreas Gohr        global $INPUT;
465f757686SAndreas Gohr        $diagrams = (array)json_decode($INPUT->str('diagrams'));
475f757686SAndreas Gohr
485f757686SAndreas Gohr        $editable = [];
495f757686SAndreas Gohr        foreach ($diagrams as $image) {
505f757686SAndreas Gohr            $image = cleanID($image);
515f757686SAndreas Gohr            $file = mediaFN($image);
525f757686SAndreas Gohr
535f757686SAndreas Gohr            if (
545f757686SAndreas Gohr                file_exists($file) &&
555f757686SAndreas Gohr                auth_quickaclcheck($image) >= AUTH_UPLOAD &&
5659e7180eSAndreas Gohr                $this->helper->isDiagramFile($file)
575f757686SAndreas Gohr            ) {
585f757686SAndreas Gohr                $editable[] = $image;
595f757686SAndreas Gohr            }
605f757686SAndreas Gohr        }
615f757686SAndreas Gohr
625f757686SAndreas Gohr        echo json_encode($editable);
635f757686SAndreas Gohr    }
645f757686SAndreas Gohr
6559e7180eSAndreas Gohr    /**
66ca5b8841SAndreas Gohr     * Check if the given media ID is a diagram
67ca5b8841SAndreas Gohr     *
68ca5b8841SAndreas Gohr     * @param Doku_Event $event AJAX_CALL_UNKNOWN
69ca5b8841SAndreas Gohr     */
70ca5b8841SAndreas Gohr    public function handleIsDiagramCheck(Doku_Event $event)
71ca5b8841SAndreas Gohr    {
72ca5b8841SAndreas Gohr        if ($event->data !== 'plugin_diagrams_mediafile_isdiagramcheck') return;
73ca5b8841SAndreas Gohr        $event->preventDefault();
74ca5b8841SAndreas Gohr        $event->stopPropagation();
75ca5b8841SAndreas Gohr
76ca5b8841SAndreas Gohr        global $INPUT;
77ca5b8841SAndreas Gohr        $diagram = $INPUT->str('diagram');
78ca5b8841SAndreas Gohr
79ca5b8841SAndreas Gohr        $file = mediaFN(cleanID($diagram));
80ca5b8841SAndreas Gohr        if (!file_exists($file)) {
81ca5b8841SAndreas Gohr            http_status(404);
82ca5b8841SAndreas Gohr            echo 0;
83ca5b8841SAndreas Gohr            return;
84ca5b8841SAndreas Gohr        }
85ca5b8841SAndreas Gohr
86ca5b8841SAndreas Gohr        if (!$this->helper->isDiagramFile($file)) {
87ca5b8841SAndreas Gohr            http_status(403);
88ca5b8841SAndreas Gohr            echo 0;
89ca5b8841SAndreas Gohr        }
90ca5b8841SAndreas Gohr
91ca5b8841SAndreas Gohr        echo 1;
92ca5b8841SAndreas Gohr    }
93ca5b8841SAndreas Gohr
94ca5b8841SAndreas Gohr    /**
9559e7180eSAndreas Gohr     * Check ACL for supplied namespace
9659e7180eSAndreas Gohr     *
9759e7180eSAndreas Gohr     * @param Doku_Event $event AJAX_CALL_UNKNOWN
9859e7180eSAndreas Gohr     */
9959e7180eSAndreas Gohr    public function handleNamespaceCheck(Doku_Event $event)
10059e7180eSAndreas Gohr    {
10159e7180eSAndreas Gohr        if ($event->data !== 'plugin_diagrams_mediafile_nscheck') return;
10259e7180eSAndreas Gohr        $event->preventDefault();
10359e7180eSAndreas Gohr        $event->stopPropagation();
10459e7180eSAndreas Gohr
10559e7180eSAndreas Gohr        global $INPUT;
10659e7180eSAndreas Gohr        $ns = $INPUT->str('ns');
10759e7180eSAndreas Gohr
10859e7180eSAndreas Gohr        echo json_encode(auth_quickaclcheck($ns . ':*') >= AUTH_UPLOAD);
10959e7180eSAndreas Gohr    }
11059e7180eSAndreas Gohr
11159e7180eSAndreas Gohr    /**
112*27b2367eSAndreas Gohr     * Set custom CSP for SVG diagrams
11359e7180eSAndreas Gohr     *
11459e7180eSAndreas Gohr     * @param Doku_Event $event MEDIA_SENDFILE
11559e7180eSAndreas Gohr     */
11659e7180eSAndreas Gohr    public function handleCSP(Doku_Event $event)
11759e7180eSAndreas Gohr    {
11859e7180eSAndreas Gohr        if ($event->data['ext'] === 'svg' && $this->helper->isDiagramFile($event->data['file'])) {
119*27b2367eSAndreas Gohr            $event->data['csp'] = Diagrams::CSP;
12059e7180eSAndreas Gohr        }
12159e7180eSAndreas Gohr    }
122b7bd8fa5SAnna Dabrowska
123b7bd8fa5SAnna Dabrowska    /**
124b7bd8fa5SAnna Dabrowska     * Registers our handler with the move plugin
125b7bd8fa5SAnna Dabrowska     *
126b7bd8fa5SAnna Dabrowska     * @param Doku_Event $event
127b7bd8fa5SAnna Dabrowska     * @return void
128b7bd8fa5SAnna Dabrowska     */
129b7bd8fa5SAnna Dabrowska    public function registerMoveHandler(Doku_Event $event)
130b7bd8fa5SAnna Dabrowska    {
131b7bd8fa5SAnna Dabrowska        $event->data['handlers']['diagrams_mediafile'] = [new \syntax_plugin_diagrams_mediafile(), 'handleMove'];
132b7bd8fa5SAnna Dabrowska    }
1335f757686SAndreas Gohr}
134