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,484 @@
1
+<?php
2
+
3
+require_once("BDecode.php");
4
+require_once("BEncode.php");
5
+
6
+function classicoutput($array, $infohash)
7
+{
8
+
9
+	if (isset($array["info"]["pieces"]))
10
+		$array["info"]["pieces"] = "<i>Checksum data (" . strlen ($array["info"]["pieces"]) / 20 . " pieces)</i>";
11
+
12
+	echo "Info hash: <TT>$infohash</TT><br>";
13
+	echo "<pre>";
14
+	print_r(cleaner($array));
15
+	echo "</pre>";
16
+}
17
+
18
+function announceoutput($array)
19
+{
20
+	if (!isset($array["peers"][0]))
21
+	{
22
+		echo "Not a tracker announce block. Falling back on classic.<br><br>";
23
+		classicoutput($array, "(Not checked)");
24
+		exit;
25
+	}
26
+	echo "<h2>Client configuration options</h2>";
27
+	echo "<table border=0 cellpadding=2 cellspacing=2>";
28
+	foreach ($array as $left => $right)
29
+	{
30
+		if ($left == "peers")
31
+			continue;
32
+		if (is_array($right))
33
+			$myright = "<I>Error</I>";
34
+		else
35
+			$myright = $right;
36
+		echo "<tr><td align=right>".$left."</td><td>=</td><td>".$myright."</td></tr>\n";
37
+	}
38
+	echo "</table><br><h2>Peers</h2><pre>";
39
+	foreach ($array["peers"] as $data)
40
+	{
41
+		if (!is_array($data)) // special case: [0] == true  means empty list
42
+		{
43
+			echo "(Empty results)\n";
44
+			break;
45
+		}
46
+		echo 		bin2hex($data["peer id"])." at ".$data["ip"].":".$data["port"]."\n";
47
+	}
48
+	echo "</pre>";
49
+}
50
+
51
+function escapeURL($url)
52
+{
53
+	$ret = "";
54
+	$i=0;
55
+	while (strlen($url) > $i)
56
+	{
57
+		$ret .= "%".$url[$i].$url[$i + 1];
58
+		$i+=2;
59
+	}
60
+	return $ret;
61
+}
62
+
63
+
64
+function stringcleaner($str)
65
+{
66
+	/* WARNING:
67
+	
68
+	It appears PHP doesn't handle null bytes in the key portion
69
+	of string-indexed arrays. $array["abcd\0e"] = $something
70
+	will find itself with only 4 letters in the key. This may
71
+	cause some confusion when using /scrape, for example.
72
+	
73
+	*/
74
+
75
+	$len = strlen($str);
76
+	for ($i=0; $i < $len; $i ++)
77
+	{
78
+		if (ord($str[$i]) < 32 || ord($str[$i]) > 128)
79
+			return "<B>".bin2hex($str)."</B>";
80
+	}
81
+	return $str;
82
+}
83
+
84
+function cleaner($array)
85
+{
86
+	if (!is_array($array))
87
+		return $array;
88
+	$newarray = array();
89
+	foreach($array as $left => $right)
90
+	{
91
+		if (is_string($left))
92
+			$newleft = stringcleaner(stripslashes($left));
93
+		else
94
+			$newleft = $left;
95
+
96
+		if (is_string($right))
97
+			$newright = stringcleaner($right);
98
+		else if (is_array($right))
99
+			$newright = cleaner($right);
100
+		else
101
+			$newright = $right;
102
+
103
+		$newarray[$newleft] = $newright;
104
+	}
105
+	return $newarray;
106
+}
107
+
108
+
109
+if (isset($_POST["output"]))
110
+{
111
+	if (!is_numeric($_POST["output"]))
112
+		$output = -1;
113
+	else
114
+		$output = $_POST["output"];
115
+	if ($output > 3 || $output < -1)
116
+		$output = -1;
117
+
118
+
119
+}
120
+else if (isset($_GET["style"]))
121
+	$output = $_GET["style"];
122
+else
123
+	$output = -1;
124
+
125
+//used by index.php to show information on torrent
126
+if (isset($_POST["hash"]))
127
+{
128
+	if (!isset($status)) //not coming from newtorrents page
129
+	{
130
+	?>
131
+	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
132
+	<html>
133
+	<head>
134
+		<title>Torrent Information</title>
135
+		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
136
+		<link rel="stylesheet" type="text/css" href="./css/style.css" />
137
+	</head>
138
+	<body>
139
+	<?php
140
+	}
141
+	//lookup file
142
+	require_once("config.php");
143
+	require_once("funcsv2.php");
144
+	//connect to DB
145
+	if ($GLOBALS["persist"])
146
+		$db = mysql_pconnect($dbhost, $dbuser, $dbpass) or die(errorMessage() . "Tracker error: can't connect to database - " . mysql_error() . "</p>");
147
+	else
148
+		$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(errorMessage() . "Tracker error: can't connect to database - " . mysql_error() . "</p>");
149
+	mysql_select_db($database) or die(errorMessage() . "Tracker error: can't open database $database - " . mysql_error() . "</p>");
150
+	$query = "SELECT filename FROM ".$prefix."namemap WHERE info_hash = '" . $_POST["hash"] . "'";
151
+	$results = mysql_query($query) or die(errorMessage() . "Can't do SQL query - " . mysql_error() . "</p>");
152
+	$data = mysql_fetch_row($results);
153
+	//find filename and set it
154
+	$_FILES["torrent"]["tmp_name"] = "torrents/" . $data[0] . ".torrent";
155
+	if (!isset($status))
156
+		echo "<h1>" . $data[0] . "</h1>"; 
157
+	echo "<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>\n";
158
+}
159
+
160
+//main displaying and processing
161
+if (isset($_FILES["torrent"]) || isset($_POST["url"]) || isset($_GET["url"]))
162
+{
163
+	if (strlen($_FILES["torrent"]["tmp_name"]) > 0 && file_exists($_FILES["torrent"]["tmp_name"])) //for DumpTorrentCGI.php, and index.php
164
+	{
165
+		$fd = fopen($_FILES["torrent"]["tmp_name"], "rb") or die(errorMessage() . "File upload error 1</p>");
166
+		if (!isset($_POST["hash"]))
167
+			is_uploaded_file($_FILES["torrent"]["tmp_name"]) or die(errorMessage() . "File upload error 2</p>");
168
+		$alltorrent = fread($fd, filesize($_FILES["torrent"]["tmp_name"]));
169
+		fclose($fd);
170
+	}
171
+	else if (file_exists("torrents/" . $filename . ".torrent")) //for newtorrents.php
172
+	{
173
+		$fd = fopen("torrents/" . $filename . ".torrent", "rb") or die(errorMessage() . "File upload error 1</p>");
174
+		$alltorrent = fread($fd, filesize("torrents/" . $filename . ".torrent"));
175
+		fclose($fd);
176
+	}
177
+	else if (isset($_POST["url"]))
178
+	{
179
+		(strlen($_POST["url"]) > 0) or die(errorMessage() . "Logic error in script.</p>");
180
+		if (strtolower(substr($_POST["url"], 0, 7)) != "http://")
181
+			die(errorMessage() . "Error: you must specify \"http://\" as part of the URL.</p>");
182
+		$fd = fopen($_POST["url"], "rb") or die(errorMessage() . "File download error.</p>");
183
+		$alltorrent = "";
184
+		while (!feof($fd))
185
+		{
186
+			$alltorrent .= fread($fd, 4096);
187
+			if (strlen($alltorrent) > 50000)
188
+				die(errorMessage() . "File too large to download.</p>");
189
+		}
190
+		fclose($fd);
191
+	}
192
+	else if (isset($_GET["url"]))
193
+	{
194
+ 	 	(strlen($_GET["url"]) > 0) or die(errorMessage() . "Logic error in script.</p>");
195
+ 	 	if (strtolower(substr($_GET["url"], 0, 7)) != "http://")
196
+ 	 	        die(errorMessage() . "Error: you must specify \"http://\" as part of the URL</p>");
197
+ 	 	$fd = fopen($_GET["url"], "rb") or die(errorMessage() . "File download error.</p>");
198
+ 	 	$alltorrent = "";
199
+ 	 	while (!feof($fd))
200
+ 	 	{
201
+ 	 	 	$alltorrent .= fread($fd, 4096);
202
+ 	 	 	if (strlen($alltorrent) > 50000)
203
+ 	 	 	        die(errorMessage() . "File too large to download.</p>");
204
+ 	 	}
205
+ 	 	fclose($fd);
206
+
207
+	}
208
+	$array = BDecode($alltorrent);
209
+	if (!isset($array))
210
+	{
211
+		echo errorMessage() . "There was an error handling your uploaded torrent. It may be corrupted. Are you sure it's of type .torrent?</p>";
212
+		exit;
213
+	}
214
+
215
+	if ($array == false)
216
+	{
217
+      echo errorMessage() . "There was an error handling your uploaded torrent. It may be corrupted. Are you sure it's of type .torrent?</p>";             
218
+		exit;
219
+	}
220
+
221
+	// Making torrents look nice: If $array["info"] exists, it is used to calculate
222
+	// an Info_hash value.
223
+
224
+	$infohash = "<I>Not applicable</I>";	
225
+	if (isset($array["info"]))
226
+		if (is_array($array["info"]))
227
+		{
228
+			if (function_exists("sha1"))
229
+				$infohash = @sha1(BEncode($array["info"]));
230
+			else
231
+				$infohash = "(No SHA1 available to calculate info_hash)</TT><br>";
232
+			
233
+			// If the "pieces" section exists, it is replaced by some nice text.
234
+			// The alternative is pages of garbage.
235
+		}
236
+
237
+	// Auto-detect file type
238
+	if ($output == -1)
239
+	{
240
+		if (isset($array["announce"]) && isset($array["info"]))
241
+			$output = 1;
242
+		else if (isset($array["files"]))
243
+			$output = 2;
244
+		else if (isset($array["peers"]))
245
+			$output = 3;
246
+		else
247
+			$output = 0;
248
+	}
249
+
250
+	// Output information.
251
+	if ($output == 0)
252
+	{
253
+		classicoutput($array, $infohash);
254
+	}
255
+
256
+	if ($output == 1)
257
+	{
258
+		if (!isset($array["info"]))
259
+		{
260
+		 	echo "Error: not a torrent file. Falling back on classic.<br><br>";
261
+
262
+		 	classicoutput($array, "<I>Not applicable</I>");
263
+		 	exit;	                
264
+		}
265
+
266
+		echo "<br><h2>Non-file data:</h2>\n";
267
+		echo "<table border=0 cellpadding=2 cellspacing=2><tr>";
268
+		echo "<td align=right>Info hash</td><td>=</td><td><TT>$infohash</TT></td></tr>\n";
269
+		echo "<tr><td align=right>Announce URL(s)</td><td>=</td><td>";
270
+		if (isset($array["announce-list"])) {
271
+			for ($i = 0; $i < count($array["announce-list"]); $i++) {
272
+				echo $array["announce-list"][$i][0] . "<br>";
273
+			}
274
+		} else {
275
+			//single tracker
276
+			echo $array["announce"];
277
+		}
278
+		echo "</td></tr>\n";
279
+
280
+		if (isset($array["creation date"]))
281
+		{
282
+			echo "<tr><td align=right>Creation date</td><td>=</td><td>";
283
+			if (is_numeric($array["creation date"]))
284
+				echo date("F j, Y", $array["creation date"]);
285
+			else
286
+				echo $array["creation date"];
287
+			echo "</td></tr>";
288
+		}
289
+		if ($array["info"]["private"] == 1)
290
+			echo "<tr><td align=right>Private (No DHT Allowed)</td><td>=</td><td>yes</td></tr>\n";
291
+		else
292
+			echo "<tr><td align=right>Private (No DHT Allowed)</td><td>=</td><td>no</td></tr>\n";
293
+
294
+		foreach ($array as $left => $right)
295
+		{
296
+			if ($left == "announce" || $left == "info" || $left == "creation date" || $left == "announce-list")
297
+				continue; // skip
298
+			if ($left == "url-list" || $left == "httpseeds")
299
+			{
300
+				echo "<tr><td align=right>$left</td><td>=</td><td>";
301
+				print_r(cleaner($array[$left]));
302
+				echo "</td></tr>\n";
303
+				continue;
304
+			}
305
+			echo "<tr><td align=right>$left</td><td>=</td><td>".$array[$left]."</td></tr>\n";
306
+		}
307
+		
308
+		echo "</table><br><br><h2>File data:</h2><pre>";
309
+		$info = $array["info"];
310
+		
311
+		$total_size = 0;
312
+		if (isset($info["files"]))
313
+		{
314
+			echo "Directory: ".$info["name"]."\nFiles:\n";
315
+			foreach ($info["files"] as $file)
316
+			{
317
+				if (isset($file["path"][1]))
318
+				{
319
+					echo "    " . $file["path"][0];
320
+					for ($i=1; isset($file["path"][$i]); $i++)
321
+						echo "/".$file["path"][$i];
322
+				}
323
+				else
324
+					echo "    " . $file["path"][0];
325
+				echo "  (".$file["length"]." bytes)\n";
326
+				$total_size = $total_size + $file["length"];
327
+			}
328
+			echo "\n";
329
+		}
330
+		else
331
+		{
332
+			echo "File: ".$info["name"]. " (".$info["length"]." bytes)\n\n";
333
+			$total_size = $info["length"];
334
+		}
335
+		
336
+		echo "Piece length: ".$info["piece length"]."\nNumber of pieces: ". strlen ($array["info"]["pieces"])/20 . "\n\n";
337
+		if ($total_size < 1024) //dealing with bytes
338
+			echo "Total Size: " . $total_size . " bytes</pre>\n";
339
+		elseif ($total_size < 1048576) //dealing with kilobytes
340
+			echo "Total Size: " . round($total_size/1024, 2) . " kilobytes</pre>\n";
341
+		elseif ($total_size < 1073741824) //dealing with megabytes
342
+			echo "Total Size: " . round($total_size/1048576, 2) . " megabytes</pre>\n";
343
+		elseif ($total_size >= 1073741824) //dealing with gigabytes
344
+			echo "Total Size: " . round($total_size/1073741824, 2) . " gigabytes</pre>\n";
345
+	}
346
+
347
+	if ($output == 2)
348
+	{
349
+		if (!isset($array["files"]))
350
+		{
351
+			echo "Error: not /scrape data. Falling back on classic.<br><br>";
352
+			classicoutput($array, $infohash);
353
+			exit;		
354
+		}
355
+		$files = $array["files"];
356
+		
357
+		// Copy and paste from python tracker output, with some 
358
+		// formatting changes
359
+		echo '<table cellpadding=2 cellspacing=2 border=1 summary="files"><tr><th>info hash</th><th align="right">complete</th><th align="right">downloading</th><th>finished downloads</th><th>file name</th></tr>';
360
+		
361
+				
362
+		foreach ($files as $hash => $data)
363
+		{
364
+			echo "<tr><td><TT>".bin2hex(stripslashes($hash))."</TT>";
365
+			echo "</td><td>".$data["complete"]."</td><td>".$data["incomplete"]."</td><td>";
366
+			if (isset($data["downloaded"]))
367
+				echo $data["downloaded"];
368
+			else
369
+				echo "-";
370
+			echo "</td><td>";
371
+			if (isset($data["name"]))
372
+				echo $data["name"];
373
+			else
374
+				echo "(unavailable)";
375
+			echo "</td></tr>";
376
+		}
377
+		echo "</table>";
378
+	}
379
+
380
+	// http://tracker.com:6969/announce
381
+	if ($output == 3)
382
+	{
383
+		announceoutput($array);
384
+	}
385
+
386
+	if (isset($filename) && file_exists("torrents/" . $filename . ".torrent")) //for newtorrents.php
387
+	{
388
+		echo "<a href=\"newtorrents.php\"><img src=\"images/add.png\" border=\"0\" class=\"icon\" alt=\"Add Torrent\" title=\"Add Torrent\" /></a><a href=\"newtorrents.php\">Add Another Torrent</a><br>\n";
389
+		//add in Bittornado HTTP seeding spec
390
+		if (isset($_POST["httpseed"]) && $_POST["httpseed"] == "enabled")
391
+		{
392
+			//add information into database
393
+			$info = $array["info"] or die("Invalid torrent file.");
394
+			
395
+			$fsbase = $_POST["relative_path"];
396
+			
397
+			if (isset($info["files"])) // Multi-file
398
+			{
399
+				if (substr($fsbase, -1) != '/')
400
+					$fsbase .= '/';
401
+				$pieceno = 0;
402
+				$fileno = 0;
403
+				$piecelen = 0;
404
+				
405
+				// Iterate for each file.
406
+				while (isset($info["files"][$fileno]))
407
+				{
408
+					if ($piecelen == $info["piece length"])
409
+					{
410
+						$pieceno++;
411
+						$piecelen = 0;
412
+					}
413
+					$startoffset = $piecelen;
414
+					$startpiece = $pieceno;
415
+					$filesize = $info["files"][$fileno]["length"];
416
+					while (true)
417
+					{
418
+						$sub = min($info["piece length"]-$piecelen, $filesize);
419
+						$piecelen += $sub;
420
+						$filesize -= $sub;
421
+						
422
+						if ($filesize == 0)
423
+							break;
424
+						if ($piecelen == $info["piece length"])
425
+						{
426
+							$pieceno++;
427
+							$piecelen = 0;
428
+						}
429
+						if ($piecelen > $info["piece length"])
430
+							die("Logic error in script. Please report to the author.");
431
+					}
432
+					$filename = $fsbase;
433
+					if (isset($info["files"][$fileno]["path"][1]))
434
+					{
435
+						$filename .= $file["path"][0];
436
+						for ($i=1; isset($info["files"][$fileno]["path"][$i]); $i++)
437
+							$filename .= "/".$info["files"][$fileno]["path"][$i];
438
+					}
439
+					else
440
+						$filename .= $info["files"][$fileno]["path"][0];
441
+					$filename = mysql_real_escape_string($filename);			
442
+					mysql_query("INSERT INTO ".$prefix."webseedfiles (info_hash,filename,startpiece,endpiece,startpieceoffset,fileorder) values (\"$hash\", \"$filename\", $startpiece, $pieceno, $startoffset, $fileno)");
443
+					$fileno++;
444
+				}
445
+			} // end of multi-file section
446
+			else //single file
447
+				mysql_query("INSERT INTO ".$prefix."webseedfiles (info_hash,filename,startpiece,endpiece,startpieceoffset,fileorder) values (\"$hash\", \"".mysql_real_escape_string($fsbase)."\", 0, ". (strlen($array["info"]["pieces"])/20 - 1).", 0, 0)");
448
+		}
449
+		
450
+		if ((isset($_POST["getrightseed"]) && $_POST["getrightseed"] == "enabled") || (isset($_POST["httpseed"]) && $_POST["httpseed"] == "enabled")) //only do one write
451
+		{
452
+			//edit torrent file
453
+			$read_httpseed = fopen("torrents/" . $filename . ".torrent", "rb");
454
+			$binary_data = fread($read_httpseed, filesize("torrents/" . $filename . ".torrent"));
455
+			$data_array = BDecode($binary_data);
456
+			
457
+			if ($_POST["httpseed"] == "enabled")
458
+				$data_array["httpseeds"][0] = $website_url . substr($_SERVER['REQUEST_URI'], 0, -15) . "seed.php";
459
+			if ($_POST["getrightseed"] == "enabled")
460
+				$data_array["url-list"][0] = $_POST["httpftplocation"];
461
+				
462
+			$to_write = BEncode($data_array);
463
+			fclose($read_httpseed);
464
+			//write torrent file
465
+			$write_httpseed = fopen("torrents/" . $filename . ".torrent", "wb");
466
+			fwrite($write_httpseed, $to_write);
467
+			fclose($write_httpseed);
468
+		}
469
+		
470
+		//add in piecelength and number of pieces
471
+		$query = "UPDATE ".$prefix."summary SET piecelength=\"" . $info["piece length"] . "\", numpieces=\"" . strlen ($array["info"]["pieces"])/20 . "\" WHERE info_hash=\"" . $hash . "\"";
472
+		quickQuery($query);
473
+	}
474
+	
475
+	if (!isset($_POST["hash"])) //don't display admin link if coming from index.php
476
+		echo "<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><br>";
477
+	?>
478
+	<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>
479
+	</body></html>
480
+	<?php
481
+	exit();
482
+}
483
+
484
+?>