3 /// Check that config.php exists, if not then call the install script
4 if (!file_exists('../config.php')) {
5 header('Location: ../install.php');
9 /// Check that PHP is of a sufficient version
10 /// Moved here because older versions do not allow while(@ob_end_clean());
11 if (version_compare(phpversion(), "4.3.0") < 0) {
12 $phpversion = phpversion();
13 echo "Sorry, Moodle requires PHP 4.3.0 or later (currently using version $phpversion)";
17 /// Turn off time limits and try to flush everything all the time, sometimes upgrades can be slow.
20 @ob_implicit_flush
(true);
21 while(@ob_end_clean
()); // ob_end_flush prevents sending of headers
24 require_once('../config.php');
25 require_once($CFG->libdir
.'/adminlib.php'); // Contains various admin-only functions
26 require_once($CFG->libdir
.'/ddllib.php'); // Install/upgrade related db functions
28 $id = optional_param('id', '', PARAM_ALPHANUM
);
29 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL
);
30 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL
);
31 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL
);
32 $autopilot = optional_param('autopilot', 0, PARAM_BOOL
);
33 $ignoreupgradewarning = optional_param('ignoreupgradewarning', 0, PARAM_BOOL
);
35 /// check upgrade status first
36 if ($ignoreupgradewarning and !empty($_SESSION['upgraderunning'])) {
37 $_SESSION['upgraderunning'] = 0;
39 upgrade_check_running("Upgrade already running in this session, please wait!<br />Click on the exclamation marks to ignore this warning (<a href=\"index.php?ignoreupgradewarning=1\">!!!</a>).", 10);
41 /// set install/upgrade autocontinue session flag
43 $_SESSION['installautopilot'] = $autopilot;
46 /// Check some PHP server settings
48 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
50 if (ini_get_bool('session.auto_start')) {
51 error("The PHP server variable 'session.auto_start' should be Off - $documentationlink");
54 if (ini_get_bool('magic_quotes_runtime')) {
55 error("The PHP server variable 'magic_quotes_runtime' should be Off - $documentationlink");
58 if (!ini_get_bool('file_uploads')) {
59 error("The PHP server variable 'file_uploads' is not turned On - $documentationlink");
62 if (empty($CFG->prefix
) && $CFG->dbfamily
!= 'mysql') { //Enforce prefixes for everybody but mysql
63 error('$CFG->prefix can\'t be empty for your target DB (' . $CFG->dbtype
. ')');
66 if ($CFG->dbfamily
== 'oracle' && strlen($CFG->prefix
) > 2) { //Max prefix length for Oracle is 2cc
67 error('$CFG->prefix maximum allowed length for Oracle DBs is 2cc.');
70 /// Check that config.php has been edited
72 if ($CFG->wwwroot
== "http://example.com/moodle") {
73 error("Moodle has not been configured yet. You need to edit config.php first.");
77 /// Check settings in config.php
79 $dirroot = dirname(realpath("../index.php"));
80 if (!empty($dirroot) and $dirroot != $CFG->dirroot
) {
81 error("Please fix your settings in config.php:
83 <p>\$CFG->dirroot = \"".addslashes($CFG->dirroot
)."\";
85 <p>\$CFG->dirroot = \"".addslashes($dirroot)."\";",
89 /// Set some necessary variables during set-up to avoid PHP warnings later on this page
90 if (!isset($CFG->framename
)) {
91 $CFG->framename
= "_top";
93 if (!isset($CFG->release
)) {
96 if (!isset($CFG->version
)) {
100 if (is_readable("$CFG->dirroot/version.php")) {
101 include_once("$CFG->dirroot/version.php"); # defines $version
104 if (!$version or !$release) {
105 error('Main version.php was not readable or specified');# without version, stop
108 /// Check if the main tables have been installed yet or not.
110 if (! $tables = $db->Metatables() ) { // No tables yet at all.
113 } else { // Check for missing main tables
115 $mtables = array("config", "course", "course_categories", "course_modules",
116 "course_sections", "log", "log_display", "modules",
118 foreach ($mtables as $mtable) {
119 if (!in_array($CFG->prefix
.$mtable, $tables)) {
126 /// hide errors from headers in case debug enabled in config.php
127 $origdebug = $CFG->debug
;
128 $CFG->debug
= DEBUG_MINIMAL
;
129 error_reporting($CFG->debug
);
130 if (empty($agreelicense)) {
131 $strlicense = get_string('license');
132 print_header($strlicense, $strlicense, $strlicense, "", "", false, " ", " ");
133 print_heading("<a href=\"http://moodle.org\">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment");
134 print_heading(get_string('copyrightnotice'));
135 print_box(text_to_html(get_string('gpl')), 'copyrightnotice');
137 notice_yesno(get_string('doyouagree'), "index.php?agreelicense=1",
138 "http://docs.moodle.org/en/License");
139 print_footer('none');
142 if (empty($confirmrelease)) {
143 $strcurrentrelease = get_string("currentrelease");
144 print_header($strcurrentrelease, $strcurrentrelease, $strcurrentrelease, "", "", false, " ", " ");
145 print_heading("Moodle $release");
146 print_box(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes'), 'generalbox boxaligncenter boxwidthwide');
147 echo '<form action="index.php"><div>';
148 echo '<input type="hidden" name="agreelicense" value="1" />';
149 echo '<input type="hidden" name="confirmrelease" value="1" />';
151 echo '<div class="continuebutton"><input name="autopilot" id="autopilot" type="checkbox" value="1" /><label for="autopilot">'.get_string('unattendedoperation', 'admin').'</label>';
152 echo '<br /><br /><input type="submit" value="'.get_string('continue').'" /></div>';
154 print_footer('none');
158 $strdatabasesetup = get_string("databasesetup");
159 $strdatabasesuccess = get_string("databasesuccess");
160 print_header($strdatabasesetup, $strdatabasesetup, $strdatabasesetup,
161 "", upgrade_get_javascript(), false, " ", " ");
162 /// return to original debugging level
163 $CFG->debug
= $origdebug;
164 error_reporting($CFG->debug
);
168 /// Both old .sql files and new install.xml are supported
169 /// But we prioritise install.xml (XMLDB) if present
171 change_db_encoding(); // first try to change db encoding to utf8
172 if (!setup_is_unicodedb()) {
173 // If could not convert successfully, throw error, and prevent installation
174 print_error('unicoderequired', 'admin');
178 if (file_exists("$CFG->libdir/db/install.xml")) {
179 $status = install_from_xmldb_file("$CFG->libdir/db/install.xml"); //New method
180 } else if (file_exists("$CFG->libdir/db/$CFG->dbtype.sql")) {
181 $status = modify_database("$CFG->libdir/db/$CFG->dbtype.sql"); //Old method
183 error("Error: Your database ($CFG->dbtype) is not yet fully supported by Moodle or install.xml is not present. See the lib/db directory.");
186 // all new installs are in unicode - keep for backwards compatibility and 1.8 upgrade checks
187 set_config('unicodedb', 1);
189 /// Continue with the instalation
192 // Install the roles system.
193 moodle_install_roles();
194 set_config('statsrolesupgraded',time());
196 // Write default settings unconditionally (i.e. even if a setting is already set, overwrite it)
197 // (this should only have any effect during initial install).
198 $adminroot = admin_get_root();
199 $adminroot->prune('backups'); // backup settings table not created yet
200 apply_default_settings($adminroot);
202 /// This is used to handle any settings that must exist in $CFG but which do not exist in
203 /// admin_get_root()/$ADMIN as admin_setting objects (there are some exceptions).
204 apply_default_exception_settings(array('alternateloginurl' => '',
206 'auth_pop3mailbox' => 'INBOX',
207 'changepassword' => '',
209 'enrol_plugins_enabled' => 'manual',
210 'guestloginbutton' => 1,
211 'registerauth' => 'email',
212 'style' => 'default',
213 'template' => 'default',
214 'theme' => 'standardwhite',
215 'filter_multilang_converted' => 1));
217 notify($strdatabasesuccess, "green");
218 require_once $CFG->dirroot
.'/mnet/lib.php';
220 error("Error: Main databases NOT set up successfully");
222 print_continue('index.php');
223 print_footer('none');
228 /// Check version of Moodle code on disk compared with database
229 /// and upgrade if possible.
231 if (file_exists("$CFG->dirroot/lib/db/$CFG->dbtype.php")) {
232 include_once("$CFG->dirroot/lib/db/$CFG->dbtype.php"); # defines old upgrades
234 if (file_exists("$CFG->dirroot/lib/db/upgrade.php")) {
235 include_once("$CFG->dirroot/lib/db/upgrade.php"); # defines new upgrades
238 $stradministration = get_string("administration");
241 if ($version > $CFG->version
) { // upgrade
243 /// If the database is not already Unicode then we do not allow upgrading!
244 /// Instead, we print an error telling them to upgrade to 1.7 first. MDL-6857
245 if (empty($CFG->unicodedb
)) {
246 print_error('unicodeupgradeerror', 'error', '', $version);
249 $a->oldversion
= "$CFG->release ($CFG->version)";
250 $a->newversion
= "$release ($version)";
251 $strdatabasechecking = get_string("databasechecking", "", $a);
253 // hide errors from headers in case debug is enabled
254 $origdebug = $CFG->debug
;
255 $CFG->debug
= DEBUG_MINIMAL
;
256 error_reporting($CFG->debug
);
258 // logout in case we are upgrading from pre 1.7 version - prevention of weird session problems
259 if ($CFG->version
< 2006050600) {
263 if (empty($confirmupgrade)) {
264 print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
265 "", "", false, " ", " ");
267 notice_yesno(get_string('upgradesure', 'admin', $a->newversion
), 'index.php?confirmupgrade=1', 'index.php');
270 } else if (empty($confirmrelease)){
271 $strcurrentrelease = get_string("currentrelease");
272 print_header($strcurrentrelease, $strcurrentrelease, $strcurrentrelease, "", "", false, " ", " ");
273 print_heading("Moodle $release");
274 print_box(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes'));
276 require_once($CFG->libdir
.'/environmentlib.php');
277 print_heading(get_string('environment', 'admin'));
278 if (!check_moodle_environment($release, $environment_results, true)) {
279 notice_yesno(get_string('environmenterrorupgrade', 'admin'),
280 'index.php?confirmupgrade=1&confirmrelease=1', 'index.php');
282 notify(get_string('environmentok', 'admin'), 'notifysuccess');
284 echo '<form action="index.php"><div>';
285 echo '<input type="hidden" name="confirmupgrade" value="1" />';
286 echo '<input type="hidden" name="confirmrelease" value="1" />';
288 echo '<div class="continuebutton"><input name="autopilot" id="autopilot" type="checkbox" value="0" /><label for="autopilot">'.get_string('unattendedoperation', 'admin').'</label>';
289 echo '<br /><br /><input type="submit" value="'.get_string('continue').'" /></div>';
293 print_footer('none');
296 $strdatabasesuccess = get_string("databasesuccess");
297 print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
298 "", upgrade_get_javascript(), false, " ", " ");
300 /// return to original debugging level
301 $CFG->debug
= $origdebug;
302 error_reporting($CFG->debug
);
305 /// Upgrade current language pack if we can
306 upgrade_language_pack();
308 print_heading($strdatabasechecking);
310 /// Launch the old main upgrade (if exists)
312 if (function_exists('main_upgrade')) {
313 $status = main_upgrade($CFG->version
);
315 /// If succesful and exists launch the new main upgrade (XMLDB), called xmldb_main_upgrade
316 if ($status && function_exists('xmldb_main_upgrade')) {
317 $status = xmldb_main_upgrade($CFG->version
);
320 /// If successful, continue upgrading roles and setting everything properly
322 if (empty($CFG->rolesactive
)) {
323 // Upgrade to the roles system.
324 moodle_install_roles();
325 set_config('rolesactive', 1);
326 } else if (!update_capabilities()) {
327 error('Had trouble upgrading the core capabilities for the Roles System');
329 require_once($CFG->libdir
.'/statslib.php');
330 if (!stats_upgrade_for_roles_wrapper()) {
331 notify('Couldn\'t upgrade the stats tables to use the new roles system');
333 if (set_config("version", $version)) {
334 remove_dir($CFG->dataroot
. '/cache', true); // flush cache
335 notify($strdatabasesuccess, "green");
336 print_continue("upgradesettings.php");
337 print_footer('none');
340 error('Upgrade failed! (Could not update version in config table)');
342 /// Main upgrade not success
344 notify('Main Upgrade failed! See lib/db/upgrade.php');
345 print_continue('index.php?confirmupgrade=1&confirmrelease=1');
346 print_footer('none');
349 upgrade_log_finish();
351 } else if ($version < $CFG->version
) {
353 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
354 upgrade_log_finish();
357 if (!set_config("version", $version)) {
358 error("A problem occurred inserting current version into databases");
362 /// Updated human-readable release version if necessary
364 if ($release <> $CFG->release
) { // Update the release version
365 if (!set_config("release", $release)) {
366 error("ERROR: Could not update release version in database!!");
370 /// Find and check all main modules and load them up or upgrade them if necessary
371 /// first old *.php update and then the new upgrade.php script
372 upgrade_activity_modules("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
374 /// Check all questiontype plugins and upgrade if necessary
375 /// first old *.php update and then the new upgrade.php script
376 /// It is important that this is done AFTER the quiz module has been upgraded
377 upgrade_plugins('qtype', 'question/type', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
379 /// Upgrade backup/restore system if necessary
380 /// first old *.php update and then the new upgrade.php script
381 require_once("$CFG->dirroot/backup/lib.php");
382 upgrade_backup_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
384 /// Upgrade blocks system if necessary
385 /// first old *.php update and then the new upgrade.php script
386 require_once("$CFG->dirroot/lib/blocklib.php");
387 upgrade_blocks_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
389 /// Check all blocks and load (or upgrade them if necessary)
390 /// first old *.php update and then the new upgrade.php script
391 upgrade_blocks_plugins("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
393 /// Check all enrolment plugins and upgrade if necessary
394 /// first old *.php update and then the new upgrade.php script
395 upgrade_plugins('enrol', 'enrol', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
397 /// Check all course formats and upgrade if necessary
398 upgrade_plugins('format','course/format',"$CFG->wwwroot/$CFG->admin/index.php");
400 /// Check for local database customisations
401 /// first old *.php update and then the new upgrade.php script
402 require_once("$CFG->dirroot/lib/locallib.php");
403 upgrade_local_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
405 /// Check for new groups and upgrade if necessary. TODO:
406 require_once("$CFG->dirroot/group/db/upgrade.php");
407 upgrade_group_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
409 /// Check for changes to RPC functions
410 require_once($CFG->dirroot
.'/admin/mnet/adminlib.php');
411 upgrade_RPC_functions("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
414 /// just make sure upgrade logging is properly terminated
415 upgrade_log_finish();
417 unset($_SESSION['installautopilot']);
419 /// Set up the blank site - to be customized later at the end of install.
420 if (! $site = get_site()) {
421 // We are about to create the site "course"
422 require_once($CFG->libdir
.'/blocklib.php');
424 $newsite = new Object();
425 $newsite->fullname
= "";
426 $newsite->shortname
= "";
427 $newsite->summary
= "";
428 $newsite->newsitems
= 3;
429 $newsite->numsections
= 0;
430 $newsite->category
= 0;
431 $newsite->format
= 'site'; // Only for this course
432 $newsite->teacher
= get_string("defaultcourseteacher");
433 $newsite->teachers
= get_string("defaultcourseteachers");
434 $newsite->student
= get_string("defaultcoursestudent");
435 $newsite->students
= get_string("defaultcoursestudents");
436 $newsite->timemodified
= time();
438 if ($newid = insert_record('course', $newsite)) {
439 // Site created, add blocks for it
440 $page = page_create_object(PAGE_COURSE_VIEW
, $newid);
441 blocks_repopulate_page($page); // Return value not checked because you can always edit later
444 $cat->name
= get_string('miscellaneous');
445 if (insert_record('course_categories', $cat)) {
446 redirect('index.php');
448 error("Serious Error! Could not set up a default course category!");
451 error("Serious Error! Could not set up the site!");
455 // initialise default blocks on admin and site page if needed
456 if (empty($CFG->adminblocks_initialised
)) {
457 require_once("$CFG->dirroot/$CFG->admin/pagelib.php");
458 require_once($CFG->libdir
.'/blocklib.php');
459 page_map_class(PAGE_ADMIN
, 'page_admin');
460 $page = page_create_object(PAGE_ADMIN
, 0); // there must be some id number
461 blocks_repopulate_page($page);
463 //add admin_tree block to site if not already present
464 if ($admintree = get_record('block', 'name', 'admin_tree')) {
465 $page = page_create_object(PAGE_COURSE_VIEW
, SITEID
);
466 blocks_execute_action($page, blocks_get_by_page($page), 'add', (int)$admintree->id
, false, false);
467 if ($admintreeinstance = get_record('block_instance', 'pagetype', $page->type
, 'pageid', SITEID
, 'blockid', $admintree->id
)) {
468 blocks_execute_action($page, blocks_get_by_page($page), 'moveleft', $admintreeinstance, false, false);
472 set_config('adminblocks_initialised', 1);
475 /// Define the unique site ID code if it isn't already
476 if (empty($CFG->siteidentifier
)) { // Unique site identification code
477 set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']);
480 /// Check if the guest user exists. If not, create one.
481 if (! record_exists("user", "username", "guest")) {
482 $guest->auth
= "manual";
483 $guest->username
= "guest";
484 $guest->password
= md5("guest");
485 $guest->firstname
= addslashes(get_string("guestuser"));
486 $guest->lastname
= " ";
487 $guest->email
= "root@localhost";
488 $guest->description
= addslashes(get_string("guestuserinfo"));
489 $guest->mnethostid
= $CFG->mnet_localhost_id
;
490 $guest->confirmed
= 1;
491 $guest->lang
= $CFG->lang
;
492 $guest->timemodified
= time();
494 if (! $guest->id
= insert_record("user", $guest)) {
495 notify("Could not create guest user record !!!");
500 /// Set up the admin user
501 if (empty($CFG->rolesactive
)) {
505 /// Check for valid admin user
508 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
510 require_capability('moodle/site:config', $context);
512 /// check that site is properly customized
513 if (empty($site->shortname
) or empty($site->shortname
)) {
514 redirect('settings.php?section=frontpagesettings&return=site');
517 /// Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
520 if ($id == $CFG->siteidentifier
) {
521 set_config('registered', time());
525 /// Everything should now be set up, and the user is an admin
527 /// Print default admin page with notifications.
529 $adminroot = admin_get_root();
530 admin_externalpage_setup('adminnotifications', $adminroot);
531 admin_externalpage_print_header($adminroot);
533 /// Deprecated database! Warning!!
534 if (!empty($CFG->migrated_to_new_db
)) {
535 print_box(print_string('dbmigrationdeprecateddb', 'admin'), 'generalbox adminwarning');
538 /// Check for any special upgrades that might need to be run
539 if (!empty($CFG->upgrade
)) {
540 print_box(get_string("upgrade$CFG->upgrade", "admin", "$CFG->wwwroot/$CFG->admin/upgrade$CFG->upgrade.php"));
543 if (ini_get_bool('register_globals') && !ini_get_bool('magic_quotes_gpc')) {
544 print_box(get_string('globalsquoteswarning', 'admin'), 'generalbox adminwarning');
547 if (is_dataroot_insecure()) {
548 print_box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot
), 'generalbox adminwarning');
551 /// If no recently cron run
552 $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix
. 'modules');
553 if (time() - $lastcron > 3600 * 24) {
554 $strinstallation = get_string('installation', 'install');
555 $helpbutton = helpbutton('install', $strinstallation, 'moodle', true, false, '', true);
556 print_box(get_string('cronwarning', 'admin')." ".$helpbutton, 'generalbox adminwarning');
559 /// Print multilang upgrade notice if needed
560 if (empty($CFG->filter_multilang_converted
)) {
561 print_box(get_string('multilangupgradenotice', 'admin'), 'generalbox adminwarning');
564 /// Alert if we are currently in maintenance mode
565 if (file_exists($CFG->dataroot
.'/1/maintenance.html')) {
566 print_box(get_string('sitemaintenancewarning', 'admin'), 'generalbox adminwarning');
570 /// Print slightly annoying registration button every six months ;-)
571 /// You can set the "registered" variable to something far in the future
572 /// if you really want to prevent this. eg 9999999999
573 if (!isset($CFG->registered
) ||
$CFG->registered
< (time() - 3600*24*30*6)) {
575 $options['sesskey'] = $USER->sesskey
;
576 print_box_start('generalbox adminwarning');
577 print_string('pleaseregister', 'admin');
578 print_single_button('register.php', $options, get_string('registration'));
580 $registrationbuttonshown = true;
583 //////////////////////////////////////////////////////////////////////////////////////////////////
584 //// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
585 $copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
586 '<a href="http://docs.moodle.org/en/Release">'.$CFG->release
.'</a> ('.$CFG->version
.')<br />'.
587 'Copyright © 1999 onwards, Martin Dougiamas<br />'.
588 'and <a href="http://docs.moodle.org/en/Credits">many other contributors</a>.<br />'.
589 '<a href="http://docs.moodle.org/en/License">GNU Public License</a>';
590 print_box($copyrighttext, 'copyright');
591 //////////////////////////////////////////////////////////////////////////////////////////////////
594 if (empty($registrationbuttonshown)) {
596 $options['sesskey'] = $USER->sesskey
;
597 print_single_button('register.php', $options, get_string('registration'));
601 if (optional_param('dbmigrate')) { // ??? Is this actually used?
603 require_once($CFG->dirroot
.'/'.$CFG->admin
.'/utfdbmigrate.php');
609 admin_externalpage_print_footer($adminroot);