xref: /webtrees/app/Http/RequestHandlers/RegisterPage.php (revision b11cdcd45131b1585d66693fab363cfeb18c51a4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * 'Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\CaptchaService;
26use Fisharebest\Webtrees\Session;
27use Fisharebest\Webtrees\Site;
28use Fisharebest\Webtrees\Validator;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function is_string;
34
35/**
36 * Show a registration page.
37 */
38class RegisterPage implements RequestHandlerInterface
39{
40    use ViewResponseTrait;
41
42    private CaptchaService $captcha_service;
43
44    /**
45     * RegisterPage constructor.
46     *
47     * @param CaptchaService $captcha_service
48     */
49    public function __construct(CaptchaService $captcha_service)
50    {
51        $this->captcha_service = $captcha_service;
52    }
53
54    /**
55     * @param ServerRequestInterface $request
56     *
57     * @return ResponseInterface
58     */
59    public function handle(ServerRequestInterface $request): ResponseInterface
60    {
61        $this->checkRegistrationAllowed();
62
63        $tree = Validator::attributes($request)->treeOptional();
64
65        $comments = Session::get('register_comments');
66        $comments = is_string($comments) ? $comments : '';
67
68        $email    = Session::get('register_email');
69        $email    = is_string($email) ? $email : '';
70
71        $realname = Session::get('register_realname');
72        $realname = is_string($realname) ? $realname : '';
73
74        $username = Session::get('register_username');
75        $username = is_string($username) ? $username : '';
76
77        $show_caution = Site::getPreference('SHOW_REGISTER_CAUTION') === '1';
78
79        $title = I18N::translate('Request a new user account');
80
81        return $this->viewResponse('register-page', [
82            'captcha'      => $this->captcha_service->createCaptcha(),
83            'comments'     => $comments,
84            'email'        => $email,
85            'realname'     => $realname,
86            'show_caution' => $show_caution,
87            'title'        => $title,
88            'tree'         => $tree,
89            'username'     => $username,
90        ]);
91    }
92
93    /**
94     * Check that visitors are allowed to register on this site.
95     *
96     * @return void
97     * @throws HttpNotFoundException
98     */
99    private function checkRegistrationAllowed(): void
100    {
101        if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') {
102            throw new HttpNotFoundException();
103        }
104    }
105}
106