<?
require_once('../config.php');
require_once('lib/db.php');
require_once('lib/socnetauth.php');

$SocAuth = new SocAuth();

if( !$SocAuth->get_config_param('socnetauth_status') )
{
	header("Location: ".$_SERVER['HTTP_REFERER']);
	exit();
}


$IS_DEBUG = 0;

if( !empty($_GET['first']) )
{
	$STATE = 'facebook_socnetauth_'.rand();
	
	$CURRENT_URI = $_SERVER['HTTP_REFERER'];
	$REDIRECT_URI = 'http://'.$_SERVER['HTTP_HOST'].'/socnetauth/facebook.php';
		
	$CLIENT_ID = $SocAuth->get_config_param('socnetauth_facebook_appid');
		
	$url = 'https://www.facebook.com/dialog/oauth?'.
		'client_id='.$CLIENT_ID.
		'&redirect_uri='.$REDIRECT_URI.
		'&scope=email,user_hometown,user_location&state='.$STATE;
			
	$SocAuth->setRecord($STATE, $CURRENT_URI);
	header("Location: ".$url);
	exit();
}

if( !empty( $_GET['state'] ) && !empty( $_GET['code'] ) &&
		$recordData = $SocAuth->getRecord( $_GET['state'] ) )
{
	$CODE = $_GET['code'];
	$CURRENT_URI = $recordData['redirect'];
	$REDIRECT_URI = 'http://'.$_SERVER['HTTP_HOST'].'/socnetauth/facebook.php';
	
	
	$CLIENT_ID = $SocAuth->get_config_param('socnetauth_facebook_appid');
	$CLIENT_SECRET = $SocAuth->get_config_param('socnetauth_facebook_appsecret');
	
	
	$url = "https://graph.facebook.com/oauth/access_token?".
				   "client_id=".$CLIENT_ID.
				   "&redirect_uri=".$REDIRECT_URI.
				   "&client_secret=".$CLIENT_SECRET.
				   "&code=".$CODE;
	if( $IS_DEBUG ) echo $url."<hr>";
			
	
	if( extension_loaded('curl') )
	{
		$c = curl_init($url);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($c);
		curl_close($c);
	}
	else
	{
		$response = file_get_contents($url);
	}
			
			
	if( $IS_DEBUG ) echo $response."<hr>";
	$data = null;
	parse_str($response, $data);	
			
	if( !empty($data['access_token']) )
	{
		$graph_url = "https://graph.facebook.com/me?access_token=".$data['access_token'];
		if( $IS_DEBUG ) echo $graph_url."<hr>";
		
		if( extension_loaded('curl') )
		{
			$c = curl_init($graph_url);
			curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
			$json = curl_exec($c);
			curl_close($c);
		}
		else
		{
			$json = file_get_contents($graph_url);
		}
		
			/*				
				id=>100000402380563
				name=>Petrov Konstantin
				first_name=>Petrov
				last_name=>Konstantin
				link=>http://www.facebook.com/petrov.konstantin
				username=>petrov.konstantin
				email=>kin208@gmail.com
				timezone=>6
				locale=>en_US
				verified=>1
				updated_time=>2012-02-11T12:39:00+0000
			*/
		
		if( $IS_DEBUG ) echo $json;
		$userdata = json_decode($json, TRUE);
				
		if( $customer = $SocAuth->facebook_check($userdata['id']) )
		{
			session_start();
			$_SESSION['customer_id'] = $customer['customer_id'];
					
			if( $IS_DEBUG ) exit('RES1|'.$REDIRECT_URI);
					
			header("Location: ".$CURRENT_URI ); 
		}
		else
		{
			$precode = $SocAuth->facebook_preregistration($userdata);
				
			if( $IS_DEBUG ) exit('RES2');
			
			header('Location: /index.php?route=socnetauth/registration&precode='.$precode.'&redirect_uri='.urlencode($CURRENT_URI) ); 
		}
	}
}


?>