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
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
1 |
|
public function isLoaded() { |
71
|
1 |
|
return $this->isLoaded; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Método que devuelve las cabeceras de la petición |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
2 |
|
private function parseHeaders() |
79
|
|
|
{ |
80
|
2 |
|
return getallheaders(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Método que verifica si existe una cabecera concreta |
85
|
|
|
* @param $header |
86
|
|
|
* |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
11 |
|
public function hasHeader($header) |
90
|
|
|
{ |
91
|
11 |
|
return array_key_exists($header, $this->header); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Método que indica si una petición tiene cookies |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
1 |
|
public function hasCookies() |
100
|
|
|
{ |
101
|
1 |
|
return (null !== $this->cookies && 0 !== count($this->cookies)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Método que indica si una petición tiene cookies |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
1 |
|
public function hasUpload() |
109
|
|
|
{ |
110
|
1 |
|
return (null !== $this->upload && 0 !== count($this->upload)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Método que devuelve el TimeStamp de la petición |
115
|
|
|
* |
116
|
|
|
* @param boolean $formatted |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public static function ts($formatted = false) |
121
|
|
|
{ |
122
|
|
|
return self::getInstance()->getTs($formatted); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
public function getTs($formatted = false) |
126
|
|
|
{ |
127
|
1 |
|
return $formatted ? date('Y-m-d H:i:s', $this->server['REQUEST_TIME_FLOAT']) : $this->server['REQUEST_TIME_FLOAT']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Método que devuelve el Método HTTP utilizado |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
5 |
|
public function getMethod() |
135
|
|
|
{ |
136
|
5 |
|
return array_key_exists('REQUEST_METHOD', $this->server) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Método que devuelve una cabecera de la petición si existe |
141
|
|
|
* @param string $name |
142
|
|
|
* @param string $default |
143
|
|
|
* |
144
|
|
|
* @return string|null |
145
|
|
|
*/ |
146
|
10 |
|
public static function header($name, $default = null) |
147
|
|
|
{ |
148
|
10 |
|
return self::getInstance()->getHeader($name, $default); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $name |
153
|
|
|
* @param string $default |
154
|
|
|
* @return string|null |
155
|
|
|
*/ |
156
|
10 |
|
public function getHeader($name, $default = null) |
157
|
|
|
{ |
158
|
10 |
|
$header = null; |
159
|
10 |
|
if ($this->hasHeader($name)) { |
160
|
|
|
$header = $this->header[$name]; |
161
|
10 |
|
} else if(array_key_exists('h_' . strtolower($name), $this->query)) { |
162
|
|
|
$header = $this->query['h_' . strtolower($name)]; |
163
|
10 |
|
} else if(array_key_exists('HTTP_' . strtoupper(str_replace('-', '_', $name)), $this->server)) { |
164
|
|
|
$header = $this->server['HTTP_' . strtoupper(str_replace('-', '_', $name))]; |
165
|
|
|
} |
166
|
10 |
|
return $header ?: $default; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Método que devuelve la url solicitada |
171
|
|
|
* @return string|null |
172
|
|
|
*/ |
173
|
1 |
|
public static function requestUri() |
174
|
|
|
{ |
175
|
1 |
|
return self::getInstance()->getRequestUri(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
8 |
|
public function getRequestUri() |
182
|
|
|
{ |
183
|
8 |
|
return array_key_exists('REQUEST_URI', $this->server) ? $this->server['REQUEST_URI'] : ''; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Método que devuelve el idioma de la petición |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getLanguage() |
191
|
|
|
{ |
192
|
|
|
return array_key_exists('HTTP_ACCEPT_LANGUAGE', $this->server) ? $this->server['HTTP_ACCEPT_LANGUAGE'] : 'es_ES'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Método que determina si se ha solicitado un fichero |
197
|
|
|
* @return boolean |
198
|
|
|
*/ |
199
|
5 |
|
public function isFile() |
200
|
|
|
{ |
201
|
5 |
|
return preg_match('/\.[a-z0-9]{2,4}$/', $this->getRequestUri()) !== 0; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get query params |
206
|
|
|
* |
207
|
|
|
* @param string $queryParams |
208
|
|
|
* |
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
|
|
public function getQuery($queryParams) |
212
|
|
|
{ |
213
|
|
|
return array_key_exists($queryParams, $this->query) ? $this->query[$queryParams] : null; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get all query params |
218
|
|
|
* |
219
|
|
|
* @return mixed |
220
|
|
|
*/ |
221
|
2 |
|
public function getQueryParams() |
222
|
|
|
{ |
223
|
2 |
|
return $this->query; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Método que devuelve un parámetro de la solicitud |
228
|
|
|
* @param string $param |
229
|
|
|
* |
230
|
|
|
* @return string|null |
231
|
|
|
*/ |
232
|
|
|
public function get($param) |
233
|
|
|
{ |
234
|
|
|
return array_key_exists($param, $this->data) ? $this->data[$param] : null; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Método que devuelve todos los datos del Request |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
1 |
|
public function getData() |
242
|
|
|
{ |
243
|
1 |
|
return array_merge($this->data, $this->raw); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
public function getRawData() { |
250
|
|
|
return $this->raw; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Método que realiza una redirección a la url dada |
255
|
|
|
* @param string $url |
256
|
|
|
*/ |
257
|
|
|
public function redirect($url = null) |
258
|
|
|
{ |
259
|
|
|
if (null === $url) { |
260
|
|
|
$url = $this->getServer('HTTP_ORIGIN'); |
261
|
|
|
} |
262
|
|
|
ob_start(); |
263
|
|
|
header('Location: ' . $url); |
264
|
|
|
ob_end_clean(); |
265
|
|
|
Security::getInstance()->updateSession(); |
266
|
|
|
exit(t('Redireccionando...')); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Devuelve un parámetro de $_SERVER |
271
|
|
|
* @param string $param |
272
|
|
|
* @param $default |
273
|
|
|
* @return string|null |
274
|
|
|
*/ |
275
|
5 |
|
public function getServer($param, $default = null) |
276
|
|
|
{ |
277
|
5 |
|
return array_key_exists($param, $this->server) ? $this->server[$param] : $default; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Devuelve el nombre del servidor |
282
|
|
|
* @return string|null |
283
|
|
|
*/ |
284
|
2 |
|
public function getServerName() |
285
|
|
|
{ |
286
|
2 |
|
$serverName = $this->getServer('SERVER_NAME', 'localhost'); |
287
|
2 |
|
return strlen($serverName) ? $serverName : $this->getServer('HTTP_HOST', 'localhost'); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Devuelve el protocolo de la conexión |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
2 |
|
public function getProtocol() |
295
|
|
|
{ |
296
|
2 |
|
return ($this->getServer('HTTPS') || $this->getServer('https')) ? 'https://' : 'http://'; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Devuelve la url completa de base |
301
|
|
|
* @param boolean $hasProtocol |
302
|
|
|
* @return string |
303
|
|
|
*/ |
304
|
2 |
|
public function getRootUrl($hasProtocol = true) |
305
|
|
|
{ |
306
|
2 |
|
$url = $this->getServerName(); |
307
|
2 |
|
$protocol = $hasProtocol ? $this->getProtocol() : ''; |
308
|
2 |
|
if (!empty($protocol)) { |
309
|
2 |
|
$url = $protocol . $url; |
310
|
|
|
} |
311
|
2 |
|
if (!in_array((integer)$this->getServer('SERVER_PORT'), [80, 443], true)) { |
312
|
2 |
|
$url .= ':' . $this->getServer('SERVER_PORT'); |
313
|
|
|
} |
314
|
2 |
|
return $url; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Método que devuelve el valor de una cookie en caso de que exista |
319
|
|
|
* @param string $name |
320
|
|
|
* |
321
|
|
|
* @return string |
322
|
|
|
*/ |
323
|
1 |
|
public function getCookie($name) |
324
|
|
|
{ |
325
|
1 |
|
return array_key_exists($name, $this->cookies) ? $this->cookies[$name] : null; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Método que devuelve los files subidos por POST |
330
|
|
|
* @param $name |
331
|
|
|
* |
332
|
|
|
* @return array |
333
|
|
|
*/ |
334
|
|
|
public function getFile($name) |
335
|
|
|
{ |
336
|
|
|
return array_key_exists($name, $this->upload) ? $this->upload[$name] : array(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Método que devuelve si la petición es ajax o no |
341
|
|
|
* @return boolean |
342
|
|
|
*/ |
343
|
1 |
|
public function isAjax() |
344
|
|
|
{ |
345
|
1 |
|
$requested = $this->getServer('HTTP_X_REQUESTED_WITH'); |
346
|
1 |
|
return (null !== $requested && strtolower($requested) === 'xmlhttprequest'); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
} |
350
|
|
|
|