MDL-39087 Fix plugin_manager::can_uninstall_plugin() implementation
[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 *
21 * @package core
22 * @subpackage admin
23 * @copyright 2011 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once(dirname(dirname(__FILE__)) . '/config.php');
28require_once($CFG->libdir . '/adminlib.php');
29require_once($CFG->libdir . '/pluginlib.php');
436d9447 30require_once($CFG->libdir . '/filelib.php');
b9934a17 31
b9934a17 32admin_externalpage_setup('pluginsoverview');
1c2bf99e 33require_capability('moodle/site:config', context_system::instance());
3204daea
DM
34
35$fetchremote = optional_param('fetchremote', false, PARAM_BOOL);
4df8bced
DM
36$updatesonly = optional_param('updatesonly', false, PARAM_BOOL);
37$contribonly = optional_param('contribonly', false, PARAM_BOOL);
436d9447
DM
38$uninstall = optional_param('uninstall', '', PARAM_COMPONENT);
39$delete = optional_param('delete', '', PARAM_COMPONENT);
40$confirmed = optional_param('confirm', false, PARAM_BOOL);
41
42$output = $PAGE->get_renderer('core', 'admin');
3204daea
DM
43
44$pluginman = plugin_manager::instance();
436d9447
DM
45
46if ($uninstall) {
47 require_sesskey();
48 $pluginfo = $pluginman->get_plugin_info($uninstall);
49
50 if (is_null($pluginfo)) {
51 throw new moodle_exception('err_uninstalling_unknown_plugin', 'core_plugin', '', array('plugin' => $uninstall),
52 'plugin_manager::get_plugin_info() returned null for the plugin to be uninstalled');
53 }
54
55 $requiredby = $pluginman->other_plugins_that_require($pluginfo->component);
56 if (!empty($requiredby)) {
57 throw new moodle_exception('err_uninstalling_required_plugin', 'core_plugin', '',
58 array('plugin' => $pluginfo->component, 'requiredby' => implode(', ', $requiredby)),
59 'plugin_manager::other_plugins_that_require() returned non-empty array');
60 }
61
62 if (!$confirmed) {
63 $continueurl = new moodle_url($PAGE->url, array('uninstall' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
64 echo $output->plugin_uninstall_confirm_page($pluginman, $pluginfo, $continueurl);
65 exit();
66
67 } else {
68 $messages = array(); // Collect uninstall process messages here.
69 $pluginman->uninstall_plugin($pluginfo->component, $messages);
70
71 if ($pluginman->is_plugin_folder_removable($pluginfo->component)) {
72 $continueurl = new moodle_url($PAGE->url, array('delete' => $pluginfo->component, 'sesskey' => sesskey(), 'confirm' => 1));
73 echo $output->plugin_uninstall_results_removable_page($pluginman, $pluginfo, $messages, $continueurl);
74 exit();
75
76 } else {
77 echo $output->plugin_uninstall_results_page($pluginman, $pluginfo, $messages);
78 exit();
79 }
80 }
81}
82
83if ($delete and $confirmed) {
84 require_sesskey();
85 $pluginfo = $pluginman->get_plugin_info($delete);
86
87 // Make sure we know the plugin.
88 if (is_null($pluginfo)) {
89 throw new moodle_exception('err_removing_unknown_plugin', 'core_plugin', '', array('plugin' => $delete),
90 'plugin_manager::get_plugin_info() returned null for the plugin to be deleted');
91 }
92
93 // Make sure it is not installed.
94 if (!is_null($pluginfo->versiondb)) {
95 throw new moodle_exception('err_removing_installed_plugin', 'core_plugin', '',
96 array('plugin' => $pluginfo->component, 'versiondb' => $pluginfo->versiondb),
97 'plugin_manager::get_plugin_info() returned not-null versiondb for the plugin to be deleted');
98 }
99
100 // Make sure the folder is removable.
101 if (!$pluginman->is_plugin_folder_removable($pluginfo->component)) {
102 throw new moodle_exception('err_removing_unremovable_folder', 'core_plugin', '',
103 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir),
104 'plugin root folder is not removable as expected');
105 }
106
107 // Make sure the folder is within Moodle installation tree.
108 if (strpos($pluginfo->rootdir, $CFG->dirroot) !== 0) {
109 throw new moodle_exception('err_unexpected_plugin_rootdir', 'core_plugin', '',
110 array('plugin' => $pluginfo->component, 'rootdir' => $pluginfo->rootdir, 'dirroot' => $CFG->dirroot),
111 'plugin root folder not in the moodle dirroot');
112 }
113
114 // So long, and thanks for all the bugs.
115 fulldelete($pluginfo->rootdir);
116 cache::make('core', 'pluginlist')->purge();
117 redirect($PAGE->url);
118}
119
3204daea
DM
120$checker = available_update_checker::instance();
121
4df8bced
DM
122// Filtering options.
123$options = array(
124 'updatesonly' => $updatesonly,
125 'contribonly' => $contribonly,
126);
127
3204daea 128if ($fetchremote) {
00ea11f8
DM
129 require_sesskey();
130 $checker->fetch();
4df8bced 131 redirect(new moodle_url($PAGE->url, $options));
3204daea
DM
132}
133
bcc8397a
DM
134$deployer = available_update_deployer::instance();
135if ($deployer->enabled()) {
136 $myurl = new moodle_url($PAGE->url, array('updatesonly' => $updatesonly, 'contribonly' => $contribonly));
137 $deployer->initialize($myurl, $myurl);
138
139 $deploydata = $deployer->submitted_data();
140 if (!empty($deploydata)) {
141 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
142 die();
143 }
144}
145
4df8bced 146echo $output->plugin_management_page($pluginman, $checker, $options);