MDL-41245 fix new addon redirect regression
[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
b9934a17 41admin_externalpage_setup('pluginsoverview');
1c2bf99e 42require_capability('moodle/site:config', context_system::instance());
3204daea
DM
43
44$fetchremote = optional_param('fetchremote', false, PARAM_BOOL);
4df8bced
DM
45$updatesonly = optional_param('updatesonly', false, PARAM_BOOL);
46$contribonly = optional_param('contribonly', false, PARAM_BOOL);
436d9447
DM
47$uninstall = optional_param('uninstall', '', PARAM_COMPONENT);
48$delete = optional_param('delete', '', PARAM_COMPONENT);
49$confirmed = optional_param('confirm', false, PARAM_BOOL);
50
82b1cf00 51/** @var core_admin_renderer $output */
436d9447 52$output = $PAGE->get_renderer('core', 'admin');
3204daea
DM
53
54$pluginman = plugin_manager::instance();
436d9447
DM
55
56if ($uninstall) {
57 require_sesskey();
58 $pluginfo = $pluginman->get_plugin_info($uninstall);
59
c2d2001a 60 // Make sure we know the plugin.
436d9447
DM
61 if (is_null($pluginfo)) {
62 throw new moodle_exception('err_uninstalling_unknown_plugin', 'core_plugin', '', array('plugin' => $uninstall),
63 'plugin_manager::get_plugin_info() returned null for the plugin to be uninstalled');
64 }
65
c2d2001a
DM
66 $pluginname = $pluginman->plugin_name($pluginfo->component);
67 $PAGE->set_title($pluginname);
68 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
69
70 if (!$pluginman->can_uninstall_plugin($pluginfo->component)) {
71 throw new moodle_exception('err_cannot_uninstall_plugin', 'core_plugin', '',
72 array('plugin' => $pluginfo->component),
73 'plugin_manager::can_uninstall_plugin() returned false');
436d9447
DM
74 }
75
76 if (!$confirmed) {
77 $continueurl = new moodle_url($PAGE->url, array('uninstall' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
78 echo $output->plugin_uninstall_confirm_page($pluginman, $pluginfo, $continueurl);
79 exit();
80
81 } else {
3ca1b546
DM
82 $progress = new progress_trace_buffer(new text_progress_trace(), false);
83 $pluginman->uninstall_plugin($pluginfo->component, $progress);
84 $progress->finished();
436d9447
DM
85
86 if ($pluginman->is_plugin_folder_removable($pluginfo->component)) {
87 $continueurl = new moodle_url($PAGE->url, array('delete' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
3ca1b546 88 echo $output->plugin_uninstall_results_removable_page($pluginman, $pluginfo, $progress, $continueurl);
c05a5099
PS
89 // Reset op code caches.
90 if (function_exists('opcache_reset')) {
91 opcache_reset();
92 }
436d9447
DM
93 exit();
94
95 } else {
3ca1b546 96 echo $output->plugin_uninstall_results_page($pluginman, $pluginfo, $progress);
c05a5099
PS
97 // Reset op code caches.
98 if (function_exists('opcache_reset')) {
99 opcache_reset();
100 }
436d9447
DM
101 exit();
102 }
103 }
104}
105
106if ($delete and $confirmed) {
107 require_sesskey();
108 $pluginfo = $pluginman->get_plugin_info($delete);
109
110 // Make sure we know the plugin.
111 if (is_null($pluginfo)) {
112 throw new moodle_exception('err_removing_unknown_plugin', 'core_plugin', '', array('plugin' => $delete),
113 'plugin_manager::get_plugin_info() returned null for the plugin to be deleted');
114 }
115
c2d2001a
DM
116 $pluginname = $pluginman->plugin_name($pluginfo->component);
117 $PAGE->set_title($pluginname);
118 $PAGE->navbar->add(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
119
436d9447
DM
120 // Make sure it is not installed.
121 if (!is_null($pluginfo->versiondb)) {
122 throw new moodle_exception('err_removing_installed_plugin', 'core_plugin', '',
123 array('plugin' => $pluginfo->component, 'versiondb' => $pluginfo->versiondb),
124 'plugin_manager::get_plugin_info() returned not-null versiondb for the plugin to be deleted');
125 }
126
127 // Make sure the folder is removable.
128 if (!$pluginman->is_plugin_folder_removable($pluginfo->component)) {
129 throw new moodle_exception('err_removing_unremovable_folder', 'core_plugin', '',
130 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir),
131 'plugin root folder is not removable as expected');
132 }
133
134 // Make sure the folder is within Moodle installation tree.
135 if (strpos($pluginfo->rootdir, $CFG->dirroot) !== 0) {
136 throw new moodle_exception('err_unexpected_plugin_rootdir', 'core_plugin', '',
137 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir, 'dirroot' => $CFG->dirroot),
138 'plugin root folder not in the moodle dirroot');
139 }
140
141 // So long, and thanks for all the bugs.
142 fulldelete($pluginfo->rootdir);
c05a5099
PS
143 // Reset op code caches.
144 if (function_exists('opcache_reset')) {
145 opcache_reset();
146 }
82b1cf00
PS
147 // We need to execute upgrade to make sure everything including caches is up to date.
148 redirect(new moodle_url('/admin/index.php'));
436d9447
DM
149}
150
3204daea
DM
151$checker = available_update_checker::instance();
152
4df8bced
DM
153// Filtering options.
154$options = array(
155 'updatesonly' => $updatesonly,
156 'contribonly' => $contribonly,
157);
158
3204daea 159if ($fetchremote) {
00ea11f8
DM
160 require_sesskey();
161 $checker->fetch();
4df8bced 162 redirect(new moodle_url($PAGE->url, $options));
3204daea
DM
163}
164
bcc8397a
DM
165$deployer = available_update_deployer::instance();
166if ($deployer->enabled()) {
167 $myurl = new moodle_url($PAGE->url, array('updatesonly' => $updatesonly, 'contribonly' => $contribonly));
5d7a4bab 168 $deployer->initialize($myurl, new moodle_url('/admin'));
bcc8397a
DM
169
170 $deploydata = $deployer->submitted_data();
171 if (!empty($deploydata)) {
172 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
173 die();
174 }
175}
176
4df8bced 177echo $output->plugin_management_page($pluginman, $checker, $options);