19e5d8e6fSGreg Roach<?php 29e5d8e6fSGreg Roach 39e5d8e6fSGreg Roach/** 49e5d8e6fSGreg Roach * webtrees: online genealogy 59e5d8e6fSGreg Roach * Copyright (C) 2019 webtrees development team 69e5d8e6fSGreg Roach * This program is free software: you can redistribute it and/or modify 79e5d8e6fSGreg Roach * it under the terms of the GNU General Public License as published by 89e5d8e6fSGreg Roach * the Free Software Foundation, either version 3 of the License, or 99e5d8e6fSGreg Roach * (at your option) any later version. 109e5d8e6fSGreg Roach * This program is distributed in the hope that it will be useful, 119e5d8e6fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 129e5d8e6fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 139e5d8e6fSGreg Roach * GNU General Public License for more details. 149e5d8e6fSGreg Roach * You should have received a copy of the GNU General Public License 159e5d8e6fSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 169e5d8e6fSGreg Roach */ 17fcfa147eSGreg Roach 189e5d8e6fSGreg Roachdeclare(strict_types=1); 199e5d8e6fSGreg Roach 209e5d8e6fSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 219e5d8e6fSGreg Roach 22ee4364daSGreg Roachuse Aura\Router\RouterContainer; 23f307429bSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 2482e92bfaSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 250c0910bfSGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 26ee4364daSGreg Roachuse Fisharebest\Webtrees\Tree; 2782e92bfaSGreg Roachuse Middleland\Dispatcher; 289e5d8e6fSGreg Roachuse Psr\Http\Message\ResponseInterface; 299e5d8e6fSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 309e5d8e6fSGreg Roachuse Psr\Http\Server\MiddlewareInterface; 319e5d8e6fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 320c0910bfSGreg Roach 339e5d8e6fSGreg Roachuse function app; 3482e92bfaSGreg Roachuse function array_map; 359e5d8e6fSGreg Roach 369e5d8e6fSGreg Roach/** 379e5d8e6fSGreg Roach * Simple class to help migrate to a third-party routing library. 389e5d8e6fSGreg Roach */ 3971378461SGreg Roachclass Router implements MiddlewareInterface 409e5d8e6fSGreg Roach{ 4182e92bfaSGreg Roach /** @var ModuleService */ 4282e92bfaSGreg Roach private $module_service; 4382e92bfaSGreg Roach 443ecf8b4eSGreg Roach /** @var RouterContainer */ 453ecf8b4eSGreg Roach private $router_container; 463ecf8b4eSGreg Roach 470c0910bfSGreg Roach /** @var TreeService */ 480c0910bfSGreg Roach private $tree_service; 490c0910bfSGreg Roach 5082e92bfaSGreg Roach /** 5182e92bfaSGreg Roach * Router constructor. 5282e92bfaSGreg Roach * 5382e92bfaSGreg Roach * @param ModuleService $module_service 544874f72dSGreg Roach * @param RouterContainer $router_container 550c0910bfSGreg Roach * @param TreeService $tree_service 5682e92bfaSGreg Roach */ 570c0910bfSGreg Roach public function __construct(ModuleService $module_service, RouterContainer $router_container, TreeService $tree_service) 5882e92bfaSGreg Roach { 5982e92bfaSGreg Roach $this->module_service = $module_service; 603ecf8b4eSGreg Roach $this->router_container = $router_container; 610c0910bfSGreg Roach $this->tree_service = $tree_service; 629e5d8e6fSGreg Roach } 639e5d8e6fSGreg Roach 649e5d8e6fSGreg Roach /** 659e5d8e6fSGreg Roach * @param ServerRequestInterface $request 669e5d8e6fSGreg Roach * @param RequestHandlerInterface $handler 679e5d8e6fSGreg Roach * 689e5d8e6fSGreg Roach * @return ResponseInterface 699e5d8e6fSGreg Roach */ 709e5d8e6fSGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 719e5d8e6fSGreg Roach { 724874f72dSGreg Roach // Turn the ugly URL into a pretty one, so the router can parse it. 738753199aSGreg Roach $pretty = $request; 748753199aSGreg Roach 7554a7c588SGreg Roach if ($request->getAttribute('rewrite_urls') !== '1') { 768753199aSGreg Roach // Ugly URLs store the path in a query parameter. 778753199aSGreg Roach $url_route = $request->getQueryParams()['route'] ?? ''; 788753199aSGreg Roach $uri = $request->getUri()->withPath($url_route); 798753199aSGreg Roach $pretty = $request->withUri($uri); 80ee4364daSGreg Roach } 819e5d8e6fSGreg Roach 823ecf8b4eSGreg Roach // Match the request to a route. 838753199aSGreg Roach $route = $this->router_container->getMatcher()->match($pretty); 843ecf8b4eSGreg Roach 85f307429bSGreg Roach // No route matched? Let the default handler take care of it 8615958f0eSGreg Roach if ($route === false) { 879e5d8e6fSGreg Roach return $handler->handle($request); 889e5d8e6fSGreg Roach } 899e5d8e6fSGreg Roach 90f307429bSGreg Roach // Add the route as attribute of the request 91f307429bSGreg Roach $request = $request->withAttribute('route', $route); 92f307429bSGreg Roach 93*5d3d7b86SGreg Roach // This middleware cannot run until after the routing, as it needs to know the route. 94f307429bSGreg Roach $post_routing_middleware = [CheckCsrf::class]; 95f307429bSGreg Roach $post_routing_middleware = array_map('app', $post_routing_middleware); 96f307429bSGreg Roach 9782e92bfaSGreg Roach // Firstly, apply the route middleware 98ee4364daSGreg Roach $route_middleware = $route->extras['middleware'] ?? []; 9982e92bfaSGreg Roach $route_middleware = array_map('app', $route_middleware); 1009e5d8e6fSGreg Roach 10182e92bfaSGreg Roach // Secondly, apply any module middleware 10282e92bfaSGreg Roach $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); 1039e5d8e6fSGreg Roach 10482e92bfaSGreg Roach // Finally, run the handler using middleware 105ee4364daSGreg Roach $handler_middleware = [new WrapHandler($route->handler)]; 10682e92bfaSGreg Roach 107f307429bSGreg Roach $middleware = array_merge( 108f307429bSGreg Roach $post_routing_middleware, 109f307429bSGreg Roach $route_middleware, 110f307429bSGreg Roach $module_middleware, 111f307429bSGreg Roach $handler_middleware 112f307429bSGreg Roach ); 113f307429bSGreg Roach 114f307429bSGreg Roach // Add the matched attributes to the request. 115f307429bSGreg Roach foreach ($route->attributes as $key => $value) { 116f307429bSGreg Roach if ($key === 'tree') { 117f307429bSGreg Roach $value = $this->tree_service->all()->get($value); 118f307429bSGreg Roach app()->instance(Tree::class, $value); 119f307429bSGreg Roach 120f307429bSGreg Roach // Missing mandatory parameter? Let the default handler take care of it. 121f307429bSGreg Roach if ($value === null && strpos($route->path, '{tree}') !== false) { 122f307429bSGreg Roach return $handler->handle($request); 123f307429bSGreg Roach } 124f307429bSGreg Roach } 125f307429bSGreg Roach 126f307429bSGreg Roach $request = $request->withAttribute((string) $key, $value); 127f307429bSGreg Roach } 128f307429bSGreg Roach 129f307429bSGreg Roach // Bind the request into the container 130f307429bSGreg Roach app()->instance(ServerRequestInterface::class, $request); 13182e92bfaSGreg Roach 13282e92bfaSGreg Roach $dispatcher = new Dispatcher($middleware, app()); 13382e92bfaSGreg Roach 13482e92bfaSGreg Roach return $dispatcher->dispatch($request); 1359e5d8e6fSGreg Roach } 1369e5d8e6fSGreg Roach} 137