xref: /webtrees/app/Http/RequestHandlers/ContactPage.php (revision 0bf228069c24a7332eb6c887e08149b658118911)
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\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\MessageService;
25use Fisharebest\Webtrees\Services\UserService;
26use Fisharebest\Webtrees\Tree;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
31
32use function assert;
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    /** @var MessageService */
44    private $message_service;
45
46    /** @var UserService */
47    private $user_service;
48
49    /**
50     * MessagePage constructor.
51     *
52     * @param MessageService $message_service
53     * @param UserService    $user_service
54     */
55    public function __construct(MessageService $message_service, UserService $user_service)
56    {
57        $this->user_service    = $user_service;
58        $this->message_service = $message_service;
59    }
60
61    /**
62     * @param ServerRequestInterface $request
63     *
64     * @return ResponseInterface
65     */
66    public function handle(ServerRequestInterface $request): ResponseInterface
67    {
68        $tree = $request->getAttribute('tree');
69        assert($tree instanceof Tree);
70
71        $params     = $request->getQueryParams();
72        $body       = $params['body'] ?? '';
73        $from_email = $params['from_email'] ?? '';
74        $from_name  = $params['from_name'] ?? '';
75        $subject    = $params['subject'] ?? '';
76        $to         = $params['to'] ?? '';
77        $url        = $params['url'] ?? route(HomePage::class);
78
79        $to_user = $this->user_service->findByUserName($to);
80
81        if (!in_array($to_user, $this->message_service->validContacts($tree), false)) {
82            throw new AccessDeniedHttpException('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            'from_email' => $from_email,
92            'from_name'  => $from_name,
93            'subject'    => $subject,
94            'title'      => $title,
95            'to'         => $to,
96            'to_name'    => $to_name,
97            'tree'       => $tree,
98            'url'        => $url,
99        ]);
100    }
101}
102