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 $agreelicence = optional_param('agreelicence',0, PARAM_BOOL
);
31 $ignoreupgradewarning = optional_param('ignoreupgradewarning', 0, PARAM_BOOL
);
33 /// check upgrade status first
34 if ($ignoreupgradewarning and !empty($_SESSION['upgraderunning'])) {
35 $_SESSION['upgraderunning'] = 0;
37 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);
39 /// Check some PHP server settings
41 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
43 if (ini_get_bool('session.auto_start')) {
44 error("The PHP server variable 'session.auto_start' should be Off - $documentationlink");
47 if (ini_get_bool('magic_quotes_runtime')) {
48 error("The PHP server variable 'magic_quotes_runtime' should be Off - $documentationlink");
51 if (!ini_get_bool('file_uploads')) {
52 error("The PHP server variable 'file_uploads' is not turned On - $documentationlink");
55 if (empty($CFG->prefix
) && $CFG->dbtype
!= 'mysql') { //Enforce prefixes for everybody but mysql
56 error('$CFG->prefix can\'t be empty for your target DB (' . $CFG->dbtype
. ')');
59 if ($CFG->dbtype
== 'oci8po' && strlen($CFG->prefix
) > 2) { //Max prefix length for Oracle is 2cc
60 error('$CFG->prefix maximum allowed length for Oracle DBs is 2cc.');
63 /// Check that config.php has been edited
65 if ($CFG->wwwroot
== "http://example.com/moodle") {
66 error("Moodle has not been configured yet. You need to edit config.php first.");
70 /// Check settings in config.php
72 $dirroot = dirname(realpath("../index.php"));
73 if (!empty($dirroot) and $dirroot != $CFG->dirroot
) {
74 error("Please fix your settings in config.php:
76 <p>\$CFG->dirroot = \"".addslashes($CFG->dirroot
)."\";
78 <p>\$CFG->dirroot = \"".addslashes($dirroot)."\";",
82 /// Set some necessary variables during set-up to avoid PHP warnings later on this page
83 if (!isset($CFG->framename
)) {
84 $CFG->framename
= "_top";
86 if (!isset($CFG->release
)) {
89 if (!isset($CFG->version
)) {
93 /// Check if the main tables have been installed yet or not.
95 if (! $tables = $db->Metatables() ) { // No tables yet at all.
98 } else { // Check for missing main tables
100 $mtables = array("config", "course", "course_categories", "course_modules",
101 "course_sections", "log", "log_display", "modules",
103 foreach ($mtables as $mtable) {
104 if (!in_array($CFG->prefix
.$mtable, $tables)) {
111 $linktoscrolltoerrors = '<script type="text/javascript" src="' . $CFG->wwwroot
. '/lib/scroll_to_errors.js"></script>';
113 /// hide errors from headers in case debug enabled in config.php
114 $origdebug = $CFG->debug
;
115 $CFG->debug
= DEBUG_MINIMAL
;
116 error_reporting($CFG->debug
);
117 if (empty($agreelicence)) {
118 $strlicense = get_string("license");
119 print_header($strlicense, $strlicense, $strlicense, "", "", false, " ", " ");
120 print_heading("<a href=\"http://moodle.org\">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment");
121 print_heading(get_string("copyrightnotice"));
122 print_simple_box_start('center');
123 echo text_to_html(get_string("gpl"));
124 print_simple_box_end();
126 notice_yesno(get_string("doyouagree"), "index.php?agreelicence=true",
127 "http://docs.moodle.org/en/License");
131 $strdatabasesetup = get_string("databasesetup");
132 $strdatabasesuccess = get_string("databasesuccess");
133 print_header($strdatabasesetup, $strdatabasesetup, $strdatabasesetup,
134 "", $linktoscrolltoerrors, false, " ", " ");
135 /// return to original debugging level
136 $CFG->debug
= $origdebug;
137 error_reporting($CFG->debug
);
141 /// Both old .sql files and new install.xml are supported
142 /// But we prioritise install.xml (XMLDB) if present
144 change_db_encoding(); // first try to change db encoding to utf8
146 if (file_exists("$CFG->libdir/db/install.xml")) {
147 $status = install_from_xmldb_file("$CFG->libdir/db/install.xml"); //New method
148 } else if (file_exists("$CFG->libdir/db/$CFG->dbtype.sql")) {
149 $status = modify_database("$CFG->libdir/db/$CFG->dbtype.sql"); //Old method
151 error("Error: Your database ($CFG->dbtype) is not yet fully supported by Moodle or install.xml is not present. See the lib/db directory.");
154 /// Continue with the instalation
157 // Install the roles system.
158 moodle_install_roles();
159 set_config('statsrolesupgraded',time());
161 // Write default settings unconditionally (i.e. even if a setting is already set, overwrite it)
162 // (this should only have any effect during initial install).
163 $adminroot = admin_get_root();
164 $adminroot->prune('backups'); // backup settings table not created yet
165 apply_default_settings($adminroot);
167 /// This is used to handle any settings that must exist in $CFG but which do not exist in
168 /// admin_get_root()/$ADMIN as admin_setting objects (there are some exceptions).
169 apply_default_exception_settings(array('alternateloginurl' => '',
171 'auth_pop3mailbox' => 'INBOX',
172 'changepassword' => '',
174 'enrol_plugins_enabled' => 'manual',
175 'guestloginbutton' => 1,
176 'style' => 'default',
177 'template' => 'default',
178 'theme' => 'standardwhite'));
180 notify($strdatabasesuccess, "green");
182 error("Error: Main databases NOT set up successfully");
184 print_continue('index.php');
189 /// Check version of Moodle code on disk compared with database
190 /// and upgrade if possible.
192 if ( is_readable("$CFG->dirroot/version.php")) {
193 include_once("$CFG->dirroot/version.php"); # defines $version
196 error('Main version.php was not readable or specified');# without version, stop
199 if (file_exists("$CFG->dirroot/lib/db/$CFG->dbtype.php")) {
200 include_once("$CFG->dirroot/lib/db/$CFG->dbtype.php"); # defines old upgrades
202 if (file_exists("$CFG->dirroot/lib/db/upgrade.php")) {
203 include_once("$CFG->dirroot/lib/db/upgrade.php"); # defines new upgrades
206 $stradministration = get_string("administration");
209 if ($version > $CFG->version
) { // upgrade
211 $a->oldversion
= "$CFG->release ($CFG->version)";
212 $a->newversion
= "$release ($version)";
213 $strdatabasechecking = get_string("databasechecking", "", $a);
215 // hide errors from headers in case debug is enabled
216 $origdebug = $CFG->debug
;
217 $CFG->debug
= DEBUG_MINIMAL
;
218 error_reporting($CFG->debug
);
220 // logout in case we are upgrading from pre 1.7 version - prevention of weird session problems
221 if ($CFG->version
< 2006050600) {
225 if (empty($confirmupgrade)) {
226 print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
227 "", "", false, " ", " ");
228 notice_yesno(get_string('upgradesure', 'admin', $a->newversion
), 'index.php?confirmupgrade=yes', 'index.php');
232 $strdatabasesuccess = get_string("databasesuccess");
233 print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
234 "", $linktoscrolltoerrors, false, " ", " ");
235 /// return to original debugging level
236 $CFG->debug
= $origdebug;
237 error_reporting($CFG->debug
);
239 print_heading($strdatabasechecking);
241 /// Launch the old main upgrade (if exists)
243 if (function_exists('main_upgrade')) {
244 $status = main_upgrade($CFG->version
);
246 /// If succesful and exists launch the new main upgrade (XMLDB), called xmldb_main_upgrade
247 if ($status && function_exists('xmldb_main_upgrade')) {
248 $status = xmldb_main_upgrade($CFG->version
);
251 /// If successful, continue upgrading roles and setting everything properly
253 if (empty($CFG->rolesactive
)) {
254 // Upgrade to the roles system.
255 moodle_install_roles();
256 set_config('rolesactive', 1);
257 } else if (!update_capabilities()) {
258 error('Had trouble upgrading the core capabilities for the Roles System');
260 require_once($CFG->libdir
.'/statslib.php');
261 if (!stats_upgrade_for_roles_wrapper()) {
262 notify('Couldn\'t upgrade the stats tables to use the new roles system');
264 if (set_config("version", $version)) {
265 remove_dir($CFG->dataroot
. '/cache', true); // flush cache
266 notify($strdatabasesuccess, "green");
267 print_continue("upgradesettings.php");
270 notify("Upgrade failed! (Could not update version in config table)");
272 /// Main upgrade not success
274 notify("Upgrade failed! See /version.php");
276 upgrade_log_finish();
278 } else if ($version < $CFG->version
) {
280 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
281 upgrade_log_finish();
284 if (!set_config("version", $version)) {
285 error("A problem occurred inserting current version into databases");
289 /// Updated human-readable release version if necessary
291 if ($release <> $CFG->release
) { // Update the release version
292 $strcurrentrelease = get_string("currentrelease");
293 print_header($strcurrentrelease, $strcurrentrelease, $strcurrentrelease, "", "", false, " ", " ");
294 print_heading("Moodle $release");
295 if (!set_config("release", $release)) {
296 notify("ERROR: Could not update release version in database!!");
298 notice(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes'), 'index.php', 'none');
301 /// Find and check all main modules and load them up or upgrade them if necessary
302 /// first old *.php update and then the new upgrade.php script
303 upgrade_activity_modules("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
305 /// Check all questiontype plugins and upgrade if necessary
306 /// first old *.php update and then the new upgrade.php script
307 /// It is important that this is done AFTER the quiz module has been upgraded
308 upgrade_plugins('qtype', 'question/type', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
310 /// Upgrade backup/restore system if necessary
311 /// first old *.php update and then the new upgrade.php script
312 require_once("$CFG->dirroot/backup/lib.php");
313 upgrade_backup_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
315 /// Upgrade blocks system if necessary
316 /// first old *.php update and then the new upgrade.php script
317 require_once("$CFG->dirroot/lib/blocklib.php");
318 upgrade_blocks_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
320 /// Check all blocks and load (or upgrade them if necessary)
321 /// first old *.php update and then the new upgrade.php script
322 upgrade_blocks_plugins("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
324 /// Check all enrolment plugins and upgrade if necessary
325 /// first old *.php update and then the new upgrade.php script
326 upgrade_plugins('enrol', 'enrol', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
328 /// Check for local database customisations
329 /// first old *.php update and then the new upgrade.php script
330 require_once("$CFG->dirroot/lib/locallib.php");
331 upgrade_local_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
333 /// just make sure upgrade logging is properly terminated
334 upgrade_log_finish();
336 /// Set up the blank site - to be customized later at the end of install.
337 if (! $site = get_site()) {
338 // We are about to create the site "course"
339 require_once($CFG->libdir
.'/blocklib.php');
341 $newsite = new Object();
342 $newsite->fullname
= "";
343 $newsite->shortname
= "";
344 $newsite->summary
= "";
345 $newsite->newsitems
= 3;
346 $newsite->numsections
= 0;
347 $newsite->category
= 0;
348 $newsite->format
= 'site'; // Only for this course
349 $newsite->teacher
= get_string("defaultcourseteacher");
350 $newsite->teachers
= get_string("defaultcourseteachers");
351 $newsite->student
= get_string("defaultcoursestudent");
352 $newsite->students
= get_string("defaultcoursestudents");
353 $newsite->timemodified
= time();
355 if ($newid = insert_record('course', $newsite)) {
356 // Site created, add blocks for it
357 $page = page_create_object(PAGE_COURSE_VIEW
, $newid);
358 blocks_repopulate_page($page); // Return value not checked because you can always edit later
361 $cat->name
= get_string('miscellaneous');
362 if (insert_record('course_categories', $cat)) {
363 redirect('index.php');
365 error("Serious Error! Could not set up a default course category!");
368 error("Serious Error! Could not set up the site!");
372 // initialise default blocks on admin and site page if needed
373 if (empty($CFG->adminblocks_initialised
)) {
374 require_once("$CFG->dirroot/$CFG->admin/pagelib.php");
375 require_once($CFG->libdir
.'/blocklib.php');
376 page_map_class(PAGE_ADMIN
, 'page_admin');
377 $page = page_create_object(PAGE_ADMIN
, 0); // there must be some id number
378 blocks_repopulate_page($page);
380 //add admin_tree block to site if not already present
381 if ($admintree = get_record('block', 'name', 'admin_tree')) {
382 $page = page_create_object(PAGE_COURSE_VIEW
, SITEID
);
383 blocks_execute_action($page, blocks_get_by_page($page), 'add', (int)$admintree->id
, false, false);
384 if ($admintreeinstance = get_record('block_instance', 'pagetype', $page->type
, 'pageid', SITEID
, 'blockid', $admintree->id
)) {
385 blocks_execute_action($page, blocks_get_by_page($page), 'moveleft', $admintreeinstance, false, false);
389 set_config('adminblocks_initialised', 1);
392 /// Define the unique site ID code if it isn't already
393 if (empty($CFG->siteidentifier
)) { // Unique site identification code
394 set_config('siteidentifier', random_string(32));
397 /// Check if the guest user exists. If not, create one.
398 if (! record_exists("user", "username", "guest")) {
399 $guest->auth
= "manual";
400 $guest->username
= "guest";
401 $guest->password
= md5("guest");
402 $guest->firstname
= addslashes(get_string("guestuser"));
403 $guest->lastname
= " ";
404 $guest->email
= "root@localhost";
405 $guest->description
= addslashes(get_string("guestuserinfo"));
406 $guest->confirmed
= 1;
407 $guest->lang
= $CFG->lang
;
408 $guest->timemodified
= time();
410 if (! $guest->id
= insert_record("user", $guest)) {
411 notify("Could not create guest user record !!!");
416 /// Set up the admin user
417 if (empty($CFG->rolesactive
)) {
418 redirect('user.php');
421 /// Check for valid admin user
424 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
426 require_capability('moodle/site:config', $context);
428 /// check that site is properly customized
429 if (empty($site->shortname
) or empty($site->shortname
)) {
430 redirect('settings.php?section=frontpage&return=site');
433 /// Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
436 if ($id == $CFG->siteidentifier
) {
437 set_config('registered', time());
441 /// Everything should now be set up, and the user is an admin
443 /// Print default admin page with notifications.
445 $adminroot = admin_get_root();
446 admin_externalpage_setup('adminnotifications', $adminroot);
447 admin_externalpage_print_header($adminroot);
449 /// Deprecated database! Warning!!
450 if (!empty($CFG->migrated_to_new_db
)) {
451 print_simple_box_start('center','60%');
452 print_string('dbmigrationdeprecateddb','admin');
453 print_simple_box_end();
456 /// Check for any special upgrades that might need to be run
457 if (!empty($CFG->upgrade
)) {
458 print_simple_box(get_string("upgrade$CFG->upgrade", "admin",
459 "$CFG->wwwroot/$CFG->admin/upgrade$CFG->upgrade.php"), "center", '60%');
463 if (ini_get_bool('register_globals') && !ini_get_bool('magic_quotes_gpc')) {
464 print_simple_box(get_string('globalsquoteswarning', 'admin'), 'center', '60%');
467 if (is_dataroot_insecure()) {
468 print_simple_box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot
), 'center', '60%');
471 /// If no recently cron run
472 $lastcron = get_field_sql('SELECT max(lastcron) FROM ' . $CFG->prefix
. 'modules');
473 if (time() - $lastcron > 3600 * 24) {
474 $strinstallation = get_string('installation', 'install');
475 $helpbutton = helpbutton('install', $strinstallation, 'moodle', true, false, '', true);
476 print_simple_box(get_string('cronwarning', 'admin')." ".$helpbutton, 'center', '60%');
479 /// Alert if we are currently in maintenance mode
480 if (file_exists($CFG->dataroot
.'/1/maintenance.html')) {
481 print_simple_box(get_string('sitemaintenancewarning', 'admin') , 'center', '60%');
484 /// Alert to display the utf-8 migration button (if !unicode yet and DB is MySQL or PG)
485 if (empty($CFG->unicodedb
) && in_array($CFG->dbtype
, array('mysql', 'postgres7'))) {
486 print_simple_box(get_string('unicodeupgradenotice', 'admin') , 'center', '60%');
489 /// Print slightly annoying registration button every six months ;-)
490 /// You can set the "registered" variable to something far in the future
491 /// if you really want to prevent this. eg 9999999999
492 if (!isset($CFG->registered
) ||
$CFG->registered
< (time() - 3600*24*30*6)) {
494 $options['sesskey'] = $USER->sesskey
;
495 print_simple_box_start('center','60%');
496 echo '<div align="center">';
497 print_string('pleaseregister', 'admin');
498 print_single_button('register.php', $options, get_string('registration'));
500 print_simple_box_end();
501 $registrationbuttonshown = true;
504 //////////////////////////////////////////////////////////////////////////////////////////////////
505 //// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
506 $copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
507 '<a href="http://docs.moodle.org/en/Release">'.$CFG->release
.'</a> ('.$CFG->version
.')<br />'.
508 'Copyright © 1999 onwards, Martin Dougiamas<br />'.
509 'and <a href="http://docs.moodle.org/en/Credits">many other contributors</a>.<br />'.
510 '<a href="http://docs.moodle.org/en/License">GNU Public License</a>';
511 echo '<p class="copyright">'.$copyrighttext.'</p>';
512 //////////////////////////////////////////////////////////////////////////////////////////////////
515 if (empty($registrationbuttonshown)) {
516 echo '<div align="center">';
518 $options['sesskey'] = $USER->sesskey
;
519 print_single_button('register.php', $options, get_string('registration'));
524 if (optional_param('dbmigrate')) { // ??? Is this actually used?
525 print_simple_box_start('center','60%');
526 require_once($CFG->dirroot
.'/'.$CFG->admin
.'/utfdbmigrate.php');
528 print_simple_box_end();
532 admin_externalpage_print_footer($adminroot);