MDL-41806 Add assessors to moodle_url class
[moodle.git] / admin / index.php
blob1f186b6417a06901ec03f39c13ec61e3aa6efd32
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
81 require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
83 $id = optional_param('id', '', PARAM_TEXT);
84 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
85 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
86 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
87 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
88 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
89 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
90 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW);
92 // Set up PAGE.
93 $url = new moodle_url('/admin/index.php');
94 if ($cache) {
95 $url->param('cache', 1);
97 $PAGE->set_url($url);
98 unset($url);
100 // Are we returning from an add-on installation request at moodle.org/plugins?
101 if ($newaddonreq and !$cache and empty($CFG->disableonclickaddoninstall)) {
102 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
103 'installaddonrequest' => $newaddonreq,
104 'confirm' => 0));
105 if (!isloggedin() or isguestuser()) {
106 // Login and go the the add-on tool page.
107 $SESSION->wantsurl = $target->out();
108 redirect(get_login_url());
110 redirect($target);
113 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
115 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
117 // Check some PHP server settings
119 if (ini_get_bool('session.auto_start')) {
120 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
123 if (ini_get_bool('magic_quotes_runtime')) {
124 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
127 if (!ini_get_bool('file_uploads')) {
128 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
131 if (is_float_problem()) {
132 print_error('phpfloatproblem', 'admin', '', $documentationlink);
135 // Set some necessary variables during set-up to avoid PHP warnings later on this page
136 if (!isset($CFG->release)) {
137 $CFG->release = '';
139 if (!isset($CFG->version)) {
140 $CFG->version = '';
142 if (!isset($CFG->branch)) {
143 $CFG->branch = '';
146 $version = null;
147 $release = null;
148 $branch = null;
149 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
150 $CFG->target_release = $release; // used during installation and upgrades
152 if (!$version or !$release) {
153 print_error('withoutversion', 'debug'); // without version, stop
156 if (!core_tables_exist()) {
157 $PAGE->set_pagelayout('maintenance');
158 $PAGE->set_popup_notification_allowed(false);
160 // fake some settings
161 $CFG->docroot = 'http://docs.moodle.org';
163 $strinstallation = get_string('installation', 'install');
165 // remove current session content completely
166 session_get_instance()->terminate_current();
168 if (empty($agreelicense)) {
169 $strlicense = get_string('license');
171 $PAGE->navbar->add($strlicense);
172 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
173 $PAGE->set_heading($strinstallation);
174 $PAGE->set_cacheable(false);
176 /** @var core_admin_renderer $output */
177 $output = $PAGE->get_renderer('core', 'admin');
178 echo $output->install_licence_page();
179 die();
181 if (empty($confirmrelease)) {
182 require_once($CFG->libdir.'/environmentlib.php');
183 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
184 $strcurrentrelease = get_string('currentrelease');
186 $PAGE->navbar->add($strcurrentrelease);
187 $PAGE->set_title($strinstallation);
188 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
189 $PAGE->set_cacheable(false);
191 /** @var core_admin_renderer $output */
192 $output = $PAGE->get_renderer('core', 'admin');
193 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
194 die();
197 // check plugin dependencies
198 $failed = array();
199 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
200 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
201 $PAGE->set_title($strinstallation);
202 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
204 $output = $PAGE->get_renderer('core', 'admin');
205 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
206 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
207 die();
209 unset($failed);
211 //TODO: add a page with list of non-standard plugins here
213 $strdatabasesetup = get_string('databasesetup');
214 upgrade_init_javascript();
216 $PAGE->navbar->add($strdatabasesetup);
217 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
218 $PAGE->set_heading($strinstallation);
219 $PAGE->set_cacheable(false);
221 $output = $PAGE->get_renderer('core', 'admin');
222 echo $output->header();
224 if (!$DB->setup_is_unicodedb()) {
225 if (!$DB->change_db_encoding()) {
226 // If could not convert successfully, throw error, and prevent installation
227 print_error('unicoderequired', 'admin');
231 install_core($version, true);
235 // Check version of Moodle code on disk compared with database
236 // and upgrade if possible.
238 $stradministration = get_string('administration');
239 $PAGE->set_context(context_system::instance());
241 if (empty($CFG->version)) {
242 print_error('missingconfigversion', 'debug');
245 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
246 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
247 purge_all_caches();
248 redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
251 if (!$cache and $version > $CFG->version) { // upgrade
252 // We purge all of MUC's caches here.
253 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
254 // This ensures a real config object is loaded and the stores will be purged.
255 // This is the only way we can purge custom caches such as memcache or APC.
256 // Note: all other calls to caches will still used the disabled API.
257 cache_helper::purge_all(true);
258 // We then purge the regular caches.
259 purge_all_caches();
261 $PAGE->set_pagelayout('maintenance');
262 $PAGE->set_popup_notification_allowed(false);
264 if (upgrade_stale_php_files_present()) {
265 $PAGE->set_title($stradministration);
266 $PAGE->set_cacheable(false);
268 /** @var core_admin_renderer $output */
269 $output = $PAGE->get_renderer('core', 'admin');
270 echo $output->upgrade_stale_php_files_page();
271 die();
274 if (empty($confirmupgrade)) {
275 $a = new stdClass();
276 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
277 $a->newversion = "$release (".sprintf('%.2f', $version).")";
278 $strdatabasechecking = get_string('databasechecking', '', $a);
280 $PAGE->set_title($stradministration);
281 $PAGE->set_heading($strdatabasechecking);
282 $PAGE->set_cacheable(false);
284 /** @var core_admin_renderer $output */
285 $output = $PAGE->get_renderer('core', 'admin');
286 echo $output->upgrade_confirm_page($a->newversion, $maturity);
287 die();
289 } else if (empty($confirmrelease)){
290 require_once($CFG->libdir.'/environmentlib.php');
291 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
292 $strcurrentrelease = get_string('currentrelease');
294 $PAGE->navbar->add($strcurrentrelease);
295 $PAGE->set_title($strcurrentrelease);
296 $PAGE->set_heading($strcurrentrelease);
297 $PAGE->set_cacheable(false);
299 /** @var core_admin_renderer $output */
300 $output = $PAGE->get_renderer('core', 'admin');
301 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
302 die();
304 } else if (empty($confirmplugins)) {
305 $strplugincheck = get_string('plugincheck');
307 $PAGE->navbar->add($strplugincheck);
308 $PAGE->set_title($strplugincheck);
309 $PAGE->set_heading($strplugincheck);
310 $PAGE->set_cacheable(false);
312 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
314 /** @var core_admin_renderer $output */
315 $output = $PAGE->get_renderer('core', 'admin');
317 // check plugin dependencies first
318 $failed = array();
319 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
320 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
321 die();
323 unset($failed);
325 if ($fetchupdates) {
326 // no sesskey support guaranteed here
327 if (empty($CFG->disableupdatenotifications)) {
328 available_update_checker::instance()->fetch();
330 redirect($reloadurl);
333 $deployer = available_update_deployer::instance();
334 if ($deployer->enabled()) {
335 $deployer->initialize($reloadurl, $reloadurl);
337 $deploydata = $deployer->submitted_data();
338 if (!empty($deploydata)) {
339 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
340 die();
344 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
345 $version, $showallplugins, $reloadurl,
346 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
347 die();
349 } else {
350 // Launch main upgrade
351 upgrade_core($version, true);
353 } else if ($version < $CFG->version) {
354 // better stop here, we can not continue with plugin upgrades or anything else
355 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
358 // Updated human-readable release version if necessary
359 if (!$cache and $release <> $CFG->release) { // Update the release version
360 set_config('release', $release);
363 if (!$cache and $branch <> $CFG->branch) { // Update the branch
364 set_config('branch', $branch);
367 if (!$cache and moodle_needs_upgrading()) {
368 if (!$PAGE->headerprinted) {
369 // means core upgrade or installation was not already done
370 if (!$confirmplugins) {
371 $strplugincheck = get_string('plugincheck');
373 $PAGE->set_pagelayout('maintenance');
374 $PAGE->set_popup_notification_allowed(false);
375 $PAGE->navbar->add($strplugincheck);
376 $PAGE->set_title($strplugincheck);
377 $PAGE->set_heading($strplugincheck);
378 $PAGE->set_cacheable(false);
380 if ($fetchupdates) {
381 // no sesskey support guaranteed here
382 available_update_checker::instance()->fetch();
383 redirect($PAGE->url);
386 $output = $PAGE->get_renderer('core', 'admin');
388 $deployer = available_update_deployer::instance();
389 if ($deployer->enabled()) {
390 $deployer->initialize($PAGE->url, $PAGE->url);
392 $deploydata = $deployer->submitted_data();
393 if (!empty($deploydata)) {
394 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
395 die();
399 // check plugin dependencies first
400 $failed = array();
401 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
402 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
403 die();
405 unset($failed);
407 // dependencies check passed, let's rock!
408 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
409 $version, $showallplugins,
410 new moodle_url($PAGE->url),
411 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
412 die();
415 // install/upgrade all plugins and other parts
416 upgrade_noncore(true);
419 // If this is the first install, indicate that this site is fully configured
420 // except the admin password
421 if (during_initial_install()) {
422 set_config('rolesactive', 1); // after this, during_initial_install will return false.
423 set_config('adminsetuppending', 1);
424 // we need this redirect to setup proper session
425 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
428 // make sure admin user is created - this is the last step because we need
429 // session to be working properly in order to edit admin account
430 if (!empty($CFG->adminsetuppending)) {
431 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
432 if (!$sessionstarted) {
433 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
434 } else {
435 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
436 if (!$sessionverify) {
437 $SESSION->sessionverify = 1;
438 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
439 } else {
440 if (empty($SESSION->sessionverify)) {
441 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
443 unset($SESSION->sessionverify);
447 // Cleanup SESSION to make sure other code does not complain in the future.
448 unset($SESSION->has_timed_out);
449 unset($SESSION->wantsurl);
451 // 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
452 $adminids = explode(',', $CFG->siteadmins);
453 $adminuser = get_complete_user_data('id', reset($adminids));
455 if ($adminuser->password === 'adminsetuppending') {
456 // prevent installation hijacking
457 if ($adminuser->lastip !== getremoteaddr()) {
458 print_error('installhijacked', 'admin');
460 // login user and let him set password and admin details
461 $adminuser->newadminuser = 1;
462 complete_user_login($adminuser);
463 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
465 } else {
466 unset_config('adminsetuppending');
469 } else {
470 // just make sure upgrade logging is properly terminated
471 upgrade_finished('upgradesettings.php');
474 // Now we can be sure everything was upgraded and caches work fine,
475 // redirect if necessary to make sure caching is enabled.
476 if (!$cache) {
477 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
480 // Check for valid admin user - no guest autologin
481 require_login(0, false);
482 $context = context_system::instance();
483 require_capability('moodle/site:config', $context);
485 // check that site is properly customized
486 $site = get_site();
487 if (empty($site->shortname)) {
488 // probably new installation - lets return to frontpage after this step
489 // remove settings that we want uninitialised
490 unset_config('registerauth');
491 redirect('upgradesettings.php?return=site');
494 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
495 if (!empty($id) and $id == $CFG->siteidentifier) {
496 set_config('registered', time());
499 // setup critical warnings before printing admin tree block
500 $insecuredataroot = is_dataroot_insecure(true);
501 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
503 $adminroot = admin_get_root();
505 // Check if there are any new admin settings which have still yet to be set
506 if (any_new_admin_settings($adminroot)){
507 redirect('upgradesettings.php');
510 // Everything should now be set up, and the user is an admin
512 // Print default admin page with notifications.
513 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
515 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
516 $cronoverdue = ($lastcron < time() - 3600 * 24);
517 $dbproblems = $DB->diagnose();
518 $maintenancemode = !empty($CFG->maintenance_enabled);
520 // Available updates for Moodle core
521 $updateschecker = available_update_checker::instance();
522 $availableupdates = array();
523 $availableupdates['core'] = $updateschecker->get_update_info('core',
524 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
526 // Available updates for contributed plugins
527 $pluginman = plugin_manager::instance();
528 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
529 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
530 if (!empty($plugininfo->availableupdates)) {
531 foreach ($plugininfo->availableupdates as $pluginavailableupdate) {
532 if ($pluginavailableupdate->version > $plugininfo->versiondisk) {
533 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
534 $availableupdates[$plugintype.'_'.$pluginname] = array();
536 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
543 // The timestamp of the most recent check for available updates
544 $availableupdatesfetch = $updateschecker->get_last_timefetched();
546 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
547 //check if the site is registered on Moodle.org
548 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
550 admin_externalpage_setup('adminnotifications');
552 if ($fetchupdates) {
553 require_sesskey();
554 $updateschecker->fetch();
555 redirect(new moodle_url('/admin/index.php'));
558 $output = $PAGE->get_renderer('core', 'admin');
559 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
560 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
561 $registered);