xref: /webtrees/app/Http/RequestHandlers/RegisterPage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
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     * @param CaptchaService $captcha_service
46     */
47    public function __construct(CaptchaService $captcha_service)
48    {
49        $this->captcha_service = $captcha_service;
50    }
51
52    /**
53     * @param ServerRequestInterface $request
54     *
55     * @return ResponseInterface
56     */
57    public function handle(ServerRequestInterface $request): ResponseInterface
58    {
59        $this->checkRegistrationAllowed();
60
61        $tree = Validator::attributes($request)->treeOptional();
62
63        $comments = Session::get('register_comments');
64        $comments = is_string($comments) ? $comments : '';
65
66        $email    = Session::get('register_email');
67        $email    = is_string($email) ? $email : '';
68
69        $realname = Session::get('register_realname');
70        $realname = is_string($realname) ? $realname : '';
71
72        $username = Session::get('register_username');
73        $username = is_string($username) ? $username : '';
74
75        $show_caution = Site::getPreference('SHOW_REGISTER_CAUTION') === '1';
76
77        $title = I18N::translate('Request a new user account');
78
79        return $this->viewResponse('register-page', [
80            'captcha'      => $this->captcha_service->createCaptcha(),
81            'comments'     => $comments,
82            'email'        => $email,
83            'realname'     => $realname,
84            'show_caution' => $show_caution,
85            'title'        => $title,
86            'tree'         => $tree,
87            'username'     => $username,
88        ]);
89    }
90
91    /**
92     * Check that visitors are allowed to register on this site.
93     *
94     * @return void
95     * @throws HttpNotFoundException
96     */
97    private function checkRegistrationAllowed(): void
98    {
99        if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') {
100            throw new HttpNotFoundException();
101        }
102    }
103}
104