Merge branch 'MDL-55910-31' of https://github.com/LukeCarrier/moodle into MOODLE_31_S...
[moodle.git] / admin / index.php
blobd9d76b63480e0ffbe459e2845c165c12d3aad530
1 <?php
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.
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/>.
18 /**
19 * Main administration script.
21 * @package core
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // Check that config.php exists, if not then call the install script
27 if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
29 die();
32 // Check that PHP is of a sufficient version as soon as possible
33 if (version_compare(phpversion(), '5.4.4') < 0) {
34 $phpversion = phpversion();
35 // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
36 echo "Moodle 2.7 or later requires at least PHP 5.4.4 (currently using version $phpversion).<br />";
37 echo "Please upgrade your server software or install older Moodle version.";
38 die();
41 // make sure iconv is available and actually works
42 if (!function_exists('iconv')) {
43 // this should not happen, this must be very borked install
44 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
45 die();
48 // Make sure php5-json is available.
49 if (!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();
55 // Make sure xml extension is available.
56 if (!extension_loaded('xml')) {
57 echo 'Moodle requires the xml PHP extension. Please install or enable the xml extension.';
58 die();
61 define('NO_OUTPUT_BUFFERING', true);
63 if (isset($_POST['upgradekey'])) {
64 // Before you start reporting issues about the collision attacks against
65 // SHA-1, you should understand that we are not actually attempting to do
66 // any cryptography here. This is hashed purely so that the key is not
67 // that apparent in the address bar itself. Anyone who catches the HTTP
68 // traffic can immediately use it as a valid admin key.
69 header('Location: index.php?cache=0&upgradekeyhash='.sha1($_POST['upgradekey']));
70 die();
73 if ((isset($_GET['cache']) and $_GET['cache'] === '0')
74 or (isset($_POST['cache']) and $_POST['cache'] === '0')
75 or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
76 // Prevent caching at all cost when visiting this page directly,
77 // we redirect to self once we known no upgrades are necessary.
78 // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
79 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
80 define('CACHE_DISABLE_ALL', true);
82 // Force OPcache reset if used, we do not want any stale caches
83 // when detecting if upgrade necessary or when running upgrade.
84 if (function_exists('opcache_reset')) {
85 opcache_reset();
87 $cache = 0;
89 } else {
90 $cache = 1;
93 require('../config.php');
95 // Invalidate the cache of version.php in any circumstances to help core_component
96 // detecting if the version has changed and component cache should be reset.
97 if (function_exists('opcache_invalidate')) {
98 opcache_invalidate($CFG->dirroot . '/version.php', true);
100 // Make sure the component cache gets rebuilt if necessary, any method that
101 // indirectly calls the protected init() method is good here.
102 core_component::get_core_subsystems();
104 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
105 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
107 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
108 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
109 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
110 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
111 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
112 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
113 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
114 $upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
115 $installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
116 $installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
117 $confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
118 $abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
119 $abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
120 $confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
121 $abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
122 $abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
123 $confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
124 $installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
125 $installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
126 $installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
127 $confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
129 if (!empty($CFG->disableupdateautodeploy)) {
130 // Invalidate all requests to install plugins via the admin UI.
131 $newaddonreq = null;
132 $installdep = null;
133 $installdepx = false;
134 $abortupgrade = null;
135 $abortupgradex = null;
136 $installupdate = null;
137 $installupdateversion = null;
138 $installupdatex = false;
141 // Set up PAGE.
142 $url = new moodle_url('/admin/index.php');
143 $url->param('cache', $cache);
144 if (isset($upgradekeyhash)) {
145 $url->param('upgradekeyhash', $upgradekeyhash);
147 $PAGE->set_url($url);
148 unset($url);
150 // Are we returning from an add-on installation request at moodle.org/plugins?
151 if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
152 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
153 'installaddonrequest' => $newaddonreq,
154 'confirm' => 0));
155 if (!isloggedin() or isguestuser()) {
156 // Login and go the the add-on tool page.
157 $SESSION->wantsurl = $target->out();
158 redirect(get_login_url());
160 redirect($target);
163 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
165 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
167 // Check some PHP server settings
169 if (ini_get_bool('session.auto_start')) {
170 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
173 if (!ini_get_bool('file_uploads')) {
174 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
177 if (is_float_problem()) {
178 print_error('phpfloatproblem', 'admin', '', $documentationlink);
181 // Set some necessary variables during set-up to avoid PHP warnings later on this page
182 if (!isset($CFG->release)) {
183 $CFG->release = '';
185 if (!isset($CFG->version)) {
186 $CFG->version = '';
188 if (!isset($CFG->branch)) {
189 $CFG->branch = '';
192 $version = null;
193 $release = null;
194 $branch = null;
195 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
196 $CFG->target_release = $release; // used during installation and upgrades
198 if (!$version or !$release) {
199 print_error('withoutversion', 'debug'); // without version, stop
202 if (!core_tables_exist()) {
203 $PAGE->set_pagelayout('maintenance');
204 $PAGE->set_popup_notification_allowed(false);
206 // fake some settings
207 $CFG->docroot = 'http://docs.moodle.org';
209 $strinstallation = get_string('installation', 'install');
211 // remove current session content completely
212 \core\session\manager::terminate_current();
214 if (empty($agreelicense)) {
215 $strlicense = get_string('license');
217 $PAGE->navbar->add($strlicense);
218 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
219 $PAGE->set_heading($strinstallation);
220 $PAGE->set_cacheable(false);
222 $output = $PAGE->get_renderer('core', 'admin');
223 echo $output->install_licence_page();
224 die();
226 if (empty($confirmrelease)) {
227 require_once($CFG->libdir.'/environmentlib.php');
228 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
229 $strcurrentrelease = get_string('currentrelease');
231 $PAGE->navbar->add($strcurrentrelease);
232 $PAGE->set_title($strinstallation);
233 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
234 $PAGE->set_cacheable(false);
236 $output = $PAGE->get_renderer('core', 'admin');
237 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
238 die();
241 // check plugin dependencies
242 $failed = array();
243 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
244 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
245 $PAGE->set_title($strinstallation);
246 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
248 $output = $PAGE->get_renderer('core', 'admin');
249 $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
250 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
251 die();
253 unset($failed);
255 //TODO: add a page with list of non-standard plugins here
257 $strdatabasesetup = get_string('databasesetup');
258 upgrade_init_javascript();
260 $PAGE->navbar->add($strdatabasesetup);
261 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
262 $PAGE->set_heading($strinstallation);
263 $PAGE->set_cacheable(false);
265 $output = $PAGE->get_renderer('core', 'admin');
266 echo $output->header();
268 if (!$DB->setup_is_unicodedb()) {
269 if (!$DB->change_db_encoding()) {
270 // If could not convert successfully, throw error, and prevent installation
271 print_error('unicoderequired', 'admin');
275 install_core($version, true);
279 // Check version of Moodle code on disk compared with database
280 // and upgrade if possible.
282 if (!$cache) {
283 // Do not try to do anything fancy in non-cached mode,
284 // this prevents themes from fetching data from non-existent tables.
285 $PAGE->set_pagelayout('maintenance');
286 $PAGE->set_popup_notification_allowed(false);
289 $stradministration = get_string('administration');
290 $PAGE->set_context(context_system::instance());
292 if (empty($CFG->version)) {
293 print_error('missingconfigversion', 'debug');
296 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
297 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
298 purge_all_caches();
299 redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
302 if (!$cache and $version > $CFG->version) { // upgrade
304 $PAGE->set_url(new moodle_url($PAGE->url, array(
305 'confirmupgrade' => $confirmupgrade,
306 'confirmrelease' => $confirmrelease,
307 'confirmplugincheck' => $confirmplugins,
308 )));
310 check_upgrade_key($upgradekeyhash);
312 // Warning about upgrading a test site.
313 $testsite = false;
314 if (defined('BEHAT_SITE_RUNNING')) {
315 $testsite = 'behat';
318 // We purge all of MUC's caches here.
319 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
320 // This ensures a real config object is loaded and the stores will be purged.
321 // This is the only way we can purge custom caches such as memcache or APC.
322 // Note: all other calls to caches will still used the disabled API.
323 cache_helper::purge_all(true);
324 // We then purge the regular caches.
325 purge_all_caches();
327 $output = $PAGE->get_renderer('core', 'admin');
329 if (upgrade_stale_php_files_present()) {
330 $PAGE->set_title($stradministration);
331 $PAGE->set_cacheable(false);
333 echo $output->upgrade_stale_php_files_page();
334 die();
337 if (empty($confirmupgrade)) {
338 $a = new stdClass();
339 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
340 $a->newversion = "$release (".sprintf('%.2f', $version).")";
341 $strdatabasechecking = get_string('databasechecking', '', $a);
343 $PAGE->set_title($stradministration);
344 $PAGE->set_heading($strdatabasechecking);
345 $PAGE->set_cacheable(false);
347 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
348 die();
350 } else if (empty($confirmrelease)){
351 require_once($CFG->libdir.'/environmentlib.php');
352 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
353 $strcurrentrelease = get_string('currentrelease');
355 $PAGE->navbar->add($strcurrentrelease);
356 $PAGE->set_title($strcurrentrelease);
357 $PAGE->set_heading($strcurrentrelease);
358 $PAGE->set_cacheable(false);
360 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
361 die();
363 } else if (empty($confirmplugins)) {
364 $strplugincheck = get_string('plugincheck');
366 $PAGE->navbar->add($strplugincheck);
367 $PAGE->set_title($strplugincheck);
368 $PAGE->set_heading($strplugincheck);
369 $PAGE->set_cacheable(false);
371 $pluginman = core_plugin_manager::instance();
373 // Check for available updates.
374 if ($fetchupdates) {
375 // No sesskey support guaranteed here, because sessions might not work yet.
376 $updateschecker = \core\update\checker::instance();
377 if ($updateschecker->enabled()) {
378 $updateschecker->fetch();
380 redirect($PAGE->url);
383 // Cancel all plugin installations.
384 if ($abortinstallx) {
385 // No sesskey support guaranteed here, because sessions might not work yet.
386 $abortables = $pluginman->list_cancellable_installations();
387 if ($abortables) {
388 if ($confirmabortinstall) {
389 foreach ($abortables as $plugin) {
390 $pluginman->cancel_plugin_installation($plugin->component);
392 redirect($PAGE->url);
393 } else {
394 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
395 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
396 die();
399 redirect($PAGE->url);
402 // Cancel single plugin installation.
403 if ($abortinstall) {
404 // No sesskey support guaranteed here, because sessions might not work yet.
405 if ($confirmabortinstall) {
406 $pluginman->cancel_plugin_installation($abortinstall);
407 redirect($PAGE->url);
408 } else {
409 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
410 $abortable = $pluginman->get_plugin_info($abortinstall);
411 if ($pluginman->can_cancel_plugin_installation($abortable)) {
412 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
413 die();
415 redirect($PAGE->url);
419 // Cancel all plugins upgrades (that is, restore archived versions).
420 if ($abortupgradex) {
421 // No sesskey support guaranteed here, because sessions might not work yet.
422 $restorable = $pluginman->list_restorable_archives();
423 if ($restorable) {
424 upgrade_install_plugins($restorable, $confirmabortupgrade,
425 get_string('cancelupgradehead', 'core_plugin'),
426 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
429 redirect($PAGE->url);
432 // Cancel single plugin upgrade (that is, install the archived version).
433 if ($abortupgrade) {
434 // No sesskey support guaranteed here, because sessions might not work yet.
435 $restorable = $pluginman->list_restorable_archives();
436 if (isset($restorable[$abortupgrade])) {
437 $restorable = array($restorable[$abortupgrade]);
438 upgrade_install_plugins($restorable, $confirmabortupgrade,
439 get_string('cancelupgradehead', 'core_plugin'),
440 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
443 redirect($PAGE->url);
446 // Install all available missing dependencies.
447 if ($installdepx) {
448 // No sesskey support guaranteed here, because sessions might not work yet.
449 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
450 upgrade_install_plugins($installable, $confirminstalldep,
451 get_string('dependencyinstallhead', 'core_plugin'),
452 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
456 // Install single available missing dependency.
457 if ($installdep) {
458 // No sesskey support guaranteed here, because sessions might not work yet.
459 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
460 if (!empty($installable[$installdep])) {
461 $installable = array($installable[$installdep]);
462 upgrade_install_plugins($installable, $confirminstalldep,
463 get_string('dependencyinstallhead', 'core_plugin'),
464 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
469 // Install all available updates.
470 if ($installupdatex) {
471 // No sesskey support guaranteed here, because sessions might not work yet.
472 $installable = $pluginman->filter_installable($pluginman->available_updates());
473 upgrade_install_plugins($installable, $confirminstallupdate,
474 get_string('updateavailableinstallallhead', 'core_admin'),
475 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
479 // Install single available update.
480 if ($installupdate and $installupdateversion) {
481 // No sesskey support guaranteed here, because sessions might not work yet.
482 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
483 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
484 upgrade_install_plugins($installable, $confirminstallupdate,
485 get_string('updateavailableinstallallhead', 'core_admin'),
486 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
487 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
493 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
494 $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
495 die();
497 } else {
498 // Always verify plugin dependencies!
499 $failed = array();
500 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
501 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
502 die();
504 unset($failed);
506 // Launch main upgrade.
507 upgrade_core($version, true);
509 } else if ($version < $CFG->version) {
510 // better stop here, we can not continue with plugin upgrades or anything else
511 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
514 // Updated human-readable release version if necessary
515 if (!$cache and $release <> $CFG->release) { // Update the release version
516 set_config('release', $release);
519 if (!$cache and $branch <> $CFG->branch) { // Update the branch
520 set_config('branch', $branch);
523 if (!$cache and moodle_needs_upgrading()) {
525 $PAGE->set_url(new moodle_url($PAGE->url, array('confirmplugincheck' => $confirmplugins)));
527 check_upgrade_key($upgradekeyhash);
529 if (!$PAGE->headerprinted) {
530 // means core upgrade or installation was not already done
532 $pluginman = core_plugin_manager::instance();
533 $output = $PAGE->get_renderer('core', 'admin');
535 if (!$confirmplugins) {
536 $strplugincheck = get_string('plugincheck');
538 $PAGE->navbar->add($strplugincheck);
539 $PAGE->set_title($strplugincheck);
540 $PAGE->set_heading($strplugincheck);
541 $PAGE->set_cacheable(false);
543 // Check for available updates.
544 if ($fetchupdates) {
545 require_sesskey();
546 $updateschecker = \core\update\checker::instance();
547 if ($updateschecker->enabled()) {
548 $updateschecker->fetch();
550 redirect($PAGE->url);
553 // Cancel all plugin installations.
554 if ($abortinstallx) {
555 require_sesskey();
556 $abortables = $pluginman->list_cancellable_installations();
557 if ($abortables) {
558 if ($confirmabortinstall) {
559 foreach ($abortables as $plugin) {
560 $pluginman->cancel_plugin_installation($plugin->component);
562 redirect($PAGE->url);
563 } else {
564 $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
565 'confirmabortinstall' => 1));
566 echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
567 die();
570 redirect($PAGE->url);
573 // Cancel single plugin installation.
574 if ($abortinstall) {
575 require_sesskey();
576 if ($confirmabortinstall) {
577 $pluginman->cancel_plugin_installation($abortinstall);
578 redirect($PAGE->url);
579 } else {
580 $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
581 $abortable = $pluginman->get_plugin_info($abortinstall);
582 if ($pluginman->can_cancel_plugin_installation($abortable)) {
583 echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
584 die();
586 redirect($PAGE->url);
590 // Cancel all plugins upgrades (that is, restore archived versions).
591 if ($abortupgradex) {
592 require_sesskey();
593 $restorable = $pluginman->list_restorable_archives();
594 if ($restorable) {
595 upgrade_install_plugins($restorable, $confirmabortupgrade,
596 get_string('cancelupgradehead', 'core_plugin'),
597 new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
600 redirect($PAGE->url);
603 // Cancel single plugin upgrade (that is, install the archived version).
604 if ($abortupgrade) {
605 require_sesskey();
606 $restorable = $pluginman->list_restorable_archives();
607 if (isset($restorable[$abortupgrade])) {
608 $restorable = array($restorable[$abortupgrade]);
609 upgrade_install_plugins($restorable, $confirmabortupgrade,
610 get_string('cancelupgradehead', 'core_plugin'),
611 new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
614 redirect($PAGE->url);
617 // Install all available missing dependencies.
618 if ($installdepx) {
619 require_sesskey();
620 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
621 upgrade_install_plugins($installable, $confirminstalldep,
622 get_string('dependencyinstallhead', 'core_plugin'),
623 new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
627 // Install single available missing dependency.
628 if ($installdep) {
629 require_sesskey();
630 $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
631 if (!empty($installable[$installdep])) {
632 $installable = array($installable[$installdep]);
633 upgrade_install_plugins($installable, $confirminstalldep,
634 get_string('dependencyinstallhead', 'core_plugin'),
635 new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
640 // Install all available updates.
641 if ($installupdatex) {
642 require_sesskey();
643 $installable = $pluginman->filter_installable($pluginman->available_updates());
644 upgrade_install_plugins($installable, $confirminstallupdate,
645 get_string('updateavailableinstallallhead', 'core_admin'),
646 new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
650 // Install single available update.
651 if ($installupdate and $installupdateversion) {
652 require_sesskey();
653 if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
654 $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
655 upgrade_install_plugins($installable, $confirminstallupdate,
656 get_string('updateavailableinstallallhead', 'core_admin'),
657 new moodle_url($PAGE->url, array('installupdate' => $installupdate,
658 'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
664 // Show plugins info.
665 echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
666 $version, $showallplugins,
667 new moodle_url($PAGE->url),
668 new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
669 die();
672 // Make sure plugin dependencies are always checked.
673 $failed = array();
674 if (!$pluginman->all_plugins_ok($version, $failed)) {
675 $output = $PAGE->get_renderer('core', 'admin');
676 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
677 die();
679 unset($failed);
682 // install/upgrade all plugins and other parts
683 upgrade_noncore(true);
686 // If this is the first install, indicate that this site is fully configured
687 // except the admin password
688 if (during_initial_install()) {
689 set_config('rolesactive', 1); // after this, during_initial_install will return false.
690 set_config('adminsetuppending', 1);
691 // we need this redirect to setup proper session
692 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
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");
706 } else {
707 if (empty($SESSION->sessionverify)) {
708 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
710 unset($SESSION->sessionverify);
714 // Cleanup SESSION to make sure other code does not complain in the future.
715 unset($SESSION->has_timed_out);
716 unset($SESSION->wantsurl);
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
719 $adminids = explode(',', $CFG->siteadmins);
720 $adminuser = get_complete_user_data('id', reset($adminids));
722 if ($adminuser->password === 'adminsetuppending') {
723 // prevent installation hijacking
724 if ($adminuser->lastip !== getremoteaddr()) {
725 print_error('installhijacked', 'admin');
727 // login user and let him set password and admin details
728 $adminuser->newadminuser = 1;
729 complete_user_login($adminuser);
730 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
732 } else {
733 unset_config('adminsetuppending');
736 } else {
737 // just make sure upgrade logging is properly terminated
738 upgrade_finished('upgradesettings.php');
741 if (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();
748 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
752 // Now we can be sure everything was upgraded and caches work fine,
753 // redirect if necessary to make sure caching is enabled.
754 if (!$cache) {
755 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
758 // Check for valid admin user - no guest autologin
759 require_login(0, false);
760 if (isguestuser()) {
761 // Login as real user!
762 $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
763 redirect(get_login_url());
765 $context = context_system::instance();
767 if (!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;
776 // check that site is properly customized
777 $site = get_site();
778 if (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');
782 unset_config('timezone'); // Force admin to select timezone!
783 redirect('upgradesettings.php?return=site');
786 // setup critical warnings before printing admin tree block
787 $insecuredataroot = is_dataroot_insecure(true);
788 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
790 $adminroot = admin_get_root();
792 // Check if there are any new admin settings which have still yet to be set
793 if (any_new_admin_settings($adminroot)){
794 redirect('upgradesettings.php');
797 // Return to original page that started the plugin uninstallation if necessary.
798 if (isset($SESSION->pluginuninstallreturn)) {
799 $return = $SESSION->pluginuninstallreturn;
800 unset($SESSION->pluginuninstallreturn);
801 if ($return) {
802 redirect($return);
806 // Everything should now be set up, and the user is an admin
808 // Print default admin page with notifications.
809 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
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}');
813 $cronoverdue = ($lastcron < time() - 3600 * 24);
814 $dbproblems = $DB->diagnose();
815 $maintenancemode = !empty($CFG->maintenance_enabled);
817 // Available updates for Moodle core.
818 $updateschecker = \core\update\checker::instance();
819 $availableupdates = array();
820 $availableupdatesfetch = null;
822 if ($updateschecker->enabled()) {
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));
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) {
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();
837 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
843 // The timestamp of the most recent check for available updates
844 $availableupdatesfetch = $updateschecker->get_last_timefetched();
847 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
848 //check if the site is registered on Moodle.org
849 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
850 // Check if there are any cache warnings.
851 $cachewarnings = cache_helper::warnings();
852 // Check if there are events 1 API handlers.
853 $eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
855 admin_externalpage_setup('adminnotifications');
857 $output = $PAGE->get_renderer('core', 'admin');
859 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
860 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
861 $registered, $cachewarnings, $eventshandlers);