if (!($row = mysqli_fetch_assoc($query))) {
// $row not available - INSERT, DELETE FROM, etc
} else {
// $row available - UPDATE, DELETE FROM, continue, etc
}
$mysqli->mysqli_num_rows()
$pdo->getRowCount()
$query = SQL_Query_exec("SELECT id FROM users WHERE username = " . sqlesc($Data));
if ($row = mysqli_fetch_assoc($query)) {
$Userid = $row['id'];
SQL_Query_exec("DELETE FROM ajshoutbox WHERE uid = " . sqlesc($Userid)) or sqlerr(__FILE__, 293));
execcommand_message();
}
if (extension_loaded('com_dotnet')) {
// The com_dotnet extension is loaded
$wmi = new COM('WinMgmts:\\\\.');
// Rest of your code here
} else {
// The com_dotnet extension is not loaded or supported
// Handle the situation accordingly
echo "The com_dotnet extension is not available.";
}
function getip($convertToIPv6 = false)
{
$trustedHeaders = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_CF_CONNECTING_IP',
'REMOTE_ADDR'
];
$ip = '';
foreach ($trustedHeaders as $header) {
if (!empty($_SERVER[$header])) {
$ip = $_SERVER[$header];
break;
}
}
if (empty($ip)) {
$ip = $_SERVER['REMOTE_ADDR'];
}
if (strpos($ip, ',') !== false) {
$ip = explode(',', $ip)[0];
}
if (strpos($ip, '::ffff:') === 0) {
$ip = substr($ip, 7);
}
$ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
if ($convertToIPv6 && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$ip = '::ffff:' . $ip;
}
return $ip;
}
<?
// Cleanup V2 - PECL Parallel support
$tasks = [
'cleancache' => [1],
'seedbonus' => [],
'comments' => [],
'checkexpiredwarns' => [],
'cleanlogs' => [1],
'cleannotifs' => [1],
'cleanshoutbox' => [1],
'autopromote' => [],
'autofreeleech' => [],
'autodemote' => [],
'pendingusers' => [],
'privseeders' => [],
'checkdead' => [],
'startsync' => [],
'ratiowarn' => [] // Heavy
];
$classFiles = [
'Extensions',
'Cleanup'
];
$results = [];
$runtime = new \parallel\Runtime;
foreach ($tasks as $taskName => $taskParams) {
$future = $runtime->run(function () use ($taskName, $taskParams, $classFiles) {
spl_autoload_register(function ($className) use ($classFiles) {
if (!empty($classFiles) && in_array($className, $classFiles)) {
include_once "classes/$className.php";
}
});
$cleanup = new \Cleanup;
$lockName = "cleanup_$taskName";
if ($cleanup->acquireLock($lockName)) {
if (method_exists($cleanup, $taskName)) {
call_user_func_array([$cleanup, $taskName], $taskParams);
echo "Task '$taskName' completed.\n";
}
$cleanup->query_params('DELETE FROM locks WHERE name = :name', array(
'name' => $lockName
));
} else {
echo "Task '$taskName' skipped due to a lock.\n";
}
});
}
echo "All tasks completed.\n";
interface Tasks
{
public function seedbonus();
public function comments();
public function checkexpiredwarns();
public function ratiowarn();
public function cleanlogs();
public function autopromote();
public function autofreeleech();
public function autodemote();
public function checkdead();
public function pendingusers();
public function privseeders();
public function startsync();
public function cleanshoutbox();
public function cleancache();
public function cleannotifs();
public function acquireLock($name);
}
class Cleanup extends Extensions implements Tasks
{
public function acquireLock($name) {
$lock = $this->query_params('INSERT INTO locks (name, created_at, updated_at) VALUES (:name, NOW(), NOW()) ON DUPLICATE KEY UPDATE updated_at = NOW()', array(
'name' => $name
));
return $lock;
}
$userid = 0;
if ($MEMBERSONLY){
//check passkey is valid, and get users details
$res = SQL_Query_exec("SELECT $userfields FROM users u INNER JOIN groups g ON u.class = g.group_id WHERE u.passkey=".sqlesc($passkey)." AND u.enabled = 'yes' AND u.status = 'confirmed' LIMIT 1") or err("Cannot Get User Details");
$user = mysqli_fetch_assoc($res);
if (!$user)
err("Cannot locate a user with that passkey!");
if ($user["can_download"] == "no")
err("You do not have permission to download.");
$userid = $user["id"]; //etc
}
if ($MEMBERSONLY){
//check passkey is valid, and get users details
$res = SQL_Query_exec("SELECT $userfields FROM users u INNER JOIN groups g ON u.class = g.group_id WHERE u.passkey=".sqlesc($passkey)." AND u.enabled = 'yes' AND u.status = 'confirmed' LIMIT 1") or err("Cannot Get User Details");
$user = mysqli_fetch_assoc($res);
if (!$user)
err("Cannot locate a user with that passkey!");
if ($user["can_download"] == "no")
err("You do not have permission to download.");
$userid = $user["id"]; //etc
$torrent_id = 0;
$res2 = SQL_Query_exec("SELECT id FROM torrents WHERE info_hash = ".sqlesc($GLOBALS['info_hash'])) or err("Cannot Get Torrent Details");
$torrent = mysqli_fetch_assoc($res2);
$torrent_id = $torrent['id'];
}
if (array_key_exists('key', $data)) {
$max_connections = $site_config['max_conn_allowed']; // default: 5
$torrent_max_connections = 50;
$reduced_connections = min(5, $torrent_max_connections);
$user_id = sqlesc($userid);
$torrent_id = sqlesc($torrent_id);
$checkdata = mysqli_query($GLOBALS['DBconnector'], "SELECT clientkey FROM peers WHERE id = $user_id AND torrent_id = $torrent_id AND seeder = 'no' AND completed = 0");
$num_connections = mysqli_num_rows($checkdata);
$unique_clientkeys = array();
while ($row = mysqli_fetch_assoc($checkdata)) {
$unique_clientkeys[] = $row['clientkey'];
}
$num_unique_clientkeys = count(array_unique($unique_clientkeys));
if ($site_config['max_conn_increased'] === 0) {
if ($num_unique_clientkeys > $max_connections) {
mysqli_query($GLOBALS['DBconnector'], "DELETE p FROM peers p LEFT JOIN (SELECT clientkey FROM peers WHERE id = $user_id ORDER BY RAND() LIMIT $user_id) excluded ON p.clientkey = excluded.clientkey WHERE p.user_id = $user_id AND p.torrent = $torrent_id AND p.seeder = 'no' AND p.completed = 0 AND excluded.clientkey IS NOT NULL");
$num_connections = $reduced_connections;
}
if ($num_unique_clientkeys > 0) {
header('X-Connection-Limit: ' . $num_unique_clientkeys);
}
} else {
header('X-Connection-Limit: ' . $max_connections);
}
mysqli_close($GLOBALS['DBconnector']);
}
'max_conn_allowed' = 5;
'max_conn_increased' = 0; // 0: manage connections, 1: no control (open)
if (array_key_exists('key', $data)) {
$max_connections = $user_row['max_conn_allowed'];
$torrent_max_connections = 50;
$reduced_connections = min(5, $torrent_max_connections);
$checkdata = $db->query_params("SELECT clientkey FROM peers WHERE user_id = :user_id AND torrent_id = :torrent_id AND seeder = 'no' AND completed = 0", array(
'user_id' => $user_id,
'torrent_id' => $torrent_id
));
$num_connections = count(array_unique(array_column($checkdata->fetchAll(), 'clientkey')));
if ($user_row['max_conn_increased'] === 0) {
if ($num_connections > $max_connections) {
$db->query_params("DELETE p FROM peers p LEFT JOIN (SELECT clientkey FROM peers WHERE user_id = :user_id ORDER BY RAND() LIMIT :limit) excluded ON p.clientkey = excluded.clientkey WHERE p.user_id = :user_id AND p.torrent_id = :torrent_id AND p.seeder = 'no' AND p.completed = 0 AND excluded.clientkey IS NOT NULL", array(
'user_id' => $user_id,
'torrent_id' => $torrent_id,
'limit' => $reduced_connections
));
$num_connections = $reduced_connections;
}
if ($num_connections > 0) {
header('X-Connection-Limit: ' . $num_connections);
$account->sendNotif($user_row['user_id'], 'Connections reduced (' . $num_connections . ')', 'Tracker');
$logevent = $db->query_params('INSERT INTO announce_log (message, added) VALUES (:message, NOW())', array(
'message' => 'Connections reduced (' . $num_connections . ')'
));
}
} else {
header('X-Connection-Limit: ' . $max_connections);
}
}
ALTER TABLE users ADD COLUMN max_conn_increased TINYINT DEFAULT 0 NULL;
ALTER TABLE users ADD COLUMN max_conn_allowed TINYINT DEFAULT 5 NULL;
function crc32c($crc, $data) {
static $table = null;
if ($table === null) {
$table = array();
for ($i = 0; $i < 256; ++$i) {
$crc = $i;
for ($j = 0; $j < 8; ++$j) {
$crc = ($crc & 1) ? (0x82f63b78 ^ ($crc >> 1)) : ($crc >> 1);
}
$table[$i] = $crc;
}
}
$crc = ~$crc;
foreach (str_split($data) as $byte) {
$crc = $table[($crc ^ ord($byte)) & 0xff] ^ ($crc >> 8);
}
return ~$crc;
}
function calculate_node_id($ip, $num_octets) {
$v4_mask = array(0x03, 0x0f, 0x3f, 0xff);
$v6_mask = array(0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff);
$mask = ($num_octets == 4) ? $v4_mask : $v6_mask;
for ($i = 0; $i < $num_octets; ++$i) {
$ip[$i] &= $mask[$i];
}
$rand = mt_rand(0, 0xff);
$r = $rand & 0x7;
$ip[0] |= $r << 5;
$crc = crc32c(0, pack('C*', ...$ip));
$node_id = array();
$node_id[] = ($crc >> 24) & 0xff;
$node_id[] = ($crc >> 16) & 0xff;
$node_id[] = (($crc >> 8) & 0xf8) | (mt_rand(0, 0x7));
for ($i = 3; $i < 19; ++$i) {
$node_id[] = mt_rand(0, 0xff);
}
$node_id[] = $rand;
return $node_id;
}
// example usage
$ip = array(124, 31, 75, 21);
$num_octets = 4;
$node_id = calculate_node_id($ip, $num_octets);
print implode('', array_map('sprintf', array_fill(0, count($node_id), '%02x'), $node_id)) . "\n";
function estimateCount(array $bloom): float
{
$m = 256 * 8;
$k = 2;
$c = $m - 1;
for ($i = 0; $i < count($bloom); $i++) {
$c -= ($bloom[$i] === 0) ? 8 : substr_count(decbin($bloom[$i]), '0');
}
$c = max(1, $c);
$size = log($c / $m) / ($k * log(1 - 1 / $m));
return $size;
}
function insertIP(array $ip, array &$bloom): void
{
$hash = sha1(implode('.', $ip), true);
$index1 = unpack('v', substr($hash, 0, 2))[1];
$index2 = unpack('v', substr($hash, 2, 2))[1];
$m = 256 * 8;
$index1 %= $m;
$index2 %= $m;
$bloom[$index1 >> 3] |= (1 << ($index1 & 7));
$bloom[$index2 >> 3] |= (1 << ($index2 & 7));
}