1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\Http\Factory; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\HttpFactory; |
|
|
|
|
8
|
|
|
use Laminas\Diactoros\ServerRequestFactory as LaminasServerRequestFactory; |
|
|
|
|
9
|
|
|
use Laminas\Diactoros\UriFactory as LaminasUriFactory; |
|
|
|
|
10
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
11
|
|
|
use PhpMyAdmin\Http\ServerRequest; |
12
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
15
|
|
|
use Psr\Http\Message\UriInterface; |
16
|
|
|
use Slim\Psr7\Factory\ServerRequestFactory as SlimServerRequestFactory; |
|
|
|
|
17
|
|
|
use Slim\Psr7\Factory\UriFactory as SlimUriFactory; |
|
|
|
|
18
|
|
|
|
19
|
|
|
use function class_exists; |
20
|
|
|
use function count; |
21
|
|
|
use function current; |
22
|
|
|
use function explode; |
23
|
|
|
use function function_exists; |
24
|
|
|
use function getallheaders; |
25
|
|
|
use function in_array; |
26
|
|
|
use function is_numeric; |
27
|
|
|
use function is_string; |
28
|
|
|
use function parse_url; |
29
|
|
|
use function preg_match; |
30
|
|
|
use function strpos; |
31
|
|
|
use function strstr; |
32
|
|
|
use function substr; |
33
|
|
|
|
34
|
|
|
use const PHP_URL_QUERY; |
35
|
|
|
|
36
|
|
|
class ServerRequestFactory |
37
|
|
|
{ |
38
|
|
|
private ServerRequestFactoryInterface $serverRequestFactory; |
39
|
|
|
|
40
|
|
|
private UriFactoryInterface $uriFactory; |
41
|
|
|
|
42
|
11 |
|
public function __construct( |
43
|
|
|
ServerRequestFactoryInterface|null $serverRequestFactory = null, |
44
|
|
|
UriFactoryInterface|null $uriFactory = null, |
45
|
|
|
) { |
46
|
11 |
|
$this->serverRequestFactory = $serverRequestFactory ?? $this->createServerRequestFactory(); |
47
|
11 |
|
$this->uriFactory = $uriFactory ?? $this->createUriFactory(); |
48
|
|
|
} |
49
|
|
|
|
50
|
7 |
|
private function createServerRequestFactory(): ServerRequestFactoryInterface |
51
|
|
|
{ |
52
|
7 |
|
if (class_exists(Psr17Factory::class)) { |
53
|
|
|
/** @var ServerRequestFactoryInterface $factory */ |
54
|
4 |
|
$factory = new Psr17Factory(); |
55
|
3 |
|
} elseif (class_exists(HttpFactory::class)) { |
56
|
|
|
/** @var ServerRequestFactoryInterface $factory */ |
57
|
2 |
|
$factory = new HttpFactory(); |
58
|
1 |
|
} elseif (class_exists(LaminasServerRequestFactory::class)) { |
59
|
|
|
/** @var ServerRequestFactoryInterface $factory */ |
60
|
1 |
|
$factory = new LaminasServerRequestFactory(); |
61
|
|
|
} else { |
62
|
|
|
$factory = new SlimServerRequestFactory(); |
63
|
|
|
} |
64
|
|
|
|
65
|
7 |
|
return $factory; |
66
|
|
|
} |
67
|
|
|
|
68
|
11 |
|
private function createUriFactory(): UriFactoryInterface |
69
|
|
|
{ |
70
|
11 |
|
if (class_exists(Psr17Factory::class)) { |
71
|
|
|
/** @var UriFactoryInterface $factory */ |
72
|
6 |
|
$factory = new Psr17Factory(); |
73
|
5 |
|
} elseif (class_exists(HttpFactory::class)) { |
74
|
|
|
/** @var UriFactoryInterface $factory */ |
75
|
3 |
|
$factory = new HttpFactory(); |
76
|
2 |
|
} elseif (class_exists(LaminasUriFactory::class)) { |
77
|
|
|
/** @var UriFactoryInterface $factory */ |
78
|
2 |
|
$factory = new LaminasUriFactory(); |
79
|
|
|
} else { |
80
|
|
|
$factory = new SlimUriFactory(); |
81
|
|
|
} |
82
|
|
|
|
83
|
11 |
|
return $factory; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
public static function createFromGlobals(): ServerRequest |
87
|
|
|
{ |
88
|
4 |
|
if (class_exists(SlimServerRequestFactory::class)) { |
89
|
|
|
/** @psalm-suppress InternalMethod */ |
90
|
|
|
$serverRequest = SlimServerRequestFactory::createFromGlobals(); |
91
|
4 |
|
} elseif (class_exists(LaminasServerRequestFactory::class)) { |
92
|
|
|
/** @var ServerRequestInterface $serverRequest */ |
93
|
1 |
|
$serverRequest = LaminasServerRequestFactory::fromGlobals(); |
94
|
|
|
} else { |
95
|
3 |
|
$creator = new self(); |
96
|
3 |
|
$serverRequest = self::createServerRequestFromGlobals($creator); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
return new ServerRequest($serverRequest); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** @return array<string, string> */ |
103
|
3 |
|
protected function getallheaders(): array |
104
|
|
|
{ |
105
|
|
|
/** @var array<string, string> $headers */ |
106
|
3 |
|
$headers = function_exists('getallheaders') ? getallheaders() : []; |
107
|
|
|
|
108
|
3 |
|
return $headers; |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
private static function createServerRequestFromGlobals(self $creator): ServerRequestInterface |
112
|
|
|
{ |
113
|
7 |
|
$serverRequest = $creator->serverRequestFactory->createServerRequest( |
114
|
7 |
|
$_SERVER['REQUEST_METHOD'] ?? 'GET', |
115
|
7 |
|
$creator->createUriFromGlobals($_SERVER), |
116
|
7 |
|
$_SERVER, |
117
|
7 |
|
); |
118
|
|
|
|
119
|
7 |
|
foreach ($creator->getallheaders() as $name => $value) { |
120
|
5 |
|
$serverRequest = $serverRequest->withAddedHeader($name, $value); |
121
|
|
|
} |
122
|
|
|
|
123
|
7 |
|
$serverRequest = $serverRequest->withQueryParams($_GET); |
124
|
|
|
|
125
|
7 |
|
if ($serverRequest->getMethod() !== 'POST') { |
126
|
3 |
|
return $serverRequest; |
127
|
|
|
} |
128
|
|
|
|
129
|
4 |
|
$contentType = ''; |
130
|
4 |
|
foreach ($serverRequest->getHeader('Content-Type') as $headerValue) { |
131
|
4 |
|
$contentType = current(explode(';', $headerValue)); |
132
|
|
|
} |
133
|
|
|
|
134
|
4 |
|
if (in_array($contentType, ['application/x-www-form-urlencoded', 'multipart/form-data'], true)) { |
135
|
4 |
|
return $serverRequest->withParsedBody($_POST); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $serverRequest; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Create new Uri from environment. |
143
|
|
|
* |
144
|
|
|
* Initially based on the \Slim\Psr7\Factory\UriFactory::createFromGlobals() implementation. |
145
|
|
|
* |
146
|
|
|
* @param mixed[] $server |
147
|
|
|
*/ |
148
|
7 |
|
private function createUriFromGlobals(array $server): UriInterface |
149
|
|
|
{ |
150
|
7 |
|
$uri = $this->uriFactory->createUri(''); |
151
|
|
|
|
152
|
7 |
|
$uri = $uri->withScheme(! isset($server['HTTPS']) || $server['HTTPS'] === 'off' ? 'http' : 'https'); |
153
|
|
|
|
154
|
7 |
|
if (isset($server['PHP_AUTH_USER']) && is_string($server['PHP_AUTH_USER']) && $server['PHP_AUTH_USER'] !== '') { |
155
|
|
|
$uri = $uri->withUserInfo( |
156
|
|
|
$server['PHP_AUTH_USER'], |
157
|
|
|
isset($server['PHP_AUTH_PW']) && is_string($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : null, |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
7 |
|
if (isset($server['HTTP_HOST']) && is_string($server['HTTP_HOST'])) { |
162
|
7 |
|
$uri = $uri->withHost($server['HTTP_HOST']); |
163
|
|
|
} elseif (isset($server['SERVER_NAME']) && is_string($server['SERVER_NAME'])) { |
164
|
|
|
$uri = $uri->withHost($server['SERVER_NAME']); |
165
|
|
|
} |
166
|
|
|
|
167
|
7 |
|
if (isset($server['SERVER_PORT']) && is_numeric($server['SERVER_PORT']) && $server['SERVER_PORT'] >= 1) { |
168
|
|
|
$uri = $uri->withPort((int) $server['SERVER_PORT']); |
169
|
|
|
} else { |
170
|
7 |
|
$uri = $uri->withPort($uri->getScheme() === 'https' ? 443 : 80); |
171
|
|
|
} |
172
|
|
|
|
173
|
7 |
|
if (preg_match('/^(\[[a-fA-F0-9:.]+])(:\d+)?\z/', $uri->getHost(), $matches)) { |
174
|
|
|
$uri = $uri->withHost($matches[1]); |
175
|
|
|
if (isset($matches[2])) { |
176
|
|
|
$uri = $uri->withPort((int) substr($matches[2], 1)); |
177
|
|
|
} |
178
|
|
|
} else { |
179
|
7 |
|
$pos = strpos($uri->getHost(), ':'); |
180
|
7 |
|
if ($pos !== false) { |
181
|
|
|
$port = (int) substr($uri->getHost(), $pos + 1); |
182
|
|
|
$host = (string) strstr($uri->getHost(), ':', true); |
183
|
|
|
$uri = $uri->withHost($host)->withPort($port); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
7 |
|
if (isset($server['QUERY_STRING']) && is_string($server['QUERY_STRING'])) { |
188
|
7 |
|
$uri = $uri->withQuery($server['QUERY_STRING']); |
189
|
|
|
} |
190
|
|
|
|
191
|
7 |
|
if (isset($server['REQUEST_URI']) && is_string($server['REQUEST_URI'])) { |
192
|
7 |
|
$uriFragments = explode('?', $server['REQUEST_URI']); |
193
|
7 |
|
$uri = $uri->withPath($uriFragments[0]); |
194
|
7 |
|
if ($uri->getQuery() === '' && count($uriFragments) > 1) { |
195
|
|
|
$query = parse_url('https://www.example.com' . $server['REQUEST_URI'], PHP_URL_QUERY); |
196
|
|
|
if (is_string($query) && $query !== '') { |
197
|
|
|
$uri = $uri->withQuery($query); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
7 |
|
return $uri; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths