forked from mlangill/biotorrents
/
takeupload.php
295 lines (231 loc) · 8.54 KB
/
takeupload.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
require_once("include/benc.php");
require_once("include/bittorrent.php");
require_once "include/user_functions.php";
ini_set("upload_max_filesize",$max_torrent_size);
function bark($msg) {
genbark($msg, "Upload failed!");
}
dbconn();
loggedinorreturn();
if ($CURUSER['class'] < UC_USER)
die;
foreach(explode(":","descr:type:name") as $v) {
if (!isset($_POST[$v]))
bark("missing form data");
}
if (!isset($_FILES["file"]))
bark("missing form data: file");
$f = $_FILES["file"];
$fname = unesc($f["name"]);
if (empty($fname))
bark("Empty filename!");
$nfo = sqlesc('');
/////////////////////// NFO FILE ////////////////////////
if(isset($_FILES['nfo']) && !empty($_FILES['nfo']['name'])) {
$nfofile = $_FILES['nfo'];
if ($nfofile['name'] == '')
bark("No NFO!");
if ($nfofile['size'] == 0)
bark("0-byte NFO");
if ($nfofile['size'] > 65535)
bark("NFO is too big! Max 65,535 bytes.");
$nfofilename = $nfofile['tmp_name'];
if (@!is_uploaded_file($nfofilename))
bark("NFO upload failed");
$nfo = sqlesc(str_replace("\x0d\x0d\x0a", "\x0d\x0a", @file_get_contents($nfofilename)));
}
/////////////////////// NFO FILE END /////////////////////
$descr = unesc($_POST["descr"]);
if (!$descr)
bark("You must enter a description!");
$catid = (0 + $_POST["type"]);
if (!is_valid_id($catid))
bark("You must select a category to put the torrent in!");
$lic_id =(0 + $_POST["lic"]);
if (!is_valid_id($lic_id))
bark("You must select a license category for the torrent!");
$version_torrent_id =(0 + $_POST["version"]);
if (!validfilename($fname))
bark("Invalid filename!");
if (!preg_match('/^(.+)\.torrent$/si', $fname, $matches))
bark("Invalid filename (not a .torrent).");
$shortfname = $torrent = $matches[1];
if (!empty($_POST["name"]))
$torrent = unesc($_POST["name"]);
$tmpname = $f["tmp_name"];
if (!is_uploaded_file($tmpname))
bark("eek");
if (!filesize($tmpname))
bark("Empty file!");
$dict = bdec_file($tmpname, $max_torrent_size);
if (!isset($dict))
bark("What the hell did you upload? This is not a bencoded file!");
function dict_check($d, $s) {
if ($d["type"] != "dictionary")
bark("not a dictionary");
$a = explode(":", $s);
$dd = $d["value"];
$ret = array();
$t='';
foreach ($a as $k) {
unset($t);
if (preg_match('/^(.*)\((.*)\)$/', $k, $m)) {
$k = $m[1];
$t = $m[2];
}
if (!isset($dd[$k]))
bark("dictionary is missing key(s)");
if (isset($t)) {
if ($dd[$k]["type"] != $t)
bark("invalid entry in dictionary");
$ret[] = $dd[$k]["value"];
}
else
$ret[] = $dd[$k];
}
return $ret;
}
function dict_get($d, $k, $t) {
if ($d["type"] != "dictionary")
bark("not a dictionary");
$dd = $d["value"];
if (!isset($dd[$k]))
return;
$v = $dd[$k];
if ($v["type"] != $t)
bark("invalid dictionary entry type");
return $v["value"];
}
list($ann, $info) = dict_check($dict, "announce(string):info");
$tmaker = (isset($dict['value']['created by']) && !empty($dict['value']['created by']['value'])) ? sqlesc($dict['value']['created by']['value']) : sqlesc('Unknown');
//Morgan: Hack to strip hashkey from announce string
$my_dict=$dict;
//Overwrite user supplied announce url with simple biotorrent one
$my_dict['value']['announce']['value'] = $announce_urls[0];
$my_dict['value']['announce']['string'] = strlen($my_dict['value']['announce']['value']).":".$my_dict['value']['announce']['value'];
$my_dict['value']['announce']['strlen'] = strlen($my_dict['value']['announce']['string']);
//Added support for announce-list
if(isset($my_dict['value']['announce-list']['value'][0]['value'][0]['value'])){
$my_dict['value']['announce-list']['value'][0]['value'][0]['value'] = $my_dict['value']['announce']['value'];
$my_dict['value']['announce-list']['value'][0]['value'][0]['string'] = strlen($my_dict['value']['announce-list']['value'][0]['value'][0]['value']).":".$my_dict['value']['announce-list']['value'][0]['value'][0]['value'];
$my_dict['value']['announce-list']['value'][0]['value'][0]['strlen'] = strlen($my_dict['value']['announce-list']['value'][0]['value'][0]['string']);
}
unset($dict);
list($dname, $plen, $pieces) = dict_check($info, "name(string):piece length(integer):pieces(string)");
#if (!in_array($ann, $announce_urls, 1))
# bark("invalid announce url! must be " . $announce_urls[0]);
if($ann != $announce_urls[0]."?passkey={$CURUSER['passkey']}")
bark("invalid announce url! must be " . $announce_urls[0] ."?passkey={$CURUSER['passkey']}" . "not $ann");
if (strlen($pieces) % 20 != 0)
bark("invalid pieces");
$filelist = array();
$totallen = dict_get($info, "length", "integer");
if (isset($totallen)) {
$filelist[] = array($dname, $totallen);
$type = "single";
}
else {
$flist = dict_get($info, "files", "list");
if (!isset($flist))
bark("missing both length and files");
if (!count($flist))
bark("no files");
$totallen = 0;
foreach ($flist as $fn) {
list($ll, $ff) = dict_check($fn, "length(integer):path(list)");
$totallen += $ll;
$ffa = array();
foreach ($ff as $ffe) {
if ($ffe["type"] != "string")
bark("filename error");
$ffa[] = $ffe["value"];
}
if (!count($ffa))
bark("filename error");
$ffe = implode("/", $ffa);
$filelist[] = array($ffe, $ll);
}
$type = "multi";
}
$infohash = pack("H*", sha1($info["string"]));
unset($info);
// Replace punctuation characters with spaces
$torrent = str_replace("_", " ", $torrent);
#Morgan: Add version insert if applicable
$version_id = get_version_id_for_torrent($version_torrent_id,0);
$ret = mysql_query("INSERT INTO torrents (search_text, filename, owner, visible, info_hash, name, size, numfiles, type,descr, ori_descr, category,license, save_as, added, last_action, nfo, client_created_by, version) VALUES (" .
implode(",", array_map("sqlesc", array(searchfield("$shortfname $dname $torrent"), $fname, $CURUSER["id"], "no", $infohash, $torrent, $totallen, count($filelist), $type, $descr, $descr, $catid, $lic_id, $dname))) .
", " . time() . ", " . time() . ", $nfo, $tmaker, $version_id)");
if (!$ret) {
if (mysql_errno() == 1062)
bark("torrent already uploaded!");
bark("mysql puked: ".mysql_error());
}
$id = mysql_insert_id();
@mysql_query("DELETE FROM files WHERE torrent = $id");
function file_list($arr,$id)
{
foreach($arr as $v)
$new[] = "($id,".sqlesc($v[0]).",".$v[1].")";
return join(",",$new);
}
mysql_query("INSERT INTO files (torrent, filename, size) VALUES ".file_list($filelist,$id));
//#Morgan: Instead of moving the uploaded torrent just create a new one with the simpler announce url
#move_uploaded_file($tmpname, "$torrent_dir/$id.torrent");
$morgan_fh = fopen("$torrent_dir/$id.torrent", 'w');
fwrite($morgan_fh,benc($my_dict));
fclose($morgan_fh);
write_log("Torrent $id ($torrent) was uploaded by " . $CURUSER["username"]);
/* RSS feeds */
require_once("rss_old.php");
/* Email notifs */
/*******************
$res = mysql_query("SELECT name FROM categories WHERE id=$catid") or sqlerr();
$arr = mysql_fetch_assoc($res);
$cat = $arr["name"];
$res = mysql_query("SELECT email FROM users WHERE enabled='yes' AND notifs LIKE '%[cat$catid]%'") or sqlerr();
$uploader = $CURUSER['username'];
$size = mksize($totallen);
$description = ($html ? strip_tags($descr) : $descr);
$body = <<<EOD
A new torrent has been uploaded.
Name: $torrent
Size: $size
Category: $cat
Uploaded by: $uploader
Description
-------------------------------------------------------------------------------
$description
-------------------------------------------------------------------------------
You can use the URL below to download the torrent (you may have to login).
$DEFAULTBASEURL/details.php?id=$id&hit=1
--
$SITENAME
EOD;
$to = "";
$nmax = 100; // Max recipients per message
$nthis = 0;
$ntotal = 0;
$total = mysql_num_rows($res);
while ($arr = mysql_fetch_row($res))
{
if ($nthis == 0)
$to = $arr[0];
else
$to .= "," . $arr[0];
++$nthis;
++$ntotal;
if ($nthis == $nmax || $ntotal == $total)
{
if (!mail("Multiple recipients <$SITEEMAIL>", "New torrent - $torrent", $body,
"From: $SITEEMAIL\r\nBcc: $to", "-f$SITEEMAIL"))
stderr("Error", "Your torrent has been been uploaded. DO NOT RELOAD THE PAGE!\n" .
"There was however a problem delivering the e-mail notifcations.\n" .
"Please let an administrator know about this error!\n");
$nthis = 0;
}
}
*******************/
header("Location: $BASEURL/details.php?id=$id&uploaded=1");
?>