1 | <?php |
||
2 | |||
3 | /** |
||
4 | * webtrees: online genealogy |
||
5 | * Copyright (C) 2025 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 | |||
18 | declare(strict_types=1); |
||
19 | |||
20 | namespace Fisharebest\Webtrees\Http\Middleware; |
||
21 | |||
22 | use Aura\Router\Route; |
||
23 | use Aura\Router\RouterContainer; |
||
24 | use Aura\Router\Rule\Accepts; |
||
25 | use Aura\Router\Rule\Allows; |
||
26 | use Fig\Http\Message\StatusCodeInterface; |
||
27 | use Fisharebest\Webtrees\Http\Dispatcher; |
||
0 ignored issues
–
show
|
|||
28 | use Fisharebest\Webtrees\Registry; |
||
29 | use Fisharebest\Webtrees\Services\ModuleService; |
||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Services\ModuleService was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
30 | use Fisharebest\Webtrees\Services\TreeService; |
||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Services\TreeService was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
31 | use Fisharebest\Webtrees\Tree; |
||
32 | use Fisharebest\Webtrees\Validator; |
||
33 | use Psr\Http\Message\ResponseInterface; |
||
34 | use Psr\Http\Message\ServerRequestInterface; |
||
35 | use Psr\Http\Server\MiddlewareInterface; |
||
36 | use Psr\Http\Server\RequestHandlerInterface; |
||
37 | |||
38 | use function explode; |
||
39 | use function implode; |
||
40 | use function str_contains; |
||
41 | |||
42 | /** |
||
43 | * Simple class to help migrate to a third-party routing library. |
||
44 | */ |
||
45 | readonly class Router implements MiddlewareInterface |
||
46 | { |
||
47 | public function __construct( |
||
48 | private ModuleService $module_service, |
||
49 | private RouterContainer $router_container, |
||
50 | private TreeService $tree_service |
||
51 | ) { |
||
52 | } |
||
53 | |||
54 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
55 | { |
||
56 | // Ugly URLs store the path in a query parameter. |
||
57 | $url_route = Validator::queryParams($request)->string('route', ''); |
||
58 | |||
59 | if (Validator::attributes($request)->boolean('rewrite_urls', false)) { |
||
60 | // We are creating pretty URLs, but received an ugly one. Probably a search-engine. Redirect it. |
||
61 | if ($url_route !== '') { |
||
62 | $uri = $request->getUri() |
||
63 | ->withPath($url_route) |
||
64 | ->withQuery(explode('&', $request->getUri()->getQuery(), 2)[1] ?? ''); |
||
65 | |||
66 | return Registry::responseFactory() |
||
67 | ->redirectUrl($uri, StatusCodeInterface::STATUS_PERMANENT_REDIRECT) |
||
68 | ->withHeader('Link', '<' . $uri . '>; rel="canonical"'); |
||
69 | } |
||
70 | |||
71 | $pretty = $request; |
||
72 | } else { |
||
73 | // Turn the ugly URL into a pretty one, so the router can parse it. |
||
74 | $uri = $request->getUri()->withPath($url_route); |
||
75 | $pretty = $request->withUri($uri); |
||
76 | } |
||
77 | |||
78 | // Match the request to a route. |
||
79 | $matcher = $this->router_container->getMatcher(); |
||
80 | $route = $matcher->match($pretty); |
||
81 | |||
82 | // No route matched? |
||
83 | if ($route === false) { |
||
84 | $failed_route = $matcher->getFailedRoute(); |
||
85 | |||
86 | if ($failed_route instanceof Route) { |
||
87 | if ($failed_route->failedRule === Allows::class) { |
||
88 | return Registry::responseFactory()->response('', StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED, [ |
||
89 | 'Allow' => implode(', ', $failed_route->allows), |
||
90 | ]); |
||
91 | } |
||
92 | |||
93 | if ($failed_route->failedRule === Accepts::class) { |
||
94 | return Registry::responseFactory()->response('Negotiation failed', StatusCodeInterface::STATUS_NOT_ACCEPTABLE); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $handler->handle($request); |
||
99 | } |
||
100 | |||
101 | // Add the route as attribute of the request |
||
102 | $request = $request->withAttribute('route', $route); |
||
103 | |||
104 | $route_middleware = $route->extras['middleware'] ?? []; |
||
105 | |||
106 | $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); |
||
107 | |||
108 | $middleware = [ |
||
109 | ...$route_middleware, |
||
110 | CheckCsrf::class, |
||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Http\Middleware\CheckCsrf was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
111 | ...$module_middleware, |
||
112 | RequestHandler::class, |
||
113 | ]; |
||
114 | |||
115 | // Add the matched attributes to the request. |
||
116 | foreach ($route->attributes as $key => $value) { |
||
117 | if ($key === 'tree') { |
||
118 | $value = $this->tree_service->all()->get($value); |
||
119 | |||
120 | if ($value instanceof Tree) { |
||
121 | Registry::container()->set(Tree::class, $value); |
||
122 | } |
||
123 | |||
124 | // Missing mandatory parameter? Let the default handler take care of it. |
||
125 | if ($value === null && str_contains($route->path, '{tree}')) { |
||
126 | return $handler->handle($request); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | $request = $request->withAttribute((string) $key, $value); |
||
131 | } |
||
132 | |||
133 | // Bind the updated request into the container |
||
134 | Registry::container()->set(ServerRequestInterface::class, $request); |
||
135 | |||
136 | return Dispatcher::dispatch(middleware: $middleware, request: $request); |
||
137 | } |
||
138 | } |
||
139 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths