xref: /webtrees/app/Http/Middleware/NoRouteFound.php (revision 71378461661e7642e52abe7d41c9cfffb3e5369b)
1a22c26f3SGreg Roach<?php
23976b470SGreg Roach
3a22c26f3SGreg Roach/**
4a22c26f3SGreg Roach * webtrees: online genealogy
5a22c26f3SGreg Roach * Copyright (C) 2019 webtrees development team
6a22c26f3SGreg Roach * This program is free software: you can redistribute it and/or modify
7a22c26f3SGreg Roach * it under the terms of the GNU General Public License as published by
8a22c26f3SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a22c26f3SGreg Roach * (at your option) any later version.
10a22c26f3SGreg Roach * This program is distributed in the hope that it will be useful,
11a22c26f3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a22c26f3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a22c26f3SGreg Roach * GNU General Public License for more details.
14a22c26f3SGreg Roach * You should have received a copy of the GNU General Public License
15a22c26f3SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a22c26f3SGreg Roach */
17a22c26f3SGreg Roachdeclare(strict_types=1);
18a22c26f3SGreg Roach
19a22c26f3SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
20a22c26f3SGreg Roach
21ee4364daSGreg Roachuse Fig\Http\Message\RequestMethodInterface;
22a22c26f3SGreg Roachuse Fisharebest\Webtrees\Auth;
2356f9a9c1SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
24a22c26f3SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
25ee4364daSGreg Roachuse Fisharebest\Webtrees\Site;
26a22c26f3SGreg Roachuse Fisharebest\Webtrees\Tree;
27ee4364daSGreg Roachuse Fisharebest\Webtrees\User;
28a22c26f3SGreg Roachuse Psr\Http\Message\ResponseInterface;
29a22c26f3SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30a22c26f3SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
31a22c26f3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32ee4364daSGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
333976b470SGreg Roach
34a22c26f3SGreg Roachuse function redirect;
35a22c26f3SGreg Roachuse function route;
36a22c26f3SGreg Roach
37a22c26f3SGreg Roach/**
38a22c26f3SGreg Roach * Middleware to generate a response when no route was matched.
39a22c26f3SGreg Roach */
40*71378461SGreg Roachclass NoRouteFound implements MiddlewareInterface
41a22c26f3SGreg Roach{
42a22c26f3SGreg Roach    use ViewResponseTrait;
43a22c26f3SGreg Roach
44a22c26f3SGreg Roach    /**
45a22c26f3SGreg Roach     * @param ServerRequestInterface  $request
46a22c26f3SGreg Roach     * @param RequestHandlerInterface $handler
47a22c26f3SGreg Roach     *
48a22c26f3SGreg Roach     * @return ResponseInterface
49a22c26f3SGreg Roach     */
50a22c26f3SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
51a22c26f3SGreg Roach    {
52*71378461SGreg Roach        if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {
53ee4364daSGreg Roach            throw new NotFoundHttpException();
54ee4364daSGreg Roach        }
55a22c26f3SGreg Roach
56ee4364daSGreg Roach        $user = $request->getAttribute('user');
57ee4364daSGreg Roach
58ee4364daSGreg Roach        // Choose the default tree (if it exists), or the first tree found.
59ee4364daSGreg Roach        $default = Site::getPreference('DEFAULT_GEDCOM');
60ee4364daSGreg Roach        $tree    = Tree::findByName($default) ?? Tree::all()->first();
61ee4364daSGreg Roach
62ee4364daSGreg Roach        if ($tree instanceof Tree) {
63ee4364daSGreg Roach            if ($tree->getPreference('imported') === '1') {
64ee4364daSGreg Roach                // Logged in?  Go to the user's page.
65ee4364daSGreg Roach                if ($user instanceof User) {
66ee4364daSGreg Roach                    return redirect(route('user-page', ['tree' => $tree->name()]));
67ee4364daSGreg Roach                }
68ee4364daSGreg Roach
69ee4364daSGreg Roach                // Not logged in?  Go to the tree's page.
70a22c26f3SGreg Roach                return redirect(route('tree-page', ['ged' => $tree->name()]));
71a22c26f3SGreg Roach            }
72a22c26f3SGreg Roach
73ee4364daSGreg Roach            return redirect(route('admin-trees', ['ged' => $tree->name()]));
74a22c26f3SGreg Roach        }
75a22c26f3SGreg Roach
76ee4364daSGreg Roach        // No tree available?  Create one.
77ee4364daSGreg Roach        if (Auth::isAdmin($user)) {
78a22c26f3SGreg Roach            return redirect(route('admin-trees'));
79a22c26f3SGreg Roach        }
80a22c26f3SGreg Roach
81ee4364daSGreg Roach        // Logged in, but no access to any tree.
82ee4364daSGreg Roach        if ($user instanceof User) {
83a22c26f3SGreg Roach            return $this->viewResponse('errors/no-tree-access', ['title' => '']);
84a22c26f3SGreg Roach        }
85ee4364daSGreg Roach
86ee4364daSGreg Roach        // Not logged in.
8756f9a9c1SGreg Roach        return redirect(route(LoginPage::class, ['url' => $request->getAttribute('request_uri')]));
88ee4364daSGreg Roach    }
89a22c26f3SGreg Roach}
90