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