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