xref: /webtrees/app/Http/RequestHandlers/VerifyEmail.php (revision d812eb6b3dd6c33a96d07cd964cb4c3b88cec447)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\RequestHandlers;
20
21use Fisharebest\Webtrees\Http\ViewResponseTrait;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Log;
24use Fisharebest\Webtrees\NoReplyUser;
25use Fisharebest\Webtrees\Services\MailService;
26use Fisharebest\Webtrees\Services\UserService;
27use Fisharebest\Webtrees\SiteUser;
28use Fisharebest\Webtrees\User;
29use Illuminate\Database\Capsule\Manager as DB;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34/**
35 * Acknowledge an email verification code.
36 */
37class VerifyEmail implements RequestHandlerInterface
38{
39    use ViewResponseTrait;
40
41    /** @var MailService */
42    private $mail_service;
43
44    /** @var UserService */
45    private $user_service;
46
47    /**
48     * MessageController constructor.
49     *
50     * @param MailService $mail_service
51     * @param UserService $user_service
52     */
53    public function __construct(MailService $mail_service, UserService $user_service)
54    {
55        $this->mail_service = $mail_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        $username = $request->getQueryParams()['username'] ?? '';
69        $token    = $request->getQueryParams()['token'] ?? '';
70
71        $title = I18N::translate('User verification');
72
73        $user = $this->user_service->findByUserName($username);
74
75        if ($user instanceof User && $user->getPreference('reg_hashcode') === $token) {
76            foreach ($this->user_service->administrators() as $administrator) {
77                // switch language to administrator settings
78                I18N::init($administrator->getPreference('language'));
79
80                $base_url = $request->getAttribute('base_url');
81
82                /* I18N: %s is a server name/URL */
83                $subject = I18N::translate('New user at %s', $base_url);
84
85                $this->mail_service->send(
86                    new SiteUser(),
87                    $administrator,
88                    new NoReplyUser(),
89                    $subject,
90                    view('emails/verify-notify-text', ['user' => $user]),
91                    view('emails/verify-notify-html', ['user' => $user])
92                );
93
94                $mail1_method = $administrator->getPreference('CONTACT_METHOD');
95
96                if ($mail1_method !== 'messaging3' && $mail1_method !== 'mailto' && $mail1_method !== 'none') {
97                    DB::table('message')->insert([
98                        'sender'     => $username,
99                        'ip_address' => $request->getAttribute('client-ip'),
100                        'user_id'    => $administrator->id(),
101                        'subject'    => $subject,
102                        'body'       => view('emails/verify-notify-text', ['user' => $user]),
103                    ]);
104                }
105                I18N::init(WT_LOCALE);
106            }
107
108            $user
109                ->setPreference('verified', '1')
110                ->setPreference('reg_timestamp', date('U'))
111                ->setPreference('reg_hashcode', '');
112
113            Log::addAuthenticationLog('User ' . $username . ' verified their email address');
114
115            return $this->viewResponse('verify-success-page', [
116                'title' => $title,
117            ]);
118        }
119
120        return $this->viewResponse('verify-failure-page', [
121            'title' => $title,
122        ]);
123    }
124}
125