Browse code

Import from the old rivettracker git repository at sourceforge (amisaph/amisapphire branch)

Clarissa Walker (ami-sapphire) authored on 2014/01/24 14:02:23
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,218 @@
1
+<?php
2
+require ("config.php");
3
+require_once("funcsv2.php");
4
+//Check session
5
+session_start();
6
+
7
+if (!$_SESSION['admin_logged_in'])
8
+{
9
+	//check fails
10
+	header("Location: authenticate.php?status=session");
11
+	exit();
12
+}
13
+?>
14
+
15
+
16
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
17
+<html>
18
+<head>
19
+	<title>Check Tracker for Expired Peers</title>
20
+	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
21
+	<link rel="stylesheet" type="text/css" href="./css/style.css" />
22
+</head>
23
+<body>
24
+<h1>Check Tracker for Expired Peers</h1>
25
+<?php
26
+
27
+
28
+error_reporting(E_ALL);
29
+//header("Content-Type: text/plain");
30
+
31
+//require_once("config.php");
32
+//require_once("funcsv2.php");
33
+
34
+$summaryupdate = array();
35
+
36
+// Non-persistant: we lock tables!
37
+$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(errorMessage() . "Tracker error: can't connect to database - ".mysql_error() . "</p>");
38
+mysql_select_db($database) or die(errorMessage() . "Tracker error: can't open database $database - ".mysql_error() . "</p>");
39
+
40
+if (isset($_GET["nolock"]))
41
+	$locking = false;
42
+else
43
+	$locking = true;
44
+
45
+// Assumes success
46
+if ($locking)
47
+	quickQuery("LOCK TABLES ".$prefix."summary WRITE, ".$prefix."namemap READ");
48
+
49
+?>
50
+<table class="torrentlist" cellspacing="1">
51
+<!-- Column Headers -->
52
+<tr>
53
+	<th>Name/Info Hash</th>
54
+	<th>Seeders</th>
55
+	<th>Leechers</th>
56
+	<th>Bytes Transfered</th>
57
+	<th>Stale Clients</th>
58
+	<th>Peer Cache</th>
59
+</tr>
60
+<?php
61
+
62
+$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");
63
+
64
+$i = 0;
65
+
66
+while ($row = mysql_fetch_row($results))
67
+{
68
+	$writeout = "row" . $i % 2;
69
+	list($hash, $seeders, $leechers, $bytes, $filename) = $row;
70
+	if ($locking)
71
+	{
72
+		//peercaching ALWAYS on
73
+		quickQuery("LOCK TABLES ".$prefix."x$hash WRITE, ".$prefix."y$hash WRITE, ".$prefix."summary WRITE");
74
+	}
75
+	$results2 = mysql_query("SELECT status, COUNT(status) from ".$prefix."x$hash GROUP BY status");
76
+	echo "<tr class=\"$writeout\"><td>";
77
+	if (!is_null($filename))
78
+		echo $filename;
79
+	else
80
+		echo $hash;
81
+	echo "</td>";
82
+	if (!$results2)
83
+	{
84
+		echo "<td colspan=\"4\">Unable to process: ".mysql_error()."</td></tr>";
85
+		continue;
86
+	}
87
+
88
+	$counts = array();
89
+	while ($row = mysql_fetch_row($results2))
90
+		$counts[$row[0]] = $row[1];	
91
+	if (!isset($counts["leecher"]))
92
+		$counts["leecher"] = 0;
93
+	if (!isset($counts["seeder"]))
94
+		$counts["seeder"] = 0;
95
+
96
+	if ($counts["seeder"] != $seeders)
97
+	{
98
+		quickQuery("UPDATE ".$prefix."summary SET seeds=".$counts["seeder"]." WHERE info_hash=\"$hash\"");
99
+		echo "<td class=\"center\">$seeders -> ".$counts["seeder"]."</td>";
100
+	}
101
+	else
102
+		echo "<td class=\"center\">$seeders</td>";
103
+		
104
+	if ($counts["leecher"] != $leechers)
105
+	{
106
+		quickQuery("UPDATE ".$prefix."summary SET leechers=".$counts["leecher"]." WHERE info_hash=\"$hash\"");
107
+		echo "<td class=\"center\">$leechers -> ".$counts["leecher"]."</td>";
108
+	}
109
+	else
110
+		echo "<td class=\"center\">$leechers</td>";
111
+		
112
+	if ($counts["leecher"] == 0)
113
+	{
114
+		//If there are no leechers, set the speed to zero
115
+		quickQuery("UPDATE ".$prefix."summary set speed=0 WHERE info_hash=\"$hash\"");
116
+	}
117
+
118
+	if ($bytes < 0)
119
+	{
120
+		quickQuery("UPDATE ".$prefix."summary SET dlbytes=0 WHERE info_hash=\"$hash\"");
121
+		echo "<td class=\"center\">$bytes -> Zero</td>";
122
+	}
123
+	else
124
+		echo "<td class=\"center\">". round($bytes/1048576/1024,3) ." GB</td>";
125
+
126
+	myTrashCollector($hash, $report_interval, time(), $writeout);
127
+	echo "<td class=\"center\">";
128
+	
129
+	$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>");
130
+	if (mysql_num_rows($result) > 0)
131
+	{
132
+		echo "Added ", mysql_num_rows($result);
133
+		$row = array();
134
+		
135
+		while ($data = mysql_fetch_row($result))
136
+				$row[] = "sequence=\"${data[0]}\"";
137
+		$where = implode(" OR ", $row);
138
+		$query = mysql_query("SELECT * FROM ".$prefix."x$hash WHERE $where");
139
+		
140
+		while ($row = mysql_fetch_assoc($query))
141
+		{
142
+			$compact = mysql_real_escape_string(pack('Nn', ip2long($row["ip"]), $row["port"]));
143
+			$peerid = mysql_real_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . '7:peer id20:' . hex2bin($row["peer_id"]) . "4:porti{$row["port"]}e");
144
+			$no_peerid = mysql_real_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . "4:porti{$row["port"]}e");
145
+			mysql_query("INSERT INTO ".$prefix."y$hash SET sequence='{$row["sequence"]}', compact='$compact', with_peerid='$peerid', without_peerid='$no_peerid'");
146
+		}
147
+	}	
148
+	else
149
+		echo "Added none";
150
+
151
+	$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");
152
+	if (mysql_num_rows($result) > 0)
153
+	{
154
+		echo ", Deleted ",mysql_num_rows($result);
155
+
156
+		$row = array();
157
+		
158
+		while ($data = mysql_fetch_row($result))
159
+			$row[] = "sequence=\"${data[0]}\"";
160
+		$where = implode(" OR ", $row);
161
+		$query = mysql_query("DELETE FROM ".$prefix."y$hash WHERE $where");
162
+	}
163
+	else
164
+		echo ", Deleted none";
165
+
166
+	echo "</td>";
167
+	
168
+	echo "</tr>\n";
169
+	$i ++;
170
+
171
+
172
+	if ($locking)
173
+		quickQuery("UNLOCK TABLES");
174
+		
175
+	//Repair tables, is this necessary?  Sometimes the tables crash...
176
+	//Can't repair table if locked?
177
+	//quickQuery("REPAIR Table x$hash");
178
+	//quickQuery("REPAIR Table y$hash");
179
+
180
+	// Finally, it's time to do stuff to the summary table.
181
+	if (!empty($summaryupdate))
182
+	{
183
+		$stuff = "";
184
+		foreach ($summaryupdate as $column => $value)
185
+		{
186
+			$stuff .= ', '.$column. ($value[1] ? "=" : "=$column+") . $value[0];
187
+		}
188
+		mysql_query("UPDATE ".$prefix."summary SET ".substr($stuff, 1)." WHERE info_hash=\"$hash\"");
189
+		$summaryupdate = array();
190
+	}
191
+
192
+
193
+}
194
+
195
+function myTrashCollector($hash, $timeout, $now, $writeout)
196
+{
197
+//	error_log("Trash collector working on $hash");
198
+ 	require("config.php");
199
+ 	$peers = loadLostPeers($hash, $timeout);
200
+ 	for ($i=0; $i < $peers["size"]; $i++)
201
+	        killPeer($peers[$i]["peer_id"], $hash, $peers[$i]["bytes"], $peers[$i]);
202
+	if ($i != 0)
203
+		echo "<td class=\"center\">Removed $i</td>";
204
+	else
205
+		echo "<td class=\"center\">Removed 0</td>";
206
+ 	quickQuery("UPDATE ".$prefix."summary SET lastcycle='$now' WHERE info_hash='$hash'");
207
+}
208
+
209
+
210
+
211
+
212
+?>
213
+</table>
214
+<p><a href="sanity.php?nolock=on">Not working? Try running this.</a></p>
215
+<a href="index.php"><img src="images/stats.png" border="0" class="icon" alt="Tracker Statistics" title="Tracker Statistics" /></a><a href="index.php">Return to Statistics Page</a><br>
216
+<a href="admin.php"><img src="images/admin.png" border="0" class="icon" alt="Admin Page" title="Admin Page" /></a><a href="admin.php">Return to Admin Page</a>
217
+</body>
218
+</html>