Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.59 KB | None | 0 0
  1. <?php
  2.  
  3. class Cleanup {
  4. /* Settings for cleanup */
  5. private $signup_timeout = 2;
  6. private $max_dead_torrent_time_hours = 6;
  7. private $announce_interval_minutes = 15;
  8. private $not_connectable_length = 1;
  9. private $max_inactive_user_days = 60;
  10. private $invite_code_expiration_days = 7;
  11. private $max_free_leech_days = 14;
  12. private $ratio_warning_length = 5;
  13. private $hnrwarn_expiration_days = 5;
  14. private $ratio_warning_minimum_gb = 20;
  15. private $ratio_warning_minimum_ratio = 0.5;
  16. private $move_to_archive_after_days = 30;
  17. private $delete_inactive_torrents_after_days = 7;
  18. private $delete_inactive_requests_after_days = 60;
  19. private $delete_unseeded_torrents_after_minutes = 760;
  20. private $demote_uploaders_after_days_inactive = 60;
  21. private $delete_messages_after_days = 30;
  22. private $delete_logs_after_days = 60;
  23. private $peer_deadtime;
  24. private $datetime;
  25. private $userClassPromotions;
  26.  
  27. /* Hit N Run Settings */
  28. private $hnrMaxHnrTorrents = 5;
  29. private $hnr_warning_days = 7;
  30. private $hnrMinLimitDays = 5;
  31. private $hnrSeedTimeLimitDays = 5;
  32. private $hnrPercentTolerance = 10;
  33. private $hnrDaysTolerance = 10;
  34. private $hnrImmuneMinClass = 7;
  35.  
  36. private $db;
  37. private $user;
  38. private $torrent;
  39. private $mailbox;
  40. private $log;
  41. private $adminlog;
  42. private $requests;
  43.  
  44. public function __construct($db, $user, $torrent, $log, $adminlog, $mailbox, $requests) {
  45. $this->db = $db;
  46. $this->user = $user;
  47. $this->torrent = $torrent;
  48. $this->mailbox = $mailbox;
  49. $this->log = $log;
  50. $this->adminlog = $adminlog;
  51. $this->requests = $requests;
  52.  
  53. $this->peer_deadtime = time() - floor($this->announce_interval_minutes * 60 * 1.2);
  54. $this->datetime = date("Y-m-d H:i:s");
  55.  
  56. $this->userClassPromotions = array(
  57. array(
  58. "minratio" => 1.10,
  59. "className" => Config::$userClasses[3],
  60. "classId" => 3,
  61. "minimumGigabyteUpload" => 1200,
  62. "minimumMemberDays" => 210,
  63. "perks" => L::get("CLASS_3_PERKS")),
  64. array(
  65. "minratio" => 1.10,
  66. "className" => Config::$userClasses[2],
  67. "classId" => 2,
  68. "minimumGigabyteUpload" => 300,
  69. "minimumMemberDays" => 105,
  70. "perks" => L::get("CLASS_2_PERKS")),
  71. array(
  72. "minratio" => 1.05,
  73. "className" => Config::$userClasses[1],
  74. "classId" => 1,
  75. "minimumGigabyteUpload" => 50,
  76. "minimumMemberDays" => 14,
  77. "perks" => L::get("CLASS_1_PERKS"))
  78. );
  79. }
  80.  
  81. public function run() {
  82.  
  83. if ($_SERVER['SERVER_ADDR'] != $_SERVER["REMOTE_ADDR"]) {
  84. throw new Exception(L::get("MUST_BE_RUN_BY_SERVER_ERROR"), 401);
  85. }
  86.  
  87. /* Delete dead peers and correct all seeders, leechers amounts */
  88. $this->db->query("DELETE FROM peers WHERE last_action < FROM_UNIXTIME(".$this->peer_deadtime.")");
  89.  
  90. $torrents = array();
  91. $res = $this->db->query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder");
  92. while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
  93. if ($row["seeder"] == "yes")
  94. $key = "seeders";
  95. else
  96. $key = "leechers";
  97. $torrents[$row["torrent"]][$key] = $row["c"];
  98. }
  99.  
  100. $fields = explode(":", "leechers:seeders");
  101. $res = $this->db->query("SELECT id, seeders, leechers FROM torrents");
  102. while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
  103. $id = $row["id"];
  104. $torr = array();
  105. if (!empty($torrents[$id])) {
  106. $torr = $torrents[$id];
  107. }
  108. foreach ($fields as $field) {
  109. if (!isset($torr[$field]))
  110. $torr[$field] = 0;
  111. }
  112. $update = array();
  113. foreach ($fields as $field) {
  114. if ($torr[$field] != $row[$field])
  115. $update[] = "$field = " . $torr[$field];
  116. }
  117. if (count($update)) {
  118. $this->db->query("UPDATE torrents SET " . implode(",", $update) . " WHERE id = $id");
  119. }
  120. }
  121.  
  122. /* Disabled inactive user accounts */
  123. $reason = L::get("AUTO_DISABLED_INACTIVITY");
  124. $dt = time() - $this->max_inactive_user_days * 86400;
  125. $maxclass = 6;
  126. $res = $this->db->query("SELECT id FROM users WHERE class < $maxclass AND last_access < FROM_UNIXTIME($dt) AND parkerad = 0 AND enabled = 'yes'");
  127. $text = date("Y-m-d") . " - " .$reason .= "\n";
  128. while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
  129. $this->db->query("UPDATE users SET modcomment = concat('$text', modcomment), secret = '$reason' WHERE id = " . $row["id"]);
  130. }
  131. $this->db->query("UPDATE users SET enabled = 'no' WHERE class < $maxclass AND last_access < FROM_UNIXTIME($dt) AND parkerad = 0");
  132.  
  133. /* Remove unused invit codes */
  134. $maxdt = time() - 86400 * $this->invite_code_expiration_days;
  135. $res = $this->db->query("SELECT invites.id, invites.userid, users.username, users.language FROM invites LEFT JOIN users ON invites.userid = users.id WHERE skapad < FROM_UNIXTIME($maxdt)");
  136. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  137. $this->db->query("DELETE FROM invites WHERE id = " . $arr["id"]);
  138. $this->db->query("UPDATE users SET invites = invites + 1 WHERE id = " . $arr["userid"]);
  139. $this->mailbox->sendSystemMessage($arr["userid"], L::get("UNCONFIRMED_INVITE_PM_SUBJECT", null, $arr["language"]), L::get("UNCONFIRMED_INVITE_PM_BODY", null, $arr["language"]));
  140. }
  141.  
  142. /* Remove free leech from new torrents */
  143. $dt = time() - $this->max_free_leech_days * 86400;
  144. $this->db->query("UPDATE torrents SET frileech = 0 WHERE section = 'new' AND added < FROM_UNIXTIME($dt) AND size < 16106127360");
  145.  
  146.  
  147. /* Bad ratio warning */
  148. $limit = "0";
  149. $limit = $limit*1024*1024*1024;
  150. $siteName = Config::NAME;
  151. $min_downloaded = $this->ratio_warning_minimum_gb*1024*1024*1024;
  152. $warned_until = time() + $this->ratio_warning_length*86400;
  153. $res = $this->db->query("SELECT id, username, language FROM users WHERE class = 0 AND enabled = 'yes' AND downloaded > {$min_downloaded} AND uploaded / downloaded < {$this->ratio_warning_minimum_ratio} AND warned = 'no'");
  154. $modcomment = date("Y-m-d") . " - " . L::get("RATIO_WARNING_LOG") . "\n";
  155. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  156. $this->db->query("UPDATE users SET warned = 'yes', warneduntil = FROM_UNIXTIME({$warned_until}), modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  157. $this->mailbox->sendSystemMessage($arr["id"], L::get("RATIO_WARNING_PM_SUBJECT", null, $arr["language"]), L::get("RATIO_WARNING_PM_BODY", [$this->ratio_warning_length, $siteName], $arr["language"]));
  158. $this->adminlog->create(L::get("RATIO_WARNING_ADMIN_LOG", [$arr["id"], $arr["username"], $arr["username"]]));
  159. }
  160.  
  161. /* Ban when warning expired and ratio still bad */
  162. $reasonban = L::get("BAD_RATIO_AUTO_DISABLED");
  163. $res = $this->db->query("SELECT id, ip, username, modcomment, language FROM users WHERE class = 0 AND warned = 'yes' AND warneduntil < NOW() AND enabled = 'yes' AND downloaded > $limit AND uploaded / downloaded < {$this->ratio_warning_minimum_ratio} AND donor = 'no'");
  164. $modcomment = date("Y-m-d") . " - " .L::get("BAD_RATIO_AUTO_DISABLED"). "\n";
  165. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  166. $this->db->query("UPDATE users SET enabled = 'no', secret = '$reasonban', modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  167. $this->adminlog->create(L::get("BAD_RATIO_AUTO_DISABLED_ADMIN_LOG", [$arr["id"], $arr["username"], $arr["username"]]));
  168. }
  169.  
  170. /* Remove expired warnings */
  171. $res = $this->db->query("SELECT id, language FROM users WHERE warned = 'yes' AND warneduntil < NOW() AND warneduntil <> '0000-00-00 00:00:00'") or sqlerr(__FILE__,__LINE__);
  172. $modcomment = date("Y-m-d") . " - Warning removed!\n";
  173. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  174. $this->db->query("UPDATE users SET warned = 'no', warneduntil = '0000-00-00 00:00:00', modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  175. $this->mailbox->sendSystemMessage($arr["id"], L::get("WARNING_REMOVED_PM_SUBJECT", null, $arr["language"]), L::get("WARNING_AUTO_REMOVED_PM_BODY", null, $arr["language"]));
  176. }
  177.  
  178. /* Move torrents from New to Archive */
  179. $dt = time() - $this->move_to_archive_after_days * 86400;
  180. $res = $this->db->query("SELECT id FROM torrents WHERE added < FROM_UNIXTIME({$dt}) AND section = 'new'");
  181. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  182. $this->db->query("UPDATE torrents SET section = 'archive' WHERE id = ". $arr["id"]);
  183. $this->db->query("UPDATE peers SET section = 'new' WHERE torrent = ". $arr["id"]);
  184. }
  185.  
  186. /* Delete inactive torrents */
  187. $dt = time() - $this->delete_inactive_torrents_after_days * 86400;
  188. //$dt = time() - $this->delete_inactive_torrents_after_days * 600;
  189. $res = $this->db->query("SELECT id, name, reqid FROM torrents WHERE last_action < FROM_UNIXTIME({$dt}) AND seeders = 0 AND leechers = 0 AND section = 'archive'");
  190. //$res = $this->db->query("SELECT id, name, reqid FROM torrents WHERE last_action < NOW() - INTERVAL 1 DAY AND seeders = 0 AND leechers = 0 AND section = 'archive'");
  191. /* Prevent deletion of "all" torrents if site has been offline or similiar */
  192. if ($res->rowCount() < 100) {
  193. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  194. $this->torrent->delete($arr["id"], L::get("AUTO_DELETE_INACTIVE_TORRENT", [$this->delete_inactive_torrents_after_days]));
  195. }
  196. } else {
  197. $this->adminlog->create(L::get("AUTO_DELETE_INACTIVE_TORRENTS_PREVENTED", [$res->rowCount()]));
  198. }
  199.  
  200. /* Delete new unseeded torrents*/
  201.  
  202.  
  203. $dt = time() - $this->delete_unseeded_torrents_after_minutes * 60;
  204. $dtmax = time() - 86400;
  205. $res = $this->db->query("SELECT id, name, reqid FROM torrents WHERE added < FROM_UNIXTIME({$dt}) AND added > FROM_UNIXTIME({$dtmax}) AND seeders = 0 AND leechers > 0 AND reqid = 0");
  206. /* Prevent deletion of lots of torrents if site has been offline or similiar*/
  207.  
  208. if ($res->rowCount() < 10) {
  209. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  210. $this->torrent->delete($arr["id"], L::get("AUTO_DELETE_UNSEEDED_TORRENTS", [$this->delete_unseeded_torrents_after_minutes]), 1);
  211. }
  212. } else {
  213. $this->adminlog->create(L::get("AUTO_DELETE_INACTIVE_TORRENTS_PREVENTED", [$res->rowCount()]));
  214. }
  215.  
  216.  
  217. /* Delete inactive requests */
  218. $dt = time() - $this->delete_inactive_requests_after_days * 86400;
  219. $res = $this->db->query("SELECT id FROM requests WHERE added < FROM_UNIXTIME({$dt}) AND filled = 0");
  220. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  221. $this->requests->delete($arr["id"], L::get("REQUEST_NOT_FILLED", [$this->delete_inactive_requests_after_days]));
  222. }
  223.  
  224. /* Demote inactive Uploaders */
  225. $dt = time() - $this->demote_uploaders_after_days_inactive * 86400;
  226. $res = $this->db->query("SELECT users.id, users.username, users.language FROM `users` WHERE class = 6 AND (SELECT added FROM torrents WHERE owner = users.id ORDER BY `added` DESC LIMIT 1) < FROM_UNIXTIME({$dt})");
  227. $modcomment = date("Y-m-d") . " - ".L::get("UPLOADED_AUTO_DOWNGRADED").".\n";
  228. while ($arr = $res->fetch(PDO::FETCH_ASSOC)){
  229. $this->db->query("UPDATE users SET class = 1, modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  230. $this->adminlog->create(L::get("UPLOADED_AUTO_DOWNGRADED_ADMIN_LOG", [$arr["id"], $arr["username"], $arr["username"]]));
  231. $this->mailbox->sendSystemMessage($arr["id"], ucfirst(L::get("STATUS_DOWNGRADED", null, $arr["language"])), L::get("UPLOADED_AUTO_DOWNGRADED_PM_BODY", [$this->demote_uploaders_after_days_inactive], $arr["language"]));
  232. }
  233.  
  234. /* Delete old inbox messages */
  235. $dt = time() - $this->delete_messages_after_days * 86400;
  236. $this->db->query("DELETE FROM messages WHERE last < FROM_UNIXTIME({$dt}) AND saved = 0 AND unread = 'no';");
  237.  
  238. /* Delete old logs */
  239. $dt = time() - $this->delete_logs_after_days * 86400;
  240. $this->db->query("DELETE FROM sitelog WHERE added < FROM_UNIXTIME({$dt})");
  241.  
  242. /* Give gold coin icon to users invited users with high leech bonus */
  243. $res = $this->db->query("SELECT invited_by, username FROM users WHERE leechbonus >= 25 AND invited_by > 1");
  244. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  245. $who = $this->db->query("SELECT id, coin, language FROM users WHERE id = " . $arr["invited_by"]);
  246. while ($arr2 = $who->fetch(PDO::FETCH_ASSOC)) {
  247. if ($arr2["coin"] == 0) {
  248. $this->db->query("UPDATE users SET coin = 1 WHERE id = ". $arr2["id"]);
  249. $this->mailbox->sendSystemMessage($arr2["id"], L::get("GOLD_COIN_PM_SUBJECT", null, $arr2["language"]), L::get("GOLD_COIN_PM_BODY", [$arr["username"]], $arr2["language"]));
  250. }
  251. }
  252. }
  253.  
  254. /* Update which suggestions that are "hot" */
  255. $this->db->query('UPDATE suggestions SET hotpoints = 0');
  256. $res = $this->db->query('SELECT id, suggestid FROM `topics` WHERE suggestid > 0');
  257. $dt = time() - 30 * 86400;
  258. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  259. $re = $this->db->query("SELECT COUNT(*) FROM `posts` WHERE topicid = ".$arr["id"]." AND added > FROM_UNIXTIME({$dt})");
  260. $re = $re->fetch();
  261. $this->db->query('UPDATE suggestions SET hotpoints = ' . $re[0] . ' WHERE id = ' . $arr["suggestid"]);
  262. }
  263.  
  264. /* Update forum posts amount on suggestions */
  265. $res = $this->db->query("SELECT id, topicid FROM suggestions");
  266. while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
  267. $t = $this->db->query("SELECT COUNT(*) FROM posts WHERE topicid = " . $row["topicid"]);
  268. $r = $t->fetch();
  269. if ($r[0] > 0) {
  270. $this->db->query("UPDATE suggestions SET comments = ".($r[0] - 1)." WHERE id = " . $row["id"]);
  271. }
  272. }
  273.  
  274. foreach ($this->userClassPromotions as $class) {
  275. $limit = $class["minimumGigabyteUpload"] * 1024*1024*1024;
  276. $dt = time() - 86400 * $class["minimumMemberDays"];
  277. $message = L::get("AUTO_PROMOTED_PM_BODY", [$class["className"]]);
  278. if ($class["perks"]) {
  279. $message .= "\n\n". L::get("AUTO_PROMOTED_PM_PERKS", [$class["className"]]) . "\n\n". $class["perks"];
  280. }
  281. $modcomment = date("Y-m-d") . " - " . L::get("AUTO_PROMOTED_LOG", [$class["className"]]) ."\n";
  282.  
  283. $res = $this->db->query("SELECT id, class, doljuploader, title, language FROM users WHERE class < ".$class["classId"]." AND uploaded >= ".$limit." AND uploaded / downloaded >= ".$class["minratio"]." AND added < FROM_UNIXTIME({$dt})");
  284. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  285. $this->mailbox->sendSystemMessage($arr["id"], L::get("AUTO_PROMOTED_PM_SUBJECT", [$class["className"]], $arr["language"]), $message);
  286. $this->db->query("UPDATE users SET class = ".$class["classId"].", modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  287. if ($class["classId"] >= 2) {
  288. $this->db->query('DELETE FROM iplog WHERE userid = ' . $arr["id"]);
  289. }
  290. if ($class["classId"] >= 3) {
  291. $this->db->query('UPDATE users SET invites = invites + 2, reqslots = 3 WHERE id = ' . $arr["id"]);
  292. }
  293. }
  294. }
  295.  
  296. /* Remove expired donors */
  297. $res = $this->db->query("SELECT * FROM users WHERE donate_end != '0000-00-00 00:00:00' AND donate_end < NOW()");
  298. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  299. $this->db->query("UPDATE users SET class = last_class, donor = 'no', donate_end = '0000-00-00 00:00:00' WHERE id = " . $arr["id"]);
  300. $this->db->query('DELETE FROM donated WHERE userid = ' . $arr["id"]);
  301. $this->mailbox->sendSystemMessage($arr["id"], L::get("DONATION_EXPIRED_PM_SUBJECT", null, $arr["language"]), L::get("DONATION_EXPIRED_PM_BODY", null, $arr["language"]));
  302. }
  303.  
  304. /*remove all hnr for users if donated*/
  305. $res = $this->db->query("SELECT id FROM users WHERE donor = 'yes'");
  306. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  307. $this->db->query("UPDATE snatch SET hnr = 'no', immune = 'yes' WHERE userid = " . $arr["id"]);
  308. }
  309.  
  310. /* Flag snatches as "immune" for users that have donate_end > now()*/
  311. $finishedAt = time() - 60*60;
  312.  
  313. //$this->db->query("UPDATE snatch JOIN users ON snatch.userid = users.id SET snatch.immune = 'yes' WHERE users.donate_end > NOW() AND snatch.finishedat > FROM_UNIXTIME({$finishedAt})");
  314.  
  315. $retry=0;
  316. $notdone=TRUE;
  317. while( $notdone && $retry<3 ) {
  318. try {
  319. $this->db->query("UPDATE snatch JOIN users ON snatch.userid = users.id SET snatch.immune = 'yes' WHERE users.donate_end > NOW() AND snatch.finishedat > FROM_UNIXTIME({$finishedAt})");
  320.  
  321. $notdone=FALSE;
  322. } catch( Exception $e ) {
  323. $retry++;
  324. }
  325. }
  326. if( 3 == $retry ) {
  327. throw new Exception("Try later, sorry, too much shite");
  328. }
  329.  
  330. /* Flag snatches as Hit n Runs */
  331. /*
  332. * 1:1 ratio Or 7 days
  333. * 10 days tolerance
  334. * 10% download tolerance
  335. */
  336. $seedTime = 60 * 60 * 24 * $this->hnrSeedTimeLimitDays;
  337. $finishedAt = time() - 86400 * $this->hnrDaysTolerance;
  338. // $this->db->query("UPDATE snatch JOIN torrents ON snatch.torrentid = torrents.id JOIN users ON snatch.userid = users.id SET snatch.hnr = 'no' WHERE snatch.hnr = 'yes' AND snatch.seeding = 'yes' OR (snatch.uploaded > snatch.downloaded OR snatch.seedtime >= {$seedTime})");
  339. // $this->db->query("UPDATE snatch JOIN torrents ON snatch.torrentid = torrents.id JOIN users ON snatch.userid = users.id SET snatch.hnr = 'no' WHERE snatch.hnr = 'yes' AND snatch.seeding = 'no' AND (snatch.uploaded > snatch.downloaded OR snatch.seedtime >= {$seedTime})");
  340. //$this->db->query("UPDATE snatch JOIN torrents ON snatch.torrentid = torrents.id JOIN users ON snatch.userid = users.id SET snatch.hnr = 'yes' WHERE snatch.timesCompleted > 0 AND snatch.uploaded < snatch.downloaded AND snatch.seedtime < {$seedTime} AND snatch.finishedat != '0000-00-00 00:00:00' AND snatch.seeding = 'no' AND snatch.finishedat < FROM_UNIXTIME({$finishedAt}) AND snatch.immune = 'no' AND snatch.downloaded > torrents.size/{$this->hnrPercentTolerance}");
  341.  
  342. $retry=0;
  343. $notdone=TRUE;
  344. while( $notdone && $retry<3 ) {
  345. try {
  346. $this->db->query("UPDATE snatch JOIN torrents ON snatch.torrentid = torrents.id JOIN users ON snatch.userid = users.id SET snatch.hnr = 'no' WHERE snatch.hnr = 'yes' AND snatch.seeding = 'yes' OR (snatch.uploaded > snatch.downloaded OR snatch.seedtime >= {$seedTime})");
  347.  
  348. $notdone=FALSE;
  349. } catch( Exception $e ) {
  350. $retry++;
  351. }
  352. }
  353. if( 3 == $retry ) {
  354. throw new Exception("Try later, sorry, too much shite");
  355. }
  356.  
  357. $retry=0;
  358. $notdone=TRUE;
  359. while( $notdone && $retry<3 ) {
  360. try {
  361. $this->db->query("UPDATE snatch JOIN torrents ON snatch.torrentid = torrents.id JOIN users ON snatch.userid = users.id SET snatch.hnr = 'no' WHERE snatch.hnr = 'yes' AND snatch.seeding = 'no' AND (snatch.uploaded > snatch.downloaded OR snatch.seedtime >= {$seedTime})");
  362.  
  363. $notdone=FALSE;
  364. } catch( Exception $e ) {
  365. $retry++;
  366. }
  367. }
  368. if( 3 == $retry ) {
  369. throw new Exception("Try later, sorry, too much shite");
  370. }
  371.  
  372. $retry=0;
  373. $notdone=TRUE;
  374. while( $notdone && $retry<3 ) {
  375. try {
  376. $this->db->query("UPDATE snatch JOIN torrents ON snatch.torrentid = torrents.id JOIN users ON snatch.userid = users.id SET snatch.hnr = 'yes' WHERE snatch.timesCompleted > 0 AND snatch.uploaded < snatch.downloaded AND snatch.seedtime < {$seedTime} AND snatch.finishedat != '0000-00-00 00:00:00' AND snatch.seeding = 'no' AND snatch.finishedat < FROM_UNIXTIME({$finishedAt}) AND snatch.immune = 'no' AND snatch.downloaded > torrents.size/{$this->hnrPercentTolerance}");
  377.  
  378. $notdone=FALSE;
  379. } catch( Exception $e ) {
  380. $retry++;
  381. }
  382. }
  383. if( 3 == $retry ) {
  384. throw new Exception("Try later, sorry, too much shite");
  385. }
  386.  
  387. /* HNR-Warn users with 5 or more Hit N Runs for x days */
  388. $warned_until = time() + $this->hnr_warning_days * 86400;
  389. $modcomment = date("Y-m-d") . " - " . L::get("HNR_WARNING_LOG") . "\n";
  390. $res = $this->db->query("SELECT users.id, users.language FROM users WHERE users.enabled = 'yes' AND users.class < {$this->hnrImmuneMinClass} AND users.hnr_warned = 'no' AND (SELECT COUNT(*) FROM snatch WHERE snatch.userid = users.id AND snatch.hnr = 'yes') > " . $this->hnrMaxHnrTorrents);
  391. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  392. $this->db->query("UPDATE users SET hnr_warned = 'yes', hnr_warned_until = FROM_UNIXTIME({$warned_until}), modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  393. $this->mailbox->sendSystemMessage($arr["id"], L::get("HNR_WARNING_PM_SUBJECT", null, $arr["language"]), L::get("HNR_WARNING_PM_BODY", [$this->hnr_warning_days, $this->hnrMaxHnrTorrents, $siteName], $arr["language"]));
  394. }
  395.  
  396. /* Ban HnR-warned users*/
  397. //$res = $this->db->query("SELECT users.id, users.username, users.passkey, users.modcomment, users.language FROM users WHERE users.hnr_warned = 'yes' AND users.hnr_warned_until < NOW() AND (SELECT COUNT(*) FROM snatch JOIN torrents ON snatch.torrentid = torrents.id WHERE snatch.userid = users.id AND snatch.timesCompleted > 0 AND snatch.uploaded < snatch.downloaded AND snatch.seedtime < {$seedTime} AND snatch.finishedat != '0000-00-00 00:00:00' AND snatch.finishedat < FROM_UNIXTIME({$finishedAt}) AND snatch.immune = 'no' AND snatch.downloaded > torrents.size/{$this->hnrPercentTolerance}) > " . $this->hnrMaxHnrTorrents);
  398. //$modcomment = date("Y-m-d") . " - " .L::get("BAD_HNR_AUTO_DISABLED"). "\n";
  399. //while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  400. // $banReason = L::get("BAD_HNR_AUTO_DISABLED", null, $arr["language"]);
  401. // $this->db->query("UPDATE users SET enabled = 'no', modcomment = concat('{$modcomment}', modcomment), hnr_warned = 'no', hnr_warned_until = '0000-00-00 00:00:00', secret = '{$banReason}' WHERE id = " . $arr["id"]);
  402. // $this->adminlog->create(L::get("BAD_HNR_AUTO_DISABLED_ADMIN_LOG", [$arr["id"], $arr["username"], $arr["username"]]));
  403. //}
  404.  
  405. /* Download Ban HnR-warned users / disable leeching*/
  406. $res = $this->db->query("SELECT users.id, users.username, users.passkey, users.modcomment, users.language FROM users WHERE users.enabled = 'yes' AND users.downloadban < 1 AND users.hnr_warned = 'yes' AND users.hnr_warned_until < NOW() AND (SELECT COUNT(*) FROM snatch JOIN torrents ON snatch.torrentid = torrents.id WHERE snatch.userid = users.id AND snatch.timesCompleted > 0 AND snatch.uploaded < snatch.downloaded AND snatch.seedtime < {$seedTime} AND snatch.finishedat != '0000-00-00 00:00:00' AND snatch.finishedat < FROM_UNIXTIME({$finishedAt}) AND snatch.immune = 'no' AND snatch.downloaded > torrents.size/{$this->hnrPercentTolerance}) > " . $this->hnrMaxHnrTorrents);
  407. $modcomment = date("Y-m-d") . " - " .L::get("BAD_HNR_AUTO_DISABLED_LEECH"). "\n";
  408. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  409. $banReason = L::get("BAD_HNR_AUTO_DISABLED_LEECH", null, $arr["language"]);
  410. $this->db->query("UPDATE users SET modcomment = concat('{$modcomment}', modcomment), downloadban = '1', hnr_warned_until = FROM_UNIXTIME({$warned_until}), secret = '{$banReason}' WHERE id = " . $arr["id"]);
  411. $this->adminlog->create(L::get("BAD_HNR_AUTO_DISABLED_ADMIN_LOG_LEECH", [$arr["id"], $arr["username"], $arr["username"]]));
  412. }
  413.  
  414. /* Delete HnR marked torrents that are removed on site, lucky users :D*/
  415. $res = $this->db->query("SELECT snatch.torrentid FROM snatch LEFT JOIN torrents ON snatch.torrentid = torrents.id WHERE torrents.id is NULL AND snatch.hnr = 'yes'");
  416. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  417. $this->db->query("DELETE FROM snatch WHERE snatch.torrentid = " . $arr["torrentid"]);
  418. }
  419.  
  420. /* Remove expired HNR-warning */
  421. $res = $this->db->query("SELECT users.id, users.language FROM users WHERE users.enabled = 'yes' AND users.hnr_warned = 'yes' AND users.class < {$this->hnrImmuneMinClass} AND (SELECT COUNT(*) FROM snatch WHERE snatch.userid = users.id AND snatch.hnr = 'yes') <= " . $this->hnrMaxHnrTorrents);
  422. $modcomment = date("Y-m-d") . " - " . L::get("HNR_WARNING_AUTO_REMOVED_PM_SUBJECT") . "\n";
  423. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  424. $this->db->query("UPDATE users SET hnr_warned = 'no', downloadban = '0', hnr_warned_until = '0000-00-00 00:00:00', modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  425. $this->mailbox->sendSystemMessage($arr["id"], L::get("HNR_WARNING_AUTO_REMOVED_PM_SUBJECT", null, $arr["language"]), L::get("HNR_WARNING_AUTO_REMOVED_PM_BODY", null, $arr["language"]));
  426. }
  427.  
  428. /* Demote users with bad ratio */
  429. $modcomment = date("Y-m-d") . " - ".L::get("AUTO_DEMOTED_TO_CLASS_1")."\n";
  430. $res = $this->db->query("SELECT id, class, language FROM users WHERE class > 0 AND class < 4 AND uploaded / downloaded < 0.90");
  431. while ($arr = $res->fetch(PDO::FETCH_ASSOC)) {
  432. $this->db->query("UPDATE users SET class = 0, modcomment = concat('{$modcomment}', modcomment) WHERE id = " . $arr["id"]);
  433. $this->mailbox->sendSystemMessage($arr["id"], L::get("DEMOTED_TO_CLASS_1_PM_SUBJECT", null, $arr["language"]), L::get("DEMOTED_TO_CLASS_1_PM_BODY", null, $arr["language"]));
  434. }
  435.  
  436. /* Update peer record */
  437. $peers = $this->db->query("SELECT COUNT(DISTINCT userid, torrent) FROM peers");
  438. $peers = $peers->fetch();
  439. $peersRecord = $this->db->query("SELECT value_i FROM settings WHERE arg = 'peers_rekord'");
  440. $peersRecord = $peersRecord->fetch();
  441. if ($peers[0] > $peersRecord[0]) {
  442. $sth = $this->db->prepare("UPDATE settings SET value_i = ? WHERE arg = 'peers_rekord'");
  443. $sth->bindParam(1, $peers[0], PDO::PARAM_INT);
  444. $sth->execute();
  445. }
  446. }
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement