<?
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.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';
$ID = intval($_GET['id']);
if (isset($_GET['id']) && $_GET['id']!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm` WHERE `id` = '".intval($_GET['id'])."'"),0)!=0){	
$comm = get_comm($ID);
$set['title']='Сообщество - '.text($comm['name']).''; // заголовок страницы
include_once '../sys/inc/thead.php';
title();
aut();
if(isset($user) && $comm['id_creater']!=$user['id'] && isset($_GET['act']) && $_GET['act']=='out' && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)==1){
	mysql_query("DELETE FROM `comm_user` WHERE `id_comm` = '$ID' AND `id_user` = '$user[id]'");
	$_SESSION['message'] = 'Вы успешно покинули сообщество '.$comm['name'].'';
	header("Location: /comm/$ID/");
	exit;
}
if(isset($user) && $comm['id_creater']!=$user['id'] && $comm['vstup']==1 && isset($_GET['act']) && $_GET['act']=='invite' && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)==0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_invite` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)!=0){
	mysql_query("DELETE FROM `comm_invite` WHERE `id_comm` = '$ID' AND `id_user` = '$user[id]'");
	mysql_query("INSERT INTO `comm_user` (`id_user`, `id_comm`, `level`) values('$user[id]', '$ID', '0')");
	$_SESSION['message'] = 'Вы успешно вступили в сообщество '.$comm['name'].'';
	header("Location: /comm/$ID/");
	exit;
}


