xref: /webtrees/app/Http/RequestHandlers/RegisterAction.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1d403609dSGreg Roach<?php
2d403609dSGreg Roach
3d403609dSGreg Roach/**
4d403609dSGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 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;
24d403609dSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
2698b7e8b3SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
27d403609dSGreg Roachuse Fisharebest\Webtrees\I18N;
28d403609dSGreg Roachuse Fisharebest\Webtrees\Log;
29d403609dSGreg Roachuse Fisharebest\Webtrees\NoReplyUser;
3070ca9c90SGreg Roachuse Fisharebest\Webtrees\Services\CaptchaService;
31e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\EmailService;
329ed332c7SGreg Roachuse Fisharebest\Webtrees\Services\RateLimitService;
33d403609dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
34f91b88bbSGreg Roachuse Fisharebest\Webtrees\Session;
35d403609dSGreg Roachuse Fisharebest\Webtrees\Site;
36d403609dSGreg Roachuse Fisharebest\Webtrees\SiteUser;
37d403609dSGreg Roachuse Fisharebest\Webtrees\Tree;
38d403609dSGreg Roachuse Fisharebest\Webtrees\TreeUser;
39b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
40d403609dSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
41256b18b9SGreg Roachuse Illuminate\Support\Str;
42d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
43d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
4498b7e8b3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
45f3874e19SGreg Roach
469fa6ab69SGreg Roachuse function view;
4790a2f718SGreg Roach
48d403609dSGreg Roach/**
49d403609dSGreg Roach * Process a user registration.
50d403609dSGreg Roach */
5198b7e8b3SGreg Roachclass RegisterAction implements RequestHandlerInterface
52d403609dSGreg Roach{
5398b7e8b3SGreg Roach    use ViewResponseTrait;
5498b7e8b3SGreg Roach
55c4943cffSGreg Roach    private CaptchaService $captcha_service;
5670ca9c90SGreg Roach
57c4943cffSGreg Roach    private EmailService $email_service;
58d403609dSGreg Roach
599ed332c7SGreg Roach    private RateLimitService $rate_limit_service;
609ed332c7SGreg Roach
61c4943cffSGreg Roach    private UserService $user_service;
62d403609dSGreg Roach
63d403609dSGreg Roach    /**
64d403609dSGreg Roach     * RegisterController constructor.
65d403609dSGreg Roach     *
6670ca9c90SGreg Roach     * @param CaptchaService   $captcha_service
67e381f98dSGreg Roach     * @param EmailService     $email_service
689ed332c7SGreg Roach     * @param RateLimitService $rate_limit_service
69d403609dSGreg Roach     * @param UserService      $user_service
70d403609dSGreg Roach     */
7170ca9c90SGreg Roach    public function __construct(
7270ca9c90SGreg Roach        CaptchaService $captcha_service,
7370ca9c90SGreg Roach        EmailService $email_service,
749ed332c7SGreg Roach        RateLimitService $rate_limit_service,
7570ca9c90SGreg Roach        UserService $user_service
7670ca9c90SGreg Roach    ) {
7770ca9c90SGreg Roach        $this->captcha_service    = $captcha_service;
78e381f98dSGreg Roach        $this->email_service      = $email_service;
799ed332c7SGreg Roach        $this->rate_limit_service = $rate_limit_service;
80d403609dSGreg Roach        $this->user_service       = $user_service;
81d403609dSGreg Roach    }
82d403609dSGreg Roach
83d403609dSGreg Roach    /**
84d403609dSGreg Roach     * Perform a registration.
85d403609dSGreg Roach     *
86d403609dSGreg Roach     * @param ServerRequestInterface $request
87d403609dSGreg Roach     *
88d403609dSGreg Roach     * @return ResponseInterface
89d403609dSGreg Roach     */
90d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
91d403609dSGreg Roach    {
92b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
939fa6ab69SGreg Roach
94d403609dSGreg Roach        $this->checkRegistrationAllowed();
95d403609dSGreg Roach
96b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
97b46c87bdSGreg Roach
98f91b88bbSGreg Roach        $comments = $params['comments'] ?? '';
99b46c87bdSGreg Roach        $email    = $params['email'] ?? '';
100b46c87bdSGreg Roach        $password = $params['password'] ?? '';
101b46c87bdSGreg Roach        $realname = $params['realname'] ?? '';
102b46c87bdSGreg Roach        $username = $params['username'] ?? '';
103d403609dSGreg Roach
104d403609dSGreg Roach        try {
10570ca9c90SGreg Roach            if ($this->captcha_service->isRobot($request)) {
10670ca9c90SGreg Roach                throw new Exception(I18N::translate('Please try again.'));
10770ca9c90SGreg Roach            }
10870ca9c90SGreg Roach
109f91b88bbSGreg Roach            $this->doValidateRegistration($request, $username, $email, $realname, $comments, $password);
110f91b88bbSGreg Roach
111f91b88bbSGreg Roach            Session::forget('register_comments');
112f91b88bbSGreg Roach            Session::forget('register_email');
113f91b88bbSGreg Roach            Session::forget('register_realname');
114f91b88bbSGreg Roach            Session::forget('register_username');
115d403609dSGreg Roach        } catch (Exception $ex) {
116d403609dSGreg Roach            FlashMessages::addMessage($ex->getMessage(), 'danger');
117d403609dSGreg Roach
118f91b88bbSGreg Roach            Session::put('register_comments', $comments);
119f91b88bbSGreg Roach            Session::put('register_email', $email);
120f91b88bbSGreg Roach            Session::put('register_realname', $realname);
121f91b88bbSGreg Roach            Session::put('register_username', $username);
122f91b88bbSGreg Roach
123f91b88bbSGreg Roach            return redirect(route(RegisterPage::class));
124d403609dSGreg Roach        }
125d403609dSGreg Roach
1269ed332c7SGreg Roach        $this->rate_limit_service->limitRateForSite(5, 300, 'rate-limit-registration');
1279ed332c7SGreg Roach
128d403609dSGreg Roach        Log::addAuthenticationLog('User registration requested for: ' . $username);
129d403609dSGreg Roach
130d403609dSGreg Roach        $user  = $this->user_service->create($username, $realname, $email, $password);
131256b18b9SGreg Roach        $token = Str::random(32);
132256b18b9SGreg Roach
1331fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag());
1348a07c98eSGreg Roach        $user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'));
1351fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '');
1361fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '');
1371fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U'));
1381fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_VERIFICATION_TOKEN, $token);
1391fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_CONTACT_METHOD, 'messaging2');
140f91b88bbSGreg Roach        $user->setPreference(UserInterface::PREF_NEW_ACCOUNT_COMMENT, $comments);
1411fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, '1');
1421fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS, '');
1431fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '');
1441fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0');
145d403609dSGreg Roach
146b55cbc6bSGreg Roach        $base_url = Validator::attributes($request)->string('base_url');
147d403609dSGreg Roach        $reply_to = $tree instanceof Tree ? new TreeUser($tree) : new SiteUser();
148d403609dSGreg Roach
149f917a287SGreg Roach        $verify_url = route(VerifyEmail::class, [
150f917a287SGreg Roach            'username' => $user->userName(),
151256b18b9SGreg Roach            'token'    => $token,
152f917a287SGreg Roach            'tree'     => $tree instanceof Tree ? $tree->name() : null,
153f917a287SGreg Roach        ]);
154f917a287SGreg Roach
155d403609dSGreg Roach        // Send a verification message to the user.
156d403609dSGreg Roach        /* I18N: %s is a server name/URL */
157e381f98dSGreg Roach        $this->email_service->send(
158b4c5c807SGreg Roach            new SiteUser(),
159d403609dSGreg Roach            $user,
160d403609dSGreg Roach            $reply_to,
161d403609dSGreg Roach            I18N::translate('Your registration at %s', $base_url),
162f917a287SGreg Roach            view('emails/register-user-text', ['user' => $user, 'base_url' => $base_url, 'verify_url' => $verify_url]),
163f917a287SGreg Roach            view('emails/register-user-html', ['user' => $user, 'base_url' => $base_url, 'verify_url' => $verify_url])
164d403609dSGreg Roach        );
165d403609dSGreg Roach
166d403609dSGreg Roach        // Tell the administrators about the registration.
167d403609dSGreg Roach        foreach ($this->user_service->administrators() as $administrator) {
1681fe542e9SGreg Roach            I18N::init($administrator->getPreference(UserInterface::PREF_LANGUAGE));
169d403609dSGreg Roach
170d403609dSGreg Roach            /* I18N: %s is a server name/URL */
171d403609dSGreg Roach            $subject = I18N::translate('New registration at %s', $base_url);
172d403609dSGreg Roach
1739fa6ab69SGreg Roach            $body_text = view('emails/register-notify-text', [
1749fa6ab69SGreg Roach                'user'     => $user,
175f91b88bbSGreg Roach                'comments' => $comments,
1769fa6ab69SGreg Roach                'base_url' => $base_url,
1779fa6ab69SGreg Roach                'tree'     => $tree,
1789fa6ab69SGreg Roach            ]);
1799fa6ab69SGreg Roach
1809fa6ab69SGreg Roach            $body_html = view('emails/register-notify-html', [
1819fa6ab69SGreg Roach                'user'     => $user,
182f91b88bbSGreg Roach                'comments' => $comments,
1839fa6ab69SGreg Roach                'base_url' => $base_url,
1849fa6ab69SGreg Roach                'tree'     => $tree,
1859fa6ab69SGreg Roach            ]);
1869fa6ab69SGreg Roach
1879fa6ab69SGreg Roach
188d403609dSGreg Roach            /* I18N: %s is a server name/URL */
189e381f98dSGreg Roach            $this->email_service->send(
190d403609dSGreg Roach                new SiteUser(),
191d403609dSGreg Roach                $administrator,
192d403609dSGreg Roach                new NoReplyUser(),
193d403609dSGreg Roach                $subject,
1949fa6ab69SGreg Roach                $body_text,
1959fa6ab69SGreg Roach                $body_html
196d403609dSGreg Roach            );
197d403609dSGreg Roach
1981fe542e9SGreg Roach            $mail1_method = $administrator->getPreference(UserInterface::PREF_CONTACT_METHOD);
199d403609dSGreg Roach            if ($mail1_method !== 'messaging3' && $mail1_method !== 'mailto' && $mail1_method !== 'none') {
200d403609dSGreg Roach                DB::table('message')->insert([
201d403609dSGreg Roach                    'sender'     => $user->email(),
2024874f72dSGreg Roach                    'ip_address' => $request->getAttribute('client-ip'),
203d403609dSGreg Roach                    'user_id'    => $administrator->id(),
204d403609dSGreg Roach                    'subject'    => $subject,
2059fa6ab69SGreg Roach                    'body'       => $body_text,
206d403609dSGreg Roach                ]);
207d403609dSGreg Roach            }
208d403609dSGreg Roach        }
209d403609dSGreg Roach
210d403609dSGreg Roach        $title = I18N::translate('Request a new user account');
211d403609dSGreg Roach
212d403609dSGreg Roach        return $this->viewResponse('register-success-page', [
213d403609dSGreg Roach            'title' => $title,
2149fa6ab69SGreg Roach            'tree'  => $tree,
215d403609dSGreg Roach            'user'  => $user,
216d403609dSGreg Roach        ]);
217d403609dSGreg Roach    }
218d403609dSGreg Roach
219d403609dSGreg Roach    /**
220d403609dSGreg Roach     * Check that visitors are allowed to register on this site.
221d403609dSGreg Roach     *
222d403609dSGreg Roach     * @return void
223d501c45dSGreg Roach     * @throws HttpNotFoundException
224d403609dSGreg Roach     */
225d403609dSGreg Roach    private function checkRegistrationAllowed(): void
226d403609dSGreg Roach    {
227d403609dSGreg Roach        if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') {
228d501c45dSGreg Roach            throw new HttpNotFoundException();
229d403609dSGreg Roach        }
230d403609dSGreg Roach    }
231d403609dSGreg Roach
232d403609dSGreg Roach    /**
233d403609dSGreg Roach     * Check the registration details.
234d403609dSGreg Roach     *
235d403609dSGreg Roach     * @param ServerRequestInterface $request
236d403609dSGreg Roach     * @param string                 $username
237d403609dSGreg Roach     * @param string                 $email
238d403609dSGreg Roach     * @param string                 $realname
239d403609dSGreg Roach     * @param string                 $comments
240d403609dSGreg Roach     * @param string                 $password
241d403609dSGreg Roach     *
242d403609dSGreg Roach     * @return void
243d403609dSGreg Roach     * @throws Exception
244d403609dSGreg Roach     */
245d403609dSGreg Roach    private function doValidateRegistration(ServerRequestInterface $request, string $username, string $email, string $realname, string $comments, string $password): void
246d403609dSGreg Roach    {
247d403609dSGreg Roach        // All fields are required
248d403609dSGreg Roach        if ($username === '' || $email === '' || $realname === '' || $comments === '' || $password === '') {
249d403609dSGreg Roach            throw new Exception(I18N::translate('All fields must be completed.'));
250d403609dSGreg Roach        }
251d403609dSGreg Roach
252d403609dSGreg Roach        // Username already exists
253d403609dSGreg Roach        if ($this->user_service->findByUserName($username) !== null) {
254d403609dSGreg Roach            throw new Exception(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
255d403609dSGreg Roach        }
256d403609dSGreg Roach
257d403609dSGreg Roach        // Email already exists
258d403609dSGreg Roach        if ($this->user_service->findByEmail($email) !== null) {
259d403609dSGreg Roach            throw new Exception(I18N::translate('Duplicate email address. A user with that email already exists.'));
260d403609dSGreg Roach        }
261d403609dSGreg Roach
262b55cbc6bSGreg Roach        $base_url = Validator::attributes($request)->string('base_url');
263d403609dSGreg Roach
264d403609dSGreg Roach        // No external links
265d403609dSGreg Roach        if (preg_match('/(?!' . preg_quote($base_url, '/') . ')(((?:http|https):\/\/)[a-zA-Z0-9.-]+)/', $comments, $match)) {
266d403609dSGreg 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])));
267d403609dSGreg Roach        }
268d403609dSGreg Roach    }
269d403609dSGreg Roach}
270