-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape.php
executable file
·64 lines (64 loc) · 2.56 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
<?php
require_once('config.php');
require_once('Bencode.php');
use \PureBencode\Bencode;
header('Content-Type: text/plain; charset=utf-8');
if (isset($_SERVER['QUERY_STRING'])) {
preg_match_all('/info_hash=([^&]*)/i', urldecode($_SERVER['QUERY_STRING']), $info_hash_match);
$clientInfoHashList = array_map(function ($v) { $v = strtolower(bin2hex($v)); if (strlen($v) !== 40) { die(Bencode::encode(array('failure reason' => ErrorMessage[3]))); } return $v; }, $info_hash_match[1]);
}
if (!isset($rawInfoHashList) || count($rawInfoHashList) < 1 || count($clientInfoHashList) < 1) {
die(Bencode::encode(array('failure reason' => ErrorMessage[2])));
} else if (count($clientInfoHashList) > 100) {
die(Bencode::encode(array('failure reason' => ErrorMessage[4])));
} else if (!empty($_SERVER['HTTP_USER_AGENT']) && (preg_match('/((^(xunlei?).?\d+.\d+.\d+.\d+)|cacao_torrent)/i', $_SERVER['HTTP_USER_AGENT']) === 1)) {
die(Bencode::encode(array('failure reason' => ErrorMessage[6])));
}
if (DBPort === null) {
die(Bencode::encode(array('failure reason' => ErrorMessage[11])));
} else {
$db = @new MySQLi(DBPAddress, DBUser, DBPass, DBName, DBPort, DBSocket);
if ($db->connect_errno > 0) {
die(Bencode::encode(array('failure reason' => ErrorMessage[1])));
}
}
/*
try {
$cache = new Redis();
if (CachePersistence) {
$cache->pconnect(CacheAddress, CachePort, CacheTimeout);
} else {
$cache->connect(CacheAddress, CachePort, CacheTimeout);
}
if (CacheAuth !== null) {
$cache->auth(CacheAuth);
}
if ($cache->ping() !== true) {
die(Bencode::encode(array('failure reason' => ErrorMessage[1])));
}
} catch (Exception $e) {
die(Bencode::encode(array('failure reason' => ErrorMessage[1])));
}
*/
$resBencodeArr = array('files' => array(), 'flags' => array('min_request_interval' => ScrapeMinInterval));
foreach ($clientInfoHashList as $clientInfoHash) {
$escapeValue = $db->escape_string($clientInfoHash);
$queryWhereSQL .= "info_hash = '{$escapeValue}' OR ";
$resBencodeArr['files'][hex2bin($clientInfoHash)]['downloaded'] = 0;
}
$queryWhereSQL = substr($queryWhereSQL, 0, -4) . ' LIMIT ' . count($clientInfoHashList);
$torrentTotalCompletedList = $db->query("SELECT info_hash, total_completed FROM Torrents {$queryWhereSQL}");
while ($torrentTotalCompleted = $torrentTotalCompletedList->fetch_assoc()) {
if (empty($torrentTotalCompleted['info_hash'])) {
continue;
}
$resBencodeArr['files'][hex2bin($torrentTotalCompleted['info_hash'])]['downloaded'] = $torrentTotalCompleted['total_completed'];
}
$db->close();
/*
if (!CachePersistence) {
$cache->close();
}
*/
echo Bencode::encode($resBencodeArr);
?>