if(isset($user) && $comm['id_creater']!=$user['id'] && $comm['vstup']==0 && isset($_GET['act']) && $_GET['act']=='in' && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)==0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_blacklist` WHERE `id_user` = '$user[id]' AND `id_comm` = '$comm[id]' && (`time_ban` > '$time' OR `navsegda` = '1')"),0)==0){
	mysql_query("INSERT INTO `comm_user` (`id_user`, `id_comm`, `level`) values('$user[id]', '$ID', '0')");
	$_SESSION['message'] = 'Вы успешно вступили в сообщество '.$comm['name'].'';
	header("Location: /comm/$ID/");
	exit;
}
$moder = mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_user` = '".intval($user['id'])."' AND `id_comm` = '$ID' AND `level` > '0'"),0);
$admin = mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_user` = '".intval($user['id'])."' AND `id_comm` = '$ID' AND `level` > '1'"),0);
$sozdatel = mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_user` = '".intval($user['id'])."' AND `id_comm` = '$ID' AND `level` > '2'"),0);
if (isset($user) && isset($_GET['deletecomm']) && isset($_POST['delete']) &&($sozdatel!=0 || user_access('comm_edit'))){
	$_SESSION['categorycomm'] = $comm['id_category'];
	mysql_query("DELETE FROM `comm_blacklist` WHERE `id_comm` = '$comm[id]'");
	mysql_query("DELETE FROM `comm_user` WHERE `id_comm` = '$comm[id]'");
	mysql_query("DELETE FROM `comm_news` WHERE `id_comm` = '$comm[id]'");
	mysql_query("DELETE FROM `comm_forum_razdel` WHERE `id_comm` = '$comm[id]'");
	mysql_query("DELETE FROM `comm_forum_theme` WHERE `id_comm` = '$comm[id]'");
	mysql_query("DELETE FROM `comm_fav` WHERE `id_comm` = '$comm[id]'");
	mysql_query("DELETE FROM `comm_obmen` WHERE `id_comm` = '$comm[id]'");
	mysql_query("DELETE FROM `comm` WHERE `id` = '$comm[id]'");
	$_SESSION['message'] = 'Сообщество '.$comm['name'].' успешно удалено!';
	if(isset($_SESSION['categorycomm']))$ct = $_SESSION['categorycomm'];
	
	header("Location: /comm/cat.php?id=$ct");
	exit;
}
if(isset($user) && user_access('comm_edit') && isset($_GET['act']) && $_GET['act']=='delete'){
		echo'<form class="foot" action="/comm/'.$comm['id'].'/?deletecomm" method="post">';
		echo "Вы действительно хотите удалить сообщество ".text($comm['name'])."?<br />\n";
		echo "<input name=\"delete\" value=\"Удалить\" type=\"submit\" /><br />\n";
		echo "</form>\n";
	
		echo "<div class='foot'><img src='/style/icons/str2.gif' alt=''/> <a href='?'>Назад</a></div>";
		include_once '../sys/inc/tfoot.php';
		exit;
		
}
if ($moder!=0 && isset($user) && isset($_GET['edit']) && $_GET['edit']=='status'){
	if (isset($_POST['deviz']) && isset($_POST['save']))
	{
	$deviz = my_esc($_POST['deviz']);
	if (strlen2($deviz)>100){ $err[] = "Девиз сообщества содержит более 100 символов"; }
	if (strlen2($deviz)<3){ $err[] = "Девиз сообщества содержит менее 3 символов"; }
	$mat = antimat($deviz);
	if ($mat)$err[] = "В девизе сообщества обнаружен мат: $mat";
	err();
	if (!isset($err)){
	mysql_query("UPDATE `comm` SET `status` = '$deviz' WHERE `id` = '$ID' LIMIT 1");
	$_SESSION['message'] = 'Девиз успешно изменен';
	header("Location: /comm/$ID/");
	exit;
	}
	}
	echo "<form method=\"post\" name='save_deviz' action=\"?edit=status\">";
	echo "Девиз:$tPanel<textarea name=\"deviz\">".text($comm['status'])."</textarea><br />\n";
	echo "<input name=\"save\" value=\"Сохранить\" type=\"submit\" /><br />\n";
	echo "</form>\n";
	echo "<div class='foot'><img src='/style/icons/str2.gif' alt=''/> <a href='/comm/$ID/'>Назад</a></div>";
	include_once '../sys/inc/tfoot.php';
	exit;
}
if($comm['close']==0)echo'<div class="err">Открыто для чтения</div>';
elseif($comm['close']==1)echo'<div class="err">Закрыто для чтения</div>';
$ret = $comm['rating'];
$aqr = $comm['rating']/100;
$aqr = floor($aqr);
if($comm['rating']>100)$ret = $comm['rating']-(100*$aqr);
echo "<div class='nav2'>";
echo'<div style="display: inline-block; height: 14px; width: 140px; border: 1px solid DodgerBlue; position: relative; background-color: White;">
	 <div style="height: 14px; width: 329%; background: LightGray; position: absolute; max-width: 100%;"></div>
	 <div style="height: 14px; width: '.$ret.'%; background: #32CD32; position: absolute;  max-width: 100%;"></div>
	 <span style="position: absolute; text-align: center; width: 100%; display: block; font-size: 11px;">'.$comm['rating'].'%</span>
	</div>

	<br/>';
	echo avatar_comm($comm['id'],128);
	echo "</div><div class='st_2'>";
	echo output_text($comm['status']);
	if ($moder!=0 && isset($user))echo " [<a href='?edit=status'><img src='/style/icons/edit.gif' alt='*'> ред</a>]";
	echo"</div>";
	//=====================================================//
	if(isset($user) && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_blacklist` WHERE `id_user` = '$user[id]' AND `id_comm` = '$comm[id]' && (`time_ban` > '$time' OR `navsegda` = '1')"),0)!=0){
	echo'<div class="mess">Вы были забанены ';
	$banned = mysql_fetch_array(mysql_query("SELECT * FROM `comm_blacklist` WHERE `id_user` = '$user[id]' AND `id_comm` = '$comm[id]'"));
	$bancon = mysql_fetch_array(mysql_query("SELECT * FROM `comm_user` WHERE `id_user` = '$banned[id_ban]' AND `id_comm` = '$comm[id]'"));
	if($bancon['level']==1){
		echo 'модератором ';
	}elseif($bancon['level']==2){
		echo 'администратором ';
	}elseif($bancon['level']==3){
		echo 'создателем ';
	}
	echo group($bancon['id_user']);
	echo user::nick($bancon['id_user'],1,1,1);
	if($banned['navsegda']==1){
		echo' навсегда!';
	}else{
		$timefe = $banned['time_ban']-$banned['time'];
		echo ' на ';
		if($timefe>3600){
			$timef = $timefe/3600;
			$timefe = floor($timef);
			
			$timem = $timef-$timefe;
			if($timem==0){
			echo''.$timefe.' часов!';
			}else{
				$timem = round($timem*60);
				echo''.$timefe.' часов '.$timem.' минут!';
			}
		}elseif($timefe<3600){
			$timef = $timefe/60;
			$timefe = round($timef);
			echo''.$timefe.' минут!';
		}
		echo ' <small>';
		echo vremja($banned['time']);
		echo '</small>';
	}
		echo "</div><div class='foot'><img src='/style/icons/str2.gif' alt=''/> <a href='/comm/cat.php?id=$comm[id_category]'>Назад в категорию</a></div>"; 
		include_once '../sys/inc/tfoot.php';
		exit;
	}
	//===========================================//
	if (isset($_GET['fav']) && isset($user)){

	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_fav` WHERE `id_user` = '" . $user['id'] . "' AND `id_comm` = '" . $comm['id'] . "' LIMIT 1"),0) == 0 && $_GET['fav'] == 1){
		mysql_query("INSERT INTO `comm_fav` (`id_comm`, `id_user`, `time`) VALUES ('$comm[id]', '$user[id]', '$time')");
		$_SESSION['message'] = 'Сообщество ' . text($comm['name']) . ' добавлено в закладки';
	}

	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_fav` WHERE `id_user` = '" . $user['id'] . "' AND `id_comm` = '" . $comm['id'] . "' LIMIT 1"),0) == 1 && $_GET['fav'] == 0){
		mysql_query("DELETE FROM `comm_fav` WHERE `id_user` = '$user[id]' AND  `id_comm` = '$comm[id]'");
		$_SESSION['message'] = 'Сообщество ' . text($comm['name']) . ' удалено из закладок';
	}
	
		header("Location: ?");
		exit;
}
	//===========================================//
	$news=mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_news` WHERE `id_comm` = '$ID'",$db), 0);
	$k_news= mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_news` WHERE `id_comm` = '$ID' AND `time` > '".$ftime."'",$db), 0);
if ($k_news==0)$k_news=NULL;
else $k_news='+'.$k_news;
	echo "<div class='nav2'>";
	echo "<img src='/style/icons/news.png' alt='news'/> <a href='/comm/news/$ID/'>Новости</a> ($news) <font color='red'>$k_news</font>";
	echo"</div>";
	echo "<div class='nav1'>";
	echo "<img src='/style/icons/forum.png' alt='forum'/> <a href='/comm/forum/$ID/'>Форум</a>";
	$them = mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_forum_theme` WHERE `time`>'$ftime' AND `id_comm` = '$comm[id]'"),0);
