1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http; |
4
|
|
|
|
5
|
|
|
class HttpRequest implements Request |
6
|
|
|
{ |
7
|
|
|
protected $getParameters; |
8
|
|
|
protected $postParameters; |
9
|
|
|
protected $server; |
10
|
|
|
protected $files; |
11
|
|
|
protected $cookies; |
12
|
|
|
|
13
|
|
|
public function __construct( |
14
|
|
|
array $get, |
15
|
|
|
array $post, |
16
|
|
|
array $cookies, |
17
|
|
|
array $files, |
18
|
|
|
array $server |
19
|
|
|
) { |
20
|
|
|
$this->getParameters = $get; |
21
|
|
|
$this->postParameters = $post; |
22
|
|
|
$this->cookies = $cookies; |
23
|
|
|
$this->files = $files; |
24
|
|
|
$this->server = $server; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns a parameter value or a default value if none is set. |
29
|
|
|
* |
30
|
|
|
* @param string $key |
31
|
|
|
* @param string $defaultValue (optional) |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getParameter($key, $defaultValue = null) |
35
|
|
|
{ |
36
|
|
|
if (array_key_exists($key, $this->postParameters)) { |
37
|
|
|
return $this->postParameters[$key]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (array_key_exists($key, $this->getParameters)) { |
41
|
|
|
return $this->getParameters[$key]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $defaultValue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns a query parameter value or a default value if none is set. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* @param string $defaultValue (optional) |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getQueryParameter($key, $defaultValue = null) |
55
|
|
|
{ |
56
|
|
|
if (array_key_exists($key, $this->getParameters)) { |
57
|
|
|
return $this->getParameters[$key]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $defaultValue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns a body parameter value or a default value if none is set. |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @param string $defaultValue (optional) |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getBodyParameter($key, $defaultValue = null) |
71
|
|
|
{ |
72
|
|
|
if (array_key_exists($key, $this->postParameters)) { |
73
|
|
|
return $this->postParameters[$key]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $defaultValue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns a file value or a default value if none is set. |
81
|
|
|
* |
82
|
|
|
* @param string $key |
83
|
|
|
* @param string $defaultValue (optional) |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getFile($key, $defaultValue = null) |
87
|
|
|
{ |
88
|
|
|
if (array_key_exists($key, $this->files)) { |
89
|
|
|
return $this->files[$key]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $defaultValue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns a cookie value or a default value if none is set. |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* @param string $defaultValue (optional) |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getCookie($key, $defaultValue = null) |
103
|
|
|
{ |
104
|
|
|
if (array_key_exists($key, $this->cookies)) { |
105
|
|
|
return $this->cookies[$key]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $defaultValue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns all parameters. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getParameters() |
117
|
|
|
{ |
118
|
|
|
return array_merge($this->getParameters, $this->postParameters); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns all query parameters. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function getQueryParameters() |
127
|
|
|
{ |
128
|
|
|
return $this->getParameters; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns all body parameters. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getBodyParameters() |
137
|
|
|
{ |
138
|
|
|
return $this->postParameters; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
+ Returns raw values from the read-only stream that allows you to read raw data from the request body. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getRawBodyParameters() |
147
|
|
|
{ |
148
|
|
|
return file_get_contents('php://input'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns a Cookie Iterator. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getCookies() |
157
|
|
|
{ |
158
|
|
|
return $this->cookies; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns a File Iterator. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getFiles() |
167
|
|
|
{ |
168
|
|
|
return $this->files; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* The URI which was given in order to access this page |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
* @throws MissingRequestMetaVariableException |
176
|
|
|
*/ |
177
|
|
|
public function getUri() |
178
|
|
|
{ |
179
|
|
|
return $this->getServerVariable('REQUEST_URI'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return just the path |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function getPath() |
188
|
|
|
{ |
189
|
|
|
return strtok($this->getServerVariable('REQUEST_URI'), '?'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Which request method was used to access the page; |
194
|
|
|
* i.e. 'GET', 'HEAD', 'POST', 'PUT'. |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
* @throws MissingRequestMetaVariableException |
198
|
|
|
*/ |
199
|
|
|
public function getMethod() |
200
|
|
|
{ |
201
|
|
|
return $this->getServerVariable('REQUEST_METHOD'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Contents of the Accept: header from the current request, if there is one. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
* @throws MissingRequestMetaVariableException |
209
|
|
|
*/ |
210
|
|
|
public function getHttpAccept() |
211
|
|
|
{ |
212
|
|
|
return $this->getServerVariable('HTTP_ACCEPT'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* The address of the page (if any) which referred the user agent to the |
217
|
|
|
* current page. |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
* @throws MissingRequestMetaVariableException |
221
|
|
|
*/ |
222
|
|
|
public function getReferer() |
223
|
|
|
{ |
224
|
|
|
return $this->getServerVariable('HTTP_REFERER'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Content of the User-Agent header from the request, if there is one. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
* @throws MissingRequestMetaVariableException |
232
|
|
|
*/ |
233
|
|
|
public function getUserAgent() |
234
|
|
|
{ |
235
|
|
|
return $this->getServerVariable('HTTP_USER_AGENT'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* The IP address from which the user is viewing the current page. |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
* @throws MissingRequestMetaVariableException |
243
|
|
|
*/ |
244
|
|
|
public function getIpAddress() |
245
|
|
|
{ |
246
|
|
|
return $this->getServerVariable('REMOTE_ADDR'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Checks to see whether the current request is using HTTPS. |
251
|
|
|
* |
252
|
|
|
* @return boolean |
253
|
|
|
*/ |
254
|
|
|
public function isSecure() |
255
|
|
|
{ |
256
|
|
|
return (array_key_exists('HTTPS', $this->server) |
257
|
|
|
&& $this->server['HTTPS'] !== 'off' |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* The query string, if any, via which the page was accessed. |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
* @throws MissingRequestMetaVariableException |
266
|
|
|
*/ |
267
|
|
|
public function getQueryString() |
268
|
|
|
{ |
269
|
|
|
return $this->getServerVariable('QUERY_STRING'); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
private function getServerVariable($key) |
273
|
|
|
{ |
274
|
|
|
if (!array_key_exists($key, $this->server)) { |
275
|
|
|
throw new MissingRequestMetaVariableException($key); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $this->server[$key]; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|