MDL-15249 refactoring towards cli install script
[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 *
21 * @package moodlecore
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
39a5a35d 26/// Check that config.php exists, if not then call the install script
045e9e24 27 if (!file_exists('../config.php')) {
39a5a35d 28 header('Location: ../install.php');
d98228e4 29 die;
30 }
249ab745 31
ccba465e 32/// Check that PHP is of a sufficient version as soon as possible
33 if (version_compare(phpversion(), "5.2.0") < 0) {
249ab745 34 $phpversion = phpversion();
ccba465e 35 // do NOT localise - lang strings would not work here and we CAN not move it to later palce
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.";
249ab745 38 die;
39 }
40
775f811a 41/// try to flush everything all the time
249ab745 42 @ob_implicit_flush(true);
43 while(@ob_end_clean()); // ob_end_flush prevents sending of headers
44
45
775f811a 46 require('../config.php');
47 require_once($CFG->libdir.'/adminlib.php'); // Contains various admin-only functions
db9d4a3d 48 require_once($CFG->libdir.'/upgradelib.php');
4d00e5e4 49
b27b1473 50 $id = optional_param('id', '', PARAM_TEXT);
db5af934 51 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
52 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
53 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
54 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
371a32e3 55
d686bf50 56/// Check some PHP server settings
57
95056a03 58 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
d686bf50 59
c39c66a5 60 if (ini_get_bool('session.auto_start')) {
775f811a 61 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
d686bf50 62 }
c39c66a5 63
64 if (ini_get_bool('magic_quotes_runtime')) {
775f811a 65 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
d686bf50 66 }
67
c39c66a5 68 if (!ini_get_bool('file_uploads')) {
775f811a 69 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
d686bf50 70 }
71
61f9c4b4 72 if (is_float_problem()) {
73 print_error('phpfloatproblem', 'admin', '', $documentationlink);
74 }
75
d98228e4 76/// Check settings in config.php
77
cf3eb7c3 78 $dirroot = dirname(realpath("../index.php"));
c454b86c 79 /// Check correct dirroot, ignoring slashes (though should be always forward slashes). MDL-18195
80 if (!empty($dirroot) and str_replace('\\', '/', $dirroot) != str_replace('\\', '/', $CFG->dirroot)) {
a4ac30c0 81 print_error('fixsetting', 'debug', '', (object)array('current'=>$CFG->dirroot, 'found'=>str_replace('\\', '/', $dirroot)));
d98228e4 82 }
83
1dd24519 84/// Set some necessary variables during set-up to avoid PHP warnings later on this page
85 if (!isset($CFG->framename)) {
86 $CFG->framename = "_top";
87 }
88 if (!isset($CFG->release)) {
89 $CFG->release = "";
90 }
91 if (!isset($CFG->version)) {
92 $CFG->version = "";
93 }
94
775f811a 95 $version = null;
96 $release = null;
c20ce874 97 require("$CFG->dirroot/version.php"); // defines $version and $release
9ace5094 98 $CFG->target_release = $release; // used during installation and upgrades
d02bc6ce 99
100 if (!$version or !$release) {
78664df0 101 print_error('withoutversion', 'debug'); // without version, stop
d02bc6ce 102 }
103
db5af934 104 /// Check if the main tables have been installed yet or not.
105 if (!$tables = $DB->get_tables() ) { // No tables yet at all.
106 $maintables = false;
107
108 } else { // Check for missing main tables
109 $maintables = true;
110 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
111 foreach ($mtables as $mtable) {
112 if (!in_array($mtable, $tables)) {
113 $maintables = false;
114 break;
115 }
116 }
117 unset($mtables);
118 }
119 unset($tables);
120
5c144d60 121 // Turn off xmlstrictheaders during upgrade.
122 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
123 $CFG->xmlstrictheaders = false;
124
db5af934 125 if (!$maintables) {
126 /// hide errors from headers in case debug enabled in config.php
35d6a2a4 127
90509582 128 /// fake some settings
129 $CFG->docroot = 'http://docs.moodle.org';
130
844d58f7 131 $strinstallation = get_string('installation', 'install');
132
35d6a2a4 133 /// remove current session content completely
134 session_get_instance()->terminate_current();
135
db5af934 136 if (empty($agreelicense)) {
137 $strlicense = get_string('license');
138 $navigation = build_navigation(array(array('name'=>$strlicense, 'link'=>null, 'type'=>'misc')));
9ace5094 139 print_header($strinstallation.' - Moodle '.$CFG->target_release, $strinstallation, $navigation, "", "", false, "&nbsp;", "&nbsp;");
db5af934 140 print_heading("<a href=\"http://moodle.org\">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment");
141 print_heading(get_string('copyrightnotice'));
712b48cc 142 $copyrightnotice = text_to_html(get_string('gpl'));
143 $copyrightnotice = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $copyrightnotice); // extremely ugly validation hack
144 print_box($copyrightnotice, 'copyrightnotice');
db5af934 145 echo "<br />";
08ff9f81 146 notice_yesno(get_string('doyouagree'), "index.php?agreelicense=1&lang=$CFG->lang",
db5af934 147 "http://docs.moodle.org/en/License");
9ace5094 148 print_footer('upgrade');
90509582 149 die;
db5af934 150 }
151 if (empty($confirmrelease)) {
152 $strcurrentrelease = get_string("currentrelease");
153 $navigation = build_navigation(array(array('name'=>$strcurrentrelease, 'link'=>null, 'type'=>'misc')));
9ace5094 154 print_header($strinstallation.' - Moodle '.$CFG->target_release, $strinstallation, $navigation, "", "", false, "&nbsp;", "&nbsp;");
db5af934 155 print_heading("Moodle $release");
90509582 156 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
157 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
158 print_box($releasenoteslink, 'generalbox boxaligncenter boxwidthwide');
159
160 require_once($CFG->libdir.'/environmentlib.php');
493c3651 161 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
9ace5094 162 print_upgrade_reload("index.php?agreelicense=1&amp;lang=$CFG->lang");
90509582 163 } else {
164 notify(get_string('environmentok', 'admin'), 'notifysuccess');
9ace5094 165 print_continue("index.php?agreelicense=1&amp;confirmrelease=1&amp;lang=$CFG->lang");
90509582 166 }
167
9ace5094 168 print_footer('upgrade');
db5af934 169 die;
170 }
171
844d58f7 172 $strdatabasesetup = get_string("databasesetup");
db5af934 173 $navigation = build_navigation(array(array('name'=>$strdatabasesetup, 'link'=>null, 'type'=>'misc')));
9ace5094 174 print_header($strinstallation.' - Moodle '.$CFG->target_release, $strinstallation, $navigation, "", upgrade_get_javascript(), false, "&nbsp;", "&nbsp;");
db5af934 175
db5af934 176 if (!$DB->setup_is_unicodedb()) {
177 if (!$DB->change_db_encoding()) {
178 // If could not convert successfully, throw error, and prevent installation
179 print_error('unicoderequired', 'admin');
180 }
181 }
182
8580535b 183 install_core($version, true);
db5af934 184 }
185
186
187/// Check version of Moodle code on disk compared with database
188/// and upgrade if possible.
189
190 $stradministration = get_string('administration');
e88462a0 191 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
db5af934 192
193 if (empty($CFG->version)) {
194 print_error('missingconfigversion', 'debug');
195 }
196
197 if ($version > $CFG->version) { // upgrade
198 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
199 require_once($CFG->libdir.'/db/upgradelib.php'); // Upgrade-related functions
200
201 $a->oldversion = "$CFG->release ($CFG->version)";
202 $a->newversion = "$release ($version)";
203 $strdatabasechecking = get_string("databasechecking", "", $a);
204
db5af934 205 if (empty($confirmupgrade)) {
206 $navigation = build_navigation(array(array('name'=>$strdatabasechecking, 'link'=>null, 'type'=>'misc')));
795a08ad 207 print_header($strdatabasechecking, $stradministration, $navigation, "", "", false, "&nbsp;", "&nbsp;");
db5af934 208
209 notice_yesno(get_string('upgradesure', 'admin', $a->newversion), 'index.php?confirmupgrade=1', 'index.php');
9ace5094 210 print_footer('upgrade');
db5af934 211 exit;
212
213 } else if (empty($confirmrelease)){
214 $strcurrentrelease = get_string("currentrelease");
215 $navigation = build_navigation(array(array('name'=>$strcurrentrelease, 'link'=>null, 'type'=>'misc')));
216 print_header($strcurrentrelease, $strcurrentrelease, $navigation, "", "", false, "&nbsp;", "&nbsp;");
217 print_heading("Moodle $release");
90509582 218 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
219 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
220 print_box($releasenoteslink);
db5af934 221
222 require_once($CFG->libdir.'/environmentlib.php');
493c3651 223 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
90509582 224 print_upgrade_reload('index.php?confirmupgrade=1');
db5af934 225 } else {
226 notify(get_string('environmentok', 'admin'), 'notifysuccess');
227 if (empty($CFG->skiplangupgrade)) {
90509582 228 print_box_start('generalbox', 'notice');
db5af934 229 print_string('langpackwillbeupdated', 'admin');
230 print_box_end();
231 }
9ace5094 232 print_continue('index.php?confirmupgrade=1&amp;confirmrelease=1');
db5af934 233 }
234
9ace5094 235 print_footer('upgrade');
db5af934 236 die;
237
238 } elseif (empty($confirmplugins)) {
239 $strplugincheck = get_string('plugincheck');
240 $navigation = build_navigation(array(array('name'=>$strplugincheck, 'link'=>null, 'type'=>'misc')));
241 print_header($strplugincheck, $strplugincheck, $navigation, "", "", false, "&nbsp;", "&nbsp;");
242 print_heading($strplugincheck);
90509582 243 print_box_start('generalbox', 'notice');
db5af934 244 print_string('pluginchecknotice');
245 print_box_end();
246 print_plugin_tables();
90509582 247 print_upgrade_reload('index.php?confirmupgrade=1&amp;confirmrelease=1');
cbe2a956 248 print_continue('index.php?confirmupgrade=1&amp;confirmrelease=1&amp;confirmplugincheck=1');
db5af934 249 print_footer('none');
250 die();
251
252 } else {
8580535b 253 // Launch main upgrade
254 upgrade_core($version, true);
db5af934 255 }
256 } else if ($version < $CFG->version) {
257 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
258 }
259
260/// Updated human-readable release version if necessary
db5af934 261 if ($release <> $CFG->release) { // Update the release version
013376de 262 set_config("release", $release);
db5af934 263 }
264
8580535b 265 // upgrade all plugins and other parts
266 upgrade_noncore(true);
db5af934 267
88582df4 268/// indicate that this site is fully configured except the admin password
269 if (empty($CFG->rolesactive)) {
270 set_config('rolesactive', 1);
271 set_config('adminsetuppending', 1);
272 // we neeed this redirect to setup proper session
9ace5094 273 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
88582df4 274 }
275
db5af934 276/// make sure admin user is created - this is the last step because we need
277/// session to be working properly in order to edit admin account
88582df4 278 if (!empty($CFG->adminsetuppending)) {
35d6a2a4 279 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
280 if (!$sessionstarted) {
88582df4 281 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
282 } else {
283 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
284 if (!$sessionverify) {
285 $SESSION->sessionverify = 1;
286 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
287 } else {
288 if (empty($SESSION->sessionverify)) {
289 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
290 }
291 unset($SESSION->sessionverify);
292 }
293 }
294
295 $adminuser = get_complete_user_data('username', 'admin');
296
297 if ($adminuser->password === 'adminsetuppending') {
298 // prevent installation hijacking
299 if ($adminuser->lastip !== getremoteaddr()) {
300 print_error('installhijacked', 'admin');
301 }
302 // login user and let him set password and admin details
303 $adminuser->newadminuser = 1;
304 message_set_default_message_preferences($adminuser);
305 complete_user_login($adminuser, false);
306 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
307
308 } else {
309 unset_config('adminsetuppending');
35d6a2a4 310 }
db5af934 311
5c144d60 312 } else {
313 /// just make sure upgrade logging is properly terminated
0cb93a7e 314 upgrade_finished('upgradesettings.php');
db5af934 315 }
aa893d6b 316
88582df4 317// Turn xmlstrictheaders back on now.
5c144d60 318 $CFG->xmlstrictheaders = $origxmlstrictheaders;
319 unset($origxmlstrictheaders);
320
cb0e9fbd 321/// Check for valid admin user - no guest autologin
322 require_login(0, false);
957f6fc9 323 $context = get_context_instance(CONTEXT_SYSTEM);
f2e21e6c 324 require_capability('moodle/site:config', $context);
325
e0f6e995 326/// check that site is properly customized
3bfb1f59 327 $site = get_site();
8e5da17a 328 if (empty($site->shortname)) {
329 // probably new installation - lets return to frontpage after this step
e9180ff8 330 // remove settings that we want uninitialised
331 unset_config('registerauth');
8e5da17a 332 redirect('upgradesettings.php?return=site');
e0f6e995 333 }
f9903ed0 334
a2a3c590 335/// Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
db5af934 336 if (!empty($id) and $id == $CFG->siteidentifier) {
337 set_config('registered', time());
a2a3c590 338 }
339
bba0beae 340/// setup critical warnings before printing admin tree block
341 $insecuredataroot = is_dataroot_insecure(true);
bba0beae 342
fbf2c91e 343 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
bba0beae 344
db5af934 345 $adminroot = admin_get_root();
2acc1f96 346
347/// Check if there are any new admin settings which have still yet to be set
220a90c5 348 if (any_new_admin_settings($adminroot)){
2acc1f96 349 redirect('upgradesettings.php');
350 }
351
6e0993ee 352/// Everything should now be set up, and the user is an admin
f9903ed0 353
6e0993ee 354/// Print default admin page with notifications.
90a73bb3 355
1ae083e4 356 admin_externalpage_setup('adminnotifications');
357 admin_externalpage_print_header();
6e0993ee 358
cb8229d0 359/// Deprecated database! Warning!!
360 if (!empty($CFG->migrated_to_new_db)) {
4ed13ed8 361 print_box(print_string('dbmigrationdeprecateddb', 'admin'), 'generalbox adminwarning');
cb8229d0 362 }
363
6e0993ee 364/// Check for any special upgrades that might need to be run
eef868d1 365 if (!empty($CFG->upgrade)) {
0a8d5d77 366 print_box(get_string("upgrade$CFG->upgrade", "admin", "$CFG->wwwroot/$CFG->admin/upgrade$CFG->upgrade.php"));
4da1a0a1 367 }
368
bba0beae 369 if ($insecuredataroot == INSECURE_DATAROOT_WARNING) {
4ed13ed8 370 print_box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot), 'generalbox adminwarning');
bba0beae 371 } else if ($insecuredataroot == INSECURE_DATAROOT_ERROR) {
372 print_box(get_string('datarootsecurityerror', 'admin', $CFG->dataroot), 'generalbox adminerror');
b9c639d6 373
57e35f32 374 }
375
b3732604 376 if (defined('WARN_DISPLAY_ERRORS_ENABLED')) {
377 print_box(get_string('displayerrorswarning', 'admin'), 'generalbox adminwarning');
378 }
379
35de9e24 380/// If no recently cron run
a5d424df 381 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
35de9e24 382 if (time() - $lastcron > 3600 * 24) {
0301965a 383 $strinstallation = get_string('installation', 'install');
384 $helpbutton = helpbutton('install', $strinstallation, 'moodle', true, false, '', true);
4ed13ed8 385 print_box(get_string('cronwarning', 'admin')."&nbsp;".$helpbutton, 'generalbox adminwarning');
35de9e24 386 }
387
f16242ce 388/// Print multilang upgrade notice if needed
389 if (empty($CFG->filter_multilang_converted)) {
4ed13ed8 390 print_box(get_string('multilangupgradenotice', 'admin'), 'generalbox adminwarning');
f16242ce 391 }
392
1695b680 393/// Alert if we are currently in maintenance mode
394 if (file_exists($CFG->dataroot.'/1/maintenance.html')) {
4ed13ed8 395 print_box(get_string('sitemaintenancewarning', 'admin'), 'generalbox adminwarning');
799ce77d 396 }
397
1695b680 398
c15421f2 399 //////////////////////////////////////////////////////////////////////////////////////////////////
e4d81c60 400 //// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
e58e1a94 401 $copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
8cd94820 402 '<a href="http://docs.moodle.org/en/Release" title="'.$CFG->version.'">'.$CFG->release.'</a><br />'.
c4d044b5 403 'Copyright &copy; 1999 onwards, Martin Dougiamas<br />'.
e4d81c60 404 'and <a href="http://docs.moodle.org/en/Credits">many other contributors</a>.<br />'.
c4d044b5 405 '<a href="http://docs.moodle.org/en/License">GNU Public License</a>';
0a8d5d77 406 print_box($copyrighttext, 'copyright');
c15421f2 407 //////////////////////////////////////////////////////////////////////////////////////////////////
55e4b5f9 408
1ae083e4 409 admin_externalpage_print_footer();
74944b73 410