Test Setup Failed
Branch master (0844ab)
by Fran
07:46 queued 04:14
created

Request.php ➔ getallheaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace PSFS\base;
4
5
use PSFS\base\types\SingletonTrait;
6
7
if(!function_exists("getallheaders"))
8
{
9
    function getallheaders()
10
    {
11
        $headers = array();
12
        foreach($_SERVER as $h=>$v)
13
            if(preg_match('/HTTP_(.+)/',$h,$hp))
14
                $headers[$hp[1]]=$v;
15
        return $headers;
16
    }
17
}
18
19
/**
20
 * Class Request
21
 * @package PSFS
22
 */
23
class Request {
24
    use SingletonTrait;
25
    protected $server;
26
    protected $cookies;
27
    protected $upload;
28
    protected $header;
29
    protected $data;
30
    protected $query;
31
32
    /**
33
     */
34
    public function __construct() {
35
        $this->server = $_SERVER;
36
        $this->cookies = $_COOKIE;
37
        $this->upload = $_FILES;
38
        $this->header = $this->parseHeaders();
39
        $this->data = $_REQUEST;
40
        $this->query = $_GET;
41
        $contentType = (array_key_exists('Content-Type', $this->header)) ? $this->header['Content-Type'] : "text/html";
42
        if (preg_match('/application\/json/i', $contentType))
43
        {
44
            $this->data += json_decode(file_get_contents("php://input"), true) ?: array();
45
        }
46
    }
47
48
    /**
49
     * Método que devuelve las cabeceras de la petición
50
     * @return array
51
     */
52
    private function parseHeaders() { return getallheaders(); }
53
54
    /**
55
     * Método que verifica si existe una cabecera concreta
56
     * @param $header
57
     *
58
     * @return boolean
59
     */
60
    public function hasHeader($header) { return array_key_exists($header, $this->header); }
61
62
63
    /**
64
     * Método que indica si una petición tiene cookies
65
     * @return boolean
66
     */
67
    public function hasCookies() { return (null !== $this->cookies && 0 !== count($this->cookies)); }
68
69
    /**
70
     * Método que indica si una petición tiene cookies
71
     * @return boolean
72
     */
73
    public function hasUpload() { return (null !== $this->upload && 0 !== count($this->upload)); }
74
75
    /**
76
     * Método que devuelve el TimeStamp de la petición
77
     *
78
     * @param boolean $formatted
79
     *
80
     * @return string
81
     */
82
    public static function ts($formatted = false) { return self::getInstance()->getTs($formatted); }
83
    public function getTs($formatted = false) { return ($formatted) ? date('Y-m-d H:i:s', $this->server['REQUEST_TIME_FLOAT']) : $this->server['REQUEST_TIME_FLOAT']; }
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 167 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
84
85
    /**
86
     * Método que devuelve el Método HTTP utilizado
87
     * @return string
88
     */
89
    public function getMethod() { return (array_key_exists('REQUEST_METHOD', $this->server)) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET'; }
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 149 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
90
91
    /**
92
     * Método que devuelve una cabecera de la petición si existe
93
     * @param $name
94
     *
95
     * @return string|null
96
     */
97
    public static function header($name) { return self::getInstance()->getHeader($name); }
98
    public function getHeader($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
99
    {
100
        $header = null;
101
        if ($this->hasHeader($name))
102
        {
103
            $header = $this->header[$name];
104
        }
105
        return $header;
106
    }
107
108
    /**
109
     * Método que devuelve la url solicitada
110
     * @return string|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
111
     */
112
    public static function requestUri(){ return self::getInstance()->getRequestUri(); }
113
114
    /**
115
     * @return string
116
     */
117
    public function getRequestUri(){ return array_key_exists('REQUEST_URI', $this->server) ? $this->server['REQUEST_URI'] : ''; }
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
118
119
    /**
120
     * Método que devuelve el idioma de la petición
121
     * @return string
122
     */
123
    public function getLanguage()
124
    {
125
        return array_key_exists('HTTP_ACCEPT_LANGUAGE', $this->server) ? $this->server['HTTP_ACCEPT_LANGUAGE'] : 'es_ES';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
126
    }
127
128
    /**
129
     * Método que determina si se ha solicitado un fichero
130
     * @return boolean
131
     */
132
    public function isFile()
133
    {
134
        $file = (preg_match('/\.(css|js|png|jpg|jpeg|woff|ttf|svg|eot|xml|bmp|gif|txt|zip|yml|ini|conf|php|ico)$/', $this->getRequestUri()) !== 0);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 147 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
135
        return $file;
136
    }
137
138
    /**
139
     * Get query params
140
     *
141
     * @param string $queryParams
142
     *
143
     * @return mixed
144
     */
145
    public function getQuery($queryParams)
146
    {
147
        return (array_key_exists($queryParams, $this->query)) ? $this->query[$queryParams] : null;
148
    }
149
150
    /**
151
     * Get all query params
152
     *
153
     * @return mixed
154
     */
155
    public function getQueryParams()
156
    {
157
        return $this->query;
158
    }
159
160
    /**
161
     * Método que devuelve un parámetro de la solicitud
162
     * @param string $param
163
     *
164
     * @return string|null
165
     */
166
    public function get($param)
167
    {
168
        return (array_key_exists($param, $this->data)) ? $this->data[$param] : null;
169
    }
170
171
    /**
172
     * Método que devuelve todos los datos del Request
173
     * @return array
174
     */
175
    public function getData() { return $this->data; }
176
177
    /**
178
     * Método que realiza una redirección a la url dada
179
     * @param string $url
180
     */
181
    public function redirect($url = null)
182
    {
183
        if (null === $url) $url = $this->server['HTTP_ORIGIN'];
184
        ob_start();
185
        header('Location: '.$url);
186
        ob_end_clean();
187
        exit(_("Redireccionando..."));
188
    }
189
190
    /**
191
     * Devuelve un parámetro de $_SERVER
192
     * @param string $param
193
     *
194
     * @return string|null
195
     */
196
    public function getServer($param)
197
    {
198
        return array_key_exists($param, $this->server) ? $this->server[$param] : null;
199
    }
200
201
    /**
202
     * Devuelve el nombre del servidor
203
     * @return string|null
204
     */
205
    public function getServerName()
206
    {
207
        return $this->getServer("SERVER_NAME");
208
    }
209
210
    /**
211
     * Devuelve el protocolo de la conexión
212
     * @return string
213
     */
214
    public function getProtocol()
215
    {
216
        return ($this->getServer("HTTPS") || $this->getServer("https")) ? 'https://' : 'http://';
217
    }
218
219
    /**
220
     * Devuelve la url completa de base
221
     * @param boolean $protocol
222
     * @return string
223
     */
224
    public function getRootUrl($protocol = true)
225
    {
226
        $host = $this->getServerName();
227
        $protocol = $protocol ? $this->getProtocol() : '';
228
        $url = '';
229
        if (!empty($host) && !empty($protocol)) $url = $protocol.$host;
230
        return $url;
231
    }
232
233
    /**
234
     * Método que devuelve el valor de una cookie en caso de que exista
235
     * @param string $name
236
     *
237
     * @return string
238
     */
239
    public function getCookie($name)
240
    {
241
        return array_key_exists($name, $this->cookies) ? $this->cookies[$name] : null;
242
    }
243
244
    /**
245
     * Método que devuelve los files subidos por POST
246
     * @param $name
247
     *
248
     * @return array
249
     */
250
    public function getFile($name)
251
    {
252
        return array_key_exists($name, $this->upload) ? $this->upload[$name] : array();
253
    }
254
255
    /**
256
     * Método que devuelve si la petición es ajax o no
257
     * @return boolean
258
     */
259
    public function isAjax()
260
    {
261
        $requested = $this->getServer("HTTP_X_REQUESTED_WITH");
262
        return (null !== $requested && strtolower($requested) == 'xmlhttprequest');
263
    }
264
265
}
266