<?php
include 'system/bd.php';
include 'system/cook.php';
include ("system/gzip.php");
if(isset($_SESSION['auth']) && $_SESSION['auth']==1){
include 'system/head.php';

if($user['save'] == 0) {

  echo '<div class=\'content\'><font color=\'#909090\'>Для общения в чате вам нужно сохранить своего персонажа</font></div>';
include 'system/foot.php';
exit;}



if(isset($_POST['msg'])){
$_POST['msg'] = trim($_POST['msg']);

$faq = mysql_query("SELECT * FROM `config`");
$base = mysql_fetch_array($faq);

if($user[lvl]<$base[chat_lvl]){
$err[]='<center><img src="/pic/main/!.png" alt="*"/> Писать в чате доступно с '.$base[chat_lvl].' уровня.</center>';
}else{


if(!empty($_POST['msg'])){


if(mysql_result(mysql_query("SELECT COUNT(*) FROM `chat` WHERE `user`='".$user['id']."'"),0)>0 AND 5>(time()-mysql_result(mysql_query("SELECT `time` FROM `chat` WHERE `user`='".$user['id']."' ORDER BY `id` DESC LIMIT 1"),0)))$err[]='<center>Нельзя писать так часто!</center>';
if(strlen($_POST['msg'])>1000) $err[]='<center><img src="/pic/main/!.png" alt="*"/> Слишком длинное сообщение!</center>';
if(strlen($_POST['msg'])<2) $err[]='<center><img src="/pic/main/!.png" alt="*"/> Слишком короткое сообщение!</center>';
}else{

$err[]='<center><img src="/pic/main/!.png" alt="*"/> Пустое сообщение!</center>';

}}



if(!$err){


$req = mysql_query("SELECT * FROM `block`  WHERE `usr`='$user[id]' and `ban_time`>'$time' and `tip` = '1'");
////////////////////////////
$avto=mysql_num_rows($req);
if($avto>=1){
While($mag = mysql_fetch_array($req))
{
header("Location: chat.php"); exit;
}}



$msg=$_POST['msg'];
$msg=substr($msg, 0, 1000);

$msg=str_replace("<","",$msg);
$msg=str_replace(">","",$msg);

$msg = addslashes($msg);
$msg=preg_replace ("|[\r\n]+|si","",$msg);
 include ("inc/antireklama.php");


mysql_query("INSERT INTO `chat`(`user`,`msg`,`time`) VALUES('".$user['id']."','$msg','".time()."')");

header("Location: chat.php"); exit;
}else{
foreach($err as $value){
echo '
<div class="help">'.$value.'</div>
';
}
}
}


$req = mysql_query("SELECT * FROM `block`  WHERE `usr`='$user[id]' and `ban_time`>'$time' and `tip` = '1' ");
////////////////////////////
$avto=mysql_num_rows($req);
if($avto>=1){
While($mag = mysql_fetch_array($req))
{
echo'<div class="title">Игровой чат</div>';

echo' <div class="empty_block item_center"> На вашего персонажа наложена молчанка, вы не можете писать сообщения в чате.</div>';


////////////////////
$cop=10;
$ca=mysql_result(mysql_query("SELECT COUNT(*) FROM `chat`"),0);
$cp=ceil($ca/$cop);
if(isset($_GET['page']) && is_numeric($_GET['page'])){
$page = $_GET['page'];
if($_GET['page']>$cp) $page=$cp;
if($_GET['page']<1)$page=1;
}else{
$page=1;
}
$start=$page*$cop-$cop;
$dlist = 'list1';
$q=mysql_query("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT $start,$cop");
while ($result=mysql_fetch_assoc($q)){
echo '<div class="'.$dlist.'">';
$_user = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id`='".$result['user']."'"));



echo'<div class="chat">';

$t = time()-7000;
if($_user[online] < $t) { 
echo"<img src='img/user/icons/".$_user[storona]."_".$_user[rasa]."_off.png'>"; 
}else{
echo"<img src='img/user/icons/".$_user[storona]."_".$_user[rasa].".png'>";
}

echo '
<a href="users.php?id='.$_user['id'].'"> '.$_user['login'].'</a>';
if($_user[prava] == '3' ){
echo "<font color='orange'> [Админ] </font> ";}
if($_user[prava] == '2' ){
echo "<font color='lime'> [Модератор] </font> ";}
if($_user[prava] == '1' ){
echo "<font color='yellow'> [Консультант] </font> ";}


echo' <span class="role0"> ';


$koment = strip_tags($result['msg']);
$koment = smiles($koment);

if($result[tyrney]=="1"){
echo "$result[msg]</span>";
}else{
if($_user[prava] == '3' ){
echo "<font color='orange'></br> $koment </font> ";
}else{
if($_user[prava] == '2' ){
echo "<font color='lime'></br> $koment </font> ";
}else{
if($_user[prava] == '1' ){
echo "<font color='yellow'></br> $koment </font> ";
}else{
echo "</br> $koment";
}}}
}


$d = time()-$result['time'];
echo '</br><span class="role0"><small> 
 ('.sec2day($d).' назад)</small></span>';


echo '</div>	';



if ($dlist == 'list1'){
	$dlist = 'list2';
}else{
	$dlist = 'list1';
}
}





echo ' <div class="empty_block top pagi item_center">';

if($page-1>0){

echo '<a href="?page='.($page-1).'">&lt;</a> ';

}

if($page-1>0){

echo '<a href="?page='.($page-1).'">'.($page-1).'</a> ';

}


echo '<b>'.$page.'</b>';

if($page+1<=$cp){

echo ' <a href="?page='.($page+1).'">'.($page+1).'</a>';

}

if($page+1<=$cp){

echo ' <a href="?page='.($page+1).'">&gt;</a>';



}
echo '</div>';



///////////////////
		
			
	


echo'<center>';
if($user[prava] == 1 ){
}
if($user[prava] == 1){
if(isset($_GET['chat'])){
mysql_query("DELETE FROM `chat`");
$dater = time();
mysql_query("INSERT INTO `chat` SET `user` = '2', `msg` = '$user[login] очистил чат',`time` = '$dater'");
 
}}

echo'</center>';


echo "</div> ";
//////////////////////////////////////

/////////////без бана//////////////

include 'system/foot.php';
exit;
}}

echo'<div class="title">Игровой чат</div>';

if($_GET['go']=='delete' && ($user['prava']=="1" || $user['prava']=="5")){
if(isset($_GET['id'])){
$del = $_GET['id'];
}
if($del){
mysql_query("DELETE FROM `chat` WHERE `id`='".$del."'");
$dater = time();
mysql_query("INSERT INTO `chat` SET `user` = '2', `msg` = '$user[login] удалил сообщние', `time` = '$dater'");

header ("Location: chat.php");
 exit;

}else{
header ("Location: chat.php"); exit;
}}
if ($_GET['say']){
	$say=mysql_result(mysql_query("SELECT `login` FROM `user` WHERE `id`='".intval($_GET['say'])."'"),0);
	$say = str_replace("$say","$say, ",$say);
}else{
	$say='';
}

echo' <div class="empty_block del_bottom item_center">';

echo '<form name="form" action="chat.php" method="post"><textarea name="msg" cols="25" rows="2">'.$say.'</textarea>

<br><input type="submit" class="button" value="Отправить">
</form>
</div><hr>';



////////////////////
$cop=10;
$ca=mysql_result(mysql_query("SELECT COUNT(*) FROM `chat`"),0);
$cp=ceil($ca/$cop);
if(isset($_GET['page']) && is_numeric($_GET['page'])){
$page = $_GET['page'];
if($_GET['page']>$cp) $page=$cp;
if($_GET['page']<1)$page=1;
}else{
$page=1;
}
$start=$page*$cop-$cop;
$dlist = 'list1';
$q=mysql_query("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT $start,$cop");
while ($result=mysql_fetch_assoc($q)){
echo '<div class="'.$dlist.'">';
$_user = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id`='".$result['user']."'"));




echo'<div class="chat">';
$t = time()-7000;
if($_user[online] < $t) { 
echo"<img src='img/user/icons/".$_user[storona]."_".$_user[rasa]."_off.png'>"; 
}else{
echo"<img src='img/user/icons/".$_user[storona]."_".$_user[rasa].".png'>";
}
echo '
<a href="users.php?id='.$_user['id'].'"> '.$_user['login'].'</a>';

if($_user[prava] == '3' ){
echo "<font color='orange'> [Админ]</font> ";
}
if($_user[prava] == '2' ){
echo "<font color='lime'> [Модератор]</font> ";
}
if($_user[prava] == '1' ){
echo "<font color='yellow'> [Консультант]</font> ";
}
if($_user[id] == '2' ){
echo "<font color='orange'> [Системный робот]</font> ";
}
if($_user[login] == $user[login]){
echo'';
}else{
echo' <a href="?say='.$_user['id'].'">[отв]</a>';
}
if($_user[login] == $user[login]){
echo'';
}else{
if($user[prava] == '3' ){
 echo' <a href="moderka.php?mod=1&id='.$_user['id'].'"> [бан] </a><span class="role0">';
}
}
if ($user['prava']=='3'){
	echo ' <a href="?go=delete&id='.$result['id'].'"><img src=\'img/images/del.png\'></a> ';
}



$koment = strip_tags($result['msg']);
$koment = smiles($koment);

if($result[tyrney]=="1"){
echo "$result[msg]</span>";
}else{

if($_user[prava] == '3' ){
echo "<font color='orange'></br> $koment </font> ";
}else{
if($_user[prava] == '2' ){
echo "<font color='lime'></br> $koment </font> ";
}else{
if($_user[prava] == '1' ){
echo "<font color='yellow'></br> $koment </font> ";
}else{
if($_user[id] == '2' ){
echo "<font color='orange'></br> $koment </font> ";
}else{
echo "</br> $koment ";

}}}}}

$d = time()-$result['time'];
echo '</br><span class="role0"> ('.sec2day($d).' назад)</span>';


echo '</div>';



if ($dlist == 'list1'){
	$dlist = 'list2';
}else{
	$dlist = 'list1';
}
}





echo ' <div class="empty_block top pagi item_center">
<center>';

if($page-1>0){

echo '<a href="?page='.($page-1).'">&lt;</a> ';

}

if($page-1>0){

echo '<a href="?page='.($page-1).'">'.($page-1).'</a> ';

}


echo '<b>'.$page.'</b>';

if($page+1<=$cp){

echo ' <a href="?page='.($page+1).'">'.($page+1).'</a>';

}

if($page+1<=$cp){

echo ' <a href="?page='.($page+1).'">&gt;</a>';



}
echo '</center></div>';



///////////////////
		
			
	


echo'<center>';
if($user[prava] == '3' ){
}
if($user[prava] == 3){
if(isset($_GET['chat'])){
mysql_query("DELETE FROM `chat`");
$dater = time();
mysql_query("INSERT INTO `chat` SET `user` = '2', `msg` = '$user[login] очистил чат',`time` = '$dater'");
 
}}

echo'</center>';


if($user[prava] == '3' ){
echo "
<div class='line'></div>

<div class='block_link'> <a href='?chat'><img src='img/images/arrow.png'>Очистить чат</a></div>";
}
echo "</div>";
/////////////



}else{
header ("Location: index.php"); exit;
}
include 'system/foot.php';
?>
