body<=
'href="/login.bml?ret=1"'}))
unless $remote;
my $authas = $GET{'authas'} || $remote->{'user'};
my $u = LJ::get_authas_user($authas);
return LJ::bad_input($ML{'error.invalidauth'})
unless $u;
return ""
unless $u->{'journaltype'} =~ /[PCS]/;
my %err;
my ($ren, $newuser);
my $ret = '';
# authas switcher form
$ret .= "
\n\n";
$ret .= BML::ml('.intent', {'aopts0' => 'href="./"', 'aopts1' => 'href="/pay/?item=rename"'});
my $err = sub {
my $area = shift;
return unless defined $err{$area};
$ret .= " $err{$area}";
};
my $form = sub {
$ret .= "";
return $ret;
};
return $form->() unless LJ::did_post();
my $dbh = LJ::get_db_writer();
unless ($FORM{'userid'} == $u->{'userid'}) {
$err{'user'} = $ML{'.error.differentusername'};
}
$newuser = LJ::canonical_username($FORM{'newuser'});
if (! $newuser) {
$err{'newuser'} = $ML{'.error.username'};
} elsif ($newuser eq $u->{'user'}) {
$err{'newuser'} = $ML{'.error.same'};
}
$ren;
if ($FORM{'token'} =~ /^([0-9a-f]{6,6})(\w{10,10})$/) {
$ren = $dbh->selectrow_hashref("SELECT * FROM renames WHERE renid=? AND token=?",
undef, hex $1, $2);
if (! $ren) {
$err{'token'} = $ML{'.error.token'};
} elsif ($ren->{'rendate'}) {
if ($ren->{'fromuser'} eq $u->{'user'} &&
$ren->{'touser'} eq $newuser) {
return $ML{'.success.usernamerenamed'};
} else {
$err{'token'} = $ML{'.error.usedtoken'};
}
}
} else {
$err{'token'} = $ML{'.error.token'};
}
return $form->() if %err;
my $tou = LJ::load_user($newuser);
my $move_away = 0;
my $exname = undef;
if ($tou) {
# decide if we can move away the existing destination username
if ($tou->{'statusvis'} eq "X") {
# expunged usernames can be moved away. they're already deleted.
$move_away = 1;
} elsif (lc($tou->{'email'}) eq lc($u->{'email'}) &&
$tou->{'statusvis'} eq "V" &&
$tou->{'journaltype'} eq "P") {
if ($tou->{'password'} eq $u->{'password'}) {
if ($tou->{'status'} ne 'A' || $u->{'status'} ne 'A') {
$err{'newuser'} = $ML{'.error.notvalidated'};
} else {
$move_away = 1;
}
} else {
$err{'newuser'} = $ML{'.error.badpass'};
}
} else {
$err{'newuser'} = $ML{'.error.usernametaken'};
}
# if we will be moving the destination username away,
# we need to allocate a name for it
if ($move_away) {
my $tempname = $tou->{'user'};
$tempname = substr($tempname, 0, 9) if length($tempname) > 9;
for (1..10) {
# if it exists, first try the exname found on the
# previous (confirmation) page
if ($_ == 1 && $FORM{'exname'}) {
$exname = $FORM{'exname'};
# otherwise we either didn't have one or it's been
# taken in the meantime?
} else {
$exname = "ex_$tempname" . int(rand(999));
}
# check to see if this exname already exists
unless ($dbh->selectrow_array("SELECT COUNT(*) FROM user WHERE user=?",
undef, $exname)) {
# name doesn't already exist, use this one
last;
}
# name existed, try and get another
if ($_ >= 10) {
return $ML{'.error.allocating'};
}
}
}
}
return $form->() if %err;
my $confirm = sub {
$ret .= "";
return $ret;
};
# display confirmation page if "form" is okay and no confirmation sent
return $confirm->() if LJ::did_post && ! exists $FORM{'yes'};
# error check confirmation
unless ($FORM{'yes'} eq "YES") {
$err{'yes'} = $ML{'.error.readit'};
}
return $confirm->() if %err;
my $do_rename = sub {
my ($from, $to) = map { LJ::canonical_username($_) } @_;
return 0 unless $from ne "" && $to ne "";
my $u = LJ::load_user($from);
return 0 unless $u;
foreach my $table (qw(user useridmap overrides style))
{
$dbh->do("UPDATE $table SET user=? WHERE user=?",
undef, $to, $from);
return 0 if $dbh->err;
}
LJ::memcache_kill($u, "userid");
LJ::MemCache::delete("uidof:$from");
LJ::MemCache::delete("uidof:$to");
LJ::infohistory_add($u, 'username', $from);
# tell all web machines to clear their caches for this userid/name mapping
LJ::procnotify_add("rename_user", { 'userid' => $u->{'userid'},
'user' => $u->{'user'} });
return 1;
};
if ($tou && $move_away) {
# time to move this one out of the way we already
# have a valid $exname from above
unless ($exname && $do_rename->($tou->{'user'}, $exname) &&
$dbh->do("INSERT INTO renames (token, payid, userid, fromuser, touser, rendate) " .
"VALUES (?,?,?,?,?,NOW())",
undef, "[moveaway]", 0, $tou->{'userid'}, $tou->{'user'}, $exname))
{
return $ML{'.error.reserved'};
}
}
unless ($do_rename->($u->{'user'}, $newuser)) {
return $ML{'.error.unknown'};
}
$dbh->do("UPDATE renames SET userid=?, fromuser=?, touser=?, rendate=NOW() WHERE renid=?",
undef, $u->{'userid'}, $u->{'user'}, $newuser, $ren->{'renid'});
if ($u->{journaltype} eq 'P') {
if ($FORM{'opt_delfriends'}) {
# delete friends
my $friends = LJ::get_friends($u, undef, undef, 'force') || {};
LJ::remove_friend($u, [ keys %$friends ]);
# delete access to post to communities
LJ::clear_rel('*', $u, 'P');
# delete friend-ofs that are communities
# TAG:fr:bml_rename_use:get_member_of
my $users = $dbh->selectcol_arrayref(qq{
SELECT u.userid FROM friends f, user u
WHERE f.friendid=$u->{'userid'} AND
f.userid=u.userid and u.journaltype <> 'P'
});
if ($users && @$users) {
my $in = join(',', @$users);
$dbh->do("DELETE FROM friends WHERE friendid=$u->{'userid'} AND userid IN ($in)");
LJ::memcache_kill($_, "friends") foreach @$users;
}
}
if ($FORM{'opt_delfriendofs'}) {
# delete people (only people) who list this user as a friend
my $users = $dbh->selectcol_arrayref(qq{
SELECT u.userid FROM friends f, user u
WHERE f.friendid=$u->{'userid'} AND
f.userid=u.userid and u.journaltype = 'P'
});
if ($users && @$users) {
my $in = join(',', @$users);
$dbh->do("DELETE FROM friends WHERE friendid=$u->{'userid'} AND userid IN ($in)");
LJ::memcache_kill($_, "friends") foreach @$users;
}
}
# delete friend of memcaching, as either path might have done it
LJ::MemCache::delete([ $u->{userid}, "friendofs:$u->{userid}" ]);
}
my $id = LJ::create_account({
'user' => $u->{'user'},
'password' => '',
'name' => '[renamed acct]',
});
my $alias_changed = $dbh->do("UPDATE email_aliases SET alias=? WHERE alias=?",
undef, "$newuser\@$LJ::USER_DOMAIN",
"$u->{'user'}\@$LJ::USER_DOMAIN");
if ($u->{journaltype} ne 'P' || $FORM{'opt_redir'}) {
LJ::update_user($id, { raw => "journaltype='R', statusvis='R', statusvisdate=NOW()" });
LJ::set_userprop($dbh, $id, "renamedto", $newuser);
if ($alias_changed > 0) {
$dbh->do("INSERT INTO email_aliases VALUES (?,?)", undef,
"$u->{'user'}\@$LJ::USER_DOMAIN",
$u->{'email'});
}
} else {
LJ::update_user($id, { journaltype => $u->{journaltype}, raw => "statusvis='D', statusvisdate=NOW()" });
}
$u->kill_session;
# overwrite $ret and give success page
$ret = "