MDL-49329 admin: Fix checking for available updates
[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?
aa1d100c 106$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
531381f9
DM
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.
c20e9ae8
DM
115$abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
116$abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
117$confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
531381f9
DM
118$installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
119$installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
120$installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
121$confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
00be1916 122
b0fc7898
DM
123if (!empty($CFG->disableupdateautodeploy)) {
124 // Invalidate all requests to install plugins via the admin UI.
125 $newaddonreq = null;
126 $installdep = null;
127 $installdepx = false;
128 $abortupgrade = null;
129 $abortupgradex = null;
130 $installupdate = null;
131 $installupdateversion = null;
132 $installupdatex = false;
133}
134
e2e35e71
PS
135// Set up PAGE.
136$url = new moodle_url('/admin/index.php');
fc281113 137$url->param('cache', $cache);
98b32c9e
DM
138if (isset($upgradekeyhash)) {
139 $url->param('upgradekeyhash', $upgradekeyhash);
140}
e2e35e71
PS
141$PAGE->set_url($url);
142unset($url);
143
82b1cf00 144// Are we returning from an add-on installation request at moodle.org/plugins?
b0fc7898 145if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
82b1cf00
PS
146 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
147 'installaddonrequest' => $newaddonreq,
148 'confirm' => 0));
149 if (!isloggedin() or isguestuser()) {
150 // Login and go the the add-on tool page.
151 $SESSION->wantsurl = $target->out();
152 redirect(get_login_url());
153 }
154 redirect($target);
155}
156
6d80fad4 157$PAGE->set_pagelayout('admin'); // Set a default pagelayout
ee73b1ff 158
00be1916 159$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
160
e2e35e71
PS
161// Check some PHP server settings
162
00be1916 163if (ini_get_bool('session.auto_start')) {
164 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
165}
166
00be1916 167if (!ini_get_bool('file_uploads')) {
168 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
169}
170
171if (is_float_problem()) {
172 print_error('phpfloatproblem', 'admin', '', $documentationlink);
173}
174
00be1916 175// Set some necessary variables during set-up to avoid PHP warnings later on this page
00be1916 176if (!isset($CFG->release)) {
bc76b3c0 177 $CFG->release = '';
00be1916 178}
179if (!isset($CFG->version)) {
bc76b3c0 180 $CFG->version = '';
00be1916 181}
ad394588
AB
182if (!isset($CFG->branch)) {
183 $CFG->branch = '';
184}
00be1916 185
186$version = null;
187$release = null;
ed01233a
AB
188$branch = null;
189require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
00be1916 190$CFG->target_release = $release; // used during installation and upgrades
191
192if (!$version or !$release) {
193 print_error('withoutversion', 'debug'); // without version, stop
194}
195
3316fe24 196if (!core_tables_exist()) {
78946b9b 197 $PAGE->set_pagelayout('maintenance');
d3875524 198 $PAGE->set_popup_notification_allowed(false);
00be1916 199
200 // fake some settings
201 $CFG->docroot = 'http://docs.moodle.org';
202
203 $strinstallation = get_string('installation', 'install');
204
205 // remove current session content completely
d79d5ac2 206 \core\session\manager::terminate_current();
00be1916 207
208 if (empty($agreelicense)) {
209 $strlicense = get_string('license');
cc359566 210
69d77c23 211 $PAGE->navbar->add($strlicense);
212 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
213 $PAGE->set_heading($strinstallation);
214 $PAGE->set_cacheable(false);
cc359566 215
82b1cf00 216 /** @var core_admin_renderer $output */
da2fdc3f 217 $output = $PAGE->get_renderer('core', 'admin');
cc359566 218 echo $output->install_licence_page();
3ba28588 219 die();
249ab745 220 }
00be1916 221 if (empty($confirmrelease)) {
cc359566
TH
222 require_once($CFG->libdir.'/environmentlib.php');
223 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
bc76b3c0 224 $strcurrentrelease = get_string('currentrelease');
cc359566 225
69d77c23 226 $PAGE->navbar->add($strcurrentrelease);
1a4d6a5b
DM
227 $PAGE->set_title($strinstallation);
228 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
69d77c23 229 $PAGE->set_cacheable(false);
249ab745 230
82b1cf00 231 /** @var core_admin_renderer $output */
cc359566 232 $output = $PAGE->get_renderer('core', 'admin');
8d1da748 233 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
3ba28588 234 die();
1dd24519 235 }
236
39f15cc7
DM
237 // check plugin dependencies
238 $failed = array();
e87214bd 239 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
39f15cc7
DM
240 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
241 $PAGE->set_title($strinstallation);
242 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
243
244 $output = $PAGE->get_renderer('core', 'admin');
98b32c9e 245 $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
39f15cc7
DM
246 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
247 die();
248 }
249 unset($failed);
250
8d1da748
PS
251 //TODO: add a page with list of non-standard plugins here
252
bc76b3c0 253 $strdatabasesetup = get_string('databasesetup');
543f54d3 254 upgrade_init_javascript();
cc359566 255
69d77c23 256 $PAGE->navbar->add($strdatabasesetup);
257 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
2ea05146 258 $PAGE->set_heading($strinstallation);
69d77c23 259 $PAGE->set_cacheable(false);
cc359566 260
da2fdc3f
TH
261 $output = $PAGE->get_renderer('core', 'admin');
262 echo $output->header();
d02bc6ce 263
00be1916 264 if (!$DB->setup_is_unicodedb()) {
265 if (!$DB->change_db_encoding()) {
266 // If could not convert successfully, throw error, and prevent installation
267 print_error('unicoderequired', 'admin');
db5af934 268 }
db5af934 269 }
90509582 270
00be1916 271 install_core($version, true);
272}
db5af934 273
db5af934 274
00be1916 275// Check version of Moodle code on disk compared with database
276// and upgrade if possible.
db5af934 277
1a361486
PS
278if (!$cache) {
279 // Do not try to do anything fancy in non-cached mode,
280 // this prevents themes from fetching data from non-existent tables.
281 $PAGE->set_pagelayout('maintenance');
282 $PAGE->set_popup_notification_allowed(false);
283}
284
00be1916 285$stradministration = get_string('administration');
bf006d2c 286$PAGE->set_context(context_system::instance());
db5af934 287
00be1916 288if (empty($CFG->version)) {
289 print_error('missingconfigversion', 'debug');
290}
db5af934 291
e2e35e71 292// Detect config cache inconsistency, this happens when you switch branches on dev servers.
82b1cf00
PS
293if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
294 purge_all_caches();
98b32c9e 295 redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
e2e35e71
PS
296}
297
82b1cf00 298if (!$cache and $version > $CFG->version) { // upgrade
6e09cf98 299
2f29cf6e
DM
300 $PAGE->set_url(new moodle_url($PAGE->url, array(
301 'confirmupgrade' => $confirmupgrade,
302 'confirmrelease' => $confirmrelease,
303 'confirmplugincheck' => $confirmplugins,
304 )));
305
98b32c9e
DM
306 check_upgrade_key($upgradekeyhash);
307
6e09cf98
DM
308 // Warning about upgrading a test site.
309 $testsite = false;
310 if (defined('BEHAT_SITE_RUNNING')) {
311 $testsite = 'behat';
312 }
313
b0dd08dd
SH
314 // We purge all of MUC's caches here.
315 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
316 // This ensures a real config object is loaded and the stores will be purged.
317 // This is the only way we can purge custom caches such as memcache or APC.
318 // Note: all other calls to caches will still used the disabled API.
319 cache_helper::purge_all(true);
320 // We then purge the regular caches.
a95682b2 321 purge_all_caches();
33082590 322
fc281113
PS
323 /** @var core_admin_renderer $output */
324 $output = $PAGE->get_renderer('core', 'admin');
325
9008ec16
PS
326 if (upgrade_stale_php_files_present()) {
327 $PAGE->set_title($stradministration);
328 $PAGE->set_cacheable(false);
329
9008ec16
PS
330 echo $output->upgrade_stale_php_files_page();
331 die();
332 }
333
00be1916 334 if (empty($confirmupgrade)) {
17854cb9 335 $a = new stdClass();
c22a579b
RT
336 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
337 $a->newversion = "$release (".sprintf('%.2f', $version).")";
cc359566
TH
338 $strdatabasechecking = get_string('databasechecking', '', $a);
339
1a4d6a5b
DM
340 $PAGE->set_title($stradministration);
341 $PAGE->set_heading($strdatabasechecking);
69d77c23 342 $PAGE->set_cacheable(false);
cc359566 343
6e09cf98 344 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
3ba28588 345 die();
db5af934 346
00be1916 347 } else if (empty($confirmrelease)){
cc359566
TH
348 require_once($CFG->libdir.'/environmentlib.php');
349 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
bc76b3c0 350 $strcurrentrelease = get_string('currentrelease');
cc359566 351
69d77c23 352 $PAGE->navbar->add($strcurrentrelease);
353 $PAGE->set_title($strcurrentrelease);
52273536 354 $PAGE->set_heading($strcurrentrelease);
69d77c23 355 $PAGE->set_cacheable(false);
db5af934 356
faadd326
TH
357 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
358 die();
db5af934 359
cc359566 360 } else if (empty($confirmplugins)) {
00be1916 361 $strplugincheck = get_string('plugincheck');
cc359566 362
69d77c23 363 $PAGE->navbar->add($strplugincheck);
364 $PAGE->set_title($strplugincheck);
52273536 365 $PAGE->set_heading($strplugincheck);
69d77c23 366 $PAGE->set_cacheable(false);
b9934a17 367
2f29cf6e
DM
368 $pluginman = core_plugin_manager::instance();
369
c20e9ae8
DM
370 // Check for available updates.
371 if ($fetchupdates) {
372 // No sesskey support guaranteed here, because sessions might not work yet.
373 $updateschecker = \core\update\checker::instance();
374 if ($updateschecker->enabled()) {
375 $updateschecker->fetch();
376 }
377 redirect($PAGE->url);
378 }
379
4d7528f9 380 // Cancel all plugin installations.
2f29cf6e 381 if ($abortinstallx) {
531381f9 382 // No sesskey support guaranteed here, because sessions might not work yet.
c20e9ae8
DM
383 $abortables = $pluginman->list_cancellable_installations();
384 if ($abortables) {
385 if ($confirmabortinstall) {
386 foreach ($abortables as $plugin) {
387 $pluginman->cancel_plugin_installation($plugin->component);
388 }
389 redirect($PAGE->url);
390 } else {
391 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
392 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
393 die();
394 }
4d7528f9 395 }
c20e9ae8 396 redirect($PAGE->url);
2f29cf6e
DM
397 }
398
4d7528f9 399 // Cancel single plugin installation.
2f29cf6e 400 if ($abortinstall) {
531381f9 401 // No sesskey support guaranteed here, because sessions might not work yet.
4d7528f9
DM
402 if ($confirmabortinstall) {
403 $pluginman->cancel_plugin_installation($abortinstall);
404 redirect($PAGE->url);
405 } else {
406 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
c20e9ae8
DM
407 $abortable = $pluginman->get_plugin_info($abortinstall);
408 if ($pluginman->can_cancel_plugin_installation($abortable)) {
409 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
410 die();
411 }
412 redirect($PAGE->url);
4d7528f9 413 }
2f29cf6e
DM
414 }
415
c20e9ae8
DM
416 // Cancel all plugins upgrades (that is, restore archived versions).
417 if ($abortupgradex) {
418 // No sesskey support guaranteed here, because sessions might not work yet.
419 $restorable = $pluginman->list_restorable_archives();
420 if ($restorable) {
421 upgrade_install_plugins($restorable, $confirmabortupgrade,
422 get_string('cancelupgradehead', 'core_plugin'),
423 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
424 );
425 }
426 redirect($PAGE->url);
427 }
428
429 // Cancel single plugin upgrade (that is, install the archived version).
430 if ($abortupgrade) {
431 // No sesskey support guaranteed here, because sessions might not work yet.
432 $restorable = $pluginman->list_restorable_archives();
433 if (isset($restorable[$abortupgrade])) {
434 $restorable = array($restorable[$abortupgrade]);
435 upgrade_install_plugins($restorable, $confirmabortupgrade,
436 get_string('cancelupgradehead', 'core_plugin'),
437 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
438 );
439 }
440 redirect($PAGE->url);
441 }
442
531381f9
DM
443 // Install all available missing dependencies.
444 if ($installdepx) {
445 // No sesskey support guaranteed here, because sessions might not work yet.
446 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
2d00be61 447 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
448 get_string('dependencyinstallhead', 'core_plugin'),
449 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
450 );
451 }
452
453 // Install single available missing dependency.
454 if ($installdep) {
455 // No sesskey support guaranteed here, because sessions might not work yet.
456 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
457 if (!empty($installable[$installdep])) {
458 $installable = array($installable[$installdep]);
2d00be61 459 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
460 get_string('dependencyinstallhead', 'core_plugin'),
461 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
462 );
463 }
464 }
465
4d7528f9 466 // Install all available updates.
531381f9
DM
467 if ($installupdatex) {
468 // No sesskey support guaranteed here, because sessions might not work yet.
469 $installable = $pluginman->filter_installable($pluginman->available_updates());
2d00be61 470 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
471 get_string('updateavailableinstallallhead', 'core_admin'),
472 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
473 );
474 }
475
476 // Install single available update.
477 if ($installupdate and $installupdateversion) {
478 // No sesskey support guaranteed here, because sessions might not work yet.
479 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
480 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
2d00be61 481 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
482 get_string('updateavailableinstallallhead', 'core_admin'),
483 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
484 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
485 )
486 );
487 }
488 }
ead8ba3b 489
e87214bd 490 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
2f29cf6e 491 $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
00be1916 492 die();
db5af934 493
00be1916 494 } else {
fc281113
PS
495 // Always verify plugin dependencies!
496 $failed = array();
497 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
2f29cf6e 498 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
fc281113
PS
499 die();
500 }
501 unset($failed);
502
503 // Launch main upgrade.
00be1916 504 upgrade_core($version, true);
88582df4 505 }
00be1916 506} else if ($version < $CFG->version) {
80380bd7
PS
507 // better stop here, we can not continue with plugin upgrades or anything else
508 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
00be1916 509}
510
511// Updated human-readable release version if necessary
82b1cf00 512if (!$cache and $release <> $CFG->release) { // Update the release version
bc76b3c0 513 set_config('release', $release);
00be1916 514}
515
82b1cf00 516if (!$cache and $branch <> $CFG->branch) { // Update the branch
ed01233a
AB
517 set_config('branch', $branch);
518}
519
82b1cf00 520if (!$cache and moodle_needs_upgrading()) {
98b32c9e 521
2f29cf6e
DM
522 $PAGE->set_url(new moodle_url($PAGE->url, array('confirmplugincheck' => $confirmplugins)));
523
98b32c9e
DM
524 check_upgrade_key($upgradekeyhash);
525
575f245f 526 if (!$PAGE->headerprinted) {
2361b7dd 527 // means core upgrade or installation was not already done
4d7528f9
DM
528
529 /** @var core_plugin_manager $pluginman */
531381f9 530 $pluginman = core_plugin_manager::instance();
fc281113 531
4d7528f9
DM
532 /** @var core_admin_renderer $output */
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 {
564 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
565 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
566 die();
567 }
4d7528f9 568 }
c20e9ae8 569 redirect($PAGE->url);
2f29cf6e
DM
570 }
571
4d7528f9 572 // Cancel single plugin installation.
2f29cf6e
DM
573 if ($abortinstall) {
574 require_sesskey();
4d7528f9
DM
575 if ($confirmabortinstall) {
576 $pluginman->cancel_plugin_installation($abortinstall);
577 redirect($PAGE->url);
578 } else {
579 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
c20e9ae8
DM
580 $abortable = $pluginman->get_plugin_info($abortinstall);
581 if ($pluginman->can_cancel_plugin_installation($abortable)) {
582 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
583 die();
584 }
585 redirect($PAGE->url);
4d7528f9 586 }
2f29cf6e
DM
587 }
588
c20e9ae8
DM
589 // Cancel all plugins upgrades (that is, restore archived versions).
590 if ($abortupgradex) {
fc281113 591 require_sesskey();
c20e9ae8
DM
592 $restorable = $pluginman->list_restorable_archives();
593 if ($restorable) {
594 upgrade_install_plugins($restorable, $confirmabortupgrade,
595 get_string('cancelupgradehead', 'core_plugin'),
596 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
597 );
598 }
599 redirect($PAGE->url);
600 }
601
602 // Cancel single plugin upgrade (that is, install the archived version).
603 if ($abortupgrade) {
604 require_sesskey();
605 $restorable = $pluginman->list_restorable_archives();
606 if (isset($restorable[$abortupgrade])) {
607 $restorable = array($restorable[$abortupgrade]);
608 upgrade_install_plugins($restorable, $confirmabortupgrade,
609 get_string('cancelupgradehead', 'core_plugin'),
610 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
611 );
fc281113 612 }
96dd9237
DM
613 redirect($PAGE->url);
614 }
615
531381f9
DM
616 // Install all available missing dependencies.
617 if ($installdepx) {
618 require_sesskey();
619 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
2d00be61 620 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
621 get_string('dependencyinstallhead', 'core_plugin'),
622 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
623 );
624 }
625
626 // Install single available missing dependency.
627 if ($installdep) {
628 require_sesskey();
629 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
630 if (!empty($installable[$installdep])) {
631 $installable = array($installable[$installdep]);
2d00be61 632 upgrade_install_plugins($installable, $confirminstalldep,
531381f9
DM
633 get_string('dependencyinstallhead', 'core_plugin'),
634 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
635 );
636 }
637 }
12890c57 638
531381f9
DM
639 // Install all available updates.
640 if ($installupdatex) {
641 require_sesskey();
642 $installable = $pluginman->filter_installable($pluginman->available_updates());
2d00be61 643 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
644 get_string('updateavailableinstallallhead', 'core_admin'),
645 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
646 );
647 }
292dbeac 648
531381f9
DM
649 // Install single available update.
650 if ($installupdate and $installupdateversion) {
651 require_sesskey();
652 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
653 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
2d00be61 654 upgrade_install_plugins($installable, $confirminstallupdate,
531381f9
DM
655 get_string('updateavailableinstallallhead', 'core_admin'),
656 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
657 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
658 )
659 );
292dbeac
DM
660 }
661 }
662
fc281113 663 // Show plugins info.
2f29cf6e 664 echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
96dd9237
DM
665 $version, $showallplugins,
666 new moodle_url($PAGE->url),
98b32c9e 667 new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
fc281113
PS
668 die();
669 }
670
671 // Make sure plugin dependencies are always checked.
672 $failed = array();
2f29cf6e 673 if (!$pluginman->all_plugins_ok($version, $failed)) {
12890c57
SH
674 /** @var core_admin_renderer $output */
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();
0aff15c2 852
cc359566 853admin_externalpage_setup('adminnotifications');
55585f3a 854
915140c9 855/* @var core_admin_renderer $output */
cc359566 856$output = $PAGE->get_renderer('core', 'admin');
915140c9
SH
857
858echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
859 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
860 $registered, $cachewarnings);