1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
<?php
/**
* Encapsulates access to the $_REQUEST array, making sure used parameters are initialized and
* have the correct type.
*
* All function access the $_REQUEST array by default, if you want to access $_POST or $_GET
* explicitly use the $post and $get members.
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
class Input {
/** @var PostInput Access $_POST parameters */
public $post;
/** @var GetInput Access $_GET parameters */
public $get;
/** @var ServerInput Access $_SERVER parameters */
public $server;
protected $access;
/**
* @var Callable
*/
protected $filter;
/**
* Intilizes the Input class and it subcomponents
*/
function __construct() {
$this->access = &$_REQUEST;
$this->post = new PostInput();
$this->get = new GetInput();
$this->server = new ServerInput();
}
/**
* Apply the set filter to the given value
*
* @param string $data
* @return string
*/
protected function applyfilter($data){
if(!$this->filter) return $data;
return call_user_func($this->filter, $data);
}
/**
* Return a filtered copy of the input object
*
* Expects a callable that accepts one string parameter and returns a filtered string
*
* @param Callable|string $filter
* @return Input
*/
public function filter($filter='stripctl'){
$this->filter = $filter;
$clone = clone $this;
$this->filter = '';
return $clone;
}
/**
* Check if a parameter was set
*
* Basically a wrapper around isset. When called on the $post and $get subclasses,
* the parameter is set to $_POST or $_GET and to $_REQUEST
*
* @see isset
* @param string $name Parameter name
* @return bool
*/
public function has($name) {
return isset($this->access[$name]);
}
/**
* Remove a parameter from the superglobals
*
* Basically a wrapper around unset. When NOT called on the $post and $get subclasses,
* the parameter will also be removed from $_POST or $_GET
*
* @see isset
* @param string $name Parameter name
*/
public function remove($name) {
if(isset($this->access[$name])) {
unset($this->access[$name]);
}
// also remove from sub classes
if(isset($this->post) && isset($_POST[$name])) {
unset($_POST[$name]);
}
if(isset($this->get) && isset($_GET[$name])) {
unset($_GET[$name]);
}
}
/**
* Access a request parameter without any type conversion
*
* @param string $name Parameter name
* @param mixed $default Default to return if parameter isn't set
* @param bool $nonempty Return $default if parameter is set but empty()
* @return mixed
*/
public function param($name, $default = null, $nonempty = false) {
if(!isset($this->access[$name])) return $default;
$value = $this->applyfilter($this->access[$name]);
if($nonempty && empty($value)) return $default;
return $value;
}
/**
* Sets a parameter
*
* @param string $name Parameter name
* @param mixed $value Value to set
*/
public function set($name, $value) {
$this->access[$name] = $value;
}
/**
* Get a reference to a request parameter
*
* This avoids copying data in memory, when the parameter is not set it will be created
* and intialized with the given $default value before a reference is returned
*
* @param string $name Parameter name
* @param mixed $default If parameter is not set, initialize with this value
* @param bool $nonempty Init with $default if parameter is set but empty()
* @return mixed (reference)
*/
public function &ref($name, $default = '', $nonempty = false) {
if(!isset($this->access[$name]) || ($nonempty && empty($this->access[$name]))) {
$this->set($name, $default);
}
return $this->access[$name];
}
/**
* Access a request parameter as int
*
* @param string $name Parameter name
* @param int $default Default to return if parameter isn't set or is an array
* @param bool $nonempty Return $default if parameter is set but empty()
* @return int
*/
public function int($name, $default = 0, $nonempty = false) {
if(!isset($this->access[$name])) return $default;
if(is_array($this->access[$name])) return $default;
$value = $this->applyfilter($this->access[$name]);
if($value === '') return $default;
if($nonempty && empty($value)) return $default;
return (int) $value;
}
/**
* Access a request parameter as string
*
* @param string $name Parameter name
* @param string $default Default to return if parameter isn't set or is an array
* @param bool $nonempty Return $default if parameter is set but empty()
* @return string
*/
public function str($name, $default = '', $nonempty = false) {
if(!isset($this->access[$name])) return $default;
if(is_array($this->access[$name])) return $default;
$value = $this->applyfilter($this->access[$name]);
if($nonempty && empty($value)) return $default;
return (string) $value;
}
/**
* Access a request parameter and make sure it is has a valid value
*
* Please note that comparisons to the valid values are not done typesafe (request vars
* are always strings) however the function will return the correct type from the $valids
* array when an match was found.
*
* @param string $name Parameter name
* @param array $valids Array of valid values
* @param mixed $default Default to return if parameter isn't set or not valid
* @return null|mixed
*/
public function valid($name, $valids, $default = null) {
if(!isset($this->access[$name])) return $default;
if(is_array($this->access[$name])) return $default; // we don't allow arrays
$value = $this->applyfilter($this->access[$name]);
$found = array_search($value, $valids);
if($found !== false) return $valids[$found]; // return the valid value for type safety
return $default;
}
/**
* Access a request parameter as bool
*
* Note: $nonempty is here for interface consistency and makes not much sense for booleans
*
* @param string $name Parameter name
* @param mixed $default Default to return if parameter isn't set
* @param bool $nonempty Return $default if parameter is set but empty()
* @return bool
*/
public function bool($name, $default = false, $nonempty = false) {
if(!isset($this->access[$name])) return $default;
if(is_array($this->access[$name])) return $default;
$value = $this->applyfilter($this->access[$name]);
if($value === '') return $default;
if($nonempty && empty($value)) return $default;
return (bool) $value;
}
/**
* Access a request parameter as array
*
* @param string $name Parameter name
* @param mixed $default Default to return if parameter isn't set
* @param bool $nonempty Return $default if parameter is set but empty()
* @return array
*/
public function arr($name, $default = array(), $nonempty = false) {
if(!isset($this->access[$name])) return $default;
if(!is_array($this->access[$name])) return $default;
if($nonempty && empty($this->access[$name])) return $default;
return (array) $this->access[$name];
}
/**
* Create a simple key from an array key
*
* This is useful to access keys where the information is given as an array key or as a single array value.
* For example when the information was submitted as the name of a submit button.
*
* This function directly changes the access array.
*
* Eg. $_REQUEST['do']['save']='Speichern' becomes $_REQUEST['do'] = 'save'
*
* This function returns the $INPUT object itself for easy chaining
*
* @param string $name
* @return Input
*/
public function extract($name){
if(!isset($this->access[$name])) return $this;
if(!is_array($this->access[$name])) return $this;
$keys = array_keys($this->access[$name]);
if(!$keys){
// this was an empty array
$this->remove($name);
return $this;
}
// get the first key
$value = array_shift($keys);
if($value === 0){
// we had a numeric array, assume the value is not in the key
$value = array_shift($this->access[$name]);
}
$this->set($name, $value);
return $this;
}
}
/**
* Internal class used for $_POST access in Input class
*/
class PostInput extends Input {
protected $access;
/**
* Initialize the $access array, remove subclass members
*/
function __construct() {
$this->access = &$_POST;
}
/**
* Sets a parameter in $_POST and $_REQUEST
*
* @param string $name Parameter name
* @param mixed $value Value to set
*/
public function set($name, $value) {
parent::set($name, $value);
$_REQUEST[$name] = $value;
}
}
/**
* Internal class used for $_GET access in Input class
*/
class GetInput extends Input {
protected $access;
/**
* Initialize the $access array, remove subclass members
*/
function __construct() {
$this->access = &$_GET;
}
/**
* Sets a parameter in $_GET and $_REQUEST
*
* @param string $name Parameter name
* @param mixed $value Value to set
*/
public function set($name, $value) {
parent::set($name, $value);
$_REQUEST[$name] = $value;
}
}
/**
* Internal class used for $_SERVER access in Input class
*/
class ServerInput extends Input {
protected $access;
/**
* Initialize the $access array, remove subclass members
*/
function __construct() {
$this->access = &$_SERVER;
}
}
|