-
Notifications
You must be signed in to change notification settings - Fork 9
/
newtorrents.php
262 lines (244 loc) · 8.82 KB
/
newtorrents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
require_once("config.php");
require_once("funcsv2.php");
//Check session
session_start();
if (!$_SESSION['admin_logged_in'] && !$_SESSION['upload_logged_in'])
{
//check fails
header("Location: authenticate.php?status=error");
exit();
}
?>
<html>
<head>
<title>Add Torrent to Tracker</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="./css/style.css" />
</head>
<body>
<?php
$tracker_url = $website_url . substr($_SERVER['REQUEST_URI'], 0, -15) . "announce.php";
if (isset($_FILES["torrent"]))
addTorrent();
endOutput();
function addTorrent()
{
require ("config.php");
$tracker_url = $website_url . substr($_SERVER['REQUEST_URI'], 0, -15) . "announce.php";
$hash = strtolower($_POST["hash"]);
require_once ("funcsv2.php");
require_once ("BDecode.php");
require_once ("BEncode.php");
if ($_FILES["torrent"]["error"] != 4)
{
$fd = fopen($_FILES["torrent"]["tmp_name"], "rb") or die(errorMessage() . "File upload error 1</p>\n");
is_uploaded_file($_FILES["torrent"]["tmp_name"]) or die(errorMessage() . "File upload error 2</p>\n");
$alltorrent = fread($fd, filesize($_FILES["torrent"]["tmp_name"]));
$array = BDecode($alltorrent);
if (!$array)
{
echo errorMessage() . "Error: The parser was unable to load your torrent. Please re-create and re-upload the torrent.</p>\n";
endOutput();
exit;
}
if (strtolower($array["announce"]) != $tracker_url)
{
echo errorMessage() . "Error: The tracker announce URL does not match this:<br>$tracker_url<br>Please re-create and re-upload the torrent.</p>\n";
endOutput();
exit;
}
if (@$_POST["httpseed"] == "enabled" && @$_POST["relative_path"] == "")
{
echo errorMessage() . "Error: HTTP seeding was checked however no relative path was given.</p>\n";
endOutput();
exit;
}
if ($_POST["httpseed"] == "enabled" && $_POST["relative_path"] != "")
{
if (Substr($_POST["relative_path"], -1) == "/")
{
if (!is_dir($_POST["relative_path"]))
{
echo errorMessage() . "Error: HTTP seeding relative path ends in / but is not a valid directory.</p>\n";
endOutput();
exit;
}
}
else
{
if (!is_file($_POST["relative_path"]))
{
echo errorMessage() . "Error: HTTP seeding relative path is not a valid file.</p>\n";
endOutput();
exit;
}
}
}
if (@$_POST["getrightseed"] == "enabled" && @$_POST["httpftplocation"] == "")
{
echo errorMessage() . "Error: GetRight HTTP seeding was checked however no URL was given.</p>\n";
endOutput();
exit;
}
if (@$_POST["getrightseed"] == "enabled" && (Substr(@$_POST["httpftplocation"], 0, 7) != "http://" && Substr(@$_POST["httpftplocation"], 0, 6) != "ftp://"))
{
echo errorMessage() . "Error: GetRight HTTP seeding URL must start with http:// or ftp://</p>\n";
endOutput();
exit;
}
$hash = @sha1(BEncode($array["info"]));
fclose($fd);
$target_path = "torrents/";
$target_path = $target_path . basename( clean($_FILES['torrent']['name']));
$move_torrent = move_uploaded_file($_FILES["torrent"]["tmp_name"], $target_path);
if ($move_torrent == false)
{
echo errorMessage() . "Unable to move " . $_FILES["torrent"]["tmp_name"] . " to torrents/</p>\n";
}
}
if (isset($_POST["filename"]))
$filename = clean($_POST["filename"]);
else
$filename = "";
if (isset($_POST["url"]))
$url = clean($_POST["url"]);
else
$url = "";
if (isset($_POST["autoset"]))
if (strcmp($_POST["autoset"], "enabled") == 0)
{
if (strlen($filename) == 0 && isset($array["info"]["name"]))
$filename = $array["info"]["name"];
}
//figure out total size of all files in torrent
$info = $array["info"];
$total_size = 0;
if (isset($info["files"]))
{
foreach ($info["files"] as $file)
{
$total_size = $total_size + $file["length"];
}
}
else
{
$total_size = $info["length"];
}
//Validate torrent file, make sure everything is correct
$filename = $sql->real_escape_string($filename);
$filename = htmlspecialchars(clean($filename));
$url = htmlspecialchars($sql->real_escape_string($url));
if ((strlen($hash) != 40) || !verifyHash($hash))
{
echo errorMessage() . "Error: Info hash must be exactly 40 hex bytes.</p>\n";
endOutput();
}
if (Substr($url, 0, 7) != "http://" && $url != "")
{
echo errorMessage() . "Error: The Torrent URL does not start with http:// Make sure you entered a correct URL.</p>\n";
endOutput();
}
$magent = 'magnet:?xt=urn:btih:'.strtoupper($hash)."&tr=".rawurlencode($website_url . substr($_SERVER['REQUEST_URI'], 0, -15) . "announce.php");
$query = "INSERT INTO ".$prefix."namemap (info_hash, filename, url, size, pubDate, magnet) VALUES (\"$hash\", \"$filename\", \"$url\", \"$total_size\", \"" . date('D, j M Y h:i:s') . "\", '$magent')";
$status = makeTorrent($hash, true);
$sql->query($query);
if ($status)
{
echo "<p class=\"success\">Torrent was added successfully.</p>\n";
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";
//rename torrent file to match filename
rename("torrents/" . clean($_FILES['torrent']['name']), "torrents/" . $filename . ".torrent");
//make torrent file readable by all
chmod("torrents/" . $filename . ".torrent", 0644);
//run RSS generator
require_once("rss_generator.php");
//Display information from DumpTorrentCGI.php
require_once("torrent_functions.php");
}
else
{
echo errorMessage() . "There were some errors. Check if this torrent has been added previously.</p>\n";
//delete torrent file if it doesn't exist in database
$query = "SELECT COUNT(*) FROM ".$prefix."summary WHERE info_hash = '$hash'";
$results = $sql->query($query);
$data = $results->fetch_row();
if ($data[0] == 0)
{
if (file_exists("torrents/" . $_FILES['torrent']['name']))
unlink("torrents/" . $_FILES['torrent']['name']);
}
//make torrent file readable by all
chmod("torrents/" . $filename . ".torrent", 0644);
endOutput();
}
}
function endOutput()
{
require("config.php");
$tracker_url = $website_url . substr($_SERVER['REQUEST_URI'], 0, -15) . "announce.php";
?>
<p align="right"><a href="./docs/help.html"><img src="images/help.png" border="0" class="icon" alt="Help" title="Help" /></a><a href="./docs/help.html">Help</a></p>
<div class="center">
<h1>Add Torrent to Tracker Database</h1>
<h3>Tracker URL: <?php echo $tracker_url;?></h3>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td class="right">Torrent file:</td>
<td class="left"><?php
if (function_exists("sha1"))
echo "<input type=\"file\" name=\"torrent\" size=\"50\"/>";
else
echo '<i>File uploading not available - no SHA1 function.</i>';
?></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr>
<td class="center" colspan="2"><input type="checkbox" name="httpseed" value="enabled">Use BitTornado HTTP seeding specification (optional)</td>
</tr>
<tr>
<td class="right">Relative location of file or directory:<br>e.g. ../../files/file.zip</td>
<td class="left"><input type="text" name="relative_path" size="70"/></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr>
<td class="center" colspan="2"><input type="checkbox" name="getrightseed" value="enabled">Use GetRight HTTP seeding specification (optional)</td>
</tr>
<tr>
<td class="right">FTP/HTTP URL of file or directory:<br>e.g. http://yourwebsite.com/file.zip</td>
<td class="left"><input type="text" name="httpftplocation" size="70"/></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<?php if (function_exists("sha1"))
echo "<tr><td class=\"center\" colspan=\"2\"><input type=\"checkbox\" name=\"autoset\" value=\"enabled\" checked=\"checked\" /> Fill in fields below automatically using data from the torrent file.</td></tr>\n";
?>
<tr>
<td class="right">Info Hash:</td>
<td class="left"><input type="text" name="hash" size="40"/></td>
</tr>
<tr>
<td class="right">File name (optional): </td>
<td class="left"><input type="text" name="filename" size="60" maxlength="200"/></td>
</tr>
<tr>
<td class="right">Torrent's URL (optional): </td>
<td class="left"><input type="text" name="url" size="60" maxlength="200"/></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr>
<td class="center" colspan="2"><input type="submit" value="Add Torrent to Database"/> - <input type="reset" value="Clear Settings"/></td>
</tr>
</table>
<br>
<input type="hidden" name="username" value="<?php echo $_POST['username']; ?>"/>
<input type="hidden" name="password" value="<?php echo $_POST['password']; ?>"/>
</form>
<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>
</div>
</body></html>
<?php
// Still in function endOutput()
exit;
}
?>