1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\Middleware; 21 22use Aura\Router\RouterContainer; 23use Aura\Router\Rule\Accepts; 24use Aura\Router\Rule\Allows; 25use Fig\Http\Message\StatusCodeInterface; 26use Fisharebest\Webtrees\Services\ModuleService; 27use Fisharebest\Webtrees\Services\TreeService; 28use Fisharebest\Webtrees\Tree; 29use Middleland\Dispatcher; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\MiddlewareInterface; 33use Psr\Http\Server\RequestHandlerInterface; 34 35use function app; 36use function response; 37use function str_contains; 38 39/** 40 * Simple class to help migrate to a third-party routing library. 41 */ 42class Router implements MiddlewareInterface 43{ 44 /** @var ModuleService */ 45 private $module_service; 46 47 /** @var RouterContainer */ 48 private $router_container; 49 50 /** @var TreeService */ 51 private $tree_service; 52 53 /** 54 * Router constructor. 55 * 56 * @param ModuleService $module_service 57 * @param RouterContainer $router_container 58 * @param TreeService $tree_service 59 */ 60 public function __construct(ModuleService $module_service, RouterContainer $router_container, TreeService $tree_service) 61 { 62 $this->module_service = $module_service; 63 $this->router_container = $router_container; 64 $this->tree_service = $tree_service; 65 } 66 67 /** 68 * @param ServerRequestInterface $request 69 * @param RequestHandlerInterface $handler 70 * 71 * @return ResponseInterface 72 */ 73 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 74 { 75 // Turn the ugly URL into a pretty one, so the router can parse it. 76 $pretty = $request; 77 78 if ($request->getAttribute('rewrite_urls') !== '1') { 79 // Ugly URLs store the path in a query parameter. 80 $url_route = $request->getQueryParams()['route'] ?? ''; 81 $uri = $request->getUri()->withPath($url_route); 82 $pretty = $request->withUri($uri); 83 } 84 85 // Match the request to a route. 86 $matcher = $this->router_container->getMatcher(); 87 $route = $matcher->match($pretty); 88 89 // No route matched? 90 if ($route === false) { 91 $failed_route = $matcher->getFailedRoute(); 92 93 switch ($failed_route->failedRule) { 94 case Allows::class: 95 return response('', StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED, [ 96 'Allow' => implode(', ', $failed_route->allows), 97 ]); 98 99 case Accepts::class: 100 // We don't use this, but modules might. 101 return response('Negotiation failed', StatusCodeInterface::STATUS_NOT_ACCEPTABLE); 102 103 default: 104 // Not found 105 return $handler->handle($request); 106 } 107 } 108 109 // Add the route as attribute of the request 110 $request = $request->withAttribute('route', $route); 111 112 // This middleware cannot run until after the routing, as it needs to know the route. 113 $post_routing_middleware = [CheckCsrf::class]; 114 115 // Firstly, apply the route middleware 116 $route_middleware = $route->extras['middleware'] ?? []; 117 118 // Secondly, apply any module middleware 119 $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); 120 121 // Finally, run the handler using middleware 122 $handler_middleware = [RequestHandler::class]; 123 124 $middleware = array_merge( 125 $post_routing_middleware, 126 $route_middleware, 127 $module_middleware, 128 $handler_middleware 129 ); 130 131 // Add the matched attributes to the request. 132 foreach ($route->attributes as $key => $value) { 133 if ($key === 'tree') { 134 $value = $this->tree_service->all()->get($value); 135 app()->instance(Tree::class, $value); 136 137 // Missing mandatory parameter? Let the default handler take care of it. 138 if ($value === null && str_contains($route->path, '{tree}')) { 139 return $handler->handle($request); 140 } 141 } 142 143 $request = $request->withAttribute((string) $key, $value); 144 } 145 146 // Bind the updated request into the container 147 app()->instance(ServerRequestInterface::class, $request); 148 149 $dispatcher = new Dispatcher($middleware, app()); 150 151 return $dispatcher->dispatch($request); 152 } 153} 154