<html>
<body>
<?php
session_start();
date_default_timezone_set("America/Los_Angeles");
//set status
if($_SESSION['username']!="guest"){
	$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);
}

//get users color
if($_SESSION['username']!="guest"){
	$userxml=simplexml_load_file("../account_data/".$_SESSION['username'].".xml");
	$color=$userxml->color;
} else {
	$color="#ffffff";
}

//read file
$get_chat_file=file_get_contents("chat.txt");
//manage text eg image parsing and link parsing ect
$usertext=str_replace(array("<",">"),"",$_POST['text']);
$usertext_split=explode(" ",$usertext);
$usertext="";
foreach($usertext_split as $text){
	
	if(substr($text,0,29)=="https://www.youtube.com/watch"){
		$text='
		
		
		<iframe width="500" height="300" src="https://www.youtube.com/embed/'.explode("&",explode("=",$text)[1])[0].'"></iframe>
		
		';
	}
	elseif(substr($text,0,4)=="http"||substr($text,0,5)=="https"){
		if(strpos($text,".jpg")||strpos($text,".png")||strpos($text,".gif")){
			$text='
			
			
			<a href="'.$text.'" target="_BLANK"><img width="500px" src="'.$text.'"></img></a>
			
			';
		} else {
			$text='<a href="'.$text.'" target="_BLANK">'.$text.'</a>';
		}
	}
	if($text=="nigger"){
		$text="ERROR. N**** TRIED TO BYPASS";
	}
	$usertext=$usertext.$text." ";
}

//tags
$tags="";
if($_SESSION['username']=="iceyrazor"){
	$tags=$tags.'<code style="color: white; background-color: rgba(0,50,200,0.4); padding-left: 4px; padding-right: 4px;">site owner</code>';
}
$chatfile=fopen("chat.txt","w");
$chat_file_send_content=$get_chat_file.'
<div class="inner-chat-box">
	<a class="inner-chat-box-a" href="profile.php?username='.$_SESSION['username'].'" target="_blank">
		<img class="inner-chat-box-pfp" src="'.$_SESSION['profilepicture'].'" />
	</a>
	<code class="inner-chat-box-wrap">
		'.$tags.'
		<code style="color: '.$color.';">'.$_SESSION['username'].'</code>: 
		<code style="padding-left: 5px; white-space: pre-line;"> '.$usertext.'</code>
	</code>
</div>
<br>
';

//cheak perms and run commands
$bot_tag='<code style="color: white; background-color: rgba(0,50,200,0.4); padding-left: 4px; padding-right: 4px;">site bot</code>';
$perm_clear=false;
$perm_mute_guest=false;
$perm_mute=false;
$perm_lock_chat=false;
$perm_bypass_lock=false;
foreach($userxml->permissions->perm as $perm){
	if($perm=="simplychatty.clear"){ $perm_clear=true; };
	if($perm=="simplychatty.mute-guest"){ $perm_mute_guest=true; };
	if($perm=="simplychatty.mute"){ $perm_mute=true; };
	if($perm=="simplychatty.lock-chat"){ $perm_lock_chat=true; };
	if($perm=="simplychatty.bypass-lock"){ $perm_bypass_lock=true; };
}
if($perm_clear==true&$usertext=="/clear "){
	$chat_file_send_content='
	<div style="font-size: 20; color: lightblue; text-decoration: underline;">
		Welcome To Simply Chatty
	</div><br><br><br><br>';
}
$chatstat=file_get_contents("chatstat.txt");
if($perm_lock_chat==true&$usertext=="/lock chat "){
	$file_chatstat=fopen("chatstat.txt","w");
	if($chatstat=="locked"){
		$chat_file_send_content=$get_chat_file.'<div class="inner-chat-box">'.$bot_tag." ".
		$_SESSION['username'].' unlocked the chat </div><br>';
		fwrite($file_chatstat,"unlocked");
	} else {
		$chat_file_send_content=$get_chat_file.'<div class="inner-chat-box">'.$bot_tag." ".
		$_SESSION['username'].' locked the chat </div><br>';
		fwrite($file_chatstat,"locked");
	}
	fclose($file_chatstat);
}
if($perm_bypass_lock==false&$chatstat=="locked"){
	$chat_file_send_content=$get_chat_file."";
}
if(false==true){
	//unfinished mute
	$chat_file_send_content=$get_chat_file.'<div class="inner-chat-box">'.$bot_tag." ".
	$_SESSION['username']." muted ".$usertext_split[1].'</div><br>';
}
//
fwrite($chatfile,$chat_file_send_content);
fclose($chatfile);

?>
<script language="javascript" type="text/javascript">window.close();</script>
</body>
</html>