1<?php
2/**
3 * DokuWiki Plugin extension (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Hamann <michael@content-space.de>
7 */
8
9/**
10 * Admin part of the extension manager
11 */
12class admin_plugin_extension extends DokuWiki_Admin_Plugin
13{
14    protected $infoFor = null;
15    /** @var  helper_plugin_extension_gui */
16    protected $gui;
17
18    /**
19     * Constructor
20     *
21     * loads additional helpers
22     */
23    public function __construct()
24    {
25        $this->gui = plugin_load('helper', 'extension_gui');
26    }
27
28    /**
29     * @return int sort number in admin menu
30     */
31    public function getMenuSort()
32    {
33        return 0;
34    }
35
36    /**
37     * @return bool true if only access for superuser, false is for superusers and moderators
38     */
39    public function forAdminOnly()
40    {
41        return true;
42    }
43
44    /**
45     * Execute the requested action(s) and initialize the plugin repository
46     */
47    public function handle()
48    {
49        global $INPUT;
50        // initialize the remote repository
51        /* @var helper_plugin_extension_repository $repository */
52        $repository = $this->loadHelper('extension_repository');
53
54        if (!$repository->hasAccess(!$INPUT->bool('purge'))) {
55            $url = $this->gui->tabURL('', ['purge' => 1], '&');
56            msg($this->getLang('repo_error').
57                ' [<a href="'.$url.'">'.$this->getLang('repo_retry').'</a>]', -1
58            );
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 $ext => $info) {
80                                    msg(sprintf(
81                                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
82                                        $info['base']), 1
83                                    );
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())), 1
93                                    );
94                                } else {
95                                    msg(sprintf(
96                                        $this->getLang('msg_delete_failed'),
97                                        hsc($extension->getDisplayName())), -1
98                                    );
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())), 1
110                                    );
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())), 1
122                                    );
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                foreach ($installed as $ext => $info) {
134                    msg(sprintf(
135                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
136                        $info['base']), 1
137                    );
138                }
139                send_redirect($this->gui->tabURL('', [], '&', true));
140            } elseif (isset($_FILES['installfile']) && checkSecurityToken()) {
141                $installed = $extension->installFromUpload('installfile', $INPUT->post->bool('overwrite'));
142                foreach ($installed as $ext => $info) {
143                    msg(sprintf(
144                        $this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'),
145                        $info['base']), 1
146                    );
147                }
148                send_redirect($this->gui->tabURL('', [], '&', true));
149            }
150        } catch (Exception $e) {
151            msg($e->getMessage(), -1);
152            send_redirect($this->gui->tabURL('', [], '&', true));
153        }
154    }
155
156    /**
157     * Render HTML output
158     */
159    public function html()
160    {
161        echo '<h1>'.$this->getLang('menu').'</h1>'.DOKU_LF;
162        echo '<div id="extension__manager">'.DOKU_LF;
163
164        $this->gui->tabNavigation();
165
166        switch ($this->gui->currentTab()) {
167            case 'search':
168                $this->gui->tabSearch();
169                break;
170            case 'templates':
171                $this->gui->tabTemplates();
172                break;
173            case 'install':
174                $this->gui->tabInstall();
175                break;
176            case 'plugins':
177            default:
178                $this->gui->tabPlugins();
179        }
180
181        echo '</div>'.DOKU_LF;
182    }
183}
184
185// vim:ts=4:sw=4:et:
186