xref: /dokuwiki/lib/plugins/extension/admin.php (revision a19c9aa0217112e3ab7ebc160354c7e9fbabe8eb)
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        if (!in_array('ssl', stream_get_transports())) {
62            msg($this->getLang('nossl'), -1);
63        }
64
65        /* @var helper_plugin_extension_extension $extension */
66        $extension = $this->loadHelper('extension_extension');
67
68        try {
69            if ($INPUT->post->has('fn') && checkSecurityToken()) {
70                $actions = $INPUT->post->arr('fn');
71                foreach ($actions as $action => $extensions) {
72                    foreach ($extensions as $extname => $label) {
73                        switch ($action) {
74                            case 'install':
75                            case 'reinstall':
76                            case 'update':
77                                $extension->setExtension($extname);
78                                $installed = $extension->installOrUpdate();
79                                foreach ($installed as $info) {
80                                    msg(sprintf(
81                                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
82                                        $info['base']
83                                    ), 1);
84                                }
85                                break;
86                            case 'uninstall':
87                                $extension->setExtension($extname);
88                                $status = $extension->uninstall();
89                                if ($status) {
90                                    msg(sprintf(
91                                        $this->getLang('msg_delete_success'),
92                                        hsc($extension->getDisplayName())
93                                    ), 1);
94                                } else {
95                                    msg(sprintf(
96                                        $this->getLang('msg_delete_failed'),
97                                        hsc($extension->getDisplayName())
98                                    ), -1);
99                                }
100                                break;
101                            case 'enable':
102                                $extension->setExtension($extname);
103                                $status = $extension->enable();
104                                if ($status !== true) {
105                                    msg($status, -1);
106                                } else {
107                                    msg(sprintf(
108                                        $this->getLang('msg_enabled'),
109                                        hsc($extension->getDisplayName())
110                                    ), 1);
111                                }
112                                break;
113                            case 'disable':
114                                $extension->setExtension($extname);
115                                $status = $extension->disable();
116                                if ($status !== true) {
117                                    msg($status, -1);
118                                } else {
119                                    msg(sprintf(
120                                        $this->getLang('msg_disabled'),
121                                        hsc($extension->getDisplayName())
122                                    ), 1);
123                                }
124                                break;
125                        }
126                    }
127                }
128                send_redirect($this->gui->tabURL('', [], '&', true));
129            } elseif ($INPUT->post->str('installurl') && checkSecurityToken()) {
130                $installed = $extension->installFromURL(
131                    $INPUT->post->str('installurl'),
132                    $INPUT->post->bool('overwrite')
133                );
134                foreach ($installed as $info) {
135                    msg(sprintf(
136                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
137                        $info['base']
138                    ), 1);
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']
147                    ), 1);
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