<?

require "include/bittorrent.php";

dbconn ( false );

validcan ( false, "can_banip|can_ban" );
global $CURGROUP, $ALLGROUPS, $CURUSER;
$remove = ( int ) $_GET ['remove'];
if (is_valid_id ( $remove )) {
	$res = sql_query ( "SELECT first, last, uid, email FROM bans WHERE id=$remove" ) or sqlerr ( __FILE__, __LINE__ );
	$row = mysql_fetch_array ( $res );
	if ($row ["first"])
		$first = long2ip ( $row ["first"] );
	if ($row ["last"])
		$last = long2ip ( $row ["last"] );
	if ($row ["uid"])
		$uid = $row ["uid"];
	if ($row ["email"])
		$email = $row ["email"];
	sql_query ( "DELETE FROM bans WHERE id=$remove" ) or sqlerr ( __FILE__, __LINE__ );
	if (($first || $last) && $CURGROUP ['can_banip'])
		write_log ( "Бан IP адреса номер $remove (" . ($first == $last ? $fisrt : "адреса с $first по $last") . ") был убран пользователем $CURUSER[username]." );
	if ($uid && $CURGROUP ['can_ban']) {
		sql_query ( "UPDATE users SET enabled='yes' WHERE id=$uid LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
		write_log ( "Бан номер $remove пользователя с ID = $uid был убран пользователем $CURUSER[username].", '', 'bans' );
	}
	if ($email && $CURGROUP ['can_admin'])
		write_log ( "Бан номер $remove E-Mail адреса $email был убран пользователем $CURUSER[username]." );
	if ($_GET ['location'])
		location ( 'banuser.php?viewlog=1' );
}

if ($_SERVER ["REQUEST_METHOD"] == "POST") {
	$first = trim ( $_POST ["first"] );
	$last = trim ( $_POST ["last"] );
	$uid = intval ( trim ( $_POST ["uid"] ) );
	$y = intval ( $_POST ["tyear"] );
	$m = intval ( $_POST ["tmonth"] );
	$d = intval ( $_POST ["tday"] );
	$h = intval ( $_POST ["thour"] );
	$mn = intval ( $_POST ["tminut"] );
	$s = intval ( $_POST ["tsecond"] );
	$never = $_POST ['never'];
	if ((! $y || ! $m || ! $d || $h < 0 || $mn < 0 || $s < 0) && $uid && ! $never)
		stderr ( $tracker_lang ['error'], $tracker_lang ['missing_form_data'] );
	$totime = $y . "-" . $m . "-" . $d . " " . $h . ":" . $mn . ":" . $s;
	if ($never)
		$totime = "0000-00-00 00:00:00";
	$email = trim ( $_POST ["email"] );
	$comment = trim ( $_POST ["comment"] );
	$commentuser = trim ( $_POST ["commentuser"] );
	$commentemail = trim ( $_POST ["commentemail"] );
	if ((! $first || ! $last || ! $comment) && (! $uid || ! $totime || ! $commentuser) && (! $email || ! $commentemail))
		stderr ( $tracker_lang ['error'], $tracker_lang ['missing_form_data'] );
	$first = ip2long ( $first );
	$last = ip2long ( $last );
	if (($first == - 1 || $last == - 1) && ! $uid && ! $totime && ! $email)
		stderr ( $tracker_lang ['error'], $tracker_lang ['invalid_ip'] );
	$comment = sqlesc ( htmlspecialchars_uni ( $comment ) );
	$commentuser = sqlesc ( htmlspecialchars_uni ( $commentuser ) );
	$commentemail = sqlesc ( htmlspecialchars_uni ( $commentemail ) );
	$added = sqlesc ( get_date_time () );
	if (($first || $last) && $CURGROUP ['can_banip']) {
		sql_query ( "INSERT INTO bans (added, addedby, first, last, comment) VALUES($added, $CURUSER[id], " . sqlesc ( $first ) . ", " . sqlesc ( $last ) . ", $comment)" ) or sqlerr ( __FILE__, __LINE__ );
		write_log ( "IP адреса от " . long2ip ( $first ) . " до " . long2ip ( $last ) . " были забанены пользователем $CURUSER[username]." );
	}
	if ($uid && $totime && $CURGROUP ['can_ban']) {
		$res = sql_query ( "SELECT `group` FROM users WHERE id=$uid LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
		$row = mysql_fetch_array ( $res );
		$sum1 = summgroup ( $ALLGROUPS [$row ['group']] );
		$sum2 = summgroup ( $CURGROUP );
		if ($ALLGROUPS [$row ['group']] ['can_bebanned'] && $sum2 > $sum1) {
			sql_query ( "INSERT INTO bans (added, addedby, uid, totime, comment) VALUES($added, $CURUSER[id], $uid, " . sqlesc ( $totime ) . ", $commentuser)" ) or sqlerr ( __FILE__, __LINE__ );
			sql_query ( "UPDATE users SET enabled='no' WHERE id=$uid LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
			write_log ( "Пользователь ID $uid был забанен пользователем $CURUSER[username] по $totime.", "", "bans" );
		}
	}
	if ($email && $CURGROUP ['can_admin']) {
		sql_query ( "INSERT INTO bans (added, addedby, email, comment) VALUES($added, $CURUSER[id], " . sqlesc ( $email ) . ", $commentemail)" ) or sqlerr ( __FILE__, __LINE__ );
		write_log ( "E-Mail $email был забанен пользователем $CURUSER[username]." );
	}
	location ( "$DEFAULTBASEURL/bans.php" );
	die ();
}

gzip ();

$res = sql_query ( "SELECT b.*, u.username, u2.username AS banned FROM bans AS b LEFT JOIN users AS u ON b.addedby=u.id LEFT JOIN users AS u2 ON b.uid=u2.id ORDER BY b.added DESC" ) or sqlerr ( __FILE__, __LINE__ );

stdhead ( $tracker_lang ['bans'] );

if (mysql_num_rows ( $res ) == 0)
	print ( "<p align=\"center\"><b>" . $tracker_lang ['nothing_found'] . "</b></p>\n" );
else {
	//print("<table border=1 cellspacing=0 cellpadding=5>\n");
	$new_arr = array ();
	$be = array ();
	while ( $arr = mysql_fetch_assoc ( $res ) ) {
		$new_arr [] = $arr;
		if ($arr ['first'] || $arr ['last'])
			$be ['ip'] = 1;
		if ($arr ['email'])
			$be ['email'] = 1;
		if ($arr ['uid'])
			$be ['user'] = 1;
	}
	if ($CURGROUP ['can_banip'] && $be ['ip']) {
		print ( "<center><strong>Забаненые IP</strong></center>\n" );
		begin_table ();
		print ( "<tr><td class=\"colhead\">Добавлен</td><td class=\"colhead\" align=\"left\">Первый IP</td><td class=\"colhead\" align=\"left\">Последний IP</td>" . "<td class=\"colhead\" align=\"left\">Кем</td><td class=\"colhead\" align=\"left\">Комментарий</td><td class=\"colhead\">Снять бан</td></tr>\n" );
		foreach ( $new_arr as $arr ) {
			if ($arr ["first"] || $arr ["last"]) {
				$arr ["first"] = long2ip ( $arr ["first"] );
				$arr ["last"] = long2ip ( $arr ["last"] );
				print ( "<tr><td class=\"row1\">$arr[added]</td><td class=\"row1\" align=\"left\">$arr[first]</td><td  class=\"row1\" align=\"left\">$arr[last]</td><td  class=\"row1\" align=\"left\">" . ($CURGROUP ['can_viewprof'] ? "<a href=\"userdetails.php?id=$arr[addedby]\">$arr[username]</a>" : $arr [username]) . "</td><td  class=\"row1\" align=\"left\">" . $arr ["comment"] . "</td><td  class=\"row1\"><a href=\"bans.php?remove=$arr[id]\">Снять бан</a></td></tr>\n" );
			}
		}
		end_table ();
	}
	if ($CURGROUP ['can_ban'] && $be ['user']) {
		print ( "<center><strong>Забаненые пользователи</strong></center>\n" );
		begin_table ();
		print ( "<tr><td class=\"colhead\">Добавлен</td><td class=\"colhead\" align=\"left\">Пользователь</td><td class=\"colhead\" align=\"left\">До</td>" . "<td class=\"colhead\" align=\"left\">Кем</td><td class=\"colhead\" align=\"left\">Комментарий</td><td class=\"colhead\">Снять бан</td></tr>\n" );
		foreach ( $new_arr as $arr ) {
			if ($arr ["uid"])
				print ( "<tr><td class=\"row1\">$arr[added]</td><td class=\"row1\" align=\"left\">" . ($CURGROUP ['can_viewprof'] ? "<a href=\"userdetails.php?id=$arr[uid]\">$arr[banned]</a>" : $arr [banned]) . "</td><td  class=\"row1\" align=\"left\">" . ($arr [totime] != "0000-00-00 00:00:00" ? $arr [totime] : "Никогда") . "</td><td  class=\"row1\" align=\"left\">" . ($CURGROUP ['can_viewprof'] ? "<a href=\"userdetails.php?id=$arr[addedby]\">$arr[username]</a>" : $arr [username]) . "</td><td  class=\"row1\" align=\"left\">" . $arr ["comment"] . "</td><td  class=\"row1\"><a href=\"bans.php?remove=$arr[id]\">Снять бан</a></td></tr>\n" );
		}
		end_table ();
	}
	if ($CURGROUP ['can_admin'] && $be ['email']) {
		print ( "<center><strong>Забаненые E-Mail`ы</strong></center>\n" );
		begin_table ();
		print ( "<tr><td class=\"colhead\">Добавлен</td><td class=\"colhead\" align=\"left\">E-Mail</td>" . "<td class=\"colhead\" align=\"left\">Кем</td><td class=\"colhead\" align=\"left\">Комментарий</td><td class=\"colhead\">Снять бан</td></tr>\n" );
		foreach ( $new_arr as $arr ) {
			if ($arr ["email"])
				print ( "<tr><td class=\"row1\">$arr[added]</td><td class=\"row1\" align=\"left\">$arr[email]</td><td  class=\"row1\" align=\"left\">" . ($CURGROUP ['can_viewprof'] ? "<a href=\"userdetails.php?id=$arr[addedby]\">$arr[username]</a>" : $arr [username]) . "</td><td  class=\"row1\" align=\"left\">" . $arr ["comment"] . "</td><td  class=\"row1\"><a href=\"bans.php?remove=$arr[id]\">Снять бан</a></td></tr>\n" );
		}
		end_table ();
	}
}

//print("<table border=1 cellspacing=0 cellpadding=5>\n");
print ( "<br />\n" );
print ( "<form method=\"post\" action=\"bans.php\">\n" );
if ($CURGROUP ['can_banip']) {
	begin_table ();
	print ( "<tr><td class=\"colhead\" colspan=\"2\">Забанить IP адрес</td></tr>" );
	print ( "<tr><td class=\"rowhead\">Первый IP</td><td class=\"row1\"><input type=\"text\" name=\"first\" size=\"40\"/></td></tr>\n" );
	print ( "<tr><td class=\"rowhead\">Последний IP</td><td class=\"row1\"><input type=\"text\" name=\"last\" size=\"40\"/></td></tr>\n" );
	print ( "<tr><td class=\"rowhead\">Комментарий</td><td class=\"row1\"><input type=\"text\" name=\"comment\" size=\"40\"/></td></tr>\n" );
	end_table ();
}
if ($CURGROUP ['can_ban']) {
	begin_table ();
	print ( "<tr><td class=\"colhead\" colspan=\"2\">Забанить пользователя</td></tr>" );
	print ( "<tr><td class=\"rowhead\">ID пользователя</td><td class=\"row1\"><input type=\"text\" name=\"uid\" size=\"40\"/></td></tr>\n" );
	print ( "<tr><td class=\"rowhead\">Дата разбана</td><td class=\"row1\">" . take_full_datetime ( date ( "Y" ), date ( "m" ), date ( "d" ) ) . "<br /><center><input type='checkbox' name='never'> Никогда</center></td></tr>\n" );
	print ( "<tr><td class=\"rowhead\">Комментарий</td><td class=\"row1\"><input type=\"text\" name=\"commentuser\" size=\"40\"/></td></tr>\n" );
	end_table ();
}
if ($CURGROUP ['can_admin']) {
	begin_table ();
	print ( "<tr><td class=\"colhead\" colspan=\"2\">Забанить e-mail</td></tr>" );
	print ( "<tr><td class=\"rowhead\">E-mail</td><td class=\"row1\"><input type=\"text\" name=\"email\" size=\"40\"/></td></tr>\n" );
	print ( "<tr><td class=\"rowhead\">Комментарий</td><td class=\"row1\"><input type=\"text\" name=\"commentemail\" size=\"40\"/></td></tr>\n" );
	end_table ();
}
begin_table ();
print ( "<tr><td class=\"row1\" align=\"center\" colspan=\"2\"><input type=\"submit\" value=\"Забанить\" class=\"btn\"/></td></tr>\n" );
end_table ();
print ( "</form>\n" );

stdfoot ();

?>