<?php
/**************************************************
*  Created:  2010-06-08
*
*  帐号相关操作
*
*  @Xweibo (C)1996-2099 SINA Inc.
*  @Author xionghui <xionghui1@staff.sina.com.cn>
*
***************************************************/

class account 
{
	function account(){}
	
	/// 退出登录
	function logout(){
		//统计上报
		$storage = APP::N('clientUser');
		$http = APP::ADP('http');
		$url_report = 'http://beacon.x.weibo.com/a.gif?xt=logout&akey='.WB_AKEY.'&uid='.$storage->getInfo('sina_uid').'&ip='.APP::F('get_client_ip').'&random='.mt_rand();
		$http->setUrl($url_report);
		$http->request();

		foreach ($_SESSION as $k=>$v ){
			unset($_SESSION[$k]);
		}
		setcookie(XWB_AUTH_CK_NAME, '',0, XWB_AUTH_CK_PATH, XWB_AUTH_CK_DOMAIN);
		$bind_enable = V('-:sysConfig/bind_enable');
		$url = empty($bind_enable) ? URL('index') : XWB_OWNER_SITE_X_URL.'logout.php';
		$url = URL('index');
		APP::redirect($url,3);
	}	
	/// 从微博API认证返回后执行的操作
	function oauthBind(){ 
		$wb	= APP::N('weibo');
		$last_key = $wb->getAccessToken(V('r:oauth_verifier')) ;
		$sess = APP::N('clientUser');
		$sess->setOAuthKey($last_key, true);

		//重新设置token
		$wb->setConfig();
		$uInfo = $wb->verifyCredentials();
		$sess->setInfo('sina_uid',		$uInfo['id']);
		$sess->setInfo('screen_name',	$uInfo['screen_name']);
		$sess->setInfo('description',	$uInfo['description']);
		//$sess->setInfo('sina_name',		$uInfo['xxxx']);
		$sess->setInfo('uid',			$uInfo['id']);
		
		$site_uid = $sess->getInfo('site_uid');
		//print_r($last_key);print_r($uInfo);var_dump($site_uid);exit;
		
		//统计上报
		$http = APP::ADP('http');
		$url_report = 'http://beacon.x.weibo.com/a.gif?xt=login&akey='.WB_AKEY.'&uid='.$sess->getInfo('sina_uid').'&ip='.APP::F('get_client_ip').'&random='.mt_rand();
		$http->setUrl($url_report);
		$http->request();
		
		if ($sess->_getNeedToBind() && !empty($site_uid)){
			// 将绑定的信息入库
			$inData = array();
			$inData['add_time'] 	= time();
			$inData['screen_name']	= $uInfo['screen_name'];
			$inData['sina_uid'] 	= $uInfo['id'];
			$inData['uid']			= $site_uid;
			$inData['oauth_token']	= $last_key['oauth_token'];
			$inData['oauth_token_secret']	= $last_key['oauth_token_secret'];
			
			$db = APP::ADP('db'); 
			$r	= $db->save($inData, '', T_USER_TOKEN);
			if ( $r===false ) {
				trigger_error("DB error :  ".$db->getError(), E_USER_ERROR);
				return false;
			}
		}/**/
		APP::redirect('index',2);
	}
}
?>
