| [ Index ] |
PHP Cross Reference of Moodle 1.9 |
[Summary view] [Print] [Text view]
1 <?php // $Id: index.php,v 1.286.2.17 2008/02/26 10:32:40 sam_marshall Exp $ 2 3 /// Check that config.php exists, if not then call the install script 4 if (!file_exists('../config.php')) { 5 header('Location: ../install.php'); 6 die; 7 } 8 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)"; 14 die; 15 } 16 17 /// Turn off time limits and try to flush everything all the time, sometimes upgrades can be slow. 18 19 @set_time_limit(0); 20 @ob_implicit_flush(true); 21 while(@ob_end_clean()); // ob_end_flush prevents sending of headers 22 23 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 27 require_once($CFG->libdir.'/db/upgradelib.php'); // Upgrade-related functions 28 29 $id = optional_param('id', '', PARAM_TEXT); 30 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); 31 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); 32 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); 33 $autopilot = optional_param('autopilot', 0, PARAM_BOOL); 34 $ignoreupgradewarning = optional_param('ignoreupgradewarning', 0, PARAM_BOOL); 35 $confirmplugincheck = optional_param('confirmplugincheck', 0, PARAM_BOOL); 36 37 /// check upgrade status first 38 if ($ignoreupgradewarning and !empty($_SESSION['upgraderunning'])) { 39 $_SESSION['upgraderunning'] = 0; 40 } 41 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); 42 43 /// set install/upgrade autocontinue session flag 44 if ($autopilot) { 45 $_SESSION['installautopilot'] = $autopilot; 46 } 47 48 /// Check some PHP server settings 49 50 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>'; 51 52 if (ini_get_bool('session.auto_start')) { 53 error("The PHP server variable 'session.auto_start' should be Off - $documentationlink"); 54 } 55 56 if (ini_get_bool('magic_quotes_runtime')) { 57 error("The PHP server variable 'magic_quotes_runtime' should be Off - $documentationlink"); 58 } 59 60 if (!ini_get_bool('file_uploads')) { 61 error("The PHP server variable 'file_uploads' is not turned On - $documentationlink"); 62 } 63 64 if (empty($CFG->prefix) && $CFG->dbfamily != 'mysql') { //Enforce prefixes for everybody but mysql 65 error('$CFG->prefix can\'t be empty for your target DB (' . $CFG->dbtype . ')'); 66 } 67 68 if ($CFG->dbfamily == 'oracle' && strlen($CFG->prefix) > 2) { //Max prefix length for Oracle is 2cc 69 error('$CFG->prefix maximum allowed length for Oracle DBs is 2cc.'); 70 } 71 72 /// Check that config.php has been edited 73 74 if ($CFG->wwwroot == "http://example.com/moodle") { 75 error("Moodle has not been configured yet. You need to edit config.php first."); 76 } 77 78 79 /// Check settings in config.php 80 81 $dirroot = dirname(realpath("../index.php")); 82 if (!empty($dirroot) and $dirroot != $CFG->dirroot) { 83 error("Please fix your settings in config.php: 84 <p>You have: 85 <p>\$CFG->dirroot = \"".addslashes($CFG->dirroot)."\"; 86 <p>but it should be: 87 <p>\$CFG->dirroot = \"".addslashes($dirroot)."\";", 88 "./"); 89 } 90 91 /// Set some necessary variables during set-up to avoid PHP warnings later on this page 92 if (!isset($CFG->framename)) { 93 $CFG->framename = "_top"; 94 } 95 if (!isset($CFG->release)) { 96 $CFG->release = ""; 97 } 98 if (!isset($CFG->version)) { 99 $CFG->version = ""; 100 } 101 102 if (is_readable("$CFG->dirroot/version.php")) { 103 include_once("$CFG->dirroot/version.php"); # defines $version 104 } 105 106 if (!$version or !$release) { 107 error('Main version.php was not readable or specified');# without version, stop 108 } 109 110 /// Check if the main tables have been installed yet or not. 111 112 if (! $tables = $db->Metatables() ) { // No tables yet at all. 113 $maintables = false; 114 115 } else { // Check for missing main tables 116 $maintables = true; 117 $mtables = array("config", "course", "course_categories", "course_modules", 118 "course_sections", "log", "log_display", "modules", 119 "user"); 120 foreach ($mtables as $mtable) { 121 if (!in_array($CFG->prefix.$mtable, $tables)) { 122 $maintables = false; 123 break; 124 } 125 } 126 } 127 128 if (! $maintables) { 129 /// hide errors from headers in case debug enabled in config.php 130 $origdebug = $CFG->debug; 131 $CFG->debug = DEBUG_MINIMAL; 132 error_reporting($CFG->debug); 133 if (empty($agreelicense)) { 134 $strlicense = get_string('license'); 135 $navigation = build_navigation(array(array('name'=>$strlicense, 'link'=>null, 'type'=>'misc'))); 136 print_header($strlicense, $strlicense, $navigation, "", "", false, " ", " "); 137 print_heading("<a href=\"http://moodle.org\">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment"); 138 print_heading(get_string('copyrightnotice')); 139 print_box(text_to_html(get_string('gpl')), 'copyrightnotice'); 140 echo "<br />"; 141 notice_yesno(get_string('doyouagree'), "index.php?agreelicense=1", 142 "http://docs.moodle.org/en/License"); 143 print_footer('none'); 144 exit; 145 } 146 if (empty($confirmrelease)) { 147 $strcurrentrelease = get_string("currentrelease"); 148 $navigation = build_navigation(array(array('name'=>$strcurrentrelease, 'link'=>null, 'type'=>'misc'))); 149 print_header($strcurrentrelease, $strcurrentrelease, $navigation, "", "", false, " ", " "); 150 print_heading("Moodle $release"); 151 print_box(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes'), 'generalbox boxaligncenter boxwidthwide'); 152 echo '<form action="index.php"><div>'; 153 echo '<input type="hidden" name="agreelicense" value="1" />'; 154 echo '<input type="hidden" name="confirmrelease" value="1" />'; 155 echo '</div>'; 156 echo '<div class="continuebutton"><input name="autopilot" id="autopilot" type="checkbox" value="1" /><label for="autopilot">'.get_string('unattendedoperation', 'admin').'</label>'; 157 echo '<br /><br /><input type="submit" value="'.get_string('continue').'" /></div>'; 158 echo '</form>'; 159 print_footer('none'); 160 die; 161 } 162 163 164 $strdatabasesetup = get_string("databasesetup"); 165 $strdatabasesuccess = get_string("databasesuccess"); 166 $navigation = build_navigation(array(array('name'=>$strdatabasesetup, 'link'=>null, 'type'=>'misc'))); 167 print_header($strdatabasesetup, $strdatabasesetup, $navigation, 168 "", upgrade_get_javascript(), false, " ", " "); 169 /// return to original debugging level 170 $CFG->debug = $origdebug; 171 error_reporting($CFG->debug); 172 upgrade_log_start(); 173 $db->debug = true; 174 175 /// Both old .sql files and new install.xml are supported 176 /// But we prioritise install.xml (XMLDB) if present 177 178 change_db_encoding(); // first try to change db encoding to utf8 179 if (!setup_is_unicodedb()) { 180 // If could not convert successfully, throw error, and prevent installation 181 print_error('unicoderequired', 'admin'); 182 } 183 184 $status = false; 185 if (file_exists("$CFG->libdir/db/install.xml")) { 186 $status = install_from_xmldb_file("$CFG->libdir/db/install.xml"); //New method 187 } else if (file_exists("$CFG->libdir/db/$CFG->dbtype.sql")) { 188 $status = modify_database("$CFG->libdir/db/$CFG->dbtype.sql"); //Old method 189 } else { 190 error("Error: Your database ($CFG->dbtype) is not yet fully supported by Moodle or install.xml is not present. See the lib/db directory."); 191 } 192 193 // all new installs are in unicode - keep for backwards compatibility and 1.8 upgrade checks 194 set_config('unicodedb', 1); 195 196 /// Continue with the instalation 197 $db->debug = false; 198 if ($status) { 199 200 /// Groups install is now in core above. 201 202 // Install the roles system. 203 moodle_install_roles(); 204 set_config('statsrolesupgraded',time()); 205 206 // install core event handlers 207 events_update_definition(); 208 209 /// This is used to handle any settings that must exist in $CFG but which do not exist in 210 /// admin_get_root()/$ADMIN as admin_setting objects (there are some exceptions). 211 apply_default_exception_settings(array('auth' => 'email', 212 'auth_pop3mailbox' => 'INBOX', 213 'enrol' => 'manual', 214 'enrol_plugins_enabled' => 'manual', 215 'style' => 'default', 216 'template' => 'default', 217 'theme' => 'standardwhite', 218 'filter_multilang_converted' => 1)); 219 220 // Write default settings unconditionally (i.e. even if a setting is already set, overwrite it) 221 // (this should only have any effect during initial install). 222 admin_apply_default_settings(NULL, true); 223 224 notify($strdatabasesuccess, "green"); 225 require_once $CFG->dirroot.'/mnet/lib.php'; 226 } else { 227 error("Error: Main databases NOT set up successfully"); 228 } 229 print_continue('index.php'); 230 print_footer('none'); 231 die; 232 } 233 234 235 /// Check version of Moodle code on disk compared with database 236 /// and upgrade if possible. 237 238 if (file_exists("$CFG->dirroot/lib/db/$CFG->dbtype.php")) { 239 include_once("$CFG->dirroot/lib/db/$CFG->dbtype.php"); # defines old upgrades 240 } 241 if (file_exists("$CFG->dirroot/lib/db/upgrade.php")) { 242 include_once("$CFG->dirroot/lib/db/upgrade.php"); # defines new upgrades 243 } 244 245 $stradministration = get_string("administration"); 246 247 if ($CFG->version) { 248 if ($version > $CFG->version) { // upgrade 249 250 /// If the database is not already Unicode then we do not allow upgrading! 251 /// Instead, we print an error telling them to upgrade to 1.7 first. MDL-6857 252 if (empty($CFG->unicodedb)) { 253 print_error('unicodeupgradeerror', 'error', '', $version); 254 } 255 256 $a->oldversion = "$CFG->release ($CFG->version)"; 257 $a->newversion = "$release ($version)"; 258 $strdatabasechecking = get_string("databasechecking", "", $a); 259 260 // hide errors from headers in case debug is enabled 261 $origdebug = $CFG->debug; 262 $CFG->debug = DEBUG_MINIMAL; 263 error_reporting($CFG->debug); 264 265 // logo ut in case we are upgrading from pre 1.9 version in order to prevent 266 // weird session/role problems caused by incorrect data in USER and SESSION 267 if ($CFG->version < 2007101500) { 268 require_logout(); 269 } 270 271 if (empty($confirmupgrade)) { 272 $navigation = build_navigation(array(array('name'=>$strdatabasechecking, 'link'=>null, 'type'=>'misc'))); 273 print_header($strdatabasechecking, $stradministration, $navigation, 274 "", "", false, " ", " "); 275 276 notice_yesno(get_string('upgradesure', 'admin', $a->newversion), 'index.php?confirmupgrade=1', 'index.php'); 277 print_footer('none'); 278 exit; 279 280 } else if (empty($confirmrelease)){ 281 $strcurrentrelease = get_string("currentrelease"); 282 $navigation = build_navigation(array(array('name'=>$strcurrentrelease, 'link'=>null, 'type'=>'misc'))); 283 print_header($strcurrentrelease, $strcurrentrelease, $navigation, "", "", false, " ", " "); 284 print_heading("Moodle $release"); 285 print_box(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes')); 286 287 require_once($CFG->libdir.'/environmentlib.php'); 288 print_heading(get_string('environment', 'admin')); 289 if (!check_moodle_environment($release, $environment_results, true)) { 290 print_box_start('generalbox', 'notice'); // MDL-8330 291 print_string('langpackwillbeupdated', 'admin'); 292 print_box_end(); 293 notice_yesno(get_string('environmenterrorupgrade', 'admin'), 294 'index.php?confirmupgrade=1&confirmrelease=1', 'index.php'); 295 } else { 296 notify(get_string('environmentok', 'admin'), 'notifysuccess'); 297 print_box_start('generalbox', 'notice'); // MDL-8330 298 print_string('langpackwillbeupdated', 'admin'); 299 print_box_end(); 300 echo '<form action="index.php"><div>'; 301 echo '<input type="hidden" name="confirmupgrade" value="1" />'; 302 echo '<input type="hidden" name="confirmrelease" value="1" />'; 303 echo '</div>'; 304 echo '<div class="continuebutton">'; 305 echo '<br /><br /><input type="submit" value="'.get_string('continue').'" /></div>'; 306 echo '</form>'; 307 } 308 309 print_footer('none'); 310 die; 311 } elseif (empty($confirmplugincheck)) { 312 $strplugincheck = get_string('plugincheck'); 313 $navigation = build_navigation(array(array('name'=>$strplugincheck, 'link'=>null, 'type'=>'misc'))); 314 print_header($strplugincheck, $strplugincheck, $navigation, "", "", false, " ", " "); 315 print_heading($strplugincheck); 316 print_box_start('generalbox', 'notice'); // MDL-8330 317 print_string('pluginchecknotice'); 318 print_box_end(); 319 print_plugin_tables(); 320 echo "<br />"; 321 echo '<div class="continuebutton">'; 322 print_single_button('index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1), get_string('reload'), 'get'); 323 echo '</div><br />'; 324 echo '<form action="index.php"><div>'; 325 echo '<input type="hidden" name="confirmupgrade" value="1" />'; 326 echo '<input type="hidden" name="confirmrelease" value="1" />'; 327 echo '<input type="hidden" name="confirmplugincheck" value="1" />'; 328 echo '</div>'; 329 echo '<div class="continuebutton"><input name="autopilot" id="autopilot" type="checkbox" value="1" /><label for="autopilot">'.get_string('unattendedoperation', 'admin').'</label>'; 330 echo '<br /><br /><input type="submit" value="'.get_string('continue').'" /></div>'; 331 echo '</form>'; 332 print_footer('none'); 333 die(); 334 335 } else { 336 $strdatabasesuccess = get_string("databasesuccess"); 337 $navigation = build_navigation(array(array('name'=>$strdatabasesuccess, 'link'=>null, 'type'=>'misc'))); 338 print_header($strdatabasechecking, $stradministration, $navigation, 339 "", upgrade_get_javascript(), false, " ", " "); 340 341 /// return to original debugging level 342 $CFG->debug = $origdebug; 343 error_reporting($CFG->debug); 344 upgrade_log_start(); 345 346 /// Upgrade current language pack if we can 347 upgrade_language_pack(); 348 349 print_heading($strdatabasechecking); 350 $db->debug=true; 351 /// Launch the old main upgrade (if exists) 352 $status = true; 353 if (function_exists('main_upgrade')) { 354 $status = main_upgrade($CFG->version); 355 } 356 /// If succesful and exists launch the new main upgrade (XMLDB), called xmldb_main_upgrade 357 if ($status && function_exists('xmldb_main_upgrade')) { 358 $status = xmldb_main_upgrade($CFG->version); 359 } 360 $db->debug=false; 361 /// If successful, continue upgrading roles and setting everything properly 362 if ($status) { 363 if (empty($CFG->rolesactive)) { 364 365 /// Groups upgrade is now in core above. 366 367 // Upgrade to the roles system. 368 moodle_install_roles(); 369 set_config('rolesactive', 1); 370 } else if (!update_capabilities()) { 371 error('Had trouble upgrading the core capabilities for the Roles System'); 372 } 373 // update core events 374 events_update_definition(); 375 376 require_once($CFG->libdir.'/statslib.php'); 377 if (!stats_upgrade_for_roles_wrapper()) { 378 notify('Couldn\'t upgrade the stats tables to use the new roles system'); 379 } 380 if (set_config("version", $version)) { 381 remove_dir($CFG->dataroot . '/cache', true); // flush cache 382 notify($strdatabasesuccess, "green"); 383 print_continue("upgradesettings.php"); 384 print_footer('none'); 385 exit; 386 } else { 387 error('Upgrade failed! (Could not update version in config table)'); 388 } 389 /// Main upgrade not success 390 } else { 391 notify('Main Upgrade failed! See lib/db/upgrade.php'); 392 print_continue('index.php?confirmupgrade=1&confirmrelease=1&confirmplugincheck=1'); 393 print_footer('none'); 394 die; 395 } 396 upgrade_log_finish(); 397 } 398 } else if ($version < $CFG->version) { 399 upgrade_log_start(); 400 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!"); 401 upgrade_log_finish(); 402 } 403 } else { 404 if (!set_config("version", $version)) { 405 error("A problem occurred inserting current version into databases"); 406 } 407 } 408 409 /// Updated human-readable release version if necessary 410 411 if ($release <> $CFG->release) { // Update the release version 412 if (!set_config("release", $release)) { 413 error("ERROR: Could not update release version in database!!"); 414 } 415 } 416 417 /// Groups install/upgrade is now in core above. 418 419 420 /// Find and check all main modules and load them up or upgrade them if necessary 421 /// first old *.php update and then the new upgrade.php script 422 upgrade_activity_modules("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 423 424 /// Check all questiontype plugins and upgrade if necessary 425 /// first old *.php update and then the new upgrade.php script 426 /// It is important that this is done AFTER the quiz module has been upgraded 427 upgrade_plugins('qtype', 'question/type', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 428 429 /// Upgrade backup/restore system if necessary 430 /// first old *.php update and then the new upgrade.php script 431 require_once("$CFG->dirroot/backup/lib.php"); 432 upgrade_backup_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 433 434 /// Upgrade blocks system if necessary 435 /// first old *.php update and then the new upgrade.php script 436 require_once("$CFG->dirroot/lib/blocklib.php"); 437 upgrade_blocks_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 438 439 /// Check all blocks and load (or upgrade them if necessary) 440 /// first old *.php update and then the new upgrade.php script 441 upgrade_blocks_plugins("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 442 443 /// Check all enrolment plugins and upgrade if necessary 444 /// first old *.php update and then the new upgrade.php script 445 upgrade_plugins('enrol', 'enrol', "$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 446 447 /// Check all auth plugins and upgrade if necessary 448 upgrade_plugins('auth','auth',"$CFG->wwwroot/$CFG->admin/index.php"); 449 450 /// Check all course formats and upgrade if necessary 451 upgrade_plugins('format','course/format',"$CFG->wwwroot/$CFG->admin/index.php"); 452 453 /// Check for local database customisations 454 /// first old *.php update and then the new upgrade.php script 455 require_once("$CFG->dirroot/lib/locallib.php"); 456 upgrade_local_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 457 458 /// Check for changes to RPC functions 459 require_once("$CFG->dirroot/$CFG->admin/mnet/adminlib.php"); 460 upgrade_RPC_functions("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards 461 462 /// Upgrade all plugins for gradebook 463 upgrade_plugins('gradeexport', 'grade/export', "$CFG->wwwroot/$CFG->admin/index.php"); 464 upgrade_plugins('gradeimport', 'grade/import', "$CFG->wwwroot/$CFG->admin/index.php"); 465 upgrade_plugins('gradereport', 'grade/report', "$CFG->wwwroot/$CFG->admin/index.php"); 466 467 /// Check all message output plugins and upgrade if necessary 468 upgrade_plugins('message','message/output',"$CFG->wwwroot/$CFG->admin/index.php"); 469 470