1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Http\ViewResponseTrait; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Services\TreeService; 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\RequestHandlerInterface; 31 32/** 33 * Show a login form. 34 */ 35class LoginPage implements RequestHandlerInterface 36{ 37 use ViewResponseTrait; 38 39 private TreeService $tree_service; 40 41 /** 42 * LoginPage constructor. 43 * 44 * @param TreeService $tree_service 45 */ 46 public function __construct(TreeService $tree_service) 47 { 48 $this->tree_service = $tree_service; 49 } 50 51 /** 52 * @param ServerRequestInterface $request 53 * 54 * @return ResponseInterface 55 */ 56 public function handle(ServerRequestInterface $request): ResponseInterface 57 { 58 $tree = $request->getAttribute('tree'); 59 $user = $request->getAttribute('user'); 60 61 // Already logged in? 62 if ($user instanceof User) { 63 return redirect(route(UserPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : ''])); 64 } 65 66 $url = $request->getQueryParams()['url'] ?? ''; 67 $username = $request->getQueryParams()['username'] ?? ''; 68 69 // No tree? perhaps we came here from a page without one. 70 if ($tree === null) { 71 $default = Site::getPreference('DEFAULT_GEDCOM'); 72 $tree = $this->tree_service->all()->get($default) ?? $this->tree_service->all()->first(); 73 74 if ($tree instanceof Tree) { 75 return redirect(route(self::class, ['tree' => $tree->name(), 'url' => $url])); 76 } 77 } 78 79 $title = I18N::translate('Sign in'); 80 81 switch (Site::getPreference('WELCOME_TEXT_AUTH_MODE')) { 82 case '1': 83 default: 84 $welcome = I18N::translate('Anyone with a user account can access this website.'); 85 break; 86 case '2': 87 $welcome = I18N::translate('You need to be an authorized user to access this website.'); 88 break; 89 case '3': 90 $welcome = I18N::translate('You need to be a family member to access this website.'); 91 break; 92 case '4': 93 $welcome = Site::getPreference('WELCOME_TEXT_AUTH_MODE_' . I18N::languageTag()); 94 break; 95 } 96 97 if (Site::getPreference('USE_REGISTRATION_MODULE') === '1') { 98 $welcome .= ' ' . I18N::translate('You can apply for an account using the link below.'); 99 } 100 101 $can_register = Site::getPreference('USE_REGISTRATION_MODULE') === '1'; 102 103 return $this->viewResponse('login-page', [ 104 'can_register' => $can_register, 105 'title' => $title, 106 'url' => $url, 107 'tree' => $tree, 108 'username' => $username, 109 'welcome' => $welcome, 110 ]); 111 } 112} 113