<?
session_start();
header('Content-Type: text/html; charset=utf-8');
require_once './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_o_t_',$v_oauth_token, time()+3000);
			setcookie('v_o_t_s_',$v_oauth_token_secret, time()+3000);
			
			$isoido=OpenSDK_Tencent_Weibo::call('friends/check', array('names'=>'vqu_cc','flag'=>'1'));
			$isoido=$isoido['data'];
			if($isoido['vqu_cc']==true)
			{
				setcookie('follow','1', time()+3000);
			}else
			{
				setcookie('follow','0', time()+3000);
			}
			/*
			$url=$_SERVER['PHP_SELF'];
			$app_names=explode('/',$url);
			$app_name=$app_names[1];
			switch($app_name)
			{
				case 'ajg':$insert_app=1;break;
				case 'aln':$insert_app=1;break;
				case 'gdmz':$insert_app=1;break;
				case 'hyyz':$insert_app=1;break;
				case 'lyb':$insert_app=1;break;
				case 'ngcs':$insert_app=1;break;
				case 'qssf':$insert_app=1;break;
				case 'sjz':$insert_app=1;break;
				case 'wlqt':$insert_app=1;break;
				case 'thzs':$insert_app=1;break;
				case 'wlzy':$insert_app=1;break;
				case 'zhh':$insert_app=1;break;
			}
			if($insert_app==1)
			{
				require_once '../sql.inc.php';
				$table='vqu_'.$app_name.'_users';
				$sql="insert into $table values(
				'$name','$fansnum','$v_oauth_token','$v_oauth_token_secret'
				) ON DUPLICATE KEY UPDATE fansnum='$fansnum',v_oauth_token='$v_oauth_token',v_oauth_token_secret='$v_oauth_token_secret'";
				
				@mysql_query($sql);
			}*/
			header('Location: ./');
		}
		else
		{
			echo '授权失败！请返回重试。 <a href="./">点此返回</a>';
		}
	}
	else
	{
		$callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?id=t';
		$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);
		}*/
		echo $r_url.'sss';
		//header('Location: ' . $r_url);
	}
?>