xref: /plugin/diagrams/action/mediafile.php (revision 5f757686d683fd550247221d4a6d2848f21773e6)
1*5f757686SAndreas Gohr<?php
2*5f757686SAndreas Gohr
3*5f757686SAndreas Gohr/**
4*5f757686SAndreas Gohr * Action component of diagrams plugin
5*5f757686SAndreas Gohr *
6*5f757686SAndreas Gohr */
7*5f757686SAndreas Gohrclass action_plugin_diagrams_mediafile extends DokuWiki_Action_Plugin
8*5f757686SAndreas Gohr{
9*5f757686SAndreas Gohr
10*5f757686SAndreas Gohr    /**
11*5f757686SAndreas Gohr     * Registers a callback function for a given event
12*5f757686SAndreas Gohr     *
13*5f757686SAndreas Gohr     * @param \Doku_Event_Handler $controller
14*5f757686SAndreas Gohr     */
15*5f757686SAndreas Gohr    public function register(Doku_Event_Handler $controller)
16*5f757686SAndreas Gohr    {
17*5f757686SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleEditCheck');
18*5f757686SAndreas Gohr    }
19*5f757686SAndreas Gohr
20*5f757686SAndreas Gohr    /**
21*5f757686SAndreas Gohr     * Check all supplied diagrams and return only editable diagrams
22*5f757686SAndreas Gohr     *
23*5f757686SAndreas Gohr     * @param Doku_Event $event
24*5f757686SAndreas Gohr     */
25*5f757686SAndreas Gohr    public function handleEditCheck(Doku_Event $event)
26*5f757686SAndreas Gohr    {
27*5f757686SAndreas Gohr        if ($event->data !== 'plugin_diagrams_mediafile_editcheck') return;
28*5f757686SAndreas Gohr        $event->preventDefault();
29*5f757686SAndreas Gohr        $event->stopPropagation();
30*5f757686SAndreas Gohr
31*5f757686SAndreas Gohr        global $INPUT;
32*5f757686SAndreas Gohr        $diagrams = (array) json_decode($INPUT->str('diagrams'));
33*5f757686SAndreas Gohr
34*5f757686SAndreas Gohr        /** @var helper_plugin_diagrams $helper */
35*5f757686SAndreas Gohr        $helper = plugin_load('helper', 'diagrams');
36*5f757686SAndreas Gohr
37*5f757686SAndreas Gohr        $editable = [];
38*5f757686SAndreas Gohr        foreach ($diagrams as $image) {
39*5f757686SAndreas Gohr            $image = cleanID($image);
40*5f757686SAndreas Gohr            $file = mediaFN($image);
41*5f757686SAndreas Gohr
42*5f757686SAndreas Gohr            if (
43*5f757686SAndreas Gohr                file_exists($file) &&
44*5f757686SAndreas Gohr                auth_quickaclcheck($image) >= AUTH_UPLOAD &&
45*5f757686SAndreas Gohr                $helper->isDiagramFile($file)
46*5f757686SAndreas Gohr            ) {
47*5f757686SAndreas Gohr                $editable[] = $image;
48*5f757686SAndreas Gohr            }
49*5f757686SAndreas Gohr        }
50*5f757686SAndreas Gohr
51*5f757686SAndreas Gohr        echo json_encode($editable);
52*5f757686SAndreas Gohr    }
53*5f757686SAndreas Gohr
54*5f757686SAndreas Gohr}
55