Completed
Branch FET/11174/bot-detection-middle... (17f260)
by
unknown
93:18 queued 82:13
created

EE_Request::setIsBot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\interfaces\InterminableInterface;
4
5
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed');
6
7
8
9
/**
10
 * class EE_Request
11
 *
12
 * @package     Event Espresso
13
 * @subpackage  /core/
14
 * @author      Brent Christensen
15
 */
16
class EE_Request implements InterminableInterface
17
{
18
19
    /**
20
     * $_GET parameters
21
     *
22
     * @var array $_get
23
     */
24
    private $_get;
25
26
    /**
27
     * $_POST parameters
28
     *
29
     * @var    array $_post
30
     */
31
    private $_post;
32
33
    /**
34
     * $_COOKIE parameters
35
     *
36
     * @var array $_cookie
37
     */
38
    private $_cookie;
39
40
    /**
41
     * $_REQUEST parameters
42
     *
43
     * @var array $_params
44
     */
45
    private $_params;
46
47
    /**
48
     * whether current request is for the admin but NOT via AJAX
49
     *
50
     * @var boolean $admin
51
     */
52
    public $admin = false;
53
54
    /**
55
     * whether current request is via AJAX
56
     *
57
     * @var boolean $ajax
58
     */
59
    public $ajax = false;
60
61
    /**
62
     * whether current request is via AJAX from the frontend of the site
63
     *
64
     * @var boolean $front_ajax
65
     */
66
    public $front_ajax = false;
67
68
    /**
69
     * IP address for request
70
     *
71
     * @var string $_ip_address
72
     */
73
    private $_ip_address;
74
75
    /**
76
     * @var string $user_agent
77
     */
78
    private $user_agent;
79
80
    /**
81
     * true if current user appears to be some kind of bot
82
     *
83
     * @var bool $is_bot
84
     */
85
    private $is_bot;
86
87
88
89
    /**
90
     * class constructor
91
     *
92
     * @access    public
93
     * @param array $get
94
     * @param array $post
95
     * @param array $cookie
96
     */
97
    public function __construct(array $get, array $post, array $cookie)
98
    {
99
        // grab request vars
100
        $this->_get    = $get;
101
        $this->_post   = $post;
102
        $this->_cookie = $cookie;
103
        $this->_params = array_merge($this->_get, $this->_post);
104
        // AJAX ???
105
        $this->ajax       = defined('DOING_AJAX') && DOING_AJAX;
106
        $this->front_ajax = $this->ajax
107
                            && $this->is_set('ee_front_ajax')
108
                            && filter_var($this->get('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN);
109
        $this->admin      = is_admin() && ! $this->ajax;
110
        // grab user IP
111
        $this->_ip_address = $this->_visitor_ip();
112
    }
113
114
115
116
    /**
117
     * @return array
118
     */
119
    public function get_params()
120
    {
121
        return $this->_get;
122
    }
123
124
125
126
    /**
127
     * @return array
128
     */
129
    public function post_params()
130
    {
131
        return $this->_post;
132
    }
133
134
135
136
    /**
137
     * @return array
138
     */
139
    public function cookie_params()
140
    {
141
        return $this->_cookie;
142
    }
143
144
145
146
    /**
147
     * returns contents of $_REQUEST
148
     *
149
     * @return array
150
     */
151
    public function params()
152
    {
153
        return $this->_params;
154
    }
155
156
157
158
    /**
159
     * @param      $key
160
     * @param      $value
161
     * @param bool $override_ee
162
     * @return    void
163
     */
164
    public function set($key, $value, $override_ee = false)
165
    {
166
        // don't allow "ee" to be overwritten unless explicitly instructed to do so
167
        if (
168
            $key !== 'ee'
169
            || ($key === 'ee' && empty($this->_params['ee']))
170
            || ($key === 'ee' && ! empty($this->_params['ee']) && $override_ee)
171
        ) {
172
            $this->_params[$key] = $value;
173
        }
174
    }
175
176
177
178
    /**
179
     * returns   the value for a request param if the given key exists
180
     *
181
     * @param       $key
182
     * @param null  $default
183
     * @return mixed
184
     */
185
    public function get($key, $default = null)
186
    {
187
        return $this->request_parameter_drill_down($key, $default, 'get');
188
    }
189
190
191
192
    /**
193
     * check if param exists
194
     *
195
     * @param       $key
196
     * @return bool
197
     */
198
    public function is_set($key)
199
    {
200
        return $this->request_parameter_drill_down($key);
201
    }
202
203
204
205
    /**
206
     * the supplied key can be a simple string to represent a "top-level" request parameter
207
     * or represent a key for a request parameter that is nested deeper within the request parameter array,
208
     * by using square brackets to surround keys for deeper array elements.
209
     * For example :
210
     * if the supplied $key was: "first[second][third]"
211
     * then this will attempt to drill down into the request parameter array to find a value.
212
     * Given the following request parameters:
213
     *  array(
214
     *      'first' => array(
215
     *          'second' => array(
216
     *              'third' => 'has a value'
217
     *          )
218
     *      )
219
     *  )
220
     * would return true
221
     *
222
     * @param string $is_set_or_get
223
     * @param        $key
224
     * @param null   $default
225
     * @param array  $request_params
226
     * @return bool|mixed|null
227
     */
228
    private function request_parameter_drill_down(
229
        $key,
230
        $default = null,
231
        $is_set_or_get = 'is_set',
232
        array $request_params = array()
233
    ) {
234
        $request_params = ! empty($request_params)
235
            ? $request_params
236
            : $this->_params;
237
        // does incoming key represent an array like 'first[second][third]'  ?
238
        if (strpos($key, '[') !== false) {
239
            // turn it into an actual array
240
            $key  = str_replace(']', '', $key);
241
            $keys = explode('[', $key);
242
            $key  = array_shift($keys);
243
            // check if top level key exists
244
            if (isset($request_params[$key])) {
245
                // build a new key to pass along like: 'second[third]'
246
                // or just 'second' depending on depth of keys
247
                $key_string = array_shift($keys);
248
                if (! empty($keys)) {
249
                    $key_string .= '[' . implode('][', $keys) . ']';
250
                }
251
                return $this->request_parameter_drill_down(
252
                    $key_string,
253
                    $default,
254
                    $is_set_or_get,
255
                    $request_params[$key]
256
                );
257
            }
258
        }
259
        if ($is_set_or_get === 'is_set') {
260
            return isset($request_params[$key]);
261
        }
262
        return isset($request_params[$key])
263
            ? $request_params[$key]
264
            : $default;
265
    }
266
267
268
269
    /**
270
     * remove param
271
     *
272
     * @param      $key
273
     * @param bool $unset_from_global_too
274
     */
275
    public function un_set($key, $unset_from_global_too = false)
276
    {
277
        unset($this->_params[$key]);
278
        if ($unset_from_global_too) {
279
            unset($_REQUEST[$key]);
280
        }
281
    }
282
283
284
285
    /**
286
     * @return string
287
     */
288
    public function ip_address()
289
    {
290
        return $this->_ip_address;
291
    }
292
293
294
    /**
295
     * @return bool
296
     */
297
    public function isAdmin()
298
    {
299
        return $this->admin;
300
    }
301
302
303
    /**
304
     * @return mixed
305
     */
306
    public function isAjax()
307
    {
308
        return $this->ajax;
309
    }
310
311
312
    /**
313
     * @return mixed
314
     */
315
    public function isFrontAjax()
316
    {
317
        return $this->front_ajax;
318
    }
319
320
321
322
    /**
323
     * _visitor_ip
324
     *    attempt to get IP address of current visitor from server
325
     * plz see: http://stackoverflow.com/a/2031935/1475279
326
     *
327
     * @access public
328
     * @return string
329
     */
330
    private function _visitor_ip()
331
    {
332
        $visitor_ip  = '0.0.0.0';
333
        $server_keys = array(
334
            'HTTP_CLIENT_IP',
335
            'HTTP_X_FORWARDED_FOR',
336
            'HTTP_X_FORWARDED',
337
            'HTTP_X_CLUSTER_CLIENT_IP',
338
            'HTTP_FORWARDED_FOR',
339
            'HTTP_FORWARDED',
340
            'REMOTE_ADDR',
341
        );
342
        foreach ($server_keys as $key) {
343
            if (isset($_SERVER[$key])) {
344
                foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) {
345
                    if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) {
346
                        $visitor_ip = $ip;
347
                    }
348
                }
349
            }
350
        }
351
        return $visitor_ip;
352
    }
353
354
355
    /**
356
     * @return string
357
     */
358
    public function userAgent()
359
    {
360
        return $this->user_agent;
361
    }
362
363
364
    /**
365
     * @param string $user_agent
366
     */
367
    public function setUserAgent($user_agent = '')
368
    {
369
        if ($user_agent === '' || ! is_string($user_agent)) {
370
            $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? esc_attr($_SERVER['HTTP_USER_AGENT']) : '';
371
        }
372
        $this->user_agent = $user_agent;
373
    }
374
375
376
    /**
377
     * @return bool
378
     */
379
    public function isBot()
380
    {
381
        return $this->is_bot;
382
    }
383
384
385
    /**
386
     * @param bool $is_bot
387
     */
388
    public function setIsBot($is_bot)
389
    {
390
        $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN);
391
    }
392
393
394
395
}
396
// End of file EE_Request.core.php
397
// Location: /core/request_stack/EE_Request.core.php
398