xref: /dokuwiki/lib/plugins/extension/admin.php (revision a4337320b350017434b0e9e35aa4dd73fe968082)
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()) {
55            $url = $this->gui->tabURL('', array('purge' => 1));
56            msg($this->getLang('repo_error').' [<a href="'.$url.'">'.$this->getLang('repo_retry').'</a>]', -1);
57        }
58
59        if (!in_array('ssl', stream_get_transports())) {
60            msg($this->getLang('nossl'), -1);
61        }
62
63        /* @var helper_plugin_extension_extension $extension */
64        $extension = $this->loadHelper('extension_extension');
65
66        try {
67            if ($INPUT->post->has('fn') && checkSecurityToken()) {
68                $actions = $INPUT->post->arr('fn');
69                foreach ($actions as $action => $extensions) {
70                    foreach ($extensions as $extname => $label) {
71                        switch ($action) {
72                            case 'install':
73                            case 'reinstall':
74                            case 'update':
75                                $extension->setExtension($extname);
76                                $installed = $extension->installOrUpdate();
77                                foreach ($installed as $ext => $info) {
78                                    msg(
79                                        sprintf(
80                                            $this->getLang('msg_' . $info['type'] . '_' . $info['action'] . '_success'),
81                                            $info['base']
82                                        ),
83                                        1
84                                    );
85                                }
86                                break;
87                            case 'uninstall':
88                                $extension->setExtension($extname);
89                                $status = $extension->uninstall();
90                                if ($status) {
91                                    msg(
92                                        sprintf(
93                                            $this->getLang('msg_delete_success'),
94                                            hsc($extension->getDisplayName())
95                                        ),
96                                        1
97                                    );
98                                } else {
99                                    msg(
100                                        sprintf(
101                                            $this->getLang('msg_delete_failed'),
102                                            hsc($extension->getDisplayName())
103                                        ),
104                                        -1
105                                    );
106                                }
107                                break;
108                            case 'enable':
109                                $extension->setExtension($extname);
110                                $status = $extension->enable();
111                                if ($status !== true) {
112                                    msg($status, -1);
113                                } else {
114                                    msg(sprintf($this->getLang('msg_enabled'), hsc($extension->getDisplayName())), 1);
115                                }
116                                break;
117                            case 'disable':
118                                $extension->setExtension($extname);
119                                $status = $extension->disable();
120                                if ($status !== true) {
121                                    msg($status, -1);
122                                } else {
123                                    msg(sprintf($this->getLang('msg_disabled'), hsc($extension->getDisplayName())), 1);
124                                }
125                                break;
126                        }
127                    }
128                }
129                send_redirect($this->gui->tabURL('', array(), '&', true));
130            } elseif ($INPUT->post->str('installurl') && checkSecurityToken()) {
131                $installed = $extension->installFromURL($INPUT->post->str('installurl'));
132                foreach ($installed as $ext => $info) {
133                    msg(sprintf($this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'), $info['base']), 1);
134                }
135                send_redirect($this->gui->tabURL('', array(), '&', true));
136            } elseif (isset($_FILES['installfile']) && checkSecurityToken()) {
137                $installed = $extension->installFromUpload('installfile');
138                foreach ($installed as $ext => $info) {
139                    msg(sprintf($this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'), $info['base']), 1);
140                }
141                send_redirect($this->gui->tabURL('', array(), '&', true));
142            }
143        } catch (Exception $e) {
144            msg($e->getMessage(), -1);
145            send_redirect($this->gui->tabURL('', array(), '&', true));
146        }
147    }
148
149    /**
150     * Render HTML output
151     */
152    public function html()
153    {
154        ptln('<h1>'.$this->getLang('menu').'</h1>');
155        ptln('<div id="extension__manager">');
156
157        $this->gui->tabNavigation();
158
159        switch ($this->gui->currentTab()) {
160            case 'search':
161                $this->gui->tabSearch();
162                break;
163            case 'templates':
164                $this->gui->tabTemplates();
165                break;
166            case 'install':
167                $this->gui->tabInstall();
168                break;
169            case 'plugins':
170            default:
171                $this->gui->tabPlugins();
172        }
173
174        ptln('</div>');
175    }
176}
177
178// vim:ts=4:sw=4:et:
179