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