<?php 
session_start();
class qq
{
	function login($appid, $scope, $callback) {
		$_SESSION['rurl'] = $_REQUEST ["rurl"];
		$_SESSION ['state'] = md5 ( uniqid ( rand (), true ) ); //CSRF protection
		$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" . $appid . "&redirect_uri=" . urlencode ( $callback ) . "&state=" . $_SESSION ['state'] . "&scope=" . $scope;
		header ( "Location:$login_url" );
	}
	function callback($appid,$appkey,$path) {
		if ($_REQUEST ['state'] == $_SESSION ['state']) {
			$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&" . "client_id=" . $appid . "&redirect_uri=" . urlencode ( $path ) . "&client_secret=" . $appkey . "&code=" . $_REQUEST ["code"];
			
			$response = get_url_contents ( $token_url );
			if (strpos ( $response, "callback" ) !== false) {
				$lpos = strpos ( $response, "(" );
				$rpos = strrpos ( $response, ")" );
				$response = substr ( $response, $lpos + 1, $rpos - $lpos - 1 );
				$msg = json_decode ( $response );
				if (isset ( $msg->error )) {
					echo "<h3>error:</h3>" . $msg->error;
					echo "<h3>msg  :</h3>" . $msg->error_description;
					exit ();
				}
			}
			
			$params = array ();
			parse_str ( $response, $params );
			$_SESSION ['access_token'] = $params ["access_token"];
		} else {
			echo ("The state does not match. You may be a victim of CSRF.");
			exit;
		}
	}
	function get_openid() {
		$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $_SESSION ['access_token'];
		
		$str = get_url_contents ( $graph_url );
		if (strpos ( $str, "callback" ) !== false) {
			$lpos = strpos ( $str, "(" );
			$rpos = strrpos ( $str, ")" );
			$str = substr ( $str, $lpos + 1, $rpos - $lpos - 1 );
		}
		//echo $_SESSION ['access_token'].'ddd';
		$user = json_decode ( $str );
		if (isset ( $user->error )) {
			echo "<h3>error:</h3>" . $user->error;
			echo "<h3>msg2  :</h3>" . $user->error_description;
			exit ();
		}
		$_SESSION ['openid'] = $user->openid;
	}
	function get_user_info() {
		$options = get_option('monkey-options');
		$appid = $options['qqid'];
		$get_user_info = "https://graph.qq.com/user/get_user_info?" . "access_token=" . $_SESSION ['access_token'] . "&oauth_consumer_key=".$appid."&openid=" . $_SESSION ['openid']."&format=json" ;		
		return get_url_contents ( $get_user_info );
		//return $get_user_info;
	}

	function qq_cb(){
		global $wpdb;
		if(isset($_SESSION['openid'])){
			$user_fb = get_users(array("meta_key "=>"qq_openid","meta_value"=>$_SESSION['openid']));
			if(is_wp_error($user_fb) || !count($user_fb)){
				
				$arr=range(0,9);
				shuffle($arr);
				$value = '';
				foreach($arr as $values)
				{
				   $value .= $values;
				}
			
				$pass = wp_create_nonce(rand(10,1000));
				$uinfo = json_decode($this->get_user_info());
				$login_name = "u".$value;
				$username = $uinfo->nickname;
				$userdata=array(
				  'user_login' => $login_name,
				  'display_name' => $username,
				  'nickname' => $username,
				  'user_pass' => $pass,
				  'role' => get_option('default_role'),
				  'first_name' => $username
				);
				$user_id = wp_insert_user( $userdata );
				if ( is_wp_error( $user_id ) ) {
					echo $user_id->get_error_message();
				}else{
	
					update_user_meta($user_id ,"qq_openid",$_SESSION[openid]);
					$userapi = array(
						'userimg' => $uinfo->figureurl_qq_2,
						'q_token' => $_SESSION['access_token'],
						's_token' => ''
					);
					$data = add_user_meta($user_id,'userapi',$userapi);
					update_user_meta($user_id, 'photo', $uinfo->figureurl_qq_2);
					wp_set_auth_cookie($user_id,true,false);
					wp_redirect($_SESSION['rurl']);
		   
				}
				exit();
			
			}else{
				$user_ID = $user_fb[0]->ID;
				wp_set_auth_cookie($user_ID,true,false);
				wp_redirect($_SESSION['rurl']);
				exit();
			}
		}
	    
	}
	
	function qq_bd(){
		global $wpdb;
		if(isset($_SESSION['openid'])){
		
			$user_fb = get_users(array("meta_key "=>"qq_openid","meta_value"=>$_SESSION['openid']));
			if(is_wp_error($user_fb) || !count($user_fb)){
			
				$userinfo=get_userdata(get_current_user_id());
				$userid = $userinfo->id;
				update_user_meta($userid ,"qq_openid",$_SESSION['openid']);
				$userdata = get_user_meta($userid,'userapi',true);
				$s_token = $userdata['s_token'];
				$qqimg = $userdata['userimg'];
				$uinfo = json_decode($this->get_user_info());
				if (empty($qqimg)) {
					$qqimg = $uinfo->figureurl_qq_2;
				}
				$userapi = array(
					'userimg' => $qqimg,
					'q_token' => $_SESSION['access_token'],
					's_token' => $s_token
				);
				$data = update_user_meta($userid,'userapi',$userapi);
				if ($data) {
					wp_redirect($_SESSION['rurl']);
					exit();
				}else{
					wp_redirect($_SESSION['rurl']);
					exit();
				}
			}else{
				echo '已有其他用户绑定了此QQ，请先使用此QQ登录后解绑！';
				exit();
			}
		}
	}
	
}

add_action('wp_ajax_nopriv_qq_remove_bind', 'qq_remove_bind');
add_action('wp_ajax_qq_remove_bind', 'qq_remove_bind');
function qq_remove_bind(){
	global $wpdb;
	if(is_user_logged_in()){
		delete_user_meta(get_current_user_id(),'qq_openid');

		return true;
	}
}
?>