|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file RequestData.php |
|
34
|
|
|
* |
|
35
|
|
|
* This class contains the methods to query request data |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\Http |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link http://www.iacademy.cf |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\Http; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Http\ServerRequestInterface; |
|
51
|
|
|
use Platine\Stdlib\Helper\Arr; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @class RequestData |
|
55
|
|
|
* @package Platine\Framework\Http |
|
56
|
|
|
*/ |
|
57
|
|
|
class RequestData |
|
58
|
|
|
{ |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The request body or post data |
|
62
|
|
|
* @var array<string, mixed> |
|
63
|
|
|
*/ |
|
64
|
|
|
protected array $posts = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The request get data |
|
68
|
|
|
* @var array<string, mixed> |
|
69
|
|
|
*/ |
|
70
|
|
|
protected array $gets = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The request servers environment data |
|
74
|
|
|
* @var array<string, mixed> |
|
75
|
|
|
*/ |
|
76
|
|
|
protected array $servers = []; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* The request cookies data |
|
80
|
|
|
* @var array<string, mixed> |
|
81
|
|
|
*/ |
|
82
|
|
|
protected array $cookies = []; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The request files data |
|
86
|
|
|
* @var array<string, mixed> |
|
87
|
|
|
*/ |
|
88
|
|
|
protected array $files = []; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Whether to apply input cleanup |
|
92
|
|
|
* @var bool |
|
93
|
|
|
*/ |
|
94
|
|
|
protected bool $autoEscape = true; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Create new instance |
|
98
|
|
|
* @param ServerRequestInterface $request |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __construct(ServerRequestInterface $request) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->posts = (array) $request->getParsedBody(); |
|
103
|
|
|
$this->gets = $request->getQueryParams(); |
|
104
|
|
|
$this->servers = $request->getServerParams(); |
|
105
|
|
|
$this->cookies = $request->getCookieParams(); |
|
106
|
|
|
$this->files = $request->getUploadedFiles(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set clean input status |
|
111
|
|
|
* @param bool $autoEscape |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setAutoEscape(bool $autoEscape = true): self |
|
115
|
|
|
{ |
|
116
|
|
|
$this->autoEscape = $autoEscape; |
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Return the post data |
|
123
|
|
|
* @return array<string, mixed> |
|
124
|
|
|
*/ |
|
125
|
|
|
public function posts(): array |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->applyInputClean($this->posts); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Return the get data |
|
132
|
|
|
* @return array<string, mixed> |
|
133
|
|
|
*/ |
|
134
|
|
|
public function gets(): array |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->applyInputClean($this->gets); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Return the files data |
|
141
|
|
|
* @return array<string, mixed> |
|
142
|
|
|
*/ |
|
143
|
|
|
public function files(): array |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->files; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Return the server data |
|
150
|
|
|
* @return array<string, mixed> |
|
151
|
|
|
*/ |
|
152
|
|
|
public function servers(): array |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->applyInputClean($this->servers); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Return the cookie data |
|
159
|
|
|
* @return array<string, mixed> |
|
160
|
|
|
*/ |
|
161
|
|
|
public function cookies(): array |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->applyInputClean($this->cookies); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Return the request query value for the given key |
|
168
|
|
|
* @param string $key the key to fetch also support for dot notation |
|
169
|
|
|
* @param mixed $default |
|
170
|
|
|
* |
|
171
|
|
|
* @return mixed |
|
172
|
|
|
*/ |
|
173
|
|
|
public function get(string $key, $default = null) |
|
174
|
|
|
{ |
|
175
|
|
|
$gets = $this->applyInputClean($this->gets); |
|
176
|
|
|
return Arr::get($gets, $key, $default); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Return the request body or post value for the given key |
|
181
|
|
|
* @param string $key the key to fetch also support for dot notation |
|
182
|
|
|
* @param mixed $default |
|
183
|
|
|
* |
|
184
|
|
|
* @return mixed |
|
185
|
|
|
*/ |
|
186
|
|
|
public function post(string $key, $default = null) |
|
187
|
|
|
{ |
|
188
|
|
|
$posts = $this->applyInputClean($this->posts); |
|
189
|
|
|
return Arr::get($posts, $key, $default); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Return the request server value for the given key |
|
194
|
|
|
* @param string $key the key to fetch also support for dot notation |
|
195
|
|
|
* @param mixed $default |
|
196
|
|
|
* |
|
197
|
|
|
* @return mixed |
|
198
|
|
|
*/ |
|
199
|
|
|
public function server(string $key, $default = null) |
|
200
|
|
|
{ |
|
201
|
|
|
$servers = $this->applyInputClean($this->servers); |
|
202
|
|
|
return Arr::get($servers, $key, $default); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Return the request cookie value for the given key |
|
207
|
|
|
* @param string $key the key to fetch also support for dot notation |
|
208
|
|
|
* @param mixed $default |
|
209
|
|
|
* |
|
210
|
|
|
* @return mixed |
|
211
|
|
|
*/ |
|
212
|
|
|
public function cookie(string $key, $default = null) |
|
213
|
|
|
{ |
|
214
|
|
|
$cookies = $this->applyInputClean($this->cookies); |
|
215
|
|
|
return Arr::get($cookies, $key, $default); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Return the request uploaded file for the given key |
|
220
|
|
|
* @param string $key the key to fetch also support for dot notation |
|
221
|
|
|
* |
|
222
|
|
|
* @return mixed |
|
223
|
|
|
*/ |
|
224
|
|
|
public function file(string $key) |
|
225
|
|
|
{ |
|
226
|
|
|
$files = $this->files; |
|
227
|
|
|
return Arr::get($files, $key, null); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Clean the user input, like to prevent XSS, etc. |
|
232
|
|
|
* @param mixed $str |
|
233
|
|
|
* @return mixed |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function cleanInput($str) |
|
236
|
|
|
{ |
|
237
|
|
|
if (is_array($str)) { |
|
238
|
|
|
return array_map([$this, 'cleanInput'], $str); |
|
239
|
|
|
} |
|
240
|
|
|
if (is_object($str)) { |
|
241
|
|
|
$obj = $str; |
|
242
|
|
|
foreach ($str as $var => $value) { |
|
243
|
|
|
$obj->{$var} = $this->cleanInput($value); |
|
244
|
|
|
} |
|
245
|
|
|
return $obj; |
|
246
|
|
|
} |
|
247
|
|
|
return htmlspecialchars(strip_tags((string) $str), ENT_COMPAT, 'UTF-8'); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Apply the input cleanup based on the status |
|
252
|
|
|
* @param array<mixed> $data |
|
253
|
|
|
* @return array<mixed> |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function applyInputClean(array $data): array |
|
256
|
|
|
{ |
|
257
|
|
|
if ($this->autoEscape) { |
|
258
|
|
|
$data = $this->cleanInput($data); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $data; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|