xref: /webtrees/app/Http/RequestHandlers/RegisterAction.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
1d403609dSGreg Roach<?php
2d403609dSGreg Roach
3d403609dSGreg Roach/**
4d403609dSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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 Exception;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
25d403609dSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
2681b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
2798b7e8b3SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
28d403609dSGreg Roachuse Fisharebest\Webtrees\I18N;
29d403609dSGreg Roachuse Fisharebest\Webtrees\Log;
30d403609dSGreg Roachuse Fisharebest\Webtrees\NoReplyUser;
3170ca9c90SGreg Roachuse Fisharebest\Webtrees\Services\CaptchaService;
32e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
338cfb5e7bSGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
349ed332c7SGreg Roachuse Fisharebest\Webtrees\Services\RateLimitService;
35d403609dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
36f91b88bbSGreg Roachuse Fisharebest\Webtrees\Session;
37d403609dSGreg Roachuse Fisharebest\Webtrees\Site;
38d403609dSGreg Roachuse Fisharebest\Webtrees\SiteUser;
39d403609dSGreg Roachuse Fisharebest\Webtrees\Tree;
40d403609dSGreg Roachuse Fisharebest\Webtrees\TreeUser;
41b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
42256b18b9SGreg Roachuse Illuminate\Support\Str;
43d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
44d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
4598b7e8b3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
46f3874e19SGreg Roach
479fa6ab69SGreg Roachuse function view;
4890a2f718SGreg Roach
49d403609dSGreg Roach/**
50d403609dSGreg Roach * Process a user registration.
51d403609dSGreg Roach */
5298b7e8b3SGreg Roachclass RegisterAction implements RequestHandlerInterface
53d403609dSGreg Roach{
5498b7e8b3SGreg Roach    use ViewResponseTrait;
5598b7e8b3SGreg Roach
56c4943cffSGreg Roach    private CaptchaService $captcha_service;
5770ca9c90SGreg Roach
58c4943cffSGreg Roach    private EmailService $email_service;
59d403609dSGreg Roach
609ed332c7SGreg Roach    private RateLimitService $rate_limit_service;
619ed332c7SGreg Roach
62c4943cffSGreg Roach    private UserService $user_service;
63d403609dSGreg Roach
64d403609dSGreg Roach    /**
6570ca9c90SGreg Roach     * @param CaptchaService   $captcha_service
66e381f98dSGreg Roach     * @param EmailService     $email_service
679ed332c7SGreg Roach     * @param RateLimitService $rate_limit_service
68d403609dSGreg Roach     * @param UserService      $user_service
69d403609dSGreg Roach     */
7070ca9c90SGreg Roach    public function __construct(
7170ca9c90SGreg Roach        CaptchaService $captcha_service,
7270ca9c90SGreg Roach        EmailService $email_service,
739ed332c7SGreg Roach        RateLimitService $rate_limit_service,
7470ca9c90SGreg Roach        UserService $user_service
7570ca9c90SGreg Roach    ) {
7670ca9c90SGreg Roach        $this->captcha_service    = $captcha_service;
77e381f98dSGreg Roach        $this->email_service      = $email_service;
789ed332c7SGreg Roach        $this->rate_limit_service = $rate_limit_service;
79d403609dSGreg Roach        $this->user_service       = $user_service;
80d403609dSGreg Roach    }
81d403609dSGreg Roach
82d403609dSGreg Roach    /**
83d403609dSGreg Roach     * Perform a registration.
84d403609dSGreg Roach     *
85d403609dSGreg Roach     * @param ServerRequestInterface $request
86d403609dSGreg Roach     *
87d403609dSGreg Roach     * @return ResponseInterface
88d403609dSGreg Roach     */
89d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
90d403609dSGreg Roach    {
91d403609dSGreg Roach        $this->checkRegistrationAllowed();
92d403609dSGreg Roach
93748dbe15SGreg Roach        $tree     = Validator::attributes($request)->treeOptional();
94748dbe15SGreg Roach        $comments = Validator::parsedBody($request)->string('comments');
95748dbe15SGreg Roach        $email    = Validator::parsedBody($request)->string('email');
96748dbe15SGreg Roach        $password = Validator::parsedBody($request)->string('password');
97748dbe15SGreg Roach        $realname = Validator::parsedBody($request)->string('realname');
98748dbe15SGreg Roach        $username = Validator::parsedBody($request)->string('username');
99d403609dSGreg Roach
100d403609dSGreg Roach        try {
10170ca9c90SGreg Roach            if ($this->captcha_service->isRobot($request)) {
10270ca9c90SGreg Roach                throw new Exception(I18N::translate('Please try again.'));
10370ca9c90SGreg Roach            }
10470ca9c90SGreg Roach
105f91b88bbSGreg Roach            $this->doValidateRegistration($request, $username, $email, $realname, $comments, $password);
106f91b88bbSGreg Roach
107f91b88bbSGreg Roach            Session::forget('register_comments');
108f91b88bbSGreg Roach            Session::forget('register_email');
109f91b88bbSGreg Roach            Session::forget('register_realname');
110f91b88bbSGreg Roach            Session::forget('register_username');
111d403609dSGreg Roach        } catch (Exception $ex) {
112d403609dSGreg Roach            FlashMessages::addMessage($ex->getMessage(), 'danger');
113d403609dSGreg Roach
114f91b88bbSGreg Roach            Session::put('register_comments', $comments);
115f91b88bbSGreg Roach            Session::put('register_email', $email);
116f91b88bbSGreg Roach            Session::put('register_realname', $realname);
117f91b88bbSGreg Roach            Session::put('register_username', $username);
118f91b88bbSGreg Roach
119f91b88bbSGreg Roach            return redirect(route(RegisterPage::class));
120d403609dSGreg Roach        }
121d403609dSGreg Roach
1229ed332c7SGreg Roach        $this->rate_limit_service->limitRateForSite(5, 300, 'rate-limit-registration');
1239ed332c7SGreg Roach
124d403609dSGreg Roach        Log::addAuthenticationLog('User registration requested for: ' . $username);
125d403609dSGreg Roach
126d403609dSGreg Roach        $user  = $this->user_service->create($username, $realname, $email, $password);
127256b18b9SGreg Roach        $token = Str::random(32);
128256b18b9SGreg Roach
1291fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag());
1308a07c98eSGreg Roach        $user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'));
1311fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '');
1321fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '');
1331fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U'));
1341fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_VERIFICATION_TOKEN, $token);
1358cfb5e7bSGreg Roach        $user->setPreference(UserInterface::PREF_CONTACT_METHOD, MessageService::CONTACT_METHOD_INTERNAL_AND_EMAIL);
136f91b88bbSGreg Roach        $user->setPreference(UserInterface::PREF_NEW_ACCOUNT_COMMENT, $comments);
1371fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, '1');
1381fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS, '');
1391fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '');
1401fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0');
141d403609dSGreg Roach
142b55cbc6bSGreg Roach        $base_url = Validator::attributes($request)->string('base_url');
143d403609dSGreg Roach        $reply_to = $tree instanceof Tree ? new TreeUser($tree) : new SiteUser();
144d403609dSGreg Roach
145f917a287SGreg Roach        $verify_url = route(VerifyEmail::class, [
146f917a287SGreg Roach            'username' => $user->userName(),
147256b18b9SGreg Roach            'token'    => $token,
14881bf3221SGreg Roach            'tree'     => $tree?->name(),
149f917a287SGreg Roach        ]);
150f917a287SGreg Roach
151d403609dSGreg Roach        // Send a verification message to the user.
152d403609dSGreg Roach        /* I18N: %s is a server name/URL */
153e381f98dSGreg Roach        $this->email_service->send(
154b4c5c807SGreg Roach            new SiteUser(),
155d403609dSGreg Roach            $user,
156d403609dSGreg Roach            $reply_to,
157d403609dSGreg Roach            I18N::translate('Your registration at %s', $base_url),
158f917a287SGreg Roach            view('emails/register-user-text', ['user' => $user, 'base_url' => $base_url, 'verify_url' => $verify_url]),
159f917a287SGreg Roach            view('emails/register-user-html', ['user' => $user, 'base_url' => $base_url, 'verify_url' => $verify_url])
160d403609dSGreg Roach        );
161d403609dSGreg Roach
162d403609dSGreg Roach        // Tell the administrators about the registration.
163d403609dSGreg Roach        foreach ($this->user_service->administrators() as $administrator) {
1641fe542e9SGreg Roach            I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE));
165d403609dSGreg Roach
166d403609dSGreg Roach            /* I18N: %s is a server name/URL */
167d403609dSGreg Roach            $subject = I18N::translate('New registration at %s', $base_url);
168d403609dSGreg Roach
1699fa6ab69SGreg Roach            $body_text = view('emails/register-notify-text', [
1709fa6ab69SGreg Roach                'user'     => $user,
171f91b88bbSGreg Roach                'comments' => $comments,
1729fa6ab69SGreg Roach                'base_url' => $base_url,
1739fa6ab69SGreg Roach                'tree'     => $tree,
1749fa6ab69SGreg Roach            ]);
1759fa6ab69SGreg Roach
1769fa6ab69SGreg Roach            $body_html = view('emails/register-notify-html', [
1779fa6ab69SGreg Roach                'user'     => $user,
178f91b88bbSGreg Roach                'comments' => $comments,
1799fa6ab69SGreg Roach                'base_url' => $base_url,
1809fa6ab69SGreg Roach                'tree'     => $tree,
1819fa6ab69SGreg Roach            ]);
1829fa6ab69SGreg Roach
183d403609dSGreg Roach            /* I18N: %s is a server name/URL */
184e381f98dSGreg Roach            $this->email_service->send(
185d403609dSGreg Roach                new SiteUser(),
186d403609dSGreg Roach                $administrator,
187d403609dSGreg Roach                new NoReplyUser(),
188d403609dSGreg Roach                $subject,
1899fa6ab69SGreg Roach                $body_text,
1909fa6ab69SGreg Roach                $body_html
191d403609dSGreg Roach            );
192d403609dSGreg Roach
1931fe542e9SGreg Roach            $mail1_method = $administrator->getPreference(UserInterface::PREF_CONTACT_METHOD);
1948cfb5e7bSGreg Roach            if (
1958cfb5e7bSGreg Roach                $mail1_method !== MessageService::CONTACT_METHOD_EMAIL &&
1968cfb5e7bSGreg Roach                $mail1_method !== MessageService::CONTACT_METHOD_MAILTO &&
1978cfb5e7bSGreg Roach                $mail1_method !== MessageService::CONTACT_METHOD_NONE
1988cfb5e7bSGreg Roach            ) {
199d403609dSGreg Roach                DB::table('message')->insert([
200d403609dSGreg Roach                    'sender'     => $user->email(),
2014874f72dSGreg Roach                    'ip_address' => $request->getAttribute('client-ip'),
202d403609dSGreg Roach                    'user_id'    => $administrator->id(),
203d403609dSGreg Roach                    'subject'    => $subject,
2049fa6ab69SGreg Roach                    'body'       => $body_text,
205d403609dSGreg Roach                ]);
206d403609dSGreg Roach            }
207d403609dSGreg Roach        }
208d403609dSGreg Roach
209d403609dSGreg Roach        $title = I18N::translate('Request a new user account');
210d403609dSGreg Roach
211d403609dSGreg Roach        return $this->viewResponse('register-success-page', [
212d403609dSGreg Roach            'title' => $title,
2139fa6ab69SGreg Roach            'tree'  => $tree,
214d403609dSGreg Roach            'user'  => $user,
215d403609dSGreg Roach        ]);
216d403609dSGreg Roach    }
217d403609dSGreg Roach
218d403609dSGreg Roach    /**
219d403609dSGreg Roach     * Check that visitors are allowed to register on this site.
220d403609dSGreg Roach     *
221d403609dSGreg Roach     * @return void
222d501c45dSGreg Roach     * @throws HttpNotFoundException
223d403609dSGreg Roach     */
224d403609dSGreg Roach    private function checkRegistrationAllowed(): void
225d403609dSGreg Roach    {
226d403609dSGreg Roach        if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') {
227d501c45dSGreg Roach            throw new HttpNotFoundException();
228d403609dSGreg Roach        }
229d403609dSGreg Roach    }
230d403609dSGreg Roach
231d403609dSGreg Roach    /**
232d403609dSGreg Roach     * Check the registration details.
233d403609dSGreg Roach     *
234d403609dSGreg Roach     * @param ServerRequestInterface $request
235d403609dSGreg Roach     * @param string                 $username
236d403609dSGreg Roach     * @param string                 $email
237d403609dSGreg Roach     * @param string                 $realname
238d403609dSGreg Roach     * @param string                 $comments
239d403609dSGreg Roach     * @param string                 $password
240d403609dSGreg Roach     *
241d403609dSGreg Roach     * @return void
242d403609dSGreg Roach     * @throws Exception
243d403609dSGreg Roach     */
244c5d4d20fSGreg Roach    private function doValidateRegistration(
245c5d4d20fSGreg Roach        ServerRequestInterface $request,
246f5c19939SGreg Roach        string $username,
247f5c19939SGreg Roach        string $email,
248f5c19939SGreg Roach        string $realname,
249f5c19939SGreg Roach        string $comments,
250c5d4d20fSGreg Roach        #[\SensitiveParameter] string $password
251c5d4d20fSGreg Roach    ): void {
252d403609dSGreg Roach        // All fields are required
253d403609dSGreg Roach        if ($username === '' || $email === '' || $realname === '' || $comments === '' || $password === '') {
254d403609dSGreg Roach            throw new Exception(I18N::translate('All fields must be completed.'));
255d403609dSGreg Roach        }
256d403609dSGreg Roach
257d403609dSGreg Roach        // Username already exists
258d403609dSGreg Roach        if ($this->user_service->findByUserName($username) !== null) {
259d403609dSGreg Roach            throw new Exception(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
260d403609dSGreg Roach        }
261d403609dSGreg Roach
262d403609dSGreg Roach        // Email already exists
263d403609dSGreg Roach        if ($this->user_service->findByEmail($email) !== null) {
264d403609dSGreg Roach            throw new Exception(I18N::translate('Duplicate email address. A user with that email already exists.'));
265d403609dSGreg Roach        }
266d403609dSGreg Roach
267b55cbc6bSGreg Roach        $base_url = Validator::attributes($request)->string('base_url');
268d403609dSGreg Roach
269d403609dSGreg Roach        // No external links
270d403609dSGreg Roach        if (preg_match('/(?!' . preg_quote($base_url, '/') . ')(((?:http|https):\/\/)[a-zA-Z0-9.-]+)/', $comments, $match)) {
271d403609dSGreg Roach            throw new Exception(I18N::translate('You are not allowed to send messages that contain external links.') . ' ' . I18N::translate('You should delete the “%1$s” from “%2$s” and try again.', e($match[2]), e($match[1])));
272d403609dSGreg Roach        }
273d403609dSGreg Roach    }
274d403609dSGreg Roach}
275