<?php
require_once 'core/sys.php';
include 'core/func_smiles.php';
include 'core/antimat.php';
include 'core/func_time.php';
reg();
head('Чат');
if(isset($_GET['dele']) and !empty($_GET['id']) and $user['admins']>0){
$post_id = int($_GET['id']);
$post = mysql_fetch_assoc(mysql_query("SELECT `id`,`text` FROM `chat` WHERE `id`='".$post_id."' LIMIT 1"));
if(!$post){
header("Location: index.php");
exit;
}
mysql_query("INSERT INTO `admin_log` SET 
`text`='<a href=\"pers.php?ank=".$user['id']."\">".$user['login']."</a> удалил сообщение в чате <b>".$post['text']."</b>',
`time`='".time()."'");
#mysql_query("DELETE FROM `chat` WHERE `id`='".$post['id']."'");
mysql_query("UPDATE `chat` SET 
`text`='<span style=\"color: #2A5200\"><a href=\"pers.php?ank=".$user['id']."\">".$user['login']."</a> удалил сообщение.</span>'
 WHERE `id`='".$post['id']."'");
header("Location: chat.php");
}
if(isset($_GET['ban']) and !empty($_GET['ank']) and $user['admins']>0){
$ank_id = int($_GET['ank']);
$ank = mysql_fetch_assoc(mysql_query("SELECT `login`,`id`,`admins` FROM `users` WHERE `id`='".$ank_id."' LIMIT 1"));
if($user['admins'] <= $ank['admins']){
echo '<div id="error">Самый умный тут?)</div>';
foot();
exit;
}
if(!$ank){
header("Location: index.php");
exit;
}

$time_ban = (60*60*2)+time();
echo '<div class="row">';
	echo'<a id="link_button" href="chat.php?ban&ank='.$ank['id'].'&ok">Молчанка</a> <a id="link_button" href="chat.php?">Пощадить</a></div>';
	if(isset($_GET['ok'])){
	mysql_query("UPDATE `users` SET `chat_ban`='".$time_ban ."' WHERE `id`='".$ank_id."'");
	mysql_query("INSERT INTO `chat` SET 
	`user_id`='".$user['id']."',
	`text`='<span style=\"color: #2A5200\">На <b>".$ank['login']."</b> действует молчанка.Он не может писать в чат.</span>',
	`time`='".time()."'");
	mysql_query("INSERT INTO `admin_log` SET 
	`text`='<a href=\"pers.php?ank=".$user['id']."\">".$user['login']."</a> кинул молчанку <a href=\"pers.php?ank=".$ank['id']."\">".$ank['login']."</a>(Чат)',
	`time`='".time()."'");
	header("Location: chat.php");
		}
	
foot();
exit;
}
if(isset($_GET['otv']) and !empty($_GET['ank'])){
$ank_id = int($_GET['ank']);
$ank = mysql_fetch_assoc(mysql_query("SELECT `login`,`id`,`admins` FROM `users` WHERE `id`='".$ank_id."' LIMIT 1"));
	if(!$ank){
	header("Location: chat.php");
	exit;
	}
header('Location: chat.php?otvet='.$ank['login']);
foot();
exit;
}
if(isset($_GET['add'])){
	if($user['chat_ban']> time()){
	$ban_ost = $user['chat_ban'] - time();
	echo '<div id="error">Вам запрещено писать в чате.<br>До разблокировки: '.Sec2Time($ban_ost) .'</div>';
	#83.222.115.218:27084
	foot();
	exit;
	}
	$last_post = mysql_fetch_assoc(mysql_query("SELECT `text` FROM `chat` WHERE `user_id`='".$user['id']."' ORDER BY `time` DESC LIMIT 1"));
		if($last_post['text']==$_POST['text']){
		header("Location: chat.php");
		exit;
		}
	if(empty($_POST['text'])){
	header("Location: chat.php");
	}else{
		
	$text = protect($_POST['text']);
		if(mb_strlen ($text, 'utf-8') < 2){
		echo '<div id="error">Слишком короткое сообщение</div>';
		foot();
		exit;
		}
		mysql_query("INSERT INTO `chat` SET 
		`user_id`='".$user['id']."',
		`text`='".$text."',
		`time`='".time()."'");
		mysql_query("UPDATE `users` SET `money`=`money`+3 WHERE `id`='".$user['id']."'");
		
	if(isset($_GET['monstr']) and isset($_GET['room'])){
	header("Location: attack.php?monstr=".$_GET['monstr']."&room=".$_GET['room'].'&page='.$_GET['page']);
	}else{
		header("Location: chat.php");
		}
	}
}
echo '<div class="name">Чат</div><div class="row">
<span style="font-size: 70%;">1 сообщение = +<img src="img/money/money.png"> 3<br>
За набивание сообщений будет наказание </span><br>
<form action="chat.php?add" method="post">
<textarea name="text">';
if(isset($_GET['otvet']) and !empty($_GET['otvet'])){
$otvet = protect($_GET['otvet']);
echo $otvet.', ';
}
echo '</textarea><br>
<input type="submit" value="Отправить">
<a href="faq.php?chat_rules">Правила</a>|<a href="smiles.php">Смайлы</a>
</form>
</div><div class="row" id="center"><span class="refresh_chat"><a href="chat.php"><img src="img/other/refresh.png"> Обновить</a></span><br>
</div>';
$sql_chat = mysql_query("SELECT * FROM `chat` ORDER BY `time` DESC LIMIT ".$page.",".$set['str']." ");
$total = mysql_num_rows(mysql_query("SELECT (id) FROM `chat`"));
if($total==0){
echo '<div class="row">В чате пусто. Будь первым!</div>';
}

while($chat = mysql_fetch_assoc($sql_chat)){
#header('Refresh: 10; URL=chat.php');
$ank = mysql_fetch_assoc(mysql_query("SELECT `login`,`id`,`level`,`admins` FROM `users` WHERE `id`='".$chat['user_id']."' LIMIT 1"));
$nick = '<a href="pers.php?ank='.$ank['id'].'">'.$ank['login'].'</a>';
	if($ank['admins']>0){
	
	$nick = '<a href="pers.php?ank='.$ank['id'].'"><span style="color: #0f7b8c; font-weight: bold;text-shadow: 1px 0 8px #0f7b8c;">'.$ank['login'].'</span></a>';
	}

echo '<div class="row">'.$nick.'('.$ank['level'].') <small>['.clock($chat['time']).']</small>';
if($user['id']!=$chat['user_id']){
echo '<a href="chat.php?otv&amp;ank='.$chat['user_id'].'" style="font-size: 80%;">[отв]</a>';
}
if($user['admins']>0){
echo '<span style="float: right;"><a href="chat.php?ban&amp;ank='.$chat['user_id'].'">[ban]</a>';
echo '<a href="chat.php?dele&amp;id='.$chat['id'].'">[x]</a></span>';
}
$chat['text'] = str_replace($user['login'].',','<span class="login">'.$user['login'].'</span>,',$chat['text']);
echo'<br> '.smile($chat['text']).'</div>';
}
if($total > $set['str'])
	{
		pages($page, $total, 'chat.php?', $set['str']);
	}
foot();
?>