xref: /dokuwiki/lib/plugins/extension/admin.php (revision 4b3d1701df2b99bcd239b0ab8e55217f523ea8df)
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(sprintf($this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'), $info['base']), 1);
74                                }
75                                break;
76                            case 'uninstall':
77                                $extension->setExtension($extname);
78                                $status = $extension->uninstall();
79                                if($status) {
80                                    msg(sprintf($this->getLang('msg_delete_success'), hsc($extension->getDisplayName())), 1);
81                                } else {
82                                    msg(sprintf($this->getLang('msg_delete_failed'), hsc($extension->getDisplayName())), -1);
83                                }
84                                break;
85                            case 'enable';
86                                $extension->setExtension($extname);
87                                $status = $extension->enable();
88                                if($status !== true) {
89                                    msg($status, -1);
90                                } else {
91                                    msg(sprintf($this->getLang('msg_enabled'), hsc($extension->getDisplayName())), 1);
92                                }
93                                break;
94                            case 'disable';
95                                $extension->setExtension($extname);
96                                $status = $extension->disable();
97                                if($status !== true) {
98                                    msg($status, -1);
99                                } else {
100                                    msg(sprintf($this->getLang('msg_disabled'), hsc($extension->getDisplayName())), 1);
101                                }
102                                break;
103                        }
104                    }
105                }
106                send_redirect($this->gui->tabURL('', array(), '&', true));
107            } elseif($INPUT->post->str('installurl') && checkSecurityToken()) {
108                $installed = $extension->installFromURL($INPUT->post->str('installurl'));
109                foreach($installed as $ext => $info) {
110                    msg(sprintf($this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'), $info['base']), 1);
111                }
112                send_redirect($this->gui->tabURL('', array(), '&', true));
113            } elseif(isset($_FILES['installfile']) && checkSecurityToken()) {
114                $installed = $extension->installFromUpload('installfile');
115                foreach($installed as $ext => $info) {
116                    msg(sprintf($this->getLang('msg_'.$info['type'].'_'.$info['action'].'_success'), $info['base']), 1);
117                }
118                send_redirect($this->gui->tabURL('', array(), '&', true));
119            }
120
121        } catch(Exception $e) {
122            msg($e->getMessage(), -1);
123            send_redirect($this->gui->tabURL('', array(), '&', true));
124        }
125
126    }
127
128    /**
129     * Render HTML output
130     */
131    public function html() {
132        ptln('<h1>'.$this->getLang('menu').'</h1>');
133        ptln('<div id="extension__manager">');
134
135        $this->gui->tabNavigation();
136
137        switch($this->gui->currentTab()) {
138            case 'search':
139                $this->gui->tabSearch();
140                break;
141            case 'templates':
142                $this->gui->tabTemplates();
143                break;
144            case 'install':
145                $this->gui->tabInstall();
146                break;
147            case 'plugins':
148            default:
149                $this->gui->tabPlugins();
150        }
151
152        ptln('</div>');
153    }
154}
155
156// vim:ts=4:sw=4:et:
157