-
Notifications
You must be signed in to change notification settings - Fork 12
/
CurlX.php
499 lines (413 loc) · 14.1 KB
/
CurlX.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
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
<?php
class Helper {
public function parseHeaders(string $raw) : array
{
$raw = preg_split('/\r\n/', $raw, -1, PREG_SPLIT_NO_EMPTY);
$http_headers = [];
for ($i = 1; $i < count($raw); $i++) {
if (str_contains($raw[$i], ':')) {
list($key, $value) = explode(':', $raw[$i], 2);
$key = trim($key);
$value = trim($value);
isset($http_headers[$key]) ? $http_headers[$key] .= ',' . $value : $http_headers[$key] = $value;
}
}
return [$raw['0'] ??= $raw['0'], $http_headers];
}
public function parseArray(array $raw) : array
{
if (array_key_exists('request_header', $raw)) {
list($scheme, $headers) = $this->parseHeaders($raw['request_header']);
$nh['scheme'] = $scheme;
$nh += $headers;
$raw['request_header'] = $nh;
}
return $raw;
}
public function parseHeadersHandle($raw) : array
{
if (empty($raw)) {
return [];
}
list($scheme, $headers) = $this->parseHeaders($raw);
$request_headers['scheme'] = $scheme;
unset($headers['request_header']);
foreach ($headers as $key => $value) {
$request_headers[$key] = $value;
}
return $request_headers;
}
}
class CurlException extends Exception {}
class Response {
function __construct(
private readonly bool $success = false,
private readonly int $status_code = 200,
private readonly array $headers = [],
private $body = null,
private $reason = null
) {}
public function isSuccess(): int
{
return $this->success;
}
public function getStatusCode(): int
{
return $this->status_code;
}
public function getHeaders(): array {
return $this->headers;
}
public function getBody(): ?string {
return $this->body;
}
public function getReason(): ?string {
return $this->reason;
}
}
class CurlX extends Helper
{
private array $default = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLINFO_HEADER_OUT => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 60,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
];
private string $cacheDir = '';
private CurlHandle $ch;
private array|false $info;
private stdClass $callback;
private string $cookieFile = '';
private string $userAgent = 'CurlX v2.0.0b (Created by @d3vbl4ck)';
private int $error_code;
private string $error_string;
private bool|string $body;
public function __construct(array $config = []) {
$this->default = array_replace($this->default, $config);
}
public function prepareHandle(string $url): void
{
// start curlHandle
$this->ch = curl_init($url);
$this->setOpt($this->default);
}
public function setOpt(array $option) : void
{
curl_setopt_array($this->ch, $option);
}
private function setHeader(array $header) : void
{
$this->setOpt([CURLOPT_HTTPHEADER => $header]);
}
private function tunnel(array $args) : void
{
$this->setOpt([
CURLOPT_PROXY => $args['server'],
CURLOPT_HTTPPROXYTUNNEL => true
]);
}
private function proxyAuth(array $args) : void
{
$this->setOpt([
CURLOPT_PROXY => $args['server'],
CURLOPT_PROXYUSERPWD => $args['auth']
]);
}
/**
* @param array $args
* @return void
* @throws CurlException
*/
private function autoRouter(array $args): void
{
$args = array_change_key_case($args);
match($args['method']) {
'tunnel' => $this->tunnel($args),
'custom' => $this->proxyAuth($args),
default => throw new CurlException('Invalid proxy router.')
};
}
/**
* @param string $file
* @return void
* @throws CurlException
*/
private function setCookie(CookieJarInterface|string $file_name) : void
{
$this->cacheDir = dirname(__FILE__);
if (!is_dir($this->cacheDir . '/Cache/')) {
mkdir($this->cacheDir . '/Cache/', 0755);
}
if ($file_name instanceof CookieJarInterface) {
$file = $file_name->getFileName();
$file_name
->setFileName($this->cacheDir . '/Cache/curlX_' . $file . '.txt')
->save();
} else {
$file = $file_name;
}
$this->cookieFile = sprintf("%s/Cache/curlX_%s.txt", $this->cacheDir, $file);
if (!is_writable($this->cacheDir)) {
throw new CurlException('The current directory is not writable, please add permissions 0755 to Cache dir and 0644 to CurlX.php');
}
$this->setOpt([
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_COOKIEFILE => $this->cookieFile
]);
}
/**
* @return void
* @throws CurlException
*/
public function deleteCookie(): void
{
if (empty($this->cacheDir)) {
throw new CurlException('Cookie function (setCookie) was not called!');
}
if (!is_file($this->cookieFile)) {
throw new CurlException(sprintf("The filename: %s not exits in %s.", $this->cookieFile, $this->cacheDir));
}
unlink($this->cookieFile);
}
/**
* @param $data
* @return false|string
*/
private function dataType($data): false|string
{
return match(gettype($data)) {
'string' => $data,
'array', 'object' => json_encode($data),
default => false
};
}
/**
* @param array|null $headers
* @param string|null $cookie
* @param array|null $server
* @return void
* @throws CurlException
*/
private function checkParams(?array $headers = null, string|CookieJarInterface $cookie = null, ?array $server = null): void
{
if (is_array($headers)) {
$this->setHeader($headers);
}
if (isset($cookie)) {
$this->setCookie($cookie);
}
if (is_array($server)) {
$this->autoRouter($server);
}
}
/**
* @param string $url
* @param array|null $headers
* @param string|null $cookie
* @param array|null $server
* @return object
* @throws Exception
*/
public function get(string $url, ?array $headers=null, string|CookieJarInterface $cookie=null, ?array $server=null): object
{
$this->prepareHandle($url);
$this->setOpt([CURLOPT_USERAGENT => $this->userAgent]);
$this->checkParams($headers, $cookie, $server);
return $this->run();
}
/**
* @param string $url
* @param string|array|null $data
* @param array|null $headers
* @param string|null $cookie
* @param array|null $server
* @return object
* @throws Exception
*/
public function post(string $url, string|array|null $data=null, ?array $headers=null, string|CookieJarInterface $cookie=null, ?array $server=null) : object
{
$this->prepareHandle($url);
$this->setOpt([
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $this->dataType($data)
]);
$this->checkParams($headers, $cookie, $server);
return $this->run();
}
/**
* @param string $url
* @param string $method
* @param string|array|null $data
* @param array|null $headers
* @param string|null $cookie
* @param array|null $server
* @return void
* @throws CurlException
*/
public function custom(string $url, string $method='GET', string|array|null $data=null, ?array $headers=null, string|CookieJarInterface $cookie=null, ?array $server=null) : void
{
$this->prepareHandle($url);
$this->setOpt([
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $this->dataType($data)
]);
$this->checkParams($headers, $cookie, $server);
}
private function close(): void
{
unset($this->ch);
}
public function run() : object
{
$this->makeStdClass();
$this->setOpt([CURLOPT_HEADERFUNCTION => $this->fnHeader($this->callback)]);
$this->body = curl_exec($this->ch);
$this->info = curl_getinfo($this->ch);
// Request failed
if (!$this->body) {
$this->error_code = curl_errno($this->ch);
$this->error_string = curl_error($this->ch);
$this->close();
return new Response(
success: false,
status_code: $this->info['http_code'],
headers: [
'request' => key_exists('request_header', $this->info) ? $this->parseHeadersHandle($this->info['request_header']) : [],
'response' => $this->callback->rawResponseHeaders
],
body: 'Error code: ' . $this->error_code . ' / Message: '. $this->error_string
);
}
$this->close();
return new Response(
success: true,
status_code: $this->info['http_code'],
headers: [
'request' => $this->parseHeadersHandle($this->info['request_header']),
'response' => $this->callback->rawResponseHeaders
],
body: $this->body
);
}
public function debug(): void
{
# check if is cli client
if (php_sapi_name() === 'cli') {
echo "=============================================\nCURLX DEBUG\n=============================================\n";
echo "Response:\n" . $this->body . "\n\n";
echo "=============================================\n";
echo "Information:\n";
echo print_r([
'request_headers' => $this->parseArray($this->info),
'response_headers' => $this->parseHeadersHandle($this->callback->rawResponseHeaders)
], true) . "\n";
if (isset($this->error_string)) {
echo "=============================================\n";
echo "Errors\n";
echo "Code: " . $this->error_code . "\n";
echo "Message: " . $this->error_string . "\n";
}
} else {
header('Content-Type: application/json');
echo json_encode([
'curlx_debug' => [
'information' => [
'request_headers' => $this->parseArray($this->info),
'response_headers' => $this->parseHeadersHandle($this->callback->rawResponseHeaders)
],
'errors' => [
'errnum' => $this->error_code ?? '',
'errstr' => $this->error_string ?? ''
],
'response' => $this->body
]
]);
}
}
private function makeStdClass(): void
{
$this->callback = (object)['rawResponseHeaders' => ''];
}
private function fnHeader($cb): Closure
{
return function ($_, $header) use ($cb) {
$cb->rawResponseHeaders .= $header;
return strlen($header);
};
}
}
interface CookieJarInterface {
public function setFileName(string $filename);
public function getFileName(): string;
}
class CookieJar implements CookieJarInterface {
protected string $banner = "# Netscape HTTP Cookie File\n# https://curl.se/docs/http-cookies.html\n# This file was generated by libcurl! Edit at your own risk.\n\n";
private $cookies = [];
protected $path;
public function __construct(
public string $filename = '',
) {
$this->filename = uniqid();
}
public function setFileName(string $filename) {
$this->filename = $filename;
return $this;
}
public function getFileName(): string {
return $this->filename;
}
public function add(Cookie $cookie) {
$this->cookies[] = $cookie;
return $this;
}
public function parseCookies() {
$all_cookies = $this->banner;
foreach($this->cookies as $cookie) {
$all_cookies .= $cookie->get() . "\n";
}
return trim($all_cookies);
}
public function save() {
file_put_contents(
$this->filename,
$this->parseCookies()
);
}
public function delete() {
if(file_exists($this->filename)) {
unlink($this->filename);
}
}
}
class Cookie {
public const HTTP_ONLY = '#HttpOnly_.';
public function __construct(
public string $domain = '',
public string $includeSubDomains = 'TRUE',
public string $path = '/',
public string $httpOnly = 'FALSE',
public string $expire = '',
public string $name = '',
public string $value = '',
) {
}
public function get() {
$cookie = [
'domain' => $this->httpOnly == 'TRUE' ? self::HTTP_ONLY . $this->domain : $this->domain,
'includeSubDomains' => $this->includeSubDomains,
'path' => $this->path,
'httpOnly' => $this->httpOnly,
'expire' => $this->expire,
'name' => $this->name,
'value' => $this->value
];
return implode("\t", $cookie);
}
}