MDL-55083 admin: provide friendly error in case phpxml is missing
[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
bc76b3c0 104require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
105require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
00be1916 106
531381f9
DM
107$confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
108$confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
109$confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
110$showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
111$agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
aa1d100c 112$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
531381f9
DM
113$newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
114$upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
115$installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
116$installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
117$confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
118$abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
119$abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
120$confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
c20e9ae8
DM
121$abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
122$abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
123$confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
531381f9
DM
124$installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
125$installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
126$installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
127$confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
00be1916 128
b0fc7898
DM
129if (!empty($CFG->disableupdateautodeploy)) {
130 // Invalidate all requests to install plugins via the admin UI.
131 $newaddonreq = null;
132 $installdep = null;
133 $installdepx = false;
134 $abortupgrade = null;
135 $abortupgradex = null;
136 $installupdate = null;
137 $installupdateversion = null;
138 $installupdatex = false;
139}
140
e2e35e71
PS
141// Set up PAGE.
142$url = new moodle_url('/admin/index.php');
fc281113 143$url->param('cache', $cache);
98b32c9e
DM
144if (isset($upgradekeyhash)) {
145 $url->param('upgradekeyhash', $upgradekeyhash);
146}
e2e35e71
PS
147$PAGE->set_url($url);
148unset($url);
149
82b1cf00 150// Are we returning from an add-on installation request at moodle.org/plugins?
b0fc7898 151if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
82b1cf00
PS
152 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
153 'installaddonrequest' => $newaddonreq,
154 'confirm' => 0));
155 if (!isloggedin() or isguestuser()) {
156 // Login and go the the add-on tool page.
157 $SESSION->wantsurl = $target->out();
158 redirect(get_login_url());
159 }
160 redirect($target);
161}
162
6d80fad4 163$PAGE->set_pagelayout('admin'); // Set a default pagelayout
ee73b1ff 164
00be1916 165$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
166
e2e35e71
PS
167// Check some PHP server settings
168
00be1916 169if (ini_get_bool('session.auto_start')) {
170 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
171}
172
00be1916 173if (!ini_get_bool('file_uploads')) {
174 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
175}
176
177if (is_float_problem()) {
178 print_error('phpfloatproblem', 'admin', '', $documentationlink);
179}
180
00be1916 181// Set some necessary variables during set-up to avoid PHP warnings later on this page
00be1916 182if (!isset($CFG->release)) {
bc76b3c0 183 $CFG->release = '';
00be1916 184}
185if (!isset($CFG->version)) {
bc76b3c0 186 $CFG->version = '';
00be1916 187}
ad394588
AB
188if (!isset($CFG->branch)) {
189 $CFG->branch = '';
190}
00be1916 191
192$version = null;
193$release = null;
ed01233a
AB
194$branch = null;
195require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
00be1916 196$CFG->target_release = $release; // used during installation and upgrades
197
198if (!$version or !$release) {
199 print_error('withoutversion', 'debug'); // without version, stop
200}
201
3316fe24 202if (!core_tables_exist()) {
78946b9b 203 $PAGE->set_pagelayout('maintenance');
d3875524 204 $PAGE->set_popup_notification_allowed(false);
00be1916 205
206 // fake some settings
207 $CFG->docroot = 'http://docs.moodle.org';
208
209 $strinstallation = get_string('installation', 'install');
210
211 // remove current session content completely
d79d5ac2 212 \core\session\manager::terminate_current();
00be1916 213
214 if (empty($agreelicense)) {
215 $strlicense = get_string('license');
cc359566 216
69d77c23 217 $PAGE->navbar->add($strlicense);
218 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
219 $PAGE->set_heading($strinstallation);
220 $PAGE->set_cacheable(false);
cc359566 221
da2fdc3f 222 $output = $PAGE->get_renderer('core', 'admin');
cc359566 223 echo $output->install_licence_page();
3ba28588 224 die();
249ab745 225 }
00be1916 226 if (empty($confirmrelease)) {
cc359566
TH
227 require_once($CFG->libdir.'/environmentlib.php');
228 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
bc76b3c0 229 $strcurrentrelease = get_string('currentrelease');
cc359566 230
69d77c23 231 $PAGE->navbar->add($strcurrentrelease);
1a4d6a5b
DM
232 $PAGE->set_title($strinstallation);
233 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
69d77c23 234 $PAGE->set_cacheable(false);
249ab745 235
cc359566 236 $output = $PAGE->get_renderer('core', 'admin');
8d1da748 237 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
3ba28588 238 die();
1dd24519 239 }
240
39f15cc7
DM
241 // check plugin dependencies
242 $failed = array();
e87214bd 243 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
39f15cc7
DM
244 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
245 $PAGE->set_title($strinstallation);
246 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
247
248 $output = $PAGE->get_renderer('core', 'admin');
98b32c9e 249 $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
39f15cc7
DM
250 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
251 die();
252 }
253 unset($failed);
254
8d1da748
PS
255 //TODO: add a page with list of non-standard plugins here
256
bc76b3c0 257 $strdatabasesetup = get_string('databasesetup');
543f54d3 258 upgrade_init_javascript();
cc359566 259
69d77c23 260 $PAGE->navbar->add($strdatabasesetup);
261 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
2ea05146 262 $PAGE->set_heading($strinstallation);
69d77c23 263 $PAGE->set_cacheable(false);
cc359566 264
da2fdc3f
TH
265 $output = $PAGE->get_renderer('core', 'admin');
266 echo $output->header();
d02bc6ce 267
00be1916 268 if (!$DB->setup_is_unicodedb()) {
269 if (!$DB->change_db_encoding()) {
270 // If could not convert successfully, throw error, and prevent installation
271 print_error('unicoderequired', 'admin');
db5af934 272 }
db5af934 273 }
90509582 274
00be1916 275 install_core($version, true);
276}
db5af934 277
db5af934 278
00be1916 279// Check version of Moodle code on disk compared with database
280// and upgrade if possible.
db5af934 281
1a361486
PS
282if (!$cache) {
283 // Do not try to do anything fancy in non-cached mode,
284 // this prevents themes from fetching data from non-existent tables.
285 $PAGE->set_pagelayout('maintenance');
286 $PAGE->set_popup_notification_allowed(false);
287}
288
00be1916 289$stradministration = get_string('administration');
bf006d2c 290$PAGE->set_context(context_system::instance());
db5af934 291
00be1916 292if (empty($CFG->version)) {
293 print_error('missingconfigversion', 'debug');
294}
db5af934 295
e2e35e71 296// Detect config cache inconsistency, this happens when you switch branches on dev servers.
82b1cf00
PS
297if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
298 purge_all_caches();
98b32c9e 299 redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
e2e35e71
PS
300}
301
82b1cf00 302if (!$cache and $version > $CFG->version) { // upgrade
6e09cf98 303
2f29cf6e
DM
304 $PAGE->set_url(new moodle_url($PAGE->url, array(
305 'confirmupgrade' => $confirmupgrade,
306 'confirmrelease' => $confirmrelease,
307 'confirmplugincheck' => $confirmplugins,
308 )));
309
98b32c9e
DM
310 check_upgrade_key($upgradekeyhash);
311
6e09cf98
DM
312 // Warning about upgrading a test site.
313 $testsite = false;
314 if (defined('BEHAT_SITE_RUNNING')) {
315 $testsite = 'behat';
316 }
317
b0dd08dd
SH
318 // We purge all of MUC's caches here.
319 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
320 // This ensures a real config object is loaded and the stores will be purged.
321 // This is the only way we can purge custom caches such as memcache or APC.
322 // Note: all other calls to caches will still used the disabled API.
323 cache_helper::purge_all(true);
324 // We then purge the regular caches.
a95682b2 325 purge_all_caches();
33082590 326
fc281113
PS
327 $output = $PAGE->get_renderer('core', 'admin');
328
9008ec16
PS
329 if (upgrade_stale_php_files_present()) {
330 $PAGE->set_title($stradministration);
331 $PAGE->set_cacheable(false);
332
9008ec16
PS
333 echo $output->upgrade_stale_php_files_page();
334 die();
335 }
336
00be1916 337 if (empty($confirmupgrade)) {
17854cb9 338 $a = new stdClass();
c22a579b
RT
339 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
340 $a->newversion = "$release (".sprintf('%.2f', $version).")";
cc359566
TH
341 $strdatabasechecking = get_string('databasechecking', '', $a);
342
1a4d6a5b
DM
343 $PAGE->set_title($stradministration);
344 $PAGE->set_heading($strdatabasechecking);
69d77c23 345 $PAGE->set_cacheable(false);
cc359566 346
6e09cf98 347 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
3ba28588 348 die();
db5af934 349
00be1916 350 } else if (empty($confirmrelease)){
cc359566
TH
351 require_once($CFG->libdir.'/environmentlib.php');
352 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
bc76b3c0 353 $strcurrentrelease = get_string('currentrelease');
cc359566 354
69d77c23 355 $PAGE->navbar->add($strcurrentrelease);
356 $PAGE->set_title($strcurrentrelease);
52273536 357 $PAGE->set_heading($strcurrentrelease);
69d77c23 358 $PAGE->set_cacheable(false);
db5af934 359
faadd326
TH
360 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
361 die();
db5af934 362
cc359566 363 } else if (empty($confirmplugins)) {
00be1916 364 $strplugincheck = get_string('plugincheck');
cc359566 365
69d77c23 366 $PAGE->navbar->add($strplugincheck);
367 $PAGE->set_title($strplugincheck);
52273536 368 $PAGE->set_heading($strplugincheck);
69d77c23 369 $PAGE->set_cacheable(false);
b9934a17 370
2f29cf6e
DM
371 $pluginman = core_plugin_manager::instance();
372
c20e9ae8
DM
373 // Check for available updates.
374 if ($fetchupdates) {
375 // No sesskey support guaranteed here, because sessions might not work yet.
376 $updateschecker = \core\update\checker::instance();
377 if ($updateschecker->enabled()) {
378 $updateschecker->fetch();
379 }
380 redirect($PAGE->url);
381 }
382
4d7528f9 383 // Cancel all plugin installations.
2f29cf6e 384 if ($abortinstallx) {
531381f9 385 // No sesskey support guaranteed here, because sessions might not work yet.
c20e9ae8
DM
386 $abortables = $pluginman->list_cancellable_installations();
387 if ($abortables) {
388 if ($confirmabortinstall) {
389 foreach ($abortables as $plugin) {
390 $pluginman->cancel_plugin_installation($plugin->component);
391 }
392 redirect($PAGE->url);
393 } else {
394 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
395 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
396 die();
397 }
4d7528f9 398 }
c20e9ae8 399 redirect($PAGE->url);
2f29cf6e
DM
400 }
401
4d7528f9 402 // Cancel single plugin installation.
2f29cf6e 403 if ($abortinstall) {
531381f9 404 // No sesskey support guaranteed here, because sessions might not work yet.
4d7528f9
DM
405 if ($confirmabortinstall) {
406 $pluginman->cancel_plugin_installation($abortinstall);
407 redirect($PAGE->url);
408 } else {
409 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
c20e9ae8
DM
410 $abortable = $pluginman->get_plugin_info($abortinstall);
411 if ($pluginman->can_cancel_plugin_installation($abortable)) {
412 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
413 die();
414 }
415 redirect($PAGE->url);
4d7528f9 416 }
2f29cf6e
DM
417 }
418
c20e9ae8
DM
419 // Cancel all plugins upgrades (that is, restore archived versions).
420 if ($abortupgradex) {
421 // No sesskey support guaranteed here, because sessions might not work yet.
422 $restorable = $pluginman->list_restorable_archives();
423 if ($restorable) {
424 upgrade_install_plugins($restorable, $confirmabortupgrade,
425 get_string('cancelupgradehead', 'core_plugin'),
426 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
427 );
428 }
429 redirect($PAGE->url);
430 }
431
432 // Cancel single plugin upgrade (that is, install the archived version).
433 if ($abortupgrade) {
434 // No sesskey support guaranteed here, because sessions might not work yet.
435 $restorable = $pluginman->list_restorable_archives();
436 if (isset($restorable[$abortupgrade])) {
437 $restorable = array($restorable[$abortupgrade]);
438 upgrade_install_plugins($restorable, $confirmabortupgrade,
439 get_string('cancelupgradehead', 'core_plugin'),
440 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
441 );
442 }
443 redirect($PAGE->url);
444 }
445
531381f9
DM
446 // Install all available missing dependencies.
447 if ($installdepx) {
448 // No sesskey support guaranteed here, because sessions might not work yet.
449 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
2d00be61 450 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
451 get_string('dependencyinstallhead', 'core_plugin'),
452 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
453 );
454 }
455
456 // Install single available missing dependency.
457 if ($installdep) {
458 // No sesskey support guaranteed here, because sessions might not work yet.
459 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
460 if (!empty($installable[$installdep])) {
461 $installable = array($installable[$installdep]);
2d00be61 462 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
463 get_string('dependencyinstallhead', 'core_plugin'),
464 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
465 );
466 }
467 }
468
4d7528f9 469 // Install all available updates.
531381f9
DM
470 if ($installupdatex) {
471 // No sesskey support guaranteed here, because sessions might not work yet.
472 $installable = $pluginman->filter_installable($pluginman->available_updates());
2d00be61 473 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
474 get_string('updateavailableinstallallhead', 'core_admin'),
475 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
476 );
477 }
478
479 // Install single available update.
480 if ($installupdate and $installupdateversion) {
481 // No sesskey support guaranteed here, because sessions might not work yet.
482 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
483 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
2d00be61 484 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
485 get_string('updateavailableinstallallhead', 'core_admin'),
486 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
487 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
488 )
489 );
490 }
491 }
ead8ba3b 492
e87214bd 493 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
2f29cf6e 494 $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
00be1916 495 die();
db5af934 496
00be1916 497 } else {
fc281113
PS
498 // Always verify plugin dependencies!
499 $failed = array();
500 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
2f29cf6e 501 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
fc281113
PS
502 die();
503 }
504 unset($failed);
505
506 // Launch main upgrade.
00be1916 507 upgrade_core($version, true);
88582df4 508 }
00be1916 509} else if ($version < $CFG->version) {
80380bd7
PS
510 // better stop here, we can not continue with plugin upgrades or anything else
511 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
00be1916 512}
513
514// Updated human-readable release version if necessary
82b1cf00 515if (!$cache and $release <> $CFG->release) { // Update the release version
bc76b3c0 516 set_config('release', $release);
00be1916 517}
518
82b1cf00 519if (!$cache and $branch <> $CFG->branch) { // Update the branch
ed01233a
AB
520 set_config('branch', $branch);
521}
522
82b1cf00 523if (!$cache and moodle_needs_upgrading()) {
98b32c9e 524
2f29cf6e
DM
525 $PAGE->set_url(new moodle_url($PAGE->url, array('confirmplugincheck' => $confirmplugins)));
526
98b32c9e
DM
527 check_upgrade_key($upgradekeyhash);
528
575f245f 529 if (!$PAGE->headerprinted) {
2361b7dd 530 // means core upgrade or installation was not already done
4d7528f9 531
531381f9 532 $pluginman = core_plugin_manager::instance();
4d7528f9
DM
533 $output = $PAGE->get_renderer('core', 'admin');
534
575f245f 535 if (!$confirmplugins) {
cc359566
TH
536 $strplugincheck = get_string('plugincheck');
537
575f245f
PS
538 $PAGE->navbar->add($strplugincheck);
539 $PAGE->set_title($strplugincheck);
540 $PAGE->set_heading($strplugincheck);
541 $PAGE->set_cacheable(false);
b9934a17 542
c20e9ae8
DM
543 // Check for available updates.
544 if ($fetchupdates) {
545 require_sesskey();
546 $updateschecker = \core\update\checker::instance();
547 if ($updateschecker->enabled()) {
548 $updateschecker->fetch();
549 }
550 redirect($PAGE->url);
551 }
552
4d7528f9 553 // Cancel all plugin installations.
2f29cf6e
DM
554 if ($abortinstallx) {
555 require_sesskey();
c20e9ae8
DM
556 $abortables = $pluginman->list_cancellable_installations();
557 if ($abortables) {
558 if ($confirmabortinstall) {
559 foreach ($abortables as $plugin) {
560 $pluginman->cancel_plugin_installation($plugin->component);
561 }
562 redirect($PAGE->url);
563 } else {
30c26421
DM
564 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
565 'confirmabortinstall' => 1));
c20e9ae8
DM
566 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
567 die();
568 }
4d7528f9 569 }
c20e9ae8 570 redirect($PAGE->url);
2f29cf6e
DM
571 }
572
4d7528f9 573 // Cancel single plugin installation.
2f29cf6e
DM
574 if ($abortinstall) {
575 require_sesskey();
4d7528f9
DM
576 if ($confirmabortinstall) {
577 $pluginman->cancel_plugin_installation($abortinstall);
578 redirect($PAGE->url);
579 } else {
580 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
c20e9ae8
DM
581 $abortable = $pluginman->get_plugin_info($abortinstall);
582 if ($pluginman->can_cancel_plugin_installation($abortable)) {
583 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
584 die();
585 }
586 redirect($PAGE->url);
4d7528f9 587 }
2f29cf6e
DM
588 }
589
c20e9ae8
DM
590 // Cancel all plugins upgrades (that is, restore archived versions).
591 if ($abortupgradex) {
fc281113 592 require_sesskey();
c20e9ae8
DM
593 $restorable = $pluginman->list_restorable_archives();
594 if ($restorable) {
595 upgrade_install_plugins($restorable, $confirmabortupgrade,
596 get_string('cancelupgradehead', 'core_plugin'),
597 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
598 );
599 }
600 redirect($PAGE->url);
601 }
602
603 // Cancel single plugin upgrade (that is, install the archived version).
604 if ($abortupgrade) {
605 require_sesskey();
606 $restorable = $pluginman->list_restorable_archives();
607 if (isset($restorable[$abortupgrade])) {
608 $restorable = array($restorable[$abortupgrade]);
609 upgrade_install_plugins($restorable, $confirmabortupgrade,
610 get_string('cancelupgradehead', 'core_plugin'),
611 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
612 );
fc281113 613 }
96dd9237
DM
614 redirect($PAGE->url);
615 }
616
531381f9
DM
617 // Install all available missing dependencies.
618 if ($installdepx) {
619 require_sesskey();
620 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
2d00be61 621 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
622 get_string('dependencyinstallhead', 'core_plugin'),
623 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
624 );
625 }
626
627 // Install single available missing dependency.
628 if ($installdep) {
629 require_sesskey();
630 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
631 if (!empty($installable[$installdep])) {
632 $installable = array($installable[$installdep]);
2d00be61 633 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
634 get_string('dependencyinstallhead', 'core_plugin'),
635 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
636 );
637 }
638 }
12890c57 639
531381f9
DM
640 // Install all available updates.
641 if ($installupdatex) {
642 require_sesskey();
643 $installable = $pluginman->filter_installable($pluginman->available_updates());
2d00be61 644 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
645 get_string('updateavailableinstallallhead', 'core_admin'),
646 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
647 );
648 }
292dbeac 649
531381f9
DM
650 // Install single available update.
651 if ($installupdate and $installupdateversion) {
652 require_sesskey();
653 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
654 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
2d00be61 655 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
656 get_string('updateavailableinstallallhead', 'core_admin'),
657 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
658 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
659 )
660 );
292dbeac
DM
661 }
662 }
663
fc281113 664 // Show plugins info.
2f29cf6e 665 echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
96dd9237
DM
666 $version, $showallplugins,
667 new moodle_url($PAGE->url),
98b32c9e 668 new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
fc281113
PS
669 die();
670 }
671
672 // Make sure plugin dependencies are always checked.
673 $failed = array();
2f29cf6e 674 if (!$pluginman->all_plugins_ok($version, $failed)) {
12890c57 675 $output = $PAGE->get_renderer('core', 'admin');
2f29cf6e 676 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
575f245f
PS
677 die();
678 }
fc281113 679 unset($failed);
575f245f 680 }
fc281113 681
575f245f
PS
682 // install/upgrade all plugins and other parts
683 upgrade_noncore(true);
684}
00be1916 685
31a99877 686// If this is the first install, indicate that this site is fully configured
687// except the admin password
688if (during_initial_install()) {
689 set_config('rolesactive', 1); // after this, during_initial_install will return false.
00be1916 690 set_config('adminsetuppending', 1);
c8ae55e9 691 // we need this redirect to setup proper session
00be1916 692 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
693}
694
695// make sure admin user is created - this is the last step because we need
696// session to be working properly in order to edit admin account
697 if (!empty($CFG->adminsetuppending)) {
698 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
699 if (!$sessionstarted) {
700 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
701 } else {
702 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
703 if (!$sessionverify) {
704 $SESSION->sessionverify = 1;
705 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
88582df4 706 } else {
00be1916 707 if (empty($SESSION->sessionverify)) {
708 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
88582df4 709 }
00be1916 710 unset($SESSION->sessionverify);
88582df4 711 }
00be1916 712 }
88582df4 713
4f73591a
PS
714 // Cleanup SESSION to make sure other code does not complain in the future.
715 unset($SESSION->has_timed_out);
716 unset($SESSION->wantsurl);
717
a7c9609b 718 // 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
719 $adminids = explode(',', $CFG->siteadmins);
720 $adminuser = get_complete_user_data('id', reset($adminids));
88582df4 721
00be1916 722 if ($adminuser->password === 'adminsetuppending') {
723 // prevent installation hijacking
724 if ($adminuser->lastip !== getremoteaddr()) {
725 print_error('installhijacked', 'admin');
35d6a2a4 726 }
00be1916 727 // login user and let him set password and admin details
728 $adminuser->newadminuser = 1;
0342fc36 729 complete_user_login($adminuser);
00be1916 730 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
db5af934 731
5c144d60 732 } else {
00be1916 733 unset_config('adminsetuppending');
4da1a0a1 734 }
735
00be1916 736} else {
737 // just make sure upgrade logging is properly terminated
738 upgrade_finished('upgradesettings.php');
739}
b3732604 740
fc281113
PS
741if (has_capability('moodle/site:config', context_system::instance())) {
742 if ($fetchupdates) {
743 require_sesskey();
744 $updateschecker = \core\update\checker::instance();
745 if ($updateschecker->enabled()) {
746 $updateschecker->fetch();
747 }
748 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
749 }
750}
751
e2e35e71
PS
752// Now we can be sure everything was upgraded and caches work fine,
753// redirect if necessary to make sure caching is enabled.
82b1cf00
PS
754if (!$cache) {
755 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
e2e35e71
PS
756}
757
00be1916 758// Check for valid admin user - no guest autologin
759require_login(0, false);
fc1d0e82
760if (isguestuser()) {
761 // Login as real user!
762 $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
763 redirect(get_login_url());
764}
bf006d2c 765$context = context_system::instance();
336a8433
MG
766
767if (!has_capability('moodle/site:config', $context)) {
768 // Do not throw exception display an empty page with administration menu if visible for current user.
769 $PAGE->set_title($SITE->fullname);
770 $PAGE->set_heading($SITE->fullname);
771 echo $OUTPUT->header();
772 echo $OUTPUT->footer();
773 exit;
774}
00be1916 775
776// check that site is properly customized
777$site = get_site();
778if (empty($site->shortname)) {
779 // probably new installation - lets return to frontpage after this step
780 // remove settings that we want uninitialised
781 unset_config('registerauth');
d6e7a63d 782 unset_config('timezone'); // Force admin to select timezone!
00be1916 783 redirect('upgradesettings.php?return=site');
784}
785
00be1916 786// setup critical warnings before printing admin tree block
bc76b3c0 787$insecuredataroot = is_dataroot_insecure(true);
00be1916 788$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
789
790$adminroot = admin_get_root();
791
792// Check if there are any new admin settings which have still yet to be set
793if (any_new_admin_settings($adminroot)){
794 redirect('upgradesettings.php');
795}
796
e87214bd
PS
797// Return to original page that started the plugin uninstallation if necessary.
798if (isset($SESSION->pluginuninstallreturn)) {
799 $return = $SESSION->pluginuninstallreturn;
800 unset($SESSION->pluginuninstallreturn);
801 if ($return) {
802 redirect($return);
803 }
804}
805
00be1916 806// Everything should now be set up, and the user is an admin
807
808// Print default admin page with notifications.
cc359566 809$errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
a95682b2 810
d5fcf4b4
DP
811// We make the assumption that at least one schedule task should run once per day.
812$lastcron = $DB->get_field_sql('SELECT MAX(lastruntime) FROM {task_scheduled}');
cc359566
TH
813$cronoverdue = ($lastcron < time() - 3600 * 24);
814$dbproblems = $DB->diagnose();
815$maintenancemode = !empty($CFG->maintenance_enabled);
74944b73 816
ca6f3a8b 817// Available updates for Moodle core.
e87214bd 818$updateschecker = \core\update\checker::instance();
966bd785 819$availableupdates = array();
ca6f3a8b
FM
820$availableupdatesfetch = null;
821
e9d3c212 822if ($updateschecker->enabled()) {
ca6f3a8b
FM
823 // Only compute the update information when it is going to be displayed to the user.
824 $availableupdates['core'] = $updateschecker->get_update_info('core',
825 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
826
827 // Available updates for contributed plugins
828 $pluginman = core_plugin_manager::instance();
829 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
830 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
30d8bc5f
DM
831 $pluginavailableupdates = $plugininfo->available_updates();
832 if (!empty($pluginavailableupdates)) {
833 foreach ($pluginavailableupdates as $pluginavailableupdate) {
834 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
835 $availableupdates[$plugintype.'_'.$pluginname] = array();
966bd785 836 }
30d8bc5f 837 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
966bd785
DM
838 }
839 }
840 }
841 }
966bd785 842
ca6f3a8b
FM
843 // The timestamp of the most recent check for available updates
844 $availableupdatesfetch = $updateschecker->get_last_timefetched();
845}
55585f3a 846
0aff15c2 847$buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
b3245b75
DP
848//check if the site is registered on Moodle.org
849$registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
915140c9
SH
850// Check if there are any cache warnings.
851$cachewarnings = cache_helper::warnings();
356e2354
DM
852// Check if there are events 1 API handlers.
853$eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
0aff15c2 854
cc359566 855admin_externalpage_setup('adminnotifications');
55585f3a 856
cc359566 857$output = $PAGE->get_renderer('core', 'admin');
915140c9
SH
858
859echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
860 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
356e2354 861 $registered, $cachewarnings, $eventshandlers);