<?php
/****************优客365网址导航系统 商业版********************/
/*                                                            */
/*  Youke365.com (C)2017 Youke365 Inc.                        */
/*  This is NOT a freeware, use is subject to license terms   */
/*  优客365网址导航系统是商业软件,使用于商业用途请购买授权    */
/*  V1.0 2017                                                 */
/*  技术支持：1513773524@qq.com                               */
/*  官方网址：http://www.youke365.com                         */
/*  官方论坛：http://bbs.youke365.com                         */                           
/**************************************************************/

if (!defined('YOUKE365_MEMBER')) exit('Access Denied');

if ($options['is_enabled_connect'] == 'yes') {
	if (empty($options['qq_appid']) || empty($options['qq_appkey'])) {
		msgbox('QQ一键登录未正确设置，请设置后再使用！');	
	}
} else {
	msgbox('未开启QQ一键登录或注册功能！');
}

$table = $DB->table('users');

$oper = I($_GET['oper']);
if (empty($oper)) $oper = 'init';



$config = array('appid' => $options['qq_appid'], 'appkey' => $options['qq_appkey'], 'callback' => $site_url.'connect/callback');



$oauth = oauth_qq::get_instance($config);
//login
if ($oper == 'init') 

	$oauth->login();

if ($oper == 'callback') {
	$query = parse_url($_SERVER['REQUEST_URI']);
	parse_str($query['query'],$_REQUEST);


	$response = $oauth->callback();
	$msg = json_decode($response);
		if (isset($msg->error)) {
					// echo "<h3>error:</h3>" . $msg->error;
					// echo "<h3>message:</h3>" . $msg->error_description;
			  msgbox('token失效，请重新登陆！');
	          exit;
		}
				
    $oauth->get_openid();
	$user = $oauth->get_user_info();

     $open_id = $_SESSION['openid'];
	if (empty($open_id)) {
		msgbox('QQ一键登录授权失败，请采用普通方式注册和登录！', '/member/?mod=login');
	} else {
		$row = $DB->fetch_one("SELECT user_id, user_pass, login_time FROM $table WHERE open_id='$open_id'");
		if ($row) {
			$ip_address = sprintf("%u", ip2long(get_client_ip()));
        	$login_count = $row['login_count'] + 1;
					
			$data = array(
				'login_time' => time(),
				'login_ip' => $ip_address,
				'login_count' => $login_count,
			);
			$where = array('user_id' => $row['user_id']);
			$DB->update($table, $data, $where);
			
			$auth_cookie = authcode("$row[user_id]|$row[user_pass]|$login_count");
			$expire = time() + 3600 * 24;
			setcookie('auth_cookie', $auth_cookie, $expire, $options['site_root']);
				
			redirect('/member/?mod=home');
		} else {

			/** check login  */
			$auth_cookie = $_COOKIE['auth_cookie'];
			$myself = check_user_login($auth_cookie);
			if (!empty($myself)) {
				$DB->update($table, array('open_id' => $open_id), array('user_id' => $myself['user_id']));
			} else {
				$tplfile = 'connect.html';
				
				$smarty->assign('nick_name', $user['nickname']);
				$smarty->assign('open_id', $open_id);
				smarty_output($tplfile);
			}
		}
	}
}
?>