Thank you M-Jay and the original author for this mod, and for keeping it available all these years. Thanks also to Gollum for the Hit and Run count fix, and to x7razor and everyone else who reported problems in this topic.
Here is an updated version for PHP 8, following the same steps as the first post. Please take a backup of your files and database first.
Files pass a PHP syntax check and the cleanup step runs without database errors on PHP 8.5 with MariaDB 10.10. It has not been through real downloads yet, so please try it on a test account first. Bans are automatic, so check the numbers in config.php before you enable it.
What changed from the first post
- $row[ip] and $row[ltime] crashed the admin list on PHP 8 (undefined constant)
- $site_config["hnr_disabled"] was used but never set, so warnings expired at once and members were banned at the next cleanup
- Members could be warned again at every cleanup, because the check read their first warning of any type
- A warning was not removed when a member got down to 0 Hit and Runs
- Apostrophes in mod comments broke the ban query
- Staff (class 5 and up on a default install) are no longer warned or banned automatically
- The ban email pointed to contact.php, which does not exist on a default install
- The checkboxes in the admin list sent a formatted size instead of the id, so Delete did nothing
- The Hit and Run count now uses Gollum's fix
- Language strings that were missing from the first post are added
- The snatched table is not dropped, so existing data is kept
You must have Snatchlist installed (see the fixes for it at the end of this post).
SQL:
(On MySQL remove IF NOT EXISTS.)
ALTER TABLE `snatched` ADD COLUMN IF NOT EXISTS `done` enum('yes','no') NOT NULL DEFAULT 'no';
Add in config.php :
// Anti Hit & Run
$site_config["hnr_deadline"] = 7*86400; // time to reach the seed target (7 days)
$site_config["hnr_seedtime"] = 172800; // the seed target (48 hours)
$site_config["hnr_warn"] = 5; // warned at this many hit & runs
$site_config["hnr_disabled"] = 7*86400; // a warned member has this long before being banned (7 days)
$site_config["hnr_stop_dl"] = 5; // not used by the mod yet
$site_config["hnr_ban"] = 50; // banned at this many hit & runs
in admincp.php find
<td align="center"><a href="admincp.php?action=sqlerr"><img src="images/admin/sql_error.png" border="0" width="118" width="80" alt="" /><br />SQL Error<br /></a></td>
add after
<td align="center"><a href="admincp.php?action=hnr"><img src="images/admin/hitnrun.png" border="0" width="32" height="32" alt="" /><br />List of Hit and Run</a><br /></td>
add before last ?>
#======================================================================#
# List of Hit and Run
#======================================================================#
if ($action == "hnr")
{
if (($_POST["do"] ?? "") == "delete")
{
$ids = array_map("intval", (array) ($_POST["ids"] ?? array()));
if (!$ids) show_error_msg(T_("ERROR"), "Nothing Selected.", 1);
SQL_Query_exec("UPDATE snatched SET ltime = '86400', hnr = 'no', done = 'yes' WHERE sid IN (" . implode(",", $ids) . ")");
autolink("admincp.php?action=hnr", "Entries deleted.");
}
stdhead("List of Hit and Run");
navmenu();
$count = get_row_count("snatched", "WHERE hnr = 'yes'");
list($pagertop, $pagerbottom, $limit) = pager(50, $count, "admincp.php?action=hnr&");
begin_frame("List of Hit and Run");
echo "<div align='center'>We have <b>" . (int) $count . "</b> Hit and Run" . ($count == 1 ? "" : "s") . "</div><br />";
$result = SQL_Query_exec("SELECT s.sid, s.uid, s.tid, s.uload, s.dload, s.ltime, s.stime, s.utime, u.username, t.name FROM snatched s LEFT JOIN users u ON u.id = s.uid LEFT JOIN torrents t ON t.id = s.tid WHERE s.hnr = 'yes' ORDER BY s.uid DESC $limit");
if ($result && mysqli_num_rows($result) > 0)
{
$when = function ($time) { return $time > 0 ? htmlspecialchars(utc_to_tz(get_date_time((int) $time))) : "---"; };
echo $pagertop;
echo "<form id='snatched' method='post' action='admincp.php?action=hnr'><input type='hidden' name='do' value='delete' />";
echo "<table cellpadding='5' class='table_table' width='100%'><tr>";
foreach (array("User", "Torrent", "Uploaded", "Downloaded", "Seed Time", "Started", "Last Action") as $heading) echo "<th class='table_head'>$heading</th>";
echo "<th class='table_head'><input type='checkbox' name='checkall' onclick='checkAll(this.form.id)' /></th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
$user = $row["username"] !== null ? "<a href='account-details.php?id=" . (int) $row["uid"] . "'><b>" . class_user($row["username"]) . "</b></a>" : "ID " . (int) $row["uid"];
$torrent = $row["name"] !== null ? "<a href='torrents-details.php?id=" . (int) $row["tid"] . "'>" . htmlspecialchars(CutName($row["name"], 35)) . "</a>" : "ID " . (int) $row["tid"];
echo "<tr><td class='table_col1'>$user</td><td class='table_col2'>$torrent</td>";
echo "<td class='table_col1'>" . mksize($row["uload"]) . "</td><td class='table_col2'>" . mksize($row["dload"]) . "</td>";
echo "<td class='table_col1'>" . ($row["ltime"] > 0 ? mkprettytime($row["ltime"]) : "0") . "</td>";
echo "<td class='table_col2' align='center'>" . $when($row["stime"]) . "</td><td class='table_col1' align='center'>" . $when($row["utime"]) . "</td>";
echo "<td class='table_col2' align='center'><input type='checkbox' name='ids[]' value='" . (int) $row["sid"] . "' /></td></tr>";
}
echo "</table><br /><center><input type='submit' value='Delete' /></center></form>";
echo $pagerbottom;
}
else
{
echo "<center><b>" . T_("THERE_ARE_NO_RECORDINGS") . "</b></center><br />";
}
end_frame();
stdfoot();
}
#======================================================================#
# End List of Hit and Run
#======================================================================#
in cleanup.php
Do not replace the Poor Ratio warning block from the first post, because that stops ratio warnings being removed. If your cleanup.php has a // REMOVE WARNINGS block with type != 'Poor Ratio', add this line below that condition:
AND type != 'HnR'
find
function do_cleanup() {
add above
// Anti Hit & Run: flags torrents not seeded enough, then warns and bans. Staff (class 5+) are never warned or banned.
function do_hnr_cleanup() {
global $site_config;
$timenow = gmtime();
$now_utc = gmdate("Y-m-d H:i:s", $timenow);
$deadline = (int) $site_config["hnr_deadline"];
$seedtime = (int) $site_config["hnr_seedtime"];
$warn_at = (int) $site_config["hnr_warn"];
$ban_at = (int) $site_config["hnr_ban"];
$warn_time = (int) $site_config["hnr_disabled"];
SQL_Query_exec("UPDATE snatched SET hnr = 'yes' WHERE completed = '1' AND hnr = 'no' AND uload < dload AND stime < " . ($timenow - $deadline) . " AND ltime < $seedtime AND done = 'no'");
SQL_Query_exec("UPDATE snatched SET hnr = 'no' WHERE hnr = 'yes' AND uload >= dload");
SQL_Query_exec("UPDATE snatched SET hnr = 'no' WHERE hnr = 'yes' AND ltime >= $seedtime");
$send_pm = function ($user, $subject, $msg) {
SQL_Query_exec("INSERT INTO messages (sender, receiver, added, subject, msg, poster) VALUES (0, $user, '" . get_date_time() . "', " . sqlesc($subject) . ", " . sqlesc($msg) . ", 0)");
};
$ban = function ($user, $name, $email, $modcomment, $note, $log, $reason) use ($site_config) {
$comment = sqlesc(gmdate("d/m/Y") . " - $note\n" . $modcomment);
SQL_Query_exec("UPDATE users SET enabled = 'no', warned = 'no', modcomment = $comment WHERE id = $user");
SQL_Query_exec("DELETE FROM warnings WHERE userid = $user AND type = 'HnR'");
write_log(T_("CLEANUP_THE_MEMBER") . " <a href='account-details.php?id=$user'>" . htmlspecialchars($name) . "</a> $log");
$subject = T_("CLEANUP_YOUR_ACCOUNT") . " " . $site_config["SITENAME"] . " " . T_("CLEANUP_HAS_BEEN_DISABLED");
$body = T_("CLEANUP_YOU_WERE_BANNED_FOLLOWING") . "\n\n$reason\n\n" . T_("CLEANUP_YOU_CAN_CONTACT_BY_LINK") . " " . $site_config["SITEURL"] . "\n\n" . $site_config["SITENAME"] . " " . T_("ADMIN");
sendmail($email, $subject, $body, "From: " . $site_config["SITEEMAIL"], "-f" . $site_config["SITEEMAIL"]);
};
// everyone with hit & runs, and everyone still carrying a warning for them
$members = SQL_Query_exec("SELECT DISTINCT uid FROM snatched WHERE hnr = 'yes' AND done = 'no' UNION SELECT DISTINCT userid FROM warnings WHERE type = 'HnR' AND active = 'yes'");
while ($members && ($member = mysqli_fetch_row($members))) {
$user = (int) $member[0];
$info = mysqli_fetch_assoc(SQL_Query_exec("SELECT username, email, modcomment, enabled, class FROM users WHERE id = $user"));
if (!$info || (int) $info["class"] >= 5) continue;
$row = mysqli_fetch_row(SQL_Query_exec("SELECT COUNT(*) FROM snatched WHERE uid = $user AND hnr = 'yes' AND done = 'no'"));
$count = (int) $row[0];
$row = mysqli_fetch_row(SQL_Query_exec("SELECT COUNT(*) FROM warnings WHERE userid = $user AND type = 'HnR' AND active = 'yes'"));
$is_warned = (int) $row[0] > 0;
if ($count >= $ban_at && $info["enabled"] == "yes") {
$ban($user, $info["username"], $info["email"], $info["modcomment"], T_("CLEANUP_BANNED_FOR") . " $count H&R.", T_("CLEANUP_HAS_BEEN_BANNED_REASON") . " $count H&R.", T_("CLEANUP_YOU_HAVE_ACCUMULATED") . " $count H&R.");
continue;
}
if ($count >= $warn_at && !$is_warned && $info["enabled"] == "yes") {
$expiry = gmdate("Y-m-d H:i:s", $timenow + $warn_time);
$reason = T_("CLEANUP_WARNING_FOR_ACCUMULATING") . " $warn_at H&R.";
SQL_Query_exec("UPDATE users SET warned = 'yes' WHERE id = $user");
SQL_Query_exec("INSERT INTO warnings (userid, reason, added, expiry, warnedby, type, active) VALUES ($user, " . sqlesc($reason) . ", '" . get_date_time() . "', '$expiry', 0, 'HnR', 'yes')");
$send_pm($user, T_("CLEANUP_WARNING_FOR_H&R"), T_("CLEANUP_YOU_HAVE_BEEN_WARNEWD_ACCUMULATED") . " $warn_at " . T_("CLEANUP_H&R_INVITE_CHECK_RULE") . "\n" . T_("CLEANUP_MSG_WARNING_7_DAYS_BANNED"));
}
if ($count < $warn_at && $is_warned) {
SQL_Query_exec("UPDATE users SET warned = 'no' WHERE id = $user");
SQL_Query_exec("DELETE FROM warnings WHERE userid = $user AND type = 'HnR'");
$send_pm($user, T_("CLEANUP_REMOVAL_OF_H&R_WARNING"), T_("CLEANUP_YOU_NOW_HAVE_LESS_THAN") . " $warn_at H&R.\n" . T_("CLEANUP_YOUR_WARNING_FOR_H&R_HAS_REMOVED"));
}
}
// the warning period ended and the member is still over the limit
$expired = SQL_Query_exec("SELECT DISTINCT w.userid, u.username, u.email, u.modcomment FROM warnings w INNER JOIN users u ON u.id = w.userid WHERE w.type = 'HnR' AND w.active = 'yes' AND w.expiry < '$now_utc' AND u.enabled = 'yes' AND u.class < 5");
while ($expired && ($row = mysqli_fetch_assoc($expired))) {
$ban((int) $row["userid"], $row["username"], $row["email"], $row["modcomment"], T_("CLEANUP_BANNED_FOR_EXPIRED_H&R_WARNING_TIMEOUT"), T_("CLEANUP_HAS_BEEN_BANNED_TIMEOUT"), T_("CLEANUP_TIMEOUT_WARNING_H&R_EXPIRED"));
}
}
search :
//OPTIMIZE TABLES
add before :
do_hnr_cleanup();
in account-details search :
$numtorrents = get_row_count("torrents", "WHERE owner = $id");
Add after :
$hnr = get_row_count("snatched", "WHERE uid = $id AND hnr = 'yes'");
find
<?php echo T_("RATIO"); ?>: <?php echo $ratio; ?><br />
below
<?php echo T_("H&R"); ?>: <?php echo (int) $hnr; ?><br />
Add to english.php
$LANG["CLEANUP_WARNING_FOR_ACCUMULATING"] = "You have been warned because you have accumulated";
$LANG["CLEANUP_WARNING_FOR_H&R"] = "Warning for H&R";
$LANG["CLEANUP_YOU_HAVE_BEEN_WARNEWD_ACCUMULATED"] = "You have been warned because you have accumulated";
$LANG["CLEANUP_H&R_INVITE_CHECK_RULE"] = "H&R. Please check the site rules and keep seeding your torrents.";
$LANG["CLEANUP_MSG_WARNING_7_DAYS_BANNED"] = "If this warning is still active after 7 days, your account will be banned.";
$LANG["CLEANUP_REMOVAL_OF_H&R_WARNING"] = "Your warning for H&R has been removed";
$LANG["CLEANUP_YOU_NOW_HAVE_LESS_THAN"] = "You now have fewer than";
$LANG["CLEANUP_YOUR_WARNING_FOR_H&R_HAS_REMOVED"] = "Your warning for H&R has been removed.";
$LANG["CLEANUP_BANNED_FOR"] = "Banned for accumulating";
$LANG["CLEANUP_THE_MEMBER"] = "The member";
$LANG["CLEANUP_HAS_BEEN_BANNED_REASON"] = "has been banned for accumulating";
$LANG["CLEANUP_HAS_BEEN_BANNED_TIMEOUT"] = "has been banned because the H&R warning period expired.";
$LANG["CLEANUP_YOUR_ACCOUNT"] = "Your account at";
$LANG["CLEANUP_HAS_BEEN_DISABLED"] = "has been disabled";
$LANG["CLEANUP_YOU_WERE_BANNED_FOLLOWING"] = "You have been banned for the following reason:";
$LANG["CLEANUP_YOU_HAVE_ACCUMULATED"] = "You have accumulated";
$LANG["CLEANUP_TIMEOUT_WARNING_H&R_EXPIRED"] = "Your warning period for H&R has expired.";
$LANG["CLEANUP_YOU_CAN_CONTACT_BY_LINK"] = "You can contact us through the site:";
$LANG["CLEANUP_BANNED_FOR_EXPIRED_H&R_WARNING_TIMEOUT"] = "Banned because the warning time for H&R has expired";
$LANG["THERE_ARE_NO_RECORDINGS"] = "There are no recordings of Hit and Run!";
$LANG["H&R"] = "HnR";
Add hitnrun.png from the first post to images/admin.
Fixes needed in Snatchlist for this to work
The completed update also checks utime against the current second, so it rarely matches and Hit and Runs are never flagged. In announce.php find
SQL_Query_exec("UPDATE LOW_PRIORITY `snatched` SET `completed` = '1' WHERE `tid` = '$torrentid' AND `uid` = '$userid' AND `utime` = '" . gmtime() . "'");
replace with
SQL_Query_exec("UPDATE `snatched` SET `completed` = '1' WHERE `tid` = $torrentid AND `uid` = $userid");
Seed time can be wrong when PHP and MySQL use different time zones, so let the database measure it. Find
UNIX_TIMESTAMP(last_action) AS ez,
replace with
UNIX_TIMESTAMP(last_action) AS ez, TIMESTAMPDIFF(SECOND, last_action, UTC_TIMESTAMP()) AS idle,
find
$elapsed = ($self['seeder'] == 'yes') ? $site_config['announce_interval'] - floor(($self['ez'] - time()) / 60) : 0;
replace with
$elapsed = ($self['seeder'] == 'yes') ? min(max(0, (int) $self['idle']), (int) $site_config['announce_interval'] * 2) : 0;
snatched.php lets any member see who downloaded a torrent. To keep that for staff, find
if ($tid > 0) {
$count_tid =
add above
if ($tid > 0 && $CURUSER["edit_users"] != "yes") show_error_msg(T_("ERROR"), T_("NO_PERMISSION"), 1);
If you also use the Seed Bonus mod
The HnR option is undone at the next cleanup unless it is marked as done. In mybonus.php find
`hnr` = 'no' WHERE `tid`
replace with
`hnr` = 'no', `done` = 'yes' WHERE `tid`
Thanks again!
If It Ain't Dutch, It Ain't Much!!