xref: /dokuwiki/lib/plugins/extension/admin.php (revision 8553d24d33ab5f260c6e19959de764dd8472d438)
1<?php
2use dokuwiki\Extension\AdminPlugin;
3/**
4 * DokuWiki Plugin extension (Admin Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Michael Hamann <michael@content-space.de>
8 */
9
10/**
11 * Admin part of the extension manager
12 */
13class admin_plugin_extension extends AdminPlugin
14{
15    protected $infoFor;
16    /** @var  helper_plugin_extension_gui */
17    protected $gui;
18
19    /**
20     * Constructor
21     *
22     * loads additional helpers
23     */
24    public function __construct()
25    {
26        $this->gui = plugin_load('helper', 'extension_gui');
27    }
28
29    /**
30     * @return int sort number in admin menu
31     */
32    public function getMenuSort()
33    {
34        return 0;
35    }
36
37    /**
38     * @return bool true if only access for superuser, false is for superusers and moderators
39     */
40    public function forAdminOnly()
41    {
42        return true;
43    }
44
45    /**
46     * Execute the requested action(s) and initialize the plugin repository
47     */
48    public function handle()
49    {
50        global $INPUT;
51        // initialize the remote repository
52        /* @var helper_plugin_extension_repository $repository */
53        $repository = $this->loadHelper('extension_repository');
54
55        if (!$repository->hasAccess(!$INPUT->bool('purge'))) {
56            $url = $this->gui->tabURL('', ['purge' => 1], '&');
57            msg($this->getLang('repo_error').
58                ' [<a href="'.$url.'">'.$this->getLang('repo_retry').'</a>]', -1
59            );
60        }
61
62        if (!in_array('ssl', stream_get_transports())) {
63            msg($this->getLang('nossl'), -1);
64        }
65
66        /* @var helper_plugin_extension_extension $extension */
67        $extension = $this->loadHelper('extension_extension');
68
69        try {
70            if ($INPUT->post->has('fn') && checkSecurityToken()) {
71                $actions = $INPUT->post->arr('fn');
72                foreach ($actions as $action => $extensions) {
73                    foreach ($extensions as $extname => $label) {
74                        switch ($action) {
75                            case 'install':
76                            case 'reinstall':
77                            case 'update':
78                                $extension->setExtension($extname);
79                                $installed = $extension->installOrUpdate();
80                                foreach ($installed as $info) {
81                                    msg(sprintf(
82                                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
83                                        $info['base']), 1
84                                    );
85                                }
86                                break;
87                            case 'uninstall':
88                                $extension->setExtension($extname);
89                                $status = $extension->uninstall();
90                                if ($status) {
91                                    msg(sprintf(
92                                        $this->getLang('msg_delete_success'),
93                                        hsc($extension->getDisplayName())), 1
94                                    );
95                                } else {
96                                    msg(sprintf(
97                                        $this->getLang('msg_delete_failed'),
98                                        hsc($extension->getDisplayName())), -1
99                                    );
100                                }
101                                break;
102                            case 'enable':
103                                $extension->setExtension($extname);
104                                $status = $extension->enable();
105                                if ($status !== true) {
106                                    msg($status, -1);
107                                } else {
108                                    msg(sprintf(
109                                        $this->getLang('msg_enabled'),
110                                        hsc($extension->getDisplayName())), 1
111                                    );
112                                }
113                                break;
114                            case 'disable':
115                                $extension->setExtension($extname);
116                                $status = $extension->disable();
117                                if ($status !== true) {
118                                    msg($status, -1);
119                                } else {
120                                    msg(sprintf(
121                                        $this->getLang('msg_disabled'),
122                                        hsc($extension->getDisplayName())), 1
123                                    );
124                                }
125                                break;
126                        }
127                    }
128                }
129                send_redirect($this->gui->tabURL('', [], '&', true));
130            } elseif ($INPUT->post->str('installurl') && checkSecurityToken()) {
131                $installed = $extension->installFromURL(
132                    $INPUT->post->str('installurl'),
133                    $INPUT->post->bool('overwrite'));
134                foreach ($installed as $info) {
135                    msg(sprintf(
136                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
137                        $info['base']), 1
138                    );
139                }
140                send_redirect($this->gui->tabURL('', [], '&', true));
141            } elseif (isset($_FILES['installfile']) && checkSecurityToken()) {
142                $installed = $extension->installFromUpload('installfile', $INPUT->post->bool('overwrite'));
143                foreach ($installed as $info) {
144                    msg(sprintf(
145                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
146                        $info['base']), 1
147                    );
148                }
149                send_redirect($this->gui->tabURL('', [], '&', true));
150            }
151        } catch (Exception $e) {
152            msg($e->getMessage(), -1);
153            send_redirect($this->gui->tabURL('', [], '&', true));
154        }
155    }
156
157    /**
158     * Render HTML output
159     */
160    public function html()
161    {
162        echo '<h1>'.$this->getLang('menu').'</h1>'.DOKU_LF;
163        echo '<div id="extension__manager">'.DOKU_LF;
164
165        $this->gui->tabNavigation();
166
167        switch ($this->gui->currentTab()) {
168            case 'search':
169                $this->gui->tabSearch();
170                break;
171            case 'templates':
172                $this->gui->tabTemplates();
173                break;
174            case 'install':
175                $this->gui->tabInstall();
176                break;
177            case 'plugins':
178            default:
179                $this->gui->tabPlugins();
180        }
181
182        echo '</div>'.DOKU_LF;
183    }
184}
185
186// vim:ts=4:sw=4:et:
187