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