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");
141 if (empty($confirmrelease)) {
142 $strcurrentrelease = get_string("currentrelease");
143 print_header($strcurrentrelease, $strcurrentrelease, $strcurrentrelease, "", "", false, " ", " ");
144 print_heading("Moodle $release");
145 print_box(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes'));
146 echo '<form action="index.php"><fieldset class="invisiblefieldset">';
147 echo '<input type="hidden" name="agreelicense" value="1" />';
148 echo '<input type="hidden" name="confirmrelease" value="1" />';
150 echo '<div class="continuebutton"><input name="autopilot" id="autopilot" type="checkbox" value="1" /><label for="autopilot">'.get_string('unattendedoperation', 'admin').'</label>';
151 echo '<br /><br /><input type="submit" value="'.get_string('continue').'" /></div>';
153 print_footer('none');
157 $strdatabasesetup = get_string("databasesetup");
158 $strdatabasesuccess = get_string("databasesuccess");
159 print_header($strdatabasesetup, $strdatabasesetup, $strdatabasesetup,
160 "", upgrade_get_javascript(), false, " ", " ");
161 /// return to original debugging level
162 $CFG->debug = $origdebug;
163 error_reporting($CFG->debug);
167 /// Both old .sql files and new install.xml are supported
168 /// But we prioritise install.xml (XMLDB) if present
170 change_db_encoding(); // first try to change db encoding to utf8
171 if (!setup_is_unicodedb()) {
172 // If could not convert successfully, throw error, and prevent installation
173 print_error('unicoderequired', 'admin');
177 if (file_exists("$CFG->libdir/db/install.xml")) {
178 $status = install_from_xmldb_file("$CFG->libdir/db/install.xml"); //New method
179 } else if (file_exists("$CFG->libdir/db/$CFG->dbtype.sql")) {
180 $status = modify_database("$CFG->libdir/db/$CFG->dbtype.sql"); //Old method
182 error("Error: Your database ($CFG->dbtype) is not yet fully supported by Moodle or install.xml is not present. See the lib/db directory.");
185 // all new installs are in unicode - keep for backwards compatibility and 1.8 upgrade checks
186 set_config('unicodedb', 1);
188 /// Continue with the instalation
191 // Install the roles system.
192 moodle_install_roles();
193 set_config('statsrolesupgraded',time());
195 // Write default settings unconditionally (i.e. even if a setting is already set, overwrite it)
196 // (this should only have any effect during initial install).
197 $adminroot = admin_get_root();
198 $adminroot->prune('backups'); // backup settings table not created yet
199 apply_default_settings($adminroot);
201 /// This is used to handle any settings that must exist in $CFG but which do not exist in
202 /// admin_get_root()/$ADMIN as admin_setting objects (there are some exceptions).
203 apply_default_exception_settings(array('alternateloginurl' => '',
205 'auth_pop3mailbox' => 'INBOX',
206 'changepassword' => '',
208 'enrol_plugins_enabled' => 'manual',
209 'guestloginbutton' => 1,
210 'style' => 'default',
211 'template' => 'default',
212 'theme' => 'standardwhite',
213 'filter_multilang_converted' => 1));
215 notify($strdatabasesuccess, "green");
216 require_once $CFG->dirroot.'/mnet/lib.php';
218 error("Error: Main databases NOT set up successfully");
220 print_continue('index.php');
221 print_footer('none');
226 /// Check version of Moodle code on disk compared with database
227 /// and upgrade if possible.
229 if (file_exists("$CFG->dirroot/lib/db/$CFG->dbtype.php")) {
230 include_once("$CFG->dirroot/lib/db/$CFG->dbtype.php"); # defines old upgrades
232 if (file_exists("$CFG->dirroot/lib/db/upgrade.php")) {
233 include_once("$CFG->dirroot/lib/db/upgrade.php"); # defines new upgrades
236 $stradministration = get_string("administration");
239 if ($version > $CFG->version) { // upgrade
241 /// If the database is not already Unicode then we do not allow upgrading!
242 /// Instead, we print an error telling them to upgrade to 1.7 first. MDL-6857
243 if (empty($CFG->unicodedb)) {
244 print_error('unicodeupgradeerror', 'error', '', $version);
247 $a->oldversion = "$CFG->release ($CFG->version)";
248 $a->newversion = "$release ($version)";
249 $strdatabasechecking = get_string("databasechecking", "", $a);
251 // hide errors from headers in case debug is enabled
252 $origdebug = $CFG->debug;
253 $CFG->debug = DEBUG_MINIMAL;
254 error_reporting($CFG->debug);
256 // logout in case we are upgrading from pre 1.7 version - prevention of weird session problems
257 if ($CFG->version < 2006050600) {
261 if (empty($confirmupgrade)) {
262 print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
263 "", "", false, " ", " ");
265 notice_yesno(get_string('upgradesure', 'admin', $a->newversion), 'index.php?confirmupgrade=1', 'index.php');
268 } else if (empty($confirmrelease)){
269 $strcurrentrelease = get_string("currentrelease");
270 print_header($strcurrentrelease, $strcurrentrelease, $strcurrentrelease, "", "", false, " ", " ");
271 print_heading("Moodle $release");
272 print_box(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes'));
273 echo '<form action="index.php"><fieldset class="invisiblefieldset">';
274 echo '<input type="hidden" name="confirmupgrade" value="1" />';
275 echo '<input type="hidden" name="confirmrelease" value="1" />';
277 echo '<div class="continuebutton"><input name="autopilot" id="autopilot" type="checkbox" value="0" /><label for="autopilot">'.get_string('unattendedoperation', 'admin').'</label>';
278 echo '<br /><br /><input type="submit" value="'.get_string('continue').'" /></div>';
280 print_footer('none');
283 $strdatabasesuccess = get_string("databasesuccess");
284 print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
285 "", upgrade_get_javascript(), false, " ", " ");
287 /// return to original debugging level
288 $CFG->debug = $origdebug;
289 error_reporting($CFG->debug);
292 /// Upgrade current language pack if we can
293 upgrade_language_pack();
295 print_heading($strdatabasechecking);
297 /// Launch the old main upgrade (if exists)
299 if (function_exists('main_upgrade')) {
300 $status = main_upgrade($CFG->version);
302 /// If succesful and exists launch the new main upgrade (XMLDB), called xmldb_main_upgrade
303 if ($status && function_exists('xmldb_main_upgrade')) {
304 $status = xmldb_main_upgrade($CFG->version);
307 /// If successful, continue upgrading roles and setting everything properly
309 if (empty($CFG->rolesactive)) {
310 // Upgrade to the roles system.
311 moodle_install_roles();
312 set_config('rolesactive', 1);
313 } else if (!update_capabilities()) {
314 error('Had trouble upgrading the core capabilities for the Roles System');
316 require_once($CFG->libdir.'/statslib.php');
317 if (!stats_upgrade_for_roles_wrapper()) {
318 notify('Couldn\'t upgrade the stats tables to use the new roles system');
320 if (set_config("version", $version)) {
321 remove_dir($CFG->dataroot . '/cache', true); // flush cache
322 notify($strdatabasesuccess, "green");
323 print_continue("upgradesettings.php");
324 print_footer('none');
327 error('Upgrade failed! (Could not update version in config table)');
329 /// Main upgrade not success
331 notify('Main Upgrade failed! See lib/db/upgrade.php');
332 print_continue('index.php?confirmupgrade=1&confirmrelease=1');
333 print_footer('none');
336 upgrade_log_finish();
338 } else if ($version < $CFG->version) {
340 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
341 upgrade_log_finish();
344 if (!set_config("version", $version)) {
345 error("A problem occurred inserting current version into databases");
349 /// Updated human-readable release version if necessary
351 if ($release <> $CFG->release) { // Update the release version
352 if (!set_config("release", $release)) {
353 error("ERROR: Could not update release version in database!!");
357 /// Find and check all main modules and load them up or upgrade them if necessary
358 /// first old *.php update and then the new upgrade.php script
359 upgrade_activity_modules("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
361 /// Check all questiontype plugins and upgrade if necessary
362 /// first old *.php update and then the new upgrade.php script
363 /// It is important that this is done AFTER the quiz module has been upgraded
364 upgrade_plugins('qtype', 'question/type', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
366 /// Upgrade backup/restore system if necessary
367 /// first old *.php update and then the new upgrade.php script
368 require_once("$CFG->dirroot/backup/lib.php");
369 upgrade_backup_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
371 /// Upgrade blocks system if necessary
372 /// first old *.php update and then the new upgrade.php script
373 require_once("$CFG->dirroot/lib/blocklib.php");
374 upgrade_blocks_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
376 /// Check all blocks and load (or upgrade them if necessary)
377 /// first old *.php update and then the new upgrade.php script
378 upgrade_blocks_plugins("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
380 /// Check all enrolment plugins and upgrade if necessary
381 /// first old *.php update and then the new upgrade.php script
382 upgrade_plugins('enrol', 'enrol', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
384 /// Check all course formats and upgrade if necessary
385 upgrade_plugins('format','course/format',"$CFG->wwwroot/$CFG->admin/index.php");
387 /// Check for local database customisations
388 /// first old *.php update and then the new upgrade.php script
389 require_once("$CFG->dirroot/lib/locallib.php");
390 upgrade_local_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
392 /// Check for new groups and upgrade if necessary. TODO:
393 require_once("$CFG->dirroot/group/db/upgrade.php");
394 upgrade_group_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
396 /// Check for changes to RPC functions
397 require_once($CFG->dirroot.'/admin/mnet/adminlib.php');
398 upgrade_RPC_functions("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
401 /// just make sure upgrade logging is properly terminated
402 upgrade_log_finish();
404 unset($_SESSION['installautopilot']);
406 /// Set up the blank site - to be customized later at the end of install.
407 if (! $site = get_site()) {
408 // We are about to create the site "course"
409 require_once($CFG->libdir.'/blocklib.php');
411 $newsite = new Object();
412 $newsite->fullname = "";
413 $newsite->shortname = "";
414 $newsite->summary = "";
415 $newsite->newsitems = 3;
416 $newsite->numsections = 0;
417 $newsite->category = 0;
418 $newsite->format = 'site'; // Only for this course
419 $newsite->teacher = get_string("defaultcourseteacher");
420 $newsite->teachers = get_string("defaultcourseteachers");
421 $newsite->student = get_string("defaultcoursestudent");
422 $newsite->students = get_string("defaultcoursestudents");
423 $newsite->timemodified = time();
425 if ($newid = insert_record('course', $newsite)) {
426 // Site created, add blocks for it
427 $page = page_create_object(PAGE_COURSE_VIEW, $newid);
428 blocks_repopulate_page($page); // Return value not checked because you can always edit later
431 $cat->name = get_string('miscellaneous');
432 if (insert_record('course_categories', $cat)) {
433 redirect('index.php');
435 error("Serious Error! Could not set up a default course category!");
438 error("Serious Error! Could not set up the site!");
442 // initialise default blocks on admin and site page if needed
443 if (empty($CFG->adminblocks_initialised)) {
444 require_once("$CFG->dirroot/$CFG->admin/pagelib.php");
445 require_once($CFG->libdir.'/blocklib.php');
446 page_map_class(PAGE_ADMIN, 'page_admin');
447 $page = page_create_object(PAGE_ADMIN, 0); // there must be some id number
448 blocks_repopulate_page($page);
450 //add admin_tree block to site if not already present
451 if ($admintree = get_record('block', 'name', 'admin_tree')) {
452 $page = page_create_object(PAGE_COURSE_VIEW, SITEID);
453 blocks_execute_action($page, blocks_get_by_page($page), 'add', (int)$admintree->id, false, false);
454 if ($admintreeinstance = get_record('block_instance', 'pagetype', $page->type, 'pageid', SITEID, 'blockid', $admintree->id)) {
455 blocks_execute_action($page, blocks_get_by_page($page), 'moveleft', $admintreeinstance, false, false);
459 set_config('adminblocks_initialised', 1);
462 /// Define the unique site ID code if it isn't already
463 if (empty($CFG->siteidentifier)) { // Unique site identification code
464 set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']);
467 /// Check if the guest user exists. If not, create one.
468 if (! record_exists("user", "username", "guest")) {
469 $guest->auth = "manual";
470 $guest->username = "guest";
471 $guest->password = md5("guest");
472 $guest->firstname = addslashes(get_string("guestuser"));
473 $guest->lastname = " ";
474 $guest->email = "root@localhost";
475 $guest->description = addslashes(get_string("guestuserinfo"));
476 $guest->mnethostid = $CFG->mnet_localhost_id;
477 $guest->confirmed = 1;
478 $guest->lang = $CFG->lang;
479 $guest->timemodified= time();
481 if (! $guest->id = insert_record("user", $guest)) {
482 notify("Could not create guest user record !!!");
487 /// Set up the admin user
488 if (empty($CFG->rolesactive)) {
492 /// Check for valid admin user
495 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
497 require_capability('moodle/site:config', $context);
499 /// check that site is properly customized
500 if (empty($site->shortname) or empty($site->shortname)) {
501 redirect('settings.php?section=frontpage&return=site');
504 /// Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
507 if ($id == $CFG->siteidentifier) {
508 set_config('registered', time());
512 /// Everything should now be set up, and the user is an admin
514 /// Print default admin page with notifications.
516 $adminroot = admin_get_root();
517 admin_externalpage_setup('adminnotifications', $adminroot);
518 admin_externalpage_print_header($adminroot);
520 /// Deprecated database! Warning!!
521 if (!empty($CFG->migrated_to_new_db)) {
522 print_box(print_string('dbmigrationdeprecateddb','admin'));
525 /// Check for any special upgrades that might need to be run
526 if (!empty($CFG->upgrade)) {
527 print_box(get_string("upgrade$CFG->upgrade", "admin", "$CFG->wwwroot/$CFG->admin/upgrade$CFG->upgrade.php"));
530 if (ini_get_bool('register_globals') && !ini_get_bool('magic_quotes_gpc')) {
531 print_box(get_string('globalsquoteswarning', 'admin'));
534 if (is_dataroot_insecure()) {
535 print_box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot));
538 /// If no recently cron run
539 $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix . 'modules');
540 if (time() - $lastcron > 3600 * 24) {
541 $strinstallation = get_string('installation', 'install');
542 $helpbutton = helpbutton('install', $strinstallation, 'moodle', true, false, '', true);
543 print_box(get_string('cronwarning', 'admin')." ".$helpbutton);
546 /// Print multilang upgrade notice if needed
547 if (empty($CFG->filter_multilang_converted)) {
548 print_box(get_string('multilangupgradenotice', 'admin'));
551 /// Alert if we are currently in maintenance mode
552 if (file_exists($CFG->dataroot.'/1/maintenance.html')) {
553 print_box(get_string('sitemaintenancewarning', 'admin'));
557 /// Print slightly annoying registration button every six months ;-)
558 /// You can set the "registered" variable to something far in the future
559 /// if you really want to prevent this. eg 9999999999
560 if (!isset($CFG->registered) || $CFG->registered < (time() - 3600*24*30*6)) {
562 $options['sesskey'] = $USER->sesskey;
564 print_string('pleaseregister', 'admin');
565 print_single_button('register.php', $options, get_string('registration'));
567 $registrationbuttonshown = true;
570 //////////////////////////////////////////////////////////////////////////////////////////////////
571 //// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
572 $copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
573 '<a href="http://docs.moodle.org/en/Release">'.$CFG->release.'</a> ('.$CFG->version.')<br />'.
574 'Copyright © 1999 onwards, Martin Dougiamas<br />'.
575 'and <a href="http://docs.moodle.org/en/Credits">many other contributors</a>.<br />'.
576 '<a href="http://docs.moodle.org/en/License">GNU Public License</a>';
577 print_box($copyrighttext, 'copyright');
578 //////////////////////////////////////////////////////////////////////////////////////////////////
581 if (empty($registrationbuttonshown)) {
583 $options['sesskey'] = $USER->sesskey;
584 print_single_button('register.php', $options, get_string('registration'));
588 if (optional_param('dbmigrate')) { // ??? Is this actually used?
590 require_once($CFG->dirroot.'/'.$CFG->admin.'/utfdbmigrate.php');
596 admin_externalpage_print_footer($adminroot);