<?

/**
*
* @package ppkBB3cker
* @version $Id: scrape.php 1.000 2009-03-02 12:43:00 PPK $
* @copyright (c) 2008, 2009 PPK, Based On skytracker, torrenttrader
* http://ppkbb3cker.rx22.ru, http://protoneutron.narod.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

define('IN_PHPBB', true);

$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

require($phpbb_root_path . 'config.'.$phpEx);

if($dbms!='mysql')
{
	err('Only mysql supported');
}

$c=@mysql_connect($dbhost, $dbuser, $dbpasswd);
if(!$c)
{
	err('Error connecting database: '.mysql_error());
}

$s=@mysql_select_db($dbname, $c);
if(!$s)
{
	err('Error selecting database: '.mysql_error());
}

define('TRACKER_TORRENTS_TABLE', $table_prefix . 'tracker_torrents');

$gmq_gpc = get_magic_quotes_gpc() ? 1 : 0;

$info_hash=@$_GET['?info_hash'] && @!$_GET['info_hash'] ? $_GET['?info_hash'] : @$_GET['info_hash'];
$l_info_hash=strlen($info_hash);
if ($l_info_hash == 20)
{
	$info_hash=bin2hex($info_hash);
}
else if($l_info_hash != 40)
{
	err("invalid info_hash");
}
$info_hash = $gmq_gpc ? $info_hash : mysql_real_escape_string($info_hash);

$sql = "SELECT info_hash, seeders, leechers, times_completed FROM ".TRACKER_TORRENTS_TABLE." WHERE info_hash='$info_hash'";
$result=my_sql_query($sql);
$torrent = mysql_fetch_array($result);

if (!$torrent)
{
	err("Torrent not found on this tracker - hash = " . $info_hash);
}

$reply='d5:filesd';

while($row = mysql_fetch_array($result))
{
	$hash = hex2bin($row['info_hash']);

	$reply.='20:'.$hash.'d';
	$reply.='8:completei'.$row['seeders'].'e';
	$reply.='10:downloadedi'.$row['times_completed'].'e';
	$reply.='10:incompletei'.$row['leechers'].'e';
	/*if(isset($row['filename']))
	{
		$reply.='4:name'.strlen($row['filename']).':'.$row['filename'];
	}*/
	$reply.='e';
}
mysql_free_result($result);
$reply.='ee';
echo $reply;

mysql_close();

exit();

//############################################################

function my_sql_query($query)
{
	global $startsqlcount, $c;

	$startsqlcount+=1;

	return mysql_query($query);
}

function err($msg)
{
	benc_resp(array("failure reason" => array("type" => "string", "value" => $msg)));
	exit();
}

function benc_resp($d)
{
	benc_resp_raw(benc(array("type" => "dictionary", "value" => $d)));
}

function benc_resp_raw($x)
{

	header("Content-Type: text/plain");
	header("Pragma: no-cache");

	if (@$_SERVER['HTTP_ACCEPT_ENCODING'] == 'gzip')
	{
		header("Content-Encoding: gzip");
		echo gzencode($x, 9, FORCE_GZIP);
	}
	else
	{
		print($x);
	}

}

function benc($obj)
{
	if (!is_array($obj) || !isset($obj['type']) || !isset($obj['value']))
	{
		return;
	}
	$c = $obj['value'];
	switch ($obj['type'])
	{
		case "string":
			return benc_str($c);
		case "integer":
			return benc_int($c);
		case "list":
			return benc_list($c);
		case "dictionary":
			return benc_dict($c);
		default:
			return;
	}
}

function benc_str($s)
{
	return strlen($s) . ":$s";
}

function benc_int($i)
{
	return "i" . $i . "e";
}

function benc_list($a)
{
	$s = "l";
	foreach ($a as $e)
	{
		$s .= benc($e);
	}
	$s .= "e";
	return $s;
}

function benc_dict($d)
{
	$s = "d";
	$keys = array_keys($d);
	sort($keys);
	foreach ($keys as $k)
	{
		$v = $d[$k];
		$s .= benc_str($k);
		$s .= benc($v);
	}
	$s .= "e";
	return $s;
}

function hex2bin($hexdata)
{
	$bindata = "";
	for($i=0;$i<strlen($hexdata);$i+=2)
	{
		$bindata.=chr(hexdec(substr($hexdata,$i,2)));
	}

	return $bindata;
}
?>