Merge branch 'MDL-39717_25' of git://github.com/dmonllao/moodle into MOODLE_25_STABLE
[moodle.git] / admin / index.php
blobc54e83887918f721c4e07f048bea965b7291b87e
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'])) {
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 define('CACHE_DISABLE_ALL', true);
57 require('../config.php');
58 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
59 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
60 require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
62 $id = optional_param('id', '', PARAM_TEXT);
63 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
64 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
65 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
66 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
67 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
68 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
69 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW);
70 $cache = optional_param('cache', 0, PARAM_BOOL);
72 // Set up PAGE.
73 $url = new moodle_url('/admin/index.php');
74 if (!is_null($newaddonreq)) {
75 // We need to set the eventual add-on installation request in the $PAGE's URL
76 // so that it is stored in $SESSION->wantsurl and the admin is redirected
77 // correctly once they are logged-in.
78 $url->param('installaddonrequest', $newaddonreq);
80 if ($cache) {
81 $url->param('cache', $cache);
83 $PAGE->set_url($url);
84 unset($url);
86 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
88 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
90 // Check some PHP server settings
92 if (ini_get_bool('session.auto_start')) {
93 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
96 if (ini_get_bool('magic_quotes_runtime')) {
97 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
100 if (!ini_get_bool('file_uploads')) {
101 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
104 if (is_float_problem()) {
105 print_error('phpfloatproblem', 'admin', '', $documentationlink);
108 // Set some necessary variables during set-up to avoid PHP warnings later on this page
109 if (!isset($CFG->release)) {
110 $CFG->release = '';
112 if (!isset($CFG->version)) {
113 $CFG->version = '';
115 if (!isset($CFG->branch)) {
116 $CFG->branch = '';
119 $version = null;
120 $release = null;
121 $branch = null;
122 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
123 $CFG->target_release = $release; // used during installation and upgrades
125 if (!$version or !$release) {
126 print_error('withoutversion', 'debug'); // without version, stop
129 if (!core_tables_exist()) {
130 $PAGE->set_pagelayout('maintenance');
131 $PAGE->set_popup_notification_allowed(false);
133 // fake some settings
134 $CFG->docroot = 'http://docs.moodle.org';
136 $strinstallation = get_string('installation', 'install');
138 // remove current session content completely
139 session_get_instance()->terminate_current();
141 if (empty($agreelicense)) {
142 $strlicense = get_string('license');
144 $PAGE->navbar->add($strlicense);
145 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
146 $PAGE->set_heading($strinstallation);
147 $PAGE->set_cacheable(false);
149 $output = $PAGE->get_renderer('core', 'admin');
150 echo $output->install_licence_page();
151 die();
153 if (empty($confirmrelease)) {
154 require_once($CFG->libdir.'/environmentlib.php');
155 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
156 $strcurrentrelease = get_string('currentrelease');
158 $PAGE->navbar->add($strcurrentrelease);
159 $PAGE->set_title($strinstallation);
160 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
161 $PAGE->set_cacheable(false);
163 $output = $PAGE->get_renderer('core', 'admin');
164 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
165 die();
168 // check plugin dependencies
169 $failed = array();
170 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
171 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
172 $PAGE->set_title($strinstallation);
173 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
175 $output = $PAGE->get_renderer('core', 'admin');
176 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
177 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
178 die();
180 unset($failed);
182 //TODO: add a page with list of non-standard plugins here
184 $strdatabasesetup = get_string('databasesetup');
185 upgrade_init_javascript();
187 $PAGE->navbar->add($strdatabasesetup);
188 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
189 $PAGE->set_heading($strinstallation);
190 $PAGE->set_cacheable(false);
192 $output = $PAGE->get_renderer('core', 'admin');
193 echo $output->header();
195 if (!$DB->setup_is_unicodedb()) {
196 if (!$DB->change_db_encoding()) {
197 // If could not convert successfully, throw error, and prevent installation
198 print_error('unicoderequired', 'admin');
202 install_core($version, true);
206 // Check version of Moodle code on disk compared with database
207 // and upgrade if possible.
209 $stradministration = get_string('administration');
210 $PAGE->set_context(context_system::instance());
212 if (empty($CFG->version)) {
213 print_error('missingconfigversion', 'debug');
216 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
217 if ($cache) {
218 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
219 purge_all_caches();
220 redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
224 if ($version > $CFG->version) { // upgrade
225 // We purge all of MUC's caches here.
226 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
227 // This ensures a real config object is loaded and the stores will be purged.
228 // This is the only way we can purge custom caches such as memcache or APC.
229 // Note: all other calls to caches will still used the disabled API.
230 cache_helper::purge_all(true);
231 // We then purge the regular caches.
232 purge_all_caches();
234 $PAGE->set_pagelayout('maintenance');
235 $PAGE->set_popup_notification_allowed(false);
237 if (upgrade_stale_php_files_present()) {
238 $PAGE->set_title($stradministration);
239 $PAGE->set_cacheable(false);
241 $output = $PAGE->get_renderer('core', 'admin');
242 echo $output->upgrade_stale_php_files_page();
243 die();
246 if (empty($confirmupgrade)) {
247 $a = new stdClass();
248 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
249 $a->newversion = "$release (".sprintf('%.2f', $version).")";
250 $strdatabasechecking = get_string('databasechecking', '', $a);
252 $PAGE->set_title($stradministration);
253 $PAGE->set_heading($strdatabasechecking);
254 $PAGE->set_cacheable(false);
256 $output = $PAGE->get_renderer('core', 'admin');
257 echo $output->upgrade_confirm_page($a->newversion, $maturity);
258 die();
260 } else if (empty($confirmrelease)){
261 require_once($CFG->libdir.'/environmentlib.php');
262 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
263 $strcurrentrelease = get_string('currentrelease');
265 $PAGE->navbar->add($strcurrentrelease);
266 $PAGE->set_title($strcurrentrelease);
267 $PAGE->set_heading($strcurrentrelease);
268 $PAGE->set_cacheable(false);
270 $output = $PAGE->get_renderer('core', 'admin');
271 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
272 die();
274 } else if (empty($confirmplugins)) {
275 $strplugincheck = get_string('plugincheck');
277 $PAGE->navbar->add($strplugincheck);
278 $PAGE->set_title($strplugincheck);
279 $PAGE->set_heading($strplugincheck);
280 $PAGE->set_cacheable(false);
282 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
284 // check plugin dependencies first
285 $failed = array();
286 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
287 $output = $PAGE->get_renderer('core', 'admin');
288 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
289 die();
291 unset($failed);
293 if ($fetchupdates) {
294 // no sesskey support guaranteed here
295 if (empty($CFG->disableupdatenotifications)) {
296 available_update_checker::instance()->fetch();
298 redirect($reloadurl);
301 $output = $PAGE->get_renderer('core', 'admin');
303 $deployer = available_update_deployer::instance();
304 if ($deployer->enabled()) {
305 $deployer->initialize($reloadurl, $reloadurl);
307 $deploydata = $deployer->submitted_data();
308 if (!empty($deploydata)) {
309 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
310 die();
314 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
315 $version, $showallplugins, $reloadurl,
316 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
317 die();
319 } else {
320 // Launch main upgrade
321 upgrade_core($version, true);
323 } else if ($version < $CFG->version) {
324 // better stop here, we can not continue with plugin upgrades or anything else
325 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
328 // Updated human-readable release version if necessary
329 if ($release <> $CFG->release) { // Update the release version
330 set_config('release', $release);
333 if ($branch <> $CFG->branch) { // Update the branch
334 set_config('branch', $branch);
337 if (moodle_needs_upgrading()) {
338 if (!$PAGE->headerprinted) {
339 // means core upgrade or installation was not already done
340 if (!$confirmplugins) {
341 $strplugincheck = get_string('plugincheck');
343 $PAGE->set_pagelayout('maintenance');
344 $PAGE->set_popup_notification_allowed(false);
345 $PAGE->navbar->add($strplugincheck);
346 $PAGE->set_title($strplugincheck);
347 $PAGE->set_heading($strplugincheck);
348 $PAGE->set_cacheable(false);
350 if ($fetchupdates) {
351 // no sesskey support guaranteed here
352 available_update_checker::instance()->fetch();
353 redirect($PAGE->url);
356 $output = $PAGE->get_renderer('core', 'admin');
358 $deployer = available_update_deployer::instance();
359 if ($deployer->enabled()) {
360 $deployer->initialize($PAGE->url, $PAGE->url);
362 $deploydata = $deployer->submitted_data();
363 if (!empty($deploydata)) {
364 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
365 die();
369 // check plugin dependencies first
370 $failed = array();
371 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
372 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
373 die();
375 unset($failed);
377 // dependencies check passed, let's rock!
378 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
379 $version, $showallplugins,
380 new moodle_url($PAGE->url),
381 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
382 die();
385 // install/upgrade all plugins and other parts
386 upgrade_noncore(true);
389 // If this is the first install, indicate that this site is fully configured
390 // except the admin password
391 if (during_initial_install()) {
392 set_config('rolesactive', 1); // after this, during_initial_install will return false.
393 set_config('adminsetuppending', 1);
394 // we need this redirect to setup proper session
395 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
398 // make sure admin user is created - this is the last step because we need
399 // session to be working properly in order to edit admin account
400 if (!empty($CFG->adminsetuppending)) {
401 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
402 if (!$sessionstarted) {
403 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
404 } else {
405 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
406 if (!$sessionverify) {
407 $SESSION->sessionverify = 1;
408 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
409 } else {
410 if (empty($SESSION->sessionverify)) {
411 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
413 unset($SESSION->sessionverify);
417 // Cleanup SESSION to make sure other code does not complain in the future.
418 unset($SESSION->has_timed_out);
419 unset($SESSION->wantsurl);
421 // 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
422 $adminids = explode(',', $CFG->siteadmins);
423 $adminuser = get_complete_user_data('id', reset($adminids));
425 if ($adminuser->password === 'adminsetuppending') {
426 // prevent installation hijacking
427 if ($adminuser->lastip !== getremoteaddr()) {
428 print_error('installhijacked', 'admin');
430 // login user and let him set password and admin details
431 $adminuser->newadminuser = 1;
432 complete_user_login($adminuser);
433 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
435 } else {
436 unset_config('adminsetuppending');
439 } else {
440 // just make sure upgrade logging is properly terminated
441 upgrade_finished('upgradesettings.php');
444 // Now we can be sure everything was upgraded and caches work fine,
445 // redirect if necessary to make sure caching is enabled.
446 if (!$cache) {
447 redirect(new moodle_url($PAGE->url, array('cache' => 1)));
450 // Check for valid admin user - no guest autologin
451 require_login(0, false);
452 $context = context_system::instance();
453 require_capability('moodle/site:config', $context);
455 // check that site is properly customized
456 $site = get_site();
457 if (empty($site->shortname)) {
458 // probably new installation - lets return to frontpage after this step
459 // remove settings that we want uninitialised
460 unset_config('registerauth');
461 redirect('upgradesettings.php?return=site');
464 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
465 if (!empty($id) and $id == $CFG->siteidentifier) {
466 set_config('registered', time());
469 // Check if we are returning from an add-on installation request at moodle.org/plugins
470 if (!is_null($newaddonreq)) {
471 if (!empty($CFG->disableonclickaddoninstall)) {
472 // The feature is disabled in config.php, ignore the request.
473 } else {
474 redirect(new moodle_url('/admin/tool/installaddon/index.php', array(
475 'installaddonrequest' => $newaddonreq,
476 'confirm' => 0)));
480 // setup critical warnings before printing admin tree block
481 $insecuredataroot = is_dataroot_insecure(true);
482 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
484 $adminroot = admin_get_root();
486 // Check if there are any new admin settings which have still yet to be set
487 if (any_new_admin_settings($adminroot)){
488 redirect('upgradesettings.php');
491 // Everything should now be set up, and the user is an admin
493 // Print default admin page with notifications.
494 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
496 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
497 $cronoverdue = ($lastcron < time() - 3600 * 24);
498 $dbproblems = $DB->diagnose();
499 $maintenancemode = !empty($CFG->maintenance_enabled);
501 // Available updates for Moodle core
502 $updateschecker = available_update_checker::instance();
503 $availableupdates = array();
504 $availableupdates['core'] = $updateschecker->get_update_info('core',
505 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
507 // Available updates for contributed plugins
508 $pluginman = plugin_manager::instance();
509 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
510 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
511 if (!empty($plugininfo->availableupdates)) {
512 foreach ($plugininfo->availableupdates as $pluginavailableupdate) {
513 if ($pluginavailableupdate->version > $plugininfo->versiondisk) {
514 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
515 $availableupdates[$plugintype.'_'.$pluginname] = array();
517 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
524 // The timestamp of the most recent check for available updates
525 $availableupdatesfetch = $updateschecker->get_last_timefetched();
527 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
528 //check if the site is registered on Moodle.org
529 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
531 admin_externalpage_setup('adminnotifications');
533 if ($fetchupdates) {
534 require_sesskey();
535 $updateschecker->fetch();
536 redirect(new moodle_url('/admin/index.php'));
539 $output = $PAGE->get_renderer('core', 'admin');
540 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
541 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
542 $registered);