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
|
|
|
* @version Release: 1.0 |
31
|
|
|
* @link http://xoops.org |
32
|
|
|
* @since 1.0 |
33
|
|
|
*/ |
34
|
|
|
class Request |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Available masks for cleaning variables |
38
|
|
|
*/ |
39
|
|
|
const MASK_NO_TRIM = 1; |
40
|
|
|
const MASK_ALLOW_RAW = 2; |
41
|
|
|
const MASK_ALLOW_HTML = 4; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets the request method |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
2 |
|
public static function getMethod() |
|
|
|
|
49
|
|
|
{ |
50
|
2 |
|
$method = strtoupper($_SERVER['REQUEST_METHOD']); |
51
|
|
|
|
52
|
2 |
|
return $method; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Fetches and returns a given variable. |
57
|
|
|
* |
58
|
|
|
* The default behaviour is fetching variables depending on the |
59
|
|
|
* current request method: GET and HEAD will result in returning |
60
|
|
|
* an entry from $_GET, POST and PUT will result in returning an |
61
|
|
|
* entry from $_POST. |
62
|
|
|
* |
63
|
|
|
* You can force the source by setting the $hash parameter: |
64
|
|
|
* |
65
|
|
|
* - post $_POST |
66
|
|
|
* - get $_GET |
67
|
|
|
* - files $_FILES |
68
|
|
|
* - cookie $_COOKIE |
69
|
|
|
* - env $_ENV |
70
|
|
|
* - server $_SERVER |
71
|
|
|
* - method via current $_SERVER['REQUEST_METHOD'] |
72
|
|
|
* - default $_REQUEST |
73
|
|
|
* |
74
|
|
|
* @param string $name Variable name |
75
|
|
|
* @param mixed $default Default value if the variable does not exist |
76
|
|
|
* @param string $hash Source of variable value (POST, GET, FILES, COOKIE, METHOD) |
77
|
|
|
* @param string $type Return type for the variable (INT, FLOAT, BOOLEAN, WORD, |
78
|
|
|
* ALPHANUM, CMD, BASE64, STRING, ARRAY, PATH, NONE) For more |
79
|
|
|
* information see FilterInput::clean(). |
80
|
|
|
* @param int $mask Filter mask for the variable |
81
|
|
|
* |
82
|
|
|
* @return mixed Requested variable |
83
|
|
|
*/ |
84
|
3 |
|
public static function getVar($name, $default = null, $hash = 'default', $type = 'none', $mask = 0) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
// Ensure hash and type are uppercase |
87
|
3 |
|
$hash = strtoupper($hash); |
88
|
3 |
|
if ($hash === 'METHOD') { |
89
|
|
|
$hash = static::getMethod(); |
90
|
|
|
} |
91
|
3 |
|
$type = strtoupper($type); |
92
|
|
|
|
93
|
|
|
// Get the input hash |
94
|
|
|
switch ($hash) { |
95
|
3 |
|
case 'GET': |
96
|
|
|
$input = &$_GET; |
97
|
|
|
break; |
98
|
3 |
|
case 'POST': |
99
|
1 |
|
$input = &$_POST; |
100
|
1 |
|
break; |
101
|
2 |
|
case 'FILES': |
102
|
|
|
$input = &$_FILES; |
103
|
|
|
break; |
104
|
2 |
|
case 'COOKIE': |
105
|
|
|
$input = &$_COOKIE; |
106
|
|
|
break; |
107
|
2 |
|
case 'ENV': |
108
|
|
|
$input = &$_ENV; |
109
|
|
|
break; |
110
|
2 |
|
case 'SERVER': |
111
|
|
|
$input = &$_SERVER; |
112
|
|
|
break; |
113
|
|
|
default: |
114
|
2 |
|
$input = &$_REQUEST; |
115
|
2 |
|
break; |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
if (isset($input[$name]) && $input[$name] !== null) { |
119
|
|
|
// Get the variable from the input hash and clean it |
120
|
3 |
|
$var = static::cleanVar($input[$name], $mask, $type); |
121
|
|
|
} else { |
122
|
3 |
|
if ($default !== null) { |
123
|
|
|
// Clean the default value |
124
|
1 |
|
$var = static::cleanVar($default, $mask, $type); |
125
|
|
|
} else { |
126
|
2 |
|
$var = $default; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
return $var; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Fetches and returns a given filtered variable. The integer |
135
|
|
|
* filter will allow only digits to be returned. This is currently |
136
|
|
|
* only a proxy function for getVar(). |
137
|
|
|
* |
138
|
|
|
* See getVar() for more in-depth documentation on the parameters. |
139
|
|
|
* |
140
|
|
|
* @param string $name Variable name |
141
|
|
|
* @param int $default Default value if the variable does not exist |
142
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
143
|
|
|
* |
144
|
|
|
* @return int Requested variable |
145
|
|
|
*/ |
146
|
2 |
|
public static function getInt($name, $default = 0, $hash = 'default') |
147
|
|
|
{ |
148
|
2 |
|
return static::getVar($name, $default, $hash, 'int'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Fetches and returns a given filtered variable. The float |
153
|
|
|
* filter only allows digits and periods. This is currently |
154
|
|
|
* only a proxy function for getVar(). |
155
|
|
|
* |
156
|
|
|
* See getVar() for more in-depth documentation on the parameters. |
157
|
|
|
* |
158
|
|
|
* @param string $name Variable name |
159
|
|
|
* @param float $default Default value if the variable does not exist |
160
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
161
|
|
|
* |
162
|
|
|
* @return float Requested variable |
163
|
|
|
*/ |
164
|
2 |
|
public static function getFloat($name, $default = 0.0, $hash = 'default') |
165
|
|
|
{ |
166
|
2 |
|
return static::getVar($name, $default, $hash, 'float'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Fetches and returns a given filtered variable. The bool |
171
|
|
|
* filter will only return true/false bool values. This is |
172
|
|
|
* currently only a proxy function for getVar(). |
173
|
|
|
* |
174
|
|
|
* See getVar() for more in-depth documentation on the parameters. |
175
|
|
|
* |
176
|
|
|
* @param string $name Variable name |
177
|
|
|
* @param bool $default Default value if the variable does not exist |
178
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
179
|
|
|
* |
180
|
|
|
* @return bool Requested variable |
181
|
|
|
*/ |
182
|
2 |
|
public static function getBool($name, $default = false, $hash = 'default') |
183
|
|
|
{ |
184
|
2 |
|
return static::getVar($name, $default, $hash, 'bool'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Fetches and returns a given filtered variable. The word |
189
|
|
|
* filter only allows the characters [A-Za-z_]. This is currently |
190
|
|
|
* only a proxy function for getVar(). |
191
|
|
|
* |
192
|
|
|
* See getVar() for more in-depth documentation on the parameters. |
193
|
|
|
* |
194
|
|
|
* @param string $name Variable name |
195
|
|
|
* @param string $default Default value if the variable does not exist |
196
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
197
|
|
|
* |
198
|
|
|
* @return string Requested variable |
199
|
|
|
*/ |
200
|
2 |
|
public static function getWord($name, $default = '', $hash = 'default') |
201
|
|
|
{ |
202
|
2 |
|
return static::getVar($name, $default, $hash, 'word'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Fetches and returns a given filtered variable. The cmd filter only allows the characters |
207
|
|
|
* [A-Za-z0-9.-_] and returns in lower case. This is currently a proxy function for getVar(). |
208
|
|
|
* |
209
|
|
|
* See getVar() for more in-depth documentation on the parameters. |
210
|
|
|
* |
211
|
|
|
* @param string $name Variable name |
212
|
|
|
* @param string $default Default value if the variable does not exist |
213
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
214
|
|
|
* |
215
|
|
|
* @return string Requested variable |
216
|
|
|
*/ |
217
|
2 |
|
public static function getCmd($name, $default = '', $hash = 'default') |
218
|
|
|
{ |
219
|
2 |
|
return static::getVar($name, $default, $hash, 'cmd'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Fetches and returns a given filtered variable. The string |
224
|
|
|
* filter deletes 'bad' HTML code, if not overridden by the mask. |
225
|
|
|
* This is currently only a proxy function for getVar(). |
226
|
|
|
* |
227
|
|
|
* See getVar() for more in-depth documentation on the parameters. |
228
|
|
|
* |
229
|
|
|
* @param string $name Variable name |
230
|
|
|
* @param string $default Default value if the variable does not exist |
231
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
232
|
|
|
* @param int $mask Filter mask for the variable |
233
|
|
|
* |
234
|
|
|
* @return string Requested variable |
235
|
|
|
*/ |
236
|
3 |
|
public static function getString($name, $default = '', $hash = 'default', $mask = 0) |
237
|
|
|
{ |
238
|
|
|
// Cast to string, in case static::MASK_ALLOW_RAW was specified for mask |
239
|
3 |
|
return (string) static::getVar($name, $default, $hash, 'string', $mask); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Fetches and returns an array |
244
|
|
|
* |
245
|
|
|
* @param string $name Variable name |
246
|
|
|
* @param mixed $default Default value if the variable does not exist |
247
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
2 |
|
public static function getArray($name, $default = array(), $hash = 'default') |
252
|
|
|
{ |
253
|
2 |
|
return static::getVar($name, $default, $hash, 'array'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Fetches and returns raw text |
258
|
|
|
* |
259
|
|
|
* @param string $name Variable name |
260
|
|
|
* @param string $default Default value if the variable does not exist |
261
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
262
|
|
|
* |
263
|
|
|
* @return string Requested variable |
264
|
|
|
*/ |
265
|
2 |
|
public static function getText($name, $default = '', $hash = 'default') |
266
|
|
|
{ |
267
|
2 |
|
return (string) static::getVar($name, $default, $hash, 'string', static::MASK_ALLOW_RAW); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Fetches and returns a web url |
272
|
|
|
* |
273
|
|
|
* @param string $name Variable name |
274
|
|
|
* @param string $default Default value if the variable does not exist |
275
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
276
|
|
|
* |
277
|
|
|
* @return string Requested variable |
278
|
|
|
*/ |
279
|
1 |
|
public static function getUrl($name, $default = '', $hash = 'default') |
280
|
|
|
{ |
281
|
1 |
|
return (string) static::getVar($name, $default, $hash, 'weburl'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Fetches and returns a file (or web) path |
286
|
|
|
* |
287
|
|
|
* @param string $name Variable name |
288
|
|
|
* @param string $default Default value if the variable does not exist |
289
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
290
|
|
|
* |
291
|
|
|
* @return string Requested variable |
292
|
|
|
*/ |
293
|
1 |
|
public static function getPath($name, $default = '', $hash = 'default') |
294
|
|
|
{ |
295
|
1 |
|
return (string) static::getVar($name, $default, $hash, 'path'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Fetches and returns an email address |
300
|
|
|
* |
301
|
|
|
* @param string $name Variable name |
302
|
|
|
* @param string $default Default value if the variable does not exist |
303
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
304
|
|
|
* |
305
|
|
|
* @return string email address or default if invalid |
306
|
|
|
*/ |
307
|
1 |
|
public static function getEmail($name, $default = '', $hash = 'default') |
308
|
|
|
{ |
309
|
1 |
|
$ret = (string) static::getVar($name, $default, $hash, 'email'); |
310
|
1 |
|
return empty($ret) ? $default : $ret; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Fetches and returns an IP address |
315
|
|
|
* |
316
|
|
|
* @param string $name Variable name |
317
|
|
|
* @param string $default Default value if the variable does not exist |
318
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
319
|
|
|
* |
320
|
|
|
* @return string IP address or default if invalid |
321
|
|
|
*/ |
322
|
2 |
|
public static function getIP($name, $default = '', $hash = 'default') |
323
|
|
|
{ |
324
|
2 |
|
$ret = (string) static::getVar($name, $default, $hash, 'ip'); |
325
|
2 |
|
return empty($ret) ? $default : $ret; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Return a DateTime object from a Xoops\Form\DateSelect of Xoops\Form\DateTime field |
330
|
|
|
* |
331
|
|
|
* @param string $name Variable name |
332
|
|
|
* @param mixed $default Default value if the variable does not exist |
333
|
|
|
* @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
334
|
|
|
* |
335
|
|
|
* @return \DateTime object |
336
|
|
|
*/ |
337
|
1 |
|
public static function getDateTime($name, $default = null, $hash = 'default') |
338
|
|
|
{ |
339
|
1 |
|
$values = self::getVar($name, [], $hash, 'array'); |
340
|
1 |
|
$count = count($values); |
341
|
1 |
|
if ($count === 1) { |
342
|
1 |
|
$date = reset($values); |
343
|
1 |
|
$ret = Time::inputToDateTime($date); |
344
|
1 |
|
} elseif (isset($values['date']) && isset($values['time'])) { |
345
|
1 |
|
$ret = Time::inputToDateTime($values); |
346
|
|
|
} else { |
347
|
|
|
$ret = Time::cleanTime($default); |
348
|
|
|
} |
349
|
1 |
|
return $ret; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* get request header |
354
|
|
|
* |
355
|
|
|
* @param string $headerName name of header to retrieve, case insensitive |
356
|
|
|
* @param string|null $default default to return if named header is not found |
357
|
|
|
* |
358
|
|
|
* @return string header value or default if header was not found |
359
|
|
|
*/ |
360
|
|
|
public static function getHeader($headerName, $default = '') |
|
|
|
|
361
|
|
|
{ |
362
|
|
|
static $headers = null; |
363
|
|
|
|
364
|
|
|
if (null === $headers) { |
365
|
|
|
$headers = array(); |
366
|
|
|
if (function_exists('apache_request_headers')) { |
367
|
|
|
$rawHeaders = apache_request_headers(); |
368
|
|
|
foreach ($rawHeaders as $name => $value) { |
369
|
|
|
$headers[strtolower($name)] = $value; |
370
|
|
|
} |
371
|
|
|
} else { |
372
|
|
|
// From joyview - http://php.net/manual/en/function.getallheaders.php |
373
|
|
|
foreach ($_SERVER as $name => $value) { |
374
|
|
|
if (substr($name, 0, 5) === 'HTTP_') { |
375
|
|
|
$translatedName = str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5)))); |
376
|
|
|
$headers[$translatedName] = $value; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$name = strtolower($headerName); |
383
|
|
|
if (isset($headers[$name])) { |
384
|
|
|
return static::cleanVar($headers[$name]); |
385
|
|
|
} |
386
|
|
|
return $default; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Set a variable in one of the request variables |
391
|
|
|
* |
392
|
|
|
* @param string $name Name |
393
|
|
|
* @param string $value Value |
394
|
|
|
* @param string $hash Hash |
395
|
|
|
* @param boolean $overwrite Boolean |
396
|
|
|
* |
397
|
|
|
* @return string Previous value |
398
|
|
|
*/ |
399
|
2 |
|
public static function setVar($name, $value = null, $hash = 'method', $overwrite = true) |
|
|
|
|
400
|
|
|
{ |
401
|
2 |
|
$hash = strtoupper($hash); |
402
|
2 |
|
if ($hash === 'METHOD') { |
403
|
|
|
$hash = strtoupper($_SERVER['REQUEST_METHOD']); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
// Get the requested hash and determine existing value |
407
|
2 |
|
$original = static::get($hash, static::MASK_ALLOW_RAW); |
408
|
2 |
|
if (isset($original[$name])) { |
409
|
1 |
|
$previous = $original[$name]; |
410
|
|
|
// don't overwrite value unless asked |
411
|
1 |
|
if (!$overwrite) { |
412
|
1 |
|
return $previous; |
413
|
|
|
} |
414
|
|
|
} else { |
415
|
1 |
|
$previous = null; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
// set the value |
419
|
|
|
switch ($hash) { |
420
|
2 |
|
case 'GET': |
421
|
2 |
|
$_GET[$name] = $value; |
422
|
2 |
|
$_REQUEST[$name] = $value; |
423
|
2 |
|
break; |
424
|
|
|
case 'POST': |
425
|
|
|
$_POST[$name] = $value; |
426
|
|
|
$_REQUEST[$name] = $value; |
427
|
|
|
break; |
428
|
|
|
case 'REQUEST': |
429
|
|
|
$_REQUEST[$name] = $value; |
430
|
|
|
break; |
431
|
|
|
case 'COOKIE': |
432
|
|
|
$_COOKIE[$name] = $value; |
433
|
|
|
$_REQUEST[$name] = $value; |
434
|
|
|
break; |
435
|
|
|
case 'FILES': |
436
|
|
|
$_FILES[$name] = $value; |
437
|
|
|
break; |
438
|
|
|
case 'ENV': |
439
|
|
|
$_ENV['name'] = $value; |
440
|
|
|
break; |
441
|
|
|
case 'SERVER': |
442
|
|
|
$_SERVER['name'] = $value; |
443
|
|
|
break; |
444
|
|
|
} |
445
|
|
|
|
446
|
2 |
|
return $previous; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Fetches and returns a request array. |
451
|
|
|
* |
452
|
|
|
* The default behaviour is fetching variables depending on the |
453
|
|
|
* current request method: GET and HEAD will result in returning |
454
|
|
|
* $_GET, POST and PUT will result in returning $_POST. |
455
|
|
|
* |
456
|
|
|
* You can force the source by setting the $hash parameter: |
457
|
|
|
* |
458
|
|
|
* - post $_POST |
459
|
|
|
* - get $_GET |
460
|
|
|
* - files $_FILES |
461
|
|
|
* - cookie $_COOKIE |
462
|
|
|
* - env $_ENV |
463
|
|
|
* - server $_SERVER |
464
|
|
|
* - method via current $_SERVER['REQUEST_METHOD'] |
465
|
|
|
* - default $_REQUEST |
466
|
|
|
* |
467
|
|
|
* @param string $hash to get (POST, GET, FILES, METHOD) |
468
|
|
|
* @param int $mask Filter mask for the variable |
469
|
|
|
* |
470
|
|
|
* @return mixed Request hash |
471
|
|
|
*/ |
472
|
2 |
|
public static function get($hash = 'default', $mask = 0) |
|
|
|
|
473
|
|
|
{ |
474
|
2 |
|
$hash = strtoupper($hash); |
475
|
|
|
|
476
|
2 |
|
if ($hash === 'METHOD') { |
477
|
|
|
$hash = strtoupper($_SERVER['REQUEST_METHOD']); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
switch ($hash) { |
481
|
2 |
|
case 'GET': |
482
|
|
|
$input = $_GET; |
483
|
|
|
break; |
484
|
2 |
|
case 'POST': |
485
|
|
|
$input = $_POST; |
486
|
|
|
break; |
487
|
2 |
|
case 'FILES': |
488
|
|
|
$input = $_FILES; |
489
|
|
|
break; |
490
|
2 |
|
case 'COOKIE': |
491
|
|
|
$input = $_COOKIE; |
492
|
|
|
break; |
493
|
2 |
|
case 'ENV': |
494
|
|
|
$input = &$_ENV; |
495
|
|
|
break; |
496
|
2 |
|
case 'SERVER': |
497
|
|
|
$input = &$_SERVER; |
498
|
|
|
break; |
499
|
|
|
default: |
500
|
2 |
|
$input = $_REQUEST; |
501
|
2 |
|
break; |
502
|
|
|
} |
503
|
|
|
|
504
|
2 |
|
$result = static::cleanVars($input, $mask); |
505
|
|
|
|
506
|
2 |
|
return $result; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Sets a request variable |
511
|
|
|
* |
512
|
|
|
* @param array $array An associative array of key-value pairs |
513
|
|
|
* @param string $hash The request variable to set (POST, GET, FILES, METHOD) |
514
|
|
|
* @param boolean $overwrite If true and an existing key is found, the value is overwritten, |
515
|
|
|
* otherwise it is ignored |
516
|
|
|
* |
517
|
|
|
* @return void |
518
|
|
|
*/ |
519
|
2 |
|
public static function set($array, $hash = 'method', $overwrite = true) |
520
|
|
|
{ |
521
|
2 |
|
foreach ($array as $key => $value) { |
522
|
2 |
|
static::setVar($key, $value, $hash, $overwrite); |
523
|
|
|
} |
524
|
2 |
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Clean up an input variable. |
528
|
|
|
* |
529
|
|
|
* @param mixed $var The input variable. |
530
|
|
|
* @param int $mask Filter bit mask. |
531
|
|
|
* - 1=no trim: If this flag is cleared and the input is a string, |
532
|
|
|
* the string will have leading and trailing whitespace trimmed. |
533
|
|
|
* - 2=allow_raw: If set, no more filtering is performed, higher bits are ignored. |
534
|
|
|
* - 4=allow_html: HTML is allowed, but passed through a safe HTML filter first. |
535
|
|
|
* If set, no more filtering is performed. |
536
|
|
|
* - If no bits other than the 1 bit is set, a strict filter is applied. |
537
|
|
|
* @param string $type The variable type. See {@link FilterInput::clean()}. |
538
|
|
|
* |
539
|
|
|
* @return string |
540
|
|
|
*/ |
541
|
1 |
|
protected static function cleanVar($var, $mask = 0, $type = null) |
542
|
|
|
{ |
543
|
|
|
// Static input filters for specific settings |
544
|
1 |
|
static $noHtmlFilter = null; |
545
|
1 |
|
static $safeHtmlFilter = null; |
546
|
|
|
|
547
|
|
|
// convert $var in array if $type is ARRAY |
548
|
1 |
|
if (strtolower($type) === 'array' && !is_array($var)) { |
549
|
|
|
$var = array($var); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
// If the no trim flag is not set, trim the variable |
553
|
1 |
|
if (!($mask & static::MASK_NO_TRIM) && is_string($var)) { |
554
|
1 |
|
$var = trim($var); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
// Now we handle input filtering |
558
|
|
|
// If the allow raw flag is set, do not modify the variable |
559
|
1 |
|
if (!($mask & static::MASK_ALLOW_RAW)) { |
560
|
1 |
|
if ($mask & static::MASK_ALLOW_HTML) { |
561
|
|
|
// If the allow html flag is set, apply a safe html filter to the variable |
562
|
|
|
if (null === $safeHtmlFilter) { |
563
|
|
|
$safeHtmlFilter = FilterInput::getInstance(array(), array(), 1, 1); |
564
|
|
|
} |
565
|
|
|
$var = $safeHtmlFilter->clean($var, $type); |
566
|
|
|
} else { |
567
|
|
|
// Since no allow flags were set, we will apply the most strict filter to the variable |
568
|
1 |
|
if (null === $noHtmlFilter) { |
569
|
|
|
$noHtmlFilter = FilterInput::getInstance(); |
570
|
|
|
} |
571
|
1 |
|
$var = $noHtmlFilter->clean($var, $type); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
1 |
|
return $var; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Clean up an array of variables. |
580
|
|
|
* |
581
|
|
|
* @param mixed $var The input variable. |
582
|
|
|
* @param int $mask Filter bit mask. See {@link Request::cleanVar()} |
583
|
|
|
* @param string $type The variable type. See {@link FilterInput::clean()}. |
584
|
|
|
* |
585
|
|
|
* @return string |
586
|
|
|
*/ |
587
|
|
|
protected static function cleanVars($var, $mask = 0, $type = null) |
588
|
|
|
{ |
589
|
|
|
if (is_array($var)) { |
590
|
|
|
foreach ($var as $key => &$value) { |
591
|
|
|
$value = static::cleanVars($value, $mask, $type); |
592
|
|
|
} |
593
|
|
|
} else { |
594
|
|
|
$var = static::cleanVar($var, $mask, $type); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
return $var; |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
|
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: