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