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