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