Completed
Push — master ( 33f65d...fb9c39 )
by Richard
12s
created

Request::getDateTime()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 11
c 2
b 1
f 0
nc 5
nop 3
dl 0
loc 14
rs 8.8571
ccs 9
cts 10
cp 0.9
crap 6.0359
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf;
13
14
use Xoops\Core\Locale\Time;
15
16
/**
17
 * Request Class
18
 *
19
 * This class serves to provide a common interface to access
20
 * request variables.  This includes $_POST, $_GET, and naturally $_REQUEST.  Variables
21
 * can be passed through an input filter to avoid injection or returned raw.
22
 *
23
 * @category  Xmf\Request
24
 * @package   Xmf
25
 * @author    Richard Griffith <[email protected]>
26
 * @author    trabis <[email protected]>
27
 * @author    Joomla!
28
 * @copyright 2011-2016 XOOPS Project (http://xoops.org)
29
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
30
 * @link      http://xoops.org
31
 * @since     1.0
32
 */
33
class Request
34
{
35
    /**
36
     * Available masks for cleaning variables
37
     */
38
    const MASK_NO_TRIM    = 1;
39
    const MASK_ALLOW_RAW  = 2;
40
    const MASK_ALLOW_HTML = 4;
41
42
    /**
43
     * Gets the request method
44
     *
45
     * @return string
46
     */
47 2
    public static function getMethod()
0 ignored issues
show
Coding Style introduced by
getMethod uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48
    {
49 2
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
50
51 2
        return $method;
52
    }
53
54
    /**
55
     * Fetches and returns a given variable.
56
     *
57
     * The default behaviour is fetching variables depending on the
58
     * current request method: GET and HEAD will result in returning
59
     * an entry from $_GET, POST and PUT will result in returning an
60
     * entry from $_POST.
61
     *
62
     * You can force the source by setting the $hash parameter:
63
     *
64
     *  - post       $_POST
65
     *  - get        $_GET
66
     *  - files      $_FILES
67
     *  - cookie     $_COOKIE
68
     *  - env        $_ENV
69
     *  - server     $_SERVER
70
     *  - method     via current $_SERVER['REQUEST_METHOD']
71
     *  - default    $_REQUEST
72
     *
73
     * @param string $name    Variable name
74
     * @param mixed  $default Default value if the variable does not exist
75
     * @param string $hash    Source of variable value (POST, GET, FILES, COOKIE, METHOD)
76
     * @param string $type    Return type for the variable (INT, FLOAT, BOOLEAN, WORD,
77
     *                         ALPHANUM, CMD, BASE64, STRING, ARRAY, PATH, NONE) For more
78
     *                         information see FilterInput::clean().
79
     * @param int    $mask    Filter mask for the variable
80
     *
81
     * @return mixed Requested variable
82
     */
83 3
    public static function getVar($name, $default = null, $hash = 'default', $type = 'none', $mask = 0)
0 ignored issues
show
Coding Style introduced by
getVar uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getVar uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getVar uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getVar uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getVar uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getVar uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getVar uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
    {
85
        // Ensure hash and type are uppercase
86 3
        $hash = strtoupper($hash);
87 3
        if ($hash === 'METHOD') {
88
            $hash = static::getMethod();
89
        }
90 3
        $type = strtoupper($type);
91
92
        // Get the input hash
93
        switch ($hash) {
94 3
            case 'GET':
95
                $input = &$_GET;
96
                break;
97 3
            case 'POST':
98 1
                $input = &$_POST;
99 1
                break;
100 2
            case 'FILES':
101
                $input = &$_FILES;
102
                break;
103 2
            case 'COOKIE':
104
                $input = &$_COOKIE;
105
                break;
106 2
            case 'ENV':
107
                $input = &$_ENV;
108
                break;
109 2
            case 'SERVER':
110
                $input = &$_SERVER;
111
                break;
112
            default:
113 2
                $input = &$_REQUEST;
114 2
                break;
115
        }
116
117 3
        if (isset($input[$name]) && $input[$name] !== null) {
118
            // Get the variable from the input hash and clean it
119 3
            $var = static::cleanVar($input[$name], $mask, $type);
120
        } else {
121 3
            if ($default !== null) {
122
                // Clean the default value
123 1
                $var = static::cleanVar($default, $mask, $type);
124
            } else {
125 2
                $var = $default;
126
            }
127
        }
128
129 3
        return $var;
130
    }
131
132
    /**
133
     * Fetches and returns a given filtered variable. The integer
134
     * filter will allow only digits to be returned. This is currently
135
     * only a proxy function for getVar().
136
     *
137
     * See getVar() for more in-depth documentation on the parameters.
138
     *
139
     * @param string $name    Variable name
140
     * @param int    $default Default value if the variable does not exist
141
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
142
     *
143
     * @return int Requested variable
144
     */
145 2
    public static function getInt($name, $default = 0, $hash = 'default')
146
    {
147 2
        return static::getVar($name, $default, $hash, 'int');
148
    }
149
150
    /**
151
     * Fetches and returns a given filtered variable.  The float
152
     * filter only allows digits and periods.  This is currently
153
     * only a proxy function for getVar().
154
     *
155
     * See getVar() for more in-depth documentation on the parameters.
156
     *
157
     * @param string $name    Variable name
158
     * @param float  $default Default value if the variable does not exist
159
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
160
     *
161
     * @return float Requested variable
162
     */
163 2
    public static function getFloat($name, $default = 0.0, $hash = 'default')
164
    {
165 2
        return static::getVar($name, $default, $hash, 'float');
166
    }
167
168
    /**
169
     * Fetches and returns a given filtered variable. The bool
170
     * filter will only return true/false bool values. This is
171
     * currently only a proxy function for getVar().
172
     *
173
     * See getVar() for more in-depth documentation on the parameters.
174
     *
175
     * @param string $name    Variable name
176
     * @param bool   $default Default value if the variable does not exist
177
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
178
     *
179
     * @return bool Requested variable
180
     */
181 2
    public static function getBool($name, $default = false, $hash = 'default')
182
    {
183 2
        return static::getVar($name, $default, $hash, 'bool');
184
    }
185
186
    /**
187
     * Fetches and returns a given filtered variable. The word
188
     * filter only allows the characters [A-Za-z_]. This is currently
189
     * only a proxy function for getVar().
190
     *
191
     * See getVar() for more in-depth documentation on the parameters.
192
     *
193
     * @param string $name    Variable name
194
     * @param string $default Default value if the variable does not exist
195
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
196
     *
197
     * @return string Requested variable
198
     */
199 2
    public static function getWord($name, $default = '', $hash = 'default')
200
    {
201 2
        return static::getVar($name, $default, $hash, 'word');
202
    }
203
204
    /**
205
     * Fetches and returns a given filtered variable. The cmd filter only allows the characters
206
     * [A-Za-z0-9.-_] and returns in lower case. This is currently a proxy function for getVar().
207
     *
208
     * See getVar() for more in-depth documentation on the parameters.
209
     *
210
     * @param string $name    Variable name
211
     * @param string $default Default value if the variable does not exist
212
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
213
     *
214
     * @return string Requested variable
215
     */
216 2
    public static function getCmd($name, $default = '', $hash = 'default')
217
    {
218 2
        return static::getVar($name, $default, $hash, 'cmd');
219
    }
220
221
    /**
222
     * Fetches and returns a given filtered variable. The string
223
     * filter deletes 'bad' HTML code, if not overridden by the mask.
224
     * This is currently only a proxy function for getVar().
225
     *
226
     * See getVar() for more in-depth documentation on the parameters.
227
     *
228
     * @param string $name    Variable name
229
     * @param string $default Default value if the variable does not exist
230
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
231
     * @param int    $mask    Filter mask for the variable
232
     *
233
     * @return string Requested variable
234
     */
235 3
    public static function getString($name, $default = '', $hash = 'default', $mask = 0)
236
    {
237
        // Cast to string, in case static::MASK_ALLOW_RAW was specified for mask
238 3
        return (string) static::getVar($name, $default, $hash, 'string', $mask);
239
    }
240
241
    /**
242
     * Fetches and returns an array
243
     *
244
     * @param string $name    Variable name
245
     * @param mixed  $default Default value if the variable does not exist
246
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
247
     *
248
     * @return array
249
     */
250 2
    public static function getArray($name, $default = array(), $hash = 'default')
251
    {
252 2
        return static::getVar($name, $default, $hash, 'array');
253
    }
254
255
    /**
256
     * Fetches and returns raw text
257
     *
258
     * @param string $name    Variable name
259
     * @param string $default Default value if the variable does not exist
260
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
261
     *
262
     * @return string Requested variable
263
     */
264 2
    public static function getText($name, $default = '', $hash = 'default')
265
    {
266 2
        return (string) static::getVar($name, $default, $hash, 'string', static::MASK_ALLOW_RAW);
267
    }
268
269
    /**
270
     * Fetches and returns a web url
271
     *
272
     * @param string $name    Variable name
273
     * @param string $default Default value if the variable does not exist
274
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
275
     *
276
     * @return string Requested variable
277
     */
278 1
    public static function getUrl($name, $default = '', $hash = 'default')
279
    {
280 1
        return (string) static::getVar($name, $default, $hash, 'weburl');
281
    }
282
283
    /**
284
     * Fetches and returns a file (or web) path
285
     *
286
     * @param string $name    Variable name
287
     * @param string $default Default value if the variable does not exist
288
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
289
     *
290
     * @return string Requested variable
291
     */
292 1
    public static function getPath($name, $default = '', $hash = 'default')
293
    {
294 1
        return (string) static::getVar($name, $default, $hash, 'path');
295
    }
296
297
    /**
298
     * Fetches and returns an email address
299
     *
300
     * @param string $name    Variable name
301
     * @param string $default Default value if the variable does not exist
302
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
303
     *
304
     * @return string email address or default if invalid
305
     */
306 1
    public static function getEmail($name, $default = '', $hash = 'default')
307
    {
308 1
        $ret = (string) static::getVar($name, $default, $hash, 'email');
309 1
        return empty($ret) ? $default : $ret;
310
    }
311
312
    /**
313
     * Fetches and returns an IP address
314
     *
315
     * @param string $name    Variable name
316
     * @param string $default Default value if the variable does not exist
317
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
318
     *
319
     * @return string IP address or default if invalid
320
     */
321 2
    public static function getIP($name, $default = '', $hash = 'default')
322
    {
323 2
        $ret = (string) static::getVar($name, $default, $hash, 'ip');
324 2
        return empty($ret) ? $default : $ret;
325
    }
326
327
    /**
328
     * Return a DateTime object from a Xoops\Form\DateSelect of Xoops\Form\DateTime field
329
     *
330
     * @param string $name    Variable name
331
     * @param mixed  $default Default value if the variable does not exist
332
     * @param string $hash    Where the var should come from (POST, GET, FILES, COOKIE, METHOD)
333
     *
334
     * @return \DateTime object
335
     */
336 1
    public static function getDateTime($name, $default = null, $hash = 'default')
337
    {
338 1
        $values = self::getVar($name, [], $hash, 'array');
339 1
        $count = count($values);
340 1
        if ($count === 1) {
341 1
            $date = reset($values);
342 1
            $ret = (empty($date)) ? $default : Time::inputToDateTime($date);
343 1
        } elseif (isset($values['date']) && isset($values['time'])) {
344 1
            $ret = (empty($values['date'])) ? $default : Time::inputToDateTime($values);
345
        } else {
346
            $ret = $default;
347
        }
348 1
        return $ret;
349
    }
350
351
    /**
352
     * get request header
353
     *
354
     * @param string      $headerName name of header to retrieve, case insensitive
355
     * @param string|null $default    default to return if named header is not found
356
     *
357
     * @return string header value or default if header was not found
358
     */
359
    public static function getHeader($headerName, $default = '')
0 ignored issues
show
Coding Style introduced by
getHeader uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
360
    {
361
        static $headers = null;
362
363
        if (null === $headers) {
364
            $headers = array();
365
            if (function_exists('apache_request_headers')) {
366
                $rawHeaders = apache_request_headers();
367
                foreach ($rawHeaders as $name => $value) {
368
                    $headers[strtolower($name)] = $value;
369
                }
370
            } else {
371
                // From joyview - http://php.net/manual/en/function.getallheaders.php
372
                foreach ($_SERVER as $name => $value) {
373
                    if (substr($name, 0, 5) === 'HTTP_') {
374
                        $translatedName = str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))));
375
                        $headers[$translatedName] = $value;
376
                    }
377
                }
