|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* webtrees: online genealogy |
|
5
|
|
|
* Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Fisharebest\Webtrees\Http\Middleware; |
|
20
|
|
|
|
|
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
24
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
25
|
|
|
use RuntimeException; |
|
26
|
|
|
use function explode; |
|
27
|
|
|
use function parse_url; |
|
28
|
|
|
use function preg_replace; |
|
29
|
|
|
use function rtrim; |
|
30
|
|
|
use const PHP_URL_HOST; |
|
31
|
|
|
use const PHP_URL_PATH; |
|
32
|
|
|
use const PHP_URL_PORT; |
|
33
|
|
|
use const PHP_URL_SCHEME; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Middleware to set the base URL. |
|
37
|
|
|
*/ |
|
38
|
|
|
class BaseUrl implements MiddlewareInterface |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @param ServerRequestInterface $request |
|
42
|
|
|
* @param RequestHandlerInterface $handler |
|
43
|
|
|
* |
|
44
|
|
|
* @return ResponseInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
47
|
|
|
{ |
|
48
|
|
|
// The request URL, as auto-detected from the environment. |
|
49
|
|
|
$request_url = $request->getUri(); |
|
50
|
|
|
|
|
51
|
|
|
// The base URL, as specified in the configuration file. |
|
52
|
|
|
$base_url = $request->getAttribute('base_url', ''); |
|
53
|
|
|
|
|
54
|
|
|
if ($base_url === '') { |
|
55
|
|
|
// Guess the base URL from the request URL. |
|
56
|
|
|
$base_url = rtrim('/', explode('index.php', (string) $request_url)[0]); |
|
57
|
|
|
$request = $request->withAttribute('base_url', $base_url); |
|
58
|
|
|
} else { |
|
59
|
|
|
// Update the request URL from the base URL. |
|
60
|
|
|
$base_scheme = parse_url($base_url, PHP_URL_SCHEME) ?? 'http'; |
|
61
|
|
|
$base_host = parse_url($base_url, PHP_URL_HOST) ?? 'localhost'; |
|
62
|
|
|
$base_port = parse_url($base_url, PHP_URL_PORT); |
|
63
|
|
|
|
|
64
|
|
|
$request_url = $request_url |
|
65
|
|
|
->withScheme($base_scheme) |
|
66
|
|
|
->withHost($base_host) |
|
67
|
|
|
->withPort($base_port === null ? null : (string) $base_port); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$request = $request->withUri($request_url); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $handler->handle($request); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|