xref: /webtrees/app/Http/RequestHandlers/ContactPage.php (revision e3c147d0d53873311b7c137c41b4439e01d4189e)
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\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    /** @var CaptchaService */
45    private $captcha_service;
46
47    /** @var MessageService */
48    private $message_service;
49
50    /** @var UserService */
51    private $user_service;
52
53    /**
54     * MessagePage constructor.
55     *
56     * @param CaptchaService $captcha_service
57     * @param MessageService $message_service
58     * @param UserService    $user_service
59     */
60    public function __construct(
61        CaptchaService $captcha_service,
62        MessageService $message_service,
63        UserService $user_service
64    ) {
65        $this->captcha_service = $captcha_service;
66        $this->user_service    = $user_service;
67        $this->message_service = $message_service;
68    }
69
70    /**
71     * @param ServerRequestInterface $request
72     *
73     * @return ResponseInterface
74     */
75    public function handle(ServerRequestInterface $request): ResponseInterface
76    {
77        $tree = $request->getAttribute('tree');
78        assert($tree instanceof Tree);
79
80        $params     = $request->getQueryParams();
81        $body       = $params['body'] ?? '';
82        $from_email = $params['from_email'] ?? '';
83        $from_name  = $params['from_name'] ?? '';
84        $subject    = $params['subject'] ?? '';
85        $to         = $params['to'] ?? '';
86        $url        = $params['url'] ?? route(HomePage::class);
87
88        $to_user = $this->user_service->findByUserName($to);
89
90        if (!in_array($to_user, $this->message_service->validContacts($tree), false)) {
91            throw new HttpAccessDeniedException('Invalid contact user id');
92        }
93
94        $to_name = $to_user->realName();
95
96        $title = I18N::translate('Send a message');
97
98        return $this->viewResponse('contact-page', [
99            'body'       => $body,
100            'captcha'    => $this->captcha_service->createCaptcha(),
101            'from_email' => $from_email,
102            'from_name'  => $from_name,
103            'subject'    => $subject,
104            'title'      => $title,
105            'to'         => $to,
106            'to_name'    => $to_name,
107            'tree'       => $tree,
108            'url'        => $url,
109        ]);
110    }
111}
112