<?php session_start(); ?>
<html>
<head>
<style>
html{
	overflow: scroll;
	overflow-x: hidden;
	min-height: 100%;
	position: relative;
	background-color: rgb(30,30,30);
	color: white;
}
::-webkit-scrollbar {
	width: 0px;  /* Remove scrollbar space */
	background: transparent;  /* Optional: just make scrollbar invisible */
}
code{
	padding-left: 2px;
	padding-right: 2px;
	text-align: left;
}
.chat-box{
	position: relative;
	border: solid 4px black;
	top: 0vh;
	max-width: 60%;
	height: 72vh;
	overflow: scroll;
	padding: 5px;
}
.autost{
	color: white;
	background-color: green;
	padding: 4px;
}
.statusbox{
	position: absolute;
	border: solid 3px black;
	padding: 5px;
	top: 10%;
	left: 2%;
	font-size: 15px;
}
.hider{
	position: absolute;
	background-color: rgba(0,0,0,0.8); 
	width: 100%;
	bottom: 0px;
	top: 0px;
	left: 0px;
	display: none;
}
.pbox{
	color: white;
	width: 90%; 
	height: 60vh;
	top: 30px;
	left: 30px;
	padding: 3%;
	position: relative;
	background-color: rgb(50,50,50);
	overflow: scroll;
	font-size: 20px;
}

.inner-chat-box{
	font-size: 15px;
}
.inner-chat-box-pfp{
	height: 50px; 
	width: 50px; 
	object-fit: cover; 
	border-radius: 50px;
}
.inner-chat-box-wrap{
	position: relative; 
	bottom: 20;
}
.inner-chat-box-a{
	text-decoration: none;
}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<?php
date_default_timezone_set('America/Los_Angeles');
$set_logout_status=true;
require_once("../login/login.php");
if($login_correct=="true"&&$_SESSION['username']!="guest"){
	//set status
	$statusf=simplexml_load_file("status.xml");
	$file = fopen("status.xml","w");
	$userexist=false;
	for($i=0;$i<=count($statusf->user);$i++){
		if($statusf->user[$i]==$_SESSION['username']){
			$userexist=true;
			break;
		}
		if($userexist==true){break;}
	}
	$i=0;
	fwrite($file, '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<status>'."\n");
	$wrote=false;
	foreach($statusf as $status){
		if(($statusf->user[$i]==$_SESSION['username'])||($userexist==false)){
			if($userexist==false){
			fwrite($file, '	<user time="'.$statusf->user[$i]['time'].'">'.$statusf->user[$i]."</user>\n");
			}
			if($wrote==false){fwrite($file, '	<user time="'.date("n/j/Y/H:i").'">'.$_SESSION['username']."</user>\n"); $wrote=true;}
		} else {
			fwrite($file, '	<user time="'.$statusf->user[$i]['time'].'">'.$statusf->user[$i]."</user>\n");
		}
		$i++;
	}
	fwrite($file, '</status>');
	fclose($file);
}
$xml=simplexml_load_file("../account_data/".$_SESSION['username'].".xml");
$ban="false";
for($i=count($xml->history->action);$i>=0;$i--){
	if(($xml->history->action[$i]['page']=="simplychat")&&($xml->history->action[$i]['type']=="ban")){
		$ban="true";
		$reason=strval($xml->history->action[$i]);
		break;
	}
	if($ban=="true"){break;}
}
?>
<title>welcome <?php echo $_SESSION['username']; ?></title>
<div align="center"><div id="error-message" style="display: none; text-align: center; background-color: red; width: 50%;"></div></div>
<script language="javascript" type="text/javascript">
if("<?php echo $_SESSION['errormsglogin']; ?>"!=""){
	$("#error-message").css("display","block");
	$("#error-message").html("<?php echo $_SESSION['errormsglogin']; ?>");
}
</script>
<div align="center"><div align="center" id="chat" class="chat-box"></div></div>
<a href="../index.html" style="text-decoration: none; color: white; size: 20px; border: 2px outset white; padding: 4px; position: absolute; top: 10px; left: 10px;">go home</a>
<br>


