1d403609dSGreg Roach<?php 2d403609dSGreg Roach 3d403609dSGreg Roach/** 4d403609dSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6d403609dSGreg Roach * This program is free software: you can redistribute it and/or modify 7d403609dSGreg Roach * it under the terms of the GNU General Public License as published by 8d403609dSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9d403609dSGreg Roach * (at your option) any later version. 10d403609dSGreg Roach * This program is distributed in the hope that it will be useful, 11d403609dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d403609dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d403609dSGreg Roach * GNU General Public License for more details. 14d403609dSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16d403609dSGreg Roach */ 17fcfa147eSGreg Roach 18d403609dSGreg Roachdeclare(strict_types=1); 19d403609dSGreg Roach 20d403609dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21d403609dSGreg Roach 221fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 24d403609dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 25d403609dSGreg Roachuse Fisharebest\Webtrees\I18N; 26d403609dSGreg Roachuse Fisharebest\Webtrees\Log; 27d403609dSGreg Roachuse Fisharebest\Webtrees\NoReplyUser; 28e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService; 298cfb5e7bSGreg Roachuse Fisharebest\Webtrees\Services\MessageService; 30d403609dSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 31d403609dSGreg Roachuse Fisharebest\Webtrees\SiteUser; 32d403609dSGreg Roachuse Fisharebest\Webtrees\User; 33b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 34d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface; 35d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 36d403609dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 37d403609dSGreg Roach 38d403609dSGreg Roach/** 39d403609dSGreg Roach * Acknowledge an email verification code. 40d403609dSGreg Roach */ 41d403609dSGreg Roachclass VerifyEmail implements RequestHandlerInterface 42d403609dSGreg Roach{ 43d403609dSGreg Roach use ViewResponseTrait; 44d403609dSGreg Roach 45c4943cffSGreg Roach private EmailService $email_service; 46f3874e19SGreg Roach 47c4943cffSGreg Roach private UserService $user_service; 48d403609dSGreg Roach 49d403609dSGreg Roach /** 50e381f98dSGreg Roach * @param EmailService $email_service 51d403609dSGreg Roach * @param UserService $user_service 52d403609dSGreg Roach */ 53e381f98dSGreg Roach public function __construct(EmailService $email_service, UserService $user_service) 54d403609dSGreg Roach { 55e381f98dSGreg Roach $this->email_service = $email_service; 56d403609dSGreg Roach $this->user_service = $user_service; 57d403609dSGreg Roach } 58d403609dSGreg Roach 59d403609dSGreg Roach /** 60d403609dSGreg Roach * Respond to a verification link that was emailed to a user. 61d403609dSGreg Roach * 62d403609dSGreg Roach * @param ServerRequestInterface $request 63d403609dSGreg Roach * 64d403609dSGreg Roach * @return ResponseInterface 65d403609dSGreg Roach */ 66d403609dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 67d403609dSGreg Roach { 683d61467fSGreg Roach $token = $request->getAttribute('token'); 69b55cbc6bSGreg Roach $tree = Validator::attributes($request)->treeOptional(); 703d61467fSGreg Roach $username = $request->getAttribute('username'); 71d403609dSGreg Roach 72d403609dSGreg Roach $title = I18N::translate('User verification'); 73d403609dSGreg Roach 74d403609dSGreg Roach $user = $this->user_service->findByUserName($username); 75d403609dSGreg Roach 761fe542e9SGreg Roach if ($user instanceof User && $user->getPreference(UserInterface::PREF_VERIFICATION_TOKEN) === $token) { 7765cf5706SGreg Roach $old_language = I18N::languageTag(); 7865cf5706SGreg Roach 79d403609dSGreg Roach foreach ($this->user_service->administrators() as $administrator) { 80d403609dSGreg Roach // switch language to administrator settings 811fe542e9SGreg Roach I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE)); 82d403609dSGreg Roach 83b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 84d403609dSGreg Roach 85d403609dSGreg Roach /* I18N: %s is a server name/URL */ 86d403609dSGreg Roach $subject = I18N::translate('New user at %s', $base_url); 87d403609dSGreg Roach 88e381f98dSGreg Roach $this->email_service->send( 89d403609dSGreg Roach new SiteUser(), 90d403609dSGreg Roach $administrator, 91d403609dSGreg Roach new NoReplyUser(), 92d403609dSGreg Roach $subject, 93d403609dSGreg Roach view('emails/verify-notify-text', ['user' => $user]), 94d403609dSGreg Roach view('emails/verify-notify-html', ['user' => $user]) 95d403609dSGreg Roach ); 96d403609dSGreg Roach 97d403609dSGreg Roach $mail1_method = $administrator->getPreference('CONTACT_METHOD'); 98d403609dSGreg Roach 998cfb5e7bSGreg Roach if ( 1008cfb5e7bSGreg Roach $mail1_method !== MessageService::CONTACT_METHOD_EMAIL && 1018cfb5e7bSGreg Roach $mail1_method !== MessageService::CONTACT_METHOD_MAILTO && 1028cfb5e7bSGreg Roach $mail1_method !== MessageService::CONTACT_METHOD_NONE 1038cfb5e7bSGreg Roach ) { 104d403609dSGreg Roach DB::table('message')->insert([ 105d403609dSGreg Roach 'sender' => $username, 1064874f72dSGreg Roach 'ip_address' => $request->getAttribute('client-ip'), 107d403609dSGreg Roach 'user_id' => $administrator->id(), 108d403609dSGreg Roach 'subject' => $subject, 109d403609dSGreg Roach 'body' => view('emails/verify-notify-text', ['user' => $user]), 110d403609dSGreg Roach ]); 111d403609dSGreg Roach } 112d403609dSGreg Roach } 11365cf5706SGreg Roach I18N::init($old_language); 114d403609dSGreg Roach 1151fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1'); 1161fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U')); 1171fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_VERIFICATION_TOKEN, ''); 118d403609dSGreg Roach 119d403609dSGreg Roach Log::addAuthenticationLog('User ' . $username . ' verified their email address'); 120d403609dSGreg Roach 121d403609dSGreg Roach return $this->viewResponse('verify-success-page', [ 122d403609dSGreg Roach 'title' => $title, 1233d61467fSGreg Roach 'tree' => $tree, 124d403609dSGreg Roach ]); 125d403609dSGreg Roach } 126d403609dSGreg Roach 127d403609dSGreg Roach return $this->viewResponse('verify-failure-page', [ 128d403609dSGreg Roach 'title' => $title, 1293d61467fSGreg Roach 'tree' => $tree, 130d403609dSGreg Roach ]); 131d403609dSGreg Roach } 132d403609dSGreg Roach} 133