<?php
namespace lib;
/*
 * 快捷登录接口
 */

class Oauth{
	private $apiurl;
	private $appid;
	private $appkey;
	private $callback;

	function __construct($apiurl, $appid, $appkey){
		global $siteurl;
		$base = rtrim((string)$apiurl, '/').'/';
		$this->apiurl = $base.'connect.php';
		$this->appid = $appid;
		$this->appkey = $appkey;
		$this->callback = $siteurl.'login.php';
	}

	//获取登录跳转url
	public function login($type, $callback = null, $state = null){

		//-------生成唯一随机串防CSRF攻击
		if($state === null){
			$state = md5(uniqid('', true) . time() . mt_rand());
		}
		$_SESSION['Oauth_state'] = $state;

		if($callback === null){
			$callback = $this->callback;
		}

		//-------构造请求参数列表
		$keysArr = array(
			"act" => "login",
			"appid" => $this->appid,
			"appkey" => $this->appkey,
			"type" => $type,
			"redirect_uri" => $callback,
			"state" => $state
		);
		$login_url = $this->apiurl.'?'.http_build_query($keysArr);
		
		// 使用增强的 curl 请求
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $login_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		// SSL证书验证 - 先尝试严格验证，如果失败则放宽验证
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			"Accept: application/json",
			"Accept-Language: zh-CN,zh;q=0.9"
		));
		
		$response = curl_exec($ch);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$curl_error = curl_error($ch);
		
		// 如果SSL验证失败，尝试放宽验证
		if($curl_error && strpos($curl_error, 'SSL') !== false) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			$response = curl_exec($ch);
			$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			$curl_error = curl_error($ch);
		}
		
		curl_close($ch);
		
		if($curl_error) {
			return ['code' => -1, 'msg' => 'cURL 错误: ' . $curl_error];
		}
		
		if($http_code !== 200) {
			return ['code' => -1, 'msg' => 'HTTP 错误: ' . $http_code];
		}
		
		if(empty($response)) {
			return ['code' => -1, 'msg' => '快捷登录接口无响应'];
		}
		
		$arr = json_decode($response, true);
		
		if(!is_array($arr)) {
			return ['code' => -1, 'msg' => '快捷登录接口返回数据格式错误'];
		}
		
		return $arr;
	}

	//登录成功返回网站
	public function callback(){
		//-------请求参数列表
		$keysArr = array(
			"act" => "callback",
			"appid" => $this->appid,
			"appkey" => $this->appkey,
			"code" => $_GET['code']
		);

		//------构造请求access_token的url
		$token_url = $this->apiurl.'?'.http_build_query($keysArr);
		
		// 使用增强的 curl 请求
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $token_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		// SSL证书验证 - 先尝试严格验证，如果失败则放宽验证
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			"Accept: application/json",
			"Accept-Language: zh-CN,zh;q=0.9"
		));
		
		$response = curl_exec($ch);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$curl_error = curl_error($ch);
		
		// 如果SSL验证失败，尝试放宽验证
		if($curl_error && strpos($curl_error, 'SSL') !== false) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			$response = curl_exec($ch);
			$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			$curl_error = curl_error($ch);
		}
		
		curl_close($ch);
		
		if($curl_error) {
			return ['code' => -1, 'msg' => 'cURL 错误: ' . $curl_error];
		}
		
		if($http_code !== 200) {
			return ['code' => -1, 'msg' => 'HTTP 错误: ' . $http_code];
		}

		if(empty($response)) {
			return ['code' => -1, 'msg' => '快捷登录回调接口无响应'];
		}

		$arr = json_decode($response, true);
		
		if(!is_array($arr)) {
			return ['code' => -1, 'msg' => '快捷登录回调返回数据格式错误'];
		}
		
		return $arr;
	}

	//查询用户信息
	public function query($type, $social_uid){
		//-------请求参数列表
		$keysArr = array(
			"act" => "query",
			"appid" => $this->appid,
			"appkey" => $this->appkey,
			"type" => $type,
			"social_uid" => $social_uid
		);

		//------构造请求access_token的url
		$token_url = $this->apiurl.'?'.http_build_query($keysArr);
		
		// 使用增强的 curl 请求
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $token_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		// SSL证书验证 - 先尝试严格验证，如果失败则放宽验证
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			"Accept: application/json",
			"Accept-Language: zh-CN,zh;q=0.9"
		));
		
		$response = curl_exec($ch);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$curl_error = curl_error($ch);
		
		// 如果SSL验证失败，尝试放宽验证
		if($curl_error && strpos($curl_error, 'SSL') !== false) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			$response = curl_exec($ch);
			$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			$curl_error = curl_error($ch);
		}
		
		curl_close($ch);
		
		if($curl_error) {
			return ['code' => -1, 'msg' => 'cURL 错误: ' . $curl_error];
		}
		
		if($http_code !== 200) {
			return ['code' => -1, 'msg' => 'HTTP 错误: ' . $http_code];
		}

		if(empty($response)) {
			return ['code' => -1, 'msg' => '快捷登录查询接口无响应'];
		}

		$arr = json_decode($response, true);
		
		if(!is_array($arr)) {
			return ['code' => -1, 'msg' => '快捷登录查询返回数据格式错误'];
		}
		
		return $arr;
	}
}
