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