378
            }
379
        }
380
381
        $name = strtolower($headerName);
382
        if (isset($headers[$name])) {
383
            return static::cleanVar($headers[$name]);
384
        }
385
        return $default;
386
    }
387
388
    /**
389
     * See if a variable exists in one of the request hashes
390
     *
391
     * @param string $name variable to look for
392
     * @param string $hash hash to check
393
     *
394
     * @return boolean True if hash has an element 'name', otherwise false
395
     */
396 1
    public static function hasVar($name, $hash = 'method')
0 ignored issues
show
Coding Style introduced by
hasVar uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
397
    {
398 1
        $hash = strtoupper($hash);
399 1
        if ($hash === 'METHOD') {
400
            $hash = strtoupper($_SERVER['REQUEST_METHOD']);
401
        }
402
403
        // Get the requested hash and determine existing value
404 1
        $original = static::get($hash, static::MASK_ALLOW_RAW);
405 1
        if (isset($original[$name])) {
406 1
            return true;
407
        }
408 1
        return false;
409
    }
410
411
    /**
412
     * Set a variable in one of the request variables
413
     *
414
     * @param string  $name      Name
415
     * @param string  $value     Value
416
     * @param string  $hash      Hash
417
     * @param boolean $overwrite Boolean
418
     *
419
     * @return string Previous value
420
     */
