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