Passed
Push — master ( 21cf99...2fc355 )
by William
10:56
created

classes/Http/Factory/ServerRequestFactory.php (2 issues)

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;
0 ignored issues
show
The type Slim\Psr7\Factory\ServerRequestFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Slim\Psr7\Factory\UriFactory as SlimUriFactory;
0 ignored issues
show
The type Slim\Psr7\Factory\UriFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 10
    public function __construct(
43
        ServerRequestFactoryInterface|null $serverRequestFactory = null,
44
        UriFactoryInterface|null $uriFactory = null,
45
    ) {
46 10
        $this->serverRequestFactory = $serverRequestFactory ?? $this->createServerRequestFactory();
47 10
        $this->uriFactory = $uriFactory ?? $this->createUriFactory();
48
    }
49
50 6
    private function createServerRequestFactory(): ServerRequestFactoryInterface
51
    {
52 6
        if (class_exists(Psr17Factory::class)) {
53
            /** @var ServerRequestFactoryInterface $factory */
54 2
            $factory = new Psr17Factory();
55 4
        } elseif (class_exists(HttpFactory::class)) {
56
            /** @var ServerRequestFactoryInterface $factory */
57 2
            $factory = new HttpFactory();
58 2
        } elseif (class_exists(LaminasServerRequestFactory::class)) {
59
            /** @var ServerRequestFactoryInterface $factory */
60 1
            $factory = new LaminasServerRequestFactory();
61
        } else {
62 1
            $factory = new SlimServerRequestFactory();
63
        }
64
65 6
        return $factory;
66
    }
67
68 10
    private function createUriFactory(): UriFactoryInterface
69
    {
70 10
        if (class_exists(Psr17Factory::class)) {
71
            /** @var UriFactoryInterface $factory */
72 3
            $factory = new Psr17Factory();
73 7
        } elseif (class_exists(HttpFactory::class)) {
74
            /** @var UriFactoryInterface $factory */
75 3
            $factory = new HttpFactory();
76 4
        } elseif (class_exists(LaminasUriFactory::class)) {
77
            /** @var UriFactoryInterface $factory */
78 2
            $factory = new LaminasUriFactory();
79
        } else {
80 2
            $factory = new SlimUriFactory();
81
        }
82
83 10
        return $factory;
84
    }
85
86 4
    public static function createFromGlobals(): ServerRequest
87
    {
88 4
        if (class_exists(SlimServerRequestFactory::class)) {
89
            /** @psalm-suppress InternalMethod */
90 1
            $serverRequest = SlimServerRequestFactory::createFromGlobals();
91 3
        } elseif (class_exists(LaminasServerRequestFactory::class)) {
92
            /** @var ServerRequestInterface $serverRequest */
93 1
            $serverRequest = LaminasServerRequestFactory::fromGlobals();
94
        } else {
95 2
            $creator = new self();
96 2
            $serverRequest = self::createServerRequestFromGlobals($creator);
97
        }
98
99 4
        return new ServerRequest($serverRequest);
100
    }
101
102
    /** @return array<string, string> */
103 2
    protected function getallheaders(): array
104
    {
105
        /** @var array<string, string> $headers */
106 2
        $headers = function_exists('getallheaders') ? getallheaders() : [];
107
108 2
        return $headers;
109
    }
110
111 6
    private static function createServerRequestFromGlobals(self $creator): ServerRequestInterface
112
    {
113 6
        $serverRequest = $creator->serverRequestFactory->createServerRequest(
114 6
            $_SERVER['REQUEST_METHOD'] ?? 'GET',
115 6
            $creator->createUriFromGlobals($_SERVER),
116 6
            $_SERVER,
117 6
        );
118
119 6
        foreach ($creator->getallheaders() as $name => $value) {
120 5
            $serverRequest = $serverRequest->withAddedHeader($name, $value);
121
        }
122
123 6
        $serverRequest = $serverRequest->withQueryParams($_GET);
124
125 6
        if ($serverRequest->getMethod() !== 'POST') {
126 2
            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 6
    private function createUriFromGlobals(array $server): UriInterface
149
    {
150 6
        $uri = $this->uriFactory->createUri('');
151
152 6
        $uri = $uri->withScheme(! isset($server['HTTPS']) || $server['HTTPS'] === 'off' ? 'http' : 'https');
153
154 6
        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 6
        if (isset($server['HTTP_HOST']) && is_string($server['HTTP_HOST'])) {
162 6
            $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 6
        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 6
            $uri = $uri->withPort($uri->getScheme() === 'https' ? 443 : 80);
171
        }
172
173 6
        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 6
            $pos = strpos($uri->getHost(), ':');
180 6
            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 6
        if (isset($server['QUERY_STRING']) && is_string($server['QUERY_STRING'])) {
188 6
            $uri = $uri->withQuery($server['QUERY_STRING']);
189
        }
190
191 6
        if (isset($server['REQUEST_URI']) && is_string($server['REQUEST_URI'])) {
192 6
            $uriFragments = explode('?', $server['REQUEST_URI']);
193 6
            $uri = $uri->withPath($uriFragments[0]);
194 6
            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 6
        return $uri;
203
    }
204
}
205