1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fracture\Http; |
4
|
|
|
|
5
|
|
|
class Request implements Routable |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $acceptHeader = null; |
9
|
|
|
|
10
|
|
|
private $contentTypeHeader = null; |
11
|
|
|
|
12
|
|
|
private $method = null; |
13
|
|
|
|
14
|
|
|
private $parameters = []; |
15
|
|
|
|
16
|
|
|
private $files = null; |
17
|
|
|
|
18
|
|
|
private $cookies = []; |
19
|
|
|
|
20
|
|
|
private $fileBagBuilder = null; |
21
|
|
|
|
22
|
|
|
private $address = null; |
23
|
|
|
|
24
|
|
|
private $uri = null; |
25
|
|
|
|
26
|
|
|
|
27
|
8 |
|
public function __construct($fileBagBuilder = null) |
28
|
|
|
{ |
29
|
8 |
|
$this->fileBagBuilder = $fileBagBuilder; |
30
|
8 |
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
1 |
|
private function getResolvedAcceptHeader() |
34
|
|
|
{ |
35
|
1 |
|
$header = $this->acceptHeader; |
36
|
|
|
|
37
|
|
|
// lets you override the accept header value, |
38
|
|
|
// but probably will get removed in a foreseeable future |
39
|
1 |
|
if (array_key_exists('_accept', $this->parameters)) { |
40
|
1 |
|
$value = strtolower($this->parameters['_accept']); |
41
|
1 |
|
$header->setValue($value); |
42
|
1 |
|
$header->prepare(); |
43
|
1 |
|
unset( $this->parameters['_accept']); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return $header; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
1 |
|
public function prepare() |
51
|
|
|
{ |
52
|
1 |
|
$this->acceptHeader = $this->getResolvedAcceptHeader(); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
4 |
|
public function setParameters(array $list, $override = false) |
57
|
|
|
{ |
58
|
4 |
|
$duplicates = array_intersect_key($list, $this->parameters); |
59
|
|
|
|
60
|
|
|
// checks of parameters with overlapping names |
61
|
4 |
|
if (false === $override && count($duplicates) > 0) { |
62
|
1 |
|
$message = implode("', '", array_keys($duplicates)); |
63
|
1 |
|
$message = "You are trying to override following parameter(s): '$message'"; |
64
|
1 |
|
trigger_error($message, \E_USER_WARNING); |
65
|
|
|
} |
66
|
4 |
|
$this->parameters = $list + $this->parameters; |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
3 |
|
public function getParameter($name) |
71
|
|
|
{ |
72
|
3 |
|
if (array_key_exists($name, $this->parameters)) { |
73
|
1 |
|
return $this->parameters[$name]; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
2 |
|
public function setMethod($value) |
81
|
|
|
{ |
82
|
2 |
|
$method = strtolower($value); |
83
|
2 |
|
if (in_array($method, ['get', 'post', 'put', 'delete', 'head', 'options', 'trace'])) { |
84
|
2 |
|
$this->method = $method; |
85
|
|
|
} |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
3 |
|
public function getMethod() |
90
|
|
|
{ |
91
|
3 |
|
return $this->method; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
2 |
|
public function setAcceptHeader(Headers\Accept $header) |
96
|
|
|
{ |
97
|
2 |
|
$this->acceptHeader = $header; |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
1 |
|
public function getAcceptHeader() |
102
|
|
|
{ |
103
|
1 |
|
return $this->acceptHeader; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
1 |
|
public function setContentTypeHeader(Headers\ContentType $header) |
108
|
|
|
{ |
109
|
1 |
|
$this->contentTypeHeader = $header; |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
1 |
|
public function getContentTypeHeader() |
114
|
|
|
{ |
115
|
1 |
|
return $this->contentTypeHeader; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
2 |
|
public function setUploadedFiles($list) |
120
|
|
|
{ |
121
|
2 |
|
if ($this->fileBagBuilder !== null) { |
122
|
1 |
|
$list = $this->fileBagBuilder->create($list); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
$this->files = $list; |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
2 |
|
public function getUpload($name) |
130
|
|
|
{ |
131
|
2 |
|
if (isset($this->files[$name])) { |
132
|
1 |
|
return $this->files[$name]; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
2 |
|
public function addCookie($name, $value) |
140
|
|
|
{ |
141
|
2 |
|
$this->cookies[$name] = $value; |
142
|
2 |
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
1 |
|
public function getCookie($name) |
146
|
|
|
{ |
147
|
1 |
|
if (array_key_exists($name, $this->cookies)) { |
148
|
1 |
|
return $this->cookies[$name]; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
1 |
|
public function getAllCookies() |
156
|
|
|
{ |
157
|
1 |
|
return $this->cookies; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
19 |
|
protected function resolveUri($uri) |
162
|
|
|
{ |
163
|
19 |
|
$parts = explode('/', $uri); |
164
|
19 |
|
$segments = []; |
165
|
19 |
|
foreach ($parts as $element) { |
166
|
19 |
|
$segments = $this->adjustUriSegments($segments, $element); |
167
|
|
|
} |
168
|
19 |
|
return implode('/', $segments); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Method for handling '../' in URL query |
174
|
|
|
*/ |
175
|
19 |
|
private function adjustUriSegments($list, $item) |
176
|
|
|
{ |
177
|
19 |
|
if ($item === '..') { |
178
|
4 |
|
array_pop($list); |
179
|
4 |
|
return $list; |
180
|
|
|
} |
181
|
18 |
|
array_push($list, $item); |
182
|
|
|
|
183
|
18 |
|
return $list; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
19 |
|
public function setUri($uri) |
188
|
|
|
{ |
189
|
19 |
|
$uri = $this->sanitizeUri($uri); |
190
|
19 |
|
$uri = $this->resolveUri($uri); |
191
|
19 |
|
$this->uri = '/' . $uri; |
192
|
19 |
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
19 |
|
private function sanitizeUri($uri) |
196
|
|
|
{ |
197
|
19 |
|
$uri = explode('?', $uri)[0]; |
198
|
|
|
// to remove './' at the start of $uri |
199
|
19 |
|
$uri = '/' . $uri; |
200
|
19 |
|
$uri = preg_replace(['#(/)+#', '#/(\./)+#'], '/', $uri); |
201
|
19 |
|
$uri = trim($uri, '/'); |
202
|
19 |
|
return $uri; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
19 |
|
public function getUri() |
207
|
|
|
{ |
208
|
19 |
|
return $this->uri; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $address |
214
|
|
|
*/ |
215
|
2 |
|
public function setAddress($address) |
216
|
|
|
{ |
217
|
2 |
|
if (filter_var($address, FILTER_VALIDATE_IP) === false) { |
218
|
1 |
|
$address = null; |
219
|
|
|
} |
220
|
|
|
|
221
|
2 |
|
$this->address = $address; |
222
|
2 |
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
2 |
|
public function getAddress() |
226
|
|
|
{ |
227
|
2 |
|
return $this->address; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|