xref: /webtrees/app/Http/RequestHandlers/ContactPage.php (revision f24a8589f7d8400d503c5bb64be9ca10e1637659)
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\HttpAccessDeniedException;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\CaptchaService;
26use Fisharebest\Webtrees\Services\MessageService;
27use Fisharebest\Webtrees\Services\UserService;
28use Fisharebest\Webtrees\Validator;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function in_array;
34use function route;
35
36/**
37 * Compose a message from a visitor.
38 */
39class ContactPage implements RequestHandlerInterface
40{
41    use ViewResponseTrait;
42
43    private CaptchaService $captcha_service;
44
45    private MessageService $message_service;
46
47    private UserService $user_service;
48
49    /**
50     * @param CaptchaService $captcha_service
51     * @param MessageService $message_service
52     * @param UserService    $user_service
53     */
54    public function __construct(
55        CaptchaService $captcha_service,
56        MessageService $message_service,
57        UserService $user_service
58    ) {
59        $this->captcha_service = $captcha_service;
60        $this->user_service    = $user_service;
61        $this->message_service = $message_service;
62    }
63
64    /**
65     * @param ServerRequestInterface $request
66     *
67     * @return ResponseInterface
68     */
69    public function handle(ServerRequestInterface $request): ResponseInterface
70    {
71        $tree       = Validator::attributes($request)->tree();
72        $body       = Validator::queryParams($request)->string('body', '');
73        $from_email = Validator::queryParams($request)->string('from_email', '');
74        $from_name  = Validator::queryParams($request)->string('from_name', '');
75        $subject    = Validator::queryParams($request)->string('subject', '');
76        $to         = Validator::queryParams($request)->string('to', '');
77        $url        = Validator::queryParams($request)->isLocalUrl()->string('url', route(HomePage::class));
78
79        $to_user = $this->user_service->findByUserName($to);
80
81        if ($to_user === null || !in_array($to_user, $this->message_service->validContacts($tree), false)) {
82            throw new HttpAccessDeniedException('Invalid contact user id');
83        }
84
85        $to_name = $to_user->realName();
86
87        $title = I18N::translate('Send a message');
88
89        return $this->viewResponse('contact-page', [
90            'body'       => $body,
91            'captcha'    => $this->captcha_service->createCaptcha(),
92            'from_email' => $from_email,
93            'from_name'  => $from_name,
94            'subject'    => $subject,
95            'title'      => $title,
96            'to'         => $to,
97            'to_name'    => $to_name,
98            'tree'       => $tree,
99            'url'        => $url,
100        ]);
101    }
102}
103