<?php
//
// Usage change announce URL in your favorite torrent client to
// http://my_server/this_script_name.php?tracker=one_of_the_trackers_listed_below&passkey=your_passkey
// Available trackers:
//                     -hdbits   --> does't work yet (still testing why not)
//                     -filebits --> not tested
//                     -slobytes --> tested & works
//                     -partis   --> tested & works
//
#######################################################################################################
//
// Add this table to mysql (with phpMyAdmin):
// CREATE TABLE `tracker` (
//   `id` int(10) unsigned NOT NULL auto_increment,
//   `info_hash` varchar(32) NOT NULL,
//   `downloaded` int(10) unsigned NOT NULL,
//   `uploaded` int(10) unsigned NOT NULL,
//   PRIMARY KEY  (`id`)
//) ENGINE=MyISAM AUTO_INCREMENT=1;
//
#######################################################################################################
//
// And set variables below for:
//   - database access
$hostname 'localhost'// host of your database
$db_user  'root';      // your database username
$db_pass  '';          // your database password
$database '';          // your database name
//
//   - multiplyer for your download and upload (if set to 1, script will send original data)
$DL_multiplayer 1;     // set multiplyer for DL here :)
$UL_multiplayer 1;   // set multiplyer for UL here :)
//

// keys that have to be included in response
$keys = array('info_hash''peer_id''port''uploaded''downloaded''left''key''numwant''compact''no_peer_id');
// chacking if all keys are set
foreach($keys as $req)
{
    if(!isset(
$_GET[$req]))
    {
        die(
'[c] ' $req ' is not set!');
    }
}
// additionaly check if user set 
if(!isset($_GET['tracker']))
{
    die(
'Tracker is not set ?tracker=');
}

// list of trackers
$trackers = array('hdbits' => 'http://hdbits.org/announce.php'// does't work yet (still testing why not)
                    
'filebits' => 'http://www.filebits.org/announce.php'// not tested
                    
'slobytes' => 'http://slobytes.net/announce.php'// tested & works
                    
'partis' => 'http://www.partis.si/announce.php'); // tested & works

$link mysqli_connect($hostname$db_user$db_pass) or die('Can\'t connect to MySql');
mysqli_select_db($link$database);
$result mysqli_query($link"SELECT * FROM tracker WHERE info_hash='" md5($_GET['info_hash']) . "' LIMIT 1");
$row mysqli_fetch_assoc($result);
if(!empty(
$row['info_hash']))
{
    if(
$row['downloaded'] < $_GET['downloaded'] || $row['uploaded'] < $_GET['uploaded'])
    {
        
$downloaded round(($_GET['downloaded'] - $row['downloaded']) * $DL_multiplayer) + $row['downloaded'];
        
$uploaded   round(($_GET['uploaded'] - $row['uploaded']) * $UL_multiplayer) + $row['uploaded'];
        
$handle fopen('./tracker''a');
        
fwrite($handle$_GET['downloaded'] . ' ' $downloaded ' ' $_GET['uploaded'] . ' ' $uploaded "\r\n");
        
fclose($handle);
    }
    else
    {
        
$downloaded $_GET['downloaded'];
        
$uploaded   $_GET['uploaded'];
    }
}
else
{
    
mysqli_query($link"INSERT INTO tracker (info_hash, downloaded, uploaded) VALUES ('" md5($_GET['info_hash']) . "', " $_GET['downloaded'] . ", " $_GET['uploaded'] . ")");
    
$downloaded $_GET['downloaded'];
    
$uploaded   $_GET['uploaded'];
}
mysqli_query($link"UPDATE tracker SET downloaded=" $_GET['downloaded'] . ", uploaded=" $_GET['uploaded'] . " WHERE info_hash='" md5($_GET['info_hash']) . "'");

    
$tracker $_GET['tracker'];
// parsing URL of tracker
$url parse_url($trackers[$tracker]);
if(!
$url['port']) { $url['port'] = 80; }

// establishing connection
$fp fsockopen($url['host'], intval($url['port']), $errno$errstr30);
if (!
$fp) {
    echo 
"$errstr ($errno)\n"// ups something went wrong :-(
} else {
    
$announce $url['path']; // http://www.tracker.com/announce.php
    // if this is private tracker we add passkey
    
isset($_GET['passkey']) ? $announce .= '?passkey=' $_GET['passkey'] . '&' $announce .= '?'// ?passkey=...
    // other vars that client sends
    
$announce .= 'info_hash=' urlencode($_GET['info_hash']);
    
$announce .= '&peer_id=' urlencode($_GET['peer_id']);
    
$announce .= '&port=' $_GET['port'];
    
$announce .= '&uploaded=' $uploaded ;
    
$announce .= '&downloaded=' $downloaded;
    
$announce .= '&left=' $_GET['left'];
    
$announce .= '&key=' $_GET['key'];
    
$announce .= '&numwant=' $_GET['numwant'];
    
$announce .= '&compact=' $_GET['compact'];
    
$announce .= '&no_peer_id=' $_GET['no_peer_id'];

    
$out "GET " $announce " HTTP/1.1\r\n"// sending request to remote server ie. /anounce.php?passkey=......
    
$out .= "Host: " $url['host'] . "\r\n"// www.tracker.com
    
$out .= "User-Agent: " $_SERVER['HTTP_USER_AGENT'] . "\r\n"// faking client signature so it's same as your torrent client would send it
    
$out .= "Connection: Close\r\n\r\n"// closing connection

    
fwrite($fp$out); // sending request to server
    
$response '';
    while (!
feof($fp)) {
        
$response .= fgets($fp128); //reciving response
    
}
    
fclose($fp);
    if(
$_GET['tracker'] == 'partis' || $_GET['tracker'] == 'slobytes')
    {
        
preg_match('/\r\n\r\n[\w]{2,4}\r\n(.*)/si'$response$match); // removing headers
    
}
    else
    {
        
preg_match('/\r\n\r\n(.*)/si'$response$match); // removing headers
    
}
    die(
$match[1]); // responsing with data for torrent client :)
}
?>