<?
include '../config.php';
include '../func.php';

head();
aut();
ban();
if (empty($settchat_restart_session)) {
    $settchat_restart_session = 10;
}
echo '<meta http-equiv="refresh" content="'.$time_obnov_session.'" />';

if ( $config['status_chat'] == 0) {
    echo "<div class=\"head\">Чат</div><div class=\"main\">" . katalog;
    echo link1 . 'Чат отключен' . div;
    navig();
    foot();
    exit();
}

include 'umnik.php';
$room = num($_GET['room']);
$room_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `room` WHERE `id` = '".$room."'"));
if($room_info['shut']==1) include 'bots/shutnik.php';
if($room_info['um']==1) include 'bots/umnik.php';
if($room_info['id']==2) include 'bots/anagr.php';
if($room_info['id']==1)  include 'bots/bot_bartender.php';
if($room_info['id']==14)  include 'bots/zag.php';
####################Запись онлайна##################
mysql_query("UPDATE `profile` SET `online` = 'room".$room_info['id']."' WHERE `id` = '".$id_session."'");
####################################################

echo "<div class=\"head\">Чат: <b>".$room_info['room']."</b> ";

if($room_info['um']==1) echo '<a href="../help.php?mod=vict"><img src="../images/help.png" alt="Помощь"/></a>' .div;
else echo div;
echo katalog;

if ( $_GET['error'] == 'msg' )
echo error('msg');

if ( $_GET['error'] == 'spam' )
echo error('spam');

if ( empty($_GET['error']) )
echo '';

echo aut .'<a href="add.php?room='.$room.'">Сказать</a> <a href="chat.php?room='.$room.'">Обновить</a> <a href="online.php?room='.$room.'">Кто Где?</a></div>' .div;

//echo div. kub . '<img src="../style/img/kub.gif" alt="" />' . div . katalog;


if(empty($settchat_postov_session)) $max = 10;
else $max = $settchat_postov_session;
$result = mysql_query("SELECT * FROM chat_msg  where `room` = '".$room."' order by id desc limit ".$max."");

while($msg = mysql_fetch_assoc($result)){
     
    //////////////
    $ot_user = num($msg['ot_user']);
    $ot = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$ot_user."'"));
    
$sql = mysql_query("SELECT * FROM `blacklist` WHERE `user_1` = $id_session AND `user_2` = $ot[id];");
if(@mysql_num_rows($sql) == NULL) { 
    $to_user = num($msg['to_user']);
    $to = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$to_user."'"));
    
    $time = t2($msg['date']);
    $private = num($msg['private']);
    $room = num($msg['room']);
    $emotion = num($msg['emotion']);
    $color = num($msg['color']);
    $antispam = num($msg['antispam']);
    $message = $msg['msg'];
    
    
    if (!empty($to_user)) {
        
        if ($to['login'] == $login_session) {
            $message = '<b>'.$to['login'].'</b>, '.$message;
        } else {
            $message = ''.$to['login'].', '.$message;
        }
        
    }
    
    if ( $settchat_adres_session == 1 ) {
        
    if ( $to['id'] == $id_session OR $ot['id'] == $id_session) {
    echo '<div class="'.($i++ % 2 ? 'link1' : 'link2').'">';
    if ($private != 1) {
    
    echo xstatus($ot['xstatus'],$ot_user).' <a href="inslide.php?user='.$ot_user.'&room='.$room.'"><b>'.colorl($msg['ot_user']).'</b></a>' .$time. emo($msg['emotion']).' '.colorp( mat(parsebb(adminbb(smile($message)))), $msg['color']). div;
    
    } else {
        
        if ( $ot_user == $id_session ) {
            
            echo xstatus($ot['xstatus'],$ot_user).' <a href="inslide.php?user='.$ot_user.'&room='.$room.'"><b>'.colorl($msg['ot_user']).'</b></a>' .$time.emo($msg['emotion']).' [!P] '.colorp( mat(parsebb(adminbb(smile($message)))), $msg['color']). div;
            
        }
        
        if ( $to_user == $id_session ) {
            
            echo xstatus($ot['xstatus'],$ot_user).' <a href="inslide.php?user='.$ot_user.'&room='.$room.'"><b>'.colorl($msg['ot_user']).'</b></a> ' .$time.emo($msg['emotion']).' [!P] '.colorp( mat(parsebb(adminbb(smile($message)))), $msg['color']). div;
            
        }
        
    }
        
    } else {
        echo div;
    }
    
    } else {
    echo '<div class="'.($i++ % 2 ? 'link1' : 'link2').'">';
    if ($private != 1) {
    
    echo xstatus($ot['xstatus'],$ot_user).' <a href="inslide.php?user='.$ot_user.'&room='.$room.'"><b>'.colorl($msg['ot_user']).'</b></a>' .$time.emo($msg['emotion']).' '.colorp( mat(parsebb(adminbb(smile($message)))), $msg['color']). div;
    
    } else {
        
        if ( $ot_user == $id_session ) {
            
            echo xstatus($ot['xstatus'],$ot_user).' <a href="inslide.php?user='.$ot_user.'&room='.$room.'"><b>'.colorl($msg['ot_user']).'</b></a>' .$time.emo($msg['emotion']).' [!P] '.colorp( mat(parsebb(adminbb(smile($message)))), $msg['color']). div;
            
        }
        
        if ( $to_user == $id_session ) {
            
            echo xstatus($ot['xstatus'],$ot_user).' <a href="inslide.php?user='.$ot_user.'&room='.$room.'"><b>'.colorl($msg['ot_user']).'</b></a>' .$time.emo($msg['emotion']).' [!P] '.colorp( mat(parsebb(adminbb(smile($message)))), $msg['color']). div;
            
        }
        
    }
  }



}
}

