xref: /webtrees/app/Http/RequestHandlers/ContactPage.php (revision b63ebfff10c4f748c10374a14a84550d3b653912)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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     * MessagePage constructor.
51     *
52     * @param CaptchaService $captcha_service
53     * @param MessageService $message_service
54     * @param UserService    $user_service
55     */
56    public function __construct(
57        CaptchaService $captcha_service,
58        MessageService $message_service,
59        UserService $user_service
60    ) {
61        $this->captcha_service = $captcha_service;
62        $this->user_service    = $user_service;
63        $this->message_service = $message_service;
64    }
65
66    /**
67     * @param ServerRequestInterface $request
68     *
69     * @return ResponseInterface
70     */
71    public function handle(ServerRequestInterface $request): ResponseInterface
72    {
73        $tree       = Validator::attributes($request)->tree();
74        $body       = Validator::queryParams($request)->string('body', '');
75        $from_email = Validator::queryParams($request)->string('from_email', '');
76        $from_name  = Validator::queryParams($request)->string('from_name', '');
77        $subject    = Validator::queryParams($request)->string('subject', '');
78        $to         = Validator::queryParams($request)->string('to', '');
79        $url        = Validator::queryParams($request)->isLocalUrl()->string('url', route(HomePage::class));
80
81        $to_user = $this->user_service->findByUserName($to);
82
83        if ($to_user === null || !in_array($to_user, $this->message_service->validContacts($tree), false)) {
84            throw new HttpAccessDeniedException('Invalid contact user id');
85        }
86
87        $to_name = $to_user->realName();
88
89        $title = I18N::translate('Send a message');
90
91        return $this->viewResponse('contact-page', [
92            'body'       => $body,
93            'captcha'    => $this->captcha_service->createCaptcha(),
94            'from_email' => $from_email,
95            'from_name'  => $from_name,
96            'subject'    => $subject,
97            'title'      => $title,
98            'to'         => $to,
99            'to_name'    => $to_name,
100            'tree'       => $tree,
101            'url'        => $url,
102        ]);
103    }
104}
105