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