A-A+

phpcms采用新浪微博登陆不二次登陆

2015年06月14日 phpcms 暂无评论 阅读 81 次

phpcms采用新浪微博登陆不二次登陆,直接数据库写入.

默认是判断检查会员是否绑定,已绑定直接登录,未绑定提示注册/绑定页面

如果已经绑定,则直接登录,没绑定,会写入数据库后,在调用一次判断(就是把判断的代码在复制过来一次,),这时候应该已经绑定了,比较笨的写法,望改进.

public function public_sina_login() {
define('WB_AKEY', pc_base::load_config('system', 'sina_akey'));
define('WB_SKEY', pc_base::load_config('system', 'sina_skey'));
define('WEB_CALLBACK', APP_PATH.'index.php?m=member&c=index&a=public_sina_login&callback=1');
pc_base::load_app_class('saetv2.ex', '' ,0);
$this->_session_start();

if(isset($_GET['callback']) && trim($_GET['callback'])) {
$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
if (isset($_REQUEST['code'])) {
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = WEB_CALLBACK;
try {
$token = $o->getAccessToken('code', $keys);
} catch (OAuthException $e) {
}
}
if ($token) {
$_SESSION['token'] = $token;
}
$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token'] );
$ms = $c->home_timeline(); // done
$uid_get = $c->get_uid();
$uid = $uid_get['uid'];
$me = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
if(CHARSET != 'utf-8') {
$me['name'] = iconv('utf-8', CHARSET, $me['name']);
$me['location'] = iconv('utf-8', CHARSET, $me['location']);
$me['description'] = iconv('utf-8', CHARSET, $me['description']);
$me['screen_name'] = iconv('utf-8', CHARSET, $me['screen_name']);
}
if(!empty($me['id'])) {
//检查connect会员是否绑定,已绑定直接登录,未绑定提示注册/绑定页面
$where = array('connectid'=>$me['id'], 'from'=>'sina');
$r = $this->db->get_one($where);

//connect用户已经绑定本站用户
if(!empty($r)) {
//读取本站用户信息,执行登录操作
var_dump($r);
$password = $r['password'];
$this->_init_phpsso();
$synloginstr = $this->client->ps_member_synlogin($r['phpssouid']);
$userid = $r['userid'];
$groupid = $r['groupid'];
$username = $r['username'];
$nickname = empty($r['nickname']) ? $username : $r['nickname'];
$this->db->update(array('lastip'=>ip(), 'lastdate'=>SYS_TIME, 'nickname'=>$me['name']), array('userid'=>$userid));

if(!$cookietime) $get_cookietime = param::get_cookie('cookietime');
$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
$cookietime = $_cookietime ? TIME + $_cookietime : 0;

$phpcms_auth = sys_auth($userid."t".$password, 'ENCODE', get_auth_key('login'));

param::set_cookie('auth', $phpcms_auth, $cookietime);
param::set_cookie('_userid', $userid, $cookietime);
param::set_cookie('_username', $username, $cookietime);
param::set_cookie('_groupid', $groupid, $cookietime);
param::set_cookie('cookietime', $_cookietime, $cookietime);
param::set_cookie('_nickname', $nickname, $cookietime);
$forward = isset($_GET['forward']) && !empty($_GET['forward']) ? $_GET['forward'] : 'index.php?m=member&c=index';
showmessage(L('login_success').$synloginstr, $forward);

} else {
//弹出绑定注册页面
$_SESSION = array();
$_SESSION['connectid'] = $me['id'];
$_SESSION['from'] = 'sina';
$connect_username = $me['name'];
$connect_username = $me['name'];
//SELECT max(userid) maxid FROM user

$where="SELECT MAX(phpssouid) maxid FROM `v9_member` WHERE 1";
$info = $this->db->query($where);
$row = mysql_fetch_object($info);
// $row =Myspl_fetch_array($info,MYSQL_NUM);
$maxid = $row->maxid;
$phpssouid = $maxid+1;

//写入数据库
$this->db->insert(array('nickname'=>$me['name'],'regip'=>ip(),'regdate'=>SYS_TIME,'img'=>$me['avatar_large'],'phpssouid'=>$phpssouid,'password'=>'','groupid' =>'2','lastdate'=>SYS_TIME, 'lastip'=>ip(),'connectid'=>$me['id'],'from'=>'sina'));
//加载用户模块配置
$member_setting = getcache('member_setting');
if(!$member_setting['allowregister']) {
showmessage(L('deny_register'), 'index.php?m=member&c=index&a=login');
}

//获取用户siteid
$siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
//过滤非当前站点会员模型
$modellist = getcache('member_model', 'commons');
foreach($modellist as $k=>$v) {
if($v['siteid']!=$siteid || $v['disabled']) {
unset($modellist[$k]);
}
}
if(empty($modellist)) {
showmessage(L('site_have_no_model').L('deny_register'), HTTP_REFERER);
}

$modelid = 10; //设定默认值
if(array_key_exists($modelid, $modellist)) {
//获取会员模型表单
require CACHE_MODEL_PATH.'member_form.class.php';
$member_form = new member_form($modelid);
$this->db->set_model($modelid);
$forminfos = $forminfos_arr = $member_form->get();

//万能字段过滤
foreach($forminfos as $field=>$info) {
if($info['isomnipotent']) {
unset($forminfos[$field]);
} else {
if($info['formtype']=='omnipotent') {
foreach($forminfos_arr as $_fm=>$_fm_value) {
if($_fm_value['isomnipotent']) {
$info['form'] = str_replace('{'.$_fm.'}',$_fm_value['form'], $info['form']);
}
}
$forminfos[$field]['form'] = $info['form'];
}
}
}

$formValidator = $member_form->formValidator;
}
// include template('member', 'connect');
// begin of this
define('WB_AKEY', pc_base::load_config('system', 'sina_akey'));
define('WB_SKEY', pc_base::load_config('system', 'sina_skey'));
define('WEB_CALLBACK', APP_PATH.'index.php?m=member&c=index&a=public_sina_login&callback=1');
pc_base::load_app_class('saetv2.ex', '' ,0);
$this->_session_start();

if(isset($_GET['callback']) && trim($_GET['callback'])) {
$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
if (isset($_REQUEST['code'])) {
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = WEB_CALLBACK;
try {
$token = $o->getAccessToken('code', $keys);
} catch (OAuthException $e) {
}
}
if ($token) {
$_SESSION['token'] = $token;
}
$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token'] );
$ms = $c->home_timeline(); // done
$uid_get = $c->get_uid();
$uid = $uid_get['uid'];
$me = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
if(CHARSET != 'utf-8') {
$me['name'] = iconv('utf-8', CHARSET, $me['name']);
$me['location'] = iconv('utf-8', CHARSET, $me['location']);
$me['description'] = iconv('utf-8', CHARSET, $me['description']);
$me['screen_name'] = iconv('utf-8', CHARSET, $me['screen_name']);
}
if(!empty($me['id'])) {
//检查connect会员是否绑定,已绑定直接登录,未绑定提示注册/绑定页面
// $where = array('connectid'=>$me['id'], 'from'=>'sina');
// $r = $this->db->get_one($where);
// var_dump($me['id']);
$where =" SELECT * FROM `phpcms`.`v9_member` WHERE `connectid` = ".$me['id']." AND `from` = 'sina' LIMIT 1 " ;

$r = $this->db->query($where);
$r=mysql_fetch_assoc($r);

// var_dump($result);exit();

}
//connect用户已经绑定本站用户
if(!empty($r)) {
//读取本站用户信息,执行登录操作

$password = $r['password'];
$this->_init_phpsso();
$synloginstr = $this->client->ps_member_synlogin($r['phpssouid']);
$userid = $r['userid'];
$groupid = $r['groupid'];
$username = $r['username'];
$nickname = empty($r['nickname']) ? $username : $r['nickname'];
// $this->db->update(array('lastip'=>ip(), 'lastdate'=>SYS_TIME, 'nickname'=>$me['name']), array('userid'=>$userid));

if(!$cookietime) $get_cookietime = param::get_cookie('cookietime');
$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
$cookietime = $_cookietime ? TIME + $_cookietime : 0;

$phpcms_auth = sys_auth($userid."t".$password, 'ENCODE', get_auth_key('login'));

param::set_cookie('auth', $phpcms_auth, $cookietime);
param::set_cookie('_userid', $userid, $cookietime);
param::set_cookie('_username', $username, $cookietime);
param::set_cookie('_groupid', $groupid, $cookietime);
param::set_cookie('cookietime', $_cookietime, $cookietime);
param::set_cookie('_nickname', $nickname, $cookietime);
$forward = isset($_GET['forward']) && !empty($_GET['forward']) ? $_GET['forward'] : 'index.php?m=member&c=index';
showmessage(L('login_success').$synloginstr, $forward);

}
}

// endo of this
}
} else {
showmessage(L('login_failure'), 'index.php?m=member&c=index&a=login');
}
} else {
$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
$aurl = $o->getAuthorizeURL(WEB_CALLBACK);
include template('member', 'connect_sina');
}
}

给我留言

Copyright © web前端技术开发个人博客 保留所有权利  京ICP备14060653号 Theme  Ality

用户登录