-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP.php
343 lines (299 loc) · 9.78 KB
/
HTTP.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
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
<?php
namespace CleantalkSP\Common\Helpers;
use CleantalkSP\Common\Validate;
use CleantalkSP\Templates\Singleton;
use CleantalkSP\Common\HTTP\Request;
/**
* Class HTTP
* Gather static functions designed to work with Common\HTTP\Request lib
* Such as request and URL-related things
*
* Uses "singleton" template to store already discovered HTTP-headers, requests results and whole requests
*
* @version 1.0.0
* @package CleantalkSP\Common\Helpers
* @author Cleantalk team (welcome@cleantalk.org)
* @copyright (C) CleanTalk team (http://cleantalk.org)
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
* @see https://github.com/CleanTalk/security-malware-firewall
*/
class HTTP
{
use Singleton;
/**
* @var array Stored HTTP headers
*/
public $http_headers = [];
/**
* Appends given parameter(s) to URL considering other parameters
* Adds ? or & before the append
*
* @param string $url
* @param string|array $parameters
*
* @return string
*/
public static function appendParametersToURL($url, $parameters)
{
if (empty($parameters)) {
return $url;
}
$parameters = is_array($parameters)
? http_build_query($parameters)
: $parameters;
$url .= strpos($url, '?') === false
? ('?' . $parameters)
: ('&' . $parameters);
return $url;
}
/**
* Remove given parameter(s) from URL considering other parameters
* Adds ? or & before the appendix
*
* @param string $url
* @param string|array $parameters
*
* @return string
* @psalm-suppress PossiblyUnusedMethod
*/
public static function removeParametersFromURL($url, $parameters)
{
foreach ((array) $parameters as $parameter) {
$url = preg_replace('/([?&]' . $parameter . '=.+?($|&))/', '', $url);
}
return $url;
}
/**
* Wrapper for http_request
* Requesting HTTP response code for $url
*
* @param string $url
* @param bool $ignore_redirects - if it needs to ignore redirects
*
* @return false|int
*/
public static function getResponseCode($url, $ignore_redirects = false)
{
$http = new Request();
$presets = ['get_code', 'no_cache'];
if ($ignore_redirects) {
$presets[] = 'dont_follow_redirects';
}
$code = $http->setUrl($url)
->setPresets($presets)
->request();
if (!empty($code['error'])) {
return false;
}
if (is_array($code)) {
$code = reset($code);
}
if (is_string($code)) {
$code = (int) $code;
}
return Validate::isValidHTTPResponseCode($code) ? $code : false;
}
/**
* Wrapper for http_request
* Requesting data via HTTP request with GET method
*
* @param string $url url of the source
* @param bool $check_response_code flag to check if response code is 200, default is true.
* If code is incorrect return array with error HTTP_RESPONSE_BAD.
*
* @return array|mixed|string
*/
public static function getContentFromURL($url, $check_response_code = true)
{
if ( $check_response_code && static::getResponseCode($url) !== 200 ) {
return array('error' => 'HTTP_RESPONSE_BAD');
}
$http = new Request();
return $http
->setUrl($url)
->setPresets(array('get'))
->request();
}
/**
* Get and unpack data from local or remote GZ archive
*
* @param $url
*
* @return array|mixed|string|string[]
*/
public static function getDataFromGZ($url)
{
// Check if the URL is remote address or not, and use a proper function to extract data
$url_scheme = parse_url($url, PHP_URL_SCHEME);
return $url_scheme !== false && in_array($url_scheme, array('ftp', 'http', 'https',))
? static::getDataFromRemoteGZ($url)
: static::getDataFromLocalGZ($url);
}
/**
* Wrapper for http_request
* Requesting HTTP response code for $url
*
* @param string $url
*
* @return array|mixed|string
*/
public static function getDataFromRemoteGZ($url)
{
// Get data
$data = static::getContentFromURL($url);
if (! empty($data['error'])) {
return array('error' => 'Getting datafile ' . $data['error']);
}
// Check if the 'gzdecode' function exists
if (! function_exists('gzdecode')) {
return array('error' => 'Function gzdecode not exists. Please update your PHP at least to version 5.4');
}
$data = @gzdecode($data);
if ($data !== false) {
return $data;
}
return array('error' => 'Can not unpack datafile');
}
/**
* Wrapper for http_request
* Requesting HTTP response code for $url
*
* @param string $path
*
* @return array|mixed|string
*/
public static function getDataFromLocalGZ($path)
{
if (! file_exists($path)) {
return array('error' => 'File doesn\'t exists: ' . $path);
}
if (! is_readable($path)) {
return array('error' => 'File is not readable: ' . $path);
}
$data = file_get_contents($path);
if ($data === false) {
return array('error' => 'Couldn\'t get data');
}
// Check if the 'gzdecode' function exists
if (! function_exists('gzdecode')) {
return array('error' => 'Function gzdecode not exists. Please update your PHP at least to version 5.4');
}
$data = @gzdecode($data);
if ($data !== false) {
return $data;
}
return array('error' => 'Can not unpack datafile');
}
/**
* Gets every HTTP_ headers from super global variable $_SERVER
*
* If Apache web server is missing then making
* Patch for apache_request_headers()
*
* returns array
*/
public static function getHTTPHeaders()
{
// If headers have already been got, return them
$headers = self::getInstance()->http_headers;
if (! empty($headers)) {
return $headers;
}
foreach ($_SERVER as $key => $val) {
if (0 === stripos($key, 'http_')) {
$server_key = preg_replace('/^http_/i', '', $key);
$key_parts = explode('_', $server_key);
if (strlen($server_key) > 2) {
foreach ($key_parts as $part_index => $part) {
if ($part === '') {
continue;
}
$key_parts[$part_index] = function_exists('mb_strtolower') ? mb_strtolower($part) : strtolower($part);
$key_parts[$part_index][0] = strtoupper($key_parts[$part_index][0]);
}
$server_key = implode('-', $key_parts);
}
$headers[$server_key] = $val;
}
}
// Store headers to skip the work next time
self::getInstance()->http_headers = $headers;
return $headers;
}
/**
* Returns sorted by response time
*
* @param array $hosts Expects the similar arrays in input^
*
* array(
* 'DNS_NAME1' => 'HOST1',
* 'DNS_NAME2' => 'HOST2'
* )
* OR
* array(
* 'HOST1',
* 'HOST2'
* )
*
* DNS_NAME example: 'example.com'
* HOST example: 'example.com'
* HOST example: '1.1.1.1'
*
* @return array formatted in special way:
* array(
* 0 => array(
* 'ping' => 79.3
* 'host' => '1.1.1.1'
* 'dns' => 'dns.name'
* )
* 1 => array(
* 'ping' => 165.6
* 'host' => '2.2.2.2'
* 'dns' => 'dns.name'
* )
* )
*/
public static function sortHostsByResponseTime($hosts)
{
// Get response time for each passed url/host
$output_records = array();
foreach ($hosts as $dns_name => $host) {
$output_records[] = array(
'ping' => self::ping($host),
'host' => $host,
'dns' => is_numeric($dns_name) ? 'unknown' : $dns_name,
);
}
// Sort by ping value
$pings = array_column($output_records, 'ping');
array_multisort(
$pings,
SORT_ASC,
SORT_NUMERIC,
$output_records
);
return $output_records;
}
/**
* Function to check response time for given host or IP
*
* @param string $host Host URL or string representation of IP address
*
* @return double Response time in milliseconds
*/
public static function ping($host)
{
$starttime = microtime(true);
$file = @fsockopen($host, 80, $errno, $errstr, 1500 / 1000);
$stoptime = microtime(true);
if (! $file) {
$ping = 1500 / 1000; // Site is down
} else {
$ping = ($stoptime - $starttime);
$ping = round($ping, 4); // Cut microseconds part
fclose($file);
}
// Convert seconds to milliseconds (0.712 s to 712 ms)
return $ping * 1000;
}
}