|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSFS\base; |
|
4
|
|
|
|
|
5
|
|
|
use PSFS\base\types\helpers\ServerHelper; |
|
6
|
|
|
use PSFS\base\types\traits\Helper\ServerTrait; |
|
7
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Request |
|
11
|
|
|
* @package PSFS |
|
12
|
|
|
*/ |
|
13
|
|
|
class Request |
|
14
|
|
|
{ |
|
15
|
|
|
use SingletonTrait; |
|
16
|
|
|
use ServerTrait; |
|
17
|
|
|
|
|
18
|
|
|
const VERB_GET = 'GET'; |
|
19
|
|
|
const VERB_POST = 'POST'; |
|
20
|
|
|
const VERB_PUT = 'PUT'; |
|
21
|
|
|
const VERB_DELETE = 'DELETE'; |
|
22
|
|
|
const VERB_OPTIONS = 'OPTIONS'; |
|
23
|
|
|
const VERB_HEAD = 'HEAD'; |
|
24
|
|
|
const VERB_PATCH = 'PATCH'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected array $cookies; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected array $upload; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected array $header; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected array $data; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected array $raw = []; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected array $query; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
private bool $isLoaded = false; |
|
54
|
|
|
|
|
55
|
2 |
|
public function init() |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->setServer(ServerHelper::getServerData()); |
|
58
|
2 |
|
$this->cookies = is_array($_COOKIE) ? $_COOKIE : []; |
|
59
|
2 |
|
$this->upload = is_array($_FILES) ? $_FILES : []; |
|
60
|
2 |
|
$this->header = $this->parseHeaders(); |
|
61
|
2 |
|
$this->data = is_array($_REQUEST) ? $_REQUEST : []; |
|
62
|
2 |
|
$this->query = is_array($_GET) ? $_GET : []; |
|
63
|
2 |
|
$this->raw = json_decode(file_get_contents('php://input'), true) ?: []; |
|
64
|
2 |
|
$this->isLoaded = true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Método que devuelve las cabeceras de la petición |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
2 |
|
private function parseHeaders(): array |
|
72
|
|
|
{ |
|
73
|
2 |
|
return getallheaders() ?: []; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Método que verifica si existe una cabecera concreta |
|
78
|
|
|
* @param $header |
|
79
|
|
|
* |
|
80
|
|
|
* @return boolean |
|
81
|
|
|
*/ |
|
82
|
8 |
|
public function hasHeader($header): bool |
|
83
|
|
|
{ |
|
84
|
8 |
|
return array_key_exists($header, $this->header); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Método que indica si una petición tiene cookies |
|
90
|
|
|
* @return boolean |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function hasCookies(): bool |
|
93
|
|
|
{ |
|
94
|
1 |
|
return (null !== $this->cookies && 0 !== count($this->cookies)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Método que indica si una petición tiene cookies |
|
99
|
|
|
* @return boolean |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function hasUpload(): bool |
|
102
|
|
|
{ |
|
103
|
1 |
|
return (null !== $this->upload && 0 !== count($this->upload)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Método que devuelve el TimeStamp de la petición |
|
108
|
|
|
* |
|
109
|
|
|
* @param boolean $formatted |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function getTimestamp(?bool $formatted = false) |
|
114
|
|
|
{ |
|
115
|
|
|
return self::getInstance()->getTs($formatted); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Método que devuelve una cabecera de la petición si existe |
|
120
|
|
|
* @param string $name |
|
121
|
|
|
* @param string|null $default |
|
122
|
|
|
* |
|
123
|
|
|
* @return string|null |
|
124
|
|
|
*/ |
|
125
|
6 |
|
public static function header(string $name, ?string $default = null): ?string |
|
126
|
|
|
{ |
|
127
|
6 |
|
return self::getInstance()->getHeader($name, $default); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $name |
|
132
|
|
|
* @param string|null $default |
|
133
|
|
|
* @return string|null |
|
134
|
|
|
*/ |
|
135
|
7 |
|
public function getHeader(string $name, ?string $default = null): ?string |
|
136
|
|
|
{ |
|
137
|
7 |
|
$header = null; |
|
138
|
7 |
|
if ($this->hasHeader($name)) { |
|
139
|
|
|
$header = $this->header[$name]; |
|
140
|
7 |
|
} else if (array_key_exists('h_' . strtolower($name), $this->query)) { |
|
141
|
|
|
$header = $this->query['h_' . strtolower($name)]; |
|
142
|
7 |
|
} else if (array_key_exists('HTTP_' . strtoupper(str_replace('-', '_', $name)), $this->server)) { |
|
143
|
|
|
$header = $this->getServer('HTTP_' . strtoupper(str_replace('-', '_', $name))); |
|
144
|
|
|
} |
|
145
|
7 |
|
return $header ?: $default; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $language |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
|
|
public static function setLanguageHeader(string $language): void |
|
153
|
|
|
{ |
|
154
|
|
|
self::getInstance()->header['X-API-LANG'] = $language; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Método que devuelve la url solicitada |
|
159
|
|
|
* @return string|null |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public static function requestUri(): ?string |
|
162
|
|
|
{ |
|
163
|
1 |
|
return self::getInstance()->getRequestUri(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Método que determina si se ha solicitado un fichero |
|
168
|
|
|
* @return boolean |
|
169
|
|
|
*/ |
|
170
|
5 |
|
public function isFile(): bool |
|
171
|
|
|
{ |
|
172
|
5 |
|
return preg_match('/\.[a-z0-9]{2,4}$/', $this->getRequestUri()) !== 0; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get query params |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $queryParams |
|
179
|
|
|
* |
|
180
|
|
|
* @return mixed |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getQuery(string $queryParams): mixed |
|
183
|
|
|
{ |
|
184
|
|
|
return array_key_exists($queryParams, $this->query) ? $this->query[$queryParams] : null; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get all query params |
|
189
|
|
|
* |
|
190
|
|
|
* @return mixed |
|
191
|
|
|
*/ |
|
192
|
6 |
|
public function getQueryParams(): mixed |
|
193
|
|
|
{ |
|
194
|
6 |
|
return $this->query; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Método que devuelve un parámetro de la solicitud |
|
199
|
|
|
* @param string $param |
|
200
|
|
|
* |
|
201
|
|
|
* @return string|null |
|
202
|
|
|
*/ |
|
203
|
|
|
public function get(string $param): ?string |
|
204
|
|
|
{ |
|
205
|
|
|
return array_key_exists($param, $this->data) ? $this->data[$param] : null; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Método que devuelve todos los datos del Request |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
7 |
|
public function getData(): array |
|
213
|
|
|
{ |
|
214
|
7 |
|
return array_merge($this->data, $this->raw); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return array |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getRawData(): array |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->raw; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Método que devuelve el valor de una cookie en caso de que exista |
|
227
|
|
|
* @param string $name |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
2 |
|
public function getCookie(string $name): ?string |
|
232
|
|
|
{ |
|
233
|
2 |
|
return array_key_exists($name, $this->cookies) ? $this->cookies[$name] : null; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Método que devuelve los files subidos por POST |
|
238
|
|
|
* @param string $name |
|
239
|
|
|
* |
|
240
|
|
|
* @return array |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getFile(string $name): array |
|
243
|
|
|
{ |
|
244
|
|
|
return array_key_exists($name, $this->upload) ? $this->upload[$name] : []; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Método que realiza una redirección a la url dada |
|
249
|
|
|
* @param string|null $url |
|
250
|
|
|
*/ |
|
251
|
|
|
public function redirect(?string $url = null) |
|
252
|
|
|
{ |
|
253
|
|
|
if (null === $url) { |
|
254
|
|
|
$url = $this->getServer('HTTP_ORIGIN'); |
|
255
|
|
|
} |
|
256
|
|
|
ob_start(); |
|
257
|
|
|
header('Location: ' . $url); |
|
258
|
|
|
ob_end_clean(); |
|
259
|
|
|
Security::getInstance()->updateSession(); |
|
260
|
|
|
exit(t('Redirect...')); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Devuelve la url completa de base |
|
265
|
|
|
* @param boolean $hasProtocol |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
3 |
|
public function getRootUrl(?bool $hasProtocol = true): string |
|
269
|
|
|
{ |
|
270
|
3 |
|
$url = $this->getServerName(); |
|
271
|
3 |
|
$protocol = $hasProtocol ? $this->getProtocol() : '//'; |
|
272
|
3 |
|
if (!empty($protocol)) { |
|
273
|
3 |
|
$url = $protocol . $url; |
|
274
|
|
|
} |
|
275
|
3 |
|
return $this->checkServerPort($url); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @param string $url |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
3 |
|
protected function checkServerPort(string $url): string |
|
283
|
|
|
{ |
|
284
|
3 |
|
$port = (integer)$this->getServer('SERVER_PORT'); |
|
285
|
3 |
|
$host = $this->getServer('HTTP_HOST'); |
|
286
|
3 |
|
if (!empty($host)) { |
|
287
|
|
|
$parts = explode(':', $host); |
|
288
|
|
|
$hostPort = (integer)end($parts); |
|
289
|
|
|
if ($hostPort !== $port && count($parts) > 1) { |
|
290
|
|
|
$port = $hostPort; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
3 |
|
if (!in_array($port, [80, 443], true)) { |
|
294
|
3 |
|
$url .= ':' . $port; |
|
295
|
|
|
} |
|
296
|
3 |
|
return $url; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
} |
|
300
|
|
|
|