xref: /webtrees/app/Http/Middleware/Router.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
19e5d8e6fSGreg Roach<?php
29e5d8e6fSGreg Roach
39e5d8e6fSGreg Roach/**
49e5d8e6fSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg Roach * along with this program. If not, see <https://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;
2364c83a78SGreg Roachuse Aura\Router\Rule\Accepts;
2464c83a78SGreg Roachuse Aura\Router\Rule\Allows;
2564c83a78SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2782e92bfaSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
280c0910bfSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
29ee4364daSGreg Roachuse Fisharebest\Webtrees\Tree;
3082e92bfaSGreg Roachuse Middleland\Dispatcher;
319e5d8e6fSGreg Roachuse Psr\Http\Message\ResponseInterface;
329e5d8e6fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
339e5d8e6fSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
349e5d8e6fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
350c0910bfSGreg Roach
369e5d8e6fSGreg Roachuse function app;
3782e92bfaSGreg Roachuse function array_map;
3864c83a78SGreg Roachuse function response;
39dec352c1SGreg Roachuse function str_contains;
409e5d8e6fSGreg Roach
419e5d8e6fSGreg Roach/**
429e5d8e6fSGreg Roach * Simple class to help migrate to a third-party routing library.
439e5d8e6fSGreg Roach */
4471378461SGreg Roachclass Router implements MiddlewareInterface
459e5d8e6fSGreg Roach{
4682e92bfaSGreg Roach    /** @var ModuleService */
4782e92bfaSGreg Roach    private $module_service;
4882e92bfaSGreg Roach
493ecf8b4eSGreg Roach    /** @var RouterContainer */
503ecf8b4eSGreg Roach    private $router_container;
513ecf8b4eSGreg Roach
520c0910bfSGreg Roach    /** @var TreeService */
530c0910bfSGreg Roach    private $tree_service;
540c0910bfSGreg Roach
5582e92bfaSGreg Roach    /**
5682e92bfaSGreg Roach     * Router constructor.
5782e92bfaSGreg Roach     *
5882e92bfaSGreg Roach     * @param ModuleService   $module_service
594874f72dSGreg Roach     * @param RouterContainer $router_container
600c0910bfSGreg Roach     * @param TreeService     $tree_service
6182e92bfaSGreg Roach     */
620c0910bfSGreg Roach    public function __construct(ModuleService $module_service, RouterContainer $router_container, TreeService $tree_service)
6382e92bfaSGreg Roach    {
6482e92bfaSGreg Roach        $this->module_service   = $module_service;
653ecf8b4eSGreg Roach        $this->router_container = $router_container;
660c0910bfSGreg Roach        $this->tree_service     = $tree_service;
679e5d8e6fSGreg Roach    }
689e5d8e6fSGreg Roach
699e5d8e6fSGreg Roach    /**
709e5d8e6fSGreg Roach     * @param ServerRequestInterface  $request
719e5d8e6fSGreg Roach     * @param RequestHandlerInterface $handler
729e5d8e6fSGreg Roach     *
739e5d8e6fSGreg Roach     * @return ResponseInterface
749e5d8e6fSGreg Roach     */
759e5d8e6fSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
769e5d8e6fSGreg Roach    {
774874f72dSGreg Roach        // Turn the ugly URL into a pretty one, so the router can parse it.
788753199aSGreg Roach        $pretty = $request;
798753199aSGreg Roach
8054a7c588SGreg Roach        if ($request->getAttribute('rewrite_urls') !== '1') {
818753199aSGreg Roach            // Ugly URLs store the path in a query parameter.
828753199aSGreg Roach            $url_route = $request->getQueryParams()['route'] ?? '';
838753199aSGreg Roach            $uri       = $request->getUri()->withPath($url_route);
848753199aSGreg Roach            $pretty    = $request->withUri($uri);
85ee4364daSGreg Roach        }
869e5d8e6fSGreg Roach
873ecf8b4eSGreg Roach        // Match the request to a route.
8864c83a78SGreg Roach        $matcher = $this->router_container->getMatcher();
8964c83a78SGreg Roach        $route   = $matcher->match($pretty);
903ecf8b4eSGreg Roach
9164c83a78SGreg Roach        // No route matched?
9215958f0eSGreg Roach        if ($route === false) {
9364c83a78SGreg Roach            $failed_route = $matcher->getFailedRoute();
9464c83a78SGreg Roach
9564c83a78SGreg Roach            switch ($failed_route->failedRule) {
9664c83a78SGreg Roach                case Allows::class:
9764c83a78SGreg Roach                    return response('', StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED, [
9864c83a78SGreg Roach                        'Allow' => implode(', ', $failed_route->allows),
9964c83a78SGreg Roach                    ]);
10064c83a78SGreg Roach
10164c83a78SGreg Roach                case Accepts::class:
10264c83a78SGreg Roach                    // We don't use this, but modules might.
10364c83a78SGreg Roach                    return response('Negotiation failed', StatusCodeInterface::STATUS_NOT_ACCEPTABLE);
10464c83a78SGreg Roach
10564c83a78SGreg Roach                default:
10664c83a78SGreg Roach                    // Not found
1079e5d8e6fSGreg Roach                    return $handler->handle($request);
1089e5d8e6fSGreg Roach            }
10964c83a78SGreg Roach        }
1109e5d8e6fSGreg Roach
111f307429bSGreg Roach        // Add the route as attribute of the request
112f307429bSGreg Roach        $request = $request->withAttribute('route', $route);
113f307429bSGreg Roach
1145d3d7b86SGreg Roach        // This middleware cannot run until after the routing, as it needs to know the route.
115f307429bSGreg Roach        $post_routing_middleware = [CheckCsrf::class];
116f307429bSGreg Roach        $post_routing_middleware = array_map('app', $post_routing_middleware);
117f307429bSGreg Roach
11882e92bfaSGreg Roach        // Firstly, apply the route middleware
119ee4364daSGreg Roach        $route_middleware = $route->extras['middleware'] ?? [];
12082e92bfaSGreg Roach        $route_middleware = array_map('app', $route_middleware);
1219e5d8e6fSGreg Roach
12282e92bfaSGreg Roach        // Secondly, apply any module middleware
12382e92bfaSGreg Roach        $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all();
1249e5d8e6fSGreg Roach
12582e92bfaSGreg Roach        // Finally, run the handler using middleware
126ee4364daSGreg Roach        $handler_middleware = [new WrapHandler($route->handler)];
12782e92bfaSGreg Roach
128f307429bSGreg Roach        $middleware = array_merge(
129f307429bSGreg Roach            $post_routing_middleware,
130f307429bSGreg Roach            $route_middleware,
131f307429bSGreg Roach            $module_middleware,
132f307429bSGreg Roach            $handler_middleware
133f307429bSGreg Roach        );
134f307429bSGreg Roach
135f307429bSGreg Roach        // Add the matched attributes to the request.
136f307429bSGreg Roach        foreach ($route->attributes as $key => $value) {
137f307429bSGreg Roach            if ($key === 'tree') {
138f307429bSGreg Roach                $value = $this->tree_service->all()->get($value);
139f307429bSGreg Roach                app()->instance(Tree::class, $value);
140f307429bSGreg Roach
141f307429bSGreg Roach                // Missing mandatory parameter? Let the default handler take care of it.
142dec352c1SGreg Roach                if ($value === null && str_contains($route->path, '{tree}')) {
143f307429bSGreg Roach                    return $handler->handle($request);
144f307429bSGreg Roach                }
145f307429bSGreg Roach            }
146f307429bSGreg Roach
147f307429bSGreg Roach            $request = $request->withAttribute((string) $key, $value);
148f307429bSGreg Roach        }
149f307429bSGreg Roach
150f307429bSGreg Roach        // Bind the request into the container
151f307429bSGreg Roach        app()->instance(ServerRequestInterface::class, $request);
15282e92bfaSGreg Roach
15382e92bfaSGreg Roach        $dispatcher = new Dispatcher($middleware, app());
15482e92bfaSGreg Roach
15569675509SGreg Roach        // These are deprecated, and will be removed in webtrees 2.1.0
1566b9cb339SGreg Roach        app()->instance('cache.array', Registry::cache()->array());
1576b9cb339SGreg Roach        app()->instance('cache.files', Registry::cache()->file());
15869675509SGreg Roach
15969675509SGreg Roach        // These are deprecated, and will be removed in webtrees 2.1.0
16069675509SGreg Roach        $request = $request
1616b9cb339SGreg Roach            ->withAttribute('filesystem.data', Registry::filesystem()->data())
1626b9cb339SGreg Roach            ->withAttribute('filesystem.data.name', Registry::filesystem()->dataName())
1636b9cb339SGreg Roach            ->withAttribute('filesystem.root', Registry::filesystem()->root())
1646b9cb339SGreg Roach            ->withAttribute('filesystem.root.name', Registry::filesystem()->rootName());
16569675509SGreg Roach
16682e92bfaSGreg Roach        return $dispatcher->dispatch($request);
1679e5d8e6fSGreg Roach    }
1689e5d8e6fSGreg Roach}
169