xref: /webtrees/app/Http/RequestHandlers/VerifyEmail.php (revision 65cf57062fc712114f930335533210fd95170cea)
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
22d403609dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
23d403609dSGreg Roachuse Fisharebest\Webtrees\I18N;
24d403609dSGreg Roachuse Fisharebest\Webtrees\Log;
25d403609dSGreg Roachuse Fisharebest\Webtrees\NoReplyUser;
26e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
27d403609dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
28d403609dSGreg Roachuse Fisharebest\Webtrees\SiteUser;
29d403609dSGreg Roachuse Fisharebest\Webtrees\User;
30d403609dSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
31d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
32d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
33d403609dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
34d403609dSGreg Roach
35d403609dSGreg Roach/**
36d403609dSGreg Roach * Acknowledge an email verification code.
37d403609dSGreg Roach */
38d403609dSGreg Roachclass VerifyEmail implements RequestHandlerInterface
39d403609dSGreg Roach{
40d403609dSGreg Roach    use ViewResponseTrait;
41d403609dSGreg Roach
42e381f98dSGreg Roach    /** @var EmailService */
43e381f98dSGreg Roach    private $email_service;
44f3874e19SGreg Roach
45f3874e19SGreg Roach    /** @var UserService */
46d403609dSGreg Roach    private $user_service;
47d403609dSGreg Roach
48d403609dSGreg Roach    /**
49d403609dSGreg Roach     * MessageController constructor.
50d403609dSGreg Roach     *
51e381f98dSGreg Roach     * @param EmailService $email_service
52d403609dSGreg Roach     * @param UserService  $user_service
53d403609dSGreg Roach     */
54e381f98dSGreg Roach    public function __construct(EmailService $email_service, UserService $user_service)
55d403609dSGreg Roach    {
56e381f98dSGreg Roach        $this->email_service = $email_service;
57d403609dSGreg Roach        $this->user_service  = $user_service;
58d403609dSGreg Roach    }
59d403609dSGreg Roach
60d403609dSGreg Roach    /**
61d403609dSGreg Roach     * Respond to a verification link that was emailed to a user.
62d403609dSGreg Roach     *
63d403609dSGreg Roach     * @param ServerRequestInterface $request
64d403609dSGreg Roach     *
65d403609dSGreg Roach     * @return ResponseInterface
66d403609dSGreg Roach     */
67d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
68d403609dSGreg Roach    {
69d403609dSGreg Roach        $username = $request->getQueryParams()['username'] ?? '';
70d403609dSGreg Roach        $token    = $request->getQueryParams()['token'] ?? '';
71d403609dSGreg Roach
72d403609dSGreg Roach        $title = I18N::translate('User verification');
73d403609dSGreg Roach
74d403609dSGreg Roach        $user = $this->user_service->findByUserName($username);
75d403609dSGreg Roach
767c4add84SGreg Roach        if ($user instanceof User && $user->getPreference(User::PREF_VERIFICATION_TOKEN) === $token) {
77*65cf5706SGreg Roach            $old_language = I18N::languageTag();
78*65cf5706SGreg Roach
79d403609dSGreg Roach            foreach ($this->user_service->administrators() as $administrator) {
80d403609dSGreg Roach                // switch language to administrator settings
817c4add84SGreg Roach                I18N::init($administrator->getPreference(User::PREF_LANGUAGE));
82d403609dSGreg Roach
83d403609dSGreg Roach                $base_url = $request->getAttribute('base_url');
84d403609dSGreg Roach
85d403609dSGreg Roach                /* I18N: %s is a server name/URL */
86d403609dSGreg Roach                $subject = I18N::translate('New user at %s', $base_url);
87d403609dSGreg Roach
88e381f98dSGreg Roach                $this->email_service->send(
89d403609dSGreg Roach                    new SiteUser(),
90d403609dSGreg Roach                    $administrator,
91d403609dSGreg Roach                    new NoReplyUser(),
92d403609dSGreg Roach                    $subject,
93d403609dSGreg Roach                    view('emails/verify-notify-text', ['user' => $user]),
94d403609dSGreg Roach                    view('emails/verify-notify-html', ['user' => $user])
95d403609dSGreg Roach                );
96d403609dSGreg Roach
97d403609dSGreg Roach                $mail1_method = $administrator->getPreference('CONTACT_METHOD');
98d403609dSGreg Roach
99d403609dSGreg Roach                if ($mail1_method !== 'messaging3' && $mail1_method !== 'mailto' && $mail1_method !== 'none') {
100d403609dSGreg Roach                    DB::table('message')->insert([
101d403609dSGreg Roach                        'sender'     => $username,
1024874f72dSGreg Roach                        'ip_address' => $request->getAttribute('client-ip'),
103d403609dSGreg Roach                        'user_id'    => $administrator->id(),
104d403609dSGreg Roach                        'subject'    => $subject,
105d403609dSGreg Roach                        'body'       => view('emails/verify-notify-text', ['user' => $user]),
106d403609dSGreg Roach                    ]);
107d403609dSGreg Roach                }
108d403609dSGreg Roach            }
109*65cf5706SGreg Roach            I18N::init($old_language);
110d403609dSGreg Roach
1117c4add84SGreg Roach            $user->setPreference(User::PREF_IS_EMAIL_VERIFIED, '1');
1127c4add84SGreg Roach            $user->setPreference(User::PREF_TIMESTAMP_REGISTERED, date('U'));
1137c4add84SGreg Roach            $user->setPreference(User::PREF_VERIFICATION_TOKEN, '');
114d403609dSGreg Roach
115d403609dSGreg Roach            Log::addAuthenticationLog('User ' . $username . ' verified their email address');
116d403609dSGreg Roach
117d403609dSGreg Roach            return $this->viewResponse('verify-success-page', [
118d403609dSGreg Roach                'title' => $title,
119d403609dSGreg Roach            ]);
120d403609dSGreg Roach        }
121d403609dSGreg Roach
122d403609dSGreg Roach        return $this->viewResponse('verify-failure-page', [
123d403609dSGreg Roach            'title' => $title,
124d403609dSGreg Roach        ]);
125d403609dSGreg Roach    }
126d403609dSGreg Roach}
127