-
Notifications
You must be signed in to change notification settings - Fork 4
/
scrape.php
198 lines (166 loc) · 7.19 KB
/
scrape.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
<?php
/////////////////////////////////////////////////////////////////////////////////////
// xbtit - Bittorrent tracker/frontend
//
// Copyright (C) 2004 - 2019 Btiteam
//
// This file is part of xbtit.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
////////////////////////////////////////////////////////////////////////////////////
$BASEPATH = __DIR__;
require "$BASEPATH/include/config.php";
require "$BASEPATH/include/common.php";
// protection against sql injection, xss attack
require_once $BASEPATH.'/include/crk_protection.php';
// check if we are using standard php tracker or xbtt backend
// if using xbbt we will make a redirect to xbtt announce using
// informations given in config + pid if private
// thank you petr1fied for the code.
if ($XBTT_USE) {
function implode_with_keys($glue, $array)
{
$output = [];
foreach ($array as $key => $item) {
$output[] = $key.'='.$item;
}
return implode($glue, $output);
}
if (isset($_GET['pid'])) {
$pid = $_GET['pid'];
unset($_GET['pid']);
} else {
$pid = '';
}
$query_string = implode_with_keys('&', $_GET);
if ($pid != '') { // private announce
header("Location: $XBTT_URL/$pid/scrape?".$query_string);
} else { // public
header("Location: $XBTT_URL/scrape?".$query_string);
}
exit;
}
// not using xbtt, let us go ;)
// controll if client can handle gzip
if ($GZIP_ENABLED) {
if (stristr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && extension_loaded('zlib') && ini_get('zlib.output_compression') == 0) {
if (ini_get('output_handler') != 'ob_gzhandler') {
ob_start('ob_gzhandler');
} else {
ob_start();
}
} else {
ob_start();
}
}
// end gzip controll
error_reporting(0);
// connect to db
if ($GLOBALS['persist']) {
$conres = ($GLOBALS['conn'] = mysqli_connect($dbhost, $dbuser, $dbpass)) or show_error('Tracker errore - mysql_connect: '.((is_object($GLOBALS['conn'])) ? mysqli_error($GLOBALS['conn']) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
} else {
$conres = ($GLOBALS['conn'] = mysqli_connect($dbhost, $dbuser, $dbpass)) or show_error('Tracker errore - mysql_connect: '.((is_object($GLOBALS['conn'])) ? mysqli_error($GLOBALS['conn']) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
}
((bool) mysqli_query($GLOBALS['conn'], "USE $database")) or show_error("Tracker errore - $database - ".((is_object($GLOBALS['conn'])) ? mysqli_error($GLOBALS['conn']) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
if (isset($_GET['pid'])) {
$pid = $_GET['pid'];
} else {
$pid = '';
}
if (strpos($pid, '?')) {
$tmp = substr($pid, strpos($pid, '?'));
$pid = substr($pid, 0, strpos($pid, '?'));
$tmpname = substr($tmp, 1, strpos($tmp, '=') - 1);
$tmpvalue = substr($tmp, strpos($tmp, '=') + 1);
$_GET[$tmpname] = $tmpvalue;
}
$usehash = false;
$pid = addslashes($pid);
// if private announce turned on and PID empty string or not send by client
if (($pid == '' || !$pid) && $PRIVATE_SCRAPE) {
show_error('Sorry. Private scrape is ON and PID system is required');
}
if (isset($_GET['info_hash'])) {
if ($pid != '') {
$qryStr = substr($_SERVER['QUERY_STRING'], strlen("?pid=$pid"));
} else {
$qryStr = $_SERVER['QUERY_STRING'];
}
// support for multi-scrape
// more info @ http://wiki.depthstrike.com/index.php/P2P:Programming:Trackers:PHP:Multiscrape
foreach (explode('&', $qryStr) as $item) {
if (substr($item, 0, 10) == 'info_hash=') {
$ihash = urldecode(substr($item, 10));
if (strlen($ihash) == 20) {
$ihash = bin2hex($ihash);
} elseif (strlen($ihash) == 40) {
if (!verifyHash($ihash)) {
continue; //showError(INVALID_INFO_HASH);
} else {
continue; // showError(INVALID_INFO_HASH);
}
}
$newmatches[] = $ihash;
}
}
if (get_magic_quotes_gpc()) {
//$info_hash = stripslashes($_GET["info_hash"]);
$info_hash = stripslashes(implode($newmatches, "','"));
} else { // $info_hash = $_GET["info_hash"];
$info_hash = implode($newmatches, "','");
}
$info_hash = strtolower("('$info_hash')");
$usehash = true;
}
if ($usehash) {
// $query = mysql_query("SELECT info_hash, filename FROM namemap WHERE external='no' AND info_hash=\"$info_hash\"");
$query = mysqli_query($GLOBALS['conn'], "SELECT info_hash, filename FROM {$TABLE_PREFIX}files WHERE external='no' AND info_hash IN $info_hash");
} else {
$query = mysqli_query($GLOBALS['conn'], "SELECT info_hash, filename FROM {$TABLE_PREFIX}files WHERE external='no'");
}
$namemap = [];
while ($row = mysqli_fetch_row($query)) {
$namemap[$row[0]] = $row[1];
}
if ($usehash) {
$query = mysqli_query($GLOBALS['conn'], "SELECT f.info_hash, f.seeds, f.leechers, f.finished FROM {$TABLE_PREFIX}files f WHERE external='no' AND info_hash IN $info_hash") or show_error('Database error. Cannot complete request.');
} else {
$query = mysqli_query($GLOBALS['conn'], "SELECT f.info_hash, f.seeds, f.leechers, f.finished FROM {$TABLE_PREFIX}files f WHERE external='no' ORDER BY info_hash") or show_error('Database error. Cannot complete request.');
}
$result = 'd5:filesd';
while ($row = mysqli_fetch_row($query)) {
$hash = hex2bin($row[0]);
$result .= '20:'.$hash.'d';
$result .= '8:completei'.$row[1].'e';
$result .= '10:downloadedi'.$row[3].'e';
$result .= '10:incompletei'.$row[2].'e';
if (isset($namemap[$row[0]])) {
$result .= '4:name'.strlen($namemap[$row[0]]).':'.$namemap[$row[0]];
}
$result .= 'e';
}
$result .= 'ee';
echo $result;
((is_null($___mysqli_res = mysqli_close($GLOBALS['conn']))) ? false : $___mysqli_res);
?>