Discuz!X2 用户改名程序

Discuz!X2 修改用户名小工具,可以安全的修改论坛和UCENTER中的关联信息。

在网上找了一些插件,发现没有对UCENTER数据库做检查,也没有对新的用户名做检查,容易产生错误。

[php]
<?php
require ‘./source/class/class_core.php’;
require ‘./source/function/function_forum.php’;

$discuz = & discuz_core::instance();
$discuz->init();

//加载UCENTER支持
loaducenter();

$olduserid = ”;
$oldusername = ‘76135’;
$newusername = ‘果子狸’;

DB::query("SET NAMES utf8;");
if(empty($olduserid) || !is_numeric($olduserid))
{
$user = DB::fetch_first("SELECT uid FROM ".UC_DBTABLEPRE."members WHERE username = ‘{$oldusername}’");
$olduserid = $user[‘uid’];
empty($olduserid) && exit(‘没有找到旧用户’);
}

if(empty($newusername) || !is_numeric($olduserid))
{
echo ‘请指定用户的UID或者用户名’;
exit;
}

$check = uc_user_checkname($newusername);
$error_info = array(
‘-1’ => ‘新用户名不合法’,
‘-2’ => ‘新用户名包含要不允许注册的词语’,
‘-3’ => ‘新用户名已经存在’,
);
$check !== 1 && exit($error_info[$check]);

//修改UCENTER中的记录
DB::query("UPDATE ".UC_DBTABLEPRE."members SET `username`='{$newusername}’ WHERE `uid`='{$olduserid}’");

//修改论坛数据表中的所有记录
$tables = array(
‘common_block’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘common_invite’ => array(‘id’ => ‘fuid’, ‘name’ => ‘fusername’),
‘common_member’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘common_member_security’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘common_mytask’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘common_report’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),

‘forum_thread’ => array(‘id’ => ‘authorid’, ‘name’ => ‘author’),
‘forum_post’ => array(‘id’ => ‘authorid’, ‘name’ => ‘author’),
‘forum_activityapply’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘forum_groupuser’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘forum_pollvoter’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘forum_postcomment’ => array(‘id’ => ‘authorid’, ‘name’ => ‘author’),
‘forum_ratelog’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),

‘home_album’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_blog’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_clickuser’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_docomment’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_doing’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_feed’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_feed_app’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_friend’ => array(‘id’ => ‘fuid’, ‘name’ => ‘fusername’),
‘home_friend_request’ => array(‘id’ => ‘fuid’, ‘name’ => ‘fusername’),
‘home_notification’ => array(‘id’ => ‘authorid’, ‘name’ => ‘author’),
‘home_pic’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_poke’ => array(‘id’ => ‘fromuid’, ‘name’ => ‘fromusername’),
‘home_share’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_show’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_specialuser’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘home_visitor’ => array(‘id’ => ‘vuid’, ‘name’ => ‘vusername’),

‘portal_article_title’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘portal_comment’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘portal_topic’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
‘portal_topic_pic’ => array(‘id’ => ‘uid’, ‘name’ => ‘username’),
);

foreach($tables as $table => $conf) {
DB::query("UPDATE ".DB::table($table)." SET `$conf[name]`='{$newusername}’ WHERE `$conf[id]`='{$olduserid}’");
}

echo ‘处理完成’;
exit;
?>
[/php]