-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path_data.php
More file actions
140 lines (115 loc) · 4.9 KB
/
_data.php
File metadata and controls
140 lines (115 loc) · 4.9 KB
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
<?php
/*
* Bitstorm 2 - A small and fast BitTorrent tracker
* Copyright 2011 Peter Caprioli
* Copyright 2015 Wilhelm Svenselius
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** @var mysqli $_sql */
$_sql = null;
function dbConnect() {
global $_sql;
$_sql = new mysqli(__DB_SERVER, __DB_USERNAME, __DB_PASSWORD, __DB_DATABASE);
if($_sql->connect_errno) {
die(trackError('Database connection failed'));
}
}
function dbUpdatePeer($peerId, $userAgent, $ipAddress, $key, $port) {
global $_sql;
$insert = $_sql->prepare('INSERT INTO bit_peer ( hash, user_agent, ip_address, key_hash, port ) VALUES ( ?, ?, ?, ?, ? ) '
. 'ON DUPLICATE KEY UPDATE user_agent = VALUES(user_agent), ip_address = VALUES(ip_address), port = VALUES(port), id = LAST_INSERT_ID(id)');
if(NULL == $userAgent) {
$userAgent = 'N/A';
}
$peerId = bin2hex($peerId);
$userAgent = substr($userAgent, 0, 80);
$ipAddress = substr($ipAddress, 0, 40);
$key = sha1($key);
$insert->bind_param('ssssi', $peerId, $userAgent, $ipAddress, $key, $port);
if(!$insert->execute()) {
die(trackError('Database failed when updating peer: ' . $insert->errno));
}
$insert->close();
return $_sql->insert_id;
}
function dbUpdateTorrent($infoHash) {
global $_sql;
$insert = $_sql->prepare('INSERT INTO bit_torrent ( hash ) VALUES ( ? ) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)');
$infoHash = bin2hex($infoHash);
$insert->bind_param('s', $infoHash);
if(!$insert->execute()) {
die(trackError('Database failed when updating torrent: ' . $insert->errno));
}
$insert->close();
return $_sql->insert_id;
}
function dbUpdatePeerTorrent($peerPk, $uploaded, $downloaded, $remain, $infoHash) {
global $_sql;
$insert = $_sql->prepare('INSERT INTO bit_peer_torrent ( peer_id, torrent_id, uploaded, downloaded, remain, last_updated ) '
. 'SELECT ?, bit_torrent.id, ?, ?, ?, UTC_TIMESTAMP() FROM bit_torrent WHERE bit_torrent.hash = ? '
. 'ON DUPLICATE KEY UPDATE uploaded = VALUES(uploaded), downloaded = VALUES(downloaded), remain = VALUES(remain), last_updated = VALUES(last_updated), id = LAST_INSERT_ID(bit_peer_torrent.id)');
$infoHash = bin2hex($infoHash);
$insert->bind_param('iiiis', $peerPk, $uploaded, $downloaded, $remain, $infoHash);
if(!$insert->execute()) {
die(trackError('Database failed when updating peers on torrent: ' . $insert->errno));
}
$insert->close();
return $_sql->insert_id;
}
function dbStoppedPeer($peerTorrentPk) {
global $_sql;
$update = $_sql->prepare('UPDATE bit_peer_torrent SET stopped = 1 WHERE id = ?');
$update->bind_param('i', $peerTorrentPk);
if(!$update->execute()) {
die(trackError('Database failed when updating peer on torrent: ' . $update->errno));
}
$update->close();
}
function dbGetPeers($torrentPk, $peerPk, $limit) {
global $_sql;
$select = $_sql->prepare('SELECT bit_peer.ip_address, bit_peer.port, bit_peer.hash FROM bit_peer_torrent '
. 'LEFT JOIN bit_peer ON bit_peer.id = bit_peer_torrent.peer_id '
. 'WHERE bit_peer_torrent.torrent_id = ? AND bit_peer_torrent.stopped = 0 '
. 'AND bit_peer_torrent.last_updated >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL ? SECOND) '
. 'AND bit_peer.id != ? '
. 'ORDER BY RAND() '
. 'LIMIT ?');
$interval = __INTERVAL + __TIMEOUT;
$select->bind_param('iiii', $torrentPk, $interval, $peerPk, $limit);
if(!$select->execute()) {
die(trackError('Database failed when getting peers: ' . $select->errno));
}
if(!($result = $select->get_result())) {
die(trackError('Database failed when getting peers: ' . $select->errno));
}
return $result->fetch_all();
}
function dbGetCounts($torrentPk) {
global $_sql;
$select = $_sql->prepare('SELECT IFNULL(SUM(remain > 0), 0) AS leech, IFNULL(SUM(remain = 0), 0) AS seed FROM bit_peer_torrent '
. 'WHERE bit_peer_torrent.torrent_id = ? AND bit_peer_torrent.stopped = 0 '
. 'AND bit_peer_torrent.last_updated >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL ? SECOND) '
. 'GROUP BY bit_peer_torrent.torrent_id');
$interval = __INTERVAL + __TIMEOUT;
$select->bind_param('ii', $torrentPk, $interval);
if(!$select->execute()) {
die(trackError('Database failed when getting counts: ' . $select->errno));
}
$seeders = 0;
$leechers = 0;
$select->bind_result($leechers, $seeders);
$select->fetch();
return array($seeders, $leechers);
}