MDL-24368 prevent infinite loops when caching can not be disabled
[moodle.git] / admin / index.php
CommitLineData
8580535b 1<?php
2
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.
14//
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/>.
17
18/**
19 * Main administration script.
20 *
d078f6d3 21 * @package core
8580535b 22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
f9903ed0 25
00be1916 26// Check that config.php exists, if not then call the install script
27if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
29 die;
30}
31
32// Check that PHP is of a sufficient version as soon as possible
bc76b3c0 33if (version_compare(phpversion(), '5.2.0') < 0) {
00be1916 34 $phpversion = phpversion();
35 // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
36 echo "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion). ";
37 echo "Please upgrade your server software or use latest Moodle 1.9.x instead.";
38 die;
39}
40
41// try to flush everything all the time
42@ob_implicit_flush(true);
ebba1698 43while(ob_get_level()) {
9384419a
PS
44 if (!ob_end_clean()) {
45 // prevent infinite loop
46 break;
47 }
ebba1698 48}
00be1916 49
50require('../config.php');
bc76b3c0 51require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
52require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
00be1916 53
54$id = optional_param('id', '', PARAM_TEXT);
55$confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
56$confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
57$confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
58$agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
59
60// Check some PHP server settings
61
a6855934 62$PAGE->set_url('/admin/index.php');
ee73b1ff 63
00be1916 64$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
65
66if (ini_get_bool('session.auto_start')) {
67 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
68}
69
70if (ini_get_bool('magic_quotes_runtime')) {
71 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
72}
73
74if (!ini_get_bool('file_uploads')) {
75 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
76}
77
78if (is_float_problem()) {
79 print_error('phpfloatproblem', 'admin', '', $documentationlink);
80}
81
00be1916 82// Set some necessary variables during set-up to avoid PHP warnings later on this page
83if (!isset($CFG->framename)) {
bc76b3c0 84 $CFG->framename = '_top';
00be1916 85}
86if (!isset($CFG->release)) {
bc76b3c0 87 $CFG->release = '';
00be1916 88}
89if (!isset($CFG->version)) {
bc76b3c0 90 $CFG->version = '';
00be1916 91}
92
93$version = null;
94$release = null;
95require("$CFG->dirroot/version.php"); // defines $version and $release
96$CFG->target_release = $release; // used during installation and upgrades
97
98if (!$version or !$release) {
99 print_error('withoutversion', 'debug'); // without version, stop
100}
101
00be1916 102// Turn off xmlstrictheaders during upgrade.
103$origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
104$CFG->xmlstrictheaders = false;
105
3316fe24 106if (!core_tables_exist()) {
78946b9b 107 $PAGE->set_pagelayout('maintenance');
00be1916 108
109 // fake some settings
110 $CFG->docroot = 'http://docs.moodle.org';
111
112 $strinstallation = get_string('installation', 'install');
113
114 // remove current session content completely
115 session_get_instance()->terminate_current();
116
117 if (empty($agreelicense)) {
118 $strlicense = get_string('license');
69d77c23 119 $PAGE->navbar->add($strlicense);
120 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
121 $PAGE->set_heading($strinstallation);
122 $PAGE->set_cacheable(false);
123 echo $OUTPUT->header();
2fff8846 124 echo $OUTPUT->heading('<a href="http://moodle.org">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment');
125 echo $OUTPUT->heading(get_string('copyrightnotice'));
852a1f66 126 $copyrightnotice = text_to_html(get_string('gpl3'));
00be1916 127 $copyrightnotice = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $copyrightnotice); // extremely ugly validation hack
20486a5a 128 echo $OUTPUT->box($copyrightnotice, 'copyrightnotice');
bc76b3c0 129 echo '<br />';
66f3df17
PS
130 $continue = new single_button(new moodle_url('/admin/index.php', array('lang'=>$CFG->lang, 'agreelicense'=>1)), get_string('continue'), 'get');
131 echo $OUTPUT->confirm(get_string('doyouagree'), $continue, "http://docs.moodle.org/en/License");
73d6f52f 132 echo $OUTPUT->footer();
249ab745 133 die;
134 }
00be1916 135 if (empty($confirmrelease)) {
bc76b3c0 136 $strcurrentrelease = get_string('currentrelease');
69d77c23 137 $PAGE->navbar->add($strcurrentrelease);
138 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
139 $PAGE->set_heading($strinstallation);
140 $PAGE->set_cacheable(false);
141 echo $OUTPUT->header();
2fff8846 142 echo $OUTPUT->heading("Moodle $release");
00be1916 143 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
144 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
20486a5a 145 echo $OUTPUT->box($releasenoteslink, 'generalbox boxaligncenter boxwidthwide');
00be1916 146
147 require_once($CFG->libdir.'/environmentlib.php');
148 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
149 print_upgrade_reload("index.php?agreelicense=1&amp;lang=$CFG->lang");
150 } else {
8fbce1c8 151 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
66f3df17 152 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php', array('agreelicense'=>1, 'confirmrelease'=>1, 'lang'=>$CFG->lang)));
00be1916 153 }
249ab745 154
73d6f52f 155 echo $OUTPUT->footer();
00be1916 156 die;
1dd24519 157 }
158
bc76b3c0 159 $strdatabasesetup = get_string('databasesetup');
543f54d3 160 upgrade_init_javascript();
69d77c23 161 $PAGE->navbar->add($strdatabasesetup);
162 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
2ea05146 163 $PAGE->set_heading($strinstallation);
69d77c23 164 $PAGE->set_cacheable(false);
165 echo $OUTPUT->header();
d02bc6ce 166
00be1916 167 if (!$DB->setup_is_unicodedb()) {
168 if (!$DB->change_db_encoding()) {
169 // If could not convert successfully, throw error, and prevent installation
170 print_error('unicoderequired', 'admin');
db5af934 171 }
db5af934 172 }
90509582 173
00be1916 174 install_core($version, true);
175}
db5af934 176
db5af934 177
00be1916 178// Check version of Moodle code on disk compared with database
179// and upgrade if possible.
db5af934 180
00be1916 181$stradministration = get_string('administration');
182$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
db5af934 183
00be1916 184if (empty($CFG->version)) {
185 print_error('missingconfigversion', 'debug');
186}
db5af934 187
00be1916 188if ($version > $CFG->version) { // upgrade
78946b9b 189 $PAGE->set_pagelayout('maintenance');
d4a03c00 190
00be1916 191 $a->oldversion = "$CFG->release ($CFG->version)";
192 $a->newversion = "$release ($version)";
bc76b3c0 193 $strdatabasechecking = get_string('databasechecking', '', $a);
db5af934 194
00be1916 195 if (empty($confirmupgrade)) {
69d77c23 196 $PAGE->navbar->add($strdatabasechecking);
197 $PAGE->set_title($strdatabasechecking);
52273536 198 $PAGE->set_heading($stradministration);
69d77c23 199 $PAGE->set_cacheable(false);
200 echo $OUTPUT->header();
8a138093
PS
201 $continueurl = new moodle_url('index.php', array('confirmupgrade' => 1));
202 $cancelurl = new moodle_url('index.php');
203 echo $OUTPUT->confirm(get_string('upgradesure', 'admin', $a->newversion), $continueurl, $cancelurl);
73d6f52f 204 echo $OUTPUT->footer();
00be1916 205 exit;
db5af934 206
00be1916 207 } else if (empty($confirmrelease)){
bc76b3c0 208 $strcurrentrelease = get_string('currentrelease');
69d77c23 209 $PAGE->navbar->add($strcurrentrelease);
210 $PAGE->set_title($strcurrentrelease);
52273536 211 $PAGE->set_heading($strcurrentrelease);
69d77c23 212 $PAGE->set_cacheable(false);
213 echo $OUTPUT->header();
2fff8846 214 echo $OUTPUT->heading("Moodle $release");
00be1916 215 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
216 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
20486a5a 217 echo $OUTPUT->box($releasenoteslink);
db5af934 218
00be1916 219 require_once($CFG->libdir.'/environmentlib.php');
220 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
221 print_upgrade_reload('index.php?confirmupgrade=1');
db5af934 222 } else {
8fbce1c8 223 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
00be1916 224 if (empty($CFG->skiplangupgrade)) {
20486a5a 225 echo $OUTPUT->box_start('generalbox', 'notice');
00be1916 226 print_string('langpackwillbeupdated', 'admin');
20486a5a 227 echo $OUTPUT->box_end();
00be1916 228 }
8fbce1c8 229 echo $OUTPUT->continue_button('index.php?confirmupgrade=1&confirmrelease=1');
db5af934 230 }
db5af934 231
73d6f52f 232 echo $OUTPUT->footer();
00be1916 233 die;
db5af934 234
00be1916 235 } elseif (empty($confirmplugins)) {
236 $strplugincheck = get_string('plugincheck');
69d77c23 237 $PAGE->navbar->add($strplugincheck);
238 $PAGE->set_title($strplugincheck);
52273536 239 $PAGE->set_heading($strplugincheck);
69d77c23 240 $PAGE->set_cacheable(false);
241 echo $OUTPUT->header();
2fff8846 242 echo $OUTPUT->heading($strplugincheck);
20486a5a 243 echo $OUTPUT->box_start('generalbox', 'notice');
00be1916 244 print_string('pluginchecknotice');
20486a5a 245 echo $OUTPUT->box_end();
00be1916 246 print_plugin_tables();
247 print_upgrade_reload('index.php?confirmupgrade=1&amp;confirmrelease=1');
575f245f
PS
248 $button = new single_button(new moodle_url('index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
249 $button->class = 'continuebutton';
250 echo $OUTPUT->render($button);
73d6f52f 251 echo $OUTPUT->footer();
00be1916 252 die();
db5af934 253
00be1916 254 } else {
255 // Launch main upgrade
256 upgrade_core($version, true);
88582df4 257 }
00be1916 258} else if ($version < $CFG->version) {
80380bd7
PS
259 // better stop here, we can not continue with plugin upgrades or anything else
260 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
00be1916 261}
262
263// Updated human-readable release version if necessary
264if ($release <> $CFG->release) { // Update the release version
bc76b3c0 265 set_config('release', $release);
00be1916 266}
267
575f245f
PS
268if (moodle_needs_upgrading()) {
269 if (!$PAGE->headerprinted) {
2361b7dd 270 // means core upgrade or installation was not already done
575f245f 271 if (!$confirmplugins) {
2361b7dd 272 $PAGE->set_pagelayout('maintenance');
575f245f
PS
273 $strplugincheck = get_string('plugincheck');
274 $PAGE->navbar->add($strplugincheck);
275 $PAGE->set_title($strplugincheck);
276 $PAGE->set_heading($strplugincheck);
277 $PAGE->set_cacheable(false);
278 echo $OUTPUT->header();
279 echo $OUTPUT->heading($strplugincheck);
280 echo $OUTPUT->box_start('generalbox', 'notice');
281 print_string('pluginchecknotice');
282 echo $OUTPUT->box_end();
283 print_plugin_tables();
284 print_upgrade_reload('index.php');
285 $button = new single_button(new moodle_url('index.php', array('confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
286 $button->class = 'continuebutton';
287 echo $OUTPUT->render($button);
288 echo $OUTPUT->footer();
289 die();
290 }
291 }
292 // install/upgrade all plugins and other parts
293 upgrade_noncore(true);
294}
00be1916 295
31a99877 296// If this is the first install, indicate that this site is fully configured
297// except the admin password
298if (during_initial_install()) {
299 set_config('rolesactive', 1); // after this, during_initial_install will return false.
00be1916 300 set_config('adminsetuppending', 1);
c8ae55e9 301 // we need this redirect to setup proper session
00be1916 302 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
303}
304
305// make sure admin user is created - this is the last step because we need
306// session to be working properly in order to edit admin account
307 if (!empty($CFG->adminsetuppending)) {
308 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
309 if (!$sessionstarted) {
310 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
311 } else {
312 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
313 if (!$sessionverify) {
314 $SESSION->sessionverify = 1;
315 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
88582df4 316 } else {
00be1916 317 if (empty($SESSION->sessionverify)) {
318 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
88582df4 319 }
00be1916 320 unset($SESSION->sessionverify);
88582df4 321 }
00be1916 322 }
88582df4 323
c8ae55e9
PS
324 // at this stage there can be only one admin - users may change username, so do not rely on that
325 $adminuser = get_complete_user_data('id', $CFG->siteadmins);
88582df4 326
00be1916 327 if ($adminuser->password === 'adminsetuppending') {
328 // prevent installation hijacking
329 if ($adminuser->lastip !== getremoteaddr()) {
330 print_error('installhijacked', 'admin');
35d6a2a4 331 }
00be1916 332 // login user and let him set password and admin details
333 $adminuser->newadminuser = 1;
334 message_set_default_message_preferences($adminuser);
335 complete_user_login($adminuser, false);
336 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
db5af934 337
5c144d60 338 } else {
00be1916 339 unset_config('adminsetuppending');
4da1a0a1 340 }
341
00be1916 342} else {
343 // just make sure upgrade logging is properly terminated
344 upgrade_finished('upgradesettings.php');
345}
b3732604 346
00be1916 347// Turn xmlstrictheaders back on now.
348$CFG->xmlstrictheaders = $origxmlstrictheaders;
349unset($origxmlstrictheaders);
350
351// Check for valid admin user - no guest autologin
352require_login(0, false);
353$context = get_context_instance(CONTEXT_SYSTEM);
354require_capability('moodle/site:config', $context);
355
356// check that site is properly customized
357$site = get_site();
358if (empty($site->shortname)) {
359 // probably new installation - lets return to frontpage after this step
360 // remove settings that we want uninitialised
361 unset_config('registerauth');
362 redirect('upgradesettings.php?return=site');
363}
364
365// Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
366if (!empty($id) and $id == $CFG->siteidentifier) {
367 set_config('registered', time());
368}
369
370// setup critical warnings before printing admin tree block
bc76b3c0 371$insecuredataroot = is_dataroot_insecure(true);
00be1916 372$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
373
374$adminroot = admin_get_root();
375
376// Check if there are any new admin settings which have still yet to be set
377if (any_new_admin_settings($adminroot)){
378 redirect('upgradesettings.php');
379}
380
381// Everything should now be set up, and the user is an admin
382
383// Print default admin page with notifications.
00be1916 384admin_externalpage_setup('adminnotifications');
61ef8f9f 385echo $OUTPUT->header();
00be1916 386
00be1916 387if ($insecuredataroot == INSECURE_DATAROOT_WARNING) {
20486a5a 388 echo $OUTPUT->box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot), 'generalbox adminwarning');
00be1916 389} else if ($insecuredataroot == INSECURE_DATAROOT_ERROR) {
20486a5a 390 echo $OUTPUT->box(get_string('datarootsecurityerror', 'admin', $CFG->dataroot), 'generalbox adminerror');
00be1916 391
392}
393
394if (defined('WARN_DISPLAY_ERRORS_ENABLED')) {
20486a5a 395 echo $OUTPUT->box(get_string('displayerrorswarning', 'admin'), 'generalbox adminwarning');
00be1916 396}
397
398// If no recently cron run
399$lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
400if (time() - $lastcron > 3600 * 24) {
9047d80e 401 $helpbutton = $OUTPUT->help_icon('cron', 'admin');
20486a5a 402 echo $OUTPUT->box(get_string('cronwarning', 'admin').'&nbsp;'.$helpbutton, 'generalbox adminwarning');
00be1916 403}
404
1c7b8b93
NC
405// Hidden bloglevel upgrade
406$showbloglevelupgrade = ($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete);
407if ($showbloglevelupgrade) {
408 echo $OUTPUT->box(get_string('bloglevelupgradenotice', 'admin'), 'generalbox adminwarning');
409}
410
d35ece6c
PS
411// diagnose DB, especially the sloppy MyISAM tables
412$diagnose = $DB->diagnose();
413if ($diagnose !== NULL) {
414 echo $OUTPUT->box($diagnose, 'generalbox adminwarning');
415}
416
00be1916 417// Alert if we are currently in maintenance mode
4fe2250a 418if (!empty($CFG->maintenance_enabled)) {
20486a5a 419 echo $OUTPUT->box(get_string('sitemaintenancewarning2', 'admin', "$CFG->wwwroot/$CFG->admin/settings.php?section=maintenancemode"), 'generalbox adminwarning');
00be1916 420}
421
00be1916 422//////////////////////////////////////////////////////////////////////////////////////////////////
423//// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
424$copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
425 '<a href="http://docs.moodle.org/en/Release" title="'.$CFG->version.'">'.$CFG->release.'</a><br />'.
426 'Copyright &copy; 1999 onwards, Martin Dougiamas<br />'.
427 'and <a href="http://docs.moodle.org/en/Credits">many other contributors</a>.<br />'.
428 '<a href="http://docs.moodle.org/en/License">GNU Public License</a>';
20486a5a 429echo $OUTPUT->box($copyrighttext, 'copyright');
00be1916 430//////////////////////////////////////////////////////////////////////////////////////////////////
431
73d6f52f 432echo $OUTPUT->footer();
74944b73 433