<?
require ($_SERVER['DOCUMENT_ROOT'].'/maintenance/vars.php');
require (ROOT.'/maintenance/dbconn.php');
require (ROOT.'/maintenance/functions.php');
query ("UPDATE users SET online=0 WHERE last_chat<".(time()-300)." AND last_query<".(time()-300));
$pers = user(UID);
if (isset($_REQUEST["msg"]) && !empty($_REQUEST["msg"])):
	$pers = user(UID);
	$time = time();
	$chat_time = date("H:i:s");
	$to = 'all';
	$type = 'all';
	$message = htmlspecialchars($_REQUEST["msg"]);
	$from = UID;
	chat($time,$chat_time,$to,$from,$type,$message);
endif;

query ("UPDATE users SET last_chat=".time()." WHERE uid=".UID);
?>
<script type="text/javascript" src="JS/chat_v01.js"></script>
<script type="text/javascript">

<?
$src = mysql_query("SELECT * FROM chat WHERE `time`>".$pers["last_chat"]." AND (`to`='all' OR `to`='".$pers["uid"]."') ORDER BY `time` ASC");
for ($a=0,$b=mysql_num_rows($src);$a<$b;$a++):
$chat_time = mysql_result($src,$a,'chat_time');
$from = mysql_result($src,$a,'from');
if ($from<>'system'): $from = user($from);
else: $from = 'system';
endif;
$to = '';
$message = mysql_result($src,$a,'message');
/*
if ($m["from"]<>'s'): $m["from"] = user("SELECT user FROM users WHERE uid=".$m["from"]); $m["from"] = $m["from"]["user"]; endif;
if ($m["to"]<>'all'): $m["to"] = user("SELECT user FROM users WHERE uid=".$m["to"]); $m["to"] = $m["to"]["user"]; endif;*/
?>
chat ('<?=$chat_time?>','<?=$from["user"]?>','<?=$to?>','<?=$message?>');
<? if ($pers["cfight"]<>'' && $pers["cfight"]<>0 && $pers["cpage"]<>'fight'): 
query("UPDATE users SET cpage='fight' WHERE uid=".UID);
?>
parent.frames["main_window"].location = '/main.php';
<? endif; ?>
<? endfor; ?>
</script>