1 | <?php |
||
2 | |||
3 | namespace ByJG\RestServer; |
||
4 | |||
5 | class HttpRequest |
||
6 | { |
||
7 | |||
8 | protected $get; |
||
9 | protected $post; |
||
10 | protected $server; |
||
11 | protected $session; |
||
12 | protected $cookie; |
||
13 | protected $param; |
||
14 | protected $phpRequest; |
||
15 | |||
16 | /** |
||
17 | * |
||
18 | * @param array $get |
||
19 | * @param array $post |
||
20 | * @param array $server |
||
21 | * @param array $session |
||
22 | * @param array $cookie |
||
23 | * @param array $param |
||
24 | */ |
||
25 | 15 | public function __construct($get, $post, $server, $session, $cookie, $param = []) |
|
26 | { |
||
27 | 15 | $this->get = $get; |
|
28 | 15 | $this->post = $post; |
|
29 | 15 | $this->server = $server; |
|
30 | 15 | $this->session = $session; |
|
31 | 15 | $this->cookie = $cookie; |
|
32 | 15 | $this->param = $param; |
|
33 | |||
34 | 15 | $this->phpRequest = array_merge($get, $post, $server, $session, $cookie); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * Get a parameter passed by GET (the same as $_GET). If not found return false. |
||
39 | * |
||
40 | * @param string $value |
||
41 | * @return string|boolean |
||
42 | */ |
||
43 | public function get($value, $default = false) |
||
44 | { |
||
45 | if (!isset($this->get[$value])) { |
||
46 | return $default; |
||
47 | } else { |
||
48 | return $this->get[$value]; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Get a parameter passed by POST (the same as $_POST). If not found return false. |
||
54 | * |
||
55 | * @param string $value |
||
56 | * @return string|boolean |
||
57 | */ |
||
58 | public function post($value, $default = false) |
||
59 | { |
||
60 | if (!isset($this->post[$value])) { |
||
61 | return $default; |
||
62 | } else { |
||
63 | return $this->post[$value]; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get the parameters sent by server (the same as $_SERVER). If not found return false. |
||
69 | * |
||
70 | * @param string $value |
||
71 | * @return string|boolean |
||
72 | */ |
||
73 | 12 | public function server($value, $default = false) |
|
74 | { |
||
75 | 12 | if (!isset($this->server[$value])) { |
|
76 | return $default; |
||
77 | } else { |
||
78 | 12 | return $this->server[$value]; |
|
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get a server session value(the same as $_SESSION). If not found return false. |
||
84 | * |
||
85 | * @param string $value |
||
86 | * @return string|boolean |
||
87 | */ |
||
88 | public function session($value, $default = false) |
||
89 | { |
||
90 | if (!isset($this->session[$value])) { |
||
91 | return $default; |
||
92 | } else { |
||
93 | return $this->session[$value]; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get the cookie sent by the client (the same as $_COOKIE). If not found return false. |
||
99 | * |
||
100 | * @param string $value |
||
101 | * @return string|boolean |
||
102 | */ |
||
103 | public function cookie($value, $default = false) |
||
104 | { |
||
105 | if (!isset($this->cookie[$value])) { |
||
106 | return $default; |
||
107 | } else { |
||
108 | return $this->cookie[$value]; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Get a value from any of get, post, server, cookie or session. If not found return false. |
||
114 | * |
||
115 | * @param string $value |
||
116 | * @return string|boolean |
||
117 | */ |
||
118 | public function request($value, $default = false) |
||
119 | { |
||
120 | if (!isset($this->phpRequest[$value])) { |
||
121 | return $default; |
||
122 | } else { |
||
123 | return $this->phpRequest[$value]; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get a value from the params found in the URL |
||
129 | * |
||
130 | * @param string $value |
||
131 | * @return string|boolean |
||
132 | */ |
||
133 | 4 | public function param($value, $default = false) |
|
134 | { |
||
135 | 4 | if (!isset($this->param[$value])) { |
|
136 | return $default; |
||
137 | } else { |
||
138 | 4 | return $this->param[$value]; |
|
139 | } |
||
140 | } |
||
141 | |||
142 | private $payload; |
||
143 | |||
144 | /** |
||
145 | * Get the payload passed during the request(the same as php://input). If not found return empty. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function payload() |
||
150 | { |
||
151 | if (is_null($this->payload)) { |
||
152 | $this->payload = file_get_contents("php://input"); |
||
153 | } |
||
154 | |||
155 | return $this->payload; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Use this method to get the CLIENT REQUEST IP. |
||
160 | * Note that if you behing a Proxy, the variable REMOTE_ADDR will always have the same IP |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getRequestIp() |
||
164 | { |
||
165 | $headers = [ |
||
166 | 'HTTP_CLIENT_IP', |
||
167 | 'HTTP_X_FORWARDED_FOR', |
||
168 | 'HTTP_X_FORWARDED', |
||
169 | 'HTTP_FORWARDED_FOR', |
||
170 | 'HTTP_FORWARDED', |
||
171 | 'REMOTE_ADDR', |
||
172 | ]; |
||
173 | foreach ($headers as $header) { |
||
174 | if ($this->server($header) !== false) { |
||
175 | return $this->server($header); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
176 | } |
||
177 | } |
||
178 | |||
179 | return 'UNKNOWN'; |
||
180 | } |
||
181 | |||
182 | public function getServerName() |
||
183 | { |
||
184 | $headers = [ |
||
185 | 'SERVER_NAME', |
||
186 | 'HTTP_HOST', |
||
187 | ]; |
||
188 | foreach ($headers as $header) { |
||
189 | if ($this->server($header) !== false) { |
||
190 | return $this->server('SERVER_NAME'); |
||
191 | } |
||
192 | } |
||
193 | return $this->server('SERVER_ADDR'); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Use this method to get the SERVER NAME. |
||
198 | * @param bool $port |
||
199 | * @param bool $protocol |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getRequestServer($port = false, $protocol = false) |
||
203 | { |
||
204 | $servername = $this->getServerName(); |
||
205 | |||
206 | if ($port && $this->server('SERVER_PORT' !== false)) { |
||
207 | $servername .= ':' . $this->server('SERVER_PORT'); |
||
208 | } |
||
209 | |||
210 | if ($protocol) { |
||
211 | $servername = ( |
||
212 | ($this->server('HTTPS') !== 'off' |
||
213 | || $this->server('SERVER_PORT') == 443) ? "https://" : "http://") . $servername |
||
214 | ; |
||
215 | } |
||
216 | |||
217 | return $servername; |
||
0 ignored issues
–
show
|
|||
218 | } |
||
219 | |||
220 | public function getRequestPath() |
||
221 | { |
||
222 | return parse_url($this->server('REQUEST_URI'), PHP_URL_PATH); |
||
223 | } |
||
224 | |||
225 | private $uploadedFiles; |
||
226 | |||
227 | /** |
||
228 | * @return UploadedFiles |
||
229 | */ |
||
230 | public function uploadedFiles() |
||
231 | { |
||
232 | if (!isset($this->uploadedFiles)) { |
||
233 | $this->uploadedFiles = new UploadedFiles(); |
||
234 | } |
||
235 | return $this->uploadedFiles; |
||
236 | } |
||
237 | |||
238 | 6 | public function appendVars($array) |
|
239 | { |
||
240 | 6 | $this->param = array_merge($this->param, $array); |
|
241 | } |
||
242 | } |
||
243 |