Merge branch 'MDL-36482_24' of https://github.com/mr-russ/moodle into MOODLE_24_STABLE
[moodle.git] / admin / index.php
blobd75e36488aeb57effb2c1d76164c20f4e3c69fd1
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.2') < 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.1 or later requires at least PHP 5.3.2 (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 require('../config.php');
51 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
52 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
53 require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
55 $id = optional_param('id', '', PARAM_TEXT);
56 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
57 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
58 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
59 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
60 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
61 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
63 // Check some PHP server settings
65 $PAGE->set_url('/admin/index.php');
66 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
68 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
70 if (ini_get_bool('session.auto_start')) {
71 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
74 if (ini_get_bool('magic_quotes_runtime')) {
75 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
78 if (!ini_get_bool('file_uploads')) {
79 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
82 if (is_float_problem()) {
83 print_error('phpfloatproblem', 'admin', '', $documentationlink);
86 // Set some necessary variables during set-up to avoid PHP warnings later on this page
87 if (!isset($CFG->release)) {
88 $CFG->release = '';
90 if (!isset($CFG->version)) {
91 $CFG->version = '';
93 if (!isset($CFG->branch)) {
94 $CFG->branch = '';
97 $version = null;
98 $release = null;
99 $branch = null;
100 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
101 $CFG->target_release = $release; // used during installation and upgrades
103 if (!$version or !$release) {
104 print_error('withoutversion', 'debug'); // without version, stop
107 if (!core_tables_exist()) {
108 $PAGE->set_pagelayout('maintenance');
109 $PAGE->set_popup_notification_allowed(false);
111 // fake some settings
112 $CFG->docroot = 'http://docs.moodle.org';
114 $strinstallation = get_string('installation', 'install');
116 // remove current session content completely
117 session_get_instance()->terminate_current();
119 if (empty($agreelicense)) {
120 $strlicense = get_string('license');
122 $PAGE->navbar->add($strlicense);
123 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
124 $PAGE->set_heading($strinstallation);
125 $PAGE->set_cacheable(false);
127 $output = $PAGE->get_renderer('core', 'admin');
128 echo $output->install_licence_page();
129 die();
131 if (empty($confirmrelease)) {
132 require_once($CFG->libdir.'/environmentlib.php');
133 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
134 $strcurrentrelease = get_string('currentrelease');
136 $PAGE->navbar->add($strcurrentrelease);
137 $PAGE->set_title($strinstallation);
138 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
139 $PAGE->set_cacheable(false);
141 $output = $PAGE->get_renderer('core', 'admin');
142 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
143 die();
146 // check plugin dependencies
147 $failed = array();
148 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
149 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
150 $PAGE->set_title($strinstallation);
151 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
153 $output = $PAGE->get_renderer('core', 'admin');
154 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
155 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
156 die();
158 unset($failed);
160 //TODO: add a page with list of non-standard plugins here
162 $strdatabasesetup = get_string('databasesetup');
163 upgrade_init_javascript();
165 $PAGE->navbar->add($strdatabasesetup);
166 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
167 $PAGE->set_heading($strinstallation);
168 $PAGE->set_cacheable(false);
170 $output = $PAGE->get_renderer('core', 'admin');
171 echo $output->header();
173 if (!$DB->setup_is_unicodedb()) {
174 if (!$DB->change_db_encoding()) {
175 // If could not convert successfully, throw error, and prevent installation
176 print_error('unicoderequired', 'admin');
180 install_core($version, true);
184 // Check version of Moodle code on disk compared with database
185 // and upgrade if possible.
187 $stradministration = get_string('administration');
188 $PAGE->set_context(context_system::instance());
190 if (empty($CFG->version)) {
191 print_error('missingconfigversion', 'debug');
194 if ($version > $CFG->version) { // upgrade
195 purge_all_caches();
197 $PAGE->set_pagelayout('maintenance');
198 $PAGE->set_popup_notification_allowed(false);
200 if (upgrade_stale_php_files_present()) {
201 $PAGE->set_title($stradministration);
202 $PAGE->set_cacheable(false);
204 $output = $PAGE->get_renderer('core', 'admin');
205 echo $output->upgrade_stale_php_files_page();
206 die();
209 if (empty($confirmupgrade)) {
210 $a = new stdClass();
211 $a->oldversion = "$CFG->release ($CFG->version)";
212 $a->newversion = "$release ($version)";
213 $strdatabasechecking = get_string('databasechecking', '', $a);
215 $PAGE->set_title($stradministration);
216 $PAGE->set_heading($strdatabasechecking);
217 $PAGE->set_cacheable(false);
219 $output = $PAGE->get_renderer('core', 'admin');
220 echo $output->upgrade_confirm_page($a->newversion, $maturity);
221 die();
223 } else if (empty($confirmrelease)){
224 require_once($CFG->libdir.'/environmentlib.php');
225 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
226 $strcurrentrelease = get_string('currentrelease');
228 $PAGE->navbar->add($strcurrentrelease);
229 $PAGE->set_title($strcurrentrelease);
230 $PAGE->set_heading($strcurrentrelease);
231 $PAGE->set_cacheable(false);
233 $output = $PAGE->get_renderer('core', 'admin');
234 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
235 die();
237 } else if (empty($confirmplugins)) {
238 $strplugincheck = get_string('plugincheck');
240 $PAGE->navbar->add($strplugincheck);
241 $PAGE->set_title($strplugincheck);
242 $PAGE->set_heading($strplugincheck);
243 $PAGE->set_cacheable(false);
245 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
247 // check plugin dependencies first
248 $failed = array();
249 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
250 $output = $PAGE->get_renderer('core', 'admin');
251 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
252 die();
254 unset($failed);
256 if ($fetchupdates) {
257 // no sesskey support guaranteed here
258 if (empty($CFG->disableupdatenotifications)) {
259 available_update_checker::instance()->fetch();
261 redirect($reloadurl);
264 $output = $PAGE->get_renderer('core', 'admin');
266 $deployer = available_update_deployer::instance();
267 if ($deployer->enabled()) {
268 $deployer->initialize($reloadurl, $reloadurl);
270 $deploydata = $deployer->submitted_data();
271 if (!empty($deploydata)) {
272 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
273 die();
277 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
278 $version, $showallplugins, $reloadurl,
279 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
280 die();
282 } else {
283 // Launch main upgrade
284 upgrade_core($version, true);
286 } else if ($version < $CFG->version) {
287 // better stop here, we can not continue with plugin upgrades or anything else
288 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
291 // Updated human-readable release version if necessary
292 if ($release <> $CFG->release) { // Update the release version
293 set_config('release', $release);
296 if ($branch <> $CFG->branch) { // Update the branch
297 set_config('branch', $branch);
300 if (moodle_needs_upgrading()) {
301 if (!$PAGE->headerprinted) {
302 // means core upgrade or installation was not already done
303 if (!$confirmplugins) {
304 $strplugincheck = get_string('plugincheck');
306 $PAGE->set_pagelayout('maintenance');
307 $PAGE->set_popup_notification_allowed(false);
308 $PAGE->navbar->add($strplugincheck);
309 $PAGE->set_title($strplugincheck);
310 $PAGE->set_heading($strplugincheck);
311 $PAGE->set_cacheable(false);
313 if ($fetchupdates) {
314 // no sesskey support guaranteed here
315 available_update_checker::instance()->fetch();
316 redirect($PAGE->url);
319 $output = $PAGE->get_renderer('core', 'admin');
321 $deployer = available_update_deployer::instance();
322 if ($deployer->enabled()) {
323 $deployer->initialize($PAGE->url, $PAGE->url);
325 $deploydata = $deployer->submitted_data();
326 if (!empty($deploydata)) {
327 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
328 die();
332 // check plugin dependencies first
333 $failed = array();
334 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
335 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
336 die();
338 unset($failed);
340 // dependencies check passed, let's rock!
341 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
342 $version, $showallplugins,
343 new moodle_url($PAGE->url),
344 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
345 die();
348 // install/upgrade all plugins and other parts
349 upgrade_noncore(true);
352 // If this is the first install, indicate that this site is fully configured
353 // except the admin password
354 if (during_initial_install()) {
355 set_config('rolesactive', 1); // after this, during_initial_install will return false.
356 set_config('adminsetuppending', 1);
357 // we need this redirect to setup proper session
358 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
361 // make sure admin user is created - this is the last step because we need
362 // session to be working properly in order to edit admin account
363 if (!empty($CFG->adminsetuppending)) {
364 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
365 if (!$sessionstarted) {
366 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
367 } else {
368 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
369 if (!$sessionverify) {
370 $SESSION->sessionverify = 1;
371 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
372 } else {
373 if (empty($SESSION->sessionverify)) {
374 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
376 unset($SESSION->sessionverify);
380 // Cleanup SESSION to make sure other code does not complain in the future.
381 unset($SESSION->has_timed_out);
382 unset($SESSION->wantsurl);
384 // 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
385 $adminids = explode(',', $CFG->siteadmins);
386 $adminuser = get_complete_user_data('id', reset($adminids));
388 if ($adminuser->password === 'adminsetuppending') {
389 // prevent installation hijacking
390 if ($adminuser->lastip !== getremoteaddr()) {
391 print_error('installhijacked', 'admin');
393 // login user and let him set password and admin details
394 $adminuser->newadminuser = 1;
395 complete_user_login($adminuser);
396 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
398 } else {
399 unset_config('adminsetuppending');
402 } else {
403 // just make sure upgrade logging is properly terminated
404 upgrade_finished('upgradesettings.php');
407 // Check for valid admin user - no guest autologin
408 require_login(0, false);
409 $context = context_system::instance();
410 require_capability('moodle/site:config', $context);
412 // check that site is properly customized
413 $site = get_site();
414 if (empty($site->shortname)) {
415 // probably new installation - lets return to frontpage after this step
416 // remove settings that we want uninitialised
417 unset_config('registerauth');
418 redirect('upgradesettings.php?return=site');
421 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
422 if (!empty($id) and $id == $CFG->siteidentifier) {
423 set_config('registered', time());
426 // setup critical warnings before printing admin tree block
427 $insecuredataroot = is_dataroot_insecure(true);
428 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
430 $adminroot = admin_get_root();
432 // Check if there are any new admin settings which have still yet to be set
433 if (any_new_admin_settings($adminroot)){
434 redirect('upgradesettings.php');
437 // Everything should now be set up, and the user is an admin
439 // Print default admin page with notifications.
440 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
442 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
443 $cronoverdue = ($lastcron < time() - 3600 * 24);
444 $dbproblems = $DB->diagnose();
445 $maintenancemode = !empty($CFG->maintenance_enabled);
447 // Available updates for Moodle core
448 $updateschecker = available_update_checker::instance();
449 $availableupdates = array();
450 $availableupdates['core'] = $updateschecker->get_update_info('core',
451 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
453 // Available updates for contributed plugins
454 $pluginman = plugin_manager::instance();
455 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
456 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
457 if (!empty($plugininfo->availableupdates)) {
458 foreach ($plugininfo->availableupdates as $pluginavailableupdate) {
459 if ($pluginavailableupdate->version > $plugininfo->versiondisk) {
460 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
461 $availableupdates[$plugintype.'_'.$pluginname] = array();
463 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
470 // The timestamp of the most recent check for available updates
471 $availableupdatesfetch = $updateschecker->get_last_timefetched();
473 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
474 //check if the site is registered on Moodle.org
475 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
477 admin_externalpage_setup('adminnotifications');
479 if ($fetchupdates) {
480 require_sesskey();
481 $updateschecker->fetch();
482 redirect($PAGE->url);
485 $output = $PAGE->get_renderer('core', 'admin');
486 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
487 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
488 $registered);