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

$req = mysql_query("SELECT * FROM `user` WHERE `id` = '$user[id]'");
////////////////////////////
$avto=mysql_num_rows($req);
if($avto=="0"){
echo'Ошибка 1';
include 'inc/nogi.php';
exit;
}
$us = mysql_fetch_array($req);
if(empty($us[clan])){
echo' <div class="title">Ошибка</div>
<div class="empty_block item_center"> Вы не состоите в банде! </div>';
echo'<div class="block_link">';
echo"<a href='../index.php'> <img src ='../img/images/arrow.png'> Вернуться</a></div>";
include '../system/foot.php';
exit;}
/////////////

if($us[clan]!="$clans[id]"){

$req = mysql_query("SELECT * FROM `clans` where `id`='$us[clan]' or `leader`='$us[clan]' ");

$req = mysql_query("SELECT * FROM `clans` where `id`='$us[clan]' or `leader`='$us[clan]' ");

$avto=mysql_num_rows($req);
if($avto==1){
$clan1 = mysql_fetch_array($req);
$req = mysql_query("SELECT `id` FROM `user` where `clan`='$clan1[id]'");



////////////////////////////
$kk=mysql_num_rows($req);



switch($_GET[mod]){
default:

echo' <div class="title">Клан чат</div>';



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"><form name="form" action="chat.php?mod=ch" method="post">
<textarea name="msg" cols="25" rows="2">'.$say.'</textarea>';

echo '<br><input type="submit" class="button" value="Написать" /></form></div>';

if (('chat_clear') && isset($_GET['act']) && $_GET['act']=='clear1')  {  echo"<center><hr><img src='/pic/main/!.png' alt='*'/> Длина сообщения может содержать от 3 до 1000-ти рус. или латин. символов.</center>";  }
$cop=5;
$ca=mysql_result(mysql_query("SELECT COUNT(*) FROM `clans_chat` WHERE `banda`='$us[clan]' "),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';
$req = mysql_query("SELECT * FROM `clans_chat` where `banda`='$us[clan]' ORDER BY id  DESC LIMIT $start,$cop");

$avto=mysql_num_rows($req);
if($avto==0){
echo"<div class='empty block item_center'> Нет сообщений 
</div> ";}else{
while($chat = mysql_fetch_array($req))
{
echo"";
$nickas = strip_tags($chat['usr']);
$koment = strip_tags($chat['msg']);
$koment = smiles($koment);
$req3 = mysql_query("SELECT `rasa`, `login`,`id` FROM `user` WHERE `id` = '$nickas'");
$pic = mysql_fetch_array($req3);


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


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

echo '<a href="../users.php?id='.$pic['id'].'">'.htmlspecialchars(stripslashes($pic['login'])).' </a>
<a href="chat.php?&say='.$pic['id'].'">[отв]</a> ';
if($us[clan]=="$user[id]"){
echo ' <a href="chat.php?&s=delete&id='.$chat['id'].'"><img src="../img/images/del.png"></a>';
}
$koment = tags($koment);

echo" <br> $koment <br>";

$d = time()-$chat[time];
echo ' <small><font color=#999999>('.sec2day($d).' назад)</font></small>

</div>';


echo '';
}
echo '
<div class="empty_block top pagi item_center"> ';
if($page-1>0){
echo '<a href="chat.php?&page='.($page-1).'">&lt;</a> ';
}
if($page-1>0){
echo '<a href="chat.php?&page='.($page-1).'">'.($page-1).'</a> ';
}
echo '<b>'.$page.'</b>';
if($page+1<=$cp){
echo ' <a href="chat.php?&page='.($page+1).'">'.($page+1).'</a>';
}
if($page+1<=$cp){
echo ' <a href="chat.php?&page='.($page+1).'">&gt;</a>';
}
echo "</div>";
}

if($_GET['s']=='delete' && ($us[clan]=="$user[id]")){
if(isset($_GET['id'])){
$del = $_GET['id'];
}
if($del){
mysql_query("DELETE FROM `clans_chat` WHERE `id`='".$del."'");
header ("Location: chat.php"); exit;
}else{
header ("Location: chat.php"); exit;
}}

break;
case 'ch':
$msg=htmlspecialchars($_POST[msg]);
if(mb_strlen($msg,'utf-8') < 3)
{
header("Location: chat.php?&act=clear1");
exit;
}
if(mb_strlen($msg,'utf-8') > 1000)
{
header("Location: chat.php?&act=clear1");
exit;
}


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

$dater = time();
mysql_query("INSERT INTO `clans_chat` SET `usr`='$user[id]',`banda`='$us[clan]',
`msg`='".mysql_escape_string($msg)."', `time` = '$dater' ");
header ("Location: chat.php");






}
} else{
echo"Ошибка 3";
include '../system/foot.php';
exit;}
}
//////////////////

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