xref: /webtrees/app/Http/RequestHandlers/VerifyEmail.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\MessageService;
29use Fisharebest\Webtrees\Services\UserService;
30use Fisharebest\Webtrees\SiteUser;
31use Fisharebest\Webtrees\User;
32use Fisharebest\Webtrees\Validator;
33use Illuminate\Database\Capsule\Manager as DB;
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     * MessageController constructor.
51     *
52     * @param EmailService $email_service
53     * @param UserService  $user_service
54     */
55    public function __construct(EmailService $email_service, UserService $user_service)
56    {
57        $this->email_service = $email_service;
58        $this->user_service  = $user_service;
59    }
60
61    /**
62     * Respond to a verification link that was emailed to a user.
63     *
64     * @param ServerRequestInterface $request
65     *
66     * @return ResponseInterface
67     */
68    public function handle(ServerRequestInterface $request): ResponseInterface
69    {
70        $token    = $request->getAttribute('token');
71        $tree     = Validator::attributes($request)->treeOptional();
72        $username = $request->getAttribute('username');
73
74        $title = I18N::translate('User verification');
75
76        $user = $this->user_service->findByUserName($username);
77
78        if ($user instanceof User && $user->getPreference(UserInterface::PREF_VERIFICATION_TOKEN) === $token) {
79            $old_language = I18N::languageTag();
80
81            foreach ($this->user_service->administrators() as $administrator) {
82                // switch language to administrator settings
83                I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE));
84
85                $base_url = Validator::attributes($request)->string('base_url');
86
87                /* I18N: %s is a server name/URL */
88                $subject = I18N::translate('New user at %s', $base_url);
89
90                $this->email_service->send(
91                    new SiteUser(),
92                    $administrator,
93                    new NoReplyUser(),
94                    $subject,
95                    view('emails/verify-notify-text', ['user' => $user]),
96                    view('emails/verify-notify-html', ['user' => $user])
97                );
98
99                $mail1_method = $administrator->getPreference('CONTACT_METHOD');
100
101                if (
102                    $mail1_method !== MessageService::CONTACT_METHOD_EMAIL &&
103                    $mail1_method !== MessageService::CONTACT_METHOD_MAILTO &&
104                    $mail1_method !== MessageService::CONTACT_METHOD_NONE
105                ) {
106                    DB::table('message')->insert([
107                        'sender'     => $username,
108                        'ip_address' => $request->getAttribute('client-ip'),
109                        'user_id'    => $administrator->id(),
110                        'subject'    => $subject,
111                        'body'       => view('emails/verify-notify-text', ['user' => $user]),
112                    ]);
113                }
114            }
115            I18N::init($old_language);
116
117            $user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
118            $user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U'));
119            $user->setPreference(UserInterface::PREF_VERIFICATION_TOKEN, '');
120
121            Log::addAuthenticationLog('User ' . $username . ' verified their email address');
122
123            return $this->viewResponse('verify-success-page', [
124                'title' => $title,
125                'tree'  => $tree,
126            ]);
127        }
128
129        return $this->viewResponse('verify-failure-page', [
130            'title' => $title,
131            'tree'  => $tree,
132        ]);
133    }
134}
135