<?php

include_once '../sys/inc/start.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';

if(isset($_GET['id'])){
	$id = intval($_GET['id']);
	}else{
		header("Location: /index.php");
		}

$admin = mysql_fetch_array(mysql_query("SELECT * FROM `community_user_incomm` WHERE `cid` = '$id' AND `uid` = '".$user['id']."'"));
$set['title'] = 'Мини чат '.mysql_result(mysql_query("SELECT `name` FROM `community_comm` WHERE `id` = '".intval($_GET['id'])."' LIMIT 1"),0); // заголовок страницы
include_once '../sys/inc/thead.php';

title();
aut();

echo '<div class="menu">';

$status_comm = mysql_fetch_array(mysql_query("SELECT * FROM `community_comm` WHERE `id` = '$id' LIMIT 1"));
$activate = mysql_fetch_array(mysql_query("SELECT * FROM `community_user_incomm` WHERE `cid` = '$id' AND `uid` = '".$user['id']."'"));
$comm = mysql_fetch_array(mysql_query('SELECT * FROM `community_comm` WHERE `id` = '.$id.' LIMIT 1'));
$post = mysql_fetch_array(mysql_query("SELECT * FROM `community_board` WHERE `id_comm` = '$id' AND `user` = '".$user['id']."' ORDER BY time DESC LIMIT 1"));

if($id==0 || $id<0){
	echo '<div class="err">Иди нахуй! Хакер недоношеный!</div>';
	}else if($id!=$comm['id']){
		echo '<div class="err">Сообщество не найдено.</div>';
		}else if($status_comm['status']==2 && $activate['activate']==0){
			echo '<div class="err">Сообщество закрытого типа.</div>';
			}else if(mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_ban` WHERE `id_user` = '$user[id]' AND `id_comm` = '$id' AND `time` > '$time'"), 0)!=0){
				header('Location: ban.php?id='.$id);
				}else{

if($_GET['err']=='min_mess'){
	msg('Сообщение слишком маленькое.');
	}else if($_GET['err']=='max_mess'){
		msg('Сообщение слишком длинное.');
		}else if($_GET['err']=='msg_time'){
			msg('Не стоит писать слишком часто.');
			}else if($_GET['err']=='no'){
				msg('Сообщение успешно добавлено.');
				}else if($_GET['err']=='del'){
					msg('Сообщение успешно удалено');
					}else if($_GET['err']=='no_mess'){
						msg('Сообщение не найдено.');
						}
if(isset($user)){
	include 'inc/guest_act.php';
	}

echo '<table class="post">';

$k_m=mysql_result(mysql_query("SELECT COUNT(*) FROM `community_board` WHERE `id_comm` = '$id'"), 0);
$k_page=k_page($k_m,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];

if($k_m=='0'){
	echo '<tr><td class="p_t">Нет сообщений. Будь первым.</td></tr>';
	}

$q = mysql_query("SELECT * FROM `community_board` WHERE `id_comm` = '$id' ORDER BY time DESC LIMIT $start, $set[p_str]");
while($p = mysql_fetch_array($q)){
	$a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = $p[user] LIMIT 1"));
	echo '<tr><td class="icon14">';
	echo avatar($a['id']);
	echo '</td><td class="p_m">';

echo ' <a href="/info.php?id='.$a['id'].'"><span style="color:'.$a['ncolor'].'">'.$a['nick'].'</span></a>'.online($p['user']).' ('.vremja($p['time']).')<br/>';
echo output_text($p['msg']);

if($admin['priv']==2 || $admin['priv']==1 || $user['level']>2){
	echo '<br/>&#187;&nbsp;<a href="guest.php?id='.$id.'&amp;del='.$p['id'].'">Удалить</a><br/>';
	}

echo '</td></tr>';
}

echo '</table>';

if($k_page>1){
	str("guest.php?id=$id&amp;",$k_page,$page);
	}

if($activate['uid']==$user['id']){
	include 'inc/guest_form.php';
	}

echo '&#187;&nbsp;<a href="comm.php?id='.$id.'">В сообщество</a><br/>';
}

echo '</div>';
include_once '../sys/inc/tfoot.php';
?>
