Index: admin/register.php =================================================================== RCS file: /cvsroot/moodle/moodle/admin/register.php,v retrieving revision 1.25.2.2 diff -u -r1.25.2.2 register.php --- admin/register.php 2 May 2008 04:07:28 -0000 1.25.2.2 +++ admin/register.php 19 Jan 2009 09:46:48 -0000 @@ -23,13 +23,7 @@ $admin->country = $CFG->country; } - if (empty($CFG->siteidentifier)) { // Unique site identification code - set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']); - } - - /// Print headings - $stradministration = get_string("administration"); $strregistration = get_string("registration"); $strregistrationinfo = get_string("registrationinfo"); @@ -54,7 +48,7 @@ echo "\n"; echo "\n"; echo "wwwroot\" />\n"; - echo "siteidentifier\" />\n"; + echo "\n"; echo "\n"; echo "\n"; Index: admin/index.php =================================================================== RCS file: /cvsroot/moodle/moodle/admin/index.php,v retrieving revision 1.286.2.27 diff -u -r1.286.2.27 index.php --- admin/index.php 16 Jan 2009 02:37:29 -0000 1.286.2.27 +++ admin/index.php 19 Jan 2009 09:46:47 -0000 @@ -553,10 +553,8 @@ set_config('adminblocks_initialised', 1); } -/// Define the unique site ID code if it isn't already - if (empty($CFG->siteidentifier)) { // Unique site identification code - set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']); - } +/// Define the unique site ID code if it isn't already set. This getter does that as a side-effect. + get_site_identifier(); /// Check if the guest user exists. If not, create one. if (! record_exists("user", "username", "guest")) { Index: lang/en_utf8/moodle.php =================================================================== RCS file: /cvsroot/moodle/moodle/lang/en_utf8/moodle.php,v retrieving revision 1.141.2.53 diff -u -r1.141.2.53 moodle.php --- lang/en_utf8/moodle.php 1 Jan 2009 16:06:37 -0000 1.141.2.53 +++ lang/en_utf8/moodle.php 19 Jan 2009 09:46:58 -0000 @@ -149,6 +149,7 @@ $string['backupfailed'] = 'Some of your courses weren\'t saved!!'; $string['backupfilename'] = 'backup'; $string['backupfinished'] = 'Backup completed successfully'; +$string['backupfromthissite'] = 'Backup was made on this site?'; $string['backupgradebookhistoryhelp'] = 'If enabled then gradebook history will be included in automated backups. Note that grade history must not be disabled in server settings (disablegradehistory) in order for this to work'; $string['backupincludemoduleshelp'] = 'Choose whether you want to include course modules, with or without user data, in automated backups'; $string['backupincludemoduleuserdatahelp'] = 'Choose whether you want to include module user data in automated backups.'; Index: backup/restorelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/backup/restorelib.php,v retrieving revision 1.283.2.60 diff -u -r1.283.2.60 restorelib.php --- backup/restorelib.php 9 Jan 2009 06:35:34 -0000 1.283.2.60 +++ backup/restorelib.php 19 Jan 2009 09:46:57 -0000 @@ -486,6 +486,15 @@ //The backup date $tab[3][0] = "".get_string("backupdate").":"; $tab[3][1] = userdate($info->backup_date); + //Is this the same Moodle install? + if (!empty($info->original_siteidentifier)) { + $tab[4][0] = "".get_string("backupfromthissite").":"; + if (backup_is_same_site($info)) { + $tab[4][1] = get_string('yes'); + } else { + $tab[4][1] = get_string('no'); + } + } //Print title print_heading(get_string("backup").":"); $table->data = $tab; @@ -1260,7 +1269,7 @@ $dbmetacourse = false; //Check if child course exists in destination server //(by id in the same server or by idnumber and shortname in other server) - if ($restore->original_wwwroot == $CFG->wwwroot) { + if (backup_is_same_site($restore)) { //Same server, lets see by id $dbcourse = get_record('course','id',$child->id); } else { @@ -1292,7 +1301,7 @@ $dbmetacourse = false; //Check if parent course exists in destination server //(by id in the same server or by idnumber and shortname in other server) - if ($restore->original_wwwroot == $CFG->wwwroot) { + if (backup_is_same_site($restore)) { //Same server, lets see by id $dbcourse = get_record('course','id',$parent->id); } else { @@ -2562,7 +2571,7 @@ // - if the destination site is different (by wwwroot) reset it. // - if the destination site is the same (by wwwroot), leave it unmodified - if ($restore->original_wwwroot != $CFG->wwwroot) { + if (!backup_is_same_site($restore)) { $user->policyagreed = 0; } else { //Nothing to do, we are in the same server @@ -2627,7 +2636,7 @@ // - if we are in the same server (by wwwroot), maintain it unmodified. if (empty($user->roles['teacher']->enrol)) { $user->roles['teacher']->enrol = $CFG->enrol; - } else if ($restore->original_wwwroot != $CFG->wwwroot) { + } else if (!backup_is_same_site($restore)) { $user->roles['teacher']->enrol = $CFG->enrol; } else { //Nothing to do. Leave it unmodified @@ -2673,7 +2682,7 @@ // - if we are in the same server (by wwwroot), maintain it unmodified. if (empty($user->roles['student']->enrol)) { $user->roles['student']->enrol = $CFG->enrol; - } else if ($restore->original_wwwroot != $CFG->wwwroot) { + } else if (!backup_is_same_site($restore)) { $user->roles['student']->enrol = $CFG->enrol; } else { //Nothing to do. Leave it unmodified @@ -4966,6 +4975,9 @@ case "ORIGINAL_WWWROOT": $this->info->original_wwwroot = $this->getContents(); break; + case "ORIGINAL_SITE_IDENTIFIER_HASH": + $this->info->original_siteidentifier = $this->getContents(); + break; case "MNET_REMOTEUSERS": $this->info->mnet_remoteusers = $this->getContents(); break; Index: backup/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/backup/lib.php,v retrieving revision 1.89.2.7 diff -u -r1.89.2.7 lib.php --- backup/lib.php 12 Jun 2008 11:06:57 -0000 1.89.2.7 +++ backup/lib.php 19 Jan 2009 09:46:51 -0000 @@ -435,6 +435,25 @@ upgrade_log_finish(); } + /** + * Are we restoring a backup that was made on the same site that we are restoring to? + * This relies on some information that was only added to backup files in January 2009. + * For older backup files, fall back to guessing based on wwwroot. MDL-16614 explains + * when this guess could give the wrong answer. + * @return boolean true if the backup was made on the same site we are restoring to. + */ + function backup_is_same_site(&$restore) { + global $CFG; + static $hashedsiteid = null; + if (is_null($hashedsiteid)) { + $hashedsiteid = md5(get_site_identifier()); + } + if (!empty($restore->original_siteidentifier)) { + return $restore->original_siteidentifier == $hashedsiteid; + } else { + return $restore->original_wwwroot == $CFG->wwwroot; + } + } //This function is used to insert records in the backup_ids table //If the info field is greater than max_db_storage, then its info Index: backup/backuplib.php =================================================================== RCS file: /cvsroot/moodle/moodle/backup/backuplib.php,v retrieving revision 1.179.2.36 diff -u -r1.179.2.36 backuplib.php --- backup/backuplib.php 15 Dec 2008 21:28:02 -0000 1.179.2.36 +++ backup/backuplib.php 19 Jan 2009 09:46:50 -0000 @@ -483,6 +483,8 @@ fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code)); //The original site wwwroot fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot)); + //The original site wwwroot + fwrite ($bf,full_tag("ORIGINAL_SITE_IDENTIFIER_HASH",2,false,md5(get_site_identifier()))); //The zip method used if (!empty($CFG->zip)) { $zipmethod = 'external'; Index: backup/restore_execute.html =================================================================== RCS file: /cvsroot/moodle/moodle/backup/restore_execute.html,v retrieving revision 1.62.4.2 diff -u -r1.62.4.2 restore_execute.html --- backup/restore_execute.html 2 May 2008 04:07:30 -0000 1.62.4.2 +++ backup/restore_execute.html 19 Jan 2009 09:46:51 -0000 @@ -13,6 +13,10 @@ //Add info->original_wwwroot to $restore to be able to use it in all the restore process //(mainly when decoding internal links) $restore->original_wwwroot = $info->original_wwwroot; + // Copy $info->original_siteidentifier, is present, so backup_is_same_site can work. + if (isset($info->original_siteidentifier)) { + $restore->original_siteidentifier = $info->original_siteidentifier; + } //Add info->backup_version to $restore to be able to detect versions in the restore process //(to decide when to convert wiki texts to markdown...) $restore->backup_version = $info->backup_backup_version; Index: backup/restore_check.html =================================================================== RCS file: /cvsroot/moodle/moodle/backup/restore_check.html,v retrieving revision 1.48.2.10 diff -u -r1.48.2.10 restore_check.html --- backup/restore_check.html 2 Jan 2009 16:53:30 -0000 1.48.2.10 +++ backup/restore_check.html 19 Jan 2009 09:46:51 -0000 @@ -291,7 +291,7 @@ /// If restoring users and backup has mnet remote users and we are restoring to different site, forbid restore to non-admins. MDL-17009 if ($restore->users != 2 && /// If restoring users !empty($info->mnet_remoteusers) && $info->mnet_remoteusers === 'true' && /// and backup contains remote users - $info->original_wwwroot !== $CFG->wwwroot) { /// and backup is being restored to different site + !backup_is_same_site($info)) { /// and backup is being restored to different site /// If user is admin (by 'moodle/user:create' cap), warn about conversion to local auth if missing mnet hosts and continue restore if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM))) { Index: question/restorelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/restorelib.php,v retrieving revision 1.30.2.5 diff -u -r1.30.2.5 restorelib.php --- question/restorelib.php 9 May 2008 15:10:42 -0000 1.30.2.5 +++ question/restorelib.php 19 Jan 2009 09:47:02 -0000 @@ -334,8 +334,28 @@ $question->hidden = backup_todb($que_info['#']['HIDDEN']['0']['#']); $question->timecreated = backup_todb_optional_field($que_info, 'TIMECREATED', 0); $question->timemodified = backup_todb_optional_field($que_info, 'TIMEMODIFIED', 0); - $question->createdby = backup_todb_optional_field($que_info, 'CREATEDBY', null); - $question->modifiedby = backup_todb_optional_field($que_info, 'MODIFIEDBY', null); + + // Set the createdby field, if the user was in the backup, or if we are on the same site. + $createdby = backup_todb_optional_field($que_info, 'CREATEDBY', null); + if (!empty($createdby)) { + $user = backup_getid($restore->backup_unique_code, 'user', $createdby); + if ($user) { + $question->createdby = $user->new_id; + } else if (backup_is_same_site($restore)) { + $question->createdby = $createdby; + } + } + + // Set the modifiedby field, if the user was in the backup, or if we are on the same site. + $modifiedby = backup_todb_optional_field($que_info, 'MODIFIEDBY', null); + if (!empty($createdby)) { + $user = backup_getid($restore->backup_unique_code, 'user', $modifiedby); + if ($user) { + $question->modifiedby = $user->new_id; + } else if (backup_is_same_site($restore)) { + $question->modifiedby = $modifiedby; + } + } if ($restore->backup_version < 2006032200) { // The qtype was an integer that now needs to be converted to the name Index: lib/moodlelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v retrieving revision 1.960.2.114 diff -u -r1.960.2.114 moodlelib.php --- lib/moodlelib.php 16 Jan 2009 04:18:32 -0000 1.960.2.114 +++ lib/moodlelib.php 19 Jan 2009 09:47:02 -0000 @@ -3939,15 +3939,10 @@ function generate_email_processing_address($modid,$modargs) { global $CFG; - if (empty($CFG->siteidentifier)) { // Unique site identification code - set_config('siteidentifier', random_string(32)); - } - $header = $CFG->mailprefix . substr(base64_encode(pack('C',$modid)),0,2).$modargs; - return $header . substr(md5($header.$CFG->siteidentifier),0,16).'@'.$CFG->maildomain; + return $header . substr(md5($header.get_site_identifier()),0,16).'@'.$CFG->maildomain; } - function moodle_process_email($modargs,$body) { // the first char should be an unencoded letter. We'll take this as an action switch ($modargs{0}) { @@ -8303,5 +8298,18 @@ } } +/** + * @return string $CFG->siteidentifier, first making sure it is properly initialised. + */ +function get_site_identifier() { + global $CFG; + // Check to see if it is missing. If so, initialise it. + if (empty($CFG->siteidentifier)) { + set_config('siteidentifier', random_string(32) . $_SERVER['HTTP_HOST']); + } + // Return it. + return $CFG->siteidentifier; +} + // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: ?>