<?php	
	//header('Referrer-Policy: no-referrer-when-downgrade');
	//
	$myID=-1;
	if (isset($_GET['id'])) { $user = intval($_GET['id']); }
	if (!$user) {$user = 1;}
	$login=false;
	$admin=false;
	$banned=false;
	session_start();
	include 'config.php';
	include 'parsedown.php';
	$Parsedown = new Parsedown();
	$Parsedown->setSafeMode(true);

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

	if ($_SESSION['valid']) {
		$login=true;
		$_SESSION['valid'] = true;
        $_SESSION['timeout'] = time();
		$myID = intval($_SESSION['id']);
		if ($myID==$user) {
			$admin=true;
		}

		if ($myID==1) {
			$tom=true;
		}

		if(!folder_exist('u/'.$myID)){
			//echo '<script>window.location="index.php"</script>';
			$login=false;
		}else{

      $my_source = file_get_contents('u/'.$myID.'/user.json');
      $my_data = json_decode($my_source, true);
      $my_name = $my_data["name"];
      if ($my_data["ban"]) {
        $_SESSION['valid']=false;
        echo $lockMsg.'<br>You are banned, oof, the end.';
        die();
      }

    }



	}
?><!--
  __  __        _____                   __          ___           _                   ___ ____              _   
 |  \/  |      / ____|                  \ \        / (_)         | |                 / _ \___ \            | |  
 | \  / |_   _| (___  _ __   __ _  ___ __\ \  /\  / / _ _ __   __| | _____      ____| (_) |__) | _ __   ___| |_ 
 | |\/| | | | |\___ \| '_ \ / _` |/ __/ _ \ \/  \/ / | | '_ \ / _` |/ _ \ \ /\ / / __\__, |__ < | '_ \ / _ \ __|
 | |  | | |_| |____) | |_) | (_| | (_|  __/\  /\  /  | | | | | (_| | (_) \ V  V /\__ \ / /___) || | | |  __/ |_ 
 |_|  |_|\__, |_____/| .__/ \__,_|\___\___(_)/  \/   |_|_| |_|\__,_|\___/ \_/\_/ |___//_/|____(_)_| |_|\___|\__|
          __/ |      | |                                                                                        
         |___/       |_|  

 A place for fwiends! 
-->
<!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">
	<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
	<div id="container"><?php $_REQUEST['date']; ?>
	<script>
		function timeConverter(t) {     
		    //var a = new Date(t * 1000);
		    var a = new Date(parseInt(t));
		    var today = new Date();
		    var yesterday = new Date(Date.now() - 86400000);
		    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
		    var year = a.getFullYear();
		    var month = months[a.getMonth()];
		    var date = a.getDate();
		    var hour = a.getHours();
		    if (hour<10) {hour='0'+hour};
		    var min = a.getMinutes();
		    if (min<10) {min='0'+min};
		    if (a.setHours(0,0,0,0) == today.setHours(0,0,0,0))
		        return 'Today, ' + hour + ':' + min;
		    else if (a.setHours(0,0,0,0) == yesterday.setHours(0,0,0,0))
		        return 'Yesterday, ' + hour + ':' + min;
		    else if (year == today.getFullYear())
		        return date + ' ' + month + ', ' + hour + ':' + min;
		    else
		        return date + ' ' + month + ' ' + year + ', ' + hour + ':' + min;
		}
		function addStyleString(str) {
		    var node = document.createElement('style');
		    node.innerHTML = str;
		    document.body.appendChild(node);
		}<?php 

		$baseUrl = "https://{$_SERVER['HTTP_HOST']}";

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



		


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

		$isFwiend=false;

		for ($i=0; $i < count($json_data['fwiends']) ; $i++) { 
			if ($json_data['fwiends'][$i]==$myID) {
				$isFwiend=true;
			};
		}?>	
	</script>
	<?php 


		/* online */
		if ($_SESSION['valid']) {
			$online_source = file_get_contents(ADMINFOLDER.'/online.json');
			$online_data = json_decode($online_source, true);		
			// filter / remove
			

			$onlinedate = time()*1000;

			// remove me
			for ($i = 0; $i < count($online_data); $i++) {
				if (	intval($online_data[$i]['id'])==intval($myID)	) {
					array_splice($online_data, $i, 1);
					continue;
				}
			}			
			// remove old
			for ($i = 0; $i < count($online_data); $i++) {
				if (	$onlinedate-intval($online_data[$i]['date'])>900000	) {
					array_splice($online_data, $i, 1);
					continue;
				}
			}	
			// push
			$onlineArray = array(
			    "name" => $my_name,
			    "id" => intval($myID),
			    "date" => $onlinedate
			);
			array_push($online_data, $onlineArray);
			// print
			/*
			for ($i = 0; $i < count($online_data); $i++) {
				echo '<script>';
					echo 'console.log("'.$online_data[$i]['id'].'");';
				echo '</script>';
			}
			*/
			$online_dest = json_encode($online_data, JSON_PRETTY_PRINT);	
			if ($myID!=1 && isset($_SESSION['id']) ) {
				file_put_contents(ADMINFOLDER.'/online.json', $online_dest);
			}		
			
		}
		/* */

		if ($json_data["ban"]) {

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

				include 'header.php';
				echo '<div class="topLeft">';
				echo 	'<a href="index.php?id='.$myID.'"">< Go Back</a>';
				echo 	'<h4>User is banned</h4>';
				echo '</div>';
				die();
		}	
	?>
	<style><?php 
		
		$userCSS = $json_data['css']; 
		if (strpos(strtolower($userCSS), 'script') == false && strpos($userCSS, '<?') == false && strpos(strtolower($userCSS), 'onerror') == false && strpos(strtolower($userCSS), 'style') == false && strpos(strtolower($userCSS), '.php') == false && strpos(strtolower($userCSS), 'onload') == false && strpos(strtolower($userCSS), '.xyz/myspace') == false ) {
			//

			
				$userCSS = str_replace("animation-name: slide;", "", $userCSS);
				$userCSS = str_replace("animation-name:slide;", "", $userCSS);
				$userCSS = str_replace("animation-name: slidein;", "", $userCSS);
				$userCSS = str_replace("animation-name:slidein;", "", $userCSS);
				$userCSS = str_replace("animation-name: flashy;", "", $userCSS);
				$userCSS = str_replace("animation-name:flashy;", "", $userCSS);
				$userCSS = str_replace("animation-name:flashy;", "", $userCSS);
				$userCSS = str_replace("animation-name: slideinright;", "", $userCSS);
				$userCSS = str_replace("animation-name:slideinright;", "", $userCSS);
				$userCSS = str_replace("animation-name: slideinleft;", "", $userCSS);
				$userCSS = str_replace("animation-name:slideinleft;", "", $userCSS);
				$userCSS = str_replace("-webkit-animation: fadein", "-webkit-animation: none", $userCSS);
				$userCSS = str_replace("-webkit-animation:fadein;", "-webkit-animation: none", $userCSS);
				//
				echo htmlspecialchars_decode($userCSS); 
    		//
		}

	?></style>
	<style>
		#header{all: unset;font-family: Arial;font-size: 15px;background: #003399;color: #fff;position: fixed;width: calc( 100% - 15px );height: 22px;padding: 5px;left: 0px;top: 0px;margin-bottom: 40px;border-bottom: 1px solid black;padding-left: 10px;padding-top: 6px;overflow: hidden;z-index: 300;}
		#hSub { all: unset; font-family: Arial;font-size: 15px;background: #6699cc;font-size: small;margin: 0px;padding: 5px; padding-top: 5px; padding-left: 5px;padding-left: 10px;padding-top: 4px;position: fixed;left: 0;top: 34px;width: calc(100% - 15px );color: #000;height: 15px;z-index: 300;}
		@media only screen and (max-width:768px){#header{height:51px}#hSub{top:63px}#hUp{float:left;left:0;font-size:small;position:fixed;top:35px}.sep{display:none}.topLeft{margin-top:30px}}
	</style>
	<?php include 'header.php'; ?>
	<div class="topLeft">
		<div><h2 class="uName" id="name"><?php echo $json_data['name']; ?></h2></div>
		<table class="short">
			<tr>	
				<td>
					<img src="u/<?php echo $user;?>/avatar.jpg?v=<?php echo time(); ?>" class="avatar" id="avatar">	
					<?php 
						if ($admin||$tom) {
							echo '<div style="text-align: center;"><button onClick="window.location=\'photo.php?id='.$user.'\'">Edit</button></div>';
						}
					?>	
				</td>
				<td class="shortRight">
					<div>"<span id="credo"><?php echo $json_data['credo']; ?></span>"</div><br>
					<div id="gender"><?php echo $json_data['gender']; ?></div>
					<div><span id="age"><?php echo $json_data['age']; ?></span> years old</div>
					<div id="country"><?php echo $json_data['country']; ?></div>
					<div id="online" style="display:none;margin-top:10px;"><img src="/img/online.gif"></div>
					<div style="font-size:small;margin-top:10px;">Last Login:<br><span id="lastLogin"><?php echo $json_data['last']; ?></span></div>
				</td>
			</tr>
		</table>
		<div class="boxInfo box">
			<div class="boxHead"><b>Contacting <span class="uName"><?php echo $json_data['name']; ?></span></b></div>
			<a href="send.php?id=<?php echo $user; ?>"><div class="boxButton" id="sMsg">Send Message</div></a>
			<a href="report.php?id=<?php echo $user; ?>"><div class="boxButton" id="report">Report</div></a>
			<a href="invitation.php?id=<?php echo $user; ?>"><div class="boxButton" id="add"><?php if ($isFwiend) {echo "Remove fwiend";}else{echo "Add to fwiends";}?></div></a>
			<a href="blokc.php?id=<?php echo $user; ?>"><div class="boxButton" id="block">Block</div></a>
		</div>
		<div class="url box">
			<div><b>MySpace URL:</b></div>
			<div><?php echo "<a style='color:#000;text-decoration:none;' href='"."https://{$_SERVER['HTTP_HOST']}/?id=".$user."'>"."https://{$_SERVER['HTTP_HOST']}/?id=".$user.'</a>'; ?></div>
		</div>
		<?php 
			if ($json_data['mp3']!=""&&strtolower(pathinfo($json_data['mp3'], PATHINFO_EXTENSION))=="mp3") {
			echo '<div class="player box">';
			echo '<div class="songName">';
				echo '<b>Song:</b> ';
				echo '<span class="songTitle">'.str_replace('%20', ' ', basename($json_data['mp3'], ".mp3").PHP_EOL).'</span>';
				echo '<a href="'.$baseUrl.'/u/'.$user.'/audio.mp3?v='.time().'" download="'.basename($json_data["mp3"], ".mp3").'.mp3"><img style="height: 15px; float: right; padding-right: 2px;" src="'.$baseUrl.'/img/download.svg"></a>';
			echo '</div>';	
		?><audio controls autoplay src="<?php echo 'u/'.$user.'/audio.mp3?v='.time(); ?>" controlsList="nodownload"></audio><?php		
			echo '</div>';}
			if ($admin||$tom) {
				echo '<div style="text-align: center;"><button onClick="window.location=\'mp3.php?id='.$user.'\'">Edit Mp3</button></div>';
			}
		?>	
	</div>
	<div class="topRight">
		<?php 
			if ($admin) {
		?>
		<div class="extended box"><a href="edit.php?id=<?php echo $myID; ?>"><h3>Edit your profile</h3></a></div>
		<?php
			}else{
		?><div class="extended box"><h3><span class="uName"><?php echo $json_data['name']; ?></span> is in your extended network</h3></div>
		<?php
			}
		?>
		<div class="box blurbs">
		<?php
		$filename = 'u/'.$user.'/blog.json';

		if (file_exists($filename)) {
			$json_source_blob = file_get_contents($filename);
			$json_data_blob = json_decode($json_source_blob, true);
			$blogs = $json_data_blob['blog'];

			if (count($blogs)>0 ) {
				echo "<div class='blogs'>";
				    echo "<div><b>".$json_data['name']."'s Latest Blog Entry"."</b></div>";
				    
					$min = 0;
					$max = count($blogs)-1;
					if ($max > 4) { $min=$max-4; } // max 5
					for ($i=$max; $i >=$min ; $i--) { 
						echo "<div>";
				    		echo $blogs[$i]['sub']." [<a href='blog.php?id=".$user."&b=".($i+1)."'>view more</a>]";
				    	echo "</div>";
					}
				echo "</div>";  
			}





		} else {
		    //
		}
		?>
			<div class="rightHead"><b><span class="uName"><?php echo $json_data['name']; ?></span>'s blurbs</b></div>
			<h4>About me:</h4>
			<div id="about"><?php 

			    // security fix
				$json_data['about'] = str_replace("penguins.win", "windows93.net", $json_data['about']);

				//$json_data['about'] = preg_replace('/((http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?)/', '<a href="\1">\1</a>', $json_data['about']);
				//echo nl2br($json_data['about']); 
				
				//echo $Parsedown->text($json_data['about']);
				echo Parsedown::instance()
   					->setSafeMode(true)
   					->setBreaksEnabled(true)
   					->text($json_data['about']); 

			?></div>
			<h4>Who I'd like to meet:</h4>
			<div id="who">
				<?php 

					// security fix
					$json_data['who'] = str_replace("penguins.win", "windows93.net", $json_data['who']);

					//echo nl2br($json_data['who']); 
					echo Parsedown::instance()
   						->setSafeMode(true)
   						->setBreaksEnabled(true)
   						->text($json_data['who']); 
				?>
			</div>	
		</div>
		<div class="box fwiends">
			<div class="rightHead"><b><span class="uName"><?php echo $json_data['name']; ?></span>'s Fwiends Space</b></div>
			<div class="fwiendCounter"><b><span class="uName"><?php echo $json_data['name']; ?></span> has <span class="fwiendCount"><?php echo count($json_data['fwiends']); ?></span> fwiends.</b></div>	
			<div id="top">
			<?php

				$top8 = $json_data['top'];
				if ($json_data['top']==[]) {
					$top8=$json_data['fwiends'];
				}
				$maxTop = count($top8);
				if ($maxTop>8) { $maxTop=8;}

				for ($i=0; $i < $maxTop ; $i++) { 

					if (folder_exist('u/'.$top8[$i])){

						$json_sourceTop = file_get_contents('u/'.$top8[$i].'/user.json');
						$json_dataTop = json_decode($json_sourceTop, true);

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

						if ($json_dataTop['name']!="User Banned"){
							echo '<a href="'.$baseUrl.'/?id='.$top8[$i].'"><div class="fwiendFace"><b>'.$json_dataTop['name'].'</b><img src="u/'.$top8[$i].'/avatar.jpg"></div></a>';
						}

					}


				}
			?>
			</div>
			<div id="viewFriends"><b>View <a href="all.php?id=<?php echo $user;?>">All</a> <span class="uName"><?php echo $json_data['name']; ?></span>'s Fwiends</b></div>
		</div>
		<div class="box comments">		
			<?php if($login&&!$banned){ 
				if ($isFwiend||$admin) {
				
			?>
			<div id="form">
				<div class="boxHead"><b>Leave a comment</b></div>
				<form action="comment.php" method="post">
			 		<textarea name="comment" id="commentInput"></textarea>
			 		<input type="hidden" name="user" value="<?php echo $user; ?>">
			 		<input type="submit" value="Send">
				</form>
			</div>
			<?php 
				}
			} else{ echo "<div style='margin-bottom:20px;'>Please <a href='login.php'>login</a> to leave a comment. </div>"; } ?><div class="boxHead"><b>Comments</b></div>	
			<table id="userWall">
				<?php 

					// current
					if(!folder_exist('u/'.$user)){die();}
					$json_source = file_get_contents('u/'.$user.'/wall.json');
					$json_data = json_decode($json_source, true);
					$wallCount = count($json_data);

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

						//var_dump($json_data[$i]);

						if ($json_data[$i]==null) { continue; }

						// ppl
			  			$id = $json_data[$i]['id'];
			  			if(!folder_exist('u/'.$id)){continue;}
			  			
			  			

			  			$json_sourceWallPost = file_get_contents('u/'.$id.'/user.json');
						$json_dataWallPost = json_decode($json_sourceWallPost, true);
						$wallName = $json_dataWallPost["name"];
						//
						/*
						$testString = str_replace(' ', '', $wallName);
						$testString = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $testString);
						if ($testString=='') {
							$wallName=''.$id;
						}
						*/
						//
						if ($wallName=="User Banned"){continue;}

						echo '<tr>';

							// avatar
							echo '<td class="tableLeft">';
								echo '<a href="'.$baseUrl.'/?id='.$id.'"><div><b>'.$wallName.'</b></div><img src="'.$baseUrl.'/u/'.$id.'/avatar.jpg"></a>';
							echo '</td>';

							// comment
							echo '<td class="tableRight">';


								if ($json_dataWallPost["ban"]) {
									$json_data[$i]['comment'] = "OOf";
								}

								//$json_data[$i]['comment'] = preg_replace('/((http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?)/', '<a href="\1">\1</a>', $json_data[$i]['comment']);
				
								echo '<div><b class="date">'.$json_data[$i]['date'].' </b></div>';
								


								//echo '<div>'.nl2br($json_data[$i]['comment']).'</div>';
								echo '<div>';
								echo Parsedown::instance()
			   						->setSafeMode(true)
			   						->setBreaksEnabled(true)
			   						->text($json_data[$i]['comment']); 
			   					echo '</div>';

								if ($admin||$tom) {
					  				echo '<button onClick="window.location=\'delete.php?id='.$user.'&t='.$json_data[$i]['date'].'\'">delete</button>';
					  			}else{
					  				if ($myID==$id&&$login) {
					  					echo '<button onClick="window.location=\'delete.php?id='.$user.'&t='.$json_data[$i]['date'].'\'">delete</button>';
					  				}
					  			}


							echo '</td>';

						echo '</tr>';

					}
				?>
			</table>
			<?php 
			echo '<div style="font-size:small;text-align:right;">';
				echo '('.$wallCount.'/100)';
			echo '</div>';

			?>
		</div>
	</div>
	<div id="imgPreview"><img src=""></div>
	<?php include 'footer.php'; ?>
	<script>
		$( ".date" ).each(function( index ) {
			var time =  timeConverter($( this ).text())
		 	$( this ).text(time)
		});
		var time = Date.now()-parseInt($('#lastLogin').text())
		if(time<900000||<?php echo $user; ?>==1){
			$('#online').show()
		}
		var time = new Date( parseInt($('#lastLogin').text()) )
		var d = time.getDate();if (d<10) {d="0"+d};		
		var m = time.getMonth()+1;if (m<10) {m="0"+m};
		var y = time.getFullYear();
		$('#lastLogin').text(	d+'/'+m+'/'+y	);
	</script>
	<script type="text/javascript" src="index.js"></script>
	</div>
</body>
</html>