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