xref: /webtrees/app/Http/Middleware/BaseUrl.php (revision d11be7027e34e3121be11cc025421873364403f9)
14874f72dSGreg Roach<?php
24874f72dSGreg Roach
34874f72dSGreg Roach/**
44874f72dSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
64874f72dSGreg Roach * This program is free software: you can redistribute it and/or modify
74874f72dSGreg Roach * it under the terms of the GNU General Public License as published by
84874f72dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
94874f72dSGreg Roach * (at your option) any later version.
104874f72dSGreg Roach * This program is distributed in the hope that it will be useful,
114874f72dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124874f72dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134874f72dSGreg Roach * GNU General Public License for more details.
144874f72dSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164874f72dSGreg Roach */
17fcfa147eSGreg Roach
184874f72dSGreg Roachdeclare(strict_types=1);
194874f72dSGreg Roach
204874f72dSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
214874f72dSGreg Roach
22b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
234874f72dSGreg Roachuse Psr\Http\Message\ResponseInterface;
244874f72dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
254874f72dSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
264874f72dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
274e339e11SGreg Roach
284874f72dSGreg Roachuse function explode;
294874f72dSGreg Roachuse function parse_url;
304874f72dSGreg Roachuse function rtrim;
314e339e11SGreg Roach
324874f72dSGreg Roachuse const PHP_URL_HOST;
332e4f3c66SGreg Roachuse const PHP_URL_PATH;
344874f72dSGreg Roachuse const PHP_URL_PORT;
354874f72dSGreg Roachuse const PHP_URL_SCHEME;
364874f72dSGreg Roach
374874f72dSGreg Roach/**
384874f72dSGreg Roach * Middleware to set the base URL.
394874f72dSGreg Roach */
404874f72dSGreg Roachclass BaseUrl implements MiddlewareInterface
414874f72dSGreg Roach{
424874f72dSGreg Roach    /**
434874f72dSGreg Roach     * @param ServerRequestInterface  $request
444874f72dSGreg Roach     * @param RequestHandlerInterface $handler
454874f72dSGreg Roach     *
464874f72dSGreg Roach     * @return ResponseInterface
474874f72dSGreg Roach     */
484874f72dSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
494874f72dSGreg Roach    {
504874f72dSGreg Roach        // The request URL, as auto-detected from the environment.
514874f72dSGreg Roach        $request_url = $request->getUri();
524874f72dSGreg Roach
534874f72dSGreg Roach        // The base URL, as specified in the configuration file.
54d5ee013aSGreg Roach        $base_url = Validator::attributes($request)->string('base_url', '');
554874f72dSGreg Roach
564874f72dSGreg Roach        if ($base_url === '') {
57d5ee013aSGreg Roach            // Not set in config.ini.php?  Didn't read the upgrade instructions?
58d5ee013aSGreg Roach            // We can guess the URL, provided we aren't using pretty URLs.
59808caab3SGreg Roach            $base_url    = rtrim(explode('index.php', (string) $request_url)[0], '/');
604874f72dSGreg Roach            $request     = $request->withAttribute('base_url', $base_url);
612e4f3c66SGreg Roach            $base_path   = parse_url($base_url, PHP_URL_PATH) ?? '';
622e4f3c66SGreg Roach            $request_url = $request_url->withPath($base_path);
634874f72dSGreg Roach        } else {
644874f72dSGreg Roach            // Update the request URL from the base URL.
654874f72dSGreg Roach            $base_scheme = parse_url($base_url, PHP_URL_SCHEME) ?? 'http';
664874f72dSGreg Roach            $base_host   = parse_url($base_url, PHP_URL_HOST) ?? 'localhost';
674874f72dSGreg Roach            $base_port   = parse_url($base_url, PHP_URL_PORT);
681821c9e5SGreg Roach            $request_url = $request_url->withScheme($base_scheme)->withHost($base_host)->withPort($base_port);
691821c9e5SGreg Roach        }
704874f72dSGreg Roach
714874f72dSGreg Roach        $request = $request->withUri($request_url);
724874f72dSGreg Roach
734874f72dSGreg Roach        return $handler->handle($request);
744874f72dSGreg Roach    }
754874f72dSGreg Roach}
76