<?php	
	header('Referrer-Policy: no-referrer-when-downgrade');
	//	
	$myID=-1;
	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(!folder_exist('u/'.$myID)){
			echo '<script>window.location="index.php"</script>';
			die();
		}

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

	}else{
			/*echo "You are not logged.";
			die();	*/
			$myID=0;	
	}
	if (isset($_GET['id'])) { 
		$user = intval($_GET['id']); 
	}else{
		if ($myID) {
			$user=$myID;
		}else{
			$user=1;
		}
		
	}
	if (isset($_GET['b'])) { 
		$blog = intval($_GET['b']); 
	}else{
		$blog = 0;
	}


	if ($myID==$user) {
		$admin=true;
	}

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

	if ($my_data["comB"]) {
			// notification
			$com_source = file_get_contents('u/'.$myID.'/user.json');
			$com_data = json_decode($com_source, true);
			$com_data["comB"]=false;
			$com_dest = json_encode($com_data, JSON_PRETTY_PRINT);
			file_put_contents('u/'.$myID.'/user.json', $com_dest);
	}

	$bans = getBans();

?><!--
  __  __        _____                   __          ___           _                   ___ ____              _   
 |  \/  |      / ____|                  \ \        / (_)         | |                 / _ \___ \            | |  
 | \  / |_   _| (___  _ __   __ _  ___ __\ \  /\  / / _ _ __   __| | _____      ____| (_) |__) | _ __   ___| |_ 
 | |\/| | | | |\___ \| '_ \ / _` |/ __/ _ \ \/  \/ / | | '_ \ / _` |/ _ \ \ /\ / / __\__, |__ < | '_ \ / _ \ __|
 | |  | | |_| |____) | |_) | (_| | (_|  __/\  /\  /  | | | | | (_| | (_) \ 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 class="blogBody">
	<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);

		$isFwiend=false;

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

	<?php 
		if ($json_data["ban"]) {
				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 ) { 
			//
		    $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;}
	</style>

	<?php include 'header.php'; 

		$filename = 'u/'.$user.'/blog.json';
		$blogged=false;

		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'];
			$blogged=true;

			if (count($blogs)==0) {
				$blogs = array (
			    	array("date" => $date = time()*1000, "sub" => "Nothing", "msg" => $json_data['name']." has nothing to show here.")
				); $blogged=false;
			}

		} else {

			$blogs = array (
			    array("date" => $date = time()*1000, "sub" => "Nothing", "msg" => $json_data['name']." has not blogged yet.")
			);


		    //
		    //echo "blog doesn't exist :$";
		    //die();
		}	
		    
		?>
		<div class="blogTitle">
			
				<h2 class="uName" id="name">
				<a href="/blog.php?id=<?php echo $user;?>" style="text-decoration:none;color:inherit;"><?php echo $json_data['name']; ?>'s Blog</a>
				</h2>
	
			<?php
				echo '<div style="margin-top:20px;display:inline-block;width:100%;">';
					echo '<button onClick="window.location=\'/?id='.$user.'\'" style="float:left;">⬅ Back to Profile</button> ';
				/*
				if ($blogged) {
					echo '<button onClick="window.location=\'/blog.php?id='.$user.'\'">📖 All Blogs</button> ';
				}
				*/
				
				if ($admin||$tom) {
					echo '<button onClick="window.location=\'blogNew.php\'" style="float:right;">✏️ New Blog</button> ';
				}
				echo '</div>';
			?>

		</div>

	<div class="topLeft blogCard">

		
					<img src="u/<?php echo $user;?>/avatar.jpg?v=<?php echo time();?>" class="avatar" id="avatar">	

			
					<div>"<span id="credo"><?php echo $json_data['credo']; ?></span>"</div>
					<div id="gender" style="font-size:small;"><?php echo $json_data['gender']; ?></div>
					<div><span id="age" style="font-size:small;"><?php echo $json_data['age']; ?></span> years old</div>
					<div id="country" style="font-size:small;"><?php echo $json_data['country']; ?></div>
					<div style="font-size:small;">Last Login: <span id="lastLogin"><?php echo $json_data['last']; ?></span></div>
					<div id="online" style="display:none;margin-top:10px;"><img src="/img/online.gif"></div>
	

		<div class="boxInfo box" style="margin-top:10px;">
			<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">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";}else{echo "Add";}?></div></a>
			<a href="blokc.php?id=<?php echo $user; ?>"><div class="boxButton" id="block">Block</div></a>
		</div>


		<div>
			<?php 
			if ($blogged) {
				echo "<b>All Blogs (".count($blogs)."/10)</b>";
				echo "<ul>";
				for ($i=count($blogs)-1; $i >=0 ; $i--) { 
					echo "<li>";
			    		echo "<a href='blog.php?id=".$user."&b=".($i+1)."'>".$blogs[$i]['sub']."</a>";
			    	echo "</li>";
				}
				echo "</ul>";
			}
			?>
		</div>


	</div>

	<div class="topRight blogPost">

		
		<div class="blogs">

		<?php

			echo "<div>";

			if ($blog == 0) {
				$blog = count($blogs);
				$min = 0;
				$max = count($blogs)+1;
				$com=false;
			}else{
				$min = $blog-1;
				$max = $blog-1;	
				$com=true;			
			}




			for ($i=$max; $i >=$min ; $i--) { 




				if ($blogs[$i]) {
					echo "<div class='box blog'>";
			    		echo "<div><b class='date'>".$blogs[$i]['date']."</b></div>";
			    		echo "<a href='/blog.php?id=".$user."&b=".($i+1)."' style='text-decoration:none;'>";
			    			echo "<div class='blogCorpus'><h1>".$blogs[$i]['sub']."</h1></div>";
			    		echo "</a>";
			    		
			    		echo "<div class='blogCorpus'>";



			    		// echo $blogs[$blog-1]['msg'];

			    		// security fix
						$blogs[$i]['msg'] = str_replace("penguins.win", "windows93.net", $blogs[$i]['msg']);
					    
					    echo Parsedown::instance()
		   					->setSafeMode(true)
		   					->setBreaksEnabled(true)
		   					->text($blogs[$i]['msg']); 


		   					if ($blogged) {
								if ($admin||$tom) {
						    		echo '<div style="margin-top:20px;">';
										echo '<button onClick="window.location=\'blogEdit.php?id='.$user.'&b='.($i+1).'\'">Edit</button> ';
										echo '<button onClick="window.location=\'blogDelete.php?id='.$user.'&b='.($i+1).'\'">Delete</button>';
									echo '</div>';
								}

								
		   					}
					    	

					    if (!$com) {
					    	echo '<a href="/blog.php?id='.$user.'&b='.($i+1).'">';
					    	echo '<small>';
					    	if (count($blogs[$i]['comments'])==1) {
					    		echo "1 Comment";
					    	}
					    	if (count($blogs[$i]['comments'])==0) {
					    		echo "No Comment";
					    	}
					    	if (count($blogs[$i]['comments'])>1) {
					    		echo count($blogs[$i]['comments'])." Comments";
					    	}
					    	echo '</small><br>';
					    	if($admin||$tom){
								echo '<small class="blinkViolet">';
						    	if (count($blogs[$i]['commentsWaiting'])==1) {
						    		echo "1 Comment waiting approval";
						    	}
						    	if (count($blogs[$i]['commentsWaiting'])>1) {
						    		echo count($blogs[$i]['commentsWaiting'])." Comments waiting approval";
						    	}
						    	echo '</small>';	
					    	}
					    	echo '</a>';				    	
					    }
			    		
			    		echo "</div>";





			    	echo "</div>";	

			    	echo "<div class='box comments'>";


			    	if ($com) { 
			    		
			    		if($login&&!$banned){ 

							if ($isFwiend||$admin) { ?>
								<div id="form">
									<div class="boxHead"><b>Leave a comment</b></div>
									<form action="<?php echo 'blogCom.php?id='.$user.'&b='.($i+1); ?>" method="post">
								 		<textarea name="comment" id="commentInput"></textarea>
								 		<input type="hidden" name="user" value="<?php echo $myID; ?>">
								 		<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>"; 
						}



						







			    	}

			    	echo "</div>";





			    				// approved coms

								if ($blogs[$i]['comments']&&$com) {
									echo '<div class="boxHead"><b>Comments</b></div>';
									echo '<table id="userWall">';

									// ban filter
									//
									$removeThem = [];
									for ($j=count($blogs[$i]['comments'])-1; $j >=0 ; $j--) {		
										if(checkBan(intval($blogs[$i]['comments'][$j]['id']),$bans)==true){
											array_push($removeThem,$j);
											continue;
										}
									}
									//
									if(count($removeThem)>0){
										for ($k=0; $k < count($removeThem); $k++) { 
											array_splice($blogs[$i]['comments'], $removeThem[$k], 1);
										}
										$json_data_blob["blog"] = $blogs;
										$blog_dest = json_encode($json_data_blob, JSON_PRETTY_PRINT);
										file_put_contents($filename, $blog_dest);
									}
									//
								

									for ($j=count($blogs[$i]['comments'])-1; $j >=0 ; $j--) {
										$cDate=$blogs[$i]['comments'][$j]['date'];
										$cId=$blogs[$i]['comments'][$j]['id'];
										$cMsg=$blogs[$i]['comments'][$j]['msg'];
										if(!folder_exist('u/'.$cId)){continue;}
										$json_sourceWallPost = file_get_contents('u/'.$cId.'/user.json');
										$json_dataWallPost = json_decode($json_sourceWallPost, true);
										$wallName = $json_dataWallPost["name"];

										if ($wallName=="User Banned"){continue;}
										
										
										echo '<tr>';
											// avatar
											echo '<td class="tableLeft" style="width:1%;">';
												echo '<a href="'.$baseUrl.'/?id='.$cId.'"><div><b>'.$wallName.'</b></div><img src="'.$baseUrl.'/u/'.$cId.'/avatar.jpg" style="max-width:115px;"></a>';
											echo '</td>';
											// comment
											echo '<td class="tableRight">';								
												echo '<div><b class="date">'.$cDate.' </b></div>';
												echo '<div>';
												echo Parsedown::instance()
							   						->setSafeMode(true)
							   						->setBreaksEnabled(true)
							   						->text($cMsg); 
							   					echo '</div>';
							   					
												if ($admin||$tom) {
									  				echo '<button onClick="window.location=\'deleteBlogCom.php?id='.$user.'&b='.$blog.'&c='.$j.'\'">delete</button>';
									  			}
											echo '</td>';

										echo '</tr>';
									} 
									echo '</table>';
									echo '<div style="font-size:small;text-align:right;">';
									echo '('.count($blogs[$i]['comments']).'/10)';
									echo '</div>';
								}









									// ban filter
									//
									$removeThem = [];
									for ($j=count($blogs[$i]['commentsWaiting'])-1; $j >=0 ; $j--) {		
										if(checkBan(intval($blogs[$i]['commentsWaiting'][$j]['id']),$bans)==true){
											array_push($removeThem,$j);
											continue;
										}
									}
									//
									if(count($removeThem)>0){
										for ($k=0; $k < count($removeThem); $k++) { 
											array_splice($blogs[$i]['commentsWaiting'], $removeThem[$k], 1);
										}
										$json_data_blob["blog"] = $blogs;
										$blog_dest = json_encode($json_data_blob, JSON_PRETTY_PRINT);
										file_put_contents($filename, $blog_dest);
									}
									//


			    				// waiting coms

								if ( ($blogs[$i]['commentsWaiting']&&$com)&&($admin||$tom) ) {
									echo '<div class="boxHead"><b>Comments waiting approval</b></div>';
									echo '<table id="userWall">';
									
									for ($j=count($blogs[$i]['commentsWaiting'])-1; $j >=0 ; $j--) {

										$cDate=$blogs[$i]['commentsWaiting'][$j]['date'];
										$cId=$blogs[$i]['commentsWaiting'][$j]['id'];
										$cMsg=$blogs[$i]['commentsWaiting'][$j]['msg'];
										if(!folder_exist('u/'.$cId)){continue;}
										$json_sourceWallPost = file_get_contents('u/'.$cId.'/user.json');
										$json_dataWallPost = json_decode($json_sourceWallPost, true);
										$wallName = $json_dataWallPost["name"];
										if ($wallName=="User Banned"){continue;}
										echo '<tr>';
											// avatar
											echo '<td class="tableLeft" style="width:1%;">';
												echo '<a href="'.$baseUrl.'/?id='.$cId.'"><div><b>'.$wallName.'</b></div><img src="'.$baseUrl.'/u/'.$cId.'/avatar.jpg" style="max-width:115px;"></a>';
											echo '</td>';
											// comment
											echo '<td class="tableRight">';								
												echo '<div><b class="date">'.$cDate.' </b></div>';
												echo '<div>';
												echo Parsedown::instance()
							   						->setSafeMode(true)
							   						->setBreaksEnabled(true)
							   						->text($cMsg); 
							   					echo '</div>';
							   					
												if ($admin||$tom) {
													echo '<button onClick="window.location=\'approveBlogCom.php?id='.$user.'&b='.$blog.'&c='.$j.'\'">Approve</button>';

									  				echo '<button onClick="window.location=\'deleteBlogComW.php?id='.$user.'&b='.$blog.'&c='.$j.'\'">delete</button>';
									  			}
											echo '</td>';

										echo '</tr>';
									} 
									echo '</table>';
									echo '<div style="font-size:small;text-align:right;">';
									echo '('.count($blogs[$i]['commentsWaiting']).'/10)';
									echo '</div>';
								}


				}

			
			}

			


		echo "</div>";  
		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>