MDL-49645 core_backup: fixed 'expirythreshold' setting not being saved
[moodle.git] / admin / index.php
blob8df4edfa1ff1c6255fa85ed1fb508d9614d8e1db
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 define('NO_OUTPUT_BUFFERING', true);
57 if ((isset($_GET['cache']) and $_GET['cache'] === '0')
58 or (isset($_POST['cache']) and $_POST['cache'] === '0')
59 or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
60 // Prevent caching at all cost when visiting this page directly,
61 // we redirect to self once we known no upgrades are necessary.
62 // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
63 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
64 define('CACHE_DISABLE_ALL', true);
66 // Force OPcache reset if used, we do not want any stale caches
67 // when detecting if upgrade necessary or when running upgrade.
68 if (function_exists('opcache_reset')) {
69 opcache_reset();
71 $cache = 0;
73 } else {
74 $cache = 1;
77 require('../config.php');
79 // Invalidate the cache of version.php in any circumstances to help core_component
80 // detecting if the version has changed and component cache should be reset.
81 if (function_exists('opcache_invalidate')) {
82 opcache_invalidate($CFG->dirroot . '/version.php', true);
84 // Make sure the component cache gets rebuilt if necessary, any method that
85 // indirectly calls the protected init() method is good here.
86 core_component::get_core_subsystems();
88 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
89 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
91 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
92 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
93 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
94 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
95 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
96 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
97 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW);
99 // Set up PAGE.
100 $url = new moodle_url('/admin/index.php');
101 $url->param('cache', $cache);
102 $PAGE->set_url($url);
103 unset($url);
105 // Are we returning from an add-on installation request at moodle.org/plugins?
106 if ($newaddonreq and !$cache and empty($CFG->disableonclickaddoninstall)) {
107 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
108 'installaddonrequest' => $newaddonreq,
109 'confirm' => 0));
110 if (!isloggedin() or isguestuser()) {
111 // Login and go the the add-on tool page.
112 $SESSION->wantsurl = $target->out();
113 redirect(get_login_url());
115 redirect($target);
118 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
120 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
122 // Check some PHP server settings
124 if (ini_get_bool('session.auto_start')) {
125 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
128 if (!ini_get_bool('file_uploads')) {
129 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
132 if (is_float_problem()) {
133 print_error('phpfloatproblem', 'admin', '', $documentationlink);
136 // Set some necessary variables during set-up to avoid PHP warnings later on this page
137 if (!isset($CFG->release)) {
138 $CFG->release = '';
140 if (!isset($CFG->version)) {
141 $CFG->version = '';
143 if (!isset($CFG->branch)) {
144 $CFG->branch = '';
147 $version = null;
148 $release = null;
149 $branch = null;
150 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
151 $CFG->target_release = $release; // used during installation and upgrades
153 if (!$version or !$release) {
154 print_error('withoutversion', 'debug'); // without version, stop
157 if (!core_tables_exist()) {
158 $PAGE->set_pagelayout('maintenance');
159 $PAGE->set_popup_notification_allowed(false);
161 // fake some settings
162 $CFG->docroot = 'http://docs.moodle.org';
164 $strinstallation = get_string('installation', 'install');
166 // remove current session content completely
167 \core\session\manager::terminate_current();
169 if (empty($agreelicense)) {
170 $strlicense = get_string('license');
172 $PAGE->navbar->add($strlicense);
173 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
174 $PAGE->set_heading($strinstallation);
175 $PAGE->set_cacheable(false);
177 /** @var core_admin_renderer $output */
178 $output = $PAGE->get_renderer('core', 'admin');
179 echo $output->install_licence_page();
180 die();
182 if (empty($confirmrelease)) {
183 require_once($CFG->libdir.'/environmentlib.php');
184 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
185 $strcurrentrelease = get_string('currentrelease');
187 $PAGE->navbar->add($strcurrentrelease);
188 $PAGE->set_title($strinstallation);
189 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
190 $PAGE->set_cacheable(false);
192 /** @var core_admin_renderer $output */
193 $output = $PAGE->get_renderer('core', 'admin');
194 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
195 die();
198 // check plugin dependencies
199 $failed = array();
200 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
201 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
202 $PAGE->set_title($strinstallation);
203 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
205 $output = $PAGE->get_renderer('core', 'admin');
206 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
207 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
208 die();
210 unset($failed);
212 //TODO: add a page with list of non-standard plugins here
214 $strdatabasesetup = get_string('databasesetup');
215 upgrade_init_javascript();
217 $PAGE->navbar->add($strdatabasesetup);
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->header();
225 if (!$DB->setup_is_unicodedb()) {
226 if (!$DB->change_db_encoding()) {
227 // If could not convert successfully, throw error, and prevent installation
228 print_error('unicoderequired', 'admin');
232 install_core($version, true);
236 // Check version of Moodle code on disk compared with database
237 // and upgrade if possible.
239 if (!$cache) {
240 // Do not try to do anything fancy in non-cached mode,
241 // this prevents themes from fetching data from non-existent tables.
242 $PAGE->set_pagelayout('maintenance');
243 $PAGE->set_popup_notification_allowed(false);
246 $stradministration = get_string('administration');
247 $PAGE->set_context(context_system::instance());
249 if (empty($CFG->version)) {
250 print_error('missingconfigversion', 'debug');
253 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
254 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
255 purge_all_caches();
256 redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
259 if (!$cache and $version > $CFG->version) { // upgrade
261 // Warning about upgrading a test site.
262 $testsite = false;
263 if (defined('BEHAT_SITE_RUNNING')) {
264 $testsite = 'behat';
267 // We purge all of MUC's caches here.
268 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
269 // This ensures a real config object is loaded and the stores will be purged.
270 // This is the only way we can purge custom caches such as memcache or APC.
271 // Note: all other calls to caches will still used the disabled API.
272 cache_helper::purge_all(true);
273 // We then purge the regular caches.
274 purge_all_caches();
276 /** @var core_admin_renderer $output */
277 $output = $PAGE->get_renderer('core', 'admin');
279 if (upgrade_stale_php_files_present()) {
280 $PAGE->set_title($stradministration);
281 $PAGE->set_cacheable(false);
283 echo $output->upgrade_stale_php_files_page();
284 die();
287 if (empty($confirmupgrade)) {
288 $a = new stdClass();
289 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
290 $a->newversion = "$release (".sprintf('%.2f', $version).")";
291 $strdatabasechecking = get_string('databasechecking', '', $a);
293 $PAGE->set_title($stradministration);
294 $PAGE->set_heading($strdatabasechecking);
295 $PAGE->set_cacheable(false);
297 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
298 die();
300 } else if (empty($confirmrelease)){
301 require_once($CFG->libdir.'/environmentlib.php');
302 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
303 $strcurrentrelease = get_string('currentrelease');
305 $PAGE->navbar->add($strcurrentrelease);
306 $PAGE->set_title($strcurrentrelease);
307 $PAGE->set_heading($strcurrentrelease);
308 $PAGE->set_cacheable(false);
310 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
311 die();
313 } else if (empty($confirmplugins)) {
314 $strplugincheck = get_string('plugincheck');
316 $PAGE->navbar->add($strplugincheck);
317 $PAGE->set_title($strplugincheck);
318 $PAGE->set_heading($strplugincheck);
319 $PAGE->set_cacheable(false);
321 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
323 if ($fetchupdates) {
324 // No sesskey support guaranteed here, because sessions might not work yet.
325 $updateschecker = \core\update\checker::instance();
326 if ($updateschecker->enabled()) {
327 $updateschecker->fetch();
329 redirect($reloadurl);
332 $deployer = \core\update\deployer::instance();
333 if ($deployer->enabled()) {
334 $deployer->initialize($reloadurl, $reloadurl);
336 $deploydata = $deployer->submitted_data();
337 if (!empty($deploydata)) {
338 // No sesskey support guaranteed here, because sessions might not work yet.
339 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
340 die();
344 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
345 $version, $showallplugins, $reloadurl,
346 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1, 'cache'=>0)));
347 die();
349 } else {
350 // Always verify plugin dependencies!
351 $failed = array();
352 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
353 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
354 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
355 die();
357 unset($failed);
359 // Launch main upgrade.
360 upgrade_core($version, true);
362 } else if ($version < $CFG->version) {
363 // better stop here, we can not continue with plugin upgrades or anything else
364 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
367 // Updated human-readable release version if necessary
368 if (!$cache and $release <> $CFG->release) { // Update the release version
369 set_config('release', $release);
372 if (!$cache and $branch <> $CFG->branch) { // Update the branch
373 set_config('branch', $branch);
376 if (!$cache and moodle_needs_upgrading()) {
377 if (!$PAGE->headerprinted) {
378 // means core upgrade or installation was not already done
380 if (!$confirmplugins) {
381 $strplugincheck = get_string('plugincheck');
383 $PAGE->navbar->add($strplugincheck);
384 $PAGE->set_title($strplugincheck);
385 $PAGE->set_heading($strplugincheck);
386 $PAGE->set_cacheable(false);
388 if ($fetchupdates) {
389 require_sesskey();
390 $updateschecker = \core\update\checker::instance();
391 if ($updateschecker->enabled()) {
392 $updateschecker->fetch();
394 redirect($PAGE->url);
397 /** @var core_admin_renderer $output */
398 $output = $PAGE->get_renderer('core', 'admin');
400 $deployer = \core\update\deployer::instance();
401 if ($deployer->enabled()) {
402 $deployer->initialize($PAGE->url, $PAGE->url);
404 $deploydata = $deployer->submitted_data();
405 if (!empty($deploydata)) {
406 require_sesskey();
407 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
408 die();
412 // Show plugins info.
413 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
414 $version, $showallplugins,
415 new moodle_url($PAGE->url),
416 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1, 'cache'=>0)));
417 die();
420 // Make sure plugin dependencies are always checked.
421 $failed = array();
422 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
423 /** @var core_admin_renderer $output */
424 $output = $PAGE->get_renderer('core', 'admin');
425 $reloadurl = new moodle_url('/admin/index.php', array('cache' => 0));
426 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
427 die();
429 unset($failed);
432 // install/upgrade all plugins and other parts
433 upgrade_noncore(true);
436 // If this is the first install, indicate that this site is fully configured
437 // except the admin password
438 if (during_initial_install()) {
439 set_config('rolesactive', 1); // after this, during_initial_install will return false.
440 set_config('adminsetuppending', 1);
441 // we need this redirect to setup proper session
442 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
445 // make sure admin user is created - this is the last step because we need
446 // session to be working properly in order to edit admin account
447 if (!empty($CFG->adminsetuppending)) {
448 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
449 if (!$sessionstarted) {
450 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
451 } else {
452 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
453 if (!$sessionverify) {
454 $SESSION->sessionverify = 1;
455 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
456 } else {
457 if (empty($SESSION->sessionverify)) {
458 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
460 unset($SESSION->sessionverify);
464 // Cleanup SESSION to make sure other code does not complain in the future.
465 unset($SESSION->has_timed_out);
466 unset($SESSION->wantsurl);
468 // 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
469 $adminids = explode(',', $CFG->siteadmins);
470 $adminuser = get_complete_user_data('id', reset($adminids));
472 if ($adminuser->password === 'adminsetuppending') {
473 // prevent installation hijacking
474 if ($adminuser->lastip !== getremoteaddr()) {
475 print_error('installhijacked', 'admin');
477 // login user and let him set password and admin details
478 $adminuser->newadminuser = 1;
479 complete_user_login($adminuser);
480 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
482 } else {
483 unset_config('adminsetuppending');
486 } else {
487 // just make sure upgrade logging is properly terminated
488 upgrade_finished('upgradesettings.php');
491 if (has_capability('moodle/site:config', context_system::instance())) {
492 if ($fetchupdates) {
493 require_sesskey();
494 $updateschecker = \core\update\checker::instance();
495 if ($updateschecker->enabled()) {
496 $updateschecker->fetch();
498 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
502 // Now we can be sure everything was upgraded and caches work fine,
503 // redirect if necessary to make sure caching is enabled.
504 if (!$cache) {
505 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
508 // Check for valid admin user - no guest autologin
509 require_login(0, false);
510 if (isguestuser()) {
511 // Login as real user!
512 $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
513 redirect(get_login_url());
515 $context = context_system::instance();
516 require_capability('moodle/site:config', $context);
518 // check that site is properly customized
519 $site = get_site();
520 if (empty($site->shortname)) {
521 // probably new installation - lets return to frontpage after this step
522 // remove settings that we want uninitialised
523 unset_config('registerauth');
524 redirect('upgradesettings.php?return=site');
527 // setup critical warnings before printing admin tree block
528 $insecuredataroot = is_dataroot_insecure(true);
529 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
531 $adminroot = admin_get_root();
533 // Check if there are any new admin settings which have still yet to be set
534 if (any_new_admin_settings($adminroot)){
535 redirect('upgradesettings.php');
538 // Return to original page that started the plugin uninstallation if necessary.
539 if (isset($SESSION->pluginuninstallreturn)) {
540 $return = $SESSION->pluginuninstallreturn;
541 unset($SESSION->pluginuninstallreturn);
542 if ($return) {
543 redirect($return);
547 // Everything should now be set up, and the user is an admin
549 // Print default admin page with notifications.
550 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
552 // We make the assumption that at least one schedule task should run once per day.
553 $lastcron = $DB->get_field_sql('SELECT MAX(lastruntime) FROM {task_scheduled}');
554 $cronoverdue = ($lastcron < time() - 3600 * 24);
555 $dbproblems = $DB->diagnose();
556 $maintenancemode = !empty($CFG->maintenance_enabled);
558 // Available updates for Moodle core
559 $updateschecker = \core\update\checker::instance();
560 $availableupdates = array();
561 $availableupdates['core'] = $updateschecker->get_update_info('core',
562 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
564 // Available updates for contributed plugins
565 $pluginman = core_plugin_manager::instance();
566 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
567 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
568 if (!empty($plugininfo->availableupdates)) {
569 foreach ($plugininfo->availableupdates as $pluginavailableupdate) {
570 if ($pluginavailableupdate->version > $plugininfo->versiondisk) {
571 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
572 $availableupdates[$plugintype.'_'.$pluginname] = array();
574 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
581 // The timestamp of the most recent check for available updates
582 $availableupdatesfetch = $updateschecker->get_last_timefetched();
584 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
585 //check if the site is registered on Moodle.org
586 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
587 // Check if there are any cache warnings.
588 $cachewarnings = cache_helper::warnings();
590 admin_externalpage_setup('adminnotifications');
592 /* @var core_admin_renderer $output */
593 $output = $PAGE->get_renderer('core', 'admin');
595 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
596 $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
597 $registered, $cachewarnings);