1 | <?php |
||
2 | |||
3 | /** |
||
4 | * webtrees: online genealogy |
||
5 | * Copyright (C) 2021 webtrees development team |
||
6 | * This program is free software: you can redistribute it and/or modify |
||
7 | * it under the terms of the GNU General Public License as published by |
||
8 | * the Free Software Foundation, either version 3 of the License, or |
||
9 | * (at your option) any later version. |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU General Public License for more details. |
||
14 | * You should have received a copy of the GNU General Public License |
||
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | declare(strict_types=1); |
||
19 | |||
20 | namespace Fisharebest\Webtrees\Http\Middleware; |
||
21 | |||
22 | use Psr\Http\Message\ResponseInterface; |
||
23 | use Psr\Http\Message\ServerRequestInterface; |
||
24 | use Psr\Http\Server\MiddlewareInterface; |
||
25 | use Psr\Http\Server\RequestHandlerInterface; |
||
26 | |||
27 | use function explode; |
||
28 | use function parse_url; |
||
29 | use function rtrim; |
||
30 | |||
31 | use const PHP_URL_HOST; |
||
32 | use const PHP_URL_PATH; |
||
33 | use const PHP_URL_PORT; |
||
34 | use const PHP_URL_SCHEME; |
||
35 | |||
36 | /** |
||
37 | * Middleware to set the base URL. |
||
38 | */ |
||
39 | class BaseUrl implements MiddlewareInterface |
||
40 | { |
||
41 | /** |
||
42 | * @param ServerRequestInterface $request |
||
43 | * @param RequestHandlerInterface $handler |
||
44 | * |
||
45 | * @return ResponseInterface |
||
46 | */ |
||
47 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
48 | { |
||
49 | // The request URL, as auto-detected from the environment. |
||
50 | $request_url = $request->getUri(); |
||
51 | |||
52 | // The base URL, as specified in the configuration file. |
||
53 | $base_url = $request->getAttribute('base_url', ''); |
||
54 | |||
55 | if ($base_url === '') { |
||
56 | // Guess the base URL from the request URL. |
||
57 | $base_url = rtrim(explode('index.php', (string) $request_url)[0], '/'); |
||
58 | $request = $request->withAttribute('base_url', $base_url); |
||
59 | $base_path = parse_url($base_url, PHP_URL_PATH) ?? ''; |
||
60 | $request_url = $request_url->withPath($base_path); |
||
61 | |||
62 | $request = $request->withUri($request_url); |
||
63 | } else { |
||
64 | // Update the request URL from the base URL. |
||
65 | $base_scheme = parse_url($base_url, PHP_URL_SCHEME) ?? 'http'; |
||
66 | $base_host = parse_url($base_url, PHP_URL_HOST) ?? 'localhost'; |
||
67 | $base_port = parse_url($base_url, PHP_URL_PORT); |
||
68 | |||
69 | $request_url = $request_url |
||
70 | ->withScheme($base_scheme) |
||
71 | ->withHost($base_host) |
||
72 | ->withPort($base_port); |
||
1 ignored issue
–
show
Bug
introduced
by
Loading history...
|
|||
73 | |||
74 | $request = $request->withUri($request_url); |
||
75 | } |
||
76 | |||
77 | return $handler->handle($request); |
||
78 | } |
||
79 | } |
||
80 |