<form id="chatform" align="center" method="post" action="chatposter.php" target="_blank" >
<textarea autocomplete="off" type="text" name="text" id="text" style="height: 50px; width: 50%; background-color: rgb(30,30,30); color: white; font-size: 15;" ></textarea>
</form>

<div id="status" class="statusbox"></div>

<div align="center">
<input autocomplete="off" id="autost" class="autost" type="button" onclick="autos()" value="toggle autoscroll" />
<input autocomplete="off" class="autost" style="background-color: rgba(0,0,0,0);" type="button" onclick="show('rules')" value="show rules, commands, info" />
<input autocomplete="off" class="autost" style="background-color: rgba(0,0,0,0);" type="button" onclick="cleartext()" value="clear text" />
</div>

<div id="time" align="right" style="position: relative; float: right; width: 10vw; right:1vw; bottom: 3vh;"></div>
<audio controls id="hardr" style="width: 0%; height: 0%;"><source src="hardr.wav" type="audio/mpeg"></audio>

<div style="position: absolute; right:20px; top: 40px; padding: 10px;">
<a href="profile.php?username=<?php echo $_SESSION['username']; ?>" target="_blank">
<img style="width:70px; height: 70px; border-radius: 70px; object-fit: cover;" src="<?php echo $_SESSION['profilepicture']; ?>" alt="../account_data/guest.png"></img>
<div style="position: absolute; top: 30%; left: 7%; font-size: 15px; color: white; text-decoration: none; background-color: rgba(30,30,30,0.5);" align="center">logged in as<br><?php echo $_SESSION['username']; ?></div>
</a>
</div>

<script language="javascript" type="text/javascript">username="<?php echo $_SESSION['username']; ?>"</script>
<script src="../login/login.js" ></script>

<div id="rules" class="hider" onclick="hide('rules')"><div class="pbox" id="hidertext"></div></div>
<div id="errorbox" class="hider" onclick="hide('errorbox')"><div class="pbox" id="errorboxtext"></div></div>

