1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Localization\Locale\LocaleInterface; 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 Illuminate\Database\Capsule\Manager as DB; 32use Psr\Http\Message\ResponseInterface; 33use Psr\Http\Message\ServerRequestInterface; 34use Psr\Http\Server\RequestHandlerInterface; 35 36use function assert; 37 38/** 39 * Acknowledge an email verification code. 40 */ 41class VerifyEmail implements RequestHandlerInterface 42{ 43 use ViewResponseTrait; 44 45 /** @var EmailService */ 46 private $email_service; 47 48 /** @var UserService */ 49 private $user_service; 50 51 /** 52 * MessageController constructor. 53 * 54 * @param EmailService $email_service 55 * @param UserService $user_service 56 */ 57 public function __construct(EmailService $email_service, UserService $user_service) 58 { 59 $this->email_service = $email_service; 60 $this->user_service = $user_service; 61 } 62 63 /** 64 * Respond to a verification link that was emailed to a user. 65 * 66 * @param ServerRequestInterface $request 67 * 68 * @return ResponseInterface 69 */ 70 public function handle(ServerRequestInterface $request): ResponseInterface 71 { 72 $locale = $request->getAttribute('locale'); 73 assert($locale instanceof LocaleInterface); 74 75 $username = $request->getQueryParams()['username'] ?? ''; 76 $token = $request->getQueryParams()['token'] ?? ''; 77 78 $title = I18N::translate('User verification'); 79 80 $user = $this->user_service->findByUserName($username); 81 82 if ($user instanceof User && $user->getPreference('reg_hashcode') === $token) { 83 foreach ($this->user_service->administrators() as $administrator) { 84 // switch language to administrator settings 85 I18N::init($administrator->getPreference('language')); 86 87 $base_url = $request->getAttribute('base_url'); 88 89 /* I18N: %s is a server name/URL */ 90 $subject = I18N::translate('New user at %s', $base_url); 91 92 $this->email_service->send( 93 new SiteUser(), 94 $administrator, 95 new NoReplyUser(), 96 $subject, 97 view('emails/verify-notify-text', ['user' => $user]), 98 view('emails/verify-notify-html', ['user' => $user]) 99 ); 100 101 $mail1_method = $administrator->getPreference('CONTACT_METHOD'); 102 103 if ($mail1_method !== 'messaging3' && $mail1_method !== 'mailto' && $mail1_method !== 'none') { 104 DB::table('message')->insert([ 105 'sender' => $username, 106 'ip_address' => $request->getAttribute('client-ip'), 107 'user_id' => $administrator->id(), 108 'subject' => $subject, 109 'body' => view('emails/verify-notify-text', ['user' => $user]), 110 ]); 111 } 112 I18N::init($locale->languageTag()); 113 } 114 115 $user 116 ->setPreference('verified', '1') 117 ->setPreference('reg_timestamp', date('U')) 118 ->setPreference('reg_hashcode', ''); 119 120 Log::addAuthenticationLog('User ' . $username . ' verified their email address'); 121 122 return $this->viewResponse('verify-success-page', [ 123 'title' => $title, 124 ]); 125 } 126 127 return $this->viewResponse('verify-failure-page', [ 128 'title' => $title, 129 ]); 130 } 131} 132