Merge branch 'MDL-74537-400-enfix' of https://github.com/vmdef/moodle into MOODLE_400...
[moodle.git] / admin / index.php
blob9732be58ddfe48d68840df72164b2265ea22c69d
1 <?php
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.
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/>.
18 /**
19 * Main administration script.
21 * @package core
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // Check that config.php exists, if not then call the install script
27 if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
29 die();
32 // Check that PHP is of a sufficient version as soon as possible.
33 require_once(__DIR__.'/../lib/phpminimumversionlib.php');
34 moodle_require_minimum_php_version();
36 // make sure iconv is available and actually works
37 if (!function_exists('iconv')) {
38 // this should not happen, this must be very borked install
39 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
40 die();
43 // Make sure php5-json is available.
44 if (!function_exists('json_encode') || !function_exists('json_decode')) {
45 // This also shouldn't happen.
46 echo 'Moodle requires the json PHP extension. Please install or enable the json extension.';
47 die();
50 // Make sure xml extension is available.
51 if (!extension_loaded('xml')) {
52 echo 'Moodle requires the xml PHP extension. Please install or enable the xml extension.';
53 die();
56 // Make sure mbstring extension is available.
57 if (!extension_loaded('mbstring')) {
58 echo 'Moodle requires the mbstring PHP extension. Please install or enable the mbstring extension.';
59 die();
62 define('NO_OUTPUT_BUFFERING', true);
64 if (isset($_POST['upgradekey'])) {
65 // Before you start reporting issues about the collision attacks against
66 // SHA-1, you should understand that we are not actually attempting to do
67 // any cryptography here. This is hashed purely so that the key is not
68 // that apparent in the address bar itself. Anyone who catches the HTTP
69 // traffic can immediately use it as a valid admin key.
70 header('Location: index.php?cache=0&upgradekeyhash='.sha1($_POST['upgradekey']));
71 die();
74 if ((isset($_GET['cache']) and $_GET['cache'] === '0')
75 or (isset($_POST['cache']) and $_POST['cache'] === '0')
76 or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
77 // Prevent caching at all cost when visiting this page directly,
78 // we redirect to self once we known no upgrades are necessary.
79 // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
80 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
81 define('CACHE_DISABLE_ALL', true);
83 // Force OPcache reset if used, we do not want any stale caches
84 // when detecting if upgrade necessary or when running upgrade.
85 if (function_exists('opcache_reset')) {
86 opcache_reset();
88 $cache = 0;
90 } else {
91 $cache = 1;
94 require('../config.php');
96 // Invalidate the cache of version.php in any circumstances to help core_component
97 // detecting if the version has changed and component cache should be reset.
98 if (function_exists('opcache_invalidate')) {
99 opcache_invalidate($CFG->dirroot . '/version.php', true);
101 // Make sure the component cache gets rebuilt if necessary, any method that
102 // indirectly calls the protected init() method is good here.
103 core_component::get_core_subsystems();
105 if (is_major_upgrade_required() && isloggedin()) {
106 // A major upgrade is required.
107 // Terminate the session and redirect back here before anything DB-related happens.
108 redirect_if_major_upgrade_required();
111 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
112 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
114 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
115 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
116 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
117 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
118 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
119 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
120 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
121 $upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
122 $installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
123 $installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
124 $confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
125 $abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
126 $abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
127 $confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
128 $abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
129 $abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
130 $confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
131 $installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
132 $installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
133 $installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
134 $confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
136 if (!empty($CFG->disableupdateautodeploy)) {
137 // Invalidate all requests to install plugins via the admin UI.
138 $newaddonreq = null;
139 $installdep = null;
140 $installdepx = false;
141 $abortupgrade = null;
142 $abortupgradex = null;
143 $installupdate = null;
144 $installupdateversion = null;
145 $installupdatex = false;
148 // Set up PAGE.
149 $url = new moodle_url('/admin/index.php');
150 $url->param('cache', $cache);
151 if (isset($upgradekeyhash)) {
152 $url->param('upgradekeyhash', $upgradekeyhash);
154 $PAGE->set_url($url);
155 unset($url);
157 // Are we returning from an add-on installation request at moodle.org/plugins?
158 if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
159 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
160 'installaddonrequest' => $newaddonreq,
161 'confirm' => 0));
162 if (!isloggedin() or isguestuser()) {
163 // Login and go the the add-on tool page.
164 $SESSION->wantsurl = $target->out();
165 redirect(get_login_url());
167 redirect($target);
170 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
172 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
174 // Check some PHP server settings
176 if (ini_get_bool('session.auto_start')) {
177 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
180 if (!ini_get_bool('file_uploads')) {
181 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
184 if (is_float_problem()) {
185 print_error('phpfloatproblem', 'admin', '', $documentationlink);
188 // Set some necessary variables during set-up to avoid PHP warnings later on this page
189 if (!isset($CFG->release)) {
190 $CFG->release = '';
192 if (!isset($CFG->version)) {
193 $CFG->version = '';
195 if (!isset($CFG->branch)) {
196 $CFG->branch = '';
199 $version = null;
200 $release = null;
201 $branch = null;
202 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
203 $CFG->target_release = $release; // used during installation and upgrades
205 if (!$version or !$release) {
206 print_error('withoutversion', 'debug'); // without version, stop
209 if (!core_tables_exist()) {
210 $PAGE->set_pagelayout('maintenance');
211 $PAGE->set_popup_notification_allowed(false);
213 // fake some settings
214 $CFG->docroot = 'http://docs.moodle.org';
216 $strinstallation = get_string('installation', 'install');
218 // remove current session content completely
219 \core\session\manager::terminate_current();
221 if (empty($agreelicense)) {
222 $strlicense = get_string('license');
224 $PAGE->navbar->add($strlicense);
225 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
226 $PAGE->set_heading($strinstallation);
227 $PAGE->set_cacheable(false);
229 $output = $PAGE->get_renderer('core', 'admin');
230 echo $output->install_licence_page();
231 die();
233 if (empty($confirmrelease)) {
234 require_once($CFG->libdir.'/environmentlib.php');
235 list($envstatus, $environmentresults) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
236 $strcurrentrelease = get_string('currentrelease');
238 $PAGE->navbar->add($strcurrentrelease);
239 $PAGE->set_title($strinstallation);
240 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
241 $PAGE->set_cacheable(false);
243 $output = $PAGE->get_renderer('core', 'admin');
244 echo $output->install_environment_page($maturity, $envstatus, $environmentresults, $release);
245 die();
248 // check plugin dependencies
249 $failed = array();
250 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
251 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
252 $PAGE->set_title($strinstallation);
253 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
255 $output = $PAGE->get_renderer('core', 'admin');
256 $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
257 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
258 die();
260 unset($failed);
262 //TODO: add a page with list of non-standard plugins here
264 $strdatabasesetup = get_string('databasesetup');
265 upgrade_init_javascript();
267 $PAGE->navbar->add($strdatabasesetup);
268 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
269 $PAGE->set_heading($strinstallation);
270 $PAGE->set_cacheable(false);
272 $output = $PAGE->get_renderer('core', 'admin');
273 echo $output->header();
275 if (!$DB->setup_is_unicodedb()) {
276 if (!$DB->change_db_encoding()) {
277 // If could not convert successfully, throw error, and prevent installation
278 print_error('unicoderequired', 'admin');
282 install_core($version, true);
286 // Check version of Moodle code on disk compared with database
287 // and upgrade if possible.
289 if (!$cache) {
290 // Do not try to do anything fancy in non-cached mode,
291 // this prevents themes from fetching data from non-existent tables.
292 $PAGE->set_pagelayout('maintenance');
293 $PAGE->set_popup_notification_allowed(false);
296 $stradministration = get_string('administration');
297 $PAGE->set_context(context_system::instance());
299 if (empty($CFG->version)) {
300 print_error('missingconfigversion', 'debug');
303 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
304 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
305 purge_all_caches();
306 redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
309 if (!$cache and $version > $CFG->version) { // upgrade
311 $PAGE->set_url(new moodle_url($PAGE->url, array(
312 'confirmupgrade' => $confirmupgrade,
313 'confirmrelease' => $confirmrelease,
314 'confirmplugincheck' => $confirmplugins,
315 )));
317 check_upgrade_key($upgradekeyhash);
319 // Warning about upgrading a test site.
320 $testsite = false;
321 if (defined('BEHAT_SITE_RUNNING')) {
322 $testsite = 'behat';
325 if (isset($CFG->themerev)) {
326 // Store the themerev to restore after purging caches.
327 $themerev = $CFG->themerev;
330 // We purge all of MUC's caches here.
331 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
332 // This ensures a real config object is loaded and the stores will be purged.
333 // This is the only way we can purge custom caches such as memcache or APC.
334 // Note: all other calls to caches will still used the disabled API.
335 cache_helper::purge_all(true);
336 // We then purge the regular caches.
337 purge_all_caches();
339 if (isset($themerev)) {
340 // Restore the themerev
341 set_config('themerev', $themerev);
344 $output = $PAGE->get_renderer('core', 'admin');
346 if (upgrade_stale_php_files_present()) {
347 $PAGE->set_title($stradministration);
348 $PAGE->set_cacheable(false);
350 echo $output->upgrade_stale_php_files_page();
351 die();
354 if (empty($confirmupgrade)) {
355 $a = new stdClass();
356 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
357 $a->newversion = "$release (".sprintf('%.2f', $version).")";
358 $strdatabasechecking = get_string('databasechecking', '', $a);
360 $PAGE->set_title($stradministration);
361 $PAGE->set_heading($strdatabasechecking);
362 $PAGE->set_cacheable(false);
364 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
365 die();
367 } else if (empty($confirmrelease)) {
368 require_once($CFG->libdir.'/environmentlib.php');
369 list($envstatus, $environmentresults) = check_moodle_environment($release, ENV_SELECT_RELEASE);
370 $strcurrentrelease = get_string('currentrelease');
372 $PAGE->navbar->add($strcurrentrelease);
373 $PAGE->set_title($strcurrentrelease);
374 $PAGE->set_heading($strcurrentrelease);
375 $PAGE->set_cacheable(false);
377 echo $output->upgrade_environment_page($release, $envstatus, $environmentresults);
378 die();
380 } else if (empty($confirmplugins)) {
381 $strplugincheck = get_string('plugincheck');
383 $PAGE->navbar->add($strplugincheck);
384 $PAGE->set_title($strplugincheck);
385 $PAGE->set_heading($strplugincheck);
386 $PAGE->set_cacheable(false);
388 $pluginman = core_plugin_manager::instance();
390 // Check for available updates.
391 if ($fetchupdates) {
392 // No sesskey support guaranteed here, because sessions might not work yet.
393 $updateschecker = \core\update\checker::instance();
394 if ($updateschecker->enabled()) {
395 $updateschecker->fetch();
397 redirect($PAGE->url);
400 // Cancel all plugin installations.
401 if ($abortinstallx) {
402 // No sesskey support guaranteed here, because sessions might not work yet.
403 $abortables = $pluginman->list_cancellable_installations();
404 if ($abortables) {
405 if ($confirmabortinstall) {
406 foreach ($abortables as $plugin) {
407 $pluginman->cancel_plugin_installation($plugin->component);
409 redirect($PAGE->url);
410 } else {
411 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
412 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
413 die();
416 redirect($PAGE->url);
419 // Cancel single plugin installation.
420 if ($abortinstall) {
421 // No sesskey support guaranteed here, because sessions might not work yet.
422 if ($confirmabortinstall) {
423 $pluginman->cancel_plugin_installation($abortinstall);
424 redirect($PAGE->url);
425 } else {
426 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
427 $abortable = $pluginman->get_plugin_info($abortinstall);
428 if ($pluginman->can_cancel_plugin_installation($abortable)) {
429 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
430 die();
432 redirect($PAGE->url);
436 // Cancel all plugins upgrades (that is, restore archived versions).
437 if ($abortupgradex) {
438 // No sesskey support guaranteed here, because sessions might not work yet.
439 $restorable = $pluginman->list_restorable_archives();
440 if ($restorable) {
441 upgrade_install_plugins($restorable, $confirmabortupgrade,
442 get_string('cancelupgradehead', 'core_plugin'),
443 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
446 redirect($PAGE->url);
449 // Cancel single plugin upgrade (that is, install the archived version).
450 if ($abortupgrade) {
451 // No sesskey support guaranteed here, because sessions might not work yet.
452 $restorable = $pluginman->list_restorable_archives();
453 if (isset($restorable[$abortupgrade])) {
454 $restorable = array($restorable[$abortupgrade]);
455 upgrade_install_plugins($restorable, $confirmabortupgrade,
456 get_string('cancelupgradehead', 'core_plugin'),
457 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
460 redirect($PAGE->url);
463 // Install all available missing dependencies.
464 if ($installdepx) {
465 // No sesskey support guaranteed here, because sessions might not work yet.
466 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
467 upgrade_install_plugins($installable, $confirminstalldep,
468 get_string('dependencyinstallhead', 'core_plugin'),
469 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
473 // Install single available missing dependency.
474 if ($installdep) {
475 // No sesskey support guaranteed here, because sessions might not work yet.
476 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
477 if (!empty($installable[$installdep])) {
478 $installable = array($installable[$installdep]);
479 upgrade_install_plugins($installable, $confirminstalldep,
480 get_string('dependencyinstallhead', 'core_plugin'),
481 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
486 // Install all available updates.
487 if ($installupdatex) {
488 // No sesskey support guaranteed here, because sessions might not work yet.
489 $installable = $pluginman->filter_installable($pluginman->available_updates());
490 upgrade_install_plugins($installable, $confirminstallupdate,
491 get_string('updateavailableinstallallhead', 'core_admin'),
492 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
496 // Install single available update.
497 if ($installupdate and $installupdateversion) {
498 // No sesskey support guaranteed here, because sessions might not work yet.
499 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
500 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
501 upgrade_install_plugins($installable, $confirminstallupdate,
502 get_string('updateavailableinstallallhead', 'core_admin'),
503 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
504 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
510 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
511 $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
512 die();
514 } else {
515 // Always verify plugin dependencies!
516 $failed = array();
517 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
518 echo $output->unsatisfied_dependencies_page($version, $failed, new moodle_url($PAGE->url,
519 array('confirmplugincheck' => 0)));
520 die();
522 unset($failed);
524 // Launch main upgrade.
525 upgrade_core($version, true);
527 } else if ($version < $CFG->version) {
528 // better stop here, we can not continue with plugin upgrades or anything else
529 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
532 // Updated human-readable release version if necessary
533 if (!$cache and $release <> $CFG->release) { // Update the release version
534 set_config('release', $release);
537 if (!$cache and $branch <> $CFG->branch) { // Update the branch
538 set_config('branch', $branch);
541 if (!$cache and moodle_needs_upgrading()) {
543 $PAGE->set_url(new moodle_url($PAGE->url, array(
544 'confirmrelease' => $confirmrelease,
545 'confirmplugincheck' => $confirmplugins,
546 )));
548 check_upgrade_key($upgradekeyhash);
550 if (!$PAGE->headerprinted) {
551 // means core upgrade or installation was not already done
553 $pluginman = core_plugin_manager::instance();
554 $output = $PAGE->get_renderer('core', 'admin');
556 if (empty($confirmrelease)) {
557 require_once($CFG->libdir . '/environmentlib.php');
559 list($envstatus, $environmentresults) = check_moodle_environment($release, ENV_SELECT_RELEASE);
560 $strcurrentrelease = get_string('currentrelease');
562 $PAGE->navbar->add($strcurrentrelease);
563 $PAGE->set_title($strcurrentrelease);
564 $PAGE->set_heading($strcurrentrelease);
565 $PAGE->set_cacheable(false);
567 echo $output->upgrade_environment_page($release, $envstatus, $environmentresults);
568 die();
570 } else if (!$confirmplugins) {
571 $strplugincheck = get_string('plugincheck');
573 $PAGE->navbar->add($strplugincheck);
574 $PAGE->set_title($strplugincheck);
575 $PAGE->set_heading($strplugincheck);
576 $PAGE->set_cacheable(false);
578 // Check for available updates.
579 if ($fetchupdates) {
580 require_sesskey();
581 $updateschecker = \core\update\checker::instance();
582 if ($updateschecker->enabled()) {
583 $updateschecker->fetch();
585 redirect($PAGE->url);
588 // Cancel all plugin installations.
589 if ($abortinstallx) {
590 require_sesskey();
591 $abortables = $pluginman->list_cancellable_installations();
592 if ($abortables) {
593 if ($confirmabortinstall) {
594 foreach ($abortables as $plugin) {
595 $pluginman->cancel_plugin_installation($plugin->component);
597 redirect($PAGE->url);
598 } else {
599 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
600 'confirmabortinstall' => 1));
601 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
602 die();
605 redirect($PAGE->url);
608 // Cancel single plugin installation.
609 if ($abortinstall) {
610 require_sesskey();
611 if ($confirmabortinstall) {
612 $pluginman->cancel_plugin_installation($abortinstall);
613 redirect($PAGE->url);
614 } else {
615 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
616 $abortable = $pluginman->get_plugin_info($abortinstall);
617 if ($pluginman->can_cancel_plugin_installation($abortable)) {
618 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
619 die();
621 redirect($PAGE->url);
625 // Cancel all plugins upgrades (that is, restore archived versions).
626 if ($abortupgradex) {
627 require_sesskey();
628 $restorable = $pluginman->list_restorable_archives();
629 if ($restorable) {
630 upgrade_install_plugins($restorable, $confirmabortupgrade,
631 get_string('cancelupgradehead', 'core_plugin'),
632 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
635 redirect($PAGE->url);
638 // Cancel single plugin upgrade (that is, install the archived version).
639 if ($abortupgrade) {
640 require_sesskey();
641 $restorable = $pluginman->list_restorable_archives();
642 if (isset($restorable[$abortupgrade])) {
643 $restorable = array($restorable[$abortupgrade]);
644 upgrade_install_plugins($restorable, $confirmabortupgrade,
645 get_string('cancelupgradehead', 'core_plugin'),
646 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
649 redirect($PAGE->url);
652 // Install all available missing dependencies.
653 if ($installdepx) {
654 require_sesskey();
655 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
656 upgrade_install_plugins($installable, $confirminstalldep,
657 get_string('dependencyinstallhead', 'core_plugin'),
658 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
662 // Install single available missing dependency.
663 if ($installdep) {
664 require_sesskey();
665 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
666 if (!empty($installable[$installdep])) {
667 $installable = array($installable[$installdep]);
668 upgrade_install_plugins($installable, $confirminstalldep,
669 get_string('dependencyinstallhead', 'core_plugin'),
670 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
675 // Install all available updates.
676 if ($installupdatex) {
677 require_sesskey();
678 $installable = $pluginman->filter_installable($pluginman->available_updates());
679 upgrade_install_plugins($installable, $confirminstallupdate,
680 get_string('updateavailableinstallallhead', 'core_admin'),
681 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
685 // Install single available update.
686 if ($installupdate and $installupdateversion) {
687 require_sesskey();
688 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
689 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
690 upgrade_install_plugins($installable, $confirminstallupdate,
691 get_string('updateavailableinstallallhead', 'core_admin'),
692 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
693 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
699 // Show plugins info.
700 echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
701 $version, $showallplugins,
702 new moodle_url($PAGE->url),
703 new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
704 die();
707 // Make sure plugin dependencies are always checked.
708 $failed = array();
709 if (!$pluginman->all_plugins_ok($version, $failed, $CFG->branch)) {
710 $output = $PAGE->get_renderer('core', 'admin');
711 echo $output->unsatisfied_dependencies_page($version, $failed, new moodle_url($PAGE->url,
712 array('confirmplugincheck' => 0)));
713 die();
715 unset($failed);
718 // install/upgrade all plugins and other parts
719 upgrade_noncore(true);
722 // If this is the first install, indicate that this site is fully configured
723 // except the admin password
724 if (during_initial_install()) {
725 set_config('rolesactive', 1); // after this, during_initial_install will return false.
726 set_config('adminsetuppending', 1);
727 set_config('registrationpending', 1); // Remind to register site after all other setup is finished.
729 // Apply default preset, if it is defined in $CFG and has a valid value.
730 if (!empty($CFG->setsitepresetduringinstall)) {
731 \core_adminpresets\helper::change_default_preset($CFG->setsitepresetduringinstall);
734 // we need this redirect to setup proper session
735 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
738 // make sure admin user is created - this is the last step because we need
739 // session to be working properly in order to edit admin account
740 if (!empty($CFG->adminsetuppending)) {
741 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
742 if (!$sessionstarted) {
743 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
744 } else {
745 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
746 if (!$sessionverify) {
747 $SESSION->sessionverify = 1;
748 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
749 } else {
750 if (empty($SESSION->sessionverify)) {
751 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
753 unset($SESSION->sessionverify);
757 // Cleanup SESSION to make sure other code does not complain in the future.
758 unset($SESSION->has_timed_out);
759 unset($SESSION->wantsurl);
761 // at this stage there can be only one admin unless more were added by install - users may change username, so do not rely on that
762 $adminids = explode(',', $CFG->siteadmins);
763 $adminuser = get_complete_user_data('id', reset($adminids));
765 if ($adminuser->password === 'adminsetuppending') {
766 // prevent installation hijacking
767 if ($adminuser->lastip !== getremoteaddr()) {
768 print_error('installhijacked', 'admin');
770 // login user and let him set password and admin details
771 $adminuser->newadminuser = 1;
772 complete_user_login($adminuser);
773 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
775 } else {
776 unset_config('adminsetuppending');
779 } else {
780 // just make sure upgrade logging is properly terminated
781 upgrade_finished('upgradesettings.php');
784 if (has_capability('moodle/site:config', context_system::instance())) {
785 if ($fetchupdates) {
786 require_sesskey();
787 $updateschecker = \core\update\checker::instance();
788 if ($updateschecker->enabled()) {
789 $updateschecker->fetch();
791 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
795 // Now we can be sure everything was upgraded and caches work fine,
796 // redirect if necessary to make sure caching is enabled.
797 if (!$cache) {
798 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
801 // Check for valid admin user - no guest autologin
802 require_login(0, false);
803 if (isguestuser()) {
804 // Login as real user!
805 $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
806 redirect(get_login_url());
808 $context = context_system::instance();
810 if (!has_capability('moodle/site:config', $context)) {
811 // Do not throw exception display an empty page with administration menu if visible for current user.
812 $PAGE->set_title($SITE->fullname);
813 $PAGE->set_heading($SITE->fullname);
814 echo $OUTPUT->header();
815 echo $OUTPUT->footer();
816 exit;
819 // check that site is properly customized
820 $site = get_site();
821 if (empty($site->shortname)) {
822 // probably new installation - lets return to frontpage after this step
823 // remove settings that we want uninitialised
824 unset_config('registerauth');
825 unset_config('timezone'); // Force admin to select timezone!
826 redirect('upgradesettings.php?return=site');
829 // setup critical warnings before printing admin tree block
830 $insecuredataroot = is_dataroot_insecure(true);
831 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
833 $adminroot = admin_get_root();
834 $PAGE->set_primary_active_tab('siteadminnode');
836 // Check if there are any new admin settings which have still yet to be set
837 if (any_new_admin_settings($adminroot)) {
838 redirect('upgradesettings.php');
841 // Return to original page that started the plugin uninstallation if necessary.
842 if (isset($SESSION->pluginuninstallreturn)) {
843 $return = $SESSION->pluginuninstallreturn;
844 unset($SESSION->pluginuninstallreturn);
845 if ($return) {
846 redirect($return);
850 // If site registration needs updating, redirect.
851 \core\hub\registration::registration_reminder('/admin/index.php');
853 // Everything should now be set up, and the user is an admin
855 // Print default admin page with notifications.
856 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
858 $lastcron = get_config('tool_task', 'lastcronstart');
859 $cronoverdue = ($lastcron < time() - 3600 * 24);
860 $lastcroninterval = get_config('tool_task', 'lastcroninterval');
862 $expectedfrequency = $CFG->expectedcronfrequency ?? MINSECS;
863 $croninfrequent = !$cronoverdue && ($lastcroninterval > ($expectedfrequency + MINSECS) || $lastcron < time() - $expectedfrequency);
864 $dbproblems = $DB->diagnose();
865 $maintenancemode = !empty($CFG->maintenance_enabled);
867 // Available updates for Moodle core.
868 $updateschecker = \core\update\checker::instance();
869 $availableupdates = array();
870 $availableupdatesfetch = null;
872 if ($updateschecker->enabled()) {
873 // Only compute the update information when it is going to be displayed to the user.
874 $availableupdates['core'] = $updateschecker->get_update_info('core',
875 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
877 // Available updates for contributed plugins
878 $pluginman = core_plugin_manager::instance();
879 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
880 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
881 $pluginavailableupdates = $plugininfo->available_updates();
882 if (!empty($pluginavailableupdates)) {
883 foreach ($pluginavailableupdates as $pluginavailableupdate) {
884 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
885 $availableupdates[$plugintype.'_'.$pluginname] = array();
887 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
893 // The timestamp of the most recent check for available updates
894 $availableupdatesfetch = $updateschecker->get_last_timefetched();
897 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
898 //check if the site is registered on Moodle.org
899 $registered = \core\hub\registration::is_registered();
900 // Check if there are any cache warnings.
901 $cachewarnings = cache_helper::warnings();
902 // Check if there are events 1 API handlers.
903 $eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
904 $themedesignermode = !empty($CFG->themedesignermode);
905 $mobileconfigured = !empty($CFG->enablemobilewebservice);
906 $invalidforgottenpasswordurl = !empty($CFG->forgottenpasswordurl) && empty(clean_param($CFG->forgottenpasswordurl, PARAM_URL));
908 // Check if a directory with development libraries exists.
909 if (empty($CFG->disabledevlibdirscheck) && (is_dir($CFG->dirroot.'/vendor') || is_dir($CFG->dirroot.'/node_modules'))) {
910 $devlibdir = true;
911 } else {
912 $devlibdir = false;
914 // Check if the site is being foced onto ssl.
915 $overridetossl = !empty($CFG->overridetossl);
917 // Check if moodle campaign content setting is enabled or not.
918 $showcampaigncontent = !isset($CFG->showcampaigncontent) || $CFG->showcampaigncontent;
920 // Encourage admins to enable the user feedback feature if it is not enabled already.
921 $showfeedbackencouragement = empty($CFG->enableuserfeedback);
923 // Check if the service and support content setting is enabled or not.
924 $servicesandsupportcontent = !isset($CFG->showservicesandsupportcontent) || $CFG->showservicesandsupportcontent;
926 // Check whether the XML-RPC protocol is enabled or not.
927 require_once($CFG->libdir . '/environmentlib.php');
928 $result = new environment_results('custom_checks');
929 $result = check_xmlrpc_usage($result);
930 $xmlrpcwarning = !is_null($result) ? get_string($result->getFeedbackStr(), 'admin') : '';
932 admin_externalpage_setup('adminnotifications');
934 $output = $PAGE->get_renderer('core', 'admin');
936 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
937 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
938 $registered, $cachewarnings, $eventshandlers, $themedesignermode, $devlibdir,
939 $mobileconfigured, $overridetossl, $invalidforgottenpasswordurl, $croninfrequent,
940 $showcampaigncontent, $showfeedbackencouragement, $servicesandsupportcontent,
941 $xmlrpcwarning);