<?php 
	$myID=-1;
	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']);
		if ($myID==$user) {
			$admin=true;
		}
		/*
		if ($myID==1) {
			$admin=true;
		}
		*/

	}
	$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();
	}
	$bans = getBans();
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>MYSPACE</title>
	<link rel="icon" type="image/png" href="favicon.png" />
	<link rel="stylesheet" href="style.css">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body style="padding: 20px;padding-top: 70px;"><div id="container">
<?php 
	if($admin){
		if ($my_data['newF']) {
			$my_data['newF']=false;
			$me_dest = json_encode($my_data, JSON_PRETTY_PRINT);
			file_put_contents('u/'.$myID.'/user.json', $me_dest);
		}
	}
	include 'header.php'; ?>
<?php 
	
		if ($user==1) {

			$json_source = file_get_contents(ADMINFOLDER.'/users.json');
			$json_data = json_decode($json_source, true);



			echo '<div class="allfwieds">';
			echo '<a href="/?id='.$user.'"">< Go Back</a>';
			echo '<h2>Tom\'s Last 50 Fwiends</h2>';

				//$json_data = array_reverse($json_data);

				$maxCount = count($json_data)-1;

				if ($maxCount > 50) { $maxCount = 50;}

				for ($i=count($json_data)-1; $i > count($json_data)-1-$maxCount; $i--) { 

					if ($json_data[$i]["name"]=="User Banned"){continue;}

					echo '<div class="allfwiedsItem">';
						$thisUser = $json_data[$i]["id"];
						echo '<a href="/?id='.$thisUser.'">';
							//
							$testString = str_replace(' ', '', $json_data[$i]['name']);
							$testString = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $testString);
							if ($testString=='') {
								$json_data[$i]['name']=''.$thisUser;
							}
							//
							echo '<b>'.$json_data[$i]["name"].'</b>';
							echo '<div><img src="u/'.$thisUser.'/avatar.jpg" style="width:calc( 100% - 20px ); max-height:300px;"></div>';
						echo '</a>';
					
					if ($admin) {

						if ($top) {
							echo '<button onClick="window.location=\'top.php?id='.$thisUser.'\';">Untop</button> ';
						}else{
							echo '<button onClick="window.location=\'top.php?id='.$thisUser.'\';">Top</button> ';
						}
		  	
				    }
				    if ($admin&&$id!=1) {

				    	$blocked=false;
				    	for ($j = 0; $j < count($blockeds); $j++) {
							if ($blockeds[$j]==$id) {
								$blocked=true;
							}
						}

						if ($blocked) {
							echo '<button onClick="window.location=\'block.php?id='.$thisUser.'\';">Unblock</button> ';
						}else{
							echo '<button onClick="window.location=\'block.php?id='.$thisUser.'\';">Block</button> ';
						}

				    	
				    	echo '<button onClick="window.location=\'remove.php?id='.$thisUser.'\';">Remove</button> ';
				    }
				    echo '</div>';

				}

			echo '</div>';
			die();
		}





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

		if(!folder_exist('u/'.$user)){die();}

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

		//var_dump($json_data['fwiends']);

		echo '<div class="allfwieds">';
		echo '<a href="/?id='.$user.'"">< Go Back</a>';

		//
		$testString = str_replace(' ', '', $json_data['name']);
		$testString = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $testString);
		if ($testString=='') {
			$json_data['name']=''.$user;
		}
		//

		echo '<h2>All '.$json_data['name'].'\'s Fwiends</h2>';

		$blockeds = $json_data["block"];
		$tops = $json_data["top"];

		// check bans
		$removeThem = [];
		for ($i = 0; $i < count($json_data["fwiends"]); $i++) {
			if(checkBan($json_data["fwiends"][$i],$bans)==true){
				array_push($removeThem,$i);
				continue;
			}
		}
		// remove bans
		$removeThem = array_reverse($removeThem);
		if(count($removeThem)>0){
			for ($i=0; $i < count($removeThem); $i++) { 
				array_splice($json_data["fwiends"], $removeThem[$i], 1);
			}
			$json_data_dest = json_encode($json_data, JSON_PRETTY_PRINT);
			file_put_contents('u/'.$user.'/user.json', $json_data_dest);
		}
		//

		$json_data["fwiends"] = array_reverse($json_data["fwiends"]);

		

		for ($i = 0; $i < count($json_data["fwiends"]); $i++) {
		   

			if(!folder_exist('u/'.$json_data["fwiends"][$i])){continue;}

		    $id = $json_data["fwiends"][$i];

		    $json_sourceLoop = file_get_contents('u/'.$id.'/user.json');
		    $json_dataLoop = json_decode($json_sourceLoop, true);


			if(checkBan($id,$bans)==true){
				continue;
			}

		    //if ($json_dataLoop["name"]=="User Banned"){continue;}

		    $top=false;
		    if ($admin) {

		    	
		    	for ($j = 0; $j < count($tops); $j++) {
					if (intval($tops[$j])==$id) {
						$top=true;
					}
				}
			}
		   
		    if ($top) {
		    	echo '<div class="allfwiedsItem allfwiedsItemTop">';
		    	echo '<a href="/?id='.$id.'">🔝 <b>'.$json_dataLoop["name"].'</b> 🔝';
		    }else{
		    	echo '<div class="allfwiedsItem">';
		    	echo '<a href="/?id='.$id.'"><b>'.$json_dataLoop["name"].'</b>';
		    }

		    


		    echo '<div>';
		    echo "<img style='width:calc( 100% - 20px );' src='u/".$id."/avatar.jpg'>";
		    echo '</div></a>';

		    if ($admin) {

				if ($top) {
					echo '<button onClick="window.location=\'top.php?id='.$id.'\';">Untop</button> ';
				}else{
					echo '<button onClick="window.location=\'top.php?id='.$id.'\';">Top</button> ';
				}
  	
		    }
		    if ($admin&&$id!=1) {

		    	$blocked=false;
		    	for ($j = 0; $j < count($blockeds); $j++) {
					if ($blockeds[$j]==$id) {
						$blocked=true;
					}
				}

				if ($blocked) {
					echo '<button onClick="window.location=\'block.php?id='.$id.'\';">Unblock</button> ';
				}else{
					echo '<button onClick="window.location=\'block.php?id='.$id.'\';">Block</button> ';
				}

		    	
		    	echo '<button onClick="window.location=\'remove.php?id='.$id.'\';">Remove</button> ';
		    }
		    echo '</div>';
		}

?>	
	<?php
		include 'footer.php';
	?>
</div></body>
</html>