function cleanUp() { global $dbhost, $dbuser, $dbpass, $database; require_once "config.php"; require_once "funcsv2.php"; $summaryupdate = array(); // Non-persistant: we lock tables! $db = mysql_connect($dbhost, $dbuser, $dbpass) or die("<p class=\"error\">Tracker error: can't connect to database - " . mysql_error() . "</p>"); mysql_select_db($database) or die("<p class=\"error\">Tracker error: can't open database {$database} - " . mysql_error() . "</p>"); if (isset($_GET["nolock"])) { $locking = false; } else { $locking = true; } // Assumes success if ($locking) { quickQuery("LOCK TABLES BTPHP_summary WRITE, BTPHP_namemap READ"); } ?> <table class="torrentlist" cellspacing="5"> <!-- Column Headers --> <tr> <th>Name/Hash</th> <th>Leechs</th> <th>Seeds</th> <th>Bytes Transfered</th> <th>Stale clients</th> <th>Peer Cache</th> </tr> <tr> <td colspan="5" class="nodata"></td> </tr> <?php $results = mysql_query("SELECT BTPHP_summary.info_hash, seeds, leechers, dlbytes, BTPHP_namemap.filename FROM BTPHP_summary LEFT JOIN BTPHP_namemap ON BTPHP_summary.info_hash = BTPHP_namemap.info_hash"); $i = 0; while ($row = mysql_fetch_row($results)) { $writeout = "row" . $i % 2; list($hash, $seeders, $leechers, $bytes, $filename) = $row; if ($locking) { if ($GLOBALS["peercaching"]) { quickQuery("LOCK TABLES x{$hash} WRITE, y{$hash} WRITE, summary WRITE"); } else { quickQuery("LOCK TABLES x{$hash} WRITE, summary WRITE"); } } $results2 = mysql_query("SELECT status, COUNT(status) from x{$hash} GROUP BY status"); echo "<tr class=\"{$writeout}\"><td>"; if (!is_null($filename)) { echo $filename; } else { echo $hash; } echo "</td>"; if (!$results2) { echo "<td colspan=\"4\">Unable to process: " . mysql_error() . "</td></tr>"; continue; } $counts = array(); while ($row = mysql_fetch_row($results2)) { $counts[$row[0]] = $row[1]; } if (!isset($counts["leecher"])) { $counts["leecher"] = 0; } if (!isset($counts["seeder"])) { $counts["seeder"] = 0; } if ($counts["leecher"] != $leechers) { quickQuery("UPDATE BTPHP_summary SET leechers=" . $counts["leecher"] . " WHERE info_hash=\"{$hash}\""); echo "<td>{$leechers} -> " . $counts["leecher"] . "</td>"; } else { echo "<td align=center>{$leechers}</td>"; } if ($counts["seeder"] != $seeders) { quickQuery("UPDATE BTPHP_summary SET seeds=" . $counts["seeder"] . " WHERE info_hash=\"{$hash}\""); echo "<td align=center>{$seeders} -> " . $counts["seeder"] . "</td>"; } else { echo "<td align=center>{$seeders}</td>"; } // echo "<td align=center>$finished</td>"; if ($bytes < 0) { quickQuery("UPDATE BTPHP_summary SET dlbytes=0 WHERE info_hash=\"{$hash}\""); echo "<td>{$bytes} -> Zero</td>"; } else { echo "<td align=center>" . round($bytes / 1048576 / 1024, 3) . " GB</td>"; } myTrashCollector($hash, $report_interval, time(), $writeout); echo "</td><td>"; if ($GLOBALS["peercaching"]) { $result = mysql_query("SELECT x{$hash}.sequence FROM x{$hash} LEFT JOIN y{$hash} ON x{$hash}.sequence=y{$hash}.sequence WHERE y{$hash}.sequence IS NULL") or die(mysql_error()); if (mysql_num_rows($result) > 0) { echo "Added ", mysql_num_rows($result); $row = array(); while ($data = mysql_fetch_row($result)) { $row[] = "sequence=\"{$data[0]}\""; } $where = implode(" OR ", $row); $query = mysql_query("SELECT * FROM x{$hash} WHERE {$where}"); while ($row = mysql_fetch_assoc($query)) { $compact = mysql_escape_string(pack('Nn', ip2long($row["ip"]), $row["port"])); $peerid = mysql_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . '7:peer id20:' . hex2bin($row["peer_id"]) . "4:porti{$row["port"]}e"); $no_peerid = mysql_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . "4:porti{$row["port"]}e"); mysql_query("INSERT INTO y{$hash} SET sequence=\"{$row["sequence"]}\", compact=\"{$compact}\", with_peerid=\"{$peerid}\", without_peerid=\"{$no_peerid}\""); } } else { echo "Added: none"; } $result = mysql_query("SELECT y{$hash}.sequence FROM y{$hash} LEFT JOIN x{$hash} ON y{$hash}.sequence=x{$hash}.sequence WHERE x{$hash}.sequence IS NULL"); if (mysql_num_rows($result) > 0) { echo ", Deleted: ", mysql_num_rows($result); $row = array(); while ($data = mysql_fetch_row($result)) { $row[] = "sequence=\"{$data[0]}\""; } $where = implode(" OR ", $row); $query = mysql_query("DELETE FROM y{$hash} WHERE {$where}"); } else { echo "<br>Deleted: none"; } } else { echo "N/A"; } echo "</td>"; echo "</tr>\n"; $i++; // Disabled because it's kinda not that important. // quickQuery("OPTIMIZE TABLE x$hash"); if ($locking) { quickQuery("UNLOCK TABLES"); } // Finally, it's time to do stuff to the summary table. if (!empty($summaryupdate)) { $stuff = ""; foreach ($summaryupdate as $column => $value) { $stuff .= ', ' . $column . ($value[1] ? "=" : "={$column}+") . $value[0]; } mysql_query("UPDATE BTPHP_summary SET " . substr($stuff, 1) . " WHERE info_hash=\"{$hash}\""); $summaryupdate = array(); } } }
$counts["seeder"] = 0; } if ($counts["leecher"] != $leechers) { quickQuery("UPDATE " . $prefix . "summary SET leechers=" . $counts["leecher"] . " WHERE info_hash=\"{$hash}\""); } if ($counts["seeder"] != $seeders) { quickQuery("UPDATE " . $prefix . "summary SET seeds=" . $counts["seeder"] . " WHERE info_hash=\"{$hash}\""); } if ($counts["leecher"] == 0) { //If there are no leechers, set the speed to zero quickQuery("UPDATE " . $prefix . "summary set speed=0 WHERE info_hash=\"{$hash}\""); } if ($bytes < 0) { quickQuery("UPDATE " . $prefix . "summary SET dlbytes=0 WHERE info_hash=\"{$hash}\""); } myTrashCollector($hash, $report_interval, time(), $writeout); $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>"); if (mysql_num_rows($result) > 0) { $row = array(); while ($data = mysql_fetch_row($result)) { $row[] = "sequence=\"{$data[0]}\""; } $where = implode(" OR ", $row); $query = mysql_query("SELECT * FROM " . $prefix . "x{$hash} WHERE {$where}"); while ($row = mysql_fetch_assoc($query)) { $compact = mysql_escape_string(pack('Nn', ip2long($row["ip"]), $row["port"])); $peerid = mysql_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . '7:peer id20:' . hex2bin($row["peer_id"]) . "4:porti{$row["port"]}e"); $no_peerid = mysql_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . "4:porti{$row["port"]}e"); mysql_query("INSERT INTO " . $prefix . "y{$hash} SET sequence=\"{$row["sequence"]}\", compact=\"{$compact}\", with_peerid=\"{$peerid}\", without_peerid=\"{$no_peerid}\""); } }