xref: /webtrees/app/Http/RequestHandlers/ContactPage.php (revision d9efec4aaa9aefd6a527c75c15e437b9a1a6604c)
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\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        $params     = $request->getQueryParams();
75        $body       = $params['body'] ?? '';
76        $from_email = $params['from_email'] ?? '';
77        $from_name  = $params['from_name'] ?? '';
78        $subject    = $params['subject'] ?? '';
79        $to         = $params['to'] ?? '';
80        $url        = $params['url'] ?? route(HomePage::class);
81
82        $to_user = $this->user_service->findByUserName($to);
83
84        if (!in_array($to_user, $this->message_service->validContacts($tree), false)) {
85            throw new HttpAccessDeniedException('Invalid contact user id');
86        }
87
88        $to_name = $to_user->realName();
89
90        $title = I18N::translate('Send a message');
91
92        return $this->viewResponse('contact-page', [
93            'body'       => $body,
94            'captcha'    => $this->captcha_service->createCaptcha(),
95            'from_email' => $from_email,
96            'from_name'  => $from_name,
97            'subject'    => $subject,
98            'title'      => $title,
99            'to'         => $to,
100            'to_name'    => $to_name,
101            'tree'       => $tree,
102            'url'        => $url,
103        ]);
104    }
105}
106