<?
session_start();
header('Content-Type: text/html; charset=utf-8');

if($_GET['id']=='logout')
{
	setcookie('v_o_t_',NULL);
	setcookie('v_o_t_s_',NULL);
	header('Location:'.$_SERVER['HTTP_REFERER']);
	exit;
}
//腾讯登陆
if($_GET['id']=='t'&&$_GET['app']!='')
{
	$app_name=$_GET['app'];
	require_once './../a_include/t_appkey.php';
	set_include_path(dirname(dirname(__FILE__)) . '/a_SDK/');
	require_once 'OpenSDK/Tencent/Weibo.php';
	OpenSDK_Tencent_Weibo::init($appkey, $appsecret);
	
	if( isset($_GET['oauth_token']) && isset($_GET['oauth_verifier']))
	{
		//从Callback返回时
		if($rt=OpenSDK_Tencent_Weibo::getAccessToken($_GET['oauth_verifier']))
		{
			$infos=OpenSDK_Tencent_Weibo::call('user/info', array('names'=>'daili99','flag'=>'1'));
			$infos=$infos['data'];
			
			$name=$infos['name'];
			$nick=$infos['nick'];
			$isvip=$infos['isvip'];
			$avatar=$infos['head']!=''?$infos['head'].'/100':'../file/imgs/no_avatar.jpg';
			$avatar2=$infos['head'];
			$sex=$infos['sex'];
			$idolnum=$infos['idolnum'];
			$fansnum=$infos['fansnum'];
			$email=$infos['email'];
			$regdate=time();
			$v_oauth_token=$rt['oauth_token'];
			$v_oauth_token_secret=$rt['oauth_token_secret'];
			
			setcookie('v_type','t', time()+3000);
			setcookie('v_name',$name, time()+3000);
			setcookie('v_nick',$nick, time()+3000);
			setcookie('v_avatar',$avatar, time()+3000);
			setcookie('v_sex',$sex, time()+3000);
			setcookie('v_fnum',$fansnum,time()+700000);
			setcookie('v_appname',$app_name, time()+3000);
			setcookie('v_o_t_',$v_oauth_token, time()+3000);
			setcookie('v_o_t_s_',$v_oauth_token_secret, time()+3000);
			
			header('Location: ./');
		}
		else
		{
			echo '授权失败！请返回重试。 <a href="./">点此返回</a>';
		}
	}
	else
	{
		$callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?id=t&app='.$_GET['app'];
		$request_token = OpenSDK_Tencent_Weibo::getRequestToken($callback);
		$r_url = OpenSDK_Tencent_Weibo::getAuthorizeURL($request_token);
		if(strlen($r_url)<70)
		{
			echo '服务器正忙，请稍后返回重试。<a href="./">>>点此返回</a>';
		}else
		{
			header('Location: ' . $r_url);
		}
	}
}
//新浪登陆
if($_GET['id']=='s')
{
	require_once '../a_include/s_appkey.php';
	set_include_path(dirname(dirname(__FILE__)) . '/a_SDK/');
	require_once 'OpenSDK/Sina/Weibo2.php';
	OpenSDK_Sina_Weibo2::init($appkey, $appsecret);
	
	if(isset($_GET['code']))
	{
		//从Callback返回时
		$redirect_uri='http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
		if(OpenSDK_Sina_Weibo2::getAccessToken('code',array('code'=>$_GET['code'],'redirect_uri'=>$redirect_uri)))
		{
				
			$user_id=OpenSDK_Sina_Weibo2::getParam (OpenSDK_Sina_Weibo2::OAUTH_USER_ID);
			$infos = OpenSDK_Sina_Weibo2::call('users/show',array('uid'=>$user_id));
			//$id=$infos['id'];
			$nick=$infos['screen_name'];
			$avatar=$infos['avatar_large'];
			$avatar=$avatar!=''?$avatar:'../file/imgs/no_avatar.jpg';
			$sex=$infos['gender'];
			$sex=$sex=='f'?'2':'1';
			//$idolnum=$infos['friends_count'];
			$fansnum=$infos['followers_count'];
			$v_oauth_token= OpenSDK_Sina_Weibo2::getParam (OpenSDK_Sina_Weibo2::ACCESS_TOKEN);
			$v_oauth_token_secret=OpenSDK_Sina_Weibo2::getParam (OpenSDK_Sina_Weibo2::REFRESH_TOKEN);
			if($v_oauth_token_secret==''){$v_oauth_token_secret='1';}
			
			setcookie('v_type','s', time()+3000);
			setcookie('v_name',$nick, time()+3000);
			setcookie('v_nick',$nick, time()+3000);
			setcookie('v_avatar',$avatar, time()+3000);
			setcookie('v_sex',$sex, time()+3000);
			setcookie('v_fnum',$fansnum,time()+700000);
			setcookie('v_o_t_',$v_oauth_token, time()+3000);
			setcookie('v_o_t_s_',$v_oauth_token_secret, time()+3000);
			
			header('Location: ./');
		}
		else
		{
			echo '授权失败！请返回重试。 <a href="./">点此返回</a>';
		}
	}
	else
	{
		$callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?id=s';
		$url = OpenSDK_Sina_Weibo2::getAuthorizeURL($callback, 'code', 'state');
		header('Location:' . $url);
	}
}
?>