1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\plugin\extension\Exception as RepoException;
5use dokuwiki\plugin\extension\Extension;
6use dokuwiki\plugin\extension\Gui;
7use dokuwiki\plugin\extension\GuiAdmin;
8use dokuwiki\plugin\extension\Installer;
9use dokuwiki\plugin\extension\Repository;
10
11/**
12 * DokuWiki Plugin extension (Admin Component)
13 *
14 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
15 */
16class admin_plugin_extension extends AdminPlugin
17{
18    /**
19     * Execute the requested action(s) and initialize the plugin repository
20     */
21    public function handle()
22    {
23        global $INPUT;
24
25        // check access to the repository and SSL support
26        $repo = Repository::getInstance();
27        try {
28            $repo->checkAccess();
29        } catch (RepoException $e) {
30            msg($e->getMessage(), -1);
31        }
32
33        // Only continue if there is something to do
34        if (!$INPUT->post->has('fn') && !$INPUT->post->str('installurl') && !isset($_FILES['installfile'])) {
35            return; // nothing to do
36        }
37        if (!checkSecurityToken()) return;
38
39        // Run actions on the installer
40        $installer = new Installer($INPUT->post->bool('overwrite'));
41        try {
42            foreach ($INPUT->post->arr('fn') as $action => $extensions) {
43                foreach ($extensions as $extension => $label) {
44                    $ext = Extension::createFromId($extension);
45                    switch ($action) {
46                        case 'install':
47                        case 'reinstall':
48                        case 'update':
49                            $installer->installExtension($ext);
50                            break;
51                        case 'uninstall':
52                            $installer->uninstall($ext);
53                            break;
54                        case 'enable':
55                            $ext->enable();
56                            break;
57                        case 'disable':
58                            $ext->disable();
59                            break;
60                    }
61                }
62            }
63            if ($INPUT->post->str('installurl')) {
64                $installer->installFromURL($INPUT->post->str('installurl'));
65            } elseif (isset($_FILES['installfile']) && $_FILES['installfile']['error'] !== UPLOAD_ERR_NO_FILE) {
66                $installer->installFromUpload('installfile');
67            }
68        } catch (Exception $e) {
69            msg(hsc($e->getMessage()), -1);
70        }
71
72        // Report results of the installer
73        $processed = $installer->getProcessed();
74        foreach ($processed as $id => $status) {
75            if ($status == Installer::STATUS_INSTALLED) {
76                msg(sprintf($this->getLang('msg_install_success'), $id), 1);
77            } elseif ($status == Installer::STATUS_UPDATED) {
78                msg(sprintf($this->getLang('msg_update_success'), $id), 1);
79            } elseif ($status == Installer::STATUS_SKIPPED) {
80                msg(sprintf($this->getLang('msg_nooverwrite'), $id), 0);
81            } elseif ($status == Installer::STATUS_REMOVED) {
82                msg(sprintf($this->getLang('msg_delete_success'), $id), 1);
83            }
84        }
85
86        // Redirect to clear the POST data
87        $gui = new Gui();
88        send_redirect($gui->tabURL($gui->currentTab(), [], '&', true));
89    }
90
91    /**
92     * Render HTML output
93     */
94    public function html()
95    {
96        echo '<h1>' . $this->getLang('menu') . '</h1>';
97
98        $gui = new GuiAdmin();
99        echo $gui->render();
100    }
101}
102