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