-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.php
More file actions
468 lines (418 loc) · 11.3 KB
/
Stream.php
File metadata and controls
468 lines (418 loc) · 11.3 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
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Http Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Classes\Http;
use FloatPHP\Classes\Filesystem\{Stringify, Arrayify, File, Json, Exception};
/**
* Advanced HTTP stream manipulation.
*/
final class Stream
{
/**
* @access public
*/
public const HTTP = 'http';
public const FTP = 'ftp';
public const SSH = 'ssh2';
public const PHAR = 'phar';
public const PHP = 'php';
public const FILE = 'file';
public const GLOB = 'glob';
public const DATA = 'data';
public const TCP = 'tcp';
public const UDP = 'udp';
public const SSL = 'ssl';
public const TLS = 'tls';
/**
* @access private
* @var string $transport
* @var array $responseStatus
* @var array $responseHeader
* @var string $responseBody
*/
private static $transport = self::HTTP;
private static $responseStatus = [];
private static $responseHeader = [];
private static $responseBody = '';
/**
* Set stream transport.
*
* @access public
* @param string $transport
* @return void
*/
public static function setTransport(string $transport) : void
{
self::$transport = $transport;
}
/**
* Create stream context.
*
* @access public
* @param ?array $options
* @param ?array $params
* @return mixed
*/
public static function create(?array $options = null, ?array $params = null) : mixed
{
return stream_context_create($options, $params);
}
/**
* Get stream wrappers.
*
* @access public
* @return array
*/
public static function getWrappers() : array
{
return stream_get_wrappers();
}
/**
* Get stream socket transports.
*
* @access public
* @return array
*/
public static function getTransports() : array
{
return stream_get_transports();
}
/**
* Set stream context parameters.
*
* @access public
* @param resource $context
* @param array $params
* @return bool
*/
public static function setParams($context, array $params) : bool
{
return stream_context_set_params($context, $params);
}
/**
* Execute stream context using file.
*
* @access public
* @param string $url
* @param array $options
* @param mixed $context
* @return mixed
*/
public static function exec(string $url, array $options, &$context = null) : mixed
{
$options = Stringify::undash($options);
$context = self::create($options);
return File::r($url, false, $context);
}
/**
* Execute stream context (Alias with status).
*
* @access public
* @param string $url
* @param array $options
* @return bool
*/
public static function execute(string $url, array $options) : bool
{
self::reset();
$response = self::exec($url, $options, $context);
// Set header
self::parseHeader($url, $context);
// Set body
self::$responseBody = (string)$response;
return $response !== false;
}
/**
* Advanced stream HTTP request.
*
* @access public
* @param string $url
* @param array $params
* @return array
*/
public static function request(string $url, array $params = []) : array
{
// Extract params
$params = Client::getParams($params);
extract($params);
// Set body
$body = Client::getQuery($body);
// Set User-Agent
if ( $ua ) {
$header['User-Agent'] = $ua;
}
// Set header
$header = Client::formatHeader($header);
// Set options
$options = [
self::$transport => [
'method' => $method,
'header' => $header,
'content' => $body,
'timeout' => $timeout,
'max-redirects' => $redirect,
'follow-location' => $follow == true ? 1 : 0
],
self::SSL => [
'verify-peer' => $ssl,
'verify-peer-name' => $ssl
]
];
// Get response
$error = false;
if ( !self::execute($url, $options) ) {
$header = [];
$status = self::getError();
$body = Status::getMessage(500);
$error = true;
} else {
$header = self::getResponseHeader();
$status = self::getResponseStatus();
$body = self::getResponseBody();
}
// Return reponse data
return [
'error' => $error,
'status' => $status,
'header' => $header,
'body' => $body
];
}
/**
* Get cURL effective URL (Location).
*
* @access public
* @param string $url
* @param array $params
* @return string
*/
public static function getLocation(string $url, array $params = []) : string
{
// Extract params
$params = Client::getParams($params);
extract($params);
// Set method
if ( !$method || $method == Client::POST ) {
$method = Client::HEAD;
}
// Set body
$url = $body ? Client::getQuery($body, $url) : $url;
// Set User-Agent
if ( $ua ) {
$header['User-Agent'] = $ua;
}
// Set options
$options = [
self::$transport => [
'method' => $method,
'header' => $header,
'content' => $body,
'timeout' => $timeout,
'max-redirects' => $redirect,
'follow-location' => $follow == true ? 1 : 0
],
self::SSL => [
'verify-peer' => $ssl,
'verify-peer-name' => $ssl
]
];
// Parse HTTP location
if ( self::execute($url, $options) ) {
$header = self::getResponseHeader();
$url = $header['Location'] ?? $url;
$url = trim($url);
}
return $url;
}
/**
* Get response header.
*
* @access public
* @return array
*/
public static function getResponseHeader() : array
{
return self::$responseHeader;
}
/**
* Get response status.
*
* @access public
* @return array
*/
public static function getResponseStatus() : array
{
return self::$responseStatus;
}
/**
* Get response status code.
*
* @access public
* @return int
*/
public static function getStatusCode() : int
{
$code = self::$responseStatus['code'] ?? -1;
return (int)$code;
}
/**
* Get response status message.
*
* @access public
* @return string
*/
public static function getStatusMessage() : string
{
$code = self::$responseStatus['message'] ?? '';
return (string)$code;
}
/**
* Get response body.
*
* @access public
* @return string
*/
public static function getResponseBody() : string
{
return self::$responseBody;
}
/**
* Get stream error code.
*
* @access public
* @return int
*/
public static function getErrorCode() : int
{
$error = Exception::getLastError();
return $error['type'] ?? -1;
}
/**
* Get stream error message.
*
* @access public
* @return string
*/
public static function getErrorMessage() : string
{
$error = Exception::getLastError();
$error = $error['message'] ?? '';
if ( Stringify::contains($error, 'operation failed') ) {
$pattern = '/file_get_contents\((\w+:\/\/\/|\/)\)/i';
if ( Stringify::match($pattern, $error, $matches) ) {
$host = $matches[0] ?: 'Empty';
return 'URL rejected: No host part in the URL';
}
}
if ( Stringify::contains($error, 'getaddrinfo') ) {
$pattern = '/\b[a-z0-9.-]+\.[a-z]{2,}\b/i';
if ( Stringify::match($pattern, $error, $matches) ) {
$host = $matches[0] ?: 'Empty';
return "Could not resolve host: {$host}";
}
}
if ( Stringify::contains($error, 'No such file or directory') ) {
$pattern = '/\b[a-z]+(?=:\/\/?)/i';
if ( Stringify::match($pattern, $error, $matches) ) {
$protocol = $matches[0] ?: 'Empty';
return "Protocol not supported: {$protocol}";
}
return 'Invalid protocol';
}
return 'Failed to open stream';
}
/**
* Get stream error (Normalized).
*
* @access public
* @return array
*/
public static function getError() : array
{
return [
'code' => self::getErrorCode(),
'message' => self::getErrorMessage()
];
}
/**
* Get Http response header from context.
*
* @access public
* @param string $url
* @param bool $as Associative
* @param resource $context
* @return mixed
*/
public static function getHeader(string $url, bool $as = false, $context = null) : mixed
{
return @get_headers($url, $as, $context);
}
/**
* Parse Http response header from context.
*
* @access public
* @param string $url
* @param resource $context
* @return int
*/
public static function parseHeader(string $url, $context = null) : int
{
// Get header
$header = self::getHeader($url, true, $context) ?: [];
// Parse status
$status = $header[0] ?? '';
$regex = Client::getPattern('status');
if ( Stringify::match($regex, $status, $matches) ) {
$code = $matches['code'] ?? -1;
$code = (int)$code;
$message = $matches['message'] ?? '';
if ( empty($message) ) {
$message = Status::getMessage($code);
}
self::$responseStatus = ['code' => $code, 'message' => $message];
unset($header[0]);
}
// Set formated header
self::$responseHeader = $header;
$header = Json::encode($header);
return strlen($header);
}
/**
* Check valid stream (path).
*
* @access public
* @param string $path
* @return bool
*/
public static function isValid(string $path) : bool
{
$scheme = strpos($path, '://');
if ( $scheme === false ) {
return false;
}
$stream = substr($path, 0, length: $scheme);
return Arrayify::inArray($stream, self::getWrappers());
}
/**
* Reset stream.
*
* @access private
* @return void
*/
private static function reset() : void
{
self::$responseBody = '';
self::$responseHeader = [];
self::$responseStatus = [];
}
}