xref: /webtrees/app/Http/Middleware/BaseUrl.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
14874f72dSGreg Roach<?php
24874f72dSGreg Roach
34874f72dSGreg Roach/**
44874f72dSGreg Roach * webtrees: online genealogy
54874f72dSGreg Roach * Copyright (C) 2019 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
154874f72dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
164874f72dSGreg Roach */
17*fcfa147eSGreg Roach
184874f72dSGreg Roachdeclare(strict_types=1);
194874f72dSGreg Roach
204874f72dSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
214874f72dSGreg Roach
224874f72dSGreg Roachuse Psr\Http\Message\ResponseInterface;
234874f72dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
244874f72dSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
254874f72dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
264e339e11SGreg Roach
274874f72dSGreg Roachuse function explode;
284874f72dSGreg Roachuse function parse_url;
294874f72dSGreg Roachuse function rtrim;
304e339e11SGreg Roach
314874f72dSGreg Roachuse const PHP_URL_HOST;
324874f72dSGreg Roachuse const PHP_URL_PORT;
334874f72dSGreg Roachuse const PHP_URL_SCHEME;
344874f72dSGreg Roach
354874f72dSGreg Roach/**
364874f72dSGreg Roach * Middleware to set the base URL.
374874f72dSGreg Roach */
384874f72dSGreg Roachclass BaseUrl implements MiddlewareInterface
394874f72dSGreg Roach{
404874f72dSGreg Roach    /**
414874f72dSGreg Roach     * @param ServerRequestInterface  $request
424874f72dSGreg Roach     * @param RequestHandlerInterface $handler
434874f72dSGreg Roach     *
444874f72dSGreg Roach     * @return ResponseInterface
454874f72dSGreg Roach     */
464874f72dSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
474874f72dSGreg Roach    {
484874f72dSGreg Roach        // The request URL, as auto-detected from the environment.
494874f72dSGreg Roach        $request_url = $request->getUri();
504874f72dSGreg Roach
514874f72dSGreg Roach        // The base URL, as specified in the configuration file.
524874f72dSGreg Roach        $base_url = $request->getAttribute('base_url', '');
534874f72dSGreg Roach
544874f72dSGreg Roach        if ($base_url === '') {
554874f72dSGreg Roach            // Guess the base URL from the request URL.
56808caab3SGreg Roach            $base_url = rtrim(explode('index.php', (string) $request_url)[0], '/');
574874f72dSGreg Roach            $request  = $request->withAttribute('base_url', $base_url);
584874f72dSGreg Roach        } else {
594874f72dSGreg Roach            // Update the request URL from the base URL.
604874f72dSGreg Roach            $base_scheme = parse_url($base_url, PHP_URL_SCHEME) ?? 'http';
614874f72dSGreg Roach            $base_host   = parse_url($base_url, PHP_URL_HOST) ?? 'localhost';
624874f72dSGreg Roach            $base_port   = parse_url($base_url, PHP_URL_PORT);
634874f72dSGreg Roach
644874f72dSGreg Roach            $request_url = $request_url
654874f72dSGreg Roach                ->withScheme($base_scheme)
664874f72dSGreg Roach                ->withHost($base_host)
67c4f95a68SGreg Roach                ->withPort($base_port === null ? null : (int) $base_port);
684874f72dSGreg Roach
694874f72dSGreg Roach            $request = $request->withUri($request_url);
704874f72dSGreg Roach        }
714874f72dSGreg Roach
724874f72dSGreg Roach        return $handler->handle($request);
734874f72dSGreg Roach    }
744874f72dSGreg Roach}
75