if ($them==0)$them=NULL;
else $them='+'.$them;
echo ' ('.mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_forum_theme` WHERE `id_comm` = '$comm[id]'"),0).') <font color="red">'.$them.'</font>';
	echo"</div>";
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_obmen` WHERE `id_comm` = '".intval($ID)."' AND `osn` = '1'"),0)==0){	
		mysql_query("INSERT INTO `comm_obmen` (`id_dir`, `id_comm`, `time`, `osn`, `name`, `id_dires`) values('0', '$ID', '$time', '1', 'Файлы', '/')");
		header("Location: ?");
		//===============================//
	}else{
		$obmen = mysql_fetch_array(mysql_query("SELECT * FROM `comm_obmen` WHERE `id_comm` = '$ID' AND `type` = '0' AND `osn` = '1'"));
		$dir_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_obmen` WHERE `id_comm` = '$comm[id]' AND `type` = '0' AND `osn` = '0'"),0);
	$file_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_obmen` WHERE `id_comm` = '$comm[id]' AND `type` = '1'"),0);
	$new_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_obmen` WHERE `id_comm` = '$comm[id]' AND `type` = '1' AND `time` > '$ftime'"),0);
	if($new_post==0)$new_post=NULL;
	else $new_post='+'.$new_post;
	echo "<div class='nav2'>";
	echo "<img src='/style/icons/loads.png' alt='files'/> <a href='/comm/obmen/$ID/$obmen[id]/'>Файлы</a>";
	echo' ('.$dir_post.'/'.$file_post.') <font color="red">'.$new_post.'</font>';	
	echo"</div>";
	}
	$c_user = mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."'"),0);
	echo "<div class='nav1'>";
	echo "<img src='/style/icons/druzya.png' alt='users'/> <a href='/comm/users/$ID/'>Участники</a> ($c_user)";
	echo"</div>";
	$b_user = mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_blacklist` WHERE `id_comm` = '".intval($comm['id'])."'"),0);
	echo "<div class='nav2'>";
	echo "<img src='/style/icons/guests.gif' alt='ban_users'/> <a href='/comm/ban_users/$ID/'>Нарушители</a> ($b_user)";
	echo"</div>";
	echo "<div class='nav1'>";
	echo "<img src='/style/icons/info.gif' alt='info'/> <a href='/comm/info/$ID/'>Информация</a>";
	echo"</div>";
	if(isset($user) && $comm['vstup']=='1' && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)!=0){
	echo "<div class='nav1'>";
	echo "<img src='/style/my_menu/who_rating.png' alt='invite'/> <a href='/comm/invite/$ID/'>Пригласить в сообщество</a>";
	echo"</div>";
	}
	
	if(isset($user) && $comm['vstup']==0){
	if(isset($user) && $comm['id_creater']!=$user['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)==1)echo "<div class='nav2'><img src='/style/icons/comm_out.png' alt='out_users'/> <a href='?act=out'>Покинуть сообщество</a></div>";
	elseif(isset($user) && $comm['id_creater']!=$user['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)==0)echo "<div class='nav2'><img src='/style/icons/comm_in.png' alt='in_users'/> <a href='?act=in'>Вступить в сообщество</a></div>";
	}elseif(isset($user) && $comm['vstup']==1){
		if(isset($user) && $comm['id_creater']!=$user['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)==1)echo "<div class='nav2'><img src='/style/icons/comm_out.png' alt='out_users'/> <a href='?act=out'>Покинуть сообщество</a></div>";
		elseif(isset($user) && $comm['id_creater']!=$user['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_invite` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($ID)."' AND `id_user` = '$user[id]'"),0)==0)echo "<div class='nav2'><img src='/style/icons/comm_in.png' alt='in_users'/> <a href='?act=invite'>Принять приглашение</a></div>";
	}
	if(isset($user) && $admin!=0){
		echo "<div class='nav1'>";
		echo "<img src='/style/icons/settings.png' alt='info'/> <a href='/comm/settings/$ID/'>Настройки</a>";
		echo"</div>";
	}
	//=========================//
		if(isset($user)){
		$markinfo=mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_fav` WHERE `id_comm` = '" . $comm['id'] . "'"),0);

echo "<div class='nav2'>";
echo "<img src='/style/icons/fav.gif' alt='*' /> ";
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_fav` WHERE `id_user` = '" . $user['id'] . "' AND `id_comm` = '".$comm['id']."' LIMIT 1"),0) == 0)
echo "<a href='?fav=1'>Добавить в закладки</a> <br />\n";
else
echo "<a href='?fav=0'>Удалить из закладок</a> <br />\n";
//echo "В закладках у <a href='?markinfo'>$markinfo</a> чел.";
echo "</div>";
	}
	//==========================//
	if (isset($user) && user_access('comm_edit')){
			echo "<div class='foot'><img src='/style/icons/str.gif' alt=''/> <a href='?act=delete'>Удалить сообщество</a></div>";
	}
	$cat = mysql_fetch_array(mysql_query("SELECT `id`,`name` FROM `comm_category` WHERE `id` = '$comm[id_category]'"));
	echo "<div class='foot'><img src='/style/icons/str2.gif' alt=''/> <a href='/comm/cat.php?id=$cat[id]'>$cat[name]</a> | <b>$comm[name]</b></div>";
	
}else{
	header("Location: /");
}
include_once '../sys/inc/tfoot.php';
?>