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