1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\Route; 23use Aura\Router\RouterContainer; 24use Aura\Router\Rule\Accepts; 25use Aura\Router\Rule\Allows; 26use Fig\Http\Message\StatusCodeInterface; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\ModuleService; 29use Fisharebest\Webtrees\Services\TreeService; 30use Fisharebest\Webtrees\Tree; 31use Fisharebest\Webtrees\Validator; 32use Fisharebest\Webtrees\Webtrees; 33use Psr\Http\Message\ResponseInterface; 34use Psr\Http\Message\ServerRequestInterface; 35use Psr\Http\Server\MiddlewareInterface; 36use Psr\Http\Server\RequestHandlerInterface; 37 38use function explode; 39use function implode; 40use function str_contains; 41 42/** 43 * Simple class to help migrate to a third-party routing library. 44 */ 45class Router implements MiddlewareInterface 46{ 47 private ModuleService $module_service; 48 49 private RouterContainer $router_container; 50 51 private TreeService $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( 61 ModuleService $module_service, 62 RouterContainer $router_container, 63 TreeService $tree_service 64 ) { 65 $this->module_service = $module_service; 66 $this->router_container = $router_container; 67 $this->tree_service = $tree_service; 68 } 69 70 /** 71 * @param ServerRequestInterface $request 72 * @param RequestHandlerInterface $handler 73 * 74 * @return ResponseInterface 75 */ 76 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 77 { 78 // Ugly URLs store the path in a query parameter. 79 $url_route = Validator::queryParams($request)->string('route', ''); 80 81 if (Validator::attributes($request)->boolean('rewrite_urls', false)) { 82 // We are creating pretty URLs, but received an ugly one. Probably a search-engine. Redirect it. 83 if ($url_route !== '') { 84 $uri = $request->getUri() 85 ->withPath($url_route) 86 ->withQuery(explode('&', $request->getUri()->getQuery(), 2)[1] ?? ''); 87 88 return Registry::responseFactory()->redirectUrl($uri, StatusCodeInterface::STATUS_PERMANENT_REDIRECT); 89 } 90 91 $pretty = $request; 92 } else { 93 // Turn the ugly URL into a pretty one, so the router can parse it. 94 $uri = $request->getUri()->withPath($url_route); 95 $pretty = $request->withUri($uri); 96 } 97 98 // Match the request to a route. 99 $matcher = $this->router_container->getMatcher(); 100 $route = $matcher->match($pretty); 101 102 // No route matched? 103 if ($route === false) { 104 $failed_route = $matcher->getFailedRoute(); 105 106 if ($failed_route instanceof Route) { 107 if ($failed_route->failedRule === Allows::class) { 108 return Registry::responseFactory()->response('', StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED, [ 109 'Allow' => implode(', ', $failed_route->allows), 110 ]); 111 } 112 113 if ($failed_route->failedRule === Accepts::class) { 114 return Registry::responseFactory()->response('Negotiation failed', StatusCodeInterface::STATUS_NOT_ACCEPTABLE); 115 } 116 } 117 118 // Not found 119 return $handler->handle($request); 120 } 121 122 // Add the route as attribute of the request 123 $request = $request->withAttribute('route', $route); 124 125 // This middleware cannot run until after the routing, as it needs to know the route. 126 $post_routing_middleware = [CheckCsrf::class]; 127 128 // Firstly, apply the route middleware 129 $route_middleware = $route->extras['middleware'] ?? []; 130 131 // Secondly, apply any module middleware 132 $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); 133 134 // Finally, run the handler using middleware 135 $handler_middleware = [RequestHandler::class]; 136 137 $middleware = array_merge( 138 $post_routing_middleware, 139 $route_middleware, 140 $module_middleware, 141 $handler_middleware 142 ); 143 144 // Add the matched attributes to the request. 145 foreach ($route->attributes as $key => $value) { 146 if ($key === 'tree') { 147 $value = $this->tree_service->all()->get($value); 148 149 if ($value instanceof Tree) { 150 Registry::container()->set(Tree::class, $value); 151 } 152 153 // Missing mandatory parameter? Let the default handler take care of it. 154 if ($value === null && str_contains($route->path, '{tree}')) { 155 return $handler->handle($request); 156 } 157 } 158 159 $request = $request->withAttribute((string) $key, $value); 160 } 161 162 // Bind the updated request into the container 163 Registry::container()->set(ServerRequestInterface::class, $request); 164 165 return Webtrees::dispatch($request, $middleware); 166 } 167} 168