if ( $settchat_vvod_session == 0 ) {
if (empty($_POST['submit'])) {
echo  '</div><div class="katalog"><form action="add.php?room='.$room.'" method="post" name="form">';
echo '<textarea id="tarea" name="msg" cols="30" rows="5" style="width: 99%;"></textarea><br/>';
if($settchat_emo_session==0){
echo '<select name="emo">
<option value="0" selected="selected">[Без эмоции]</option>
<option value="1">Радостно</option>
<option value="2">Печально</option>
<option value="3">Удивленно</option>
<option value="4">Ласково</option>
<option value="5">Смущенно</option>
<option value="6">Кокетливо</option>
<option value="7">Обиженно</option>
<option value="8">Настойчиво</option>
<option value="9">Шепотом</option>
<option value="10">Задумчиво</option>
<option value="11">Злобно</option>
</select><br/ >';
} 
if($settchat_color_session==0){
echo '<select name="color">
<option value="0" selected="selected">[Cтандартный]</option>
<option value="1">Красный</option>
<option value="2">Синий</option>
<option value="3">Зеленый</option>
<option value="4">Оранжевый</option>
<option value="5">Фиолетовый</option>
<option value="6">Розовый</option>
</select><br/ >';
} 
echo '<input name="submit" type="submit" class="button" value="Сказать" /></form>' . div;

//echo div. kub . '<img src="../style/img/kub.gif" alt="" />' . div . katalog;

} else {

$msg = html($_POST['msg']);
$post = $chat_post_session+1;

/////////Антиспам///////////
$msg_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `chat_msg` where `ot_user` = '".$id_session."'order by id desc limit 1"));
if ( $msg_info['antispam'] > time()  AND $msg_info['ip'] == $_SERVER['HTTP_X_FORWARDED_FOR']) {
    
    echo rew('chat.php?room='.$room.'&error=spam');
    exit();
}
$antispam = ( time() + $anti_spam );
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$result = mysql_query("INSERT INTO `chat_msg` SET `msg` = '".$msg."', `ot_user` = '".$id_session."', `room` = '".$room."', `date` = '".time()."'");
mysql_query("UPDATE `profile` SET `postov` = '".$post."' WHERE `id` = '".$id_session."'");
echo rew('chat.php?room='.$room.'');


}

//echo div . katalog;

}
$online = mysql_result(mysql_query("SELECT count(*) FROM `profile` WHERE `time` > '".(time()-300)."' AND `online` = 'room".$room."'ORDER BY `time`"), 0);

if ( $online > 0 ) {
    echo link2 . '<b>В комнате: </b>';
    $result = mysql_query("SELECT * FROM `profile` WHERE `time` > '".(time()-300)."' AND `online` = 'room".$room."'ORDER BY `time`");
    while($users = mysql_fetch_assoc($result)){
    echo user($users['id']).' ';
    }
    echo div;
    
}
if ($room_info['id'] == 1) echo '<div class="katalog"><a href="list_drink.php?room='.$room.'">Меню (Бармен)</a></div>';
echo '<div class="razdel"><center><a href="history.php?room='.$room.'">История</a> <a href="index.php?">Комнаты</a> <a href="../index.php">Главная</a> <a href="menu.php?room='.$room.'">Настройки</a> <a href="../cabinet.php">Кабинет</a></center>' .div;



foot();

?>