Merge branch 'MDL-61801-33' of git://github.com/andrewnicols/moodle into MOODLE_33_STABLE
[moodle.git] / admin / index.php
blob62da3cd8d44cdfc97d329a6ccf62573ec97340cd
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 if (version_compare(phpversion(), '5.6.5') < 0) {
34 $phpversion = phpversion();
35 // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
36 echo "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).<br />";
37 echo "Please upgrade your server software or install older Moodle version.";
38 die();
41 // make sure iconv is available and actually works
42 if (!function_exists('iconv')) {
43 // this should not happen, this must be very borked install
44 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
45 die();
48 // Make sure php5-json is available.
49 if (!function_exists('json_encode') || !function_exists('json_decode')) {
50 // This also shouldn't happen.
51 echo 'Moodle requires the json PHP extension. Please install or enable the json extension.';
52 die();
55 // Make sure xml extension is available.
56 if (!extension_loaded('xml')) {
57 echo 'Moodle requires the xml PHP extension. Please install or enable the xml extension.';
58 die();
61 define('NO_OUTPUT_BUFFERING', true);
63 if (isset($_POST['upgradekey'])) {
64 // Before you start reporting issues about the collision attacks against
65 // SHA-1, you should understand that we are not actually attempting to do
66 // any cryptography here. This is hashed purely so that the key is not
67 // that apparent in the address bar itself. Anyone who catches the HTTP
68 // traffic can immediately use it as a valid admin key.
69 header('Location: index.php?cache=0&upgradekeyhash='.sha1($_POST['upgradekey']));
70 die();
73 if ((isset($_GET['cache']) and $_GET['cache'] === '0')
74 or (isset($_POST['cache']) and $_POST['cache'] === '0')
75 or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
76 // Prevent caching at all cost when visiting this page directly,
77 // we redirect to self once we known no upgrades are necessary.
78 // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
79 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
80 define('CACHE_DISABLE_ALL', true);
82 // Force OPcache reset if used, we do not want any stale caches
83 // when detecting if upgrade necessary or when running upgrade.
84 if (function_exists('opcache_reset')) {
85 opcache_reset();
87 $cache = 0;
89 } else {
90 $cache = 1;
93 require('../config.php');
95 // Invalidate the cache of version.php in any circumstances to help core_component
96 // detecting if the version has changed and component cache should be reset.
97 if (function_exists('opcache_invalidate')) {
98 opcache_invalidate($CFG->dirroot . '/version.php', true);
100 // Make sure the component cache gets rebuilt if necessary, any method that
101 // indirectly calls the protected init() method is good here.
102 core_component::get_core_subsystems();
104 if (is_major_upgrade_required() && isloggedin()) {
105 // A major upgrade is required.
106 // Terminate the session and redirect back here before anything DB-related happens.
107 redirect_if_major_upgrade_required();
110 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
111 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
113 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
114 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
115 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
116 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
117 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
118 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
119 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
120 $upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
121 $installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
122 $installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
123 $confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
124 $abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
125 $abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
126 $confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
127 $abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
128 $abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
129 $confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
130 $installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
131 $installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
132 $installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
133 $confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
135 if (!empty($CFG->disableupdateautodeploy)) {
136 // Invalidate all requests to install plugins via the admin UI.
137 $newaddonreq = null;
138 $installdep = null;
139 $installdepx = false;
140 $abortupgrade = null;
141 $abortupgradex = null;
142 $installupdate = null;
143 $installupdateversion = null;
144 $installupdatex = false;
147 // Set up PAGE.
148 $url = new moodle_url('/admin/index.php');
149 $url->param('cache', $cache);
150 if (isset($upgradekeyhash)) {
151 $url->param('upgradekeyhash', $upgradekeyhash);
153 $PAGE->set_url($url);
154 unset($url);
156 // Are we returning from an add-on installation request at moodle.org/plugins?
157 if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
158 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
159 'installaddonrequest' => $newaddonreq,
160 'confirm' => 0));
161 if (!isloggedin() or isguestuser()) {
162 // Login and go the the add-on tool page.
163 $SESSION->wantsurl = $target->out();
164 redirect(get_login_url());
166 redirect($target);
169 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
171 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
173 // Check some PHP server settings
175 if (ini_get_bool('session.auto_start')) {
176 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
179 if (!ini_get_bool('file_uploads')) {
180 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
183 if (is_float_problem()) {
184 print_error('phpfloatproblem', 'admin', '', $documentationlink);
187 // Set some necessary variables during set-up to avoid PHP warnings later on this page
188 if (!isset($CFG->release)) {
189 $CFG->release = '';
191 if (!isset($CFG->version)) {
192 $CFG->version = '';
194 if (!isset($CFG->branch)) {
195 $CFG->branch = '';
198 $version = null;
199 $release = null;
200 $branch = null;
201 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
202 $CFG->target_release = $release; // used during installation and upgrades
204 if (!$version or !$release) {
205 print_error('withoutversion', 'debug'); // without version, stop
208 if (!core_tables_exist()) {
209 $PAGE->set_pagelayout('maintenance');
210 $PAGE->set_popup_notification_allowed(false);
212 // fake some settings
213 $CFG->docroot = 'http://docs.moodle.org';
215 $strinstallation = get_string('installation', 'install');
217 // remove current session content completely
218 \core\session\manager::terminate_current();
220 if (empty($agreelicense)) {
221 $strlicense = get_string('license');
223 $PAGE->navbar->add($strlicense);
224 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
225 $PAGE->set_heading($strinstallation);
226 $PAGE->set_cacheable(false);
228 $output = $PAGE->get_renderer('core', 'admin');
229 echo $output->install_licence_page();
230 die();
232 if (empty($confirmrelease)) {
233 require_once($CFG->libdir.'/environmentlib.php');
234 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
235 $strcurrentrelease = get_string('currentrelease');
237 $PAGE->navbar->add($strcurrentrelease);
238 $PAGE->set_title($strinstallation);
239 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
240 $PAGE->set_cacheable(false);
242 $output = $PAGE->get_renderer('core', 'admin');
243 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
244 die();
247 // check plugin dependencies
248 $failed = array();
249 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
250 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
251 $PAGE->set_title($strinstallation);
252 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
254 $output = $PAGE->get_renderer('core', 'admin');
255 $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
256 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
257 die();
259 unset($failed);
261 //TODO: add a page with list of non-standard plugins here
263 $strdatabasesetup = get_string('databasesetup');
264 upgrade_init_javascript();
266 $PAGE->navbar->add($strdatabasesetup);
267 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
268 $PAGE->set_heading($strinstallation);
269 $PAGE->set_cacheable(false);
271 $output = $PAGE->get_renderer('core', 'admin');
272 echo $output->header();
274 if (!$DB->setup_is_unicodedb()) {
275 if (!$DB->change_db_encoding()) {
276 // If could not convert successfully, throw error, and prevent installation
277 print_error('unicoderequired', 'admin');
281 install_core($version, true);
285 // Check version of Moodle code on disk compared with database
286 // and upgrade if possible.
288 if (!$cache) {
289 // Do not try to do anything fancy in non-cached mode,
290 // this prevents themes from fetching data from non-existent tables.
291 $PAGE->set_pagelayout('maintenance');
292 $PAGE->set_popup_notification_allowed(false);
295 $stradministration = get_string('administration');
296 $PAGE->set_context(context_system::instance());
298 if (empty($CFG->version)) {
299 print_error('missingconfigversion', 'debug');
302 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
303 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
304 purge_all_caches();
305 redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
308 if (!$cache and $version > $CFG->version) { // upgrade
310 $PAGE->set_url(new moodle_url($PAGE->url, array(
311 'confirmupgrade' => $confirmupgrade,
312 'confirmrelease' => $confirmrelease,
313 'confirmplugincheck' => $confirmplugins,
314 )));
316 check_upgrade_key($upgradekeyhash);
318 // Warning about upgrading a test site.
319 $testsite = false;
320 if (defined('BEHAT_SITE_RUNNING')) {
321 $testsite = 'behat';
324 if (isset($CFG->themerev)) {
325 // Store the themerev to restore after purging caches.
326 $themerev = $CFG->themerev;
329 // We purge all of MUC's caches here.
330 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
331 // This ensures a real config object is loaded and the stores will be purged.
332 // This is the only way we can purge custom caches such as memcache or APC.
333 // Note: all other calls to caches will still used the disabled API.
334 cache_helper::purge_all(true);
335 // We then purge the regular caches.
336 purge_all_caches();
338 if (isset($themerev)) {
339 // Restore the themerev
340 set_config('themerev', $themerev);
343 $output = $PAGE->get_renderer('core', 'admin');
345 if (upgrade_stale_php_files_present()) {
346 $PAGE->set_title($stradministration);
347 $PAGE->set_cacheable(false);
349 echo $output->upgrade_stale_php_files_page();
350 die();
353 if (empty($confirmupgrade)) {
354 $a = new stdClass();
355 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
356 $a->newversion = "$release (".sprintf('%.2f', $version).")";
357 $strdatabasechecking = get_string('databasechecking', '', $a);
359 $PAGE->set_title($stradministration);
360 $PAGE->set_heading($strdatabasechecking);
361 $PAGE->set_cacheable(false);
363 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
364 die();
366 } else if (empty($confirmrelease)){
367 require_once($CFG->libdir.'/environmentlib.php');
368 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
369 $strcurrentrelease = get_string('currentrelease');
371 $PAGE->navbar->add($strcurrentrelease);
372 $PAGE->set_title($strcurrentrelease);
373 $PAGE->set_heading($strcurrentrelease);
374 $PAGE->set_cacheable(false);
376 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
377 die();
379 } else if (empty($confirmplugins)) {
380 $strplugincheck = get_string('plugincheck');
382 $PAGE->navbar->add($strplugincheck);
383 $PAGE->set_title($strplugincheck);
384 $PAGE->set_heading($strplugincheck);
385 $PAGE->set_cacheable(false);
387 $pluginman = core_plugin_manager::instance();
389 // Check for available updates.
390 if ($fetchupdates) {
391 // No sesskey support guaranteed here, because sessions might not work yet.
392 $updateschecker = \core\update\checker::instance();
393 if ($updateschecker->enabled()) {
394 $updateschecker->fetch();
396 redirect($PAGE->url);
399 // Cancel all plugin installations.
400 if ($abortinstallx) {
401 // No sesskey support guaranteed here, because sessions might not work yet.
402 $abortables = $pluginman->list_cancellable_installations();
403 if ($abortables) {
404 if ($confirmabortinstall) {
405 foreach ($abortables as $plugin) {
406 $pluginman->cancel_plugin_installation($plugin->component);
408 redirect($PAGE->url);
409 } else {
410 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
411 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
412 die();
415 redirect($PAGE->url);
418 // Cancel single plugin installation.
419 if ($abortinstall) {
420 // No sesskey support guaranteed here, because sessions might not work yet.
421 if ($confirmabortinstall) {
422 $pluginman->cancel_plugin_installation($abortinstall);
423 redirect($PAGE->url);
424 } else {
425 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
426 $abortable = $pluginman->get_plugin_info($abortinstall);
427 if ($pluginman->can_cancel_plugin_installation($abortable)) {
428 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
429 die();
431 redirect($PAGE->url);
435 // Cancel all plugins upgrades (that is, restore archived versions).
436 if ($abortupgradex) {
437 // No sesskey support guaranteed here, because sessions might not work yet.
438 $restorable = $pluginman->list_restorable_archives();
439 if ($restorable) {
440 upgrade_install_plugins($restorable, $confirmabortupgrade,
441 get_string('cancelupgradehead', 'core_plugin'),
442 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
445 redirect($PAGE->url);
448 // Cancel single plugin upgrade (that is, install the archived version).
449 if ($abortupgrade) {
450 // No sesskey support guaranteed here, because sessions might not work yet.
451 $restorable = $pluginman->list_restorable_archives();
452 if (isset($restorable[$abortupgrade])) {
453 $restorable = array($restorable[$abortupgrade]);
454 upgrade_install_plugins($restorable, $confirmabortupgrade,
455 get_string('cancelupgradehead', 'core_plugin'),
456 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
459 redirect($PAGE->url);
462 // Install all available missing dependencies.
463 if ($installdepx) {
464 // No sesskey support guaranteed here, because sessions might not work yet.
465 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
466 upgrade_install_plugins($installable, $confirminstalldep,
467 get_string('dependencyinstallhead', 'core_plugin'),
468 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
472 // Install single available missing dependency.
473 if ($installdep) {
474 // No sesskey support guaranteed here, because sessions might not work yet.
475 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
476 if (!empty($installable[$installdep])) {
477 $installable = array($installable[$installdep]);
478 upgrade_install_plugins($installable, $confirminstalldep,
479 get_string('dependencyinstallhead', 'core_plugin'),
480 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
485 // Install all available updates.
486 if ($installupdatex) {
487 // No sesskey support guaranteed here, because sessions might not work yet.
488 $installable = $pluginman->filter_installable($pluginman->available_updates());
489 upgrade_install_plugins($installable, $confirminstallupdate,
490 get_string('updateavailableinstallallhead', 'core_admin'),
491 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
495 // Install single available update.
496 if ($installupdate and $installupdateversion) {
497 // No sesskey support guaranteed here, because sessions might not work yet.
498 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
499 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
500 upgrade_install_plugins($installable, $confirminstallupdate,
501 get_string('updateavailableinstallallhead', 'core_admin'),
502 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
503 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
509 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
510 $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
511 die();
513 } else {
514 // Always verify plugin dependencies!
515 $failed = array();
516 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
517 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
518 die();
520 unset($failed);
522 // Launch main upgrade.
523 upgrade_core($version, true);
525 } else if ($version < $CFG->version) {
526 // better stop here, we can not continue with plugin upgrades or anything else
527 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
530 // Updated human-readable release version if necessary
531 if (!$cache and $release <> $CFG->release) { // Update the release version
532 set_config('release', $release);
535 if (!$cache and $branch <> $CFG->branch) { // Update the branch
536 set_config('branch', $branch);
539 if (!$cache and moodle_needs_upgrading()) {
541 $PAGE->set_url(new moodle_url($PAGE->url, array('confirmplugincheck' => $confirmplugins)));
543 check_upgrade_key($upgradekeyhash);
545 if (!$PAGE->headerprinted) {
546 // means core upgrade or installation was not already done
548 $pluginman = core_plugin_manager::instance();
549 $output = $PAGE->get_renderer('core', 'admin');
551 if (!$confirmplugins) {
552 $strplugincheck = get_string('plugincheck');
554 $PAGE->navbar->add($strplugincheck);
555 $PAGE->set_title($strplugincheck);
556 $PAGE->set_heading($strplugincheck);
557 $PAGE->set_cacheable(false);
559 // Check for available updates.
560 if ($fetchupdates) {
561 require_sesskey();
562 $updateschecker = \core\update\checker::instance();
563 if ($updateschecker->enabled()) {
564 $updateschecker->fetch();
566 redirect($PAGE->url);
569 // Cancel all plugin installations.
570 if ($abortinstallx) {
571 require_sesskey();
572 $abortables = $pluginman->list_cancellable_installations();
573 if ($abortables) {
574 if ($confirmabortinstall) {
575 foreach ($abortables as $plugin) {
576 $pluginman->cancel_plugin_installation($plugin->component);
578 redirect($PAGE->url);
579 } else {
580 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
581 'confirmabortinstall' => 1));
582 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
583 die();
586 redirect($PAGE->url);
589 // Cancel single plugin installation.
590 if ($abortinstall) {
591 require_sesskey();
592 if ($confirmabortinstall) {
593 $pluginman->cancel_plugin_installation($abortinstall);
594 redirect($PAGE->url);
595 } else {
596 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
597 $abortable = $pluginman->get_plugin_info($abortinstall);
598 if ($pluginman->can_cancel_plugin_installation($abortable)) {
599 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
600 die();
602 redirect($PAGE->url);
606 // Cancel all plugins upgrades (that is, restore archived versions).
607 if ($abortupgradex) {
608 require_sesskey();
609 $restorable = $pluginman->list_restorable_archives();
610 if ($restorable) {
611 upgrade_install_plugins($restorable, $confirmabortupgrade,
612 get_string('cancelupgradehead', 'core_plugin'),
613 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
616 redirect($PAGE->url);
619 // Cancel single plugin upgrade (that is, install the archived version).
620 if ($abortupgrade) {
621 require_sesskey();
622 $restorable = $pluginman->list_restorable_archives();
623 if (isset($restorable[$abortupgrade])) {
624 $restorable = array($restorable[$abortupgrade]);
625 upgrade_install_plugins($restorable, $confirmabortupgrade,
626 get_string('cancelupgradehead', 'core_plugin'),
627 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
630 redirect($PAGE->url);
633 // Install all available missing dependencies.
634 if ($installdepx) {
635 require_sesskey();
636 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
637 upgrade_install_plugins($installable, $confirminstalldep,
638 get_string('dependencyinstallhead', 'core_plugin'),
639 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
643 // Install single available missing dependency.
644 if ($installdep) {
645 require_sesskey();
646 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
647 if (!empty($installable[$installdep])) {
648 $installable = array($installable[$installdep]);
649 upgrade_install_plugins($installable, $confirminstalldep,
650 get_string('dependencyinstallhead', 'core_plugin'),
651 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
656 // Install all available updates.
657 if ($installupdatex) {
658 require_sesskey();
659 $installable = $pluginman->filter_installable($pluginman->available_updates());
660 upgrade_install_plugins($installable, $confirminstallupdate,
661 get_string('updateavailableinstallallhead', 'core_admin'),
662 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
666 // Install single available update.
667 if ($installupdate and $installupdateversion) {
668 require_sesskey();
669 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
670 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
671 upgrade_install_plugins($installable, $confirminstallupdate,
672 get_string('updateavailableinstallallhead', 'core_admin'),
673 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
674 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
680 // Show plugins info.
681 echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
682 $version, $showallplugins,
683 new moodle_url($PAGE->url),
684 new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
685 die();
688 // Make sure plugin dependencies are always checked.
689 $failed = array();
690 if (!$pluginman->all_plugins_ok($version, $failed)) {
691 $output = $PAGE->get_renderer('core', 'admin');
692 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
693 die();
695 unset($failed);
698 // install/upgrade all plugins and other parts
699 upgrade_noncore(true);
702 // If this is the first install, indicate that this site is fully configured
703 // except the admin password
704 if (during_initial_install()) {
705 set_config('rolesactive', 1); // after this, during_initial_install will return false.
706 set_config('adminsetuppending', 1);
707 // we need this redirect to setup proper session
708 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
711 // make sure admin user is created - this is the last step because we need
712 // session to be working properly in order to edit admin account
713 if (!empty($CFG->adminsetuppending)) {
714 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
715 if (!$sessionstarted) {
716 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
717 } else {
718 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
719 if (!$sessionverify) {
720 $SESSION->sessionverify = 1;
721 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
722 } else {
723 if (empty($SESSION->sessionverify)) {
724 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
726 unset($SESSION->sessionverify);
730 // Cleanup SESSION to make sure other code does not complain in the future.
731 unset($SESSION->has_timed_out);
732 unset($SESSION->wantsurl);
734 // 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
735 $adminids = explode(',', $CFG->siteadmins);
736 $adminuser = get_complete_user_data('id', reset($adminids));
738 if ($adminuser->password === 'adminsetuppending') {
739 // prevent installation hijacking
740 if ($adminuser->lastip !== getremoteaddr()) {
741 print_error('installhijacked', 'admin');
743 // login user and let him set password and admin details
744 $adminuser->newadminuser = 1;
745 complete_user_login($adminuser);
746 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
748 } else {
749 unset_config('adminsetuppending');
752 } else {
753 // just make sure upgrade logging is properly terminated
754 upgrade_finished('upgradesettings.php');
757 if (has_capability('moodle/site:config', context_system::instance())) {
758 if ($fetchupdates) {
759 require_sesskey();
760 $updateschecker = \core\update\checker::instance();
761 if ($updateschecker->enabled()) {
762 $updateschecker->fetch();
764 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
768 // Now we can be sure everything was upgraded and caches work fine,
769 // redirect if necessary to make sure caching is enabled.
770 if (!$cache) {
771 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
774 // Check for valid admin user - no guest autologin
775 require_login(0, false);
776 if (isguestuser()) {
777 // Login as real user!
778 $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
779 redirect(get_login_url());
781 $context = context_system::instance();
783 if (!has_capability('moodle/site:config', $context)) {
784 // Do not throw exception display an empty page with administration menu if visible for current user.
785 $PAGE->set_title($SITE->fullname);
786 $PAGE->set_heading($SITE->fullname);
787 echo $OUTPUT->header();
788 echo $OUTPUT->footer();
789 exit;
792 // check that site is properly customized
793 $site = get_site();
794 if (empty($site->shortname)) {
795 // probably new installation - lets return to frontpage after this step
796 // remove settings that we want uninitialised
797 unset_config('registerauth');
798 unset_config('timezone'); // Force admin to select timezone!
799 redirect('upgradesettings.php?return=site');
802 // setup critical warnings before printing admin tree block
803 $insecuredataroot = is_dataroot_insecure(true);
804 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
806 $adminroot = admin_get_root();
808 // Check if there are any new admin settings which have still yet to be set
809 if (any_new_admin_settings($adminroot)){
810 redirect('upgradesettings.php');
813 // Return to original page that started the plugin uninstallation if necessary.
814 if (isset($SESSION->pluginuninstallreturn)) {
815 $return = $SESSION->pluginuninstallreturn;
816 unset($SESSION->pluginuninstallreturn);
817 if ($return) {
818 redirect($return);
822 // Everything should now be set up, and the user is an admin
824 // Print default admin page with notifications.
825 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
827 // We make the assumption that at least one schedule task should run once per day.
828 $lastcron = $DB->get_field_sql('SELECT MAX(lastruntime) FROM {task_scheduled}');
829 $cronoverdue = ($lastcron < time() - 3600 * 24);
830 $dbproblems = $DB->diagnose();
831 $maintenancemode = !empty($CFG->maintenance_enabled);
833 // Available updates for Moodle core.
834 $updateschecker = \core\update\checker::instance();
835 $availableupdates = array();
836 $availableupdatesfetch = null;
838 if ($updateschecker->enabled()) {
839 // Only compute the update information when it is going to be displayed to the user.
840 $availableupdates['core'] = $updateschecker->get_update_info('core',
841 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
843 // Available updates for contributed plugins
844 $pluginman = core_plugin_manager::instance();
845 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
846 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
847 $pluginavailableupdates = $plugininfo->available_updates();
848 if (!empty($pluginavailableupdates)) {
849 foreach ($pluginavailableupdates as $pluginavailableupdate) {
850 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
851 $availableupdates[$plugintype.'_'.$pluginname] = array();
853 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
859 // The timestamp of the most recent check for available updates
860 $availableupdatesfetch = $updateschecker->get_last_timefetched();
863 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
864 //check if the site is registered on Moodle.org
865 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
866 // Check if there are any cache warnings.
867 $cachewarnings = cache_helper::warnings();
868 // Check if there are events 1 API handlers.
869 $eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
870 $themedesignermode = !empty($CFG->themedesignermode);
872 // Check if a directory with development libraries exists.
873 if (is_dir($CFG->dirroot.'/vendor') || is_dir($CFG->dirroot.'/node_modules')) {
874 $devlibdir = true;
875 } else {
876 $devlibdir = false;
879 admin_externalpage_setup('adminnotifications');
881 $output = $PAGE->get_renderer('core', 'admin');
883 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
884 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
885 $registered, $cachewarnings, $eventshandlers, $themedesignermode, $devlibdir);