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