A-A+

phpcmsv9加qq第三方登录获取头像

2015年06月09日 phpcms 暂无评论 阅读 101 次

phpcmsv9加qq第三方登录获取头像,参考代码

  //QQ登录功能
//QQ登陆oAuth2.0
public function public_qq_login2() {
//以下三个变量在cache/config/system.php中存在,在后台的设置
$appid = pc_base::load_config('system', 'qq_appid');
$appkey = pc_base::load_config('system', 'qq_appkey');
$callback = pc_base::load_config('system', 'qq_callback');
//引用腾讯发布的qqOAuth2 php类
pc_base::load_app_class('qqOAuth2', '', 0);
$info = new qqOAuth2($appid, $appkey, $callback);
$this->_session_start();
//记录跳转过来的地址
if (!empty($_GET['forward'])) {
$_SESSION['qqforward'] = $_GET['forward'];
} else if (!empty($_SESSION['qqforward'])) {
$goto = $_SESSION['qqforward'];
unset($_SESSION['qqforward']);
} else {
$goto = "index.php";
}
if (!isset($_GET['code'])) {
$info->redirect_to_login();
} else {
$code = $_GET['code'];
$_SESSION['openid'] = $info->get_openid($code); //调取QQ openid值
if (!empty($_SESSION['openid'])) {
$r = $this->db->get_one(array('connectid' => $_SESSION['openid'], 'from' => 'qq'));
if (!empty($r)) {
//QQ已存在于数据库,则直接转向登陆操作
$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'];
if (pc_base::load_config('system', 'phpsso')) {
$this->_init_phpsso();
if (!$this->client->ps_member_isavatar($r['phpssouid'])) {
$touxiang = $info->get_touxiang_info();
pc_base::load_app_class('collection', 'collection', 0); //引入smsapi类
$tx = new collection();
($tx->getpicbyurl($r['phpssouid'], $touxiang));
$this->client->ps_member_setavatar($r['phpssouid']);
}
}
$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_key = md5(pc_base::load_config('system', 'auth_key') . $this->http_user_agent);
$phpcms_auth = sys_auth($userid . "t" . $password, 'ENCODE', $phpcms_auth_key);
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';
showmessage(L('login_success') . $synloginstr, $goto);
}else {
//未存在于数据库中,跳去完善资料页面。页面预置用户名(QQ返回是UTF8编码,如有需要进行转码)
$user = $info->get_user_info();
$r = $this->db->get_one(array('nickname' => $user));
if (!empty($r))
$user = $user . "niwa";
$_SESSION['from'] = 'qq';
if (CHARSET != 'utf-8') {
$connect_username = iconv('utf-8', CHARSET, $user['nickname']);
}//转编码
else {
$connect_username = $user['nickname'];
}
$siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1; //
获取用户siteid
if (!defined('SITEID')) {
define('SITEID', $siteid);
}//定义站点id常量
$member_setting = getcache('member_setting'); //加载用户模块配置
if (!$member_setting['allowregister']) {
showmessage(L('deny_register'), 'index.php?m=member&c=index&a=login');
}
header("Cache-control: private");
$userinfo = array();
$userinfo['encrypt'] = create_randomstr(6);
$qqrand = random(10, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
$userinfo['nickname'] = $user;
$userinfo['username'] = $user;
$userinfo['password'] = $qqrand;
$userinfo['email'] = $qqrand . "@qq.com";
$userinfo['modelid'] = isset($_POST['modelid']) ? intval($_POST['modelid']) : 10;
$userinfo['regip'] = ip();
$userinfo['point'] = $member_setting['defualtpoint'] ? $member_setting['defualtpoint'] : 0;
$userinfo['amount'] = $member_setting['defualtamount'] ? $member_setting['defualtamount'] : 0;
$userinfo['regdate'] = $userinfo['lastdate'] = SYS_TIME;
$userinfo['siteid'] = $siteid;
$userinfo['connectid'] = isset($_SESSION['openid']) ? $_SESSION['openid'] : '';
$userinfo['from'] = isset($_SESSION['from']) ? $_SESSION['from'] : '';
if (pc_base::load_config('system', 'phpsso')) {
$this->_init_phpsso();
$status = $this->client->ps_member_register($userinfo['username'], $userinfo['password'],
$userinfo['email'], $userinfo['regip'], $userinfo['encrypt']);
//判断是否注册成功
if ($status > 0) {
$userinfo['phpssouid'] = $status;
//传入phpsso为明文密码,加密后存入phpcms_v9
$password = $userinfo['password'];
$userinfo['password'] = password($userinfo['password'], $userinfo['encrypt']);
$userid = $this->db->insert($userinfo, 1);
if ($member_setting['choosemodel']) { //如果开启选择模型
//通过模型获取会员信息
require_once CACHE_MODEL_PATH . 'member_input.class.php';
require_once CACHE_MODEL_PATH . 'member_update.class.php';
$member_input = new member_input($userinfo['modelid']);
$_POST['info'] = array_map('htmlspecialchars', $_POST['info']);
$user_model_info = $member_input->get($_POST['info']);
$user_model_info['userid'] = $userid;
//插入会员模型数据
$this->db->set_model($userinfo['modelid']);
$this->db->insert($user_model_info);
}
//执行登陆操作
if ($userid > 0) {
//执行登陆操作
if (!$cookietime)
$get_cookietime = param::get_cookie('cookietime');
$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
$cookietime = $_cookietime ? TIME + $_cookietime : 0;
if ($userinfo['groupid'] == 7) {
param::set_cookie('_username', $userinfo['username'], $cookietime);
param::set_cookie('email', $userinfo['email'], $cookietime);
} else {
$phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key') . $this-
>http_user_agent);
$phpcms_auth = sys_auth($userid . "t" . $userinfo['password'], 'ENCODE',
$phpcms_auth_key);
param::set_cookie('auth', $phpcms_auth, $cookietime);
param::set_cookie('_userid', $userid, $cookietime);
param::set_cookie('_username', $userinfo['username'], $cookietime);
param::set_cookie('_nickname', $userinfo['nickname'], $cookietime);
param::set_cookie('_groupid', $userinfo['groupid'], $cookietime);
param::set_cookie('cookietime', $_cookietime, $cookietime);
}
}
}
} //载入pc_base::load_config('system', 'phpsso')
showmessage(L('login_success') . $synloginstr, $goto);
}
}
}
}
//创建头像所在目录
public static function getpicbyurl ($uid, $picarray)
{
$dir1 = ceil($uid / 10000);
$dir2 = ceil($uid % 10000 / 1000);
$avatarfile = pc_base::load_config('system', 'upload_path_tx') . 'avatar/';
$dir = $avatarfile . $dir1 . '/' . $dir2 . '/' . $uid . '/';
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
$piclist = array('30x30','45x45','90x90','180x180');
$i= 0;
foreach ($picarray as $key => $value) {
$filetype = explode('.', $value);
$filetype_num = count($filetype) - 1;
$file_ext = $filetype[$filetype_num]; //得到图片格式
$file_dir = $dir . $piclist[$i] . '.' . $file_ext; //图片保存路径
self::ycimgd($value, $file_dir);
$i++;
}
return true;
}
//捉取图片函数
public static function ycimgd($file, $newfile)
{
$curl = curl_init(); // 初始化一个 cURL 对象
curl_setopt($curl, CURLOPT_URL, $file); // 设置你需要抓取的URL
curl_setopt($curl, CURLOPT_HEADER, 0); // 设置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
$data = curl_exec($curl); // 运行cURL,请求网页
curl_close($curl); // 关闭URL请求
$write = @fopen($newfile, "w"); //写入获得的数据
fwrite($write, $data);
fclose($write);
return TRUE;
}

 

给我留言

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

用户登录