<?php
require "include/bittorrent.php";
dbconn ( false );
validcan ( false, 'can_downloadtor' );

global $CURGROUP, $ALLGROUPS, $CURUSER;
if (! $_GET ['id']) {
	$sql = sql_query ( "SELECT t.name, t.id, t.owner FROM snatched AS s LEFT JOIN torrents AS t ON t.id = s.torrent LEFT JOIN peers AS p ON p.userid=s.userid AND p.torrent=s.torrent WHERE s.userid = " . $CURUSER ['id'] . " AND (s.seeder='yes' || s.finished='yes' || p.seeder='yes') GROUP BY t.id" ) or sqlerr ( __FILE__, __LINE__ );
	stdhead ( "Ваши раздачи и скачанные торренты" );
	begin_frame ( "Ваши раздачи и скачанные торренты" );
	$be = array ();
	?><table width='100%'>
	<tr>
		<td>Название</td>
		<td>Ваш торрент?</td>
		<td>Перекачать</td>
	</tr><?
	while ( $res = mysql_fetch_array ( $sql ) ) {
		?><tr>
		<td><?=($CURGROUP ['can_details'] ? "<a href='details.php?id=" . $res ["id"] . "'>" . $res ['name'] . "</a>" : $res ['name'])?></td>
		<td><?=($res ['owner'] == $CURUSER ['id'] ? "<font color='green'>Да</font>" : "<font color='red'>Нет</font>")?></td>
		<td><a href='downloaded.php?id=<?=$res ['id']?>'>Перекачать</a></td>
	</tr><?
	}
	?>
</table><?
	end_frame ();
	stdfoot ();
} else {
	$id = ( int ) $_GET ['id'];
	if (@ini_get ( 'output_handler' ) == 'ob_gzhandler' and @ob_get_length () !== false) { // if output_handler = ob_gzhandler, turn it off and remove the header sent by PHP
		@ob_end_clean ();
		header ( 'Content-Encoding:' );
	}
	$sql = sql_query ( "SELECT t.filename FROM snatched AS s LEFT JOIN torrents AS t ON t.id = s.torrent LEFT JOIN peers AS p ON p.userid=s.userid AND p.torrent=s.torrent WHERE s.userid = " . $CURUSER ['id'] . " AND (s.seeder='yes' || s.finished='yes' || p.seeder='yes')  AND t.id=" . $id ) or sqlerr ( __FILE__, __LINE__ );
	$fn = "$torrent_dir/$id.torrent";
	$row = mysql_fetch_array ( $sql );
	if (! $row || ! is_file ( $fn ) || ! is_readable ( $fn ))
		stderr ( $tracker_lang ['error'], $tracker_lang ['unable_to_read_torrent'] );
	$myres = sql_query ( "SELECT pb.inrbin, p.price FROM products AS p LEFT JOIN prodbuy AS pb ON pb.pid = p.id AND pb.uid = " . $CURUSER [id] . " WHERE p.vid = '$id' AND p.type='torrent' LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
	$myresar = mysql_fetch_array ( $myres );
	
	if ($myresar ['inrbin'] && $myresar ['price'] && ! $CURGROUP ['can_dtforsell']) {
		global $tracker_lang;
		stdhead ( $tracker_lang ['error'] . "!" );
		$groups = array ();
		foreach ( $ALLGROUPS as $crgrp )
			if ($crgrp ['price'])
				$groups [] = $tracker_lang [$crgrp ['name']];
		$groupsna = implode ( ' или ', $groups );
		stdmsg ( $tracker_lang ['error'], "Этот торрент доступен только, тем кто его купил или для " . $groups [0] . " и ВЫШЕ. Он стоит $money евро. Покупка осуществляеться через систему webmoney не только в евро, но и в рублях и долларах. Вы можете <a href=takebuy.php?id=$id>купить</a>" . ($CURGROUP ['can_rbin'] ? " или <a href=takerbin.php?id=$id>добавить в корзину</a> этот продукт" : "") . ". Также вы можете сделать пожертвование и получить статусы: " . $groupsna . " <a href=donate.php>здесь</a>.", 'error' );
		stdfoot ();
		exit ();
	}
	sql_query ( "UPDATE torrents SET hits = hits + 1 WHERE id = " . sqlesc ( $id ) );
	
	require_once "include/benc.php";
	
	if (strlen ( $CURUSER ['passkey'] ) != 32) {
		$CURUSER ['passkey'] = md5 ( $CURUSER ['username'] . get_date_time () . $CURUSER ['passhash'] );
		sql_query ( "UPDATE users SET passkey=" . sqlesc ( $CURUSER [passkey] ) . " WHERE id=" . sqlesc ( $CURUSER [id] ) );
	}
	
	$dict = bdec_file ( $fn, (1024 * 1024) );
	
	$dict ['value'] ['announce'] ['value'] = $announce_urls [0] . "?passkey=$CURUSER[passkey]"; //"$DEFAULTBASEURL/announce.php?passkey=$CURUSER[passkey]";
	$dict ['value'] ['announce'] ['string'] = strlen ( $dict ['value'] ['announce'] ['value'] ) . ":" . $dict ['value'] ['announce'] ['value'];
	$dict ['value'] ['announce'] ['strlen'] = strlen ( $dict ['value'] ['announce'] ['string'] );
	
	header ( "Expires: Tue, 1 Jan 1980 00:00:00 GMT" );
	header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
	header ( "Cache-Control: no-store, no-cache, must-revalidate" );
	header ( "Cache-Control: post-check=0, pre-check=0", false );
	header ( "Pragma: no-cache" );
	header ( "X-Powered-by: CyBERhype Tracker - http://bt-tracker.co.cc" );
	header ( "Accept-Ranges: bytes" );
	header ( "Connection: close" );
	header ( "Content-Transfer-Encoding: binary" );
	header ( "Content-Disposition: attachment; filename=\"" . urlencode ( $row ['filename'] ) . "\"" );
	header ( "Content-Type: application/x-bittorrent" );
	ob_implicit_flush ( true );
	
	print ( benc ( $dict ) );

}
?>