Gazelle/classes/tracker.class.php

73 lines
2.1 KiB
PHP
Raw Normal View History

2012-10-11 08:00:15 +00:00
<?
// TODO: Turn this into a class with nice functions like update_user, delete_torrent, etc.
class Tracker {
/**
* Send a GET request over a socket directly to the tracker
* For example, Tracker::update_tracker('change_passkey', array('oldpasskey' => OLD_PASSKEY, 'newpasskey' => NEW_PASSKEY)) will send the request:
* GET /tracker_32_char_secret_code/update?action=change_passkey&oldpasskey=OLD_PASSKEY&newpasskey=NEW_PASSKEY HTTP/1.1
*
* @param string $Action The action to send
* @param array $Updates An associative array of key->value pairs to send to the tracker
* @param boolean $ToIRC Sends a message to the channel #tracker with the GET URL.
*/
2013-06-19 08:01:09 +00:00
public static function update_tracker($Action, $Updates, $ToIRC = false) {
2012-10-11 08:00:15 +00:00
//Build request
$Get = '/update?action='.$Action;
foreach ($Updates as $Key => $Value) {
$Get .= '&'.$Key.'='.$Value;
}
if ($ToIRC != false) {
send_irc('PRIVMSG #tracker :'.$Get);
}
$Path = TRACKER_SECRET.$Get;
2013-04-30 18:18:07 +00:00
$Return = '';
2012-10-11 08:00:15 +00:00
$Attempts = 0;
while ($Return != "success" && $Attempts < 3) {
// Send update
$File = fsockopen(TRACKER_HOST, TRACKER_PORT, $ErrorNum, $ErrorString);
if ($File) {
$Header = 'GET /'.$Path.' HTTP/1.1\r\n';
if (fwrite($File, $Header) === false) {
$Attempts++;
$Err = "Failed to fwrite()";
sleep(3);
continue;
}
} else {
$Attempts++;
$Err = "Failed to fsockopen() - ".$ErrorNum." - ".$ErrorString;
sleep(6);
continue;
}
// Check for response.
$ResHeader = '';
do {
$ResHeader .= fread($File, 1);
} while (!feof($File) && !Misc::ends_with($ResHeader, "\r\n\r\n"));
$Response = '';
2013-04-30 18:18:07 +00:00
while ($Line = fgets($File)) {
2012-10-11 08:00:15 +00:00
$Response .= $Line;
}
$Return = chop($Response);
$Attempts++;
}
if ($Return != "success") {
send_irc("PRIVMSG #tracker :{$Attempts} {$Err} {$Get}");
2013-08-28 23:08:41 +00:00
if (G::$Cache->get_value('ocelot_error_reported') === false) {
2012-10-11 08:00:15 +00:00
send_irc("PRIVMSG ".ADMIN_CHAN." :Failed to update ocelot: ".$Err." : ".$Get);
2013-08-28 23:08:41 +00:00
G::$Cache->cache_value('ocelot_error_reported', true, 3600);
2012-10-11 08:00:15 +00:00
}
}
return ($Return == "success");
}
}
?>