-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtracker.mysql.php
More file actions
817 lines (705 loc) · 24.9 KB
/
tracker.mysql.php
File metadata and controls
817 lines (705 loc) · 24.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
<?php
/*
___. .__ __ __ __ __
\_ |__ |__|/ |__/ |_ ___.__._/ |_ __________________ ____ _____/ |_
| __ \| \ __\ __< | |\ __\/ _ \_ __ \_ __ \_/ __ \ / \ __\
| \_\ \ || | | | \___ | | | ( <_> ) | \/| | \/\ ___/| | \ |
|___ /__||__| |__| / ____| |__| \____/|__| |__| \___ >___| /__|
\/ \/ \/ \/
Contact: contact.atmoner@gmail.com
This file is part of Bittytorrent.
Bittytorrent 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.
Bittytorrent 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 Bittytorrent. If not, see <http://www.gnu.org/licenses/>.
*/
// tracker state
$_SERVER['tracker'] = array(
// general tracker options
/* 'open_tracker' => $conf['open_tracker'],
'announce_interval' => $conf['announce_interval'],
'min_interval' => $conf['min_interval'],
'default_peers' => $conf['default_peers'],
'max_peers' => $conf['max_peers'], */
// general tracker options
'open_tracker' => FALSE, /* track anything announced to it */
'announce_interval' => 1800, /* how often client will send requests */
'min_interval' => 900, /* how often client can force requests */
'default_peers' => 50, /* default # of peers to announce */
'max_peers' => 100, /* max # of peers to announce */
// advanced tracker options
'external_ip' => TRUE, /* allow client to specify ip address */
'force_compact' => FALSE, /* force compact announces only */
'full_scrape' => FALSE, /* allow scrapes without info_hash */
'random_limit' => 500, /* if peers > #, use alternate SQL RAND() */
'clean_idle_peers' => 1, /* tweaks % of time tracker attempts idle peer removal */
/* if you have a busy tracker, you may adjust this */
/* example: 10 = 10%, 20 = 5%, 50 = 2%, 100 = 1% */
// database options
'db_host' => 'localhost', /* ip or hostname to mysql server */
'db_user' => 'root', /* username used to connect to mysql */
'db_pass' => '*igpeLaF', /* password used to connect to mysql */
'db_name' => 'torrentempire', /* name of the PeerTracker database */
// advanced database options
'db_prefix' => '', /* name prefixes for the PeerTracker tables */
'db_persist' => false, /* use persistent connections if available. */
);
// var_dump($_SERVER['tracker']);
// Tracker Operations //////////////////////////////////////////////////////////////////////////////
// fatal error, stop execution
function tracker_error($error)
{
exit('d14:failure reason' . strlen($error) . ":{$error}e");
}
// MySQL Database API //////////////////////////////////////////////////////////////////////////////
// mysql core
class peertracker_mysql
{
// database connection
public $db;
// connect to database
public function __construct()
{
// attempt to connect
if (
// connection method
!$this->db = (!$_SERVER['tracker']['db_persist'] ?
// default connection
mysql_connect(
$_SERVER['tracker']['db_host'],
$_SERVER['tracker']['db_user'],
$_SERVER['tracker']['db_pass']
) :
// persistent connection
mysql_pconnect(
$_SERVER['tracker']['db_host'],
$_SERVER['tracker']['db_user'],
$_SERVER['tracker']['db_pass']
)
// select database
) OR !mysql_select_db(
$_SERVER['tracker']['db_name'], $this->db
)
// error out if something happened
) tracker_error(
mysql_errno($this->db) . ' - ' .
mysql_error($this->db)
);
}
// close database connection
public function __destruct()
{
mysql_close($this->db);
}
// make sql safe
public function escape_sql($sql)
{
return mysql_real_escape_string($sql, $this->db);
}
// query database
public function query($sql)
{
return mysql_query($sql, $this->db);
}
// return one row
public function fetch_once($sql)
{
// execute query
$query = mysql_query($sql, $this->db) OR tracker_error(mysql_error($this->db));
$result = mysql_fetch_row($query);
// cleanup
mysql_free_result($query);
// return
return $result;
}
// return compact peers
public function peers_compact($sql, &$peers)
{
// fetch peers
$query = mysql_query($sql, $this->db) OR tracker_error('failed to select compact peers');
// build response
while($peer = mysql_fetch_row($query)) $peers .= $peer[0];
// cleanup
mysql_free_result($query);
}
// return dictionary peers
public function peers_dictionary($sql, &$response)
{
// fetch peers
$query = mysql_query($sql, $this->db) OR tracker_error('failed to select peers');
// dotted decimal string ip, 20-byte peer_id, integer port
while($peer = mysql_fetch_row($query)) $response .= 'd2:ip' . strlen($peer[1]) . ":{$peer[1]}" . "7:peer id20:{$peer[0]}4:porti{$peer[2]}ee";
// cleanup
mysql_free_result($query);
}
// return dictionary peers without peer_id
public function peers_dictionary_no_peer_id($sql, &$response)
{
// fetch peers
$query = mysql_query($sql, $this->db) OR tracker_error('failed to select peers');
// dotted decimal string ip, integer port
while($peer = mysql_fetch_row($query)) $response .= 'd2:ip' . strlen($peer[0]) . ":{$peer[0]}4:porti{$peer[1]}ee";
// cleanup
mysql_free_result($query);
}
// full scrape of all torrents
public function full_scrape($sql, &$response)
{
// fetch scrape
$query = mysql_query($sql) OR tracker_error('unable to perform a full scrape');
// 20-byte info_hash, integer complete, integer downloaded, integer incomplete
while ($scrape = mysql_fetch_row($query)) $response .= "20:{$scrape[0]}d8:completei{$scrape[1]}e10:downloadedi0e10:incompletei{$scrape[2]}ee";
// cleanup
mysql_free_result($query);
}
}
// MySQLi Database API /////////////////////////////////////////////////////////////////////////////
// mysqli core
class peertracker_mysqli
{
// database connection
public $db;
// connect to database
public function __construct()
{
// attempt to connect
$this->db = new mysqli(
// connection method
(!$_SERVER['tracker']['db_persist'] ?
// default connection
$_SERVER['tracker']['db_host'] :
// persistent connection
'p:' . $_SERVER['tracker']['db_host']
),
$_SERVER['tracker']['db_user'],
$_SERVER['tracker']['db_pass'],
$_SERVER['tracker']['db_name']
);
// error out if something happened
if ($this->db->connect_errno) tracker_error(
$this->db->connect_errno . ' - ' .
$this->db->connect_error
);
}
// close database connection
public function __destruct()
{
$this->db->close();
}
// make sql safe
public function escape_sql($sql)
{
return $this->db->real_escape_string($sql);
}
// query database
public function query($sql)
{
return $this->db->query($sql);
}
// return one row
public function fetch_once($sql)
{
// execute query
$query = $this->db->query($sql) OR tracker_error($this->db->error);
$result = $query->fetch_row();
// cleanup
$query->close();
// return
return $result;
}
public function fetch_array($sql)
{
// execute query
$query = $this->db->query($sql) OR tracker_error($this->db->error);
$result = $query->fetch_array();
// cleanup
$query->close();
// return
return $result;
}
public function fetch_all($sql)
{
// execute query
$query = $this->db->query($sql) OR tracker_error($this->db->error);
$result = $query->fetch_assoc();
// cleanup
$query->close();
// return
return $result;
}
// return compact peers
public function peers_compact($sql, &$peers)
{
// fetch peers
$query = $this->db->query($sql) OR tracker_error('failed to select compact peers');
// build response
while($peer = $query->fetch_row()) $peers .= $peer[0];
// cleanup
$query->close();
}
// return dictionary peers
public function peers_dictionary($sql, &$response)
{
// fetch peers
$query = $this->db->query($sql) OR tracker_error('failed to select peers');
// dotted decimal string ip, 20-byte peer_id, integer port
while($peer = $query->fetch_row()) $response .= 'd2:ip' . strlen($peer[1]) . ":{$peer[1]}" . "7:peer id20:{$peer[0]}4:porti{$peer[2]}ee";
// cleanup
$query->close();
}
// return dictionary peers without peer_id
public function peers_dictionary_no_peer_id($sql, &$response)
{
// fetch peers
$query = $this->db->query($sql) OR tracker_error('failed to select peers');
// dotted decimal string ip, integer port
while($peer = $query->fetch_row()) $response .= 'd2:ip' . strlen($peer[0]) . ":{$peer[0]}4:porti{$peer[1]}ee";
// cleanup
$query->close();
}
// full scrape of all torrents
public function full_scrape($sql, &$response)
{
// fetch scrape
$query = $this->db->query($sql) OR tracker_error('unable to perform a full scrape');
// 20-byte info_hash, integer complete, integer downloaded, integer incomplete
while ($scrape = $query->fetch_row()) $response .= "20:{$scrape[0]}d8:completei{$scrape[1]}e10:downloadedi0e10:incompletei{$scrape[2]}ee";
// cleanup
$query->close();
}
}
// peertracker core
class peertracker
{
// database api
public static $api;
// open database connection
public static function open()
{
// php version
$php = PHP_VERSION;
// establish database API
self::$api = (
// default connections
!$_SERVER['tracker']['db_persist'] ? (
// do we support mysqli?
class_exists('mysqli') ?
// use the mysqli api
new peertracker_mysqli() :
// use the mysql api
new peertracker_mysql()
// persistent connections
) : (
// do we support mysqli?
class_exists('mysqli') &&
// PHP is >= 5.3
(($php[0]+0) > 5 || ($php[0]+0) == 5 && ($php[2]+0) >= 3) ?
// use the mysqli api
new peertracker_mysqli() :
// use the mysql api
new peertracker_mysql()
)
);
}
// close database connection
public static function close()
{
// trigger __destruct()
self::$api = null;
}
// database cleanup
public static function clean()
{
// run cleanup once per announce interval
// check 'clean_idle_peers'% of the time to avoid excess queries
if (mt_rand(1, $_SERVER['tracker']['clean_idle_peers']) == 1)
{
// unix timestamp
$time = time();
// fetch last cleanup time
$last = self::$api->fetch_once(
// select last cleanup from tasks
"SELECT value FROM `{$_SERVER['tracker']['db_prefix']}tasks` WHERE name='prune'"
);
// first clean cycle?
if (($last[0] + 0) == 0)
{
self::$api->query(
// set tasks value prune to current unix timestamp
"REPLACE INTO `{$_SERVER['tracker']['db_prefix']}tasks` VALUES('prune', {$time})"
) OR tracker_error('could not perform maintenance');
self::$api->query(
// delete peers that have been idle too long
"DELETE FROM `{$_SERVER['tracker']['db_prefix']}peers` WHERE updated < " .
// idle length is announce interval x 2
($time - ($_SERVER['tracker']['announce_interval'] * 2))
) OR tracker_error('could not perform maintenance');
}
// prune idle peers
elseif (($last[0] + $_SERVER['tracker']['announce_interval']) < $time)
{
self::$api->query(
// set tasks value prune to current unix timestamp
"UPDATE `{$_SERVER['tracker']['db_prefix']}tasks` SET value={$time} WHERE name='prune'"
) OR tracker_error('could not perform maintenance');
self::$api->query(
// delete peers that have been idle too long
"DELETE FROM `{$_SERVER['tracker']['db_prefix']}peers` WHERE updated < " .
// idle length is announce interval x 2
($time - ($_SERVER['tracker']['announce_interval'] * 2))
) OR tracker_error('could not perform maintenance');
}
}
}
// insert new peer
public static function new_peer()
{
self::$api->query(
// insert into the peers table
"INSERT IGNORE INTO `{$_SERVER['tracker']['db_prefix']}peers` " .
// table columns
'(torrent, info_hash, userid, peer_id, compact, ip, port, state, updated) ' .
// 20-byte info_hash, 20-byte peer_id
"VALUES ('$torrentid', '{$_GET['info_hash']}','$userid', '{$_GET['peer_id']}', '" .
// 6-byte compacted peer info
self::$api->escape_sql(pack('Nn', ip2long($_GET['ip']), $_GET['port'])) . "', " .
// dotted decimal string ip, integer port, integer state and unix timestamp updated
"'{$_GET['ip']}', {$_GET['port']}, {$_SERVER['tracker']['seeding']}, " . time() . '); '
) OR tracker_error('failed to add new peer data');
}
// full peer update
public static function update_peer()
{
// update peer
self::$api->query(
// update the peers table
"UPDATE `{$_SERVER['tracker']['db_prefix']}peers` " .
// set the 6-byte compacted peer info
"SET compact='" . self::$api->escape_sql(pack('Nn', ip2long($_GET['ip']), $_GET['port'])) .
// dotted decimal string ip, integer port
"', ip='{$_GET['ip']}', port={$_GET['port']}, " .
// integer state and unix timestamp updated
"state={$_SERVER['tracker']['seeding']}, updated=" . time() .
// that matches the given info_hash and peer_id
" WHERE info_hash='{$_GET['info_hash']}' AND peer_id='{$_GET['peer_id']}'"
) OR tracker_error('failed to update peer data');
}
// update peers last access time
public static function update_last_access()
{
// update peer
self::$api->query(
// set updated to the current unix timestamp
"UPDATE `{$_SERVER['tracker']['db_prefix']}peers` SET updated=" . time() .
// that matches the given info_hash and peer_id
" WHERE info_hash='{$_GET['info_hash']}' AND peer_id='{$_GET['peer_id']}'"
) OR tracker_error('failed to update peers last access');
}
// remove existing peer
public static function delete_peer()
{
// delete peer
self::$api->query(
// delete a peer from the peers table
"DELETE FROM `{$_SERVER['tracker']['db_prefix']}peers` " .
// that matches the given info_hash and peer_id
"WHERE info_hash='{$_GET['info_hash']}' AND peer_id='{$_GET['peer_id']}'"
) OR tracker_error('failed to remove peer data');
/*if (isset($self))
{
mysql_query("DELETE FROM peers WHERE $selfwhere");
if (mysql_affected_rows())
{
if ($self["seeder"] == "yes")
$updateset[] = "seeders = seeders - 1";
else
$updateset[] = "leechers = leechers - 1";
}
}*/
}
// tracker event handling
public static function event()
{
$updateset = array();
// execute peer select
$pState = self::$api->fetch_once(
// select a peer from the peers table
"SELECT ip, port, state FROM `{$_SERVER['tracker']['db_prefix']}peers` " .
// that matches the given info_hash and peer_id
"WHERE info_hash='{$_GET['info_hash']}' AND peer_id='{$_GET['peer_id']}'"
);
// process tracker event
switch ((isset($_GET['event']) ? $_GET['event'] : false))
{
// client gracefully exited
case 'stopped':
// remove peer
if (isset($pState[2])) self::delete_peer();
break;
// client completed download
case 'completed':
// force seeding status
self::completed();
$_SERVER['tracker']['seeding'] = 1;
// client started download
case 'started':
// client continuing download
default:
// new peer
if (!isset($pState[2])) self::new_peer();
// peer status
elseif (
// check that ip addresses match
$pState[0] != $_GET['ip'] ||
// check that listening ports match
($pState[1]+0) != $_GET['port'] ||
// check whether seeding status match
($pState[2]+0) != $_SERVER['tracker']['seeding']
) self::update_peer();
// update time
else self::update_last_access();
}
}
// tracker peer list
public static function peers()
{
// fetch peer total
$total = self::$api->fetch_once(
// select a count of the number of peers that match the given info_hash
"SELECT COUNT(*) FROM `{$_SERVER['tracker']['db_prefix']}peers` WHERE info_hash='{$_GET['info_hash']}'"
) OR tracker_error('failed to select peer count');
// select
$sql = 'SELECT ' .
// 6-byte compacted peer info
($_GET['compact'] ? 'compact ' :
// 20-byte peer_id
(!$_GET['no_peer_id'] ? 'peer_id, ' : '') .
// dotted decimal string ip, integer port
'ip, port '
) .
// from peers table matching info_hash
"FROM `{$_SERVER['tracker']['db_prefix']}peers` WHERE info_hash='{$_GET['info_hash']}'" .
// less peers than requested, so return them all
($total[0] <= $_GET['numwant'] ? ';' :
// if the total peers count is low, use SQL RAND
($total[0] <= $_SERVER['tracker']['random_limit'] ?
" ORDER BY RAND() LIMIT {$_GET['numwant']};" :
// use a more efficient but less accurate RAND
" LIMIT {$_GET['numwant']} OFFSET " .
mt_rand(0, ($total[0]-$_GET['numwant']))
)
);
// begin response
$response = 'd8:intervali' . $_SERVER['tracker']['announce_interval'] .
'e12:min intervali' . $_SERVER['tracker']['min_interval'] .
'e5:peers';
// compact announce
if ($_GET['compact'])
{
// peers list
$peers = '';
// build response
self::$api->peers_compact($sql, $peers);
// 6-byte compacted peer info
$response .= strlen($peers) . ':' . $peers;
}
// dictionary announce
else
{
// list start
$response .= 'l';
// include peer_id
if (!$_GET['no_peer_id']) self::$api->peers_dictionary($sql, $response);
// omit peer_id
else self::$api->peers_dictionary_no_peer_id($sql, $response);
// list end
$response .= 'e';
}
// send response
echo $response . 'e';
// cleanup
unset($peers);
}
// Private tracking
public static function updateUser()
{
global $db;
// Pid for private tracker
// Only if private tracker is active in admin panel
// echo $pid=md5(uniqid(rand(),true)).'<br />';
//if ($_SERVER['tracker']['open_tracker'] === 'false') {
if (!isset($_GET['passkey']) || strlen($_GET['passkey']) != 32)
tracker_error('This is private tracker! Use your "Personal Passkey" for download this torrent');
else {
$user = self::$api->fetch_once("SELECT id FROM users WHERE passkey = '".self::$api->escape_sql($_GET['passkey'])."' LIMIT 1");
if ($user) {
self::$api->query("UPDATE users SET
upload=IFNULL(upload,0)+ ".self::$api->escape_sql($_GET['uploaded']).",
download=IFNULL(download,0)+ ".self::$api->escape_sql($_GET['downloaded'])."
WHERE id = '".self::$api->escape_sql($user->id)."'");
} else
tracker_error('You are not authorized to download this torrent!');
}
//}
}
public function completed()
{
$ratiocompleted = floor($uploaded / $downloaded * 1000) / 1000;
$updateset[] = "times_completed = times_completed + 1";
//===================== check if torrent has been completed alredy if zo ignore this ======= Decoder ==========
$torrent_chack = mysql_query("SELECT * FROM downloaded WHERE user=$userid AND torrent=$torrentid") or err("Error Event completed client");
$torrent_check = mysql_fetch_array($torrent_chack);
if (!$torrent_check)//================== end of check==== sql below is default dont touch it or you screw up announce.php lol ==========
{
mysql_query("INSERT INTO downloaded (torrent, user, added, username, uploaded, downloaded) VALUES ($torrentid, $userid, NOW(), '$username', $uploaded, $downloaded)") or err("FOUT2");
}
}
// tracker scrape
public static function scrape()
{
// scrape response
$response = 'd5:filesd';
// scrape info_hash
if (isset($_GET['info_hash']))
{
// scrape
$scrape = self::$api->fetch_once(
// select total seeders and leechers
'SELECT SUM(state=1), SUM(state=0) ' .
// from peers
"FROM `{$_SERVER['tracker']['db_prefix']}peers` " .
// that match info_hash
"WHERE info_hash='" . self::$api->escape_sql($_GET['info_hash']) . "'"
) OR tracker_error('unable to scrape the requested torrent');
// 20-byte info_hash, integer complete, integer downloaded, integer incomplete
$response .= "20:{$_GET['info_hash']}d8:completei" . ($scrape[0]+0) .
'e10:downloadedi0e10:incompletei' . ($scrape[1]+0) . 'ee';
}
// full scrape
else
{
// scrape
$sql = 'SELECT ' .
// info_hash, total seeders and leechers
'info_hash, SUM(state=1), SUM(state=0) ' .
// from peers
"FROM `{$_SERVER['tracker']['db_prefix']}peers` " .
// grouped by info_hash
'GROUP BY info_hash';
// build response
self::$api->full_scrape($sql, $response);
}
// send response
echo $response . 'ee';
}
// tracker statistics
public static function stats()
{
/* // statistics
$stats = self::$api->fetch_once(
// select seeders and leechers
'SELECT SUM(state=1), SUM(state=0), ' .
// unique torrents
'COUNT(DISTINCT info_hash) ' .
// from peers
"FROM `{$_SERVER['tracker']['db_prefix']}peers` "
) OR tracker_error('failed to retrieve tracker statistics');
*/
if (isset($_GET['info_hash'])) {
// statistics
$stats = self::$api->fetch_once(
// select seeders and leechers
// 'SELECT SUM(state=1), SUM(state=0),' .
'SELECT SUM(state=1), SUM(state=0), ' .
// unique torrents
'info_hash ' .
// from peers
"FROM `{$_SERVER['tracker']['db_prefix']}peers` "
// Where hash
. "WHERE `info_hash` =UNHEX( '" . self::$api->escape_sql($_GET['info_hash']) . "' )"
) OR tracker_error('failed to retrieve tracker statistics');
} else {
// statistics
$stats = self::$api->fetch_once(
// select seeders and leechers
'SELECT SUM(state=1), SUM(state=0),' .
// unique torrents
'COUNT(DISTINCT info_hash) ' .
// from peers
"FROM `{$_SERVER['tracker']['db_prefix']}peers` "
// Where hash
) OR tracker_error('failed to retrieve tracker statistics');
}
// output format
if (isset($_GET['info_hash']))
{
switch ($_GET['stats'])
{
// xml
case 'xml':
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>' .
'<tracker version="$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $">' .
'<peers>' . number_format($stats[0] + $stats[1]) . '</peers>' .
'<seeders>' . number_format($stats[0]) . '</seeders>' .
'<leechers>' . number_format($stats[1]) . '</leechers>' .
'<infohash>' . bin2hex($stats[2]) . '</infohash></tracker>';
break;
// json
case 'json':
header('Content-Type: text/javascript');
echo '{"tracker":{"version":"$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $",' .
'"peers": "' . number_format($stats[0] + $stats[1]) . '",' .
'"seeders":"' . number_format($stats[0]) . '",' .
'"leechers":"' . number_format($stats[1]) . '",' .
'"infohash":"' . bin2hex($stats[2]) . '"}}';
break;
// standard
default:
echo '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">' .
'<title>PeerTracker: $Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $</title>' .
'<body><pre>' . number_format($stats[0] + $stats[1]) .
' peers (' . number_format($stats[0]) . ' seeders + ' . number_format($stats[1]) .
' leechers) on info hash ' . bin2hex($stats[2]) . '</pre></body></html>';
}
} else {
// Sans info hash
switch ($_GET['stats'])
{
// xml
case 'xml':
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>' .
'<tracker version="$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $">' .
'<peers>' . number_format($stats[0] + $stats[1]) . '</peers>' .
'<seeders>' . number_format($stats[0]) . '</seeders>' .
'<leechers>' . number_format($stats[1]) . '</leechers>' .
'<torrents>' . number_format($stats[2]) . '</torrents></tracker>';
break;
// json
case 'json':
header('Content-Type: text/javascript');
echo '{"tracker":{"version":"$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $",' .
'"peers": "' . number_format($stats[0] + $stats[1]) . '",' .
'"seeders":"' . number_format($stats[0]) . '",' .
'"leechers":"' . number_format($stats[1]) . '",' .
'"torrents":"' . number_format($stats[2]) . '"}}';
break;
// standard
default:
echo '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">' .
'<title>PeerTracker: $Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $</title>' .
'<body><pre>' . number_format($stats[0] + $stats[1]) .
' peers (' . number_format($stats[0]) . ' seeders + ' . number_format($stats[1]) .
' leechers) in ' . number_format($stats[2]) . ' torrents</pre></body></html>';
}
}
}
}
?>