<script language="javascript" type="text/javascript">
if('<?php echo $ban; ?>' == 'true') {
	if(alert('you were banned by admin. reason: '+'<?php echo $reason; ?>')) {
		window.location.replace("../index.html");
	} else {
		window.location.replace("../index.html");
	}
	throw new Error('This is not an error. This is just to abort javascript');
}
//toggle auto scroll
function autos(){
	switch(autoscroll){
		case true:
			autoscroll=false;
			$("#autost").css("background-color", "red");
			break;
		case false:
			autoscroll=true
			$("#autost").css("background-color", "green");
			break;
	}
}
//post rule commands based on perms
var pboxtext="because of my webhost TOS, NO 18+ CONTENT IS ALLOWED ON THIS CHAT<br>";
if('<?php for($i=0;$i<=count($xml->permissions->perm);$i++){ if($xml->permissions->perm[$i]=="permission.simplychat.clear"){ echo $xml->permissions->perm[$i]; }} ?>'=="permission.simplychat.clear"){
	var pboxtext=pboxtext+
	"/clear --clears chat<br>";
}
if('<?php for($i=0;$i<=count($xml->permissions->perm);$i++){ if($xml->permissions->perm[$i]=="permission.simplychat.mute"){ echo $xml->permissions->perm[$i]; }} ?>'=="permission.simplychat.mute"){
	var pboxtext=pboxtext+
	"/mute,user,time(m minutes,h hours,d days),reason --mutes the user with the set amount of time, example"
	+" /mute,guest,1m,test<br>";
}
if('<?php for($i=0;$i<=count($xml->permissions->perm);$i++){ if($xml->permissions->perm[$i]=="permission.simplychat.ban"){ echo $xml->permissions->perm[$i]; }} ?>'=="permission.simplychat.ban"){
	var pboxtext=pboxtext+
	"/ban,user,reason --permanatly bans the user example /ban,guest,fwaggot<br>";
}
var pboxtext=pboxtext+'<br><br><a style="color: rgb(0,120,230);" target="blank" href="http://iceyfox.x10host.com/discord_rules.html">rules</a>'
$("#hidertext").html(pboxtext);
//hide and show
function show(data){
	$("#"+data).css("display","block");
}
function hide(data){
	$("#"+data).css("display","none");
}
//clear text
function cleartext(){
	$("#text").val("");
}
//read the fucking file
var autoscroll=true;
chatget('chat');
chatget('status');
chatloader();
setInterval(chatloader,2000);
function chatget(mode){
	if(mode=="chat"){
	$.ajax({
		type: "GET",
		url: "chat.txt",
		cache: false,
		async: true,
		success: function chatreturn(data){
			$("#chat").html(data);
			loadedchat=data;
		}
	});}
}
function chatloader(){
	usaTime = new Date().toLocaleString("en-US", {timeZone: "America/Los_Angeles"});
	usaTime = new Date(usaTime);
	//read file
	$.ajax({
		type: "GET",
		url: "chat.txt",
		cache: false,
		async: true,
		success: function(data){
			if(data!=loadedchat){
				chatget('chat');
			}
		}
	});
	$.ajax({
		type: "GET",
		url: "status.xml",
		cache: false,
		async: true,
		dataType: "xml",
		success: function(xml){
			str="";
			datetime = (usaTime.getMonth()+1) + "/"
			+ usaTime.getDate()  + "/" 
			+ usaTime.getFullYear() + "/"  
			+ usaTime.getHours() + ":"  
			+ usaTime.getMinutes();
			$(xml).find('user').each(function(index){
				getTime=$(this).attr('time');
				if((getTime.split(":")[0].split("/")[3] != usaTime.getHours())||((getTime.split("/")[0]+getTime.split("/")[1]+getTime.split("/")[2]) != (datetime.split("/")[0]+datetime.split("/")[1]+datetime.split("/")[2]))){
					var status = "offline";
					var statuscolor = "gray";
				} else if((parseInt(getTime.split(":")[1])+3 < parseInt(usaTime.getMinutes()))||(parseInt(getTime.split(":")[0].split("/")[3]) != parseInt(datetime.split(":")[0].split("/")[3]) )){
					var status = "away";
					var statuscolor = "#E64C00";
				} else {
					var status = "online";
					var statuscolor = "rgb(0,200,50)";
				}
				str=str+($(this).text()+' <code style="background-color: '+statuscolor+';">'+status+'</code><br>time: '+getTime+"<br><br>");
			});
			$("#time").html("pst "+datetime);
			$("#status").html(str);
		}
	});
	//rest of loop shit
	if(autoscroll==true){
		var elem = document.getElementById('chat');
		elem.scrollTop = elem.scrollHeight;
	}
}
//run the text cheak stuff
$("#text").keyup(function(event) {
	if (event.keyCode === 13 && !event.shiftKey) {
		$("#text").val($("#text").val().slice(0,-1));
		runtext();
	}
});
function runtext(){
	if(($("#text").val().toLowerCase().includes("nigger"))==true){
		document.getElementById('hardr').play();
		return false;
	} else if($("#text").val().length>2000){
		show('errorbox');
		$("#errorboxtext").html("your text can not be greater than 2000 characters<br>"+'<input autocomplete="off" class="autost" style="background-color: rgba(0,0,0,0);" type="button" onclick="cleartext()" value="clear text?" />');
		return false;
	} else {
		$("#chatform").submit();
	}
	$("#text").val('');
}
</script>
<?php $_SESSION['errormsglogin']=""; ?>
</body>
</html>