xref: /webtrees/app/Http/RequestHandlers/VerifyEmail.php (revision e381f98dae35059b6db6d6f34db84bb55bd35a4a)
1d403609dSGreg Roach<?php
2d403609dSGreg Roach
3d403609dSGreg Roach/**
4d403609dSGreg Roach * webtrees: online genealogy
5d403609dSGreg Roach * Copyright (C) 2019 webtrees development team
6d403609dSGreg Roach * This program is free software: you can redistribute it and/or modify
7d403609dSGreg Roach * it under the terms of the GNU General Public License as published by
8d403609dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d403609dSGreg Roach * (at your option) any later version.
10d403609dSGreg Roach * This program is distributed in the hope that it will be useful,
11d403609dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d403609dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d403609dSGreg Roach * GNU General Public License for more details.
14d403609dSGreg Roach * You should have received a copy of the GNU General Public License
15d403609dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16d403609dSGreg Roach */
17fcfa147eSGreg Roach
18d403609dSGreg Roachdeclare(strict_types=1);
19d403609dSGreg Roach
20d403609dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d403609dSGreg Roach
2290a2f718SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
23d403609dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
24d403609dSGreg Roachuse Fisharebest\Webtrees\I18N;
25d403609dSGreg Roachuse Fisharebest\Webtrees\Log;
26d403609dSGreg Roachuse Fisharebest\Webtrees\NoReplyUser;
27*e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
28d403609dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
29d403609dSGreg Roachuse Fisharebest\Webtrees\SiteUser;
30d403609dSGreg Roachuse Fisharebest\Webtrees\User;
31d403609dSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
32d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
33d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34d403609dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
35d403609dSGreg Roach
3690a2f718SGreg Roachuse function assert;
3790a2f718SGreg Roach
38d403609dSGreg Roach/**
39d403609dSGreg Roach * Acknowledge an email verification code.
40d403609dSGreg Roach */
41d403609dSGreg Roachclass VerifyEmail implements RequestHandlerInterface
42d403609dSGreg Roach{
43d403609dSGreg Roach    use ViewResponseTrait;
44d403609dSGreg Roach
45*e381f98dSGreg Roach    /** @var EmailService */
46*e381f98dSGreg Roach    private $email_service;
47f3874e19SGreg Roach
48f3874e19SGreg Roach    /** @var UserService */
49d403609dSGreg Roach    private $user_service;
50d403609dSGreg Roach
51d403609dSGreg Roach    /**
52d403609dSGreg Roach     * MessageController constructor.
53d403609dSGreg Roach     *
54*e381f98dSGreg Roach     * @param EmailService $email_service
55d403609dSGreg Roach     * @param UserService  $user_service
56d403609dSGreg Roach     */
57*e381f98dSGreg Roach    public function __construct(EmailService $email_service, UserService $user_service)
58d403609dSGreg Roach    {
59*e381f98dSGreg Roach        $this->email_service = $email_service;
60d403609dSGreg Roach        $this->user_service  = $user_service;
61d403609dSGreg Roach    }
62d403609dSGreg Roach
63d403609dSGreg Roach    /**
64d403609dSGreg Roach     * Respond to a verification link that was emailed to a user.
65d403609dSGreg Roach     *
66d403609dSGreg Roach     * @param ServerRequestInterface $request
67d403609dSGreg Roach     *
68d403609dSGreg Roach     * @return ResponseInterface
69d403609dSGreg Roach     */
70d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
71d403609dSGreg Roach    {
7290a2f718SGreg Roach        $locale = $request->getAttribute('locale');
7390a2f718SGreg Roach        assert($locale instanceof LocaleInterface);
7490a2f718SGreg Roach
75d403609dSGreg Roach        $username = $request->getQueryParams()['username'] ?? '';
76d403609dSGreg Roach        $token    = $request->getQueryParams()['token'] ?? '';
77d403609dSGreg Roach
78d403609dSGreg Roach        $title = I18N::translate('User verification');
79d403609dSGreg Roach
80d403609dSGreg Roach        $user = $this->user_service->findByUserName($username);
81d403609dSGreg Roach
82d403609dSGreg Roach        if ($user instanceof User && $user->getPreference('reg_hashcode') === $token) {
83d403609dSGreg Roach            foreach ($this->user_service->administrators() as $administrator) {
84d403609dSGreg Roach                // switch language to administrator settings
85d403609dSGreg Roach                I18N::init($administrator->getPreference('language'));
86d403609dSGreg Roach
87d403609dSGreg Roach                $base_url = $request->getAttribute('base_url');
88d403609dSGreg Roach
89d403609dSGreg Roach                /* I18N: %s is a server name/URL */
90d403609dSGreg Roach                $subject = I18N::translate('New user at %s', $base_url);
91d403609dSGreg Roach
92*e381f98dSGreg Roach                $this->email_service->send(
93d403609dSGreg Roach                    new SiteUser(),
94d403609dSGreg Roach                    $administrator,
95d403609dSGreg Roach                    new NoReplyUser(),
96d403609dSGreg Roach                    $subject,
97d403609dSGreg Roach                    view('emails/verify-notify-text', ['user' => $user]),
98d403609dSGreg Roach                    view('emails/verify-notify-html', ['user' => $user])
99d403609dSGreg Roach                );
100d403609dSGreg Roach
101d403609dSGreg Roach                $mail1_method = $administrator->getPreference('CONTACT_METHOD');
102d403609dSGreg Roach
103d403609dSGreg Roach                if ($mail1_method !== 'messaging3' && $mail1_method !== 'mailto' && $mail1_method !== 'none') {
104d403609dSGreg Roach                    DB::table('message')->insert([
105d403609dSGreg Roach                        'sender'     => $username,
1064874f72dSGreg Roach                        'ip_address' => $request->getAttribute('client-ip'),
107d403609dSGreg Roach                        'user_id'    => $administrator->id(),
108d403609dSGreg Roach                        'subject'    => $subject,
109d403609dSGreg Roach                        'body'       => view('emails/verify-notify-text', ['user' => $user]),
110d403609dSGreg Roach                    ]);
111d403609dSGreg Roach                }
11290a2f718SGreg Roach                I18N::init($locale->languageTag());
113d403609dSGreg Roach            }
114d403609dSGreg Roach
115d403609dSGreg Roach            $user
116d403609dSGreg Roach                ->setPreference('verified', '1')
117d403609dSGreg Roach                ->setPreference('reg_timestamp', date('U'))
118d403609dSGreg Roach                ->setPreference('reg_hashcode', '');
119d403609dSGreg Roach
120d403609dSGreg Roach            Log::addAuthenticationLog('User ' . $username . ' verified their email address');
121d403609dSGreg Roach
122d403609dSGreg Roach            return $this->viewResponse('verify-success-page', [
123d403609dSGreg Roach                'title' => $title,
124d403609dSGreg Roach            ]);
125d403609dSGreg Roach        }
126d403609dSGreg Roach
127d403609dSGreg Roach        return $this->viewResponse('verify-failure-page', [
128d403609dSGreg Roach            'title' => $title,
129d403609dSGreg Roach        ]);
130d403609dSGreg Roach    }
131d403609dSGreg Roach}
132