1
|
|
|
<?php |
2
|
|
|
namespace Zewa; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Handles everything relating to request variables/globals/properties |
6
|
|
|
* |
7
|
|
|
* @author Zechariah Walden<zech @ zewadesign.com> |
8
|
|
|
*/ |
9
|
|
|
class Request |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Reference to instantiated controller object. |
13
|
|
|
* |
14
|
|
|
* @var object |
15
|
|
|
*/ |
16
|
|
|
protected static $instance; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* System configuration |
20
|
|
|
* |
21
|
|
|
* @var object |
22
|
|
|
*/ |
23
|
|
|
private $configuration; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* normalized $_GET superglobal |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
* @access private |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
private $getContainer = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* normalized $_POST superglobal |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
* @access private |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
private $postContainer = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* normalized $_DELETE superglobal |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
* @access private |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
private $deleteContainer = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* normalized $_PUT superglobal |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
* @access private |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
private $putContainer = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* normalized $_SESSION superglobal |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
* @access private |
67
|
|
|
*/ |
68
|
|
|
|
69
|
|
|
private $sessionContainer = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* normalized $_COOKIE superglobal |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
* @access private |
76
|
|
|
*/ |
77
|
|
|
|
78
|
|
|
private $cookieContainer = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* normalized $_FILES superglobal |
82
|
|
|
* |
83
|
|
|
* @var array |
84
|
|
|
* @access private |
85
|
|
|
*/ |
86
|
|
|
|
87
|
|
|
private $filesContainer = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* normalized $_SERVER superglobal |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
* @access private |
94
|
|
|
*/ |
95
|
|
|
|
96
|
|
|
private $serverContainer = []; |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Flashdata container |
101
|
|
|
* |
102
|
|
|
* @var array |
103
|
|
|
* @access private |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
private $flashdata = []; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Flashdata identifier |
110
|
|
|
* |
111
|
|
|
* @var string |
112
|
|
|
* @access private |
113
|
|
|
* @TODO: move flashdata to sessionhandler, make available here with other request vars still |
114
|
|
|
*/ |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
private $flashdataId = '_z_session_flashdata'; |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Normalizes superglobals, handles flashdata |
122
|
|
|
*/ |
123
|
|
|
|
124
|
18 |
|
public function __construct() |
|
|
|
|
125
|
|
|
{ |
126
|
18 |
|
self::$instance = $this; |
127
|
18 |
|
$app = App::getInstance(); |
128
|
18 |
|
$this->configuration = $app->getConfiguration(); |
129
|
|
|
|
130
|
18 |
|
if( !empty($this->configuration->session) ) { |
131
|
|
|
if ($this->configuration->session !== false && $this->configuration->session->flashdataId) { |
132
|
|
|
$this->flashdataId = $this->configuration->session->flashdataId; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// $config = \HTMLPurifier_Config::createDefault(); |
|
|
|
|
137
|
|
|
// $this->purifier = new \HTMLPurifier($config); |
|
|
|
|
138
|
|
|
|
139
|
18 |
|
if (!empty($_SESSION)) { |
140
|
|
|
$this->sessionContainer = $this->normalize($_SESSION); |
141
|
|
|
} |
142
|
18 |
|
$this->registerFlashdata(); |
143
|
|
|
|
144
|
18 |
|
$this->getContainer = $this->normalize($_GET); |
145
|
18 |
|
$this->postContainer = $this->normalize($_POST); |
146
|
18 |
|
$this->cookieContainer = $this->normalize($_COOKIE); |
147
|
18 |
|
$this->filesContainer = $this->normalize($_FILES); |
148
|
18 |
|
$this->serverContainer = $this->normalize($_SERVER); |
149
|
18 |
|
if ($this->serverContainer['REQUEST_METHOD'] === 'PUT') { |
150
|
|
|
parse_str(file_get_contents('php://input', "r"), $PUT); |
151
|
|
|
$this->putContainer = $this->normalize($PUT); |
152
|
18 |
|
} elseif ($this->serverContainer['REQUEST_METHOD'] === 'DELETE') { |
153
|
|
|
parse_str(file_get_contents('php://input', "r"), $DELETE); |
154
|
|
|
$this->deleteContainer = $this->normalize($DELETE); |
155
|
|
|
} |
156
|
18 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Processes current requests flashdata, recycles old. |
160
|
|
|
* |
161
|
|
|
* @access private |
162
|
|
|
*/ |
163
|
18 |
|
private function registerFlashdata() |
|
|
|
|
164
|
|
|
{ |
165
|
18 |
|
if (!empty($this->sessionContainer[$this->flashdataId])) { |
166
|
|
|
$this->flashdata = unserialize(base64_decode($this->session($this->flashdataId))); |
|
|
|
|
167
|
|
|
// and destroy the temporary session variable |
168
|
|
|
unset($_SESSION[$this->flashdataId]); |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
if (!empty($this->flashdata)) { |
172
|
|
|
// iterate through all the entries |
173
|
|
|
foreach ($this->flashdata as $variable => $data) { |
174
|
|
|
// increment counter representing server requests |
175
|
|
|
$this->flashdata[$variable]['inc'] ++; |
176
|
|
|
|
177
|
|
|
// if we're past the first server request |
178
|
|
|
if ($this->flashdata[$variable]['inc'] > 1) { |
179
|
|
|
// unset the session variable |
180
|
|
|
unset($_SESSION[$variable]); |
181
|
|
|
|
182
|
|
|
// stop tracking |
183
|
|
|
unset($this->flashdata[$variable]); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// if there is any flashdata left to be handled |
188
|
|
|
if (!empty($this->flashdata)) { |
189
|
|
|
// store data in a temporary session variable |
190
|
|
|
$_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata)); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
18 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Sets flashdata |
198
|
|
|
* |
199
|
|
|
* @access public |
200
|
|
|
* |
201
|
|
|
* @params string $name |
202
|
|
|
* @params mixed $value |
203
|
|
|
*/ |
204
|
|
|
|
205
|
|
|
public function setFlashdata($name, $value) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
|
208
|
|
|
// set session variable |
209
|
|
|
$this->sessionContainer[$name] = $value; |
210
|
|
|
|
211
|
|
|
// initialize the counter for this flashdata |
212
|
|
|
$this->flashdata[$name] = [ |
213
|
|
|
'value' => $value, |
214
|
|
|
'inc' => 0 |
215
|
|
|
]; |
216
|
|
|
|
217
|
|
|
$_SESSION[$this->flashdataId] = base64_encode(serialize($this->flashdata)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Gets flashdata |
222
|
|
|
* |
223
|
|
|
* @access public |
224
|
|
|
* |
225
|
|
|
* @params string $name |
226
|
|
|
*/ |
227
|
|
|
|
228
|
|
|
public function getFlashdata($name = false, $default = false) |
229
|
|
|
{ |
230
|
|
|
if ($name === false && !empty($this->flashdata)) { |
231
|
|
|
return $this->flashdata; |
232
|
|
|
} |
233
|
|
|
if ($name !== false) { |
234
|
|
|
if (!empty($this->flashdata[$name]['value'])) { |
235
|
|
|
return $this->flashdata[$name]['value']; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $default; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Remove session data |
244
|
|
|
* |
245
|
|
|
* @access public |
246
|
|
|
* |
247
|
|
|
* @params string $index |
248
|
|
|
*/ |
249
|
|
|
public function removeSession($index) |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
|
252
|
|
|
unset($this->sessionContainer[$index]); |
253
|
|
|
unset($_SESSION[$index]); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set session data |
258
|
|
|
* |
259
|
|
|
* @access public |
260
|
|
|
* |
261
|
|
|
* @params string $index |
262
|
|
|
* @params mixed $value |
263
|
|
|
*/ |
264
|
|
|
|
265
|
|
|
public function setSession($index = false, $value = false) |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
if ((!is_array($index) && isset($value)) |
268
|
|
|
&& (!is_object($index) && isset($value)) |
269
|
|
|
) { |
270
|
|
|
$index = [$index => $value]; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if (!is_array($index) && !is_object($index)) { |
274
|
|
|
throw new Exception\TypeException("Invalid session value"); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
foreach ($index as $k => $v) { |
278
|
|
|
$_SESSION[$k] = $v; |
279
|
|
|
$this->sessionContainer = $this->normalize($_SESSION); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Dumps all session data |
285
|
|
|
* |
286
|
|
|
* @access public |
287
|
|
|
*/ |
288
|
|
|
public function destroySession() |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
|
291
|
|
|
$_SESSION = []; |
292
|
|
|
|
293
|
|
|
if (ini_get("session.use_cookies")) { |
294
|
|
|
$params = session_get_cookie_params(); |
295
|
|
|
setcookie( |
296
|
|
|
session_name(), |
297
|
|
|
'', |
298
|
|
|
time() - 42000, |
299
|
|
|
$params["path"], |
300
|
|
|
$params["domain"], |
301
|
|
|
$params["secure"], |
302
|
|
|
$params["httponly"] |
303
|
|
|
); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
session_destroy(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Normalizes data |
311
|
|
|
* |
312
|
|
|
* @access private |
313
|
|
|
* @TODO: expand functionality, set/perform based on configuration |
314
|
|
|
*/ |
315
|
18 |
|
private function normalize($data) |
316
|
|
|
{ |
317
|
18 |
|
if (is_array($data)) { |
318
|
18 |
|
foreach ($data as $key => $value) { |
319
|
18 |
|
unset($data[$key]); |
320
|
18 |
|
$data[$this->normalize($key)] = $this->normalize($value); |
321
|
|
|
} |
322
|
18 |
|
} elseif (is_object($data)) { |
323
|
|
|
$new = new \stdClass(); |
324
|
|
|
foreach ($data as $k => $v) { |
325
|
|
|
// unset($data->{$k}); |
|
|
|
|
326
|
|
|
$key = $this->normalize($k); |
327
|
|
|
$new->{$key} = $this->normalize($v); |
328
|
|
|
} |
329
|
|
|
$data = $new; |
330
|
|
|
} else { |
331
|
18 |
|
$data = trim($data); |
332
|
|
|
// if (function_exists('iconv') && function_exists('mb_detect_encoding')) { |
|
|
|
|
333
|
|
|
// $current_encoding = mb_detect_encoding($data); |
|
|
|
|
334
|
|
|
// |
335
|
|
|
// if ($current_encoding != 'UTF-8' && $current_encoding != 'UTF-16') { |
|
|
|
|
336
|
|
|
// $data = iconv($current_encoding, 'UTF-8', $data); |
|
|
|
|
337
|
|
|
// } |
338
|
|
|
// } |
339
|
|
|
//Global XXS? |
340
|
|
|
//we need to review this. |
341
|
18 |
|
if (function_exists('iconv') && function_exists('mb_detect_encoding')) { |
342
|
18 |
|
$current_encoding = mb_detect_encoding($data); |
343
|
|
|
|
344
|
18 |
|
if ($current_encoding != 'UTF-8' && $current_encoding != 'UTF-16') { |
345
|
18 |
|
$data = iconv($current_encoding, 'UTF-8', $data); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
// Global XXS? |
349
|
|
|
// This is not sanitary. FILTER_SANITIZE_STRING doesn't do much. |
350
|
|
|
|
351
|
|
|
// $data = filter_var($data, FILTER_SANITIZE_STRING); |
|
|
|
|
352
|
|
|
|
353
|
18 |
|
if (is_numeric($data)) { |
354
|
18 |
|
if ((intval($data) === (int)trim($data, '-')) && strlen((string)(int)$data) === strlen($data)) { |
355
|
18 |
|
$data = (int) $data; |
356
|
18 |
|
} elseif ($data === (string)(float)$data) { |
357
|
18 |
|
$data = (float) $data; |
358
|
|
|
} |
359
|
|
|
} else { |
|
|
|
|
360
|
|
|
// $data = $this->purifier->purify($data); |
|
|
|
|
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
18 |
|
return $data; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function __call($name, $arguments) |
368
|
|
|
{ |
369
|
|
|
$accepted = ['post', 'put', 'delete', 'get', 'server', 'session']; |
370
|
|
|
|
371
|
|
|
if (in_array($name, $accepted)) { |
372
|
|
|
$container = $name . 'Container'; |
373
|
|
|
$container = $this->$container; |
374
|
|
|
|
375
|
|
|
$argument = ! empty($arguments[0]) ? $arguments[0] : false; |
376
|
|
|
|
377
|
|
|
if ($argument === false && !empty($container)) { |
378
|
|
|
return $container; |
379
|
|
|
} |
380
|
|
|
if (! empty($container[$argument])) { |
381
|
|
|
if (!is_array($container[$argument]) |
382
|
|
|
&& !is_object($container[$argument]) |
383
|
|
|
&& strlen($container[$argument]) > 0 |
384
|
|
|
|| is_array($container[$argument]) |
385
|
|
|
|| is_object($container[$argument]) |
386
|
|
|
) { |
387
|
|
|
return $container[$argument]; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return ! empty($arguments[1]) ? $arguments[1] : false; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
throw new Exception\FunctionException('Method ' . $name . ' does not exist.'); |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
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: