forked from ahmyi/rivettracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_upload.php
182 lines (159 loc) · 5.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
<?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 ($_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)
{
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 = $website_url . substr($_SERVER['REQUEST_URI'], 0, -16) . "announce.php";
$array = BDecode($buffer);
if (!$array)
{
echo errorMessage() . "Error: The parser was unable to load this torrent.</p>\n";
$error_status = false;
}
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";
$error_status = false;
}
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 = $sql->real_escape_string($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, filename, url, size, pubDate) VALUES (\"$hash\", \"$filename\", \"$url\", \"$total_size\", \"" . date('D, j M Y h:i:s') . "\")";
$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
?>
<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...</p>
<?php
if (function_exists(zip_open))
{
?>
<form enctype="multipart/form-data" action="<?php echo $_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>