MDL-58857 admin: Terminate the session if a major upgrade is required
[moodle.git] / admin / index.php
CommitLineData
8580535b 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Main administration script.
20 *
d078f6d3 21 * @package core
8580535b 22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
f9903ed0 25
00be1916 26// Check that config.php exists, if not then call the install script
27if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
3ba28588 29 die();
00be1916 30}
31
32// Check that PHP is of a sufficient version as soon as possible
ae96cf62 33if (version_compare(phpversion(), '5.6.5') < 0) {
00be1916 34 $phpversion = phpversion();
35 // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
ae96cf62 36 echo "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).<br />";
eab044a0 37 echo "Please upgrade your server software or install older Moodle version.";
3ba28588 38 die();
00be1916 39}
40
695940df
PS
41// make sure iconv is available and actually works
42if (!function_exists('iconv')) {
43 // this should not happen, this must be very borked install
7d85a4e2 44 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
695940df
PS
45 die();
46}
695940df 47
3c9535dd
JC
48// Make sure php5-json is available.
49if (!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();
53}
54
f029401b 55// Make sure xml extension is available.
56if (!extension_loaded('xml')) {
57 echo 'Moodle requires the xml PHP extension. Please install or enable the xml extension.';
58 die();
59}
60
cbad562e 61define('NO_OUTPUT_BUFFERING', true);
00be1916 62
98b32c9e
DM
63if (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();
71}
72
fc281113
PS
73if ((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']))) {
e2e35e71
PS
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.
bf8c71b7 79 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
e2e35e71 80 define('CACHE_DISABLE_ALL', true);
c05a5099
PS
81
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();
86 }
82b1cf00
PS
87 $cache = 0;
88
89} else {
90 $cache = 1;
e2e35e71
PS
91}
92
00be1916 93require('../config.php');
3274c5db
FM
94
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.
97if (function_exists('opcache_invalidate')) {
98 opcache_invalidate($CFG->dirroot . '/version.php', true);
99}
100// Make sure the component cache gets rebuilt if necessary, any method that
101// indirectly calls the protected init() method is good here.
102core_component::get_core_subsystems();
103
a011effb
AN
104if (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();
108}
109
bc76b3c0 110require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
111require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
00be1916 112
531381f9
DM
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?
aa1d100c 118$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
531381f9
DM
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.
c20e9ae8
DM
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.
531381f9
DM
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?
00be1916 134
b0fc7898
DM
135if (!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;
145}
146
e2e35e71
PS
147// Set up PAGE.
148$url = new moodle_url('/admin/index.php');
fc281113 149$url->param('cache', $cache);
98b32c9e
DM
150if (isset($upgradekeyhash)) {
151 $url->param('upgradekeyhash', $upgradekeyhash);
152}
e2e35e71
PS
153$PAGE->set_url($url);
154unset($url);
155
82b1cf00 156// Are we returning from an add-on installation request at moodle.org/plugins?
b0fc7898 157if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
82b1cf00
PS
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());
165 }
166 redirect($target);
167}
168
6d80fad4 169$PAGE->set_pagelayout('admin'); // Set a default pagelayout
ee73b1ff 170
00be1916 171$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
172
e2e35e71
PS
173// Check some PHP server settings
174
00be1916 175if (ini_get_bool('session.auto_start')) {
176 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
177}
178
00be1916 179if (!ini_get_bool('file_uploads')) {
180 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
181}
182
183if (is_float_problem()) {
184 print_error('phpfloatproblem', 'admin', '', $documentationlink);
185}
186
00be1916 187// Set some necessary variables during set-up to avoid PHP warnings later on this page
00be1916 188if (!isset($CFG->release)) {
bc76b3c0 189 $CFG->release = '';
00be1916 190}
191if (!isset($CFG->version)) {
bc76b3c0 192 $CFG->version = '';
00be1916 193}
ad394588
AB
194if (!isset($CFG->branch)) {
195 $CFG->branch = '';
196}
00be1916 197
198$version = null;
199$release = null;
ed01233a
AB
200$branch = null;
201require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
00be1916 202$CFG->target_release = $release; // used during installation and upgrades
203
204if (!$version or !$release) {
205 print_error('withoutversion', 'debug'); // without version, stop
206}
207
3316fe24 208if (!core_tables_exist()) {
78946b9b 209 $PAGE->set_pagelayout('maintenance');
d3875524 210 $PAGE->set_popup_notification_allowed(false);
00be1916 211
212 // fake some settings
213 $CFG->docroot = 'http://docs.moodle.org';
214
215 $strinstallation = get_string('installation', 'install');
216
217 // remove current session content completely
d79d5ac2 218 \core\session\manager::terminate_current();
00be1916 219
220 if (empty($agreelicense)) {
221 $strlicense = get_string('license');
cc359566 222
69d77c23 223 $PAGE->navbar->add($strlicense);
224 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
225 $PAGE->set_heading($strinstallation);
226 $PAGE->set_cacheable(false);
cc359566 227
da2fdc3f 228 $output = $PAGE->get_renderer('core', 'admin');
cc359566 229 echo $output->install_licence_page();
3ba28588 230 die();
249ab745 231 }
00be1916 232 if (empty($confirmrelease)) {
cc359566
TH
233 require_once($CFG->libdir.'/environmentlib.php');
234 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
bc76b3c0 235 $strcurrentrelease = get_string('currentrelease');
cc359566 236
69d77c23 237 $PAGE->navbar->add($strcurrentrelease);
1a4d6a5b
DM
238 $PAGE->set_title($strinstallation);
239 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
69d77c23 240 $PAGE->set_cacheable(false);
249ab745 241
cc359566 242 $output = $PAGE->get_renderer('core', 'admin');
8d1da748 243 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
3ba28588 244 die();
1dd24519 245 }
246
39f15cc7
DM
247 // check plugin dependencies
248 $failed = array();
e87214bd 249 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
39f15cc7
DM
250 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
251 $PAGE->set_title($strinstallation);
252 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
253
254 $output = $PAGE->get_renderer('core', 'admin');
98b32c9e 255 $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
39f15cc7
DM
256 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
257 die();
258 }
259 unset($failed);
260
8d1da748
PS
261 //TODO: add a page with list of non-standard plugins here
262
bc76b3c0 263 $strdatabasesetup = get_string('databasesetup');
543f54d3 264 upgrade_init_javascript();
cc359566 265
69d77c23 266 $PAGE->navbar->add($strdatabasesetup);
267 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
2ea05146 268 $PAGE->set_heading($strinstallation);
69d77c23 269 $PAGE->set_cacheable(false);
cc359566 270
da2fdc3f
TH
271 $output = $PAGE->get_renderer('core', 'admin');
272 echo $output->header();
d02bc6ce 273
00be1916 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');
db5af934 278 }
db5af934 279 }
90509582 280
00be1916 281 install_core($version, true);
282}
db5af934 283
db5af934 284
00be1916 285// Check version of Moodle code on disk compared with database
286// and upgrade if possible.
db5af934 287
1a361486
PS
288if (!$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);
293}
294
00be1916 295$stradministration = get_string('administration');
bf006d2c 296$PAGE->set_context(context_system::instance());
db5af934 297
00be1916 298if (empty($CFG->version)) {
299 print_error('missingconfigversion', 'debug');
300}
db5af934 301
e2e35e71 302// Detect config cache inconsistency, this happens when you switch branches on dev servers.
82b1cf00
PS
303if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
304 purge_all_caches();
98b32c9e 305 redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
e2e35e71
PS
306}
307
82b1cf00 308if (!$cache and $version > $CFG->version) { // upgrade
6e09cf98 309
2f29cf6e
DM
310 $PAGE->set_url(new moodle_url($PAGE->url, array(
311 'confirmupgrade' => $confirmupgrade,
312 'confirmrelease' => $confirmrelease,
313 'confirmplugincheck' => $confirmplugins,
314 )));
315
98b32c9e
DM
316 check_upgrade_key($upgradekeyhash);
317
6e09cf98
DM
318 // Warning about upgrading a test site.
319 $testsite = false;
320 if (defined('BEHAT_SITE_RUNNING')) {
321 $testsite = 'behat';
322 }
323
ea10a031
AN
324 if (isset($CFG->themerev)) {
325 // Store the themerev to restore after purging caches.
326 $themerev = $CFG->themerev;
327 }
328
b0dd08dd
SH
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.
a95682b2 336 purge_all_caches();
33082590 337
ea10a031
AN
338 if (isset($themerev)) {
339 // Restore the themerev
340 set_config('themerev', $themerev);
341 }
342
fc281113
PS
343 $output = $PAGE->get_renderer('core', 'admin');
344
9008ec16
PS
345 if (upgrade_stale_php_files_present()) {
346 $PAGE->set_title($stradministration);
347 $PAGE->set_cacheable(false);
348
9008ec16
PS
349 echo $output->upgrade_stale_php_files_page();
350 die();
351 }
352
00be1916 353 if (empty($confirmupgrade)) {
17854cb9 354 $a = new stdClass();
c22a579b
RT
355 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
356 $a->newversion = "$release (".sprintf('%.2f', $version).")";
cc359566
TH
357 $strdatabasechecking = get_string('databasechecking', '', $a);
358
1a4d6a5b
DM
359 $PAGE->set_title($stradministration);
360 $PAGE->set_heading($strdatabasechecking);
69d77c23 361 $PAGE->set_cacheable(false);
cc359566 362
6e09cf98 363 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
3ba28588 364 die();
db5af934 365
00be1916 366 } else if (empty($confirmrelease)){
cc359566
TH
367 require_once($CFG->libdir.'/environmentlib.php');
368 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
bc76b3c0 369 $strcurrentrelease = get_string('currentrelease');
cc359566 370
69d77c23 371 $PAGE->navbar->add($strcurrentrelease);
372 $PAGE->set_title($strcurrentrelease);
52273536 373 $PAGE->set_heading($strcurrentrelease);
69d77c23 374 $PAGE->set_cacheable(false);
db5af934 375
faadd326
TH
376 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
377 die();
db5af934 378
cc359566 379 } else if (empty($confirmplugins)) {
00be1916 380 $strplugincheck = get_string('plugincheck');
cc359566 381
69d77c23 382 $PAGE->navbar->add($strplugincheck);
383 $PAGE->set_title($strplugincheck);
52273536 384 $PAGE->set_heading($strplugincheck);
69d77c23 385 $PAGE->set_cacheable(false);
b9934a17 386
2f29cf6e
DM
387 $pluginman = core_plugin_manager::instance();
388
c20e9ae8
DM
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();
395 }
396 redirect($PAGE->url);
397 }
398
4d7528f9 399 // Cancel all plugin installations.
2f29cf6e 400 if ($abortinstallx) {
531381f9 401 // No sesskey support guaranteed here, because sessions might not work yet.
c20e9ae8
DM
402 $abortables = $pluginman->list_cancellable_installations();
403 if ($abortables) {
404 if ($confirmabortinstall) {
405 foreach ($abortables as $plugin) {
406 $pluginman->cancel_plugin_installation($plugin->component);
407 }
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();
413 }
4d7528f9 414 }
c20e9ae8 415 redirect($PAGE->url);
2f29cf6e
DM
416 }
417
4d7528f9 418 // Cancel single plugin installation.
2f29cf6e 419 if ($abortinstall) {
531381f9 420 // No sesskey support guaranteed here, because sessions might not work yet.
4d7528f9
DM
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));
c20e9ae8
DM
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();
430 }
431 redirect($PAGE->url);
4d7528f9 432 }
2f29cf6e
DM
433 }
434
c20e9ae8
DM
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))
443 );
444 }
445 redirect($PAGE->url);
446 }
447
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))
457 );
458 }
459 redirect($PAGE->url);
460 }
461
531381f9
DM
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));
2d00be61 466 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
467 get_string('dependencyinstallhead', 'core_plugin'),
468 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
469 );
470 }
471
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]);
2d00be61 478 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
479 get_string('dependencyinstallhead', 'core_plugin'),
480 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
481 );
482 }
483 }
484
4d7528f9 485 // Install all available updates.
531381f9
DM
486 if ($installupdatex) {
487 // No sesskey support guaranteed here, because sessions might not work yet.
488 $installable = $pluginman->filter_installable($pluginman->available_updates());
2d00be61 489 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
490 get_string('updateavailableinstallallhead', 'core_admin'),
491 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
492 );
493 }
494
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));
2d00be61 500 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
501 get_string('updateavailableinstallallhead', 'core_admin'),
502 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
503 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
504 )
505 );
506 }
507 }
ead8ba3b 508
e87214bd 509 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
2f29cf6e 510 $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
00be1916 511 die();
db5af934 512
00be1916 513 } else {
fc281113
PS
514 // Always verify plugin dependencies!
515 $failed = array();
516 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
2f29cf6e 517 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
fc281113
PS
518 die();
519 }
520 unset($failed);
521
522 // Launch main upgrade.
00be1916 523 upgrade_core($version, true);
88582df4 524 }
00be1916 525} else if ($version < $CFG->version) {
80380bd7
PS
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/'));
00be1916 528}
529
530// Updated human-readable release version if necessary
82b1cf00 531if (!$cache and $release <> $CFG->release) { // Update the release version
bc76b3c0 532 set_config('release', $release);
00be1916 533}
534
82b1cf00 535if (!$cache and $branch <> $CFG->branch) { // Update the branch
ed01233a
AB
536 set_config('branch', $branch);
537}
538
82b1cf00 539if (!$cache and moodle_needs_upgrading()) {
98b32c9e 540
2f29cf6e
DM
541 $PAGE->set_url(new moodle_url($PAGE->url, array('confirmplugincheck' => $confirmplugins)));
542
98b32c9e
DM
543 check_upgrade_key($upgradekeyhash);
544
575f245f 545 if (!$PAGE->headerprinted) {
2361b7dd 546 // means core upgrade or installation was not already done
4d7528f9 547
531381f9 548 $pluginman = core_plugin_manager::instance();
4d7528f9
DM
549 $output = $PAGE->get_renderer('core', 'admin');
550
575f245f 551 if (!$confirmplugins) {
cc359566
TH
552 $strplugincheck = get_string('plugincheck');
553
575f245f
PS
554 $PAGE->navbar->add($strplugincheck);
555 $PAGE->set_title($strplugincheck);
556 $PAGE->set_heading($strplugincheck);
557 $PAGE->set_cacheable(false);
b9934a17 558
c20e9ae8
DM
559 // Check for available updates.
560 if ($fetchupdates) {
561 require_sesskey();
562 $updateschecker = \core\update\checker::instance();
563 if ($updateschecker->enabled()) {
564 $updateschecker->fetch();
565 }
566 redirect($PAGE->url);
567 }
568
4d7528f9 569 // Cancel all plugin installations.
2f29cf6e
DM
570 if ($abortinstallx) {
571 require_sesskey();
c20e9ae8
DM
572 $abortables = $pluginman->list_cancellable_installations();
573 if ($abortables) {
574 if ($confirmabortinstall) {
575 foreach ($abortables as $plugin) {
576 $pluginman->cancel_plugin_installation($plugin->component);
577 }
578 redirect($PAGE->url);
579 } else {
30c26421
DM
580 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
581 'confirmabortinstall' => 1));
c20e9ae8
DM
582 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
583 die();
584 }
4d7528f9 585 }
c20e9ae8 586 redirect($PAGE->url);
2f29cf6e
DM
587 }
588
4d7528f9 589 // Cancel single plugin installation.
2f29cf6e
DM
590 if ($abortinstall) {
591 require_sesskey();
4d7528f9
DM
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));
c20e9ae8
DM
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();
601 }
602 redirect($PAGE->url);
4d7528f9 603 }
2f29cf6e
DM
604 }
605
c20e9ae8
DM
606 // Cancel all plugins upgrades (that is, restore archived versions).
607 if ($abortupgradex) {
fc281113 608 require_sesskey();
c20e9ae8
DM
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))
614 );
615 }
616 redirect($PAGE->url);
617 }
618
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))
628 );
fc281113 629 }
96dd9237
DM
630 redirect($PAGE->url);
631 }
632
531381f9
DM
633 // Install all available missing dependencies.
634 if ($installdepx) {
635 require_sesskey();
636 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
2d00be61 637 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
638 get_string('dependencyinstallhead', 'core_plugin'),
639 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
640 );
641 }
642
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]);
2d00be61 649 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
650 get_string('dependencyinstallhead', 'core_plugin'),
651 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
652 );
653 }
654 }
12890c57 655
531381f9
DM
656 // Install all available updates.
657 if ($installupdatex) {
658 require_sesskey();
659 $installable = $pluginman->filter_installable($pluginman->available_updates());
2d00be61 660 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
661 get_string('updateavailableinstallallhead', 'core_admin'),
662 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
663 );
664 }
292dbeac 665
531381f9
DM
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));
2d00be61 671 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
672 get_string('updateavailableinstallallhead', 'core_admin'),
673 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
674 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
675 )
676 );
292dbeac
DM
677 }
678 }
679
fc281113 680 // Show plugins info.
2f29cf6e 681 echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
96dd9237
DM
682 $version, $showallplugins,
683 new moodle_url($PAGE->url),
98b32c9e 684 new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
fc281113
PS
685 die();
686 }
687
688 // Make sure plugin dependencies are always checked.
689 $failed = array();
2f29cf6e 690 if (!$pluginman->all_plugins_ok($version, $failed)) {
12890c57 691 $output = $PAGE->get_renderer('core', 'admin');
2f29cf6e 692 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
575f245f
PS
693 die();
694 }
fc281113 695 unset($failed);
575f245f 696 }
fc281113 697
575f245f
PS
698 // install/upgrade all plugins and other parts
699 upgrade_noncore(true);
700}
00be1916 701
31a99877 702// If this is the first install, indicate that this site is fully configured
703// except the admin password
704if (during_initial_install()) {
705 set_config('rolesactive', 1); // after this, during_initial_install will return false.
00be1916 706 set_config('adminsetuppending', 1);
c8ae55e9 707 // we need this redirect to setup proper session
00be1916 708 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
709}
710
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");
88582df4 722 } else {
00be1916 723 if (empty($SESSION->sessionverify)) {
724 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
88582df4 725 }
00be1916 726 unset($SESSION->sessionverify);
88582df4 727 }
00be1916 728 }
88582df4 729
4f73591a
PS
730 // Cleanup SESSION to make sure other code does not complain in the future.
731 unset($SESSION->has_timed_out);
732 unset($SESSION->wantsurl);
733
a7c9609b 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
f20edd52
PS
735 $adminids = explode(',', $CFG->siteadmins);
736 $adminuser = get_complete_user_data('id', reset($adminids));
88582df4 737
00be1916 738 if ($adminuser->password === 'adminsetuppending') {
739 // prevent installation hijacking
740 if ($adminuser->lastip !== getremoteaddr()) {
741 print_error('installhijacked', 'admin');
35d6a2a4 742 }
00be1916 743 // login user and let him set password and admin details
744 $adminuser->newadminuser = 1;
0342fc36 745 complete_user_login($adminuser);
00be1916 746 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
db5af934 747
5c144d60 748 } else {
00be1916 749 unset_config('adminsetuppending');
4da1a0a1 750 }
751
00be1916 752} else {
753 // just make sure upgrade logging is properly terminated
754 upgrade_finished('upgradesettings.php');
755}
b3732604 756
fc281113
PS
757if (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();
763 }
764 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
765 }
766}
767
e2e35e71
PS
768// Now we can be sure everything was upgraded and caches work fine,
769// redirect if necessary to make sure caching is enabled.
82b1cf00
PS
770if (!$cache) {
771 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
e2e35e71
PS
772}
773
00be1916 774// Check for valid admin user - no guest autologin
775require_login(0, false);
fc1d0e82
776if (isguestuser()) {
777 // Login as real user!
778 $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
779 redirect(get_login_url());
780}
bf006d2c 781$context = context_system::instance();
336a8433
MG
782
783if (!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;
790}
00be1916 791
792// check that site is properly customized
793$site = get_site();
794if (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');
d6e7a63d 798 unset_config('timezone'); // Force admin to select timezone!
00be1916 799 redirect('upgradesettings.php?return=site');
800}
801
00be1916 802// setup critical warnings before printing admin tree block
bc76b3c0 803$insecuredataroot = is_dataroot_insecure(true);
00be1916 804$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
805
806$adminroot = admin_get_root();
807
808// Check if there are any new admin settings which have still yet to be set
809if (any_new_admin_settings($adminroot)){
810 redirect('upgradesettings.php');
811}
812
e87214bd
PS
813// Return to original page that started the plugin uninstallation if necessary.
814if (isset($SESSION->pluginuninstallreturn)) {
815 $return = $SESSION->pluginuninstallreturn;
816 unset($SESSION->pluginuninstallreturn);
817 if ($return) {
818 redirect($return);
819 }
820}
821
00be1916 822// Everything should now be set up, and the user is an admin
823
824// Print default admin page with notifications.
cc359566 825$errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
a95682b2 826
d5fcf4b4
DP
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}');
cc359566
TH
829$cronoverdue = ($lastcron < time() - 3600 * 24);
830$dbproblems = $DB->diagnose();
831$maintenancemode = !empty($CFG->maintenance_enabled);
74944b73 832
ca6f3a8b 833// Available updates for Moodle core.
e87214bd 834$updateschecker = \core\update\checker::instance();
966bd785 835$availableupdates = array();
ca6f3a8b
FM
836$availableupdatesfetch = null;
837
e9d3c212 838if ($updateschecker->enabled()) {
ca6f3a8b
FM
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));
842
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) {
30d8bc5f
DM
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();
966bd785 852 }
30d8bc5f 853 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
966bd785
DM
854 }
855 }
856 }
857 }
966bd785 858
ca6f3a8b
FM
859 // The timestamp of the most recent check for available updates
860 $availableupdatesfetch = $updateschecker->get_last_timefetched();
861}
55585f3a 862
0aff15c2 863$buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
b3245b75
DP
864//check if the site is registered on Moodle.org
865$registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
915140c9
SH
866// Check if there are any cache warnings.
867$cachewarnings = cache_helper::warnings();
356e2354
DM
868// Check if there are events 1 API handlers.
869$eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
586cf929 870$themedesignermode = !empty($CFG->themedesignermode);
0aff15c2 871
cc359566 872admin_externalpage_setup('adminnotifications');
55585f3a 873
cc359566 874$output = $PAGE->get_renderer('core', 'admin');
915140c9
SH
875
876echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
877 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
586cf929 878 $registered, $cachewarnings, $eventshandlers, $themedesignermode);