| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,126 @@ |
| 1 |
+<?php |
|
| 2 |
+require_once("config.php");
|
|
| 3 |
+require_once("funcsv2.php");
|
|
| 4 |
+ |
|
| 5 |
+$summaryupdate = array(); |
|
| 6 |
+ |
|
| 7 |
+// Non-persistant: we lock tables! |
|
| 8 |
+$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(errorMessage() . "Tracker error: can't connect to database - ".mysql_error() . "</p>"); |
|
| 9 |
+mysql_select_db($database) or die(errorMessage() . "Tracker error: can't open database $database - ".mysql_error() . "</p>"); |
|
| 10 |
+ |
|
| 11 |
+ |
|
| 12 |
+quickQuery("LOCK TABLES ".$prefix."summary WRITE, ".$prefix."namemap READ");
|
|
| 13 |
+ |
|
| 14 |
+$results = mysql_query("SELECT ".$prefix."summary.info_hash, seeds, leechers, dlbytes, ".$prefix."namemap.filename FROM ".$prefix."summary LEFT JOIN ".$prefix."namemap ON ".$prefix."summary.info_hash = ".$prefix."namemap.info_hash");
|
|
| 15 |
+ |
|
| 16 |
+$i = 0; |
|
| 17 |
+ |
|
| 18 |
+while ($row = mysql_fetch_row($results)) |
|
| 19 |
+{
|
|
| 20 |
+ $writeout = "row" . $i % 2; |
|
| 21 |
+ list($hash, $seeders, $leechers, $bytes, $filename) = $row; |
|
| 22 |
+ if (isset($locking) && $locking) |
|
| 23 |
+ {
|
|
| 24 |
+ //peercaching ALWAYS on |
|
| 25 |
+ quickQuery("LOCK TABLES ".$prefix."x$hash WRITE, ".$prefix."y$hash WRITE, ".$prefix."summary WRITE");
|
|
| 26 |
+ } |
|
| 27 |
+ $results2 = mysql_query("SELECT status, COUNT(status) FROM ".$prefix."x$hash GROUP BY status");
|
|
| 28 |
+ |
|
| 29 |
+ if (!$results2) |
|
| 30 |
+ {
|
|
| 31 |
+ //unable to process |
|
| 32 |
+ continue; |
|
| 33 |
+ } |
|
| 34 |
+ |
|
| 35 |
+ $counts = array(); |
|
| 36 |
+ while ($row = mysql_fetch_row($results2)) |
|
| 37 |
+ $counts[$row[0]] = $row[1]; |
|
| 38 |
+ if (!isset($counts["leecher"])) |
|
| 39 |
+ $counts["leecher"] = 0; |
|
| 40 |
+ if (!isset($counts["seeder"])) |
|
| 41 |
+ $counts["seeder"] = 0; |
|
| 42 |
+ |
|
| 43 |
+ if ($counts["leecher"] != $leechers) |
|
| 44 |
+ quickQuery("UPDATE ".$prefix."summary SET leechers=".$counts["leecher"]." WHERE info_hash=\"$hash\"");
|
|
| 45 |
+ |
|
| 46 |
+ if ($counts["seeder"] != $seeders) |
|
| 47 |
+ quickQuery("UPDATE ".$prefix."summary SET seeds=".$counts["seeder"]." WHERE info_hash=\"$hash\"");
|
|
| 48 |
+ |
|
| 49 |
+ if ($counts["leecher"] == 0) |
|
| 50 |
+ {
|
|
| 51 |
+ //If there are no leechers, set the speed to zero |
|
| 52 |
+ quickQuery("UPDATE ".$prefix."summary set speed=0 WHERE info_hash='$hash'");
|
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ |
|
| 56 |
+ if ($bytes < 0) |
|
| 57 |
+ quickQuery("UPDATE ".$prefix."summary SET dlbytes=0 WHERE info_hash='$hash'");
|
|
| 58 |
+ |
|
| 59 |
+ myTrashCollector($hash, $report_interval, time(), $writeout); |
|
| 60 |
+ |
|
| 61 |
+ $result = mysql_query("SELECT ".$prefix."x$hash.sequence FROM ".$prefix."x$hash LEFT JOIN ".$prefix."y$hash ON ".$prefix."x$hash.sequence = ".$prefix."y$hash.sequence WHERE ".$prefix."y$hash.sequence IS NULL") or die(errorMessage() . "" . mysql_error() . "</p>");
|
|
| 62 |
+ if (mysql_num_rows($result) > 0) |
|
| 63 |
+ {
|
|
| 64 |
+ $row = array(); |
|
| 65 |
+ |
|
| 66 |
+ while ($data = mysql_fetch_row($result)) |
|
| 67 |
+ $row[] = "sequence=\"${data[0]}\"";
|
|
| 68 |
+ $where = implode(" OR ", $row);
|
|
| 69 |
+ $query = mysql_query("SELECT * FROM ".$prefix."x$hash WHERE $where");
|
|
| 70 |
+ |
|
| 71 |
+ while ($row = mysql_fetch_assoc($query)) |
|
| 72 |
+ {
|
|
| 73 |
+ $compact = mysql_real_escape_string(pack('Nn', ip2long($row["ip"]), $row["port"]));
|
|
| 74 |
+ $peerid = mysql_real_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . '7:peer id20:' . hex2bin($row["peer_id"]) . "4:porti{$row["port"]}e");
|
|
| 75 |
+ $no_peerid = mysql_real_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . "4:porti{$row["port"]}e");
|
|
| 76 |
+ mysql_query("INSERT INTO ".$prefix."y$hash SET sequence='{$row["sequence"]}', compact='$compact', with_peerid='$peerid', without_peerid='$no_peerid'");
|
|
| 77 |
+ } |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ $result = mysql_query("SELECT ".$prefix."y$hash.sequence FROM ".$prefix."y$hash LEFT JOIN ".$prefix."x$hash ON ".$prefix."y$hash.sequence = ".$prefix."x$hash.sequence WHERE ".$prefix."x$hash.sequence IS NULL");
|
|
| 81 |
+ if (mysql_num_rows($result) > 0) |
|
| 82 |
+ {
|
|
| 83 |
+ $row = array(); |
|
| 84 |
+ |
|
| 85 |
+ while ($data = mysql_fetch_row($result)) |
|
| 86 |
+ $row[] = "sequence=\"${data[0]}\"";
|
|
| 87 |
+ $where = implode(" OR ", $row);
|
|
| 88 |
+ $query = mysql_query("DELETE FROM ".$prefix."y$hash WHERE $where");
|
|
| 89 |
+ } |
|
| 90 |
+ |
|
| 91 |
+ |
|
| 92 |
+ $i ++; |
|
| 93 |
+ |
|
| 94 |
+ quickQuery("UNLOCK TABLES");
|
|
| 95 |
+ |
|
| 96 |
+ //Repair tables, is this necessary? Sometimes the tables crash... |
|
| 97 |
+ //Can't repair table if locked? |
|
| 98 |
+ //quickQuery("REPAIR Table x$hash");
|
|
| 99 |
+ //quickQuery("REPAIR Table y$hash");
|
|
| 100 |
+ |
|
| 101 |
+ // Finally, it's time to do stuff to the summary table. |
|
| 102 |
+ if (!empty($summaryupdate)) |
|
| 103 |
+ {
|
|
| 104 |
+ $stuff = ""; |
|
| 105 |
+ foreach ($summaryupdate as $column => $value) |
|
| 106 |
+ {
|
|
| 107 |
+ $stuff .= ', '.$column. ($value[1] ? "=" : "=$column+") . $value[0]; |
|
| 108 |
+ } |
|
| 109 |
+ mysql_query("UPDATE ".$prefix."summary SET ".substr($stuff, 1)." WHERE info_hash=\"$hash\"");
|
|
| 110 |
+ $summaryupdate = array(); |
|
| 111 |
+ } |
|
| 112 |
+ |
|
| 113 |
+} |
|
| 114 |
+ |
|
| 115 |
+ |
|
| 116 |
+function myTrashCollector($hash, $timeout, $now, $writeout) |
|
| 117 |
+{
|
|
| 118 |
+ require("config.php");
|
|
| 119 |
+ $peers = loadLostPeers($hash, $timeout); |
|
| 120 |
+ for ($i=0; $i < $peers["size"]; $i++) {
|
|
| 121 |
+ killPeer($peers[$i]["peer_id"], $hash, $peers[$i]["bytes"], $peers[$i]); |
|
| 122 |
+ } |
|
| 123 |
+ quickQuery("UPDATE ".$prefix."summary SET lastcycle='$now' WHERE info_hash='$hash'");
|
|
| 124 |
+} |
|
| 125 |
+ |
|
| 126 |
+?> |
|
| 0 | 127 |
\ No newline at end of file |