<?

require "include/bittorrent.php";

gzip ();

dbconn ();

validcan ( false, 'can_delltor' );

stdhead ( "Удалить торрент" );
begin_frame ( "Следующие торренты удалены" );
if (! $_POST ["delete"]) {
	print ( 'Не выбраны торренты!' );
	end_frame ();
	stdfoot ();
	exit ();
}
$mode = $_GET ["mode"];

if ($mode == "delete") {
	$res = sql_query ( "SELECT id, name FROM torrents WHERE id IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" );
	while ( $row = mysql_fetch_array ( $res ) ) {
		echo "ID: $row[id] - $row[name]<br>";
		$reasonstr = "Старый или не подходил под правила.";
		$text = "Торрент $row[id] ($row[name]) был удален пользователем $CURUSER[username]. Причина: $reasonstr\n";
		write_log ( $text );
	}
	foreach ( $_POST ["delete"] as $delid ) {
		@unlink ( "./torrents/" . intval ( $delid ) . ".torrent" );
	}
	$res = sql_query ( "SELECT poster, screenshot1, screenshot2, screenshot3, screenshot4 FROM torrents WHERE id IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" );
	while ( $row = mysql_fetch_array ( $res ) ) {
		if (! strpos ( " " . $row ["poster"], "../" ))
			@unlink ( "./torrents/images/" . $row ['poster'] );
		if (! strpos ( " " . $row ["screenshot1"], "../" ))
			@unlink ( "./torrents/images/" . $row ['screenshot1'] );
		if (! strpos ( " " . $row ["screenshot2"], "../" ))
			@unlink ( "./torrents/images/" . $row ['screenshot2'] );
		if (! strpos ( " " . $row ["screenshot3"], "../" ))
			@unlink ( "./torrents/images/" . $row ['screenshot3'] );
		if (! strpos ( " " . $row ["screenshot4"], "../" ))
			@unlink ( "./torrents/images/" . $row ['screenshot4'] );
	}
	sql_query ( "DELETE FROM torrents WHERE id IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" ) or sqlerr ( __FILE__, __LINE__ );
	sql_query ( "DELETE FROM snatched WHERE torrent IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" ) or sqlerr ( __FILE__, __LINE__ );
	sql_query ( "DELETE FROM ratings WHERE torrent IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" ) or sqlerr ( __FILE__, __LINE__ );
	sql_query ( "DELETE FROM checkcomm WHERE checkid IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ") AND torrent = 1" ) or sqlerr ( __FILE__, __LINE__ );
	sql_query ( "DELETE FROM bookmarks WHERE torrentid IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" ) or sqlerr ( __FILE__, __LINE__ );
	sql_query ( "DELETE FROM files WHERE torrent IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" ) or sqlerr ( __FILE__, __LINE__ );
	sql_query ( "DELETE FROM comments WHERE torrent IN (" . implode ( ", ", array_map ( "sqlesc", $_POST ["delete"] ) ) . ")" ) or sqlerr ( __FILE__, __LINE__ );
} else
	echo "Unknown mode...";

end_frame ();
stdfoot ();

?>