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