xref: /webtrees/app/Http/Middleware/NoRouteFound.php (revision 5afbc57a5c33b9caec67458db57f44e54a90f745)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\Middleware;
20
21use Fig\Http\Message\RequestMethodInterface;
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Http\RequestHandlers\CreateTreePage;
24use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
25use Fisharebest\Webtrees\Http\ViewResponseTrait;
26use Fisharebest\Webtrees\Site;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\User;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\MiddlewareInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
35use function redirect;
36use function route;
37
38/**
39 * Middleware to generate a response when no route was matched.
40 */
41class NoRouteFound implements MiddlewareInterface
42{
43    use ViewResponseTrait;
44
45    /**
46     * @param ServerRequestInterface  $request
47     * @param RequestHandlerInterface $handler
48     *
49     * @return ResponseInterface
50     */
51    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
52    {
53        if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {
54            throw new NotFoundHttpException();
55        }
56
57        $user = $request->getAttribute('user');
58
59        // Choose the default tree (if it exists), or the first tree found.
60        $default = Site::getPreference('DEFAULT_GEDCOM');
61        $tree    = Tree::findByName($default) ?? Tree::all()->first();
62
63        if ($tree instanceof Tree) {
64            if ($tree->getPreference('imported') === '1') {
65                // Logged in?  Go to the user's page.
66                if ($user instanceof User) {
67                    return redirect(route('user-page', ['tree' => $tree->name()]));
68                }
69
70                // Not logged in?  Go to the tree's page.
71                return redirect(route('tree-page', ['tree' => $tree->name()]));
72            }
73
74            return redirect(route('admin-trees', ['tree' => $tree->name()]));
75        }
76
77        // No tree available?  Create one.
78        if (Auth::isAdmin($user)) {
79            return redirect(route(CreateTreePage::class));
80        }
81
82        // Logged in, but no access to any tree.
83        if ($user instanceof User) {
84            return $this->viewResponse('errors/no-tree-access', ['title' => '']);
85        }
86
87        // Not logged in.
88        return redirect(route(LoginPage::class, ['url' => $request->getUri()]));
89    }
90}
91