<?php 
	if (isset($_GET['id'])) { $user = intval($_GET['id']); } else { $user = 1;}
	$login=false;
	$admin=false;
	$banned=false;
	session_start();
	include 'config.php';
	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();
	}

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

	$dest_source = file_get_contents('u/'.$myID.'/user.json');
	$dest_data = json_decode($dest_source, true);
	sanitizeUserData($myID,$dest_data);
	if ($dest_data["ban"]) {
		echo 'banned';
		die();
	}
	
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>MYSPACE</title>
	<meta name="description" content="A PLACE FOR FWIENDS" />
	<link rel="icon" type="image/png" href="favicon.png" />
	<meta property="og:og:title" content="MYSPACE"/>
	<meta property="og:determiner" content="MY"/>
	<meta property="og:type" content="website"/>
	<meta property="og:description" content="A PLACE FOR FWIENDS"/>
	<meta property="og:image" content="https://myspace.windows93.net/u/1/avatar.jpg">
	<meta property="og:url" content="https://myspace.windows93.net/?id=<?php echo $user; ?>">
	<meta property="og:site_name" content="MYSPACE.WINDOWS93.NET"/>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">	
	<link rel="stylesheet" href="style.css">	
</head>
<body style="background-color: aliceblue;"><div id="container">

	<?php include 'header.php'; ?>

	<?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();
		}

		$dest_source = file_get_contents('u/'.$user.'/user.json');
		$dest_data = json_decode($dest_source, true);
		$dest_data = sanitizeUserData($user,$dest_data);

		$blocked = true;
		// filtre non-ami
		for ($i = 0; $i < count($dest_data["fwiends"]); $i++) {
			if($dest_data["fwiends"][$i]==$myID){
				$blocked=false;
			}
		}
		// filtre bloqué
		for ($i = 0; $i < count($dest_data["fwiends"]); $i++) {
			if($dest_data["block"][$i]==$myID){
				$blocked=true;
				echo '<div style="padding:10px;"><h4>'.$dest_data["name"].' blocked you.</h4><br><a href="javascript:history.back()">< Go Back</a></div>';
				die();
			}
		}		
		if ($blocked==true) {
			echo '<div style="padding:10px;"><h4>You need to be fwiend with '.$dest_data["name"].' to send messages.</h4><br><a href="javascript:history.back()">< Go Back</a></div>';
			die();
		}

		$sub = "";
		if (isset($_GET['msg'])) { 
			$msg = intval($_GET['msg']); 
			$inbox_source = file_get_contents('u/'.$myID.'/box.json');
			$inbox_data = json_decode($inbox_source, true);

			$sub = $inbox_data['inbox'][$msg]["sub"];
			if($sub==''){$sub='No Subject';}
		}


	?>

		<div id="mail">
		<?php echo '<a href="javascript:history.back()">< Go Back</a>'; ?>
			<div style="padding-top: 10px;font-weight: bold;">⚠️ <i>There's no bots here, just real people.</i></div>
			<div class="boxHead" style="margin-top: 20px;margin-bottom: 10px;"><b>Send a message to <?php echo $dest_data["name"];?></b></div>
			<form action="mail.php" method="post">
				<div><b>Subject</b><br><input type="text" name="sub" value="<?php echo $sub; ?>" style="width: calc( 100% - 5px);border: 1px solid #000;" autocomplete="off"></div>
		 		<div><b>Message</b><br><textarea name="message" id="commentInput" autocomplete="off"></textarea></div>
		 		<input type="hidden" name="user" value="<?php echo $user; ?>">
		 		<input type="submit" value="Send">
			</form>
		</div>	
</div></body>
</html>


