Hi @STorrnets2020
this is working announce.php (in my case)
<?php
#================================#
# TorrentTrader 2.08 #
# http://www.torrenttrader.org #
#--------------------------------#
# Modified by M-Jay #
#================================#
// HIDE ERRORS
error_reporting(0);
// GET SETTINGS
require_once("backend/mysql.php");
require_once("backend/config.php");
// Set Database Connection
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
);
$dsn = 'mysql:host=' . $mysql_host . ';dbname=' . $mysql_db . ';charset=utf8';
try {
$pdo = new PDO($dsn, $mysql_user, $mysql_pass, $opt);
} catch (\Exception $e) {
err('The Database Details Are Incorrect');
}
// SQL FUNKTION TO DISPLAY ERRORS
function pdo($pdo, $sql, $args = NULL)
{
try {
if (!$args) {
return $pdo->query($sql);
}
$stmt = $pdo->prepare($sql);
$stmt->execute($args);
return $stmt;
} catch (\Exception $e) {
err($e);
}
}
// WRITE RESPONSE TO FILE
function mydebug($msg)
{
if (is_writeable('test.txt')) {
$fh = fopen('test.txt', 'a+');
fputs($fh, "[Log] " . date("d.m.Y H:i:s") . " $msg\n");
fclose($fh);
}
}
// MEMBERS SETTINGS
$MEMBERSONLY = $site_config["MEMBERSONLY"];
$MEMBERSONLY_WAIT = $site_config["MEMBERSONLY_WAIT"];
//START FUNCTIONS
function sqlesc($x)
{
if (!is_numeric($x)) {
$x = "'" . $x . "'";
}
return $x;
}
function unesc($x)
{
stripslashes($x);
return $x;
}
function err($msg)
{
return benc_resp_raw("d" . benc_str("failure reason") . benc_str($msg) . "e");
}
function benc_str($s)
{
return strlen($s) . ":$s";
}
function benc_int($i)
{
return "i" . $i . "e";
}
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);
}
exit();
}
function gmtime()
{
return strtotime(get_date_time());
}
function get_date_time($timestamp = 0)
{
if ($timestamp) {
return date("Y-m-d H:i:s", $timestamp);
} else {
return gmdate("Y-m-d H:i:s");
}
}
function portblacklisted($port)
{
if ($port >= 411 && $port <= 413) {
// direct connect
return true;
} elseif ($port == 1214) {
// kazaa
return true;
} elseif ($port >= 6346 && $port <= 6347) {
// gnutella
return true;
} elseif ($port == 4662) {
// emule
return true;
} elseif ($port == 6699) {
// winmx
return true;
}
return false;
}
//////////////////////// NOW WE DO THE ANNOUNCE CODE ////////////////////////
// BLOCK ACCESS WITH WEB BROWSERS - TEST ADDED
$agent = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/^Mozilla|^Opera|^Chrome|^Links|^Lynx/i", $agent))
die("Bad decision pal. None of this works this way");
//GET DETAILS OF PEERS ANNOUNCE M-JAY
//foreach (array("passkey", "info_hash", "peer_id", "ip", "event") as $x) {
// if (isset($_GET["$x"])) {
// $GLOBALS[$x] = "" . $_GET[$x];
// }
// }
//GET DETAILS OF PEERS ANNOUNCE OLD
foreach (array("passkey","info_hash","peer_id","ip","event") as $x) {
$GLOBALS[$x] = $_GET[$x];
}
foreach (array("port", "downloaded", "uploaded", "left") as $x) {
$GLOBALS[$x] = 0 + $_GET[$x];
}
foreach (array("passkey", "info_hash", "peer_id", "port", "downloaded", "uploaded", "left") as $x) {
if (!isset($x)) {
err("Missing key: $x");
}
}
$no_peer_id = (int) $_GET["no_peer_id"];
foreach (array("info_hash") as $x) {
if (strlen($GLOBALS[$x]) != 20) {
err("Invalid $x (" . strlen($GLOBALS[$x]) . " - " . urlencode($GLOBALS[$x]) . ")");
}
}
unset($x);
if ($MEMBERSONLY) {
if (strlen($passkey) != 32)
err("Invalid passkey (" . strlen($passkey) . " - $passkey)");
}
$ip = $_SERVER["REMOTE_ADDR"];
$info_hash = bin2hex($info_hash);
foreach (array("num want", "numwant", "num_want") as $k) {
if (isset($_GET[$k])) {
$rsize = (int) $_GET[$k];
break;
}
}
if (!$port || $port > 0xffff) {
err("invalid port");
}
if (!isset($event)) {
$event = "";
}
$seeder = ($left == 0) ? "yes" : "no";
// CLIENT BAN - TEST ADDED
$agentarray = array_map("trim", explode(",", $site_config["BANNED_AGENTS"]));
$useragent = substr($peer_id, 0, 8);
foreach($agentarray as $bannedclient)
if (@strpos($useragent, $bannedclient) !== false) {
mysqli_close($GLOBALS["DBconnector"]);
err("Client is banned");
}
// END CLIENT BAN
// WANTED DATA FROM DB
$userfields = "u.id, u.class, u.uploaded, u.downloaded, u.ip, u.passkey, g.can_download"; //user details to get
$peerfields = "seeder, UNIX_TIMESTAMP(last_action) AS ez, peer_id, ip, port, uploaded, downloaded, userid, passkey"; //peers details to get
$torrentfields = "id, info_hash, banned, freeleech, seeders + leechers AS numpeers, UNIX_TIMESTAMP(added) AS ts, seeders, leechers, times_completed"; //torrent details to get
// CHECK USER
$userid = 0;
if ($MEMBERSONLY) {
$res = pdo($pdo, "SELECT $userfields FROM users u INNER JOIN `groups` g ON u.class = g.group_id WHERE u.passkey=? AND u.enabled = 'yes' AND u.status = 'confirmed' LIMIT 1", [$passkey]) or err("Cannot Get User Details");
$user = $res->fetch();
if (!$user) {
err("Cannot locate a user with that passkey!");
} elseif ($user["can_download"] == "no") {
err("You do not have permission to download.");
}
$userid = $user["id"]; //etc
}
// CHECK TORRENT
$res = pdo($pdo, "SELECT $torrentfields FROM torrents WHERE info_hash=" . sqlesc($info_hash)) or err("Cannot Get Torrent Details");
$torrent = $res->fetch();
if (!$torrent) {
err("Torrent not found on this tracker - hash = " . $info_hash);
} elseif ($torrent["banned"] == 'yes') {
err("Torrent has been banned - hash = " . $info_hash);
}
$torrentid = $torrent["id"];
// LIMIT NUMBER OF PEERS
$peerlimit = 50;
$numpeers = $torrent["numpeers"];
if ($numpeers > $peerlimit) {
$limit = "ORDER BY RAND() LIMIT $peerlimit";
} else {
$limit = "";
}
// CHECK PEER NEW OR OLD
$sql = pdo($pdo, "SELECT $peerfields FROM peers WHERE torrent = $torrentid AND peer_id = " . sqlesc($peer_id) . "") or err("Error Selecting Peers");
$res = $sql->fetch(PDO::FETCH_ASSOC);
if ($res) {
// Already Peer So Update User & Peer Table
$upthis = max(0, $uploaded - $res["uploaded"]);
$downthis = max(0, $downloaded - $res["downloaded"]);
//==== ADD TO DEBUG
mydebug("upthis - $upthis - uploaded - $uploaded - peer - $res[uploaded] - torrentid - $torrent[id] - freeleech - $torrent[freeleech] - seeder - $seeder - user -$user[id] - event - $event");
mydebug("downthis - $downthis - downloaded - $downloaded - peer - $res[downloaded] - torrentid - $torrent[id] - freeleech - $torrent[freeleech] - seeder - $seeder - user -$user[id] - event - $event");
//=== END DEBUG
if (($upthis > 0 || $downthis > 0)) {
if ($torrent["freeleech"] == 1) {
pdo($pdo, "UPDATE users SET uploaded = uploaded + $upthis WHERE id=$userid") or err("Tracker error: Unable to update stats");
pdo($pdo, "UPDATE `snatched` SET `uload` = `uload` + '$upthis', `utime` = '".gmtime()."', `ltime` = `ltime` + '$elapsed' WHERE `tid` = '$torrentid' AND `uid` = '$userid'");
} else {
pdo($pdo, "UPDATE users SET uploaded = uploaded + $upthis, downloaded = downloaded + $downthis WHERE id=$userid") or err("Tracker error: Unable to update stats");
pdo($pdo, "UPDATE `snatched` SET `uload` = `uload` + '$upthis', `dload` = `dload` + '$downthis', `utime` = '".gmtime()."', `ltime` = `ltime` + '$elapsed' WHERE `tid` = '$torrentid' AND `uid` = '$userid'");
}
}
pdo($pdo, "UPDATE peers SET ip = " . sqlesc($ip) . ", passkey = " . sqlesc($passkey) . ", port = $port, uploaded = $uploaded, downloaded = $downloaded, to_go = $left, last_action = '" . get_date_time() . "', client = " . sqlesc($agent) . ", seeder = '$seeder' WHERE torrent = $torrentid AND peer_id = " . sqlesc($peer_id) . "");
} else {
// todo check new peer
//check passkey isnt leaked,
//wait time check
// NEW PEER SO ADD
pdo($pdo, "INSERT INTO peers (connectable, torrent, peer_id, ip, passkey, port, uploaded, downloaded, to_go, started, last_action, seeder, userid, client) VALUES ('yes', $torrentid, " . sqlesc($peer_id) . ", " . sqlesc($ip) . ", " . sqlesc($passkey) . ", $port, $uploaded, $downloaded, $left, '" . get_date_time() . "', '" . get_date_time() . "', '$seeder', '$userid', " . sqlesc($agent) . ")");
if ( ( $MEMBERSONLY ) && ( $seeder == 'no' ) ) {
pdo($pdo, "INSERT DELAYED INTO `snatched` (`uid`, `tid`, `stime`, `utime`) VALUES ('$userid', '$torrentid', '".gmtime()."', '".gmtime()."') ON DUPLICATE KEY UPDATE `utime` = '" . gmtime() . "'");
} else {
pdo($pdo, "INSERT DELAYED INTO `snatched` (`uid`, `tid`, `stime`, `utime`, `dload`, `completed`) VALUES ('$userid', '$torrentid', '".gmtime()."', '".gmtime()."', '".$tsize."', '1') ON DUPLICATE KEY UPDATE `utime` = '" . gmtime() . "'");
}
}
// LETS GET ALL PEERS
$res = pdo($pdo, "SELECT $peerfields FROM peers WHERE torrent = $torrentid");
// Sort Response
$seeders = 0;
$leechers = 0;
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
if ($row['seeder'] == 'no') {
$leechers++;
} else {
$seeders++;
}
$peers .= "d2:ip" . strlen($row['ip']) . ":$row[ip]4:porti$row[port]ee";
}
$peerlist['list'] = $peers;
$peerlist['seeders'] = $seeders;
$peerlist['leechers'] = $leechers;
$resp = "d8:intervali60e12:min intervali30e8:completei$peerlist[seeders]e10:incompletei$peerlist[leechers]e5:peersl$peerlist[list]ee";
// NOW DO EVENTS
if ($event == "stopped") {
pdo($pdo, "DELETE FROM peers WHERE torrent = $torrentid AND peer_id = " . sqlesc($peer_id) . "");
}
if ($event == "completed") {
$updateset[] = "times_completed = times_completed + 1";
if ($MEMBERSONLY) {
pdo($pdo, "INSERT INTO completed (userid, torrentid, date) VALUES ($userid, $torrentid, '" . get_date_time() . "')");
pdo($pdo, "UPDATE `snatched` SET `completed` = '1' WHERE `tid` = '$torrentid' AND `uid` = '$userid' AND `utime` = '" . gmtime() . "'");
}
}
// NOW UPDATE TORRENT
$updateset = array();
$updateset[] = "seeders = $peerlist[seeders]";
$updateset[] = "leechers = $peerlist[leechers]";
if ($seeder == "yes") {
if ($torrent["banned"] != "yes")
$updateset[] = "visible = 'yes'";
$updateset[] = "last_action = '" . get_date_time() . "'";
}
if (count($updateset)) {
pdo($pdo, "UPDATE torrents SET " . join(",", $updateset) . " WHERE id=$torrentid") or err("Tracker error: Unable to update torrent");
}
// SEND BENCODED DATA TO DEBUG FILE PLACE TEST.TXT IN ROOT
//mydebug($resp);
// SEND DATA TO CLIENT
benc_resp_raw($resp);
But in my whole case from beginning was problem in PHP construction
both MCRYPT and PDO was ON
MEMCACHE and MEMCACHED was off
which in turn has caused a conflict (I don't know much about sql and PHP unfortunately) but after I turned off MCRYPT and turned on memcache and memcached after a round of 30 minutes (announce default update) my torrents started to turn green again.
PHP 7.4.9
mcrypt ==> off
PDO ==> on
memcache ==> on
memcached ==> on
www.exyusubs.com
TorrentTrader V3.8.3 (SE)
OS: AlmaLinux 9.9 (BC)
Apache version: 2.4.71
PHP version: 8.5.13 (PHP-FPM)
MySQL version: 10.11.18-MariaDB