421 2
    public static function setVar($name, $value = null, $hash = 'method', $overwrite = true)
0 ignored issues
show
Coding Style introduced by
setVar uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setVar uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setVar uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setVar uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setVar uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setVar uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setVar uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
422
    {
423 2
        $hash = strtoupper($hash);
424 2
        if ($hash === 'METHOD') {
425
            $hash = strtoupper($_SERVER['REQUEST_METHOD']);
426
        }
427
428
        // Get the requested hash and determine existing value
429 2
        $original = static::get($hash, static::MASK_ALLOW_RAW);
430 2
        if (isset($original[$name])) {
431 1
            $previous = $original[$name];
432
            // don't overwrite value unless asked
433 1
            if (!$overwrite) {
434 1
                return $previous;
435
            }
436
        } else {
437 1
            $previous = null;
438
        }
439
440
        // set the value
441
        switch ($hash) {
442 2
            case 'GET':
443 2
                $_GET[$name] = $value;
444 2
                $_REQUEST[$name] = $value;
445 2
                break;
446
            case 'POST':
447
                $_POST[$name] = $value;
448
                $_REQUEST[$name] = $value;
449
                break;
450
            case 'REQUEST':
451
                $_REQUEST[$name] = $value;
452
                break;
453
            case 'COOKIE':
454
                $_COOKIE[$name] = $value;
455
                $_REQUEST[$name] = $value;
456
                break;
457
            case 'FILES':
458
                $_FILES[$name] = $value;
459
                break;
460
            case 'ENV':
461
                $_ENV['name'] = $value;
462
                break;
463
            case 'SERVER':
464
                $_SERVER['name'] = $value;
465
                break;
466
        }
467
468 2
        return $previous;
469
    }
