1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core\Dependency; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Request |
7
|
|
|
* @package Core |
8
|
|
|
* |
9
|
|
|
* we are dealing with get, post and cookies here, all Superglobals |
10
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
11
|
|
|
*/ |
12
|
|
|
class Request |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* gets the data from a get or a post request |
17
|
|
|
* @param $key |
18
|
|
|
* @return mixed |
19
|
|
|
* @throws \Exception |
20
|
|
|
*/ |
21
|
|
|
public function getData($key) |
22
|
|
|
{ |
23
|
|
|
if ($this->isGet()) { |
24
|
|
|
return $_GET[$key] ?? null; |
25
|
|
|
} |
26
|
|
|
if ($this->isPost()) { |
27
|
|
|
return $_POST[$key] ?? null; |
28
|
|
|
} |
29
|
|
|
throw new \Exception("Unknown Request Method"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* gets the full data from a get or a post request |
34
|
|
|
* @return mixed |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
|
|
public function getDataFull() |
38
|
|
|
{ |
39
|
|
|
if ($this->isGet()) { |
40
|
|
|
return $_GET ?? null; |
41
|
|
|
} |
42
|
|
|
if ($this->isPost()) { |
43
|
|
|
return $_POST ?? null; |
44
|
|
|
} |
45
|
|
|
throw new \Exception("Unknown Request Method"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* is the call a post |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function isPost(): bool |
53
|
|
|
{ |
54
|
|
|
return $_SERVER['REQUEST_METHOD'] === 'POST'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* is the call a get |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isGet(): bool |
62
|
|
|
{ |
63
|
|
|
return $_SERVER['REQUEST_METHOD'] === 'GET'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* get the current uri for routing |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function getUri() |
71
|
|
|
{ |
72
|
|
|
return $_SERVER['REQUEST_URI']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* checks if the request is a XML HTTP REQUEST |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isXmlRequest() |
80
|
|
|
{ |
81
|
|
|
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* gets the referer of the request |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
|
|
public function getReferer() |
92
|
|
|
{ |
93
|
|
|
return $_SERVER['HTTP_REFERER'] ?? null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* constructs the base url of the site |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getBaseUrl(): string |
101
|
|
|
{ |
102
|
|
|
$host = $_SERVER['HTTP_HOST']; |
103
|
|
|
$https = !empty($_SERVER['HTTPS']) ? 'https' : 'http'; |
104
|
|
|
return $https . '://' . $host . '/'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Gettint the headers |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getHeaders(): array |
112
|
|
|
{ |
113
|
|
|
return apache_request_headers() ?: []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Getting the uploaded file |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function getUploadedFiles() |
121
|
|
|
{ |
122
|
|
|
reset($_FILES); |
123
|
|
|
return current($_FILES); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* getting the server document root |
128
|
|
|
*/ |
129
|
|
|
public function getDocumentRoot() |
130
|
|
|
{ |
131
|
|
|
return $_SERVER['DOCUMENT_ROOT']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |