//insert this into the reconcileusers.php file in PHPlist 2.10.x or 2.11.1-2-3
//replace the entire existing "mergeUser($userid)" function
function mergeUser($userid) {
//get the sql record of the for the userid with the "duplicate xxx@yyy.zzz" email address
$duplicate = Sql_Fetch_Array_Query("select * from {$GLOBALS["tables"]["user"]} where id = $userid");
printf ('
%s',$duplicate["email"]);
//now extract just the "xxx@yyy.zzz" type email address from the field
if (preg_match("/^duplicate[^ ]* (.*)/",$duplicate["email"],$regs)) {
print " -> ".$regs[1];
$email = $regs[1];
} elseif (preg_match("/^([^ ]+@[^ ]+) \(\d+\)/",$duplicate["email"],$regs)) {
print " -> ".$regs[1];
$email = $regs[1];
} else {
$email = "";
}
if ($email) {
//now find if there is any record that matches that 'stripped' email address
$orig = Sql_Fetch_Row_Query(sprintf('select id from %s where email = "%s"',$GLOBALS["tables"]["user"],$email));
if ($orig[0]) {
print " - ".$GLOBALS['I18N']->get("user found") . ", #" . $orig[0];
$history_entry="Merging duplicate email, information from " . $duplicate["email"] . " userid = " . $duplicate["id"] . " merged to " . $email . " userid = " . $orig[0];
//now move any usermessages from the duplicate to the main user
$umreq = Sql_Query("select * from {$GLOBALS["tables"]["usermessage"]} where userid = ".$duplicate["id"]);
while ($um = Sql_Fetch_Array($umreq)) {
Sql_Query(sprintf('update %s set userid = %d, entered = "%s" where userid = %d and entered = "%s"',$GLOBALS["tables"]["usermessage"],$orig[0],$um["entered"],$duplicate["id"],$um["entered"]));
}
$history_entry .="\nUsermessages moved from " . $duplicate["id"] . " to " . $orig[0];
if (VERBOSE) echo "
Usermessages moved from " . $duplicate["id"] . " to " . $orig[0];
//now move any bounce messages from the duplicate to the main user
$bncreq = Sql_Query("select * from {$GLOBALS["tables"]["user_message_bounce"]} where user = ".$duplicate["id"]);
while ($bnc = Sql_Fetch_Array($bncreq)) {
Sql_Query(sprintf('update %s set user = %d, time = "%s" where user = %d and time = "%s"',$GLOBALS["tables"]["user_message_bounce"],$orig[0],$bnc["time"],$duplicate["id"],$bnc["time"]));
}
$history_entry .="\n Bounce messages moved from " . $duplicate["id"] . " to " . $orig[0];
if (VERBOSE) echo "
Bounce messages moved from " . $duplicate["id"] . " to " . $orig[0];
//now copy any attributes from the duplicate to the main user; this happens only if that attribute doesn't already exist (or is blank or NULL) for the main user
$query="select * from {$GLOBALS["tables"]["user_attribute"]} where userid = ".$duplicate["id"];
//if (VERBOSE) echo "
" . $query ;
$attreg = Sql_Query($query);
while ($att = Sql_Fetch_Array($attreg)) {
//only do it for values that actually have something in them
if ($att["value"] <> "" && $att["value"] != NULL ) {
//updates attributeid/userid from orig with value from duplicate
//if the value exists (but is blank or NULL) we update it; if it doesn't exist we create it
$query = sprintf('select * from %s where attributeid = %d and userid = %d',$GLOBALS["tables"]["user_attribute"], $att["attributeid"], $orig[0]);
//if (VERBOSE) echo $query . "
";
$attorigreg= Sql_Query($query);
$attorig= Sql_Fetch_Array($attorigreg);
if ( !Sql_Num_Rows($attorigreg) ) {
$query=sprintf('insert into %s (attributeid, userid, value ) values ( %d, %d, "%s" ) ',$GLOBALS["tables"]["user_attribute"], $att["attributeid"], $orig[0], $att["value"] );
//if (VERBOSE) echo $query . "
";
Sql_Query($query);
$history_entry .= "\n attribute " . $att["attributeid"] . " with value " . $att["value"] . " copied from " . $duplicate["id"] . " to " . $orig[0];
if (VERBOSE) echo "
Transferring attribute=" . $att["attributeid"] . " value=". $att["value"];
} else if ( $attorig ["value"] == "" || $attorig ["value"] == NULL ) {
$query=sprintf('update %s set value = "%s" where attributeid = %d and userid = %d ',$GLOBALS["tables"]["user_attribute"], $att["value"], $att["attributeid"], $orig[0] );
//if (VERBOSE) echo $query . "
";
Sql_Query($query);
$history_entry .= "\n attribute " . $att["attributeid"] . " with value " . $att["value"] . " copied from " . $duplicate["id"] . " to " . $orig[0];
if (VERBOSE) echo "
Transferring attribute=" . $att["attributeid"] . " value=". $att["value"];
}
} //end if value non empty/non-null
}//end while
//now move all list subscriptions from the duplicate to the main users
$query= "select * from {$GLOBALS["tables"]["listuser"]} where userid = ".$duplicate["id"];
//if (VERBOSE) echo $query . "
";
$subreg = Sql_Query($query);
while ($sub = Sql_Fetch_Array($subreg)) {
//if the list subscription already exists, just leave it. Otherwise, copy it over.
$query=sprintf('select * from %s where listid = %d and userid = %d', $GLOBALS["tables"]["listuser"], $sub["listid"],$orig[0]);
//if (VERBOSE) echo $query . "
";
$res=Sql_Query($query );
if ( !Sql_Num_Rows($res) ) {
$query=sprintf('insert into %s (listid, userid, entered, modified ) values ( %d, %d, "%s", now()
) ',$GLOBALS["tables"]["listuser"], $sub["listid"], $orig[0], $sub["entered"]);
//if (VERBOSE) echo $query . "
";
Sql_Query($query) ;
$history_entry .="\nSubscription to list " . $sub["listid"] . " copied from " . $duplicate["id"] . " to " . $orig[0];
if (VERBOSE) echo "
Subscription to list " . $sub["listid"] . " copied from " . $duplicate["id"] . " to " . $orig[0];
}
}
//now move the rssfrequency over (if it doesn't exist in the main record already)
$query="select rssfrequency from {$GLOBALS["tables"]["user"]} where id = ".$orig[0];
//if (VERBOSE) echo $query . "
";
$rssorigreg = Sql_Query($query);
$rssorig = Sql_Fetch_Array($rssorigreg);
$query="select rssfrequency from {$GLOBALS["tables"]["user"]} where id = ".$duplicate["id"];
//if (VERBOSE) echo $query . "
";
$rssreg = Sql_Query($query);
$rss = Sql_Fetch_Array($rssreg);
//if there is an rss frequency in the duplicate & the rss frequency doesn't exist
//or is empty or is NULL in the main entry, then replace with the "Duplicate" value
if ( ( $rss["rssfrequency"]<>"" && $rss["rssfrequency"] != NULL ) &&
( !$rssorigreg || $rssorig["rssfrequency"] == "" || $rssorig["rssfrequency"] == NULL ) ) {
$query=sprintf('update %s set rssfrequency = "%s" where id = %d ',
$GLOBALS["tables"]["user"], $rss["rssfrequency"], $orig[0] );
//if (VERBOSE) echo $query . "
";
Sql_Query($query) ;
$history_entry .="\n rssfrequency " .$rss["rssfrequency"] . " from user " . $duplicate["id"] . " copied to " . $orig[0];
if (VERBOSE) echo "
rssfrequency " .$rss["rssfrequency"] . " from user " . $duplicate["id"] . " copied to " . $orig[0];
}
if (VERBOSE) echo "
";
//now add entry to user_user_history to indicate that this change has taken place
addUserHistory($email,"Update by ".adminName($_SESSION["logindetails"]["id"]),$history_entry);
//bhugh, 10/5/2007
//eliminate this step, which deletes all list subscriptions for duplicate users
// the trouble is that this is destructive and non-reversable!
//if the admin wants to just delete all duplicate users, that is easy to do . . .
//in the meanwhile, we won't mess it up
//Sql_Query("delete from {$GLOBALS["tables"]["listuser"]} where userid = ".$duplicate["id"]);
} else {
print " ".$GLOBALS['I18N']->get("no user found");
}
flush();
} else {
print "-> ".$GLOBALS['I18N']->get("unable to find original email");
}
}