<?php 
	if (isset($_GET['id'])) { 
		$user = intval($_GET['id']); 
	} else { 
		die();
	}
	$login=false;
	$admin=false;
	$banned=false;
	session_start();
	if ($_SESSION['valid']) {
		$login=true;
		$myID = intval($_SESSION['id']);
	}else{
				echo '<link rel="stylesheet" href="style.css">';
		include 'header.php';
		echo('You are not logged.');
		die();
	}
	$my_source = file_get_contents('u/'.$myID.'/user.json');
	$my_data = json_decode($my_source, true);
	if ($my_data["ban"]) {
		echo 'you are banned';
		die();
	}

	if($_SERVER['HTTP_REFERER']!="https://myspace.windows93.net/index.php?id=".$user&&$_SERVER['HTTP_REFERER']!="https://myspace.windows93.net/?id=".$user){die();}


?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>MYSPACE</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<link rel="icon" type="image/png" href="favicon.png" />
</head>
<body><div id="container">


	
	<?php


		function folder_exist($folder){
		    $path = realpath($folder);
		    return ($path !== false AND is_dir($path)) ? $path : false;
		}

		if(!folder_exist('u/'.$user)){
			echo '<script>window.location="index.php?id='.$myID.'"</script>';
			die();
		}

		if ($myID==$user) {
			echo '<script>window.location="index.php?id='.$user.'"</script>';
			die();
		}

		$json_source = file_get_contents('u/'.$user.'/user.json');
		$json_data = json_decode($json_source, true);	

		// already fwiends
		if (in_array($myID, $json_data["fwiends"])) { 
			echo '<script>window.location="remove.php?id='.$user.'"</script>';
		  	die();
		}		

		// is blocked
		$blocked=false;
		for ($i = 0; $i < count($json_data["block"]); $i++) {
			if($json_data["block"][$i]==$myID){
				$blocked=true;
			}
		}
		if ($blocked==true) {
			echo '<script>window.location="index.php?id='.$user.'"</script>';
		  	die();
		}

		if (in_array($myID, $json_data["pending"])) { 
			echo '<script>window.location="index.php?id='.$user.'"</script>';
		  	die();
		} else { 
		  	array_push($json_data["pending"], $myID);

		  	$json_data["pending"]=array_unique($json_data["pending"]);
		  	$json_dest = json_encode($json_data, JSON_PRETTY_PRINT);
			file_put_contents('u/'.$user.'/user.json', $json_dest);
			echo '<script>window.location="index.php?id='.$user.'"</script>';
			die();
		} 



	?>



</div></body>
</html>