xref: /dokuwiki/lib/plugins/extension/action.php (revision fd51614b39b1320c5723e8195f0bf69c25baaeb7)
172dda0b4SAndreas Gohr<?php
272dda0b4SAndreas Gohr/** DokuWiki Plugin extension (Action Component)
372dda0b4SAndreas Gohr *
472dda0b4SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
572dda0b4SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
672dda0b4SAndreas Gohr */
772dda0b4SAndreas Gohr
872dda0b4SAndreas Gohr// must be run within Dokuwiki
972dda0b4SAndreas Gohrif(!defined('DOKU_INC')) die();
1072dda0b4SAndreas Gohr
1172dda0b4SAndreas Gohrclass action_plugin_extension extends DokuWiki_Action_Plugin {
1272dda0b4SAndreas Gohr
1372dda0b4SAndreas Gohr    /**
1472dda0b4SAndreas Gohr     * Registers a callback function for a given event
1572dda0b4SAndreas Gohr     *
1672dda0b4SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
1772dda0b4SAndreas Gohr     * @return void
1872dda0b4SAndreas Gohr     */
194c005e3fSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
2072dda0b4SAndreas Gohr
2172dda0b4SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'info');
2272dda0b4SAndreas Gohr
2372dda0b4SAndreas Gohr    }
2472dda0b4SAndreas Gohr
254c005e3fSAndreas Gohr    /**
264c005e3fSAndreas Gohr     * Create the detail info for a single plugin
274c005e3fSAndreas Gohr     *
284c005e3fSAndreas Gohr     * @param Doku_Event $event
294c005e3fSAndreas Gohr     * @param            $param
304c005e3fSAndreas Gohr     */
3172dda0b4SAndreas Gohr    public function info(Doku_Event &$event, $param) {
32da5f0eeeSAndreas Gohr        global $USERINFO;
3372dda0b4SAndreas Gohr        global $INPUT;
34da5f0eeeSAndreas Gohr
35b15cd32dSjgpcx        if($event->data != 'plugin_extension') return;
36b15cd32dSjgpcx        $event->preventDefault();
37b15cd32dSjgpcx        $event->stopPropagation();
38b15cd32dSjgpcx
39da5f0eeeSAndreas Gohr        if(empty($_SERVER['REMOTE_USER']) || !auth_isadmin($_SERVER['REMOTE_USER'], $USERINFO['grps'])) {
40da5f0eeeSAndreas Gohr            http_status(403);
41da5f0eeeSAndreas Gohr            echo 'Forbidden';
42da5f0eeeSAndreas Gohr            exit;
43da5f0eeeSAndreas Gohr        }
44da5f0eeeSAndreas Gohr
4572dda0b4SAndreas Gohr        $ext = $INPUT->str('ext');
4672dda0b4SAndreas Gohr        if(!$ext) {
47*fd51614bSAndreas Gohr            http_status(400);
4872dda0b4SAndreas Gohr            echo 'no extension given';
4972dda0b4SAndreas Gohr            return;
5072dda0b4SAndreas Gohr        }
5172dda0b4SAndreas Gohr
5272dda0b4SAndreas Gohr        /** @var helper_plugin_extension_extension $extension */
5372dda0b4SAndreas Gohr        $extension = plugin_load('helper', 'extension_extension');
5472dda0b4SAndreas Gohr        $extension->setExtension($ext);
5572dda0b4SAndreas Gohr
56*fd51614bSAndreas Gohr        $act = $INPUT->str('act');
57*fd51614bSAndreas Gohr        switch($act) {
58*fd51614bSAndreas Gohr            case 'enable':
59*fd51614bSAndreas Gohr            case 'disable':
60*fd51614bSAndreas Gohr                $json = new JSON();
61*fd51614bSAndreas Gohr                $extension->$act(); //enables/disables
62*fd51614bSAndreas Gohr
63*fd51614bSAndreas Gohr                $reverse = ($act == 'disable') ? 'enable' : 'disable';
64*fd51614bSAndreas Gohr
65*fd51614bSAndreas Gohr                $return = array(
66*fd51614bSAndreas Gohr                    'state'   => $act.'d', // isn't English wonderful? :-)
67*fd51614bSAndreas Gohr                    'reverse' => $reverse,
68*fd51614bSAndreas Gohr                    'label'   => $extension->getLang('btn_'.$reverse)
69*fd51614bSAndreas Gohr                );
70*fd51614bSAndreas Gohr
71*fd51614bSAndreas Gohr                header('Content-Type: application/json');
72*fd51614bSAndreas Gohr                echo $json->encode($return);
73*fd51614bSAndreas Gohr                break;
74*fd51614bSAndreas Gohr
75*fd51614bSAndreas Gohr            case 'info':
76*fd51614bSAndreas Gohr            default:
7772dda0b4SAndreas Gohr                /** @var helper_plugin_extension_list $list */
7872dda0b4SAndreas Gohr                $list = plugin_load('helper', 'extension_list');
79*fd51614bSAndreas Gohr                header('Content-Type: text/html; charset=utf-8');
8072dda0b4SAndreas Gohr                echo $list->make_info($extension);
8172dda0b4SAndreas Gohr        }
82*fd51614bSAndreas Gohr    }
8372dda0b4SAndreas Gohr
8472dda0b4SAndreas Gohr}
8572dda0b4SAndreas Gohr
86