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