<?php
include_once("incs/conf.php");
include_once("incs/fmysql.php");
include_once("incs/fnc.php");
include_once("incs/head.php");

$a = fil($_GET['a']);
$pos = fil($_GET['p']);
$id_razd = fil($_GET['id_razd']);

if($settings[chat]==1){
head("Чат",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "Чат</div>";
echo "Раздел закрыт<br>";
include_once("incs/down.php");
}

if($on==1){
$gde="chat";
mysql_query("UPDATE `user` SET `time_gde` = '$time', `gde` = '$gde' WHERE `id` = '$user[id]'");
}
$gde_user = mysql_num_rows(mysql_query("SELECT `id` FROM `user` WHERE `gde` = '$gde' AND `time_gde` > '".($time-180)."'"));

$ban_ok=$user[ban]-$time;
if($ban_ok>0){
head("Чат",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "Чат</div>";
echo "Вы забанены по причине:<br>";
echo "<b>$user[ban_prich]</b><br>";
echo "До окончания бана осталось: $ban_ok сек.<br>";
echo "Старайтесь больше не нарушать <a href=\"rules.php\">правила</a> сайта!<br>";
include_once("incs/down.php");
}

if($id_razd!=""){
$chat_razd=mysql_fetch_array(mysql_query("SELECT * FROM `chat_razd` WHERE `id` = '$id_razd'"));
if($chat_razd[name]==""){
head("Ошибка",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Чат</a> - Ошибка</div>";
echo"Такого раздела не существует!<br>";
include_once("incs/down.php");
}
}

head("Чат",$meuser,$auth);

switch($a){

case 'moder':
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Чат</a> - Модераторы</div>";

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `user` WHERE `level` = '2' OR `level` = '5' OR `level` = '6' OR `level` = '8'"));
if ($k_post==0)
{
echo "Нет модераторов<br>";
}

$mode=mysql_query("SELECT * FROM `user` WHERE `level` = '2' OR `level` = '5' OR `level` = '6' OR `level` = '8' ORDER BY date_last DESC ");
while ($moder = mysql_fetch_array($mode))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
if($moder[date_last]>($time-600)){
echo "<img src=\"styles/img/online.gif\" alt=\"image\"/> ";
}else{
echo "<img src=\"styles/img/offline.gif\" alt=\"image\"/> ";}
echo "<a href='my.php?id=$moder[id]'>$moder[nick]</a></div>";
}
include_once("incs/down.php");
break;

case 'new':
$y=1;
$vop=mysql_query("SELECT * FROM `chat_razd` WHERE `id` != '0' ORDER BY id DESC LIMIT $y");
while ($p_r = mysql_fetch_array($vop))
{
$pov_razd = $p_r[name];
}
if($pov_razd!=$_POST['name']){
if($_POST['name']!=""){
$name=$_POST['name'];
$name=filtr($name);
$nom=$_POST['nom'];
$nom=intval($nom);
if($nom!=""){
$nom=$nom;
}else{
$nom=0;
}
mysql_query("INSERT INTO `chat_razd` (`name`, `nom`) values('$name', '$nom')");
}else{
echo "Вы не ввели название раздела!<br>";
}
}else{
echo "Такой раздел уже создан!<br>";
}
break;

case 'adm':
if($user[level]>=2){
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Чат</a> - Админка</div>";
echo '<form method="post" action="?a=new">';
echo 'Название раздела:<br/><input type="text" name="name" maxlength="32"/><br>';
echo 'Место:<br/><input type="text" name="nom" maxlength="32"/><br>';
echo '<input value="Создать" type="submit"/></form>';
include_once("incs/down.php");
}
break;

case 'del_razd':
if($user[level]>=2 && $id_razd!=""){
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Чат</a> - Удаление раздела</div>";
mysql_query("DELETE FROM `chat_razd` WHERE `id` = '$id_razd'");
$del_post=mysql_query("SELECT * FROM `chat_post` WHERE `id_razd` = '$id_razd'");
while ($del_post_r = mysql_fetch_array($del_post))
{
mysql_query("DELETE FROM `chat_post` WHERE `id` = '$del_post_r[id]'");
}
echo "Раздел успешно удален!<br>";
include_once("incs/down.php");
}
break;
}

if($id_razd!=""){
if($a=="del" && $pos!=0 && $user[level]>1){
mysql_query("DELETE FROM `chat_post` WHERE `id` = '$pos'");
}

$name_r=mysql_fetch_array(mysql_query("SELECT * FROM `chat_razd` WHERE `id` = '$id_razd'"));
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Чат</a> - $name_r[name] - [<a href=\"?id_razd=$id_razd\">Обновить</a>]</div>";

if($a==new_p){
if($on==1){
$y=1;
$vop=mysql_query("SELECT * FROM `chat_post` WHERE `id` != '0' ORDER BY id DESC LIMIT $y");
while ($p_m = mysql_fetch_array($vop))
{
$pov_msg = $p_m[msg];
}
if($pov_msg!=$_POST['msg_k']){
if($_POST['msg_k']!=""){
$msg_k=$_POST['msg_k'];
$msg_k=filtr($msg_k);
$msg_k=smiles($msg_k);
mysql_query("INSERT INTO `chat_post` (`id_razd`, `id_user`, `msg`, `date`) values('$id_razd', '$user[id]', '$msg_k', '".date('H:i:s')."')");
mysql_query("UPDATE `user` SET `money` = '".($user['money']+1)."' WHERE `id` = '$user[id]'");
}else{
echo "Сообщение пустое!<br>";
}
}else{
echo "Сообщение повторяетса!<br>";
}
}
}
if($on==1){
echo "<form method=\"post\" action=\"?&id_razd=$id_razd&a=new_p\">";
echo 'Сообщение:<br/><textarea name="msg_k"></textarea><br>';
echo '<input value="Написать" type="submit"/></form>';
}

$k_postt = mysql_num_rows(mysql_query("SELECT `id` FROM `chat_post` WHERE `id_razd` = '$id_razd'"));
if($k_postt==0)
{
echo "Нет сообщений<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_postt/$end);
if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$t=mysql_query("SELECT * FROM `chat_post` WHERE `id_razd` = '$id_razd' ORDER BY id DESC LIMIT $start,$end");
while ($posts = mysql_fetch_array($t))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$v=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$posts[id_user]'"));
echo "<a href=\"my.php?id=$posts[id_user]\">$v[nick]</a> ($posts[date])";
echo "<br>".str_replace("\n","<br>",$posts[msg])."";
if($user[level]>1){
echo "<br><a href=\"chat.php?id_razd=$id_razd&a=del&p=$posts[id]\">Удалить</a><br>";
}
echo "</div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?id_razd=$id_razd&page=$i'>$i</a></span>";
}}}

echo "<br><a href='smiles.php'>Смайлы</a><br>";

if($user[level]==2 OR $user[level]==5 OR $user[level]==6 OR $user[level]==8 OR $user[level]>=9){
echo "<div class='menu'><a href=\"?a=del_razd&id_razd=$id_razd\">Удалить раздел</a></div>";
}
include_once("incs/down.php");
}
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "Чат</div>";

if($user[level]==2 OR $user[level]==5 OR $user[level]==6 OR $user[level]==8 OR $user[level]>=9){
switch($pos){
case'cline':
echo "Вы действительно хотите очистить чат?<br>";
echo "<a href=\"?p=cline_yes\">Да, очистить</a> - ";
echo "<a href=\"?\">Нет, вернутса</a><br>";
break;

case'cline_yes':
mysql_query("TRUNCATE TABLE `chat_post`");
break;
}
}

$k_post = mysql_num_rows(mysql_query("SELECT `id` FROM `chat_razd`"));
if($k_post==0)
{
echo "Нет разделов<br>";
}

$u=0;
$q=mysql_query("SELECT * FROM `chat_razd` WHERE `id` != '$u' ORDER BY nom ASC");
while ($razds = mysql_fetch_array($q))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
echo "<img src=\"styles/img/ch.png\" alt=\"image\"/>";
$anketdata=mysql_fetch_array(mysql_query("SELECT id, level FROM user WHERE id = '$postt[id]'"));
echo "<a href=\"?id_razd=$razds[id]\">$razds[name]</a></div>";
}
echo "<br>";

echo "<div class=\"menu\"><a href=\"?a=moder\">Модераторы</a></div>";

if($user[level]>=2){
echo "<div class=\"menu\"><a href=\"?a=adm\">Настройки чата</a></div>";
}
if($user[level]==2 OR $user[level]==5 OR $user[level]==6 OR $user[level]==8 OR $user[level]>=9){
echo "<div class=\"menu\"><a href=\"?p=cline\">Очистить чат</a></div>";
}
include_once("incs/down.php");
?>