这几天项目因为用到论坛,需要我们的用户帐号密码与Discuz X2的用户帐号同步起来,也就是我们平台的注册了用户,在Discuz也需要新增用户,在平台修改了密码或者Discuz修改了密码,要相互同步,研究了一下Discuz的代码,其实只是只要新增一个php文件,放到根目录,例如my_add_user.php,供自己网站调用,大概代码如下:
$clientip=$_SERVER["REMOTE_ADDR"];
echo $clientip;
$client_ip!="127.0.0.1" or die('no access!');
require_once './source/class/class_core.php';
require './source/class/class_admincp.php';
require './source/function/function_misc.php';
require './source/function/function_admincp.php';
$discuz = & discuz_core::instance();
$discuz->init();
add_new_user('kronus','test','test@test.com',10);//测试
change_password('kronus', 'test','test2');//测试
function add_new_user($newusername, $newpassword,$newemail,$newgroupid)
{
if(DB::result_first("SELECT count(*) FROM ".DB::table('common_member')." WHERE username='$newusername'")) {
}
loaducenter();
$uid = uc_user_register($newusername, $newpassword, $newemail);
if($uid <= 0) {
if($uid == -1) {
} elseif($uid == -2) {
} elseif($uid == -3) {
if(empty($_G['gp_confirmed'])) {
} else {
list($uid,, $newemail) = uc_get_user($newusername);
}
} elseif($uid == -4) {
} elseif($uid == -5) {
} elseif($uid == -6) {
}
}
$query = DB::query("SELECT groupid, radminid, type FROM ".DB::table('common_usergroup')." WHERE groupid='$newgroupid'");
$group = DB::fetch($query);
$newadminid = in_array($group['radminid'], array(1, 2, 3)) ? $group['radminid'] : ($group['type'] == 'special' ? -1 : 0);
if($group['radminid'] == 1) {
}
if(in_array($group['groupid'], array(5, 6, 7))) {
}
$timestamp = time();
$data = array(
'uid' => $uid,
'username' => $newusername,
'password' => md5(random(10)),
'email' => $newemail,
'adminid' => $newadminid,
'groupid' => $newgroupid,
'regdate' => $timestamp,
'credits' => 0,
);
DB::insert('common_member', $data);
DB::insert('common_member_profile', array('uid' => $uid));
DB::insert('common_member_field_forum', array('uid' => $uid));
DB::insert('common_member_field_home', array('uid' => $uid));
DB::insert('common_member_status', array('uid' => $uid, 'regip' => 'Manual Acting', 'lastvisit' => $timestamp, 'lastactivity' => $timestamp));
$profile = $verifyarr = array();
loadcache('fields_register');
$init_arr = explode(',', $_G['setting']['initcredits']);
$count_data = array(
'uid' => $uid,
'extcredits1' => $init_arr[0],
'extcredits2' => $init_arr[1],
'extcredits3' => $init_arr[2],
'extcredits4' => $init_arr[3],
'extcredits5' => $init_arr[4],
'extcredits6' => $init_arr[5],
'extcredits7' => $init_arr[6],
'extcredits8' => $init_arr[7]
);
DB::insert('common_member_count', $count_data);
manyoulog('user', $uid, 'add');
$newusername = dstripslashes($newusername);
}
function change_password($username, $oldpw, $newpw) {
$sql = "SELECT username, uid, password, salt FROM ".DB::table('ucenter_members')." WHERE username='$username'";
$data = DB::fetch_first($sql);
if($data['password'] != md5(md5($oldpw).$data['salt'])) {
return -1;
}
$sqladd = $newpw ? "password='".md5(md5($newpw).$data['salt'])."'" : '';
$sqladd .= $email ? ($sqladd ? ',' : '')." email='$email'" : '';
if($sqladd) {
DB::query("UPDATE ".DB::table('ucenter_members')." SET $sqladd WHERE username='$username'");
return DB::affected_rows();
} else {
return -7;
}
}
?>
还有就是Discuz修改了密码,是在uc_client/model/user.php中处理:
在edit_user函数中处理:
function edit_user($username, $oldpw, $newpw, $email, $ignoreoldpw = 0, $questionid = '', $answer = '') {
$data = $this->db->fetch_first("SELECT username, uid, password, salt FROM ".UC_DBTABLEPRE."members WHERE username='$username'");
if($ignoreoldpw) {
$isprotected = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."protectedmembers WHERE uid = '$data[uid]'");
if($isprotected) {
return -8;
}
}
if(!$ignoreoldpw && $data['password'] != md5(md5($oldpw).$data['salt'])) {
return -1;
}
$sqladd = $newpw ? "password='".md5(md5($newpw).$data['salt'])."'" : '';
$sqladd .= $email ? ($sqladd ? ',' : '')." email='$email'" : '';
if($questionid !== '') {
if($questionid > 0) {
$sqladd .= ($sqladd ? ',' : '')." secques='".$this->quescrypt($questionid, $answer)."'";
} else {
$sqladd .= ($sqladd ? ',' : '')." secques=''";
}
}
if($sqladd || $emailadd) {
$this->db->query("UPDATE ".UC_DBTABLEPRE."members SET $sqladd WHERE username='$username'");
$result = $this->db->affected_rows();
//!@调用自己平台的帐号和密码
//下面就是往我们平台修改用户密码
$this->db->query("update user.members set password='$newpw' where account='$username'");
return $result;
} else {
return -7;
}
}