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