xref: /dokuwiki/lib/plugins/extension/admin.php (revision 79ff0cd0c6f2643b1208755842379c8042176441)
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\Installer;
8use dokuwiki\plugin\extension\Repository;
9
10/**
11 * DokuWiki Plugin extension (Admin Component)
12 *
13 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
14 * @author  Michael Hamann <michael@content-space.de>
15 */
16
17/**
18 * Admin part of the extension manager
19 */
20class admin_plugin_extension extends AdminPlugin
21{
22    protected $infoFor;
23    /** @var  helper_plugin_extension_gui */
24    protected $gui;
25
26    /**
27     * Constructor
28     *
29     * loads additional helpers
30     */
31    public function __construct()
32    {
33        $this->gui = plugin_load('helper', 'extension_gui');
34    }
35
36    /**
37     * @return int sort number in admin menu
38     */
39    public function getMenuSort()
40    {
41        return 0;
42    }
43
44    /**
45     * @return bool true if only access for superuser, false is for superusers and moderators
46     */
47    public function forAdminOnly()
48    {
49        return true;
50    }
51
52    /**
53     * Execute the requested action(s) and initialize the plugin repository
54     */
55    public function handle()
56    {
57        global $INPUT;
58
59        // check access to the repository and SSL support
60        $repo = Repository::getInstance();
61        try {
62            $repo->checkAccess();
63        } catch (RepoException $e) {
64            msg($e->getMessage(), -1);
65        }
66
67        // Only continue if there is something to do
68        if (!$INPUT->post->has('fn') && !$INPUT->post->str('installurl') && !isset($_FILES['installfile'])) {
69            return; // nothing to do
70        }
71        if (!checkSecurityToken()) return;
72
73        // Run actions on the installer
74        $installer = new Installer($INPUT->post->bool('overwrite'));
75        try {
76            foreach ($INPUT->post->arr('fn') as $action => $extensions) {
77                foreach ($extensions as $extension => $label) {
78                    $ext = Extension::createFromId($extension);
79                    switch ($action) {
80                        case 'install':
81                        case 'reinstall':
82                        case 'update':
83                            $installer->installExtension($ext);
84                            break;
85                        case 'uninstall':
86                            $installer->uninstall($ext);
87                            break;
88                        case 'enable':
89                            $ext->enable();
90                            break;
91                        case 'disable':
92                            $ext->disable();
93                            break;
94                    }
95                }
96            }
97            if ($INPUT->post->str('installurl')) {
98                $installer->installFromURL($INPUT->post->str('installurl'));
99            }
100            if (isset($_FILES['installfile'])) {
101                $installer->installFromUpload('installfile');
102            }
103        } catch (Exception $e) {
104            msg(hsc($e->getMessage()), -1);
105        }
106
107        // Report results of the installer
108        $processed = $installer->getProcessed();
109        foreach ($processed as $id => $status) {
110            if ($status == Installer::STATUS_INSTALLED) {
111                msg(sprintf($this->getLang('msg_install_success'), $id), 1);
112            } else if ($status == Installer::STATUS_UPDATED) {
113                msg(sprintf($this->getLang('msg_update_success'), $id), 1);
114            } else if ($status == Installer::STATUS_SKIPPED) {
115                msg(sprintf($this->getLang('msg_nooverwrite'), $id), 0);
116            } else if ($status == Installer::STATUS_REMOVED) {
117                msg(sprintf($this->getLang('msg_delete_success'), $id), 1);
118            }
119        }
120
121        send_redirect((new Gui())->tabURL('', [], '&', true));
122    }
123
124    /**
125     * Render HTML output
126     */
127    public function html()
128    {
129        echo '<h1>' . $this->getLang('menu') . '</h1>';
130
131        $gui = new \dokuwiki\plugin\extension\GuiAdmin();
132        echo $gui->render();
133    }
134}
135
136// vim:ts=4:sw=4:et:
137