|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSFS\base; |
|
3
|
|
|
|
|
4
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class Request |
|
8
|
|
|
* @package PSFS |
|
9
|
|
|
*/ |
|
10
|
|
|
class Request |
|
11
|
|
|
{ |
|
12
|
|
|
use SingletonTrait; |
|
13
|
|
|
|
|
14
|
|
|
const VERB_GET = 'GET'; |
|
15
|
|
|
const VERB_POST = 'POST'; |
|
16
|
|
|
const VERB_PUT = 'PUT'; |
|
17
|
|
|
const VERB_DELETE = 'DELETE'; |
|
18
|
|
|
const VERB_OPTIONS = 'OPTIONS'; |
|
19
|
|
|
const VERB_HEAD = 'HEAD'; |
|
20
|
|
|
const VERB_PATCH = 'PATCH'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $server; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $cookies; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $upload; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $header; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $data; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $raw = []; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $query; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
private $isLoaded = false; |
|
54
|
|
|
|
|
55
|
2 |
|
public function init() |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->server = $_SERVER or []; |
|
58
|
2 |
|
$this->cookies = $_COOKIE or []; |
|
59
|
2 |
|
$this->upload = $_FILES or []; |
|
60
|
2 |
|
$this->header = $this->parseHeaders(); |
|
61
|
2 |
|
$this->data = $_REQUEST or []; |
|
62
|
2 |
|
$this->query = $_GET or []; |
|
63
|
2 |
|
$this->raw = json_decode(file_get_contents('php://input'), true) ?: []; |
|
64
|
2 |
|
$this->isLoaded = true; |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Método que devuelve las cabeceras de la petición |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
2 |
|
private function parseHeaders() |
|
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
|
10 |
|
public function hasHeader($header) |
|
83
|
|
|
{ |
|
84
|
10 |
|
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() |
|
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() |
|
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($formatted = false) |
|
114
|
|
|
{ |
|
115
|
|
|
return self::getTs($formatted); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
public function getTs($formatted = false) |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $formatted ? date('Y-m-d H:i:s', $this->server['REQUEST_TIME_FLOAT']) : $this->server['REQUEST_TIME_FLOAT']; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Método que devuelve el Método HTTP utilizado |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
5 |
|
public function getMethod() |
|
128
|
|
|
{ |
|
129
|
5 |
|
return array_key_exists('REQUEST_METHOD', $this->server) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Método que devuelve una cabecera de la petición si existe |
|
134
|
|
|
* @param string $name |
|
135
|
|
|
* @param string $default |
|
136
|
|
|
* |
|
137
|
|
|
* @return string|null |
|
138
|
|
|
*/ |
|
139
|
9 |
|
public static function header($name, $default = null) |
|
140
|
|
|
{ |
|
141
|
9 |
|
return self::getInstance()->getHeader($name, $default); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $name |
|
146
|
|
|
* @param string $default |
|
147
|
|
|
* @return string|null |
|
148
|
|
|
*/ |
|
149
|
9 |
|
public function getHeader($name, $default = null) |
|
150
|
|
|
{ |
|
151
|
9 |
|
$header = null; |
|
152
|
9 |
|
if ($this->hasHeader($name)) { |
|
153
|
|
|
$header = $this->header[$name]; |
|
154
|
9 |
|
} else if(array_key_exists('h_' . strtolower($name), $this->query)) { |
|
155
|
|
|
$header = $this->query['h_' . strtolower($name)]; |
|
156
|
9 |
|
} else if(array_key_exists('HTTP_' . strtoupper(str_replace('-', '_', $name)), $this->server)) { |
|
157
|
|
|
$header = $this->server['HTTP_' . strtoupper(str_replace('-', '_', $name))]; |
|
158
|
|
|
} |
|
159
|
9 |
|
return $header ?: $default; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Método que devuelve la url solicitada |
|
164
|
|
|
* @return string|null |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public static function requestUri() |
|
167
|
|
|
{ |
|
168
|
1 |
|
return self::getInstance()->getRequestUri(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
8 |
|
public function getRequestUri() |
|
175
|
|
|
{ |
|
176
|
8 |
|
return array_key_exists('REQUEST_URI', $this->server) ? $this->server['REQUEST_URI'] : ''; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Método que devuelve el idioma de la petición |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getLanguage() |
|
184
|
|
|
{ |
|
185
|
|
|
return array_key_exists('HTTP_ACCEPT_LANGUAGE', $this->server) ? $this->server['HTTP_ACCEPT_LANGUAGE'] : 'es_ES'; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Método que determina si se ha solicitado un fichero |
|
190
|
|
|
* @return boolean |
|
191
|
|
|
*/ |
|
192
|
5 |
|
public function isFile() |
|
193
|
|
|
{ |
|
194
|
5 |
|
return preg_match('/\.[a-z0-9]{2,4}$/', $this->getRequestUri()) !== 0; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get query params |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $queryParams |
|
201
|
|
|
* |
|
202
|
|
|
* @return mixed |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getQuery($queryParams) |
|
205
|
|
|
{ |
|
206
|
|
|
return array_key_exists($queryParams, $this->query) ? $this->query[$queryParams] : null; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Get all query params |
|
211
|
|
|
* |
|
212
|
|
|
* @return mixed |
|
213
|
|
|
*/ |
|
214
|
3 |
|
public function getQueryParams() |
|
215
|
|
|
{ |
|
216
|
3 |
|
return $this->query; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Método que devuelve un parámetro de la solicitud |
|
221
|
|
|
* @param string $param |
|
222
|
|
|
* |
|
223
|
|
|
* @return string|null |
|
224
|
|
|
*/ |
|
225
|
|
|
public function get($param) |
|
226
|
|
|
{ |
|
227
|
|
|
return array_key_exists($param, $this->data) ? $this->data[$param] : null; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Método que devuelve todos los datos del Request |
|
232
|
|
|
* @return array |
|
233
|
|
|
*/ |
|
234
|
3 |
|
public function getData() |
|
235
|
|
|
{ |
|
236
|
3 |
|
return array_merge($this->data, $this->raw); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return array |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getRawData() { |
|
243
|
|
|
return $this->raw; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Método que realiza una redirección a la url dada |
|
248
|
|
|
* @param string $url |
|
249
|
|
|
*/ |
|
250
|
|
|
public function redirect($url = null) |
|
251
|
|
|
{ |
|
252
|
|
|
if (null === $url) { |
|
253
|
|
|
$url = $this->getServer('HTTP_ORIGIN'); |
|
254
|
|
|
} |
|
255
|
|
|
ob_start(); |
|
256
|
|
|
header('Location: ' . $url); |
|
257
|
|
|
ob_end_clean(); |
|
258
|
|
|
Security::getInstance()->updateSession(); |
|
259
|
|
|
exit(t('Redireccionando...')); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Devuelve un parámetro de $_SERVER |
|
264
|
|
|
* @param string $param |
|
265
|
|
|
* @param $default |
|
266
|
|
|
* @return string|null |
|
267
|
|
|
*/ |
|
268
|
5 |
|
public function getServer($param, $default = null) |
|
269
|
|
|
{ |
|
270
|
5 |
|
return array_key_exists($param, $this->server) ? $this->server[$param] : $default; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Devuelve el nombre del servidor |
|
275
|
|
|
* @return string|null |
|
276
|
|
|
*/ |
|
277
|
2 |
|
public function getServerName() |
|
278
|
|
|
{ |
|
279
|
2 |
|
$serverName = $this->getServer('SERVER_NAME', 'localhost'); |
|
280
|
2 |
|
return strlen($serverName) ? $serverName : $this->getServer('HTTP_HOST', 'localhost'); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Devuelve el protocolo de la conexión |
|
285
|
|
|
* @return string |
|
286
|
|
|
*/ |
|
287
|
2 |
|
public function getProtocol() |
|
288
|
|
|
{ |
|
289
|
2 |
|
return ($this->getServer('HTTPS') || $this->getServer('https')) ? 'https://' : 'http://'; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Devuelve la url completa de base |
|
294
|
|
|
* @param boolean $hasProtocol |
|
295
|
|
|
* @return string |
|
296
|
|
|
*/ |
|
297
|
2 |
|
public function getRootUrl($hasProtocol = true) |
|
298
|
|
|
{ |
|
299
|
2 |
|
$url = $this->getServerName(); |
|
300
|
2 |
|
$protocol = $hasProtocol ? $this->getProtocol() : ''; |
|
301
|
2 |
|
if (!empty($protocol)) { |
|
302
|
2 |
|
$url = $protocol . $url; |
|
303
|
|
|
} |
|
304
|
2 |
|
$url = $this->checkServerPort($url); |
|
|
|
|
|
|
305
|
2 |
|
return $url; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Método que devuelve el valor de una cookie en caso de que exista |
|
310
|
|
|
* @param string $name |
|
311
|
|
|
* |
|
312
|
|
|
* @return string |
|
313
|
|
|
*/ |
|
314
|
1 |
|
public function getCookie($name) |
|
315
|
|
|
{ |
|
316
|
1 |
|
return array_key_exists($name, $this->cookies) ? $this->cookies[$name] : null; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Método que devuelve los files subidos por POST |
|
321
|
|
|
* @param $name |
|
322
|
|
|
* |
|
323
|
|
|
* @return array |
|
324
|
|
|
*/ |
|
325
|
|
|
public function getFile($name) |
|
326
|
|
|
{ |
|
327
|
|
|
return array_key_exists($name, $this->upload) ? $this->upload[$name] : array(); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Método que devuelve si la petición es ajax o no |
|
332
|
|
|
* @return boolean |
|
333
|
|
|
*/ |
|
334
|
1 |
|
public function isAjax() |
|
335
|
|
|
{ |
|
336
|
1 |
|
$requested = $this->getServer('HTTP_X_REQUESTED_WITH'); |
|
337
|
1 |
|
return (null !== $requested && strtolower($requested) === 'xmlhttprequest'); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @param string $url |
|
342
|
|
|
* @return string |
|
343
|
|
|
*/ |
|
344
|
2 |
|
protected function checkServerPort(string $url): string |
|
345
|
|
|
{ |
|
346
|
2 |
|
$port = (integer)$this->getServer('SERVER_PORT'); |
|
347
|
2 |
|
$host = $this->getServer('HTTP_HOST'); |
|
348
|
2 |
|
if(!empty($host)) { |
|
349
|
|
|
$parts = explode(':', $host); |
|
350
|
|
|
$hostPort = (integer)end($parts); |
|
351
|
|
|
if(count($parts) > 1 && $hostPort !== $port) { |
|
352
|
|
|
$port = $hostPort; |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
2 |
|
if (!in_array($port, [80, 443], true)) { |
|
356
|
2 |
|
$url .= ':' . $port; |
|
357
|
|
|
} |
|
358
|
2 |
|
return $url; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
} |
|
362
|
|
|
|