private function _syncUCenter($uid, $email, $uname, $password) { if (UC_SYNC) { $uc_uid = uc_user_register($uname, $password, $email); if ($uc_uid) { ts_add_ucenter_user_ref($uid, $uc_uid, $uname); } } }
/** * UC登录或者注册 * @param string $username * @param string $password * @param string $is_remember_me 是否记住登录 * @return bool */ private function ucLogin($username, $password, $is_remember_me) { //载入UC客户端SDK include_once SITE_PATH . '/api/uc_client/client.php'; //1. 获取UC信息. if ($this->isValidEmail($username)) { $use_email = true; $uc_login_type = 2; } else { $use_email = false; $uc_login_type = 0; } $uc_user = uc_user_login($username, $password, $uc_login_type); //2. 已经同步过的直接登录 $uc_user_ref = ts_get_ucenter_user_ref('', $uc_user['0'], ''); if ($uc_user_ref['uid'] && $uc_user_ref['uc_uid'] && $uc_user[0] > 0) { //登录本地帐号 $result = $uc_user_ref['uid'] > 0 ? $this->_recordLogin($uc_user_ref['uid'], $is_remember_me) : false; if ($result) { $this->success .= uc_user_synlogin($uc_user[0]); return true; } else { $this->error = '登录失败,请重试'; return false; } } //3. 关联表无、获取本地帐号信息. $ts_user = $this->getLocalUser($username, $password); // 调试用-写log // $log_message = "============================ \n " // .date('Y-m-d H:i:s')." \n ".$_SERVER['REQUEST_URI']." \n " // .var_export($uc_user,true)." \n " // .var_export($ts_user,true)." \n " // .var_export($uc_user_ref,true)." \n "; // $log_file = SITE_PATH."/ts_uc_log.txt"; // $result = error_log($log_message,3,$log_file); //4. 关联表无、UC有、本地有的 if ($uc_user[0] > 0 && $ts_user['uid'] > 0) { $result = ts_add_ucenter_user_ref($ts_user['uid'], $uc_user[0], $uc_user[1], $uc_user[3]); if (!$result) { $this->error = '用户不存在或密码错误'; return false; } //登录本地帐号 $result = $this->_recordLogin($ts_user['uid'], $is_remember_me); if ($result) { $this->success .= uc_user_synlogin($uc_user[0]); return true; } else { $this->error = '登录失败,请重试'; return false; } } //5. 关联表无、UC有、本地无的 if ($uc_user[0] > 0 && !$ts_user['uid']) { //写入本地系统 $login_salt = rand(11111, 99999); $map['uname'] = $uc_user[1]; $map['sex'] = 1; $map['login_salt'] = $login_salt; $map['password'] = md5(md5($uc_user[2]) . $login_salt); $map['login'] = $map['email'] = $uc_user[3]; $map['reg_ip'] = get_client_ip(); $map['ctime'] = time(); $map['is_audit'] = 1; $map['is_active'] = 1; $map['first_letter'] = getFirstLetter($uname); //如果包含中文将中文翻译成拼音 if (preg_match('/[\\x7f-\\xff]+/', $map['uname'])) { //昵称和呢称拼音保存到搜索字段 $map['search_key'] = $map['uname'] . ' ' . model('PinYin')->Pinyin($map['uname']); } else { $map['search_key'] = $map['uname']; } $ts_uid = model('User')->add($map); if (!$ts_uid) { $this->error = '本地用户注册失败,请联系管理员'; return false; } //写入关联表 $result = ts_add_ucenter_user_ref($ts_uid, $uc_user[0], $uc_user[1], $uc_user[3]); if (!$result) { $this->error = '用户不存在或密码错误'; return false; } // 添加至默认的用户组 $registerConfig = model('Xdata')->get('admin_Config:register'); $userGroup = empty($registerConfig['default_user_group']) ? C('DEFAULT_GROUP_ID') : $registerConfig['default_user_group']; model('UserGroupLink')->domoveUsergroup($ts_uid, implode(',', $userGroup)); // 添加双向关注用户 $eachFollow = $registerConfig['each_follow']; if (!empty($eachFollow)) { model('Follow')->eachDoFollow($ts_uid, $eachFollow); } // 添加默认关注用户 $defaultFollow = $registerConfig['default_follow']; $defaultFollow = array_diff(explode(',', $defaultFollow), explode(',', $eachFollow)); if (!empty($defaultFollow)) { model('Follow')->bulkDoFollow($ts_uid, $defaultFollow); } //登录本地帐号 $result = $this->_recordLogin($ts_uid, $is_remember_me); if ($result) { $this->success .= uc_user_synlogin($uc_user[0]); return true; } else { $this->error = '登录失败,请重试'; return false; } } //6. 关联表无、UC无、本地有 if ($uc_user[0] < 0 && $ts_user['uid'] > 0) { //写入UC $uc_uid = uc_user_register($ts_user['uname'], $password, $ts_user['email'], '', '', get_client_ip()); if ($uc_uid > 0) { $this->error = 'UC帐号注册失败,请联系管理员'; return false; } //写入关联表 $result = ts_add_ucenter_user_ref($ts_user['uid'], $uc_uid, $ts_user['uname'], $ts_user['email']); if (!$result) { $this->error = '用户不存在或密码错误'; return false; } //登录本地帐号 $result = $this->_recordLogin($ts_user['uid'], $is_remember_me); if ($result) { $this->success .= uc_user_synlogin($uc_uid); return true; } else { $this->error = '登录失败,请重试'; return false; } } //7. 关联表无、UC无、本地无的 $this->error = '用户不存在'; return false; }
/** * uc登陆或者注册。返回数组 * $result['user'] 用户信息。用于ts系统使用 * $result['login'] 同步登陆是否成功 * @param unknown_type $username * @param unknown_type $password */ public function ucLogin($username, $password) { if (isValidEmail($username)) { $user = service('Passport')->getLocalUser($username, $password); if (UC_SYNC && $user['uid']) { $uc_user_ref = ts_get_ucenter_user_ref($user['uid']); if ($uc_user_ref['uc_uid']) { $uc_user = uc_user_login($uc_user_ref['uc_uid'], $password, 1); if ($uc_user[0] == -1 || $uc_user[0] == -2) { $uc_user_ref = array(); } } else { if ($user['uname']) { $res_checkname = uc_user_checkname($user['uname']); if ($res_checkname >= -3 && $res_checkname <= -1) { $error_param = L('username'); } $res_checkemail = uc_user_checkemail($username); if ($res_checkemail >= -6 && $res_checkemail <= -4) { $error_param = $error_param ? $error_param . L('and_email') : 'Email'; } if ($error_param) { $message_data['title'] = L('sync_ucenter') . $error_param . L('sign_in_failed'); $message_data['content'] = L('you_of_site') . $error_param . L('ucenter_sign_in_failed') . $error_param . L('ucenter_clash') . U('home/Account/security') . L('ucenter_reset') . $error_param . '。'; $message_data['to'] = $user['uid']; model('Message')->postMessage($message_data, M('user')->getField('uid', 'admin_level=1')); } else { $uc_uid = uc_user_register($user['uname'], $password, $username); ts_add_ucenter_user_ref($user['uid'], $uc_uid, $user['uname']); $uc_user[0] = $uc_uid; } } } } } else { if (UC_SYNC) { $uc_user = uc_user_login($username, $password); if ($uc_user[0] > 0) { $uc_user_ref = ts_get_ucenter_user_ref('', $uc_user[0]); if (!$uc_user_ref) { // 注册 if ($this->isValidEmail($uc_user['3']) && $this->isEmailAvailable($uc_user['3'])) { $user['email'] = $uc_user['3']; } else { $message_data['title'] = L('ucenter_sync_email_clash'); $message_data['content'] = L('ucenter_email_used') . U('home/Account/bind') . L('ucenter_reset_email'); } if (isLegalUsername($uc_user['1']) && !M('user')->where("uname='{$uc_user['1']}'")->count()) { $user['uname'] = $uc_user['1']; } $user['password'] = md5($uc_user['2']); $user['ctime'] = time(); $user['is_active'] = 1; $user['uid'] = M('user')->add($user); if ($user['uid']) { $reg_from_ucenter = 1; ts_add_ucenter_user_ref($user['uid'], $uc_user['0'], $uc_user['1']); // 将用户添加到myop_userlog,以使漫游应用能获取到用户信息 $userlog = array('uid' => $user['uid'], 'action' => 'add', 'type' => '0', 'dateline' => time()); M('myop_userlog')->add($userlog); if (isset($message_data) && !empty($message_data)) { $message_data['to'] = $user['uid']; model('Message')->postMessage($message_data, M('user')->getField('uid', 'admin_level=1')); } //关联操作 //$this->registerRelation($user['uid']); } else { $user = array(); } } else { if ($username != $uc_user_ref['uc_username']) { ts_update_ucenter_user_ref('', $uc_user_ref['uc_uid'], $username); } $user = M('user')->where("uid={$uc_user_ref['uid']}")->find(); if (md5($password) != $user['password']) { M('user')->where("uid={$uc_user_ref['uid']}")->setField('password', md5($password)); } } } } else { $uc_user_ref = ts_get_ucenter_user_ref('', '', $username); if ($uc_user_ref['uid']) { $user = service('Passport')->getLocalUser($uc_user_ref['uid'], $password); } } } $result['login'] = ''; if ($user) { $result['login'] = UC_SYNC && $uc_user[0] ? uc_user_synlogin($uc_user[0]) : ''; } $result['user'] = $user; $result['reg_from_ucenter'] = $reg_from_ucenter; return $result; }
public function doModifyUCenter() { include_once SITE_PATH . '/api/uc_client/uc_sync.php'; if (UC_SYNC) { $uc_user_ref = ts_get_ucenter_user_ref($this->mid); if (!$uc_user_ref) { $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password']; if (uc_user_checkname($username) != 1 || !isLegalUsername($username) || M('user')->where("uname='{$username}' AND uid<>{$this->mid}")->count()) { $this->error('Tên tài khoản không hợp lệ hoặc đã tồn tại, hãy nhập lại'); } if (uc_user_checkemail($email) != 1 || M('user')->where("uname='{$email}' AND uid<>{$this->mid}")->count()) { $this->error('Email không hợp lệ hoặc đã tồn tại, hãy nhập lại'); } global $ts; if (md5($password) != $ts['user']['password']) { $this->error(L('password_error_retype')); } $uc_uid = uc_user_register($username, $password, $email); if ($uc_uid > 0) { ts_add_ucenter_user_ref($this->mid, $uc_uid, $username); $this->assign('jumpUrl', U('home/Account/security')); $this->success(L('ucenter_setting_success')); } else { $this->error(L('ucenter_setting_error')); } } else { redirect(U('home/Account/security')); } } else { redirect(U('home/Account/security')); } }
public function doRegister() { // 验证码 $verify_option = $this->_isVerifyOn('register'); if ($verify_option && md5($_POST['verify']) != $_SESSION['verify']) { $this->error(L('error_security_code')); } // 邀请码 $invite_code = h($_REQUEST['invite_code']); $invite_info = null; // 是否允许注册 $register_option = model('Xdata')->get('register:register_type'); if ($register_option === 'closed') { // 关闭注册 $this->error(L('reg_close')); } else { if ($register_option === 'invite') { //邀请注册 // 邀请方式 $invite_option = model('Invite')->getSet(); if ($invite_option['invite_set'] == 'close') { // 关闭邀请 $this->error(L('reg_invite_close')); } else { // 普通邀请 OR 使用邀请码 if (!$invite_code) { $this->error(L('reg_invite_warming')); } else { if (!($invite_info = $this->__getInviteInfo($invite_code))) { $this->error(L('reg_invite_code_error')); } } } } else { // 公开注册 if (!($invite_info = $this->__getInviteInfo($invite_code))) { unset($invite_code, $invite_info); } } } // 参数合法性检查 $required_field = array('email' => 'Email', 'nickname' => L('username'), 'password' => L('password'), 'repassword' => L('retype_password')); foreach ($required_field as $k => $v) { if (empty($_POST[$k])) { $this->error($v . L('not_null')); } } if (!$this->isValidEmail($_POST['email'])) { $this->error(L('email_format_error_retype')); } if (!$this->isValidNickName($_POST['nickname'])) { $this->error(L('username_format_error')); } if (strlen($_POST['password']) < 6 || strlen($_POST['password']) > 16 || $_POST['password'] != $_POST['repassword']) { $this->error(L('password_rule')); } if (!$this->isEmailAvailable($_POST['email'])) { $this->error(L('email_used_retype')); } // 是否需要Email激活 $need_email_activate = intval(model('Xdata')->get('register:register_email_activate')); // 注册 $data['email'] = $_POST['email']; $data['password'] = md5($_POST['password']); $data['uname'] = t($_POST['nickname']); $data['ctime'] = time(); $data['is_active'] = $need_email_activate ? 0 : 1; if (!($uid = D('User', 'home')->add($data))) { $this->error(L('reg_filed_retry')); } // 将用户添加到myop_userlog,以使漫游应用能获取到用户信息 $user_log = array('uid' => $uid, 'action' => 'add', 'type' => '0', 'dateline' => time()); M('myop_userlog')->add($user_log); // 将邀请码设置已用 model('Invite')->setInviteCodeUsed($invite_code); // 同步至UCenter if (UC_SYNC) { $uc_uid = uc_user_register($_POST['nickname'], $_POST['password'], $_POST['email']); //echo uc_user_synlogin($uc_uid); if ($uc_uid > 0) { ts_add_ucenter_user_ref($uid, $uc_uid, $data['uname']); } } if ($need_email_activate == 1) { // 邮件激活 $this->activate($uid, $_POST['email'], $invite_code); } else { // 置为已登陆, 供完善个人资料时使用 service('Passport')->loginLocal($uid); // 缓存邀请信息, 供完善个人资料后使用 $_SESSION["invite_info_{$uid}"] = $invite_info; if (!is_numeric(stripos($_POST['HTTP_REFERER'], dirname('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])))) { //注册完毕,跳回注册页之前 redirect($_POST['HTTP_REFERER']); } else { //注册完毕,跳转至帐号修改页 redirect(U('home/Public/userinfo')); } } }