1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\Http; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
use Psr\Http\Message\UriInterface; |
10
|
|
|
|
11
|
|
|
use function is_array; |
12
|
|
|
use function is_object; |
13
|
|
|
use function is_string; |
14
|
|
|
use function property_exists; |
15
|
|
|
|
16
|
|
|
class ServerRequest implements ServerRequestInterface |
17
|
|
|
{ |
18
|
|
|
/** @var ServerRequestInterface */ |
19
|
|
|
private $serverRequest; |
20
|
|
|
|
21
|
|
|
final public function __construct(ServerRequestInterface $serverRequest) |
22
|
|
|
{ |
23
|
|
|
$this->serverRequest = $serverRequest; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function getProtocolVersion() |
30
|
|
|
{ |
31
|
|
|
return $this->serverRequest->getProtocolVersion(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
|
|
public function withProtocolVersion($version) |
38
|
|
|
{ |
39
|
|
|
$serverRequest = $this->serverRequest->withProtocolVersion($version); |
40
|
|
|
|
41
|
|
|
return new static($serverRequest); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function getHeaders() |
48
|
|
|
{ |
49
|
|
|
return $this->serverRequest->getHeaders(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
|
|
public function hasHeader($name) |
56
|
|
|
{ |
57
|
|
|
return $this->serverRequest->hasHeader($name); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function getHeader($name) |
64
|
|
|
{ |
65
|
|
|
return $this->serverRequest->getHeader($name); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function getHeaderLine($name) |
72
|
|
|
{ |
73
|
|
|
return $this->serverRequest->getHeaderLine($name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function withHeader($name, $value) |
80
|
|
|
{ |
81
|
|
|
$serverRequest = $this->serverRequest->withHeader($name, $value); |
82
|
|
|
|
83
|
|
|
return new static($serverRequest); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function withAddedHeader($name, $value) |
90
|
|
|
{ |
91
|
|
|
$serverRequest = $this->serverRequest->withAddedHeader($name, $value); |
92
|
|
|
|
93
|
|
|
return new static($serverRequest); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function withoutHeader($name) |
100
|
|
|
{ |
101
|
|
|
$serverRequest = $this->serverRequest->withoutHeader($name); |
102
|
|
|
|
103
|
|
|
return new static($serverRequest); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function getBody() |
110
|
|
|
{ |
111
|
|
|
return $this->serverRequest->getBody(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
|
|
public function withBody(StreamInterface $body) |
118
|
|
|
{ |
119
|
|
|
$serverRequest = $this->serverRequest->withBody($body); |
120
|
|
|
|
121
|
|
|
return new static($serverRequest); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritDoc |
126
|
|
|
*/ |
127
|
|
|
public function getRequestTarget() |
128
|
|
|
{ |
129
|
|
|
return $this->serverRequest->getRequestTarget(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
|
|
public function withRequestTarget($requestTarget) |
136
|
|
|
{ |
137
|
|
|
$serverRequest = $this->serverRequest->withRequestTarget($requestTarget); |
138
|
|
|
|
139
|
|
|
return new static($serverRequest); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
|
|
public function getMethod() |
146
|
|
|
{ |
147
|
|
|
return $this->serverRequest->getMethod(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritDoc |
152
|
|
|
*/ |
153
|
|
|
public function withMethod($method) |
154
|
|
|
{ |
155
|
|
|
$serverRequest = $this->serverRequest->withMethod($method); |
156
|
|
|
|
157
|
|
|
return new static($serverRequest); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritDoc |
162
|
|
|
*/ |
163
|
|
|
public function getUri() |
164
|
|
|
{ |
165
|
|
|
return $this->serverRequest->getUri(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @inheritDoc |
170
|
|
|
*/ |
171
|
|
|
public function withUri(UriInterface $uri, $preserveHost = false) |
172
|
|
|
{ |
173
|
|
|
$serverRequest = $this->serverRequest->withUri($uri, $preserveHost); |
174
|
|
|
|
175
|
|
|
return new static($serverRequest); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritDoc |
180
|
|
|
*/ |
181
|
|
|
public function getServerParams() |
182
|
|
|
{ |
183
|
|
|
return $this->serverRequest->getServerParams(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function getCookieParams() |
190
|
|
|
{ |
191
|
|
|
return $this->serverRequest->getCookieParams(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
|
|
public function withCookieParams(array $cookies) |
198
|
|
|
{ |
199
|
|
|
$serverRequest = $this->serverRequest->withCookieParams($cookies); |
200
|
|
|
|
201
|
|
|
return new static($serverRequest); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritDoc |
206
|
|
|
*/ |
207
|
|
|
public function getQueryParams() |
208
|
|
|
{ |
209
|
|
|
return $this->serverRequest->getQueryParams(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritDoc |
214
|
|
|
*/ |
215
|
|
|
public function withQueryParams(array $query) |
216
|
|
|
{ |
217
|
|
|
$serverRequest = $this->serverRequest->withQueryParams($query); |
218
|
|
|
|
219
|
|
|
return new static($serverRequest); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @inheritDoc |
224
|
|
|
*/ |
225
|
|
|
public function getUploadedFiles() |
226
|
|
|
{ |
227
|
|
|
return $this->serverRequest->getUploadedFiles(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @inheritDoc |
232
|
|
|
*/ |
233
|
|
|
public function withUploadedFiles(array $uploadedFiles) |
234
|
|
|
{ |
235
|
|
|
$serverRequest = $this->serverRequest->withUploadedFiles($uploadedFiles); |
236
|
|
|
|
237
|
|
|
return new static($serverRequest); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @inheritDoc |
242
|
|
|
*/ |
243
|
|
|
public function getParsedBody() |
244
|
|
|
{ |
245
|
|
|
return $this->serverRequest->getParsedBody(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @inheritDoc |
250
|
|
|
*/ |
251
|
|
|
public function withParsedBody($data) |
252
|
|
|
{ |
253
|
|
|
$serverRequest = $this->serverRequest->withParsedBody($data); |
254
|
|
|
|
255
|
|
|
return new static($serverRequest); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @inheritDoc |
260
|
|
|
*/ |
261
|
|
|
public function getAttributes() |
262
|
|
|
{ |
263
|
|
|
return $this->serverRequest->getAttributes(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @inheritDoc |
268
|
|
|
*/ |
269
|
|
|
public function getAttribute($name, $default = null) |
270
|
|
|
{ |
271
|
|
|
return $this->serverRequest->getAttribute($name, $default); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @inheritDoc |
276
|
|
|
*/ |
277
|
|
|
public function withAttribute($name, $value) |
278
|
|
|
{ |
279
|
|
|
$serverRequest = $this->serverRequest->withAttribute($name, $value); |
280
|
|
|
|
281
|
|
|
return new static($serverRequest); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @inheritDoc |
286
|
|
|
*/ |
287
|
|
|
public function withoutAttribute($name) |
288
|
|
|
{ |
289
|
|
|
$serverRequest = $this->serverRequest->withoutAttribute($name); |
290
|
|
|
|
291
|
|
|
return new static($serverRequest); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param mixed $default |
296
|
|
|
* |
297
|
|
|
* @return mixed |
298
|
|
|
*/ |
299
|
|
|
public function getParam(string $param, $default = null) |
300
|
|
|
{ |
301
|
|
|
$getParams = $this->getQueryParams(); |
302
|
|
|
$postParams = $this->getParsedBody(); |
303
|
|
|
|
304
|
|
|
if (is_array($postParams) && isset($postParams[$param])) { |
305
|
|
|
return $postParams[$param]; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if (is_object($postParams) && property_exists($postParams, $param)) { |
309
|
|
|
return $postParams->$param; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if (isset($getParams[$param])) { |
313
|
|
|
return $getParams[$param]; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return $default; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param mixed $default |
321
|
|
|
* |
322
|
|
|
* @return mixed |
323
|
|
|
*/ |
324
|
|
|
public function getParsedBodyParam(string $param, $default = null) |
325
|
|
|
{ |
326
|
|
|
$postParams = $this->getParsedBody(); |
327
|
|
|
|
328
|
|
|
if (is_array($postParams) && isset($postParams[$param])) { |
329
|
|
|
return $postParams[$param]; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
if (is_object($postParams) && property_exists($postParams, $param)) { |
333
|
|
|
return $postParams->$param; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $default; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public function isPost(): bool |
340
|
|
|
{ |
341
|
|
|
return $this->getMethod() === 'POST'; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @psalm-return non-empty-string |
346
|
|
|
*/ |
347
|
|
|
public function getRoute(): string |
348
|
|
|
{ |
349
|
|
|
$getParams = $this->getQueryParams(); |
350
|
|
|
$postParams = $this->getParsedBody(); |
351
|
|
|
$route = '/'; |
352
|
|
|
if (isset($getParams['route']) && is_string($getParams['route']) && $getParams['route'] !== '') { |
353
|
|
|
$route = $getParams['route']; |
354
|
|
|
} elseif ( |
355
|
|
|
is_array($postParams) |
356
|
|
|
&& isset($postParams['route']) |
357
|
|
|
&& is_string($postParams['route']) |
358
|
|
|
&& $postParams['route'] !== '' |
359
|
|
|
) { |
360
|
|
|
$route = $postParams['route']; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* See FAQ 1.34. |
365
|
|
|
* |
366
|
|
|
* @see https://docs.phpmyadmin.net/en/latest/faq.html#faq1-34 |
367
|
|
|
*/ |
368
|
|
|
$db = isset($getParams['db']) && is_string($getParams['db']) ? $getParams['db'] : ''; |
369
|
|
|
if ($route === '/' && $db !== '') { |
370
|
|
|
$table = isset($getParams['table']) && is_string($getParams['table']) ? $getParams['table'] : ''; |
371
|
|
|
$route = $table === '' ? '/database/structure' : '/sql'; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return $route; |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|