470
471
    /**
472
     * Fetches and returns a request array.
473
     *
474
     * The default behaviour is fetching variables depending on the
475
     * current request method: GET and HEAD will result in returning
476
     * $_GET, POST and PUT will result in returning $_POST.
477
     *
478
     * You can force the source by setting the $hash parameter:
479
     *
480
     *  - post        $_POST
481
     *  - get         $_GET
482
     *  - files       $_FILES
483
     *  - cookie      $_COOKIE
484
     *  - env         $_ENV
485
     *  - server      $_SERVER
486
     *  - method      via current $_SERVER['REQUEST_METHOD']
487
     *  - default     $_REQUEST
488
     *
489
     * @param string $hash to get (POST, GET, FILES, METHOD)
490
     * @param int    $mask Filter mask for the variable
491
     *
492
     * @return mixed Request hash
493
     */
494 2
    public static function get($hash = 'default', $mask = 0)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
get uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
get uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
get uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
get uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
get uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
get uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
495
    {
496 2
        $hash = strtoupper($hash);
497
498 2
        if ($hash === 'METHOD') {
499
            $hash = strtoupper($_SERVER['REQUEST_METHOD']);
500
        }
501
502
        switch ($hash) {
503 2
            case 'GET':
504
                $input = $_GET;
505
                break;
506 2
            case 'POST':
507
                $input = $_POST;
508
                break;
509 2
            case 'FILES':
510
                $input = $_FILES;
511
                break;
512 2
            case 'COOKIE':
513
                $input = $_COOKIE;
514
                break;
515 2
            case 'ENV':
516
                $input = &$_ENV;
517
                break;
518 2
            case 'SERVER':
519
                $input = &$_SERVER;
520
                break;
521
            default:
522 2
                $input = $_REQUEST;
523 2
                break;
524
        }
525
526 2
        $result = static::cleanVars($input, $mask);
527
528 2
        return $result;
529
    }
