xref: /webtrees/app/Http/RequestHandlers/RegisterPage.php (revision 81b729d3a9a6e0a0e8b96285d1ad7955d2d0c659)
1d403609dSGreg Roach<?php
2d403609dSGreg Roach
3d403609dSGreg Roach/**
4d403609dSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
22*81b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
2370ca9c90SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
24d403609dSGreg Roachuse Fisharebest\Webtrees\I18N;
2570ca9c90SGreg Roachuse Fisharebest\Webtrees\Services\CaptchaService;
26f91b88bbSGreg Roachuse Fisharebest\Webtrees\Session;
27d403609dSGreg Roachuse Fisharebest\Webtrees\Site;
28d403609dSGreg Roachuse Psr\Http\Message\ResponseInterface;
29d403609dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3070ca9c90SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31d403609dSGreg Roach
32d403609dSGreg Roach/**
33d403609dSGreg Roach * Show a registration page.
34d403609dSGreg Roach */
3570ca9c90SGreg Roachclass RegisterPage implements RequestHandlerInterface
36d403609dSGreg Roach{
3770ca9c90SGreg Roach    use ViewResponseTrait;
3870ca9c90SGreg Roach
39c4943cffSGreg Roach    private CaptchaService $captcha_service;
4070ca9c90SGreg Roach
4170ca9c90SGreg Roach    /**
4270ca9c90SGreg Roach     * RegisterPage constructor.
4370ca9c90SGreg Roach     *
4470ca9c90SGreg Roach     * @param CaptchaService $captcha_service
4570ca9c90SGreg Roach     */
4670ca9c90SGreg Roach    public function __construct(CaptchaService $captcha_service)
4770ca9c90SGreg Roach    {
4870ca9c90SGreg Roach        $this->captcha_service = $captcha_service;
4970ca9c90SGreg Roach    }
5070ca9c90SGreg Roach
51d403609dSGreg Roach    /**
52d403609dSGreg Roach     * @param ServerRequestInterface $request
53d403609dSGreg Roach     *
54d403609dSGreg Roach     * @return ResponseInterface
55d403609dSGreg Roach     */
56d403609dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
57d403609dSGreg Roach    {
58d403609dSGreg Roach        $this->checkRegistrationAllowed();
59d403609dSGreg Roach
60a49d0e3fSGreg Roach        $tree     = $request->getAttribute('tree');
61f91b88bbSGreg Roach        $comments = Session::get('register_comments', '');
62f91b88bbSGreg Roach        $email    = Session::get('register_email', '');
63f91b88bbSGreg Roach        $realname = Session::get('register_realname', '');
64f91b88bbSGreg Roach        $username = Session::get('register_username', '');
65d403609dSGreg Roach
66d403609dSGreg Roach        $show_caution = Site::getPreference('SHOW_REGISTER_CAUTION') === '1';
67d403609dSGreg Roach
68d403609dSGreg Roach        $title = I18N::translate('Request a new user account');
69d403609dSGreg Roach
70d403609dSGreg Roach        return $this->viewResponse('register-page', [
7170ca9c90SGreg Roach            'captcha'      => $this->captcha_service->createCaptcha(),
72d403609dSGreg Roach            'comments'     => $comments,
73d403609dSGreg Roach            'email'        => $email,
74d403609dSGreg Roach            'realname'     => $realname,
75d403609dSGreg Roach            'show_caution' => $show_caution,
76d403609dSGreg Roach            'title'        => $title,
77a49d0e3fSGreg Roach            'tree'         => $tree,
78d403609dSGreg Roach            'username'     => $username,
79d403609dSGreg Roach        ]);
80d403609dSGreg Roach    }
81d403609dSGreg Roach
82d403609dSGreg Roach    /**
83d403609dSGreg Roach     * Check that visitors are allowed to register on this site.
84d403609dSGreg Roach     *
85d403609dSGreg Roach     * @return void
86d501c45dSGreg Roach     * @throws HttpNotFoundException
87d403609dSGreg Roach     */
88d403609dSGreg Roach    private function checkRegistrationAllowed(): void
89d403609dSGreg Roach    {
90d403609dSGreg Roach        if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') {
91d501c45dSGreg Roach            throw new HttpNotFoundException();
92d403609dSGreg Roach        }
93d403609dSGreg Roach    }
94d403609dSGreg Roach}
95