forked from TOMMMMMMMMMC/GreatPosterWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbencodetorrent.class.php
147 lines (137 loc) · 4.2 KB
/
bencodetorrent.class.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
<?
/**
* Torrent class that contains some convenient functions related to torrent meta data
*/
class BencodeTorrent extends BencodeDecode {
private $PathKey = 'path';
public $Files = array();
public $Size = 0;
/**
* Create a list of the files in the torrent and their sizes as well as the total torrent size
*
* @return array with a list of files and file sizes
*/
public function file_list() {
if (empty($this->Dec)) {
return false;
}
$InfoDict = &$this->Dec['info'];
if (!isset($InfoDict['files'])) {
// Single-file torrent
$this->Size = (Int64::is_int($InfoDict['length'])
? Int64::get($InfoDict['length'])
: $InfoDict['length']);
$Name = (isset($InfoDict['name.utf-8'])
? $InfoDict['name.utf-8']
: $InfoDict['name']);
$this->Files[] = array($this->Size, $Name);
} else {
if (isset($InfoDict['path.utf-8']['files'][0])) {
$this->PathKey = 'path.utf-8';
}
foreach ($InfoDict['files'] as $File) {
$TmpPath = array();
foreach ($File[$this->PathKey] as $SubPath) {
$TmpPath[] = $SubPath;
}
$CurSize = (Int64::is_int($File['length'])
? Int64::get($File['length'])
: $File['length']);
$this->Files[] = array($CurSize, implode('/', $TmpPath));
$this->Size += $CurSize;
}
uasort($this->Files, function ($a, $b) {
return strnatcasecmp($a[1], $b[1]);
});
}
return array($this->Size, $this->Files);
}
/**
* Find out the name of the torrent
*
* @return string torrent name
*/
public function get_name() {
if (empty($this->Dec)) {
return false;
}
if (isset($this->Dec['info']['name.utf-8'])) {
return $this->Dec['info']['name.utf-8'];
}
return $this->Dec['info']['name'];
}
/**
* Find out the total size of the torrent
*
* @return string torrent size
*/
public function get_size() {
if (empty($this->Files)) {
if (empty($this->Dec)) {
return false;
}
$FileList = $this->file_list();
}
return $FileList[0];
}
/**
* Checks if the "private" flag is present in the torrent
*
* @return true if the "private" flag is set
*/
public function is_private() {
if (empty($this->Dec)) {
return false;
}
return isset($this->Dec['info']['private']) && Int64::get($this->Dec['info']['private']) == 1;
}
/**
* Add the "private" flag to the torrent
*
* @return true if a change was required
*/
public function make_private() {
if (empty($this->Dec)) {
return false;
}
if ($this->is_private()) {
return false;
}
$this->Dec['info']['private'] = Int64::make(1);
ksort($this->Dec['info']);
return true;
}
/**
* Adds thet "source" flag to the torrent to allow for easier cross-seeding
*
* @return bool false if the source flag was already set to GreatPosterWall, else true
*/
function set_source() {
if (empty($this->Dec)) {
return false;
}
if (isset($this->Dec['info']['source']) && $this->Dec['info']['source'] === TORRENT_SOURCE) {
return false;
}
$this->Dec['info']['source'] = TORRENT_SOURCE;
ksort($this->Dec['info']);
return true;
}
/**
* Calculate the torrent's info hash
*
* @return info hash in hexadecimal form
*/
public function info_hash() {
if (empty($this->Dec) || !isset($this->Dec['info'])) {
return false;
}
return sha1($this->encode(false, 'info'));
}
/**
* Add the announce URL to a torrent
*/
public static function add_announce_url($Data, $Url) {
return 'd8:announce' . strlen($Url) . ':' . $Url . substr($Data, 1);
}
}