<?

require_once ("include/bittorrent.php");

function bark($msg) {
	stdhead ( $tracker_lang ['error'] );
	stdmsg ( $tracker_lang ['error'], $msg );
	stdfoot ();
	exit ();
}

if (! mkglobal ( "id" ))
	bark ( "Нехватает данных" );

$id = ( int ) $id;
if (! $id)
	die ();

dbconn ();

validcan ( false, 'can_deltor|can_delltor' );
global $tracker_lang, $CURGROUP;
$res = sql_query ( "SELECT name,owner,seeders, poster, screenshot1, screenshot2, screenshot3, screenshot4 FROM torrents WHERE id = $id" );
$row = mysql_fetch_array ( $res );
if (! $row)
	stderr ( $tracker_lang ['error'], "Такого торрента не существует." );

if (($CURUSER ["id"] == $row ["owner"] && $CURGROUP ['can_deltor']) || $CURGROUP ['can_delltor']) {
} else
	bark ( "Вы не владелец! Как такое могло произойти?\n" );

$rt = ( int ) $_POST ["reasontype"];

if (! is_int ( $rt ) || $rt < 1 || $rt > 5)
	bark ( "Неверная причина $rt." );

$r = $_POST ["r"];
$reason = $_POST ["reason"];

if ($rt == 1)
	$reasonstr = "Мертвый: 0 раздающих, 0 качающих = 0 пиров";
elseif ($rt == 2)
	$reasonstr = "Двойник" . ($reason [0] ? (": " . trim ( $reason [0] )) : "!");
elseif ($rt == 3)
	$reasonstr = "Nuked" . ($reason [1] ? (": " . trim ( $reason [1] )) : "!");
elseif ($rt == 4) {
	if (! $reason [2])
		bark ( "Вы не написали пукт правил, которые этот торрент нарушил." );
	$reasonstr = "Нарушение правил: " . trim ( $reason [2] );
} else {
	if (! $reason [3])
		bark ( "Вы не написали причину, почему удаляете торрент." );
	$reasonstr = trim ( $reason [3] );
}

deletetorrent ( $id );
if ($img_on_server) {
	if ($row ["poster"] && ! strpos ( " " . $row ["poster"], "../" )) {
		$poster = "torrents/images/$row[poster]";
		$del = unlink ( $poster );
	}
	if ($row ["screenshot1"] && ! strpos ( " " . $row ["screenshot1"], "../" )) {
		$scr1 = "torrents/images/$row[screenshot1]";
		$del = unlink ( $scr1 );
	}
	if ($row ["screenshot2"] && ! strpos ( " " . $row ["screenshot2"], "../" )) {
		$scr2 = "torrents/images/$row[screenshot2]";
		$del = unlink ( $scr2 );
	}
	if ($row ["screenshot3"] && ! strpos ( " " . $row ["screenshot3"], "../" )) {
		$scr3 = "torrents/images/$row[screenshot3]";
		$del = unlink ( $scr3 );
	}
	if ($row ["screenshot4"] && ! strpos ( " " . $row ["screenshot4"], "../" )) {
		$scr4 = "torrents/images/$row[screenshot4]";
		$del = unlink ( $scr4 );
	}
}
write_log ( "Торрент $id ($row[name]) был удален пользователем $CURUSER[username] ($reasonstr)\n", "F25B61", "torrent" );

stdhead ( "" . $tracker_lang ["torrent"] . " " . $tracker_lang ["deleted"] . "!" );

if (isset ( $_POST ["returnto"] ))
	$ret = "<a href=\"" . htmlspecialchars_uni ( $_POST ["returnto"] ) . "\">Назад</a>";
else
	$ret = "<a href=\"$DEFAULTBASEURL/\">На главную</a>";

?>
<h2><?=$tracker_lang ["torrent"]?> <?=$tracker_lang ["deleted"]?>!</h2>
<p><?=$ret?></p>
<?

stdfoot ();

?>