Merge branch 'w41_MDL-42199_m26_rev' of https://github.com/skodak/moodle
[moodle.git] / admin / index.php
blobea9f37dd294e561b0308d598b4af47c4c343305a
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.3.3') < 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.5 or later requires at least PHP 5.3.3 (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 define('NO_OUTPUT_BUFFERING', true);
50 if (empty($_GET['cache']) and empty($_POST['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey'])) {
51 // Prevent caching at all cost when visiting this page directly,
52 // we redirect to self once we known no upgrades are necessary.
53 // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
54 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
55 define('CACHE_DISABLE_ALL', true);
57 // Force OPcache reset if used, we do not want any stale caches
58 // when detecting if upgrade necessary or when running upgrade.
59 if (function_exists('opcache_reset')) {
60 opcache_reset();
62 $cache = 0;
64 } else {
65 $cache = 1;
68 require('../config.php');
70 // Invalidate the cache of version.php in any circumstances to help core_component
71 // detecting if the version has changed and component cache should be reset.
72 if (function_exists('opcache_invalidate')) {
73 opcache_invalidate($CFG->dirroot . '/version.php', true);
75 // Make sure the component cache gets rebuilt if necessary, any method that
76 // indirectly calls the protected init() method is good here.
77 core_component::get_core_subsystems();
79 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
80 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
82 $id = optional_param('id', '', PARAM_TEXT);
83 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
84 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
85 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
86 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
87 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
88 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
89 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW);
91 // Set up PAGE.
92 $url = new moodle_url('/admin/index.php');
93 if ($cache) {
94 $url->param('cache', 1);
96 $PAGE->set_url($url);
97 unset($url);
99 // Are we returning from an add-on installation request at moodle.org/plugins?
100 if ($newaddonreq and !$cache and empty($CFG->disableonclickaddoninstall)) {
101 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
102 'installaddonrequest' => $newaddonreq,
103 'confirm' => 0));
104 if (!isloggedin() or isguestuser()) {
105 // Login and go the the add-on tool page.
106 $SESSION->wantsurl = $target->out();
107 redirect(get_login_url());
109 redirect($target);
112 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
114 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
116 // Check some PHP server settings
118 if (ini_get_bool('session.auto_start')) {
119 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
122 if (ini_get_bool('magic_quotes_runtime')) {
123 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
126 if (!ini_get_bool('file_uploads')) {
127 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
130 if (is_float_problem()) {
131 print_error('phpfloatproblem', 'admin', '', $documentationlink);
134 // Set some necessary variables during set-up to avoid PHP warnings later on this page
135 if (!isset($CFG->release)) {
136 $CFG->release = '';
138 if (!isset($CFG->version)) {
139 $CFG->version = '';
141 if (!isset($CFG->branch)) {
142 $CFG->branch = '';
145 $version = null;
146 $release = null;
147 $branch = null;
148 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
149 $CFG->target_release = $release; // used during installation and upgrades
151 if (!$version or !$release) {
152 print_error('withoutversion', 'debug'); // without version, stop
155 if (!core_tables_exist()) {
156 $PAGE->set_pagelayout('maintenance');
157 $PAGE->set_popup_notification_allowed(false);
159 // fake some settings
160 $CFG->docroot = 'http://docs.moodle.org';
162 $strinstallation = get_string('installation', 'install');
164 // remove current session content completely
165 \core\session\manager::terminate_current();
167 if (empty($agreelicense)) {
168 $strlicense = get_string('license');
170 $PAGE->navbar->add($strlicense);
171 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
172 $PAGE->set_heading($strinstallation);
173 $PAGE->set_cacheable(false);
175 /** @var core_admin_renderer $output */
176 $output = $PAGE->get_renderer('core', 'admin');
177 echo $output->install_licence_page();
178 die();
180 if (empty($confirmrelease)) {
181 require_once($CFG->libdir.'/environmentlib.php');
182 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
183 $strcurrentrelease = get_string('currentrelease');
185 $PAGE->navbar->add($strcurrentrelease);
186 $PAGE->set_title($strinstallation);
187 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
188 $PAGE->set_cacheable(false);
190 /** @var core_admin_renderer $output */
191 $output = $PAGE->get_renderer('core', 'admin');
192 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
193 die();
196 // check plugin dependencies
197 $failed = array();
198 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
199 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
200 $PAGE->set_title($strinstallation);
201 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
203 $output = $PAGE->get_renderer('core', 'admin');
204 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
205 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
206 die();
208 unset($failed);
210 //TODO: add a page with list of non-standard plugins here
212 $strdatabasesetup = get_string('databasesetup');
213 upgrade_init_javascript();
215 $PAGE->navbar->add($strdatabasesetup);
216 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
217 $PAGE->set_heading($strinstallation);
218 $PAGE->set_cacheable(false);
220 $output = $PAGE->get_renderer('core', 'admin');
221 echo $output->header();
223 if (!$DB->setup_is_unicodedb()) {
224 if (!$DB->change_db_encoding()) {
225 // If could not convert successfully, throw error, and prevent installation
226 print_error('unicoderequired', 'admin');
230 install_core($version, true);
234 // Check version of Moodle code on disk compared with database
235 // and upgrade if possible.
237 $stradministration = get_string('administration');
238 $PAGE->set_context(context_system::instance());
240 if (empty($CFG->version)) {
241 print_error('missingconfigversion', 'debug');
244 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
245 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
246 purge_all_caches();
247 redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
250 if (!$cache and $version > $CFG->version) { // upgrade
252 // Warning about upgrading a test site.
253 $testsite = false;
254 if (defined('BEHAT_SITE_RUNNING')) {
255 $testsite = 'behat';
258 // We purge all of MUC's caches here.
259 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
260 // This ensures a real config object is loaded and the stores will be purged.
261 // This is the only way we can purge custom caches such as memcache or APC.
262 // Note: all other calls to caches will still used the disabled API.
263 cache_helper::purge_all(true);
264 // We then purge the regular caches.
265 purge_all_caches();
267 $PAGE->set_pagelayout('maintenance');
268 $PAGE->set_popup_notification_allowed(false);
270 if (upgrade_stale_php_files_present()) {
271 $PAGE->set_title($stradministration);
272 $PAGE->set_cacheable(false);
274 /** @var core_admin_renderer $output */
275 $output = $PAGE->get_renderer('core', 'admin');
276 echo $output->upgrade_stale_php_files_page();
277 die();
280 if (empty($confirmupgrade)) {
281 $a = new stdClass();
282 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
283 $a->newversion = "$release (".sprintf('%.2f', $version).")";
284 $strdatabasechecking = get_string('databasechecking', '', $a);
286 $PAGE->set_title($stradministration);
287 $PAGE->set_heading($strdatabasechecking);
288 $PAGE->set_cacheable(false);
290 /** @var core_admin_renderer $output */
291 $output = $PAGE->get_renderer('core', 'admin');
292 echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
293 die();
295 } else if (empty($confirmrelease)){
296 require_once($CFG->libdir.'/environmentlib.php');
297 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
298 $strcurrentrelease = get_string('currentrelease');
300 $PAGE->navbar->add($strcurrentrelease);
301 $PAGE->set_title($strcurrentrelease);
302 $PAGE->set_heading($strcurrentrelease);
303 $PAGE->set_cacheable(false);
305 /** @var core_admin_renderer $output */
306 $output = $PAGE->get_renderer('core', 'admin');
307 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
308 die();
310 } else if (empty($confirmplugins)) {
311 $strplugincheck = get_string('plugincheck');
313 $PAGE->navbar->add($strplugincheck);
314 $PAGE->set_title($strplugincheck);
315 $PAGE->set_heading($strplugincheck);
316 $PAGE->set_cacheable(false);
318 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
320 /** @var core_admin_renderer $output */
321 $output = $PAGE->get_renderer('core', 'admin');
323 // check plugin dependencies first
324 $failed = array();
325 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
326 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
327 die();
329 unset($failed);
331 if ($fetchupdates) {
332 // no sesskey support guaranteed here
333 if (empty($CFG->disableupdatenotifications)) {
334 \core\update\checker::instance()->fetch();
336 redirect($reloadurl);
339 $deployer = \core\update\deployer::instance();
340 if ($deployer->enabled()) {
341 $deployer->initialize($reloadurl, $reloadurl);
343 $deploydata = $deployer->submitted_data();
344 if (!empty($deploydata)) {
345 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
346 die();
350 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
351 $version, $showallplugins, $reloadurl,
352 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
353 die();
355 } else {
356 // Launch main upgrade
357 upgrade_core($version, true);
359 } else if ($version < $CFG->version) {
360 // better stop here, we can not continue with plugin upgrades or anything else
361 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
364 // Updated human-readable release version if necessary
365 if (!$cache and $release <> $CFG->release) { // Update the release version
366 set_config('release', $release);
369 if (!$cache and $branch <> $CFG->branch) { // Update the branch
370 set_config('branch', $branch);
373 if (!$cache and moodle_needs_upgrading()) {
374 if (!$PAGE->headerprinted) {
375 // means core upgrade or installation was not already done
376 if (!$confirmplugins) {
377 $strplugincheck = get_string('plugincheck');
379 $PAGE->set_pagelayout('maintenance');
380 $PAGE->set_popup_notification_allowed(false);
381 $PAGE->navbar->add($strplugincheck);
382 $PAGE->set_title($strplugincheck);
383 $PAGE->set_heading($strplugincheck);
384 $PAGE->set_cacheable(false);
386 if ($fetchupdates) {
387 // no sesskey support guaranteed here
388 \core\update\checker::instance()->fetch();
389 redirect($PAGE->url);
392 $output = $PAGE->get_renderer('core', 'admin');
394 $deployer = \core\update\deployer::instance();
395 if ($deployer->enabled()) {
396 $deployer->initialize($PAGE->url, $PAGE->url);
398 $deploydata = $deployer->submitted_data();
399 if (!empty($deploydata)) {
400 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
401 die();
405 // check plugin dependencies first
406 $failed = array();
407 if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
408 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
409 die();
411 unset($failed);
413 // dependencies check passed, let's rock!
414 echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
415 $version, $showallplugins,
416 new moodle_url($PAGE->url),
417 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
418 die();
421 // install/upgrade all plugins and other parts
422 upgrade_noncore(true);
425 // If this is the first install, indicate that this site is fully configured
426 // except the admin password
427 if (during_initial_install()) {
428 set_config('rolesactive', 1); // after this, during_initial_install will return false.
429 set_config('adminsetuppending', 1);
430 // we need this redirect to setup proper session
431 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
434 // make sure admin user is created - this is the last step because we need
435 // session to be working properly in order to edit admin account
436 if (!empty($CFG->adminsetuppending)) {
437 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
438 if (!$sessionstarted) {
439 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
440 } else {
441 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
442 if (!$sessionverify) {
443 $SESSION->sessionverify = 1;
444 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
445 } else {
446 if (empty($SESSION->sessionverify)) {
447 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
449 unset($SESSION->sessionverify);
453 // Cleanup SESSION to make sure other code does not complain in the future.
454 unset($SESSION->has_timed_out);
455 unset($SESSION->wantsurl);
457 // 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
458 $adminids = explode(',', $CFG->siteadmins);
459 $adminuser = get_complete_user_data('id', reset($adminids));
461 if ($adminuser->password === 'adminsetuppending') {
462 // prevent installation hijacking
463 if ($adminuser->lastip !== getremoteaddr()) {
464 print_error('installhijacked', 'admin');
466 // login user and let him set password and admin details
467 $adminuser->newadminuser = 1;
468 complete_user_login($adminuser);
469 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
471 } else {
472 unset_config('adminsetuppending');
475 } else {
476 // just make sure upgrade logging is properly terminated
477 upgrade_finished('upgradesettings.php');
480 // Now we can be sure everything was upgraded and caches work fine,
481 // redirect if necessary to make sure caching is enabled.
482 if (!$cache) {
483 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
486 // Check for valid admin user - no guest autologin
487 require_login(0, false);
488 $context = context_system::instance();
489 require_capability('moodle/site:config', $context);
491 // check that site is properly customized
492 $site = get_site();
493 if (empty($site->shortname)) {
494 // probably new installation - lets return to frontpage after this step
495 // remove settings that we want uninitialised
496 unset_config('registerauth');
497 redirect('upgradesettings.php?return=site');
500 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
501 if (!empty($id) and $id == $CFG->siteidentifier) {
502 set_config('registered', time());
505 // setup critical warnings before printing admin tree block
506 $insecuredataroot = is_dataroot_insecure(true);
507 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
509 $adminroot = admin_get_root();
511 // Check if there are any new admin settings which have still yet to be set
512 if (any_new_admin_settings($adminroot)){
513 redirect('upgradesettings.php');
516 // Return to original page that started the plugin uninstallation if necessary.
517 if (isset($SESSION->pluginuninstallreturn)) {
518 $return = $SESSION->pluginuninstallreturn;
519 unset($SESSION->pluginuninstallreturn);
520 if ($return) {
521 redirect($return);
525 // Everything should now be set up, and the user is an admin
527 // Print default admin page with notifications.
528 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
530 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
531 $cronoverdue = ($lastcron < time() - 3600 * 24);
532 $dbproblems = $DB->diagnose();
533 $maintenancemode = !empty($CFG->maintenance_enabled);
535 // Available updates for Moodle core
536 $updateschecker = \core\update\checker::instance();
537 $availableupdates = array();
538 $availableupdates['core'] = $updateschecker->get_update_info('core',
539 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
541 // Available updates for contributed plugins
542 $pluginman = core_plugin_manager::instance();
543 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
544 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
545 if (!empty($plugininfo->availableupdates)) {
546 foreach ($plugininfo->availableupdates as $pluginavailableupdate) {
547 if ($pluginavailableupdate->version > $plugininfo->versiondisk) {
548 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
549 $availableupdates[$plugintype.'_'.$pluginname] = array();
551 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
558 // The timestamp of the most recent check for available updates
559 $availableupdatesfetch = $updateschecker->get_last_timefetched();
561 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
562 //check if the site is registered on Moodle.org
563 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
565 admin_externalpage_setup('adminnotifications');
567 if ($fetchupdates) {
568 require_sesskey();
569 $updateschecker->fetch();
570 redirect(new moodle_url('/admin/index.php'));
573 $output = $PAGE->get_renderer('core', 'admin');
574 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
575 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
576 $registered);