Completed
Branch BUG-10738-inconsistency-in-ses... (a1eed8)
by
unknown
24:27 queued 12:29
created

EE_Request::params()   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 0
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
77
    /**
78
     * class constructor
79
     *
80
     * @access    public
81
     * @param array $get
82
     * @param array $post
83
     * @param array $cookie
84
     */
85
    public function __construct(array $get, array $post, array $cookie)
86
    {
87
        // grab request vars
88
        $this->_get    = $get;
89
        $this->_post   = $post;
90
        $this->_cookie = $cookie;
91
        $this->_params = array_merge($this->_get, $this->_post);
92
        // AJAX ???
93
        $this->ajax       = defined('DOING_AJAX') && DOING_AJAX;
94
        $this->front_ajax = $this->ajax
95
                            && $this->is_set('ee_front_ajax')
96
                            && filter_var($this->get('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN);
97
        $this->admin      = is_admin() && ! $this->ajax;
98
        // grab user IP
99
        $this->_ip_address = $this->_visitor_ip();
100
    }
101
102
103
104
    /**
105
     * @return array
106
     */
107
    public function get_params()
108
    {
109
        return $this->_get;
110
    }
111
112
113
114
    /**
115
     * @return array
116
     */
117
    public function post_params()
118
    {
119
        return $this->_post;
120
    }
121
122
123
124
    /**
125
     * @return array
126
     */
127
    public function cookie_params()
128
    {
129
        return $this->_cookie;
130
    }
131
132
133
134
    /**
135
     * returns contents of $_REQUEST
136
     *
137
     * @return array
138
     */
139
    public function params()
140
    {
141
        return $this->_params;
142
    }
143
144
145
146
    /**
147
     * @param      $key
148
     * @param      $value
149
     * @param bool $override_ee
150
     * @return    void
151
     */
152
    public function set($key, $value, $override_ee = false)
153
    {
154
        // don't allow "ee" to be overwritten unless explicitly instructed to do so
155
        if (
156
            $key !== 'ee'
157
            || ($key === 'ee' && empty($this->_params['ee']))
158
            || ($key === 'ee' && ! empty($this->_params['ee']) && $override_ee)
159
        ) {
160
            $this->_params[$key] = $value;
161
        }
162
    }
163
164
165
166
    /**
167
     * returns   the value for a request param if the given key exists
168
     *
169
     * @param       $key
170
     * @param null  $default
171
     * @return mixed
172
     */
173
    public function get($key, $default = null)
174
    {
175
        return $this->request_parameter_drill_down($key, $default, 'get');
176
    }
177
178
179
180
    /**
181
     * check if param exists
182
     *
183
     * @param       $key
184
     * @return bool
185
     */
186
    public function is_set($key)
187
    {
188
        return $this->request_parameter_drill_down($key);
189
    }
190
191
192
193
    /**
194
     * the supplied key can be a simple string to represent a "top-level" request parameter
195
     * or represent a key for a request parameter that is nested deeper within the request parameter array,
196
     * by using square brackets to surround keys for deeper array elements.
197
     * For example :
198
     * if the supplied $key was: "first[second][third]"
199
     * then this will attempt to drill down into the request parameter array to find a value.
200
     * Given the following request parameters:
201
     *  array(
202
     *      'first' => array(
203
     *          'second' => array(
204
     *              'third' => 'has a value'
205
     *          )
206
     *      )
207
     *  )
208
     * would return true
209
     *
210
     * @param string $is_set_or_get
211
     * @param        $key
212
     * @param null   $default
213
     * @param array  $request_params
214
     * @return bool|mixed|null
215
     */
216
    private function request_parameter_drill_down(
217
        $key,
218
        $default = null,
219
        $is_set_or_get = 'is_set',
220
        array $request_params = array()
221
    ) {
222
        $request_params = ! empty($request_params)
223
            ? $request_params
224
            : $this->_params;
225
        // does incoming key represent an array like 'first[second][third]'  ?
226
        if (strpos($key, '[') !== false) {
227
            // turn it into an actual array
228
            $key  = str_replace(']', '', $key);
229
            $keys = explode('[', $key);
230
            $key  = array_shift($keys);
231
            // check if top level key exists
232
            if (isset($request_params[$key])) {
233
                // build a new key to pass along like: 'second[third]'
234
                // or just 'second' depending on depth of keys
235
                $key_string = array_shift($keys);
236
                if (! empty($keys)) {
237
                    $key_string .= '[' . implode('][', $keys) . ']';
238
                }
239
                return $this->request_parameter_drill_down(
240
                    $key_string,
241
                    $default,
242
                    $is_set_or_get,
243
                    $request_params[$key]
244
                );
245
            }
246
        }
247
        if ($is_set_or_get === 'is_set') {
248
            return isset($request_params[$key]);
249
        }
250
        return isset($request_params[$key])
251
            ? $request_params[$key]
252
            : $default;
253
    }
254
255
256
257
    /**
258
     * remove param
259
     *
260
     * @param      $key
261
     * @param bool $unset_from_global_too
262
     */
263
    public function un_set($key, $unset_from_global_too = false)
264
    {
265
        unset($this->_params[$key]);
266
        if ($unset_from_global_too) {
267
            unset($_REQUEST[$key]);
268
        }
269
    }
270
271
272
273
    /**
274
     * @return string
275
     */
276
    public function ip_address()
277
    {
278
        return $this->_ip_address;
279
    }
280
281
282
    /**
283
     * @return bool
284
     */
285
    public function isAdmin()
286
    {
287
        return $this->admin;
288
    }
289
290
291
    /**
292
     * @return mixed
293
     */
294
    public function isAjax()
295
    {
296
        return $this->ajax;
297
    }
298
299
300
    /**
301
     * @return mixed
302
     */
303
    public function isFrontAjax()
304
    {
305
        return $this->front_ajax;
306
    }
307
308
309
310
    /**
311
     * _visitor_ip
312
     *    attempt to get IP address of current visitor from server
313
     * plz see: http://stackoverflow.com/a/2031935/1475279
314
     *
315
     * @access public
316
     * @return string
317
     */
318 View Code Duplication
    private function _visitor_ip()
319
    {
320
        $visitor_ip  = '0.0.0.0';
321
        $server_keys = array(
322
            'HTTP_CLIENT_IP',
323
            'HTTP_X_FORWARDED_FOR',
324
            'HTTP_X_FORWARDED',
325
            'HTTP_X_CLUSTER_CLIENT_IP',
326
            'HTTP_FORWARDED_FOR',
327
            'HTTP_FORWARDED',
328
            'REMOTE_ADDR',
329
        );
330
        foreach ($server_keys as $key) {
331
            if (isset($_SERVER[$key])) {
332
                foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) {
333
                    if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) {
334
                        $visitor_ip = $ip;
335
                    }
336
                }
337
            }
338
        }
339
        return $visitor_ip;
340
    }
341
342
343
344
}
345
// End of file EE_Request.core.php
346
// Location: /core/request_stack/EE_Request.core.php
347