<?php
/*
*----------------------------phpMyBitTorrent V 2.0-beta4-----------------------*
*--- The Ultimate BitTorrent Tracker and BMS (Bittorrent Management System) ---*
*--------------   Created By Antonio Anzivino (aka DJ Echelon)   --------------*
*-------------               http://www.p2pmania.it               -------------*
*------------ Based on the Bit Torrent Protocol made by Bram Cohen ------------*
*-------------              http://www.bittorrent.com             -------------*
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*
*--   This program is free software; you can redistribute it and/or modify   --*
*--   it under the terms of the GNU General Public License as published by   --*
*--   the Free Software Foundation; either version 2 of the License, or      --*
*--   (at your option) any later version.                                    --*
*--                                                                          --*
*--   This program is distributed in the hope that it will be useful,        --*
*--   but WITHOUT ANY WARRANTY; without even the implied warranty of         --*
*--   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          --*
*--   GNU General Public License for more details.                           --*
*--                                                                          --*
*--   You should have received a copy of the GNU General Public License      --*
*--   along with this program; if not, write to the Free Software            --*
*-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA --*
*--                                                                          --*
*------------------------------------------------------------------------------*
*------              ©2005 phpMyBitTorrent Development Team              ------*
*-----------               http://phpmybittorrent.com               -----------*
*------------------------------------------------------------------------------*
*-----------------   Sunday, September 14, 2008 9:05 PM   ---------------------*
*/


require_once("include/config_lite.php");

if (eregi("(Mozilla|Opera|Lynx|Netscape)",$HTTP_SERVER_VARS["HTTP_USER_AGENT"])) {
        die("<html><head><title>Error!</title></head><body><h3>Sorry, but this file is not suitable for browsers.</h3></body></html>");
}

if ($stealthmode) die();

function hash_where($name, $hash_arr) {
        $new_hash_arr = Array();
        foreach ($hash_arr as $hash) {
                array_push($new_hash_arr,"'".addslashes(urldecode($hash))."'");
        }
        return $name." IN ( ".implode(",",$new_hash_arr)." )";
}

function err($msg) {
        benc_resp_raw("d14:failure reason". strlen($msg) .":". $msg);
        die();
}

function hash_pad($hash) {
        return str_pad($hash, 20);
}
function benc_resp_raw($x) {
        header("Content-Type: text/plain");
        header("Pragma: no-cache");
        echo $x;
}
//$info_hash_array = array('info_hash');
preg_match_all('/info_hash=([^&]*)/i', $_SERVER["QUERY_STRING"], $info_hash_array);
$info_hash_array = str_replace('info_hash=','',$info_hash_array[0]);
//print_r($info_hash_array);

if (count($info_hash_array) < 1) $sql = "SELECT tracker, info_hash, seeders AS complete, completed AS downloaded, leechers AS incomplete, name FROM ".$db_prefix."_torrents WHERE tracker IS NULL AND exeem IS NOT NULL ORDER BY id;";
else $sql = "SELECT info_hash, seeders AS complete, completed AS downloaded, leechers AS incomplete, name FROM ".$db_prefix."_torrents WHERE ".hash_where("info_hash", $info_hash_array);

if (! $res = $db->sql_query($sql)) err("SQL ERROR: ".$sql);
if ($db->sql_numrows($res) < 1) err("Torrent not registered with this tracker." . $sql ."");

$r .= "d5:filesd";
while ($row = $db->sql_fetchrow($res)) {
        $r .= "20:" . hash_pad($row["info_hash"]) . "d" .
        "4:name". strlen($row["name"]) .":". $row["name"].
        "8:completei". $row["complete"] ."e".
        "10:downloadedi". $row["downloaded"] ."e".
        "10:incompletei". $row["incomplete"] ."e".
        "e";
}
$r .= "e";
if ($announce_interval > 0 OR $announce_interval_min > 0) {
        $r .= "5:flagsd20:min_request_intervali".max($announce_interval,$announce_interval_min)."ee";
}
$r .= "e";

ob_clean();
benc_resp_raw($r);

$db->sql_query("",END_TRANSACTION);
if (!$db->persistency) $db->sql_close();
die();
?>