530
531
    /**
532
     * Sets a request variable
533
     *
534
     * @param array   $array     An associative array of key-value pairs
535
     * @param string  $hash      The request variable to set (POST, GET, FILES, METHOD)
536
     * @param boolean $overwrite If true and an existing key is found, the value is overwritten,
537
     *                            otherwise it is ignored
538
     *
539
     * @return void
540
     */
541 2
    public static function set($array, $hash = 'method', $overwrite = true)
542
    {
543 2
        foreach ($array as $key => $value) {
544 2
            static::setVar($key, $value, $hash, $overwrite);
545
        }
546 2
    }
547
548
    /**
549
     * Clean up an input variable.
550
     *
551
     * @param mixed  $var  The input variable.
552
     * @param int    $mask Filter bit mask.
553
     *                      - 1=no trim: If this flag is cleared and the input is a string,
554
     *                        the string will have leading and trailing whitespace trimmed.
555
     *                      - 2=allow_raw: If set, no more filtering is performed, higher bits are ignored.
556
     *                      - 4=allow_html: HTML is allowed, but passed through a safe HTML filter first.
557
     *                        If set, no more filtering is performed.
558
     *                      - If no bits other than the 1 bit is set, a strict filter is applied.
559
     * @param string $type The variable type. See {@link FilterInput::clean()}.
560
     *
561
     * @return string
562
     */
563 1
    protected static function cleanVar($var, $mask = 0, $type = null)
564
    {
565
        // Static input filters for specific settings
566 1
        static $noHtmlFilter = null;
567 1
        static $safeHtmlFilter = null;
568
569
        // convert $var in array if $type is ARRAY
570 1
        if (strtolower($type) === 'array' && !is_array($var)) {
571
            $var = array($var);
572
        }
573
574
        // If the no trim flag is not set, trim the variable
575 1
        if (!($mask & static::MASK_NO_TRIM) && is_string($var)) {
576 1
            $var = trim($var);
577
        }
578
579
        // Now we handle input filtering
580
        // If the allow raw flag is set, do not modify the variable
581 1
        if (!($mask & static::MASK_ALLOW_RAW)) {
582 1
            if ($mask & static::MASK_ALLOW_HTML) {
583
                // If the allow html flag is set, apply a safe html filter to the variable
584
                if (null === $safeHtmlFilter) {
585
                    $safeHtmlFilter = FilterInput::getInstance(array(), array(), 1, 1);
586
                }
587
                $var = $safeHtmlFilter->clean($var, $type);
588
            } else {
589
                // Since no allow flags were set, we will apply the most strict filter to the variable
590 1
                if (null === $noHtmlFilter) {
591
                    $noHtmlFilter = FilterInput::getInstance();
592
                }
593 1
                $var = $noHtmlFilter->clean($var, $type);
594
            }
595
        }
596
597 1
        return $var;
598
    }
599
600
    /**
601
     * Clean up an array of variables.
602
     *
603
     * @param mixed  $var  The input variable.
604
     * @param int    $mask Filter bit mask. See {@link Request::cleanVar()}
605
     * @param string $type The variable type. See {@link FilterInput::clean()}.
606
     *
607
     * @return string
608
     */
609
    protected static function cleanVars($var, $mask = 0, $type = null)
610
    {
611
        if (is_array($var)) {
612
            foreach ($var as $key => &$value) {
613
                $value = static::cleanVars($value, $mask, $type);
614
            }
615
        } else {
616
            $var = static::cleanVar($var, $mask, $type);
617
        }
618
619
        return $var;
620
    }
621
}
622