This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_upload.php
216 lines (192 loc) · 7.56 KB
/
batch_upload.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
<?php
require ("config.php");
require ("funcsv2.php"); //required for errorMessage() function
//Check session
session_start();
if (!$_SESSION['admin_logged_in'])
{
//check fails
header("Location: authenticate.php?status=session");
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Batch Upload Torrents</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="./css/style.css" type="text/css" />
</head>
<body>
<center>
<h1>Batch Upload Torrents</h1>
</center>
<br>
<?php
if (isset($_FILES["zipfile"]) && $_FILES["zipfile"]["error"] != 4 && isset($_FILES["zipfile"]["tmp_name"])) //4 corresponds to the error no file uploaded
{
?>
<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><br>
<?php
$zip = zip_open($_FILES["zipfile"]["tmp_name"]);
if ($zip == true)
{
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(errorMessage() . "Couldn't connect to the database, contact the administrator</p>");
mysql_select_db($database) or die(errorMessage() . "Can't open the database.</p>");
while ($zip_entry = zip_read($zip))
{
echo "Name: " . zip_entry_name($zip_entry) . "<br>\n";
if (substr(zip_entry_name($zip_entry), -8) == ".torrent")
{
$error_status = true;
if (zip_entry_open($zip, $zip_entry, "r"))
{
//read in file from zip
$buffer = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
//go through each torrent file and add it if possible
require_once ("BDecode.php");
require_once ("BEncode.php");
$tracker_url = $announce_url0 OR $announce_url1 OR $announce_url2 OR $announce_url3 OR $announce_url4;
$array = BDecode($buffer);
if (!$array)
{
echo errorMessage() . "Error: The parser was unable to load this torrent.</p>\n";
$error_status = false;
}
if (isset($array["announce-list"])) {
//multiple trackers are listed
$found_tracker = false;
for ($i = 0; $i < count($array["announce-list"]); $i++) {
if (strtolower($array["announce-list"][$i][0]) == $tracker_url) {
$found_tracker = true;
break;
}
}
if ($found_tracker == false)
{
echo errorMessage() . "Error: Multiple trackers were found but none of them match the primary
announce URL:<br>$tracker_url<br><br>or any alternate URLs:<br>$announce_url1<br>$announce_url2<br>$announce_url3<br>$announce_url4<br><br>Please re-create and re-upload the torrent.</p>\n";
$error_status = false;
exit;
}
} else {
//a single tracker is listed
if (strtolower($array["announce"]) != $tracker_url) {
echo errorMessage() . "Error: The tracker announce URL does not match the primary
announce URL:<br>$tracker_url<br><br>or any alternate URLs:<br>$announce_url1<br>$announce_url2<br>$announce_url3<br>$announce_url4<br><br>Please re-create and re-upload the torrent.</p>\n";
$error_status = false;
exit;
}
}
if (function_exists("sha1"))
$hash = @sha1(BEncode($array["info"]));
else
{
echo errorMessage() . "Error: It looks like you do not have a hash function available, this will not work.</p>\n";
$error_status = false;
}
//figure out total size of all files in torrent, needed for insertion into database
$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 = $array["info"]["name"];
$filename = mysql_real_escape_string($filename);
$filename = stripslashes($filename);
$filename = clean($filename);
if ((strlen($hash) != 40) || !verifyHash($hash))
{
echo errorMessage() . "Error: Info hash must be exactly 40 hex bytes.</p>\n";
$error_status = false;
}
if ($error_status == true)
{
$query = "INSERT INTO " . $prefix . "namemap (info_hash, title, filename, url, size, pubDate) VALUES (\"$hash\", \"$filename\", \"$filename\", \"$url\", \"$total_size\", \"" . date("$dateformat") . "\")";
$status = makeTorrent($hash, true);
quickQuery($query);
if ($status == true)
{
//create torrent file in folder, at this point we assume it's valid
if (!$handle = fopen("torrents/" . $filename . ".torrent", 'w'))
{
echo errorMessage() . "Error: Can't write to file.</p>\n";
break;
}
//populate file with contents
if (fwrite($handle, $buffer) === FALSE)
{
echo errorMessage() . "Error: Can't write to file.</p>\n";
break;
}
fclose($handle);
//make torrent file readable by all
chmod("torrents/" . $filename . ".torrent", 0644);
echo "<p class=\"success\">Torrent was added successfully.</p>\n";
}
else
{
echo errorMessage() . "There were some errors. Check if this torrent has been added previously.</p>\n";
}
}
zip_entry_close($zip_entry);
}
}
else
echo errorMessage() . "Unable to add torrent, it doesn't end in .torrent</p>\n";
echo "<br>";
}
zip_close($zip);
}
//finished reading zip file
//run RSS generator because we have new torrents in database
require_once("rss_generator.php");
}
else
{
//display upload box
?>
<?php require("config.php"); $tracker_url = $announce_url0; $alt_tracker_url1 = $announce_url1; $alt_tracker_url2 = $announce_url2; $alt_tracker_url3 = $announce_url3; $alt_tracker_url4 = $announce_url4;?>
<p>This page lets you upload a zip file containing multiple torrents and add them into the database. The
zip file cannot have any folders in it. This requires that you are running PHP with compiled zip support.
If you are unsure, check with your system administrator or phpinfo(). Any torrents that already exist in
the database will be skipped. If you want to use HTTP seeding you'll need to add this feature to the torrent
files before you zip and upload the file. If you are uploading a very large zip file this may take some time...
<br>
<br>
Notes:
<br>
[1] Even if the custom title option is enabled, the torrents will have the same title as the filename. If you
have the custom title option enabled, you may change the titles to your preference after the batch upload has
finished.<br>[2] The torrents you are batch uploading should include the following Tracker URL:
<br><b><?php echo $tracker_url ?></b><br> or its alternate tracker URLs: <br><b><?php echo $alt_tracker_url1;?> <br>
<?php echo $alt_tracker_url2;?> <br> <?php echo $alt_tracker_url3;?> <br> <?php echo $alt_tracker_url4; ?></b></p>
<?php
if (function_exists("zip_open"))
{
?>
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<b>Zip File:</b><input type="file" name="zipfile" size="50"/>
<input type="submit" value="Upload ZIP File"/>
</form>
<?php
}
else
echo errorMessage() . "Error: It looks like you don't have ZIP support compiled into PHP.</p>\n";
}
?>
<br>
<br>
<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>
</body>
</html>