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