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