Merge branch 'MDL-63012-master' of https://github.com/jobyh/moodle
[moodle.git] / admin / index.php
blobe04c3197efbc6058450549e3b2e40f5f745f4b89
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 define('NO_OUTPUT_BUFFERING', true);
58 if (isset($_POST['upgradekey'])) {
59 // Before you start reporting issues about the collision attacks against
60 // SHA-1, you should understand that we are not actually attempting to do
61 // any cryptography here. This is hashed purely so that the key is not
62 // that apparent in the address bar itself. Anyone who catches the HTTP
63 // traffic can immediately use it as a valid admin key.
64 header('Location: index.php?cache=0&upgradekeyhash='.sha1($_POST['upgradekey']));
65 die();
68 if ((isset($_GET['cache']) and $_GET['cache'] === '0')
69 or (isset($_POST['cache']) and $_POST['cache'] === '0')
70 or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
71 // Prevent caching at all cost when visiting this page directly,
72 // we redirect to self once we known no upgrades are necessary.
73 // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
74 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
75 define('CACHE_DISABLE_ALL', true);
77 // Force OPcache reset if used, we do not want any stale caches
78 // when detecting if upgrade necessary or when running upgrade.
79 if (function_exists('opcache_reset')) {
80 opcache_reset();
82 $cache = 0;
84 } else {
85 $cache = 1;
88 require('../config.php');
90 // Invalidate the cache of version.php in any circumstances to help core_component
91 // detecting if the version has changed and component cache should be reset.
92 if (function_exists('opcache_invalidate')) {
93 opcache_invalidate($CFG->dirroot . '/version.php', true);
95 // Make sure the component cache gets rebuilt if necessary, any method that
96 // indirectly calls the protected init() method is good here.
97 core_component::get_core_subsystems();
99 if (is_major_upgrade_required() && isloggedin()) {
100 // A major upgrade is required.
101 // Terminate the session and redirect back here before anything DB-related happens.
102 redirect_if_major_upgrade_required();
105 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
106 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
108 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
109 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
110 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
111 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
112 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
113 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
114 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
115 $upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
116 $installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
117 $installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
118 $confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
119 $abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
120 $abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
121 $confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
122 $abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
123 $abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
124 $confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
125 $installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
126 $installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
127 $installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
128 $confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
130 if (!empty($CFG->disableupdateautodeploy)) {
131 // Invalidate all requests to install plugins via the admin UI.
132 $newaddonreq = null;
133 $installdep = null;
134 $installdepx = false;
135 $abortupgrade = null;
136 $abortupgradex = null;
137 $installupdate = null;
138 $installupdateversion = null;
139 $installupdatex = false;
142 // Set up PAGE.
143 $url = new moodle_url('/admin/index.php');
144 $url->param('cache', $cache);
145 if (isset($upgradekeyhash)) {
146 $url->param('upgradekeyhash', $upgradekeyhash);
148 $PAGE->set_url($url);
149 unset($url);
151 // Are we returning from an add-on installation request at moodle.org/plugins?
152 if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
153 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
154 'installaddonrequest' => $newaddonreq,
155 'confirm' => 0));
156 if (!isloggedin() or isguestuser()) {
157 // Login and go the the add-on tool page.
158 $SESSION->wantsurl = $target->out();
159 redirect(get_login_url());
161 redirect($target);
164 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
166 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
168 // Check some PHP server settings
170 if (ini_get_bool('session.auto_start')) {
171 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
174 if (!ini_get_bool('file_uploads')) {
175 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
178 if (is_float_problem()) {
179 print_error('phpfloatproblem', 'admin', '', $documentationlink);
182 // Set some necessary variables during set-up to avoid PHP warnings later on this page
183 if (!isset($CFG->release)) {
184 $CFG->release = '';
186 if (!isset($CFG->version)) {
187 $CFG->version = '';
189 if (!isset($CFG->branch)) {
190 $CFG->branch = '';
193 $version = null;
194 $release = null;
195 $branch = null;
196 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
197 $CFG->target_release = $release; // used during installation and upgrades
199 if (!$version or !$release) {
200 print_error('withoutversion', 'debug'); // without version, stop
203 if (!core_tables_exist()) {
204 $PAGE->set_pagelayout('maintenance');
205 $PAGE->set_popup_notification_allowed(false);
207 // fake some settings
208 $CFG->docroot = 'http://docs.moodle.org';
210 $strinstallation = get_string('installation', 'install');
212 // remove current session content completely
213 \core\session\manager::terminate_current();
215 if (empty($agreelicense)) {
216 $strlicense = get_string('license');
218 $PAGE->navbar->add($strlicense);
219 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
220 $PAGE->set_heading($strinstallation);
221 $PAGE->set_cacheable(false);
223 $output = $PAGE->get_renderer('core', 'admin');
224 echo $output->install_licence_page();
225 die();
227 if (empty($confirmrelease)) {
228 require_once($CFG->libdir.'/environmentlib.php');
229 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
230 $strcurrentrelease = get_string('currentrelease');
232 $PAGE->navbar->add($strcurrentrelease);
233 $PAGE->set_title($strinstallation);
234 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
235 $PAGE->set_cacheable(false);
237 $output = $PAGE->get_renderer('core', 'admin');
238 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
239 die();
242 // check plugin dependencies
243 $failed = array();
244 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
245 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
246 $PAGE->set_title($strinstallation);
247 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
249 $output = $PAGE->get_renderer('core', 'admin');
250 $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
251 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
252 die();
254 unset($failed);
256 //TODO: add a page with list of non-standard plugins here
258 $strdatabasesetup = get_string('databasesetup');
259 upgrade_init_javascript();
261 $PAGE->navbar->add($strdatabasesetup);
262 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
263 $PAGE->set_heading($strinstallation);
264 $PAGE->set_cacheable(false);
266 $output = $PAGE->get_renderer('core', 'admin');
267 echo $output->header();
269 if (!$DB->setup_is_unicodedb()) {
270 if (!$DB->change_db_encoding()) {
271 // If could not convert successfully, throw error, and prevent installation
272 print_error('unicoderequired', 'admin');
276 install_core($version, true);
280 // Check version of Moodle code on disk compared with database
281 // and upgrade if possible.
283 if (!$cache) {
284 // Do not try to do anything fancy in non-cached mode,
285 // this prevents themes from fetching data from non-existent tables.
286 $PAGE->set_pagelayout('maintenance');
287 $PAGE->set_popup_notification_allowed(false);
290 $stradministration = get_string('administration');
291 $PAGE->set_context(context_system::instance());
293 if (empty($CFG->version)) {
294 print_error('missingconfigversion', 'debug');
297 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
298 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
299 purge_all_caches();
300 redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
303 if (!$cache and $version > $CFG->version) { // upgrade
305 $PAGE->set_url(new moodle_url($PAGE->url, array(
306 'confirmupgrade' => $confirmupgrade,
307 'confirmrelease' => $confirmrelease,
308 'confirmplugincheck' => $confirmplugins,
309 )));
311 check_upgrade_key($upgradekeyhash);
313 // Warning about upgrading a test site.
314 $testsite = false;
315 if (defined('BEHAT_SITE_RUNNING')) {
316 $testsite = 'behat';
319 if (isset($CFG->themerev)) {
320 // Store the themerev to restore after purging caches.
321 $themerev = $CFG->themerev;
324 // We purge all of MUC's caches here.
325 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
326 // This ensures a real config object is loaded and the stores will be purged.
327 // This is the only way we can purge custom caches such as memcache or APC.
328 // Note: all other calls to caches will still used the disabled API.
329 cache_helper::purge_all(true);
330 // We then purge the regular caches.
331 purge_all_caches();
333 if (isset($themerev)) {
334 // Restore the themerev
335 set_config('themerev', $themerev);
338 $output = $PAGE->get_renderer('core', 'admin');
340 if (upgrade_stale_php_files_present()) {
341 $PAGE->set_title($stradministration);
342 $PAGE->set_cacheable(false);
344 echo $output->upgrade_stale_php_files_page();
345 die();
348 if (empty($confirmupgrade)) {
349 $a = new stdClass();
350 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
351 $a->newversion = "$release (".sprintf('%.2f', $version).")";
352 $strdatabasechecking = get_string('databasechecking', '', $a);
354 $PAGE->set_title($stradministration);
355 $PAGE->set_heading($strdatabasechecking);
356 $PAGE->set_cacheable(false);
358 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
359 die();
361 } else if (empty($confirmrelease)){
362 require_once($CFG->libdir.'/environmentlib.php');
363 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
364 $strcurrentrelease = get_string('currentrelease');
366 $PAGE->navbar->add($strcurrentrelease);
367 $PAGE->set_title($strcurrentrelease);
368 $PAGE->set_heading($strcurrentrelease);
369 $PAGE->set_cacheable(false);
371 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
372 die();
374 } else if (empty($confirmplugins)) {
375 $strplugincheck = get_string('plugincheck');
377 $PAGE->navbar->add($strplugincheck);
378 $PAGE->set_title($strplugincheck);
379 $PAGE->set_heading($strplugincheck);
380 $PAGE->set_cacheable(false);
382 $pluginman = core_plugin_manager::instance();
384 // Check for available updates.
385 if ($fetchupdates) {
386 // No sesskey support guaranteed here, because sessions might not work yet.
387 $updateschecker = \core\update\checker::instance();
388 if ($updateschecker->enabled()) {
389 $updateschecker->fetch();
391 redirect($PAGE->url);
394 // Cancel all plugin installations.
395 if ($abortinstallx) {
396 // No sesskey support guaranteed here, because sessions might not work yet.
397 $abortables = $pluginman->list_cancellable_installations();
398 if ($abortables) {
399 if ($confirmabortinstall) {
400 foreach ($abortables as $plugin) {
401 $pluginman->cancel_plugin_installation($plugin->component);
403 redirect($PAGE->url);
404 } else {
405 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
406 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
407 die();
410 redirect($PAGE->url);
413 // Cancel single plugin installation.
414 if ($abortinstall) {
415 // No sesskey support guaranteed here, because sessions might not work yet.
416 if ($confirmabortinstall) {
417 $pluginman->cancel_plugin_installation($abortinstall);
418 redirect($PAGE->url);
419 } else {
420 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
421 $abortable = $pluginman->get_plugin_info($abortinstall);
422 if ($pluginman->can_cancel_plugin_installation($abortable)) {
423 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
424 die();
426 redirect($PAGE->url);
430 // Cancel all plugins upgrades (that is, restore archived versions).
431 if ($abortupgradex) {
432 // No sesskey support guaranteed here, because sessions might not work yet.
433 $restorable = $pluginman->list_restorable_archives();
434 if ($restorable) {
435 upgrade_install_plugins($restorable, $confirmabortupgrade,
436 get_string('cancelupgradehead', 'core_plugin'),
437 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
440 redirect($PAGE->url);
443 // Cancel single plugin upgrade (that is, install the archived version).
444 if ($abortupgrade) {
445 // No sesskey support guaranteed here, because sessions might not work yet.
446 $restorable = $pluginman->list_restorable_archives();
447 if (isset($restorable[$abortupgrade])) {
448 $restorable = array($restorable[$abortupgrade]);
449 upgrade_install_plugins($restorable, $confirmabortupgrade,
450 get_string('cancelupgradehead', 'core_plugin'),
451 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
454 redirect($PAGE->url);
457 // Install all available missing dependencies.
458 if ($installdepx) {
459 // No sesskey support guaranteed here, because sessions might not work yet.
460 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
461 upgrade_install_plugins($installable, $confirminstalldep,
462 get_string('dependencyinstallhead', 'core_plugin'),
463 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
467 // Install single available missing dependency.
468 if ($installdep) {
469 // No sesskey support guaranteed here, because sessions might not work yet.
470 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
471 if (!empty($installable[$installdep])) {
472 $installable = array($installable[$installdep]);
473 upgrade_install_plugins($installable, $confirminstalldep,
474 get_string('dependencyinstallhead', 'core_plugin'),
475 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
480 // Install all available updates.
481 if ($installupdatex) {
482 // No sesskey support guaranteed here, because sessions might not work yet.
483 $installable = $pluginman->filter_installable($pluginman->available_updates());
484 upgrade_install_plugins($installable, $confirminstallupdate,
485 get_string('updateavailableinstallallhead', 'core_admin'),
486 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
490 // Install single available update.
491 if ($installupdate and $installupdateversion) {
492 // No sesskey support guaranteed here, because sessions might not work yet.
493 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
494 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
495 upgrade_install_plugins($installable, $confirminstallupdate,
496 get_string('updateavailableinstallallhead', 'core_admin'),
497 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
498 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
504 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
505 $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
506 die();
508 } else {
509 // Always verify plugin dependencies!
510 $failed = array();
511 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
512 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
513 die();
515 unset($failed);
517 // Launch main upgrade.
518 upgrade_core($version, true);
520 } else if ($version < $CFG->version) {
521 // better stop here, we can not continue with plugin upgrades or anything else
522 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
525 // Updated human-readable release version if necessary
526 if (!$cache and $release <> $CFG->release) { // Update the release version
527 set_config('release', $release);
530 if (!$cache and $branch <> $CFG->branch) { // Update the branch
531 set_config('branch', $branch);
534 if (!$cache and moodle_needs_upgrading()) {
536 $PAGE->set_url(new moodle_url($PAGE->url, array('confirmplugincheck' => $confirmplugins)));
538 check_upgrade_key($upgradekeyhash);
540 if (!$PAGE->headerprinted) {
541 // means core upgrade or installation was not already done
543 $pluginman = core_plugin_manager::instance();
544 $output = $PAGE->get_renderer('core', 'admin');
546 if (!$confirmplugins) {
547 $strplugincheck = get_string('plugincheck');
549 $PAGE->navbar->add($strplugincheck);
550 $PAGE->set_title($strplugincheck);
551 $PAGE->set_heading($strplugincheck);
552 $PAGE->set_cacheable(false);
554 // Check for available updates.
555 if ($fetchupdates) {
556 require_sesskey();
557 $updateschecker = \core\update\checker::instance();
558 if ($updateschecker->enabled()) {
559 $updateschecker->fetch();
561 redirect($PAGE->url);
564 // Cancel all plugin installations.
565 if ($abortinstallx) {
566 require_sesskey();
567 $abortables = $pluginman->list_cancellable_installations();
568 if ($abortables) {
569 if ($confirmabortinstall) {
570 foreach ($abortables as $plugin) {
571 $pluginman->cancel_plugin_installation($plugin->component);
573 redirect($PAGE->url);
574 } else {
575 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
576 'confirmabortinstall' => 1));
577 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
578 die();
581 redirect($PAGE->url);
584 // Cancel single plugin installation.
585 if ($abortinstall) {
586 require_sesskey();
587 if ($confirmabortinstall) {
588 $pluginman->cancel_plugin_installation($abortinstall);
589 redirect($PAGE->url);
590 } else {
591 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
592 $abortable = $pluginman->get_plugin_info($abortinstall);
593 if ($pluginman->can_cancel_plugin_installation($abortable)) {
594 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
595 die();
597 redirect($PAGE->url);
601 // Cancel all plugins upgrades (that is, restore archived versions).
602 if ($abortupgradex) {
603 require_sesskey();
604 $restorable = $pluginman->list_restorable_archives();
605 if ($restorable) {
606 upgrade_install_plugins($restorable, $confirmabortupgrade,
607 get_string('cancelupgradehead', 'core_plugin'),
608 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
611 redirect($PAGE->url);
614 // Cancel single plugin upgrade (that is, install the archived version).
615 if ($abortupgrade) {
616 require_sesskey();
617 $restorable = $pluginman->list_restorable_archives();
618 if (isset($restorable[$abortupgrade])) {
619 $restorable = array($restorable[$abortupgrade]);
620 upgrade_install_plugins($restorable, $confirmabortupgrade,
621 get_string('cancelupgradehead', 'core_plugin'),
622 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
625 redirect($PAGE->url);
628 // Install all available missing dependencies.
629 if ($installdepx) {
630 require_sesskey();
631 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
632 upgrade_install_plugins($installable, $confirminstalldep,
633 get_string('dependencyinstallhead', 'core_plugin'),
634 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
638 // Install single available missing dependency.
639 if ($installdep) {
640 require_sesskey();
641 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
642 if (!empty($installable[$installdep])) {
643 $installable = array($installable[$installdep]);
644 upgrade_install_plugins($installable, $confirminstalldep,
645 get_string('dependencyinstallhead', 'core_plugin'),
646 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
651 // Install all available updates.
652 if ($installupdatex) {
653 require_sesskey();
654 $installable = $pluginman->filter_installable($pluginman->available_updates());
655 upgrade_install_plugins($installable, $confirminstallupdate,
656 get_string('updateavailableinstallallhead', 'core_admin'),
657 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
661 // Install single available update.
662 if ($installupdate and $installupdateversion) {
663 require_sesskey();
664 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
665 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
666 upgrade_install_plugins($installable, $confirminstallupdate,
667 get_string('updateavailableinstallallhead', 'core_admin'),
668 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
669 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
675 // Show plugins info.
676 echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
677 $version, $showallplugins,
678 new moodle_url($PAGE->url),
679 new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
680 die();
683 // Make sure plugin dependencies are always checked.
684 $failed = array();
685 if (!$pluginman->all_plugins_ok($version, $failed)) {
686 $output = $PAGE->get_renderer('core', 'admin');
687 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
688 die();
690 unset($failed);
693 // install/upgrade all plugins and other parts
694 upgrade_noncore(true);
697 // If this is the first install, indicate that this site is fully configured
698 // except the admin password
699 if (during_initial_install()) {
700 set_config('rolesactive', 1); // after this, during_initial_install will return false.
701 set_config('adminsetuppending', 1);
702 set_config('registrationpending', 1); // Remind to register site after all other setup is finished.
703 // we need this redirect to setup proper session
704 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
707 // make sure admin user is created - this is the last step because we need
708 // session to be working properly in order to edit admin account
709 if (!empty($CFG->adminsetuppending)) {
710 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
711 if (!$sessionstarted) {
712 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
713 } else {
714 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
715 if (!$sessionverify) {
716 $SESSION->sessionverify = 1;
717 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
718 } else {
719 if (empty($SESSION->sessionverify)) {
720 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
722 unset($SESSION->sessionverify);
726 // Cleanup SESSION to make sure other code does not complain in the future.
727 unset($SESSION->has_timed_out);
728 unset($SESSION->wantsurl);
730 // 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
731 $adminids = explode(',', $CFG->siteadmins);
732 $adminuser = get_complete_user_data('id', reset($adminids));
734 if ($adminuser->password === 'adminsetuppending') {
735 // prevent installation hijacking
736 if ($adminuser->lastip !== getremoteaddr()) {
737 print_error('installhijacked', 'admin');
739 // login user and let him set password and admin details
740 $adminuser->newadminuser = 1;
741 complete_user_login($adminuser);
742 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
744 } else {
745 unset_config('adminsetuppending');
748 } else {
749 // just make sure upgrade logging is properly terminated
750 upgrade_finished('upgradesettings.php');
753 if (has_capability('moodle/site:config', context_system::instance())) {
754 if ($fetchupdates) {
755 require_sesskey();
756 $updateschecker = \core\update\checker::instance();
757 if ($updateschecker->enabled()) {
758 $updateschecker->fetch();
760 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
764 // Now we can be sure everything was upgraded and caches work fine,
765 // redirect if necessary to make sure caching is enabled.
766 if (!$cache) {
767 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
770 // Check for valid admin user - no guest autologin
771 require_login(0, false);
772 if (isguestuser()) {
773 // Login as real user!
774 $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
775 redirect(get_login_url());
777 $context = context_system::instance();
779 if (!has_capability('moodle/site:config', $context)) {
780 // Do not throw exception display an empty page with administration menu if visible for current user.
781 $PAGE->set_title($SITE->fullname);
782 $PAGE->set_heading($SITE->fullname);
783 echo $OUTPUT->header();
784 echo $OUTPUT->footer();
785 exit;
788 // check that site is properly customized
789 $site = get_site();
790 if (empty($site->shortname)) {
791 // probably new installation - lets return to frontpage after this step
792 // remove settings that we want uninitialised
793 unset_config('registerauth');
794 unset_config('timezone'); // Force admin to select timezone!
795 redirect('upgradesettings.php?return=site');
798 // setup critical warnings before printing admin tree block
799 $insecuredataroot = is_dataroot_insecure(true);
800 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
802 $adminroot = admin_get_root();
804 // Check if there are any new admin settings which have still yet to be set
805 if (any_new_admin_settings($adminroot)){
806 redirect('upgradesettings.php');
809 // Return to original page that started the plugin uninstallation if necessary.
810 if (isset($SESSION->pluginuninstallreturn)) {
811 $return = $SESSION->pluginuninstallreturn;
812 unset($SESSION->pluginuninstallreturn);
813 if ($return) {
814 redirect($return);
818 // If site registration needs updating, redirect.
819 \core\hub\registration::registration_reminder('/admin/index.php');
821 // Everything should now be set up, and the user is an admin
823 // Print default admin page with notifications.
824 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
826 // We make the assumption that at least one schedule task should run once per day.
827 $lastcron = $DB->get_field_sql('SELECT MAX(lastruntime) FROM {task_scheduled}');
828 $cronoverdue = ($lastcron < time() - 3600 * 24);
829 $dbproblems = $DB->diagnose();
830 $maintenancemode = !empty($CFG->maintenance_enabled);
832 // Available updates for Moodle core.
833 $updateschecker = \core\update\checker::instance();
834 $availableupdates = array();
835 $availableupdatesfetch = null;
837 if ($updateschecker->enabled()) {
838 // Only compute the update information when it is going to be displayed to the user.
839 $availableupdates['core'] = $updateschecker->get_update_info('core',
840 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
842 // Available updates for contributed plugins
843 $pluginman = core_plugin_manager::instance();
844 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
845 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
846 $pluginavailableupdates = $plugininfo->available_updates();
847 if (!empty($pluginavailableupdates)) {
848 foreach ($pluginavailableupdates as $pluginavailableupdate) {
849 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
850 $availableupdates[$plugintype.'_'.$pluginname] = array();
852 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
858 // The timestamp of the most recent check for available updates
859 $availableupdatesfetch = $updateschecker->get_last_timefetched();
862 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
863 //check if the site is registered on Moodle.org
864 $registered = \core\hub\registration::is_registered();
865 // Check if there are any cache warnings.
866 $cachewarnings = cache_helper::warnings();
867 // Check if there are events 1 API handlers.
868 $eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
869 $themedesignermode = !empty($CFG->themedesignermode);
870 $mobileconfigured = !empty($CFG->enablemobilewebservice);
871 $invalidforgottenpasswordurl = !empty($CFG->forgottenpasswordurl) && empty(clean_param($CFG->forgottenpasswordurl, PARAM_URL));
873 // Check if a directory with development libraries exists.
874 if (empty($CFG->disabledevlibdirscheck) && (is_dir($CFG->dirroot.'/vendor') || is_dir($CFG->dirroot.'/node_modules'))) {
875 $devlibdir = true;
876 } else {
877 $devlibdir = false;
879 // Check if the site is being foced onto ssl.
880 $overridetossl = !empty($CFG->overridetossl);
882 admin_externalpage_setup('adminnotifications');
884 $output = $PAGE->get_renderer('core', 'admin');
886 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
887 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
888 $registered, $cachewarnings, $eventshandlers, $themedesignermode, $devlibdir,
889 $mobileconfigured, $overridetossl, $invalidforgottenpasswordurl);