<?
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();
err();
//======================Начало приватности сообщества===============================//
	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($comm['close']==1 && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($comm['id'])."' AND `id_user` = '$user[id]'"),0)==0){
		echo'<div class="mess">Сообщество закрыто для чтения!</div>';
		echo "<div class='foot'><img src='/style/icons/str2.gif' alt=''/> <a href='/comm/$comm[id]/'>В сообщество</a></div>";
		include_once '../sys/inc/tfoot.php';
		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);
if(isset($user) && $moder!=0 && isset($_GET['act']) && $_GET['act']=='ban'){
	if(isset($_POST['banned']) && isset($_POST['vremja'])){
		$nick = my_esc($_POST['nick']);
		if($nick==NULL)$err[] = "Введите ник";
		if (strlen2($nick)>110){ $err[] = "Ник содержит более 110 символов"; }
		if (strlen2($nick)<1){ $err[] = "Ник содержит менее 1 символов"; }
		$timeq = intval($_POST['time']);
		if($timeq==NULL)$err[] = "Введите время";
		$type = my_esc($_POST['vremja']);
		if(isset($_POST['navsegda']))$navsegda = 1;
		else $navsegda = 0;
		
		if($type=='min'){
			$timeq = $timeq*60;
			$timeq = $timeq+$time;
		}elseif($type=='chas'){
			$timeq = $timeq*3600;
			$timeq = $timeq+$time;
		}else{
			$err[] = "Неверный формат времени";
		}
		if($nick=='Система')$err[] = "Нельзя блокировать систему";
		if(mysql_result(mysql_query("SELECT COUNT(*) FROM `user` WHERE `nick` = '".my_esc($nick)."'"),0)==1){
			$banus = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `nick` = '".my_esc($nick)."'"));	
			if($banus['group_access']>1)$err[] = "Нельзя блокировать администрацию сайта";
			if(mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id` = '".intval($banus['id'])."' AND `level` > '0'"),0)!=0)$err[] = "Нельзя блокировать администрацию сообщества";
			if($banus['id']==$user['id'])$err[] = "Нельзя блокировать себя";
			
		}else{
			$err[] = "Пользователь не найден";
		}
		err();
		if(!isset($err)){
			if(isset($user) && $banus['id']!=$user['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_user` WHERE `id_comm` = '".intval($comm['id'])."' AND `id_user` = '$banus[id]'"),0)!=0){
				mysql_query("DELETE FROM `comm_user` WHERE `id_comm` = '$comm[id]' AND `id_user` = '$banus[id]'");
			}
			if(mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_blacklist` WHERE `id_user` = '$banus[id]' AND `id_comm` = '$comm[id]'"),0)!=0){
				mysql_query("UPDATE `comm_blacklist` SET `time` = '$time', `time_ban` = '$timeq', `id_ban` = '$user[id]', `navsegda` = '$navsegda' WHERE `id_user` = '$banus[id]' AND `id_comm` = '$comm[id]'");
			}else{
			mysql_query("INSERT INTO `comm_blacklist` (`id_user`, `id_comm`, `time`, `time_ban`, `id_ban`, `navsegda`) values('$banus[id]', '$comm[id]', '$time', '$timeq', '$user[id]', '$navsegda')");
			}
			$_SESSION['message'] = 'Пользователь '.$banus['nick'].' успешно забанен';
			header("Location: ?");
		}
		
	}
	echo "<form action=\"?act=ban\" method=\"post\">\n";
	echo "<div class='nav1'>Ник нарушителя:</div>\n";
	echo "<input type='text' name='nick' title='Ник нарушителя' value='' maxlength='110' size='16' />\n";
	echo "<div class='nav1'>Время бана:</div>\n";
echo "<input type='text' name='time' title='Время бана' value='10' maxlength='3' size='3' />\n";
echo "<select class='form' name=\"vremja\">\n";
echo "<option value='min'>Минуты</option>\n";
echo "<option selected='selected' value='chas'>Часы</option>\n";
echo "</select><br />\n";echo "<label><input type='checkbox' name='navsegda' value='1' /> Навсегда</label><br />";
echo "<input name='banned' type='submit' value='Забанить' />\n";
echo "</form>\n";
echo "<div class='foot'><img src='/style/icons/str.gif' alt=''/> <a href='?'>Назад</a></div>";
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(isset($user) && isset($_GET['delete']) && $moder!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_blacklist` WHERE `id_user` = '".intval($_GET['delete'])."'"),0)!=0){
	$idban = intval($_GET['delete']);
	$nickban = get_user($idban);
	mysql_query("DELETE FROM `comm_blacklist` WHERE `id_user` = '$idban'");
	$_SESSION['message'] = 'Пользователь '.$nickban['nick'].' успешно удален из черного списка!';
	header("Location: ?");
	exit;
}
$search=NULL;
$likes=NULL;
//=============================//
if(isset($_GET['search'])){
if (isset($_SESSION['searchu']))$search=$_SESSION['searchu'];
if (isset($_POST['search']))$search=$_POST['search'];
$_SESSION['searchu']=$search;

$search=preg_replace("#( ){2,}#"," ",$search);
$search=preg_replace("#^( ){1,}|( ){1,}$#","",$search);



$search_a=explode(' ', $search);

for($i=0;$i<count($search_a);$i++)
{
$search_a2[$i]='<span class="search_c">'.stripcslashes(htmlspecialchars($search_a[$i])).'</span>';
$search_a[$i]=stripcslashes(htmlspecialchars($search_a[$i]));
}

$q_search=str_replace('%','',$search);
$q_search=str_replace(' ','%',$q_search);
}
//=============================//
if(isset($_GET['search'])){
	$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `user` INNER JOIN `comm_blacklist` ON `user`.`id` = `comm_blacklist`.`id_user` WHERE `comm_blacklist`.`id_comm` = '".intval($ID)."' AND `user`.`nick` like '%$q_search%'"),0);
}else{
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `comm_blacklist` WHERE `id_comm` = '".intval($ID)."'"),0);
}
	echo '<div class="mess">';
	echo "<form method=\"post\" action=\"?search\" class=\"search\">\n";
	$search=stripcslashes(htmlspecialchars($search));
	echo "<input type=\"text\" name=\"search\" maxlength=\"110\" value=\"$search\" /><br />\n";
	echo "<input type=\"submit\" value=\"Поиск\" />\n";
	echo "</form>\n";
	echo '</div>';
$k_page = k_page($k_post,$set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];
if ($k_post == 0)
{	
	echo '<div class="mess" id="no_object">';
	if(isset($_GET['search']))echo 'Ничего не найдено';
	else echo 'Нет нарушителей';
	echo '</div>';
}
if(isset($_GET['search'])){
$q = mysql_query("SELECT * FROM `user` INNER JOIN `comm_blacklist` ON `user`.`id` = `comm_blacklist`.`id_user` WHERE `comm_blacklist`.`id_comm` = '".intval($ID)."' AND `user`.`nick` like '%$q_search%' ORDER BY `user`.`nick` ASC LIMIT $start, $set[p_str]");
}else{
$q = mysql_query("SELECT * FROM `user` INNER JOIN `comm_blacklist` ON `user`.`id` = `comm_blacklist`.`id_user` WHERE `comm_blacklist`.`id_comm` = '".intval($ID)."' ORDER BY `comm_blacklist`.`time` DESC LIMIT $start, $set[p_str]");
}
while ($post = mysql_fetch_assoc($q))
{
	// Лесенка
	echo '<div class="' . ($num % 2 ? "nav1" : "nav2") . '">';
	$num++;
	
	$banuser = get_user($post['id_user']);
	echo group($banuser['id']);
	echo user::nick($banuser['id'],1,1,1);
	echo' забанен ';
	$bancon = mysql_fetch_array(mysql_query("SELECT * FROM `comm_user` WHERE `id_user` = '$post[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($post['navsegda']==1){
		echo' навсегда!';
	}else{
		$timefe = $post['time_ban']-$post['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($post['time']);
		echo '</small>';
	}
	if(isset($user) && $moder!=0){
	echo '<div align="right"><a href="?delete='.$post['id_user'].'"><img src="/style/icons/delete.gif" alt="delete"></a></div>';
	}
	echo '</div>';
}
if ($k_page>1 && !isset($_GET['search']))str("?",$k_page,$page);
elseif ($k_page>1 && isset($_GET['search']))str("?search&",$k_page,$page);

if(isset($user) && $moder!=0 && !isset($_GET['search'])){
	echo "<div class='foot'><img src='/style/icons/str.gif' alt=''/> <a href='?act=ban'>Добавить пользователя</a></div>";
}elseif(isset($_GET['search'])){
	echo "<div class='foot'><img src='/style/icons/str.gif' alt=''/> <a href='?'>Назад</a></div>";
}
echo "<div class='foot'><img src='/style/icons/str2.gif' alt=''/> <a href='/comm/$ID/'>В сообщество</a></div>";
}else{
	header("Location: /");
}
include_once '../sys/inc/tfoot.php';
?>