<?php
session_start();
class sina 
{
	function login($appid, $callback) {
		$_SESSION['rurl'] = $_REQUEST ["rurl"];
		$_SESSION ['state'] = md5 ( uniqid ( rand (), true ) ); //CSRF protection
		$login_url = "https://api.weibo.com/oauth2/authorize?client_id=".$appid."&response_type=code&redirect_uri=".$callback."&state=".$_SESSION['state'];
		header ( "Location:$login_url" );
	}
	function callback($appid,$appkey,$path){
		if ($_REQUEST ['state'] == $_SESSION ['state']) {
			$url = "https://api.weibo.com/oauth2/access_token";
			$data = "client_id=".$appid."&client_secret=".$appkey."&grant_type=authorization_code&redirect_uri=".$path."&code=".$_REQUEST ["code"];
			$output = json_decode(do_post($url,$data));
			$_SESSION['access_token'] = $output->access_token;
			$_SESSION['uid'] = $output->uid;
		}else{
			echo ("The state does not match. You may be a victim of CSRF.");
			exit;
		}
	}
	function get_user_info() {
		$get_user_info = "https://api.weibo.com/2/users/show.json?uid=".$_SESSION['uid']."&access_token=".$_SESSION['access_token'];
		return get_url_contents ( $get_user_info );
	}
	function get_user_id() {
		$get_user_id = "https://api.weibo.com/2/account/get_uid.json?access_token=".$_SESSION['access_token'];
		return get_url_contents ( $get_user_id );
	}
	
	function sina_cb(){
		global $wpdb;
		$uidArray = json_decode($this->get_user_id());
		if( isset($_SESSION['uid']) && $uidArray->uid == $wpdb->escape($_SESSION['uid']) && isset($_SESSION['access_token']) ){
			$user_fb = get_users(array("meta_key "=>"sina_uid","meta_value"=>$_SESSION['uid']));
			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));
				$str = json_decode($this->get_user_info());
				$login_name = "u".$value;
				$username = $str->screen_name;
				$userimg = $str->avatar_large;
				$description = $str->description;
				//$userid = $str->id;
				$userdata=array(
				  'user_login' => $login_name,
				  'display_name' => $username,
				  'user_pass' => $pass,
				  'role' => get_option('default_role'),
				  'nickname' => $username,
				  'first_name' => $username,
				  'description'=> $description
				);
				$user_id = wp_insert_user( $userdata );
				if ( is_wp_error( $user_id ) ) {
					echo $user_id->get_error_message();
				}else{
				   
					update_user_meta($user_id ,"sina_uid",$_SESSION['uid']);
					update_user_meta($user_id, 'weibo', 'http://weibo.com/'.$_SESSION['uid']);
					update_user_meta($user_id, 'photo', $userimg);
					wp_set_auth_cookie($user_id,true,false);
					wp_redirect($_SESSION['rurl']);
							  
				}
				exit();
				
			}else{
				wp_set_auth_cookie($user_fb[0]->ID,true,false);
				wp_redirect($_SESSION['rurl']);
				exit();
			}
		}
		
		
	}
	
	function sina_bd(){
		global $wpdb;
		$uidArray = json_decode($this->get_user_id());
		if( isset($_SESSION['uid']) && $uidArray->uid == $wpdb->escape($_SESSION['uid']) && isset($_SESSION['access_token']) ){
		
			$user_fb = get_users(array("meta_key "=>"sina_uid","meta_value"=>$_SESSION['uid']));
			if(is_wp_error($user_fb) || !count($user_fb)){
			
				$userinfo=get_userdata(get_current_user_id());
				$userid = $userinfo->id;
				update_user_meta($userid ,"sina_uid",$_SESSION['uid']);
				$userdata = get_user_meta($userid,'userapi',true);
				$q_token = $userdata['q_token'];
				$sinaimg = $userdata['userimg'];
				$str = json_decode($this->get_user_info());
				if (empty($sinaimg)) {
					$sinaimg = $str->avatar_large;
				}
				
				
				update_user_meta($userid, 'photo', $sinaimg);
				wp_redirect($_SESSION['rurl']);
				exit();
				
			}else{
				echo '已有其他账号绑定了此微博，请先使用此微博登录后解绑！';
				exit();
			}
		}
		
	}
	
}

add_action('wp_ajax_nopriv_sina_remove_bind', 'sina_remove_bind');
add_action('wp_ajax_sina_remove_bind', 'sina_remove_bind');
function sina_remove_bind(){
	global $wpdb;
	if(is_user_logged_in()){
		delete_user_meta(get_current_user_id(),'sina_uid');
		return true;
	}
	
}


?>