xref: /webtrees/app/Http/RequestHandlers/RegisterPage.php (revision 70ca9c9048575cc15d5ce70c697595af107ab972)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\CaptchaService;
26use Fisharebest\Webtrees\Site;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31/**
32 * Show a registration page.
33 */
34class RegisterPage implements RequestHandlerInterface
35{
36    use ViewResponseTrait;
37
38    /** @var CaptchaService */
39    private $captcha_service;
40
41    /**
42     * RegisterPage constructor.
43     *
44     * @param CaptchaService $captcha_service
45     */
46    public function __construct(CaptchaService $captcha_service)
47    {
48        $this->captcha_service = $captcha_service;
49    }
50
51    /**
52     * @param ServerRequestInterface $request
53     *
54     * @return ResponseInterface
55     */
56    public function handle(ServerRequestInterface $request): ResponseInterface
57    {
58        $this->checkRegistrationAllowed();
59
60        $tree     = $request->getAttribute('tree');
61        $comments = $request->getQueryParams()['comments'] ?? '';
62        $email    = $request->getQueryParams()['email'] ?? '';
63        $realname = $request->getQueryParams()['realname'] ?? '';
64        $username = $request->getQueryParams()['username'] ?? '';
65
66        $show_caution = Site::getPreference('SHOW_REGISTER_CAUTION') === '1';
67
68        $title = I18N::translate('Request a new user account');
69
70        return $this->viewResponse('register-page', [
71            'captcha'      => $this->captcha_service->createCaptcha(),
72            'comments'     => $comments,
73            'email'        => $email,
74            'realname'     => $realname,
75            'show_caution' => $show_caution,
76            'title'        => $title,
77            'tree'         => $tree,
78            'username'     => $username,
79        ]);
80    }
81
82    /**
83     * Check that visitors are allowed to register on this site.
84     *
85     * @return void
86     * @throws HttpNotFoundException
87     */
88    private function checkRegistrationAllowed(): void
89    {
90        if (Site::getPreference('USE_REGISTRATION_MODULE') !== '1') {
91            throw new HttpNotFoundException();
92        }
93    }
94}
95