<commit_before><?php
	session_start();
	include_once("config.php");
	include_once("../php/connect.php");
	include_once("facebook_class.php");

	$gpid = isset($_COOKIE['pid']) ? $_COOKIE['pid'] : null;

	if(isset($_SESSION['access_token']))
	{

		$access_token = $_SESSION['access_token'];
		$fbid = GetFacebookUserProfile($access_token);
		echo "FBID:".$fbid."<br/>";
		if($fbid != false)
		{
			GetFacebookActivities($access_token,$fbid);
		}
		

	}
	else
	{
		header('Location: ../facebook/fbcallback.php');
	}

	function GetFacebookUserProfile($access_token)
	{
		global $fb, $conn, $gpid;
		$IsSuccess = true;

		$response = $fb->get('/me?fields=id,name,gender,birthday,location,friends.summary(true)', $access_token);
		$user = $response->getGraphNode();
		//print_r($user);
		$fuser = new Profile();
		$fuser->id 			= $user->getField('id');
		$fuser->name 		= $user->getField('name');
		$fuser->gender  	= $user->getField('gender');
		$fuser->location 	= $user->getField('location')->getField('name');
		$fuser->birthyear	= $user->getField('birthday')->format('Y');
		$fuser->total_friend= $user->getField('friends')->getTotalCount();

		//SaveFacebookUserProfile
		/*mysqli_query($conn,"START TRANSACTION");
		try{
			//$gpid = 93;
			$result = IsCurrentUser('tb_user','fb_id',$fuser->id);
			$hasFacebookId = ($result->num_rows > 0) ? true : false;
			if(!$hasFacebookId)
			{
				if($gpid!=null)//Current user
				{
					//Update tb_user
					$pid =$gpid;
					$row = null;
					$result = IsCurrentUser('tb_user', 'pid', $pid);
					if ($result->num_rows > 0) {
					    // output data of each row
					    $row = $result->fetch_assoc();
					}
					$gender = $row['gender']== null ? $fuser->gender : $row['gender'];
					$birthday = $row['birthday']== null ? $fuser->birthyear : $row['birthday'];
					$country = $row['country']== null ? $fuser->location : $row['country'];
					$fb_id = $fuser->id;
					
					//Update tb_user and Create tb_facebook_profile
					if(UpdateUserProfile($pid, $fb_id, $gender, $birthday, $country))
					{
						echo "Update User Profile";
						if(!CreateFacebookUserProfile($fuser,$pid))
							$IsSuccess = false;
						echo $IsSuccess;
					}
				}
				else // new user
				{
					//Create new user in tb_user
					$pid = CreateUserProfile($fuser);
					echo "PID:".$pid;
					if($pid!=false)
					{
						$gpid=$pid; 
						if(!CreateFacebookUserProfile($fuser,$pid))
							$IsSuccess = false;
					} 
				}
			}
			else // Update tb_facebook_profile
			{
				if(!UpdateFacebookUserProfile($fuser))
					$IsSuccess = false;
			}
			
			mysqli_query($conn,"COMMIT");
		}catch(Exception $e){
			mysqli_query($conn,"ROLLBACK");
		}
		$conn->close();*/
		return $IsSuccess==true ? $fuser->id : $IsSuccess;
	}
	
	function GetFacebookActivities($access_token, $fbid)
	{
		global $fb, $conn, $gpid, $since_date;
		$IsSuccess = true;
		$fact = array();

		$response = $fb->get('me/posts?fields=id,created_time,type,status_type&limit=200', $access_token);
		$post = $response->getGraphEdge();
		//$nextPageOfPost = $fb->next($post);
		//$a = $fb->next($nextPageOfPost);
		//print_r($post);
		//print_r($a);
		//echo $post;
		$i=1;<commit_msg>edit facebook code<commit_after><?php
	session_start();
	include_once("config.php");
	include_once("../php/connect.php");
	include_once("facebook_class.php");

	$gpid = isset($_COOKIE['pid']) ? $_COOKIE['pid'] : null;

	if(isset($_SESSION['access_token']))
	{

		$access_token = $_SESSION['access_token'];
		$fbid = GetFacebookUserProfile($access_token);
		echo "FBID:".$fbid."<br/>";
		if($fbid != false)
		{
			GetFacebookActivities($access_token,$fbid);
		}
		

	}
	else
	{
		header('Location: ../facebook/fbcallback.php');
	}

	function GetFacebookUserProfile($access_token)
	{
		global $fb, $conn, $gpid;
		$IsSuccess = true;

		$response = $fb->get('/me?fields=id,name,gender,birthday,location,friends.summary(true)', $access_token);
		$user = $response->getGraphNode();
		//print_r($user);
		$fuser = new Profile();
		$fuser->id 			= $user->getField('id');
		$fuser->name 		= $user->getField('name');
		$fuser->gender  	= $user->getField('gender');
		$fuser->location 	= $user->getField('location')->getField('name');
		$fuser->birthyear	= $user->getField('birthday')->format('Y');
		$fuser->total_friend= $user->getField('friends')->getTotalCount();

		//SaveFacebookUserProfile
		/*mysqli_query($conn,"START TRANSACTION");
		try{
			//$gpid = 93;
			$result = IsCurrentUser('tb_user','fb_id',$fuser->id);
			$hasFacebookId = ($result->num_rows > 0) ? true : false;
			if(!$hasFacebookId)
			{
				if($gpid!=null)//Current user
				{
					//Update tb_user
					$pid =$gpid;
					$row = null;
					$result = IsCurrentUser('tb_user', 'pid', $pid);
					if ($result->num_rows > 0) {
					    // output data of each row
					    $row = $result->fetch_assoc();
					}
					$gender = $row['gender']== null ? $fuser->gender : $row['gender'];
					$birthday = $row['birthday']== null ? $fuser->birthyear : $row['birthday'];
					$country = $row['country']== null ? $fuser->location : $row['country'];
					$fb_id = $fuser->id;
					
					//Update tb_user and Create tb_facebook_profile
					if(UpdateUserProfile($pid, $fb_id, $gender, $birthday, $country))
					{
						echo "Update User Profile";
						if(!CreateFacebookUserProfile($fuser,$pid))
							$IsSuccess = false;
						echo $IsSuccess;
					}
				}
				else // new user
				{
					//Create new user in tb_user
					$pid = CreateUserProfile($fuser);
					echo "PID:".$pid;
					if($pid!=false)
					{
						$gpid=$pid; 
						if(!CreateFacebookUserProfile($fuser,$pid))
							$IsSuccess = false;
					} 
				}
			}
			else // Update tb_facebook_profile
			{
				if(!UpdateFacebookUserProfile($fuser))
					$IsSuccess = false;
			}
			
			mysqli_query($conn,"COMMIT");
		}catch(Exception $e){
			mysqli_query($conn,"ROLLBACK");
		}
		$conn->close();*/
		return $IsSuccess==true ? $fuser->id : $IsSuccess;
	}
	
	function GetFacebookActivities($access_token, $fbid)
	{
		global $fb, $conn, $gpid, $since_date;
		$IsSuccess = true;
		$fact = array();

		$response = $fb->get('me/posts?fields=id,created_time,type,status_type&limit=200', $access_token);
		$post = $response->getGraphEdge();

		$i=1;
{'fraction_non_alphanumeric': 0.19627966645285438, 'fraction_numerical': 0.0025657472738935213, 'mean_word_length': 3.6131656804733727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3677085', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2230, 'n_words': 355}