Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
99.14% covered (success)
99.14%
115 / 116
97.67% covered (success)
97.67%
42 / 43
CRAP
0.00% covered (danger)
0.00%
0 / 1
Request
99.14% covered (success)
99.14%
115 / 116
97.67% covered (success)
97.67%
42 / 43
98
0.00% covered (danger)
0.00%
0 / 1
 __construct
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
10
 isGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isHead
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isPost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isPut
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isDelete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isTrace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isConnect
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isPatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isSecure
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 getDocumentRoot
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getPort
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getScheme
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getHost
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 getFullHost
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 getIp
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 getCookie
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getServer
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getEnv
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getBasePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequestUri
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFullRequestUri
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSegment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSegments
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBasePath
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasFiles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQuery
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFiles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPut
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDelete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQueryData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasQueryData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParsedData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasParsedData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRawData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasRawData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequestUriObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequestDataObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
14
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Http\Server;
15
16use Pop\Http\AbstractRequest;
17use Pop\Http\Server\Request\Data;
18use Pop\Http\Server\Request\Uri;
19use Pop\Mime\Part\Body;
20
21/**
22 * HTTP server request class
23 *
24 * @category   Pop
25 * @package    Pop\Http
26 * @author     Nick Sagona, III <dev@nolainteractive.com>
27 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
28 * @license    http://www.popphp.org/license     New BSD License
29 * @version    4.1.0
30 */
31class Request extends AbstractRequest
32{
33
34    /**
35     * Request URI object
36     * @var Uri
37     */
38    protected $requestUri = null;
39
40    /**
41     * Request data object
42     * @var Data
43     */
44    protected $requestData = null;
45
46    /**
47     * COOKIE array
48     * @var array
49     */
50    protected $cookie = [];
51
52    /**
53     * SERVER array
54     * @var array
55     */
56    protected $server = [];
57
58    /**
59     * ENV array
60     * @var array
61     */
62    protected $env = [];
63
64    /**
65     * Constructor
66     *
67     * Instantiate the request object
68     *
69     * @param  string $uri
70     * @param  string $basePath
71     * @param  mixed  $filters
72     * @param  mixed  $streamToFile
73     */
74    public function __construct($uri = null, $basePath = null, $filters = null, $streamToFile = null)
75    {
76        parent::__construct($filters);
77
78        $this->cookie = (isset($_COOKIE)) ? $_COOKIE : [];
79        $this->server = (isset($_SERVER)) ? $_SERVER : [];
80        $this->env    = (isset($_ENV))    ? $_ENV    : [];
81
82        // Get any possible request headers
83        if (function_exists('getallheaders')) {
84            $this->addHeaders(getallheaders());
85        } else {
86            foreach ($_SERVER as $key => $value) {
87                if (substr($key, 0, 5) == 'HTTP_') {
88                    $key = ucfirst(strtolower(str_replace('HTTP_', '', $key)));
89                    if (strpos($key, '_') !== false) {
90                        $ary = explode('_', $key);
91                        foreach ($ary as $k => $v){
92                            $ary[$k] = ucfirst(strtolower($v));
93                        }
94                        $key = implode('-', $ary);
95                    }
96                    $this->addHeader($key, $value);
97                }
98            }
99        }
100
101        $this->requestUri  = new Uri($uri, $basePath);
102        $this->requestData = new Data(
103            $this->getHeaderValue('Content-Type'), $this->getHeaderValue('Content-Encoding'), $filters, $streamToFile
104        );
105
106        if ($this->requestData->hasRawData()) {
107            $this->body = new Body($this->requestData->getRawData());
108        }
109    }
110
111    /**
112     * Return whether or not the method is GET
113     *
114     * @return boolean
115     */
116    public function isGet()
117    {
118        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'GET'));
119    }
120
121    /**
122     * Return whether or not the method is HEAD
123     *
124     * @return boolean
125     */
126    public function isHead()
127    {
128        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'HEAD'));
129    }
130
131    /**
132     * Return whether or not the method is POST
133     *
134     * @return boolean
135     */
136    public function isPost()
137    {
138        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'POST'));
139    }
140
141    /**
142     * Return whether or not the method is PUT
143     *
144     * @return boolean
145     */
146    public function isPut()
147    {
148        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'PUT'));
149    }
150
151    /**
152     * Return whether or not the method is DELETE
153     *
154     * @return boolean
155     */
156    public function isDelete()
157    {
158        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'DELETE'));
159    }
160
161    /**
162     * Return whether or not the method is TRACE
163     *
164     * @return boolean
165     */
166    public function isTrace()
167    {
168        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'TRACE'));
169    }
170
171    /**
172     * Return whether or not the method is OPTIONS
173     *
174     * @return boolean
175     */
176    public function isOptions()
177    {
178        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'OPTIONS'));
179    }
180
181    /**
182     * Return whether or not the method is CONNECT
183     *
184     * @return boolean
185     */
186    public function isConnect()
187    {
188        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'CONNECT'));
189    }
190
191    /**
192     * Return whether or not the method is PATCH
193     *
194     * @return boolean
195     */
196    public function isPatch()
197    {
198        return (isset($this->server['REQUEST_METHOD']) && ($this->server['REQUEST_METHOD'] == 'PATCH'));
199    }
200
201    /**
202     * Return whether or not the request is secure
203     *
204     * @return boolean
205     */
206    public function isSecure()
207    {
208        return (isset($this->server['HTTPS']) || (isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] == '443')));
209    }
210
211    /**
212     * Get the document root
213     *
214     * @return string
215     */
216    public function getDocumentRoot()
217    {
218        return (isset($this->server['DOCUMENT_ROOT'])) ? $this->server['DOCUMENT_ROOT'] : null;
219    }
220
221    /**
222     * Get the method
223     *
224     * @return string
225     */
226    public function getMethod()
227    {
228        return (isset($this->server['REQUEST_METHOD'])) ? $this->server['REQUEST_METHOD'] : null;
229    }
230
231    /**
232     * Get the server port
233     *
234     * @return string
235     */
236    public function getPort()
237    {
238        return (isset($this->server['SERVER_PORT'])) ? $this->server['SERVER_PORT'] : null;
239    }
240
241    /**
242     * Get scheme
243     *
244     * @return string
245     */
246    public function getScheme()
247    {
248        return ($this->isSecure()) ? 'https' : 'http';
249    }
250
251    /**
252     * Get host without port)
253     *
254     * @return string
255     */
256    public function getHost()
257    {
258        $hostname = null;
259
260        if (!empty($this->server['HTTP_HOST'])) {
261            $hostname = $this->server['HTTP_HOST'];
262        } else if (!empty($this->server['SERVER_NAME'])) {
263            $hostname = $this->server['SERVER_NAME'];
264        }
265
266        if (strpos($hostname, ':') !== false) {
267            $hostname = substr($hostname, 0, strpos($hostname, ':'));
268        }
269
270        return $hostname;
271    }
272
273    /**
274     * Get host with port
275     *
276     * @return string
277     */
278    public function getFullHost()
279    {
280        $port     = $this->getPort();
281        $hostname = null;
282
283        if (!empty($this->server['HTTP_HOST'])) {
284            $hostname = $this->server['HTTP_HOST'];
285        } else if (!empty($this->server['SERVER_NAME'])) {
286            $hostname = $this->server['SERVER_NAME'];
287        }
288
289        if ((strpos($hostname, ':') === false) && (null !== $port)) {
290            $hostname .= ':' . $port;
291        }
292
293        return $hostname;
294    }
295
296    /**
297     * Get client's IP
298     *
299     * @param  boolean $proxy
300     * @return string
301     */
302    public function getIp($proxy = true)
303    {
304        $ip = null;
305
306        if ($proxy && isset($this->server['HTTP_CLIENT_IP'])) {
307            $ip = $this->server['HTTP_CLIENT_IP'];
308        } else if ($proxy && isset($this->server['HTTP_X_FORWARDED_FOR'])) {
309            $ip = $this->server['HTTP_X_FORWARDED_FOR'];
310        } else if (isset($this->server['REMOTE_ADDR'])) {
311            $ip = $this->server['REMOTE_ADDR'];
312        }
313
314        return $ip;
315    }
316
317    /**
318     * Get a value from $_COOKIE, or the whole array
319     *
320     * @param  string $key
321     * @return string|array
322     */
323    public function getCookie($key = null)
324    {
325        if (null === $key) {
326            return $this->cookie;
327        } else {
328            return (isset($this->cookie[$key])) ? $this->cookie[$key] : null;
329        }
330    }
331
332    /**
333     * Get a value from $_SERVER, or the whole array
334     *
335     * @param  string $key
336     * @return string|array
337     */
338    public function getServer($key = null)
339    {
340        if (null === $key) {
341            return $this->server;
342        } else {
343            return (isset($this->server[$key])) ? $this->server[$key] : null;
344        }
345    }
346
347    /**
348     * Get a value from $_ENV, or the whole array
349     *
350     * @param  string $key
351     * @return string|array
352     */
353    public function getEnv($key = null)
354    {
355        if (null === $key) {
356            return $this->env;
357        } else {
358            return (isset($this->env[$key])) ? $this->env[$key] : null;
359        }
360    }
361
362    /**
363     * Get the base path
364     *
365     * @return string
366     */
367    public function getBasePath()
368    {
369        return $this->requestUri->getBasePath();
370    }
371
372    /**
373     * Get the request URI
374     *
375     * @return string
376     */
377    public function getRequestUri()
378    {
379        return $this->requestUri->getRequestUri();
380    }
381
382    /**
383     * Get the full request URI, including base path
384     *
385     * @return string
386     */
387    public function getFullRequestUri()
388    {
389        return $this->requestUri->getFullRequestUri();
390    }
391
392    /**
393     * Get a path segment, divided by the forward slash,
394     * where $i refers to the array key index, i.e.,
395     *    0     1     2
396     * /hello/world/page
397     *
398     * @param  int $i
399     * @return string
400     */
401    public function getSegment($i)
402    {
403        return $this->requestUri->getSegment($i);
404    }
405
406    /**
407     * Get all path segments
408     *
409     * @return array
410     */
411    public function getSegments()
412    {
413        return $this->requestUri->getSegments();
414    }
415
416    /**
417     * Set the base path
418     *
419     * @param  string $path
420     * @return Request
421     */
422    public function setBasePath($path = null)
423    {
424        $this->requestUri->setBasePath($path);
425        return $this;
426    }
427
428    /**
429     * Return whether or not the request has FILES
430     *
431     * @return boolean
432     */
433    public function hasFiles()
434    {
435        return $this->requestData->hasFiles();
436    }
437
438    /**
439     * Get a value from $_GET, or the whole array
440     *
441     * @param  string $key
442     * @return string|array
443     */
444    public function getQuery($key = null)
445    {
446        return $this->requestData->getQuery($key);
447    }
448
449    /**
450     * Get a value from $_POST, or the whole array
451     *
452     * @param  string $key
453     * @return string|array
454     */
455    public function getPost($key = null)
456    {
457        return $this->requestData->getPost($key);
458    }
459
460    /**
461     * Get a value from $_FILES, or the whole array
462     *
463     * @param  string $key
464     * @return string|array
465     */
466    public function getFiles($key = null)
467    {
468        return $this->requestData->getFiles($key);
469    }
470
471    /**
472     * Get a value from PUT query data, or the whole array
473     *
474     * @param  string $key
475     * @return string|array
476     */
477    public function getPut($key = null)
478    {
479        return $this->requestData->getPut($key);
480    }
481
482    /**
483     * Get a value from PATCH query data, or the whole array
484     *
485     * @param  string $key
486     * @return string|array
487     */
488    public function getPatch($key = null)
489    {
490        return $this->requestData->getPatch($key);
491    }
492
493    /**
494     * Get a value from DELETE query data, or the whole array
495     *
496     * @param  string $key
497     * @return string|array
498     */
499    public function getDelete($key = null)
500    {
501        return $this->requestData->getDelete($key);
502    }
503
504
505    /**
506     * Get a value from query data, or the whole array
507     *
508     * @param  string $key
509     * @return string|array
510     */
511    public function getQueryData($key = null)
512    {
513        return $this->requestData->getQueryData($key);
514    }
515
516    /**
517     * Has query data
518     *
519     * @return boolean
520     */
521    public function hasQueryData()
522    {
523        return $this->requestData->hasQueryData();
524    }
525
526    /**
527     * Get a value from parsed data, or the whole array
528     *
529     * @param  string $key
530     * @return string|array
531     */
532    public function getParsedData($key = null)
533    {
534        return $this->requestData->getParsedData($key);
535    }
536
537    /**
538     * Has parsed data
539     *
540     * @return boolean
541     */
542    public function hasParsedData()
543    {
544        return $this->requestData->hasParsedData();
545    }
546
547    /**
548     * Get the raw data
549     *
550     * @return string
551     */
552    public function getRawData()
553    {
554        return $this->requestData->getRawData();
555    }
556
557    /**
558     * Has raw data
559     *
560     * @return boolean
561     */
562    public function hasRawData()
563    {
564        return $this->requestData->hasRawData();
565    }
566
567    /**
568     * Get request URI object
569     *
570     * @return Uri
571     */
572    public function getRequestUriObject()
573    {
574        return $this->requestUri;
575    }
576
577    /**
578     * Get request data object
579     *
580     * @return Data
581     */
582    public function getRequestDataObject()
583    {
584        return $this->requestData;
585    }
586
587    /**
588     * Magic method to get a value from one of the server/environment variables
589     *
590     * @param  string $name
591     * @return mixed
592     */
593    public function __get($name)
594    {
595        switch ($name) {
596            case 'get':
597                return $this->requestData->get;
598                break;
599            case 'post':
600                return $this->requestData->post;
601                break;
602            case 'files':
603                return $this->requestData->files;
604                break;
605            case 'put':
606                return $this->requestData->put;
607                break;
608            case 'patch':
609                return $this->requestData->patch;
610                break;
611            case 'delete':
612                return $this->requestData->delete;
613                break;
614            case 'parsed':
615                return $this->requestData->parsed;
616                break;
617            case 'raw':
618                return $this->requestData->raw;
619                break;
620            case 'cookie':
621                return $this->cookie;
622                break;
623            case 'server':
624                return $this->server;
625                break;
626            case 'env':
627                return $this->env;
628                break;
629            case 'headers':
630                return $this->headers;
631                break;
632            default:
633                return null;
634        }
635    }
636
637}