1<?php
2
3/**
4 * Handle adding of icons for config managers with uploadable content (implementation of ConfigManagerUploadable)
5 */
6class action_plugin_confmanager_upload extends DokuWiki_Action_Plugin {
7
8    /**
9     * @var helper_plugin_confmanager
10     */
11    var $helper;
12
13    /**
14     * Registers a callback function for a given event
15     *
16     * @param Doku_Event_Handler $controller
17     */
18    public function register(Doku_Event_Handler $controller) {
19        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, 'upload', []);
20        $this->helper = plugin_load('helper', 'confmanager');
21    }
22
23    /**
24     * Tries to add an icon
25     *
26     * @param Doku_Event $event
27     */
28    public function upload(Doku_Event $event) {
29        if ($event->data !== 'confmanager_upload') {
30            return;
31        }
32
33        $event->preventDefault();
34        $event->stopPropagation();
35        if (!auth_isadmin()) {
36            header('HTTP/1.1 403 Forbidden');
37            header('Content-Type: text/plain');
38            echo $this->getLang('upload_errNoAdmin');
39            return;
40        }
41
42        $config = $this->getConfig();
43        if ($config === false) {
44            header('HTTP/1.1 405 Method Not Allowed');
45            header('Content-Type: text/plain');
46            echo $this->getLang('upload_errNoConfig');
47            return;
48        }
49
50        if (!$config->upload()) {
51            header('HTTP/1.1 400 Bad Request');
52            return;
53        }
54        echo '1';
55    }
56
57    /**
58     * Get an config manager in case of uploadable content
59     *
60     * @return bool|ConfigManagerUploadable
61     */
62    private function getConfig() {
63        global $INPUT;
64        $configId = $INPUT->str('configId', null, true);
65        if ($configId === null) {
66            return false;
67        }
68
69        $config = $this->helper->getConfigById($configId);
70        if (!$config) {
71            return false;
72        }
73
74        if (!($config instanceof ConfigManagerUploadable)) {
75            return false;
76        }
77        return $config;
78    }
79}
80