<?
require_once 'core/system.php';
echo only_reg();
echo ban();
$header = 'Чат';
require_once 'core/head.php';
switch($act) {
case 'post':
if(isset($_POST['msg'])){
$msg = check($_POST['msg']);
if(strlen($msg) < 3 or strlen($msg) > 400) $err = 'Длина сообщения должна быть в пределах 3 - 400 символов';
if($user[level] < 10) $err = 'В чате можно писать только с 10 уровня!';
if(!isset($err)) {
mysql_query("INSERT INTO `chat` SET `id_user` = '$user[id]', `msg` = '$msg', `time` = '".time()."'");
mysql_query("UPDATE `user` SET `gold`='".($user[gold]+1)."' WHERE `id`='$user[id]'");
header('Location: ?');
$_SESSION['message'] = 'Сообщение добавлено!';
exit();
}else{
header('Location: ?');
$_SESSION['err'] = $err;
// Вывод ошибки 
exit();
} 
}else{
header('Location: ?');
$_SESSION['err'] = 'Ошибка при отправке';
exit();
}
break;
default:
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `chat`"),0);
$k_page = k_page($k_post,$set['p_str']);
$page = page($k_page);
$start = $set['p_str']*$page-$set['p_str'];
echo '</textarea><span class="end">
</div>';
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `us_ban` where `usid` = '".$user['id']."' and `type` = 'chat' and `time`>'".time()."' "));
$nick = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` where `id` = '".$us['usban']."'"));
if($us['time'] > time())
{
$ban_time=$us['time']-time();
echo '</div><div class="line"></div><b><span class="red">Вам выдан бан на общение в чате<br><div class="dot-line"></div>До конца осталось:</span>
'.(int)($ban_time/3600).' час. '.($ban_time/60%60).' мин. '.($ban_time%60).' сек.
<br/><div class="dot-line"></div><span class="blue">Заблокировал:<a href="/user/'.$nick['id'].'/"><img src="/images/icon/user.png"/>'.$nick['nick'].' </span></a><br/><div class="dot-line"></div><span class="blue">Причина: </span>'.htmlentities($us['prich']).'</b><div class="line"></div>';
echo "</div>";
}else{
echo "</div>";
if($user[chat]>time()){
$t=$user[chat]-time();
echo "До отправки следующего сообщени ".($t%60)." секунд";
}else{
if(isset($_GET['id'])) {
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $_GET[id] LIMIT 1"));
echo '<form class="player" method="post" action="?act=post">
<input class="text large" value="'.$ank[nick].', " type="text" name="msg" /><br />
<span class="btn"><span class="end"><input class="label" type="submit" value="Отправить"></span></span>
</form>';
}else{
echo '<form class="player" method="post" action="?act=post">
<input class="text large" type="text" name="msg" /><br />
<span class="btn"><span class="end"><input class="label" type="submit" value="Отправить"></span></span>
</form>';
}
}
}
$q = mysql_query("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT $start, $set[p_str]");
echo "<div class='dot-line'></div>";
echo "<div class='player' style = 'color: #d3e2ed;'>";
while($post = mysql_fetch_assoc($q)) {
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
if($ank[prava] >= 5 ){
$color = 'yellow';
}
if($ank[prava] == 7 ){
$color = 'green';
}
if($ank[id] == 2 ){
$color = 'green';
}
if($ank[id] == 13){
$color = 'green';
}
if($ank[prava] == 4 ){
$color = 'blue';
}
if($ank[prava] == 0 ){
$color = 'quality-0';
}
if($ank[prava] == 3 ){
$color = 'Vip';
}
if ($ank['storona'] == '0') {?><img src="/images/icon/0.png" height="11" alt="a"/> <?php }
	    if ($ank['storona'] == '1') {?><img src="/images/icon/1.png" height="11" alt="d"/> <?php }


$nick = ($ank[prava] == 3) ? '<span style="color: blue">'.$ank['nick'].'</span>' : $ank['nick'];
$nick = ($ank[prava] == 5) ? '<span style="color: #FF0000">'.$ank['nick'].'</span>' : $ank['nick'];

echo " <a href='/user/?id=$ank[id]'><span style = 'color: #FFFFFF;'>$nick </span></a>";


if($ank[prava] >= 5 ){
echo "  ";
}
if($ank[prava] == 4 ){
echo "  ";
}
echo "<span class='yellow'></span> <a href='?id=$ank[id]'>(»)</a>";
if ($user['prava'] >= 5){
echo "";
echo "<a style='text-decoration:none;' href='?del=msg&id_msg=".$post['id']."'><span class='red'> (x)</span></a>";}
if(htmlspecialchars($_GET['del']) == 'msg') {
$id_chmsg = intval($_GET['id_msg']);
$del_chat_msg = mysql_query("DELETE FROM `chat` WHERE `id` = '".$id_chmsg."'");
header("Location: ?");
}
echo ': <span class="'.$color.'">'.($post['msg']).'</span><br>';
}
echo "</div>";
if ($k_page>1){
echo "<div class='dot-line'></div>";
str('?',$k_page,$page); // Вывод страниц
}
echo "</div></div>";
}
require_once 'core/foot.php';
?>