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