1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/** DokuWiki Plugin extension (Action Component)
8 *
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10 * @author  Andreas Gohr <andi@splitbrain.org>
11 */
12class action_plugin_extension extends ActionPlugin
13{
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param EventHandler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(EventHandler $controller)
21    {
22        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'info');
23    }
24
25    /**
26     * Create the detail info for a single plugin
27     *
28     * @param Event $event
29     * @param $param
30     */
31    public function info(Event $event, $param)
32    {
33        global $USERINFO;
34        global $INPUT;
35
36        if ($event->data != 'plugin_extension') return;
37        $event->preventDefault();
38        $event->stopPropagation();
39
40        /** @var admin_plugin_extension $admin */
41        $admin = plugin_load('admin', 'extension');
42        if (!$admin->isAccessibleByCurrentUser()) {
43            http_status(403);
44            echo 'Forbidden';
45            exit;
46        }
47
48        $ext = $INPUT->str('ext');
49        if (!$ext) {
50            http_status(400);
51            echo 'no extension given';
52            return;
53        }
54
55        /** @var helper_plugin_extension_extension $extension */
56        $extension = plugin_load('helper', 'extension_extension');
57        $extension->setExtension($ext);
58
59        $act = $INPUT->str('act');
60        switch ($act) {
61            case 'enable':
62            case 'disable':
63                if (getSecurityToken() != $INPUT->str('sectok')) {
64                    http_status(403);
65                    echo 'Security Token did not match. Possible CSRF attack.';
66                    return;
67                }
68
69                $extension->$act(); //enables/disables
70                $reverse = ($act == 'disable') ? 'enable' : 'disable';
71
72                $return = [
73                    'state'   => $act . 'd', // isn't English wonderful? :-)
74                    'reverse' => $reverse,
75                    'label'   => $extension->getLang('btn_' . $reverse),
76                ];
77
78                header('Content-Type: application/json');
79                echo json_encode($return, JSON_THROW_ON_ERROR);
80                break;
81
82            case 'info':
83            default:
84                /** @var helper_plugin_extension_list $list */
85                $list = plugin_load('helper', 'extension_list');
86                header('Content-Type: text/html; charset=utf-8');
87                echo $list->makeInfo($extension);
88        }
89    }
90}
91