-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
570 lines (498 loc) · 18.9 KB
/
Copy pathRequest.php
File metadata and controls
570 lines (498 loc) · 18.9 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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<?php
namespace Cleantalk\Common\Http;
/**
* Class Request
*
* @version 1.0.0
* @package Cleantalk\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 Request
{
/**
* Default user agent for HTTP requests
*/
const AGENT = 'Cleantalk-Helper/1.0.0';
/**
* @var string|string[] Single URL string or array of URLs for multi request
*/
protected $url;
/**
* @var array POST|GET indexed array with data to send
*/
protected $data = [];
/**
* @var string|string[] Array with presets
* Example: array('get_code', 'async')
* Or space separated string with presets
* Example: 'get_code async get'
*
* May use the following presets(combining is possible):
* dont_follow_redirects - ignore 300-family response code and don't follow redirects
* get_code - getting only HTTP response code
* async - async requests. Sends request and return 'true' value. Doesn't wait for response.
* get - makes GET-type request instead of default POST-type
* ssl - uses SSL
* cache - allow caching for this request
* retry_with_socket - make another request with socket if cURL failed to retrieve data
*/
protected $presets = [];
/**
* @var array Optional options for CURL connection
* Example: array(
* CURLOPT_URL => $url,
* CURLOPT_TIMEOUT => 15,
* CURLOPT_LOW_SPEED_TIME => 10,
* CURLOPT_RETURNTRANSFER => true,
* )
*/
protected $options = [];
/**
* @var array [callable] Callback function to process after the request is performed without error to process received data
* If passed will be fired for both single and multi requests
*/
protected $callbacks = [];
/**
* @var Response|array<Response>
*/
public $response;
/**
* @param mixed $url
*
* @return Request
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* @param mixed $data
*
* @return Request
*/
public function setData($data)
{
// If $data scalar converting it to array
$this->data = ! empty($data) && ! self::isJson($data) && is_scalar($data)
? array((string)$data => 1)
: $data;
return $this;
}
/**
* Set one or more presets which change the way of the processing Request::request
*
* @param mixed $presets Array with presets
* Example: array('get_code', 'async')
* Or space separated string with presets
* Example: 'get_code async get'
*
* May use the following presets(combining is possible):
* dont_follow_redirects - ignore 300-family response code and don't follow redirects
* get_code - getting only HTTP response code
* async - async requests. Sends request and return 'true' value. Doesn't wait for response.
* get - makes GET-type request instead of default POST-type
* ssl - uses SSL
* cache - allow caching for this request
* retry_with_socket - make another request with socket if cURL failed to retrieve data
*
* @return Request
*/
public function setPresets($presets)
{
// Prepare $presets to process
$this->presets = ! is_array($presets)
? explode(' ', $presets)
: $presets;
return $this;
}
/**
* @param mixed $options
*
* @return Request
*/
public function setOptions($options)
{
$this->options = $options;
return $this;
}
/**
* Set callback and additional arguments which will be passed to callback function
*
* @param callable $callback
* @param array $arguments
* @param int $priority
* @param bool $pass_response
*
* @return Request
* @psalm-suppress UnusedVariable
*/
public function addCallback($callback, $arguments = array(), $priority = null, $pass_response = false)
{
$priority = $priority ?: 100;
if ( isset($this->callbacks[$priority]) ) {
return $this->addCallback($callback, $arguments, ++$priority);
}
$this->callbacks[$priority] = [
'function' => $callback,
'arguments' => $arguments,
'pass_response' => $pass_response,
];
return $this;
}
/**
* Function sends raw http request
*
* @return array|bool (array || array('error' => true))
*/
public function request()
{
// Return the error if cURL is not installed
if ( ! function_exists('curl_init') ) {
return array('error' => 'CURL_NOT_INSTALLED');
}
if ( empty($this->url) ) {
return array('error' => 'URL_IS_NOT_SET');
}
$this->convertOptionsTocURLFormat();
$this->appendOptionsObligatory();
$this->processPresets();
// Call cURL multi request if many URLs passed
$this->response = is_array($this->url)
? $this->requestMulti()
: $this->requestSingle();
// Process the error. Unavailable for multiple URLs.
if (
! is_array($this->url) &&
$this->response->getError() &&
in_array('retry_with_socket', $this->presets, true)
) {
$this->response = $this->requestWithSocket();
if ( $this->response->getError() ) {
return $this->response->getError();
}
}
return $this->runCallbacks();
}
/**
* @return Response
*/
protected function requestSingle()
{
// Make a request
$ch = curl_init();
curl_setopt_array($ch, $this->options);
$request_result = curl_exec($ch); // Gather request result
$curl_info = curl_getinfo($ch); // Gather HTTP response information
// Do not catch timeout error for async requests.
if ( in_array('async', $this->presets, true) ) {
$request_result = true;
}
if ( $request_result === false ) {
$request_result = array('error' => curl_error($ch));
}
if ( version_compare(PHP_VERSION, '8.0.0', '<') ) {
curl_close($ch);
}
return new Response($request_result, $curl_info);
}
/**
* Do multi curl requests without processing it.
*
* @return array<Response>
*/
protected function requestMulti()
{
$urls_count = count($this->url);
$curl_arr = array();
$mh = curl_multi_init();
$this->response = [];
for ( $i = 0; $i < $urls_count; $i++ ) {
$this->options[CURLOPT_URL] = $this->url[$i];
$curl_arr[$i] = curl_init($this->url[$i]);
curl_setopt_array($curl_arr[$i], $this->options);
curl_multi_add_handle($mh, $curl_arr[$i]);
}
do {
curl_multi_exec($mh, $running);
usleep(1000);
} while ( $running > 0 );
for ( $i = 0; $i < $urls_count; $i++ ) {
$curl_info = curl_getinfo($curl_arr[$i]); // Gather HTTP response information
$received_data = curl_multi_getcontent($curl_arr[$i]);
// Do not catch timeout error for async requests.
if ( in_array('async', $this->presets, true) ) {
$received_data = true;
}
if ( $received_data === '' ) {
$received_data = array('error' => curl_error($curl_arr[$i]));
}
$this->response[$this->url[$i]] = new Response($received_data, $curl_info);
}
return $this->response;
}
/**
* Make a request with socket, exactly with file_get_contents()
*
* @return Response
*/
private function requestWithSocket()
{
if ( ! ini_get('allow_url_fopen') ) {
return new Response(['error' => 'ALLOW_URL_FOPEN_IS_DISABLED'], []);
}
$context = stream_context_create(
[
'http' => [
'method' => 'GET', //in_array('get', $this->presets, true) ? 'GET' : 'POST',
'timeout' => $this->options[CURLOPT_TIMEOUT],
'content' => $this->data,
],
]
);
$response_content = @file_get_contents($this->url, false, $context)
?: ['error' => 'FAILED_TO_USE_FILE_GET_CONTENTS'];
return new Response($response_content, []);
}
// Process with callback if passed. Save the processed result.
protected function runCallbacks()
{
$return_value = [];
// Cast to array to precess result from $this->requestSingle as $this->requestMulti results
$responses = is_object($this->response)
? [$this->response]
: $this->response;
// Sort callback to keep the priority order
ksort($this->callbacks);
foreach ( $responses as $url => &$response ) {
// Skip the processing if the error occurred in this specific result
if ( $response->getError() ) {
$return_value[] = $response->getError();
continue;
}
// Get content to process
$content = $response->getContentProcessed();
// Perform all provided callback functions to each request result
if ( ! empty($this->callbacks) ) {
foreach ( $this->callbacks as $callback ) {
if ( is_callable($callback['function']) ) {
// Run callback
$content = call_user_func_array(
$callback['function'],
array_merge(
array(
$callback['pass_response'] ? $response : $content, // Pass Response or content
$url
),
$callback['arguments']
)
);
// Foolproof
if ( ! $content instanceof Response ) {
$response->setProcessed($content);
}
}
}
}
$return_value[$url] = $content instanceof Response ? $content->getContentProcessed() : $content;
}
unset($response);
// Return a single content if it was a single request
return is_array($this->response) && count($this->response) > 1
? $return_value
: reset($return_value);
}
/**
* Convert given options from simple naming like 'timeout' or 'ssl'
* to sophisticated and standardized cURL defined constants
*
* !! Called only after we make sure that cURL is exists !!
*/
private function convertOptionsTocURLFormat()
{
$temp_options = [];
foreach ( $this->options as $option_name => &$option_value ) {
switch ( $option_name ) {
case 'timeout':
$temp_options[CURLOPT_TIMEOUT] = $option_value; // String
unset($this->options[$option_name]);
break;
case 'sslverify':
if ( $option_value ) {
$temp_options[CURLOPT_SSL_VERIFYPEER] = (bool)$option_value; // Boolean
$temp_options[CURLOPT_SSL_VERIFYHOST] = (int)(bool)$option_value; // Int 0|1
unset($this->options[$option_name]);
}
break;
case 'sslcertificates':
$temp_options[CURLOPT_CAINFO] = $option_name; // String
unset($this->options[$option_name]);
break;
case 'headers':
$temp_options[CURLOPT_HTTPHEADER] = $option_name; // String[]
unset($this->options[$option_name]);
break;
case 'user-agent':
$temp_options[CURLOPT_USERAGENT] = $option_name; // String
unset($this->options[$option_name]);
break;
// Unset unsupported string names in options
default:
if ( ! is_int($option_name) ) {
unset($this->options[$option_name]);
}
break;
}
}
unset($option_value);
$this->options = array_replace($this->options, $temp_options);
}
/**
* Set default options to make a request
*/
protected function appendOptionsObligatory()
{
// Merging OBLIGATORY options with GIVEN options
$this->options = array_replace(
array(
CURLOPT_URL => ! is_array($this->url) ? $this->url : null,
CURLOPT_TIMEOUT => 10,
CURLOPT_LOW_SPEED_TIME => 7,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5000,
CURLOPT_FORBID_REUSE => true,
CURLOPT_USERAGENT => self::AGENT,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $this->data,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_HTTPHEADER => array(
'Expect:',
// Fix for large data and old servers http://php.net/manual/ru/function.curl-setopt.php#82418
'Expires: ' . date(DATE_RFC822, mktime(0, 0, 0, 1, 1, 1971)),
'Cache-Control: no-store, no-cache, must-revalidate',
'Cache-Control: post-check=0, pre-check=0',
'Pragma: no-cache',
),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
),
$this->options
);
}
/**
* Append options considering passed presets
*/
protected function processPresets()
{
foreach ( $this->presets as $preset ) {
switch ( $preset ) {
// Do not follow redirects
case 'dont_follow_redirects':
$this->options[CURLOPT_FOLLOWLOCATION] = false;
$this->options[CURLOPT_MAXREDIRS] = 0;
break;
// Get headers only
case 'get_code':
$this->options[CURLOPT_HEADER] = true;
$this->options[CURLOPT_NOBODY] = true;
$this->addCallback(
static function (Response $response, $_url) {
return $response->getResponseCode();
},
array(),
60,
true
);
break;
// Get headers only
case 'split_to_array':
$this->addCallback(
static function ($response_content, $_url) {
return explode(PHP_EOL, $response_content);
},
array(),
50
);
break;
// Make a request, don't wait for an answer
case 'async':
$this->options[CURLOPT_CONNECTTIMEOUT] = 3;
$this->options[CURLOPT_TIMEOUT] = 3;
break;
case 'get':
$this->options[CURLOPT_CUSTOMREQUEST] = 'GET';
$this->options[CURLOPT_POST] = false;
$this->options[CURLOPT_POSTFIELDS] = null;
// Append parameter in a different way for single and multiple requests
if ( is_array($this->url) ) {
$this->url = array_map(function ($elem) {
return self::appendParametersToURL($elem, $this->data);
}, $this->url);
} else {
$this->options[CURLOPT_URL] = self::appendParametersToURL(
$this->options[CURLOPT_URL],
$this->data
);
}
break;
case 'ssl':
$this->options[CURLOPT_SSL_VERIFYPEER] = true;
$this->options[CURLOPT_SSL_VERIFYHOST] = 2;
if ( defined('CLEANTALK_CASERT_PATH') && CLEANTALK_CASERT_PATH ) {
$this->options[CURLOPT_CAINFO] = CLEANTALK_CASERT_PATH;
}
break;
case 'no_cache':
// Append parameter in a different way for single and multiple requests
if ( is_array($this->url) ) {
$this->url = array_map(static function ($elem) {
return self::appendParametersToURL($elem, ['no_cache' => mt_rand()]);
}, $this->url);
} else {
$this->options[CURLOPT_URL] = self::appendParametersToURL(
$this->options[CURLOPT_URL],
['no_cache' => mt_rand()]
);
}
break;
}
}
}
/**
* 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;
}
/**
* Checks if the string is JSON type
*
* @param string $string
*
* @return bool
*/
public static function isJson($string)
{
return is_string($string) && is_array(json_decode($string, true));
}
}