<?php
require_once '../core/system.php';
echo only_reg();
echo ban();
$header = 'Общий Чат';
require_once H.'core/head.php';
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `us_ban` where `usid` = '".$user['id']."' and `type` = 'common' and `time`>'".time()."' "));
$nick = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` where `id` = '".$us['usban']."'"));
switch($act) {
case 'post':
if ($us['time'] < time()) {
if(isset($_POST['msg'])){
$msg = check(filter($_POST['msg']));
if(mb_strlen($msg) < 2 or mb_strlen($msg) > 550) $err =  'Длина сообщения должна быть в пределах 2 - 550 символов';
if($user[level] < 0) $err =  'В чате можно писать только с 6 уровня!';
if(!isset($err)) {
// генерируем уникальный id
$random_val1 = mt_rand(1000, 999999);
$unique = base_convert($random_val1, 10, 11);
if(mysql_result(mysql_query("SELECT COUNT(*)  FROM `chat` WHERE `id` = '$unique'"),0) == true) {
$random_val2 = mt_rand(1000, 999999);
$unique1 = base_convert($random_val2, 10, 11);
}
if(mysql_result(mysql_query("SELECT COUNT(*)  FROM `chat` WHERE `id` = '$unique1'"),0) == true) {
$random_val3 = mt_rand(1000, 999999);
$unique2 = base_convert($random_val3, 10, 11);
}
if (!isset($unique2) && !isset($unique1)){
$ready_id = $unique;
}elseif (isset($unique1)){
$ready_id = $unique1;
}Elseif(isset($unique2)){
$ready_id = $unique2;
}
if ($_POST['system_message'] == '1') {
$sender = $system_profile_id;
}else{
$sender = $user['id'];
}
mysql_query("INSERT INTO `chat` SET `id` = '$ready_id', `id_user` = '$sender', `msg` = '$msg', `time` = '".time()."'");
header('Location: /chat');
$_SESSION['message'] = 'Сообщение добавлено!';
exit();
}else{
header('Location: /chat');
$_SESSION['err'] = $err;
// Вывод ошибки 
exit();
} 
}else{
header('Location: /chat');
$_SESSION['err'] = 'Введите текст';
exit();
}
}Else{
header('Location: /chat');
$_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><br><span class="end">

 ';
if($us['time'] > time())
{
include '../forum_dir_data/ban_notice.php';
}else{
echo "</div>";
echo "<div class='mini-line'></div>";
if(isset($_GET['to'])) {
$ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = ".intval($_GET['to'])." LIMIT 1"));
echo '<form class="player" method="post" action="?act=post">';
if ($user['prava'] >= '7') {
echo '<input type="checkbox" name="system_message" value="1"  />От системы <a href="/user/'.$system_profile_id.'/">(Игровой сервис)</a>';
}
echo '<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">';
if ($user['prava'] >= '7') {
echo '<input type="checkbox" name="system_message" value="1"  />От системы <a href="/user/'.$system_profile_id.'/">(Игровой сервис)</a>';
}
echo '<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 `time` 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 = 'Administratr';
}
if($ank[prava] == 7 ){
$color = 'Administratr';
}
if($ank[id] == 2 ){
$color = 'Administratr';
}
if($ank[id] == 13){
$color = 'Administratr';
}
if($ank[prava] == 4 ){
$color = 'Moder';
}
if($ank[prava] == 0 ){
$color = 'quality-0';
}
if($ank[prava] == 3 ){
$color = 'Vip';
}
// если есть цветной ник, то грузим цвет
$req222 = mysql_query("SELECT * FROM `color_akk` WHERE `usr` = '$user[nick]' LIMIT 1"); // защита от нескольких акк
$avto=mysql_num_rows($req222);
if($avto==1){
$color = mysql_fetch_array($req222);
}
if ($ank['storona'] == '0') {
echo '<img src="/images/icon/0.png" height="11" alt="angel"/> ';
}
if ($ank['storona'] == '1') {
echo'<img src="/images/icon/1.png" height="11" alt="demon"/> ';
}
echo online($ank[id]);
echo " <a href='/user/?id=$ank[id]'><b><span style = 'color: #becdd8;'>$ank[nick]</span></b></a> <span class='yellow'>[".vremja($post[time])."]</span> <a href='?to=$ank[id]&page=$page'>(»)</a>";
if ($post['deleted'] == 0) {
if(htmlspecialchars($_GET['kill_message'])) {
             $id_chmsg = intval($_GET['kill_message']);
            $del_chat_msg = mysql_query("UPDATE `chat` SET `deleted` = '1', `delitor_id` = '$user[id]' WHERE `id` = $_GET[kill_message] ");
                               header("Location: /chat");
$_SESSION['message'] = 'Сообщение было удалено';                                
}
include 'bb_chat_expection.php';
}elseif ($post['deleted'] == 1) {
echo ': <span style="color:grey;">Сообщение было удалено</span><br>';

}
}
echo "</div>";
if ($k_page>1){
echo "<div class='dot-line'></div>";
str('?',$k_page,$page); // Вывод страниц
}
echo "<div class='mini-line'></div>";
echo "<div class='player menuList'>";
echo "<li><a href='/user/moderators.php'><img src='/images/icon/user.png'>Список Модераторов</a></li>";
echo "</div>";
}
require_once H.'core/foot.php';
?>