<?php
include ('inc/base.php');
include ('inc/functions.php');
include ('inc/user.php');
$title = 'Чат';
include ('inc/header.php');
if(!$user) {
header('location: /');
exit();
}
if($user['banned_time'] > time()) {
echo '<div class="main center">Бан!<br><small>Окончание: '.vremja($user['banned_time']).'</small></div>';
$location = 'banned_users';
}else{
$location = 'no_banned_users';
}
if($user['c_read_count'] != '0') {
$read_c = mysql_result(mysql_query('SELECT COUNT(*) FROM `chat` where `to` = "'.$user['id'].'" and `read` = "0"'),0);
mysql_free_result($read_c);
if($read_c >= '1') {
mysql_query("update `chat` set `read` = '1' where `to` = '".$user['id']."'");
}
}
if(isset($_POST['send'])) {
$text = text($_POST['text']);
$to = text(num($_GET['to']));
if(strlen($text) < 1 or strlen($text) > 1200) {
echo '<div class="main center">Длина сообщения 1-1200 символов!</div>';
echo '<a href="?" class="link center">Продолжить</a>';
include ('inc/footer.php');
exit();
}
if($to) {
mysql_query('INSERT INTO `chat` (`text`, `time`, `user`, `to`, `room`) VALUEs ("'.$text.'", "'.time().'", "'.$user['id'].'", "'.$to.'", "'.$location.'")');
}else{
mysql_query('INSERT INTO `chat` (`text`, `time`, `user`, `room`) VALUEs ("'.$text.'", "'.time().'", "'.$user['id'].'", "'.$location.'")');
}
echo '<div class="main center">Сообщение отправлено!</div>';
echo '<a href="?" class="link center">Продолжить</a>';
include ('inc/footer.php');
exit();
}
if(isset($_GET['to'])) {
$to = text(num($_GET['to']));
$acc_to = mysql_fetch_array(mysql_query('SELECT * FROM `accounts` WHERE `id` = "'.$to.'" LIMIT 1'));
mysql_free_result($acc_to);
if(!$acc_to or $acc_to['id'] == $user['id']) {
echo '<div class="main center">Собеседник не найден!</div>';
echo '<a href="?" class="link center">Продолжить</a>';
include ('inc/footer.php');
exit();
}
$to_msg = ' <img src="/style/src/to.png"> '.$acc_to['name'].' <a href="/chat.php">(x)</a>';
echo '<form action="?to='.$to.'" class="main center" method="POST">Сообщение '.$to_msg.' <br> <input type="text" name="text"><br><input type="submit" name="send" class="input_send" value="Отправить"></form>';
}else{
echo '<form action="?" class="main center" method="POST">Сообщение<br> <input type="text" name="text"><br><input type="submit" name="send" class="input_send" value="Отправить"></form>';
}
$max = text(num($_GET['max']));
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `chat` where `time` > '".(time() - 86400)."' and `room` = '".$location."'"),0);
mysql_free_result($k_post);
if($max and $max >= '11') {
$sql = mysql_query("SELECT * FROM `chat` where `time` > '".(time() - 86400)."' and `room` = '".$location."' ORDER BY `time` DESC LIMIT 0, $max");
}else{
$sql = mysql_query("SELECT * FROM `chat` where `time` > '".(time() - 86400)."' and `room` = '".$location."' ORDER BY `time` DESC LIMIT 0, 10");
}
while($chat = mysql_fetch_assoc($sql)) {
$acc_chat = mysql_fetch_array(mysql_query('SELECT * FROM `accounts` WHERE `id` = "'.$chat['user'].'" LIMIT 1'));
mysql_free_result($acc_chat);
if($chat['to'] == '0') {
$chat_status = '';
}else{
$account_to = mysql_fetch_array(mysql_query('SELECT * FROM `accounts` WHERE `id` = "'.$chat['to'].'" LIMIT 1'));
mysql_free_result($account_to);
$chat_status = ' <img src="/style/src/to.png"> '.$account_to['name'].'';
}
echo '<div class="main"><a href="?to='.$acc_chat['id'].'" class="none"><img src="/style/src/arrow.png"></a> '.$acc_chat['name'].' '.$chat_status.'<span style="float: right"><small>'.vremja($chat['time']).'</small></span> <br> <font color="#517878">'.text($chat['text']).'</font></div>';
mysql_free_result($chat);
}
if($k_post > '10' and !$max) {
echo '<div class="new-line"></div><a href="?max='.$k_post.'" class="link center">Показать все</a>';
}
if($k_post > '10' and $max) {
echo '<div class="new-line"></div><a href="?" class="link center">Скрыть все</a>';
}
if($k_post < '1') {
echo '<div class="main center">Сообщений нет!</div>';
}
include ('inc/footer.php');
?>