1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\request; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\domain\services\contexts\RequestTypeContextCheckerInterface; |
6
|
|
|
use EventEspresso\core\interfaces\InterminableInterface; |
7
|
|
|
use EventEspresso\core\interfaces\ReservedInstanceInterface; |
8
|
|
|
|
9
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Request |
15
|
|
|
* Representation of an incoming, server-side HTTP request |
16
|
|
|
* |
17
|
|
|
* @package EventEspresso\core\services\request |
18
|
|
|
* @author Brent Christensen |
19
|
|
|
* @since 4.9.53 |
20
|
|
|
*/ |
21
|
|
|
class Request implements InterminableInterface, RequestInterface, ReservedInstanceInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* $_GET parameters |
26
|
|
|
* |
27
|
|
|
* @var array $get |
28
|
|
|
*/ |
29
|
|
|
private $get; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* $_POST parameters |
33
|
|
|
* |
34
|
|
|
* @var array $post |
35
|
|
|
*/ |
36
|
|
|
private $post; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* $_COOKIE parameters |
40
|
|
|
* |
41
|
|
|
* @var array $cookie |
42
|
|
|
*/ |
43
|
|
|
private $cookie; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* $_SERVER parameters |
47
|
|
|
* |
48
|
|
|
* @var array $server |
49
|
|
|
*/ |
50
|
|
|
private $server; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* $_REQUEST parameters |
54
|
|
|
* |
55
|
|
|
* @var array $request |
56
|
|
|
*/ |
57
|
|
|
private $request; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var RequestTypeContextCheckerInterface |
61
|
|
|
*/ |
62
|
|
|
private $request_type; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* IP address for request |
66
|
|
|
* |
67
|
|
|
* @var string $ip_address |
68
|
|
|
*/ |
69
|
|
|
private $ip_address; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string $user_agent |
73
|
|
|
*/ |
74
|
|
|
private $user_agent; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* true if current user appears to be some kind of bot |
78
|
|
|
* |
79
|
|
|
* @var bool $is_bot |
80
|
|
|
*/ |
81
|
|
|
private $is_bot; |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array $get |
87
|
|
|
* @param array $post |
88
|
|
|
* @param array $cookie |
89
|
|
|
* @param array $server |
90
|
|
|
*/ |
91
|
|
|
public function __construct(array $get, array $post, array $cookie, array $server) |
92
|
|
|
{ |
93
|
|
|
// grab request vars |
94
|
|
|
$this->get = $get; |
95
|
|
|
$this->post = $post; |
96
|
|
|
$this->cookie = $cookie; |
97
|
|
|
$this->server = $server; |
98
|
|
|
$this->request = array_merge($this->get, $this->post); |
99
|
|
|
$this->ip_address = $this->visitorIp(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param RequestTypeContextCheckerInterface $type |
105
|
|
|
*/ |
106
|
|
|
public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type) |
107
|
|
|
{ |
108
|
|
|
$this->request_type = $type; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getParams() |
117
|
|
|
{ |
118
|
|
|
return $this->get; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function postParams() |
127
|
|
|
{ |
128
|
|
|
return $this->post; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function cookieParams() |
137
|
|
|
{ |
138
|
|
|
return $this->cookie; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function serverParams() |
146
|
|
|
{ |
147
|
|
|
return $this->server; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* returns contents of $_REQUEST |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function requestParams() |
158
|
|
|
{ |
159
|
|
|
return $this->request; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param $key |
166
|
|
|
* @param $value |
167
|
|
|
* @param bool $override_ee |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function setRequestParam($key, $value, $override_ee = false) |
171
|
|
|
{ |
172
|
|
|
// don't allow "ee" to be overwritten unless explicitly instructed to do so |
173
|
|
|
if ( |
174
|
|
|
$key !== 'ee' |
175
|
|
|
|| ($key === 'ee' && empty($this->request['ee'])) |
176
|
|
|
|| ($key === 'ee' && ! empty($this->request['ee']) && $override_ee) |
177
|
|
|
) { |
178
|
|
|
$this->request[ $key ] = $value; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* returns the value for a request param if the given key exists |
186
|
|
|
* |
187
|
|
|
* @param $key |
188
|
|
|
* @param null $default |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public function getRequestParam($key, $default = null) |
192
|
|
|
{ |
193
|
|
|
return $this->requestParameterDrillDown($key, $default, 'get'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* check if param exists |
200
|
|
|
* |
201
|
|
|
* @param $key |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function requestParamIsSet($key) |
205
|
|
|
{ |
206
|
|
|
return $this->requestParameterDrillDown($key); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* check if a request parameter exists whose key that matches the supplied wildcard pattern |
212
|
|
|
* and return the value for the first match found |
213
|
|
|
* wildcards can be either of the following: |
214
|
|
|
* ? to represent a single character of any type |
215
|
|
|
* * to represent one or more characters of any type |
216
|
|
|
* |
217
|
|
|
* @param string $pattern |
218
|
|
|
* @param null|mixed $default |
219
|
|
|
* @return false|int |
220
|
|
|
*/ |
221
|
|
|
public function getMatch($pattern, $default = null) |
222
|
|
|
{ |
223
|
|
|
return $this->requestParameterDrillDown($pattern, $default, 'match'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* check if a request parameter exists whose key matches the supplied wildcard pattern |
229
|
|
|
* wildcards can be either of the following: |
230
|
|
|
* ? to represent a single character of any type |
231
|
|
|
* * to represent one or more characters of any type |
232
|
|
|
* returns true if a match is found or false if not |
233
|
|
|
* |
234
|
|
|
* @param string $pattern |
235
|
|
|
* @return false|int |
236
|
|
|
*/ |
237
|
|
|
public function matches($pattern) |
238
|
|
|
{ |
239
|
|
|
return $this->requestParameterDrillDown($pattern, null, 'match') !== null; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
245
|
|
|
* @param string $pattern A string including wildcards to be converted to a regex pattern |
246
|
|
|
* and used to search through the current request's parameter keys |
247
|
|
|
* @param array $request_params The array of request parameters to search through |
248
|
|
|
* @param mixed $default [optional] The value to be returned if no match is found. |
249
|
|
|
* Default is null |
250
|
|
|
* @param string $return [optional] Controls what kind of value is returned. |
251
|
|
|
* Options are: |
252
|
|
|
* 'bool' will return true or false if match is found or not |
253
|
|
|
* 'key' will return the first key found that matches the supplied pattern |
254
|
|
|
* 'value' will return the value for the first request parameter |
255
|
|
|
* whose key matches the supplied pattern |
256
|
|
|
* Default is 'value' |
257
|
|
|
* @return boolean|string |
258
|
|
|
*/ |
259
|
|
|
private function match($pattern, array $request_params, $default = null, $return = 'value') |
260
|
|
|
{ |
261
|
|
|
$return = in_array($return, array('bool', 'key', 'value'), true) |
262
|
|
|
? $return |
263
|
|
|
: 'is_set'; |
264
|
|
|
// replace wildcard chars with regex chars |
265
|
|
|
$pattern = str_replace( |
266
|
|
|
array("\*", "\?"), |
267
|
|
|
array('.*', '.'), |
268
|
|
|
preg_quote($pattern, '/') |
269
|
|
|
); |
270
|
|
|
foreach ($request_params as $key => $request_param) { |
271
|
|
|
if (preg_match('/^' . $pattern . '$/is', $key)) { |
272
|
|
|
// return value for request param |
273
|
|
|
if ($return === 'value') { |
274
|
|
|
return $request_params[ $key ]; |
275
|
|
|
} |
276
|
|
|
// or actual key or true just to indicate it was found |
277
|
|
|
return $return === 'key' ? $key : true; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
// match not found so return default value or false |
281
|
|
|
return $return === 'value' ? $default : false; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* the supplied key can be a simple string to represent a "top-level" request parameter |
287
|
|
|
* or represent a key for a request parameter that is nested deeper within the request parameter array, |
288
|
|
|
* by using square brackets to surround keys for deeper array elements. |
289
|
|
|
* For example : |
290
|
|
|
* if the supplied $key was: "first[second][third]" |
291
|
|
|
* then this will attempt to drill down into the request parameter array to find a value. |
292
|
|
|
* Given the following request parameters: |
293
|
|
|
* array( |
294
|
|
|
* 'first' => array( |
295
|
|
|
* 'second' => array( |
296
|
|
|
* 'third' => 'has a value' |
297
|
|
|
* ) |
298
|
|
|
* ) |
299
|
|
|
* ) |
300
|
|
|
* would return true if default parameters were set |
301
|
|
|
* |
302
|
|
|
* @param string $callback |
303
|
|
|
* @param $key |
304
|
|
|
* @param null $default |
305
|
|
|
* @param array $request_params |
306
|
|
|
* @return bool|mixed|null |
307
|
|
|
*/ |
308
|
|
|
private function requestParameterDrillDown( |
309
|
|
|
$key, |
310
|
|
|
$default = null, |
311
|
|
|
$callback = 'is_set', |
312
|
|
|
array $request_params = array() |
313
|
|
|
) { |
314
|
|
|
$callback = in_array($callback, array('is_set', 'get', 'match'), true) |
315
|
|
|
? $callback |
316
|
|
|
: 'is_set'; |
317
|
|
|
$request_params = ! empty($request_params) |
318
|
|
|
? $request_params |
319
|
|
|
: $this->request; |
320
|
|
|
// does incoming key represent an array like 'first[second][third]' ? |
321
|
|
|
if (strpos($key, '[') !== false) { |
322
|
|
|
// turn it into an actual array |
323
|
|
|
$key = str_replace(']', '', $key); |
324
|
|
|
$keys = explode('[', $key); |
325
|
|
|
$key = array_shift($keys); |
326
|
|
|
if ($callback === 'match') { |
327
|
|
|
$real_key = $this->match($key, $request_params, $default, 'key'); |
328
|
|
|
$key = $real_key ? $real_key : $key; |
329
|
|
|
} |
330
|
|
|
// check if top level key exists |
331
|
|
|
if (isset($request_params[ $key ])) { |
332
|
|
|
// build a new key to pass along like: 'second[third]' |
333
|
|
|
// or just 'second' depending on depth of keys |
334
|
|
|
$key_string = array_shift($keys); |
335
|
|
|
if (! empty($keys)) { |
336
|
|
|
$key_string .= '[' . implode('][', $keys) . ']'; |
337
|
|
|
} |
338
|
|
|
return $this->requestParameterDrillDown( |
339
|
|
|
$key_string, |
340
|
|
|
$default, |
341
|
|
|
$callback, |
342
|
|
|
$request_params[ $key ] |
343
|
|
|
); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
if ($callback === 'is_set') { |
347
|
|
|
return isset($request_params[ $key ]); |
348
|
|
|
} |
349
|
|
|
if ($callback === 'match') { |
350
|
|
|
return $this->match($key, $request_params, $default); |
351
|
|
|
} |
352
|
|
|
return isset($request_params[ $key ]) |
353
|
|
|
? $request_params[ $key ] |
354
|
|
|
: $default; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* remove param |
360
|
|
|
* |
361
|
|
|
* @param $key |
362
|
|
|
* @param bool $unset_from_global_too |
363
|
|
|
*/ |
364
|
|
|
public function unSetRequestParam($key, $unset_from_global_too = false) |
365
|
|
|
{ |
366
|
|
|
unset($this->request[ $key ]); |
367
|
|
|
if ($unset_from_global_too) { |
368
|
|
|
unset($_REQUEST[ $key ]); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
|
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return string |
376
|
|
|
*/ |
377
|
|
|
public function ipAddress() |
378
|
|
|
{ |
379
|
|
|
return $this->ip_address; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* attempt to get IP address of current visitor from server |
385
|
|
|
* plz see: http://stackoverflow.com/a/2031935/1475279 |
386
|
|
|
* |
387
|
|
|
* @access public |
388
|
|
|
* @return string |
389
|
|
|
*/ |
390
|
|
|
private function visitorIp() |
391
|
|
|
{ |
392
|
|
|
$visitor_ip = '0.0.0.0'; |
393
|
|
|
$server_keys = array( |
394
|
|
|
'HTTP_CLIENT_IP', |
395
|
|
|
'HTTP_X_FORWARDED_FOR', |
396
|
|
|
'HTTP_X_FORWARDED', |
397
|
|
|
'HTTP_X_CLUSTER_CLIENT_IP', |
398
|
|
|
'HTTP_FORWARDED_FOR', |
399
|
|
|
'HTTP_FORWARDED', |
400
|
|
|
'REMOTE_ADDR', |
401
|
|
|
); |
402
|
|
|
foreach ($server_keys as $key) { |
403
|
|
|
if (isset($this->server[ $key ])) { |
404
|
|
|
foreach (array_map('trim', explode(',', $this->server[ $key ])) as $ip) { |
405
|
|
|
if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
406
|
|
|
$visitor_ip = $ip; |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
return $visitor_ip; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @return string |
417
|
|
|
*/ |
418
|
|
|
public function requestUri() |
419
|
|
|
{ |
420
|
|
|
$request_uri = filter_input( |
421
|
|
|
INPUT_SERVER, |
422
|
|
|
'REQUEST_URI', |
423
|
|
|
FILTER_SANITIZE_URL, |
424
|
|
|
FILTER_NULL_ON_FAILURE |
425
|
|
|
); |
426
|
|
|
if (empty($request_uri)) { |
427
|
|
|
// fallback sanitization if the above fails |
428
|
|
|
$request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']); |
429
|
|
|
} |
430
|
|
|
return $request_uri; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* @return string |
436
|
|
|
*/ |
437
|
|
|
public function userAgent() |
438
|
|
|
{ |
439
|
|
|
return $this->user_agent; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @param string $user_agent |
445
|
|
|
*/ |
446
|
|
|
public function setUserAgent($user_agent = '') |
447
|
|
|
{ |
448
|
|
|
if ($user_agent === '' || ! is_string($user_agent)) { |
449
|
|
|
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : ''; |
450
|
|
|
} |
451
|
|
|
$this->user_agent = $user_agent; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @return bool |
457
|
|
|
*/ |
458
|
|
|
public function isBot() |
459
|
|
|
{ |
460
|
|
|
return $this->is_bot; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @param bool $is_bot |
466
|
|
|
*/ |
467
|
|
|
public function setIsBot($is_bot) |
468
|
|
|
{ |
469
|
|
|
$this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* @return bool |
475
|
|
|
*/ |
476
|
|
|
public function isActivation() |
477
|
|
|
{ |
478
|
|
|
return $this->request_type->isActivation(); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @param $is_activation |
484
|
|
|
* @return bool |
485
|
|
|
*/ |
486
|
|
|
public function setIsActivation($is_activation) |
487
|
|
|
{ |
488
|
|
|
return $this->request_type->setIsActivation($is_activation); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @return bool |
494
|
|
|
*/ |
495
|
|
|
public function isAdmin() |
496
|
|
|
{ |
497
|
|
|
return $this->request_type->isAdmin(); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @return bool |
503
|
|
|
*/ |
504
|
|
|
public function isAdminAjax() |
505
|
|
|
{ |
506
|
|
|
return $this->request_type->isAdminAjax(); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* @return bool |
512
|
|
|
*/ |
513
|
|
|
public function isAjax() |
514
|
|
|
{ |
515
|
|
|
return $this->request_type->isAjax(); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* @return bool |
521
|
|
|
*/ |
522
|
|
|
public function isEeAjax() |
523
|
|
|
{ |
524
|
|
|
return $this->request_type->isEeAjax(); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* @return bool |
530
|
|
|
*/ |
531
|
|
|
public function isOtherAjax() |
532
|
|
|
{ |
533
|
|
|
return $this->request_type->isOtherAjax(); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* @return bool |
539
|
|
|
*/ |
540
|
|
|
public function isApi() |
541
|
|
|
{ |
542
|
|
|
return $this->request_type->isApi(); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* @return bool |
548
|
|
|
*/ |
549
|
|
|
public function isCli() |
550
|
|
|
{ |
551
|
|
|
return $this->request_type->isCli(); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* @return bool |
557
|
|
|
*/ |
558
|
|
|
public function isCron() |
559
|
|
|
{ |
560
|
|
|
return $this->request_type->isCron(); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* @return bool |
566
|
|
|
*/ |
567
|
|
|
public function isFeed() |
568
|
|
|
{ |
569
|
|
|
return $this->request_type->isFeed(); |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* @return bool |
575
|
|
|
*/ |
576
|
|
|
public function isFrontend() |
577
|
|
|
{ |
578
|
|
|
return $this->request_type->isFrontend(); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* @return bool |
584
|
|
|
*/ |
585
|
|
|
public function isFrontAjax() |
586
|
|
|
{ |
587
|
|
|
return $this->request_type->isFrontAjax(); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
|
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* @return bool |
594
|
|
|
*/ |
595
|
|
|
public function isIframe() |
596
|
|
|
{ |
597
|
|
|
return $this->request_type->isIframe(); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* @return string |
603
|
|
|
*/ |
604
|
|
|
public function slug() |
605
|
|
|
{ |
606
|
|
|
return $this->request_type->slug(); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
|
610
|
|
|
} |
611
|
|
|
// Location: Request.php |
612
|
|
|
|