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\Contracts\UserInterface; 23use Fisharebest\Webtrees\Http\ViewResponseTrait; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Log; 26use Fisharebest\Webtrees\NoReplyUser; 27use Fisharebest\Webtrees\Services\EmailService; 28use Fisharebest\Webtrees\Services\UserService; 29use Fisharebest\Webtrees\SiteUser; 30use Fisharebest\Webtrees\User; 31use Fisharebest\Webtrees\Validator; 32use Illuminate\Database\Capsule\Manager as DB; 33use Psr\Http\Message\ResponseInterface; 34use Psr\Http\Message\ServerRequestInterface; 35use Psr\Http\Server\RequestHandlerInterface; 36 37/** 38 * Acknowledge an email verification code. 39 */ 40class VerifyEmail implements RequestHandlerInterface 41{ 42 use ViewResponseTrait; 43 44 private EmailService $email_service; 45 46 private UserService $user_service; 47 48 /** 49 * MessageController constructor. 50 * 51 * @param EmailService $email_service 52 * @param UserService $user_service 53 */ 54 public function __construct(EmailService $email_service, UserService $user_service) 55 { 56 $this->email_service = $email_service; 57 $this->user_service = $user_service; 58 } 59 60 /** 61 * Respond to a verification link that was emailed to a user. 62 * 63 * @param ServerRequestInterface $request 64 * 65 * @return ResponseInterface 66 */ 67 public function handle(ServerRequestInterface $request): ResponseInterface 68 { 69 $token = $request->getAttribute('token'); 70 $tree = Validator::attributes($request)->treeOptional(); 71 $username = $request->getAttribute('username'); 72 73 $title = I18N::translate('User verification'); 74 75 $user = $this->user_service->findByUserName($username); 76 77 if ($user instanceof User && $user->getPreference(UserInterface::PREF_VERIFICATION_TOKEN) === $token) { 78 $old_language = I18N::languageTag(); 79 80 foreach ($this->user_service->administrators() as $administrator) { 81 // switch language to administrator settings 82 I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE)); 83 84 $base_url = Validator::attributes($request)->string('base_url'); 85 86 /* I18N: %s is a server name/URL */ 87 $subject = I18N::translate('New user at %s', $base_url); 88 89 $this->email_service->send( 90 new SiteUser(), 91 $administrator, 92 new NoReplyUser(), 93 $subject, 94 view('emails/verify-notify-text', ['user' => $user]), 95 view('emails/verify-notify-html', ['user' => $user]) 96 ); 97 98 $mail1_method = $administrator->getPreference('CONTACT_METHOD'); 99 100 if ($mail1_method !== 'messaging3' && $mail1_method !== 'mailto' && $mail1_method !== 'none') { 101 DB::table('message')->insert([ 102 'sender' => $username, 103 'ip_address' => $request->getAttribute('client-ip'), 104 'user_id' => $administrator->id(), 105 'subject' => $subject, 106 'body' => view('emails/verify-notify-text', ['user' => $user]), 107 ]); 108 } 109 } 110 I18N::init($old_language); 111 112 $user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1'); 113 $user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U')); 114 $user->setPreference(UserInterface::PREF_VERIFICATION_TOKEN, ''); 115 116 Log::addAuthenticationLog('User ' . $username . ' verified their email address'); 117 118 return $this->viewResponse('verify-success-page', [ 119 'title' => $title, 120 'tree' => $tree, 121 ]); 122 } 123 124 return $this->viewResponse('verify-failure-page', [ 125 'title' => $title, 126 'tree' => $tree, 127 ]); 128 } 129} 130