|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Torrent\Helper; |
|
4
|
|
|
|
|
5
|
|
|
class FileSystem |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* @var int |
|
10
|
|
|
*/ |
|
11
|
|
|
const timeout = 30; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* List of strings that can start a torrent file |
|
15
|
|
|
* |
|
16
|
|
|
* @static |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $torrentsChecks = array('d8:announce', 'd10:created', 'd13:creatio', 'd4:info', 'd9:'); |
|
20
|
|
|
|
|
21
|
|
|
public static function filesize($filename) |
|
22
|
|
|
{ |
|
23
|
|
|
if (is_file($filename)) { |
|
24
|
|
|
return (double) sprintf('%u', @filesize($filename)); |
|
25
|
|
|
} else { |
|
26
|
|
|
if ($content_length = preg_grep($pattern = '#^Content-Length:\s+(\d+)$#i', (array) @get_headers($filename)) |
|
27
|
|
|
) { |
|
28
|
|
|
return (int) preg_replace($pattern, '$1', reset($content_length)); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* pack data hash to binary |
|
35
|
|
|
* |
|
36
|
|
|
* @param $data |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function pack(& $data) |
|
41
|
|
|
{ |
|
42
|
|
|
return pack('H*', sha1($data)) . ($data = null); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param $url |
|
47
|
|
|
* |
|
48
|
|
|
* @return int |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function is_url($url) |
|
51
|
|
|
{ |
|
52
|
|
|
return preg_match('#^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$#i', $url); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $url |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function url_exists($url) |
|
61
|
|
|
{ |
|
62
|
|
|
return FileSystem::is_url($url) ? |
|
63
|
|
|
(bool) FileSystem::filesize($url) : |
|
64
|
|
|
false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $announces |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function untier($announces) |
|
73
|
|
|
{ |
|
74
|
|
|
$list = array(); |
|
75
|
|
|
foreach ((array) $announces as $tier) { |
|
76
|
|
|
is_array($tier) ? |
|
77
|
|
|
$list = array_merge($list, FileSystem::untier($tier)) : |
|
78
|
|
|
array_push($list, $tier); |
|
79
|
|
|
} |
|
80
|
|
|
return $list; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $array |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function is_list($array) |
|
89
|
|
|
{ |
|
90
|
|
|
foreach (array_keys($array) as $key) { |
|
91
|
|
|
if (!is_int($key)) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $path |
|
100
|
|
|
* @param $folder |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function path($path, $folder) |
|
105
|
|
|
{ |
|
106
|
|
|
array_unshift($path, $folder); |
|
107
|
|
|
return join(DIRECTORY_SEPARATOR, $path); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param $dir |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function scandir($dir) |
|
116
|
|
|
{ |
|
117
|
|
|
$paths = array(); |
|
118
|
|
|
foreach (scandir($dir) as $item) { |
|
119
|
|
|
if ($item != '.' && $item != '..') { |
|
120
|
|
|
if (is_dir($path = realpath($dir . DIRECTORY_SEPARATOR . $item))) { |
|
121
|
|
|
$paths = array_merge(self::scandir($path), $paths); |
|
122
|
|
|
} else { |
|
123
|
|
|
$paths[] = $path; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
return $paths; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param $data |
|
132
|
|
|
* |
|
133
|
|
|
* @return bool|string |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function char($data) |
|
136
|
|
|
{ |
|
137
|
|
|
return empty($data) ? |
|
138
|
|
|
false : |
|
139
|
|
|
substr($data, 0, 1); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public static function downloadTorrent($url, $timeout = self::timeout) |
|
143
|
|
|
{ |
|
144
|
|
|
if (ini_get('allow_url_fopen')) { |
|
145
|
|
|
return self::downloadViaStream($url, $timeout); |
|
146
|
|
|
} else { |
|
147
|
|
|
return self::downloadViaCurl($url, $timeout); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public static function downloadViaStream($url, $timeout = self::timeout) |
|
152
|
|
|
{ |
|
153
|
|
|
if (!ini_get('allow_url_fopen')) { |
|
154
|
|
|
throw new \Exception('Install CURL or enable "allow_url_fopen"'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return file_get_contents($url, false, stream_context_create(array('http' => array('timeout' => $timeout)))); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public static function downloadViaCurl($url, $timeout = self::timeout) |
|
161
|
|
|
{ |
|
162
|
|
|
if (!function_exists('curl_init')) { |
|
163
|
|
|
throw new \Exception('Install CURL or enable "allow_url_fopen"'); |
|
164
|
|
|
} |
|
165
|
|
|
$handle = curl_init($url); |
|
166
|
|
|
if ($timeout) { |
|
167
|
|
|
curl_setopt($handle, CURLOPT_TIMEOUT, $timeout); |
|
168
|
|
|
} |
|
169
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); |
|
170
|
|
|
$content = curl_exec($handle); |
|
171
|
|
|
curl_close($handle); |
|
172
|
|
|
return $content; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param $file |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function isTorrent($file) |
|
181
|
|
|
{ |
|
182
|
|
|
$start = substr($file, 0, 11); |
|
183
|
|
|
$check = false; |
|
184
|
|
|
foreach (self::$torrentsChecks as $value) { |
|
185
|
|
|
if (0 === strpos($start, $value)) { |
|
186
|
|
|
$check = true; |
|
187
|
|
|
continue; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $check; |
|
192
|
|
|
} |
|
193
|
|
|
} |