<?php

require '../system/start.php';
require '../system/functions.php';
reg();
$room = mysql_fetch_assoc(mysql_query("select * from `chat_rooms` where `id` = ".int($_GET['id']).""));
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `chat_users` WHERE `id_user` = ".$user['id'].""),0)==0)
mysql_query("INSERT INTO `chat_users` (`id_user`, `time`, `rid`) values(".$user['id'].", '".time()."', ".$room['id'].")");
else mysql_query("UPDATE `chat_users` SET `time` = '".time()."', `rid` = ".$room['id']." WHERE `id_user` = ".$user['id']."");
$in = mysql_query("SELECT * FROM `chat_ini`");
while($arr=mysql_fetch_assoc($in)) $ini[$arr['id']]=$arr['value'];
$str['mesto'] = 'Чат';
$str['block'] = $set['site'].' / Чат ';
$chat = mysql_result(mysql_query("select count(*) from `users` where `where` = 'Чат' and time > ".(time()-300).""),0); 
include '../style/'.$style.'/head.php';
echo '<div class="where">"'.$room['title'].'"</div>';
if (empty($room['title']))
{
	echo '<div class="on">Такой комнаты не существует!</div>';
	include '../style/'.$style.'/foot.php';
	exit;
}
if(isset($_GET['act']) && htmlspecialchars(trim($_GET['act'])) == 'delete')
{
	if($user['level'] == 3 || $user['level'] == 5)
	{
		mysql_query ("DELETE FROM `chat_posts` WHERE `id` = ".int($_GET['pid'])."");
		echo '<div class="where">Сообщение успешно удалено!</div>';
	}
	else
	{
		echo '<div class="where">У вас недостаточно прав!</div>';
	}
}
if(isset($_POST['msg']) && !empty($_POST['msg']))
{
	$flood = mysql_fetch_assoc(mysql_query("select * from `chat_posts` where `id_user` = ".$user['id']." order by `time` desc limit 1"));
    if($flood['time']<(time()-3))
    {
 		$msg = protect($_POST['msg']);
		$color = array('[color=A9A9A9]'.$msg.'[/color]', '[color=000000]'.$msg.'[/color]', '[color=FFFFFF]'.$msg.'[/color]', '[color=FF0000]'.$msg.'[/color]', '[color=FFFF00]'.$msg.'[/color]','[color=0000FF]'.$msg.'[/color]');
		$emo = array('', 'Радостно: ', 'Грустно: ', 'Злостно: ', 'Восхищенно: ','С призрением: ');
    	mysql_query("INSERT INTO `chat_posts` (rid,id_user,time,msg) values(".$room['id'].",".$user['id'].",".time().",'".$emo[$_POST['emo']].$color[$_POST['color']]."')");
	   	mysql_query("UPDATE `users` SET `chat` = `chat`+1 WHERE `id` = ".$user['id']."");
              mysql_query("update `users` set `balls`=`balls`+1 where `id` = ".$user['id']."");	    
echo '<div class="where">&raquo; Сообщение успешно добавлено!</div>';
	    if($room['umnik'] == 1)
	    {
	    	function umnik($text)
			{
				return preg_replace('/\[(.+)\]/sU', '', $text);
			}
	    	$msg = umnik(protect($_POST['msg']));
	    	if($msg == trim($ini['ans']))
	    	{
	    		mysql_query("UPDATE `chat_ini` SET `value` = '".$user['login']."' WHERE `id` = 'us'");
	    		mysql_query("UPDATE `chat_ini` SET `value` = 1 WHERE `id` = 'pd'");
	    		mysql_query("UPDATE `users` SET `chat_vict` = `chat_vict`+1 WHERE `id` = ".$user['id']."");
	    	}
	    	else
	    	{
	    		echo '<div class="where">Ответ не правильный!</div>';
	    	}
	    }
	}
	else
	{
		echo '<div class="where">&raquo; Антифлуд 3 секунд!</div>';
	}
}
if(isset($_GET['to']) && !empty($_GET['to']))
{
	$to = protect('[b]'.$_GET['to'].'[/b], ');
} else { $to = ''; }
echo '<div class="where">
<a href="who.php">Кто-Где?</a>
| <a href="../pages/info.php?act=bbcode">BBcode</a>
| <a href="../pages/info.php?act=smiles">Смайлы</a> |
<a href="room.php?id='.$room['id'].'&amp;rand='.rand(1,99).'">Обновить</a> | ' . ( $room['umnik'] == 1 ? '<a href="rate_um.php">Рейтинг умных</a>' : '' ) . '</div>	
<div class="mess">
<form action="room.php?id='.$room['id'].'&amp;rand='.rand(1,99).'" method="post"><div>
Ваше сообщение:<br/>
<textarea cols="20" rows="2" name="msg">'.$to.'</textarea><input name="id" type="hidden" value="20"/><br/>
<select size="1" name="color">
<option value="0">Цвет</option>
<option value="1">Чёрный</option>
<option value="2">Белый</option>
<option value="3">Красный</option>
<option value="4">Желтый</option>
<option value="5">Синий</option>
</select><br/>

<input type="submit" value="Сказать"/><br/>
</div></form>
</div>';
$query = mysql_query("select * from `chat_posts` where `rid` = ".$room['id']." order by `id` desc limit ".$page.", ".$set['str']."");
$total = mysql_result(mysql_query("select count(*) from `chat_posts` where `rid` = ".$room['id'].""),0);
if($room['umnik'] == 1)
{
    include('umnik.inc');
}
while ($row = mysql_fetch_assoc($query))
{
	$us = mysql_fetch_assoc(mysql_query("select * from `users` where `id` = ".$row['id_user'].""));
	if ($us['gender'] == 1)
			$pol = '<img class="ico" src="../style/'.$style.'/ico/m.png" alt="" />';
	if ($us['gender'] == 2)
			$pol = '<img class="ico" src="../style/'.$style.'/ico/w.png" alt="" />';
              echo '<div class="forum1"><small>('.clock($row['time']).')</small> ';
              echo $pol.'<a href="room.php?id='.$room['id'].'&amp;to='.$us['login'].'">'.$us['login'].'</a>: '.text($row['msg']).'';
if($user['level'] == 3 || $user['level'] == 5)
{
	echo '<br /><a href="?act=delete&amp;id='.$room['id'].'&amp;pid='.$row['id'].'">[удалить]</a>';
}
echo '</div>';
}
if($total > $set['str'])
{
	pages($page, $total, 'room.php?id='.$room['id'].'&amp;', $set['str']);
}
include '../style/'.$style.'/foot.php';
?>