weekly release 2.6dev
[moodle.git] / admin / plugins.php
CommitLineData
b9934a17
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * UI for general plugins management
20 *
c2d2001a
DM
21 * Supported HTTP parameters:
22 *
23 * ?fetchremote=1 - check for available updates
24 * ?updatesonly=1 - display plugins with available update only
25 * ?contribonly=1 - display non-standard add-ons only
26 * ?uninstall=foo_bar - uninstall the given plugin
27 * ?delete=foo_bar - delete the plugin folder (it must not be installed)
28 * &confirm=1 - confirm the uninstall or delete action
29 *
b9934a17
DM
30 * @package core
31 * @subpackage admin
32 * @copyright 2011 David Mudrak <david@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35
36require_once(dirname(dirname(__FILE__)) . '/config.php');
37require_once($CFG->libdir . '/adminlib.php');
38require_once($CFG->libdir . '/pluginlib.php');
436d9447 39require_once($CFG->libdir . '/filelib.php');
b9934a17 40
3204daea 41$fetchremote = optional_param('fetchremote', false, PARAM_BOOL);
4df8bced
DM
42$updatesonly = optional_param('updatesonly', false, PARAM_BOOL);
43$contribonly = optional_param('contribonly', false, PARAM_BOOL);
436d9447
DM
44$uninstall = optional_param('uninstall', '', PARAM_COMPONENT);
45$delete = optional_param('delete', '', PARAM_COMPONENT);
46$confirmed = optional_param('confirm', false, PARAM_BOOL);
47
a3cbc198
PS
48// NOTE: do not use admin_externalpage_setup() here because it loads
49// full admin tree which is not possible during uninstallation.
50
51require_login();
52$syscontext = context_system::instance();
53require_capability('moodle/site:config', $syscontext);
3204daea
DM
54
55$pluginman = plugin_manager::instance();
436d9447
DM
56
57if ($uninstall) {
58 require_sesskey();
a3cbc198
PS
59
60 if (!$confirmed) {
61 admin_externalpage_setup('pluginsoverview');
62 } else {
63 $PAGE->set_url('/admin/plugins.php');
64 $PAGE->set_context($syscontext);
65 $PAGE->set_pagelayout('maintenance');
66 $PAGE->set_popup_notification_allowed(false);
67 }
68
69 /** @var core_admin_renderer $output */
70 $output = $PAGE->get_renderer('core', 'admin');
71
436d9447
DM
72 $pluginfo = $pluginman->get_plugin_info($uninstall);
73
c2d2001a 74 // Make sure we know the plugin.
436d9447
DM
75 if (is_null($pluginfo)) {
76 throw new moodle_exception('err_uninstalling_unknown_plugin', 'core_plugin', '', array('plugin' => $uninstall),
77 'plugin_manager::get_plugin_info() returned null for the plugin to be uninstalled');
78 }
79
c2d2001a
DM
80 $pluginname = $pluginman->plugin_name($pluginfo->component);
81 $PAGE->set_title($pluginname);
82 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
83
84 if (!$pluginman->can_uninstall_plugin($pluginfo->component)) {
85 throw new moodle_exception('err_cannot_uninstall_plugin', 'core_plugin', '',
86 array('plugin' => $pluginfo->component),
87 'plugin_manager::can_uninstall_plugin() returned false');
436d9447
DM
88 }
89
90 if (!$confirmed) {
91 $continueurl = new moodle_url($PAGE->url, array('uninstall' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
92 echo $output->plugin_uninstall_confirm_page($pluginman, $pluginfo, $continueurl);
93 exit();
94
95 } else {
3ca1b546
DM
96 $progress = new progress_trace_buffer(new text_progress_trace(), false);
97 $pluginman->uninstall_plugin($pluginfo->component, $progress);
98 $progress->finished();
436d9447
DM
99
100 if ($pluginman->is_plugin_folder_removable($pluginfo->component)) {
101 $continueurl = new moodle_url($PAGE->url, array('delete' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
3ca1b546 102 echo $output->plugin_uninstall_results_removable_page($pluginman, $pluginfo, $progress, $continueurl);
c05a5099
PS
103 // Reset op code caches.
104 if (function_exists('opcache_reset')) {
105 opcache_reset();
106 }
436d9447
DM
107 exit();
108
109 } else {
3ca1b546 110 echo $output->plugin_uninstall_results_page($pluginman, $pluginfo, $progress);
c05a5099
PS
111 // Reset op code caches.
112 if (function_exists('opcache_reset')) {
113 opcache_reset();
114 }
436d9447
DM
115 exit();
116 }
117 }
118}
119
120if ($delete and $confirmed) {
121 require_sesskey();
a3cbc198
PS
122
123 $PAGE->set_url('/admin/plugins.php');
124 $PAGE->set_context($syscontext);
125 $PAGE->set_pagelayout('maintenance');
126 $PAGE->set_popup_notification_allowed(false);
127
128 /** @var core_admin_renderer $output */
129 $output = $PAGE->get_renderer('core', 'admin');
130
436d9447
DM
131 $pluginfo = $pluginman->get_plugin_info($delete);
132
133 // Make sure we know the plugin.
134 if (is_null($pluginfo)) {
135 throw new moodle_exception('err_removing_unknown_plugin', 'core_plugin', '', array('plugin' => $delete),
136 'plugin_manager::get_plugin_info() returned null for the plugin to be deleted');
137 }
138
c2d2001a
DM
139 $pluginname = $pluginman->plugin_name($pluginfo->component);
140 $PAGE->set_title($pluginname);
141 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
142
436d9447
DM
143 // Make sure it is not installed.
144 if (!is_null($pluginfo->versiondb)) {
145 throw new moodle_exception('err_removing_installed_plugin', 'core_plugin', '',
146 array('plugin' => $pluginfo->component, 'versiondb' => $pluginfo->versiondb),
147 'plugin_manager::get_plugin_info() returned not-null versiondb for the plugin to be deleted');
148 }
149
150 // Make sure the folder is removable.
151 if (!$pluginman->is_plugin_folder_removable($pluginfo->component)) {
152 throw new moodle_exception('err_removing_unremovable_folder', 'core_plugin', '',
153 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir),
154 'plugin root folder is not removable as expected');
155 }
156
157 // Make sure the folder is within Moodle installation tree.
158 if (strpos($pluginfo->rootdir, $CFG->dirroot) !== 0) {
159 throw new moodle_exception('err_unexpected_plugin_rootdir', 'core_plugin', '',
160 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir, 'dirroot' => $CFG->dirroot),
161 'plugin root folder not in the moodle dirroot');
162 }
163
164 // So long, and thanks for all the bugs.
165 fulldelete($pluginfo->rootdir);
c05a5099
PS
166 // Reset op code caches.
167 if (function_exists('opcache_reset')) {
168 opcache_reset();
169 }
82b1cf00
PS
170 // We need to execute upgrade to make sure everything including caches is up to date.
171 redirect(new moodle_url('/admin/index.php'));
436d9447
DM
172}
173
a3cbc198
PS
174admin_externalpage_setup('pluginsoverview');
175
176/** @var core_admin_renderer $output */
177$output = $PAGE->get_renderer('core', 'admin');
178
3204daea
DM
179$checker = available_update_checker::instance();
180
4df8bced
DM
181// Filtering options.
182$options = array(
183 'updatesonly' => $updatesonly,
184 'contribonly' => $contribonly,
185);
186
3204daea 187if ($fetchremote) {
00ea11f8
DM
188 require_sesskey();
189 $checker->fetch();
4df8bced 190 redirect(new moodle_url($PAGE->url, $options));
3204daea
DM
191}
192
bcc8397a
DM
193$deployer = available_update_deployer::instance();
194if ($deployer->enabled()) {
195 $myurl = new moodle_url($PAGE->url, array('updatesonly' => $updatesonly, 'contribonly' => $contribonly));
5d7a4bab 196 $deployer->initialize($myurl, new moodle_url('/admin'));
bcc8397a
DM
197
198 $deploydata = $deployer->submitted_data();
199 if (!empty($deploydata)) {
200 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
201 die();
202 }
203}
204
4df8bced 205echo $output->plugin_management_page($pluginman, $checker, $options);