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