. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fig\Http\Message\RequestMethodInterface; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\Site; use Fisharebest\Webtrees\Tree; use Fisharebest\Webtrees\User; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use function redirect; use function route; /** * Middleware to generate a response when no route was matched. */ class NoRouteFound implements MiddlewareInterface, RequestMethodInterface { use ViewResponseTrait; /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if ($request->getMethod() !== self::METHOD_GET) { throw new NotFoundHttpException(); } $user = $request->getAttribute('user'); // Choose the default tree (if it exists), or the first tree found. $default = Site::getPreference('DEFAULT_GEDCOM'); $tree = Tree::findByName($default) ?? Tree::all()->first(); if ($tree instanceof Tree) { if ($tree->getPreference('imported') === '1') { // Logged in? Go to the user's page. if ($user instanceof User) { return redirect(route('user-page', ['tree' => $tree->name()])); } // Not logged in? Go to the tree's page. return redirect(route('tree-page', ['ged' => $tree->name()])); } return redirect(route('admin-trees', ['ged' => $tree->name()])); } // No tree available? Create one. if (Auth::isAdmin($user)) { return redirect(route('admin-trees')); } // Logged in, but no access to any tree. if ($user instanceof User) { return $this->viewResponse('errors/no-tree-access', ['title' => '']); } // Not logged in. return redirect(route(LoginPage::class, ['url' => $request->getAttribute('request_uri')])); } }