xref: /webtrees/app/Http/RequestHandlers/ContactPage.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1e381f98dSGreg Roach<?php
2e381f98dSGreg Roach
3e381f98dSGreg Roach/**
4e381f98dSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6e381f98dSGreg Roach * This program is free software: you can redistribute it and/or modify
7e381f98dSGreg Roach * it under the terms of the GNU General Public License as published by
8e381f98dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e381f98dSGreg Roach * (at your option) any later version.
10e381f98dSGreg Roach * This program is distributed in the hope that it will be useful,
11e381f98dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e381f98dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e381f98dSGreg Roach * GNU General Public License for more details.
14e381f98dSGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e381f98dSGreg Roach */
17e381f98dSGreg Roach
18e381f98dSGreg Roachdeclare(strict_types=1);
19e381f98dSGreg Roach
20e381f98dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21e381f98dSGreg Roach
22d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
23e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
24e381f98dSGreg Roachuse Fisharebest\Webtrees\I18N;
2570ca9c90SGreg Roachuse Fisharebest\Webtrees\Services\CaptchaService;
26e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
27e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
28e381f98dSGreg Roachuse Fisharebest\Webtrees\Tree;
29e381f98dSGreg Roachuse Psr\Http\Message\ResponseInterface;
30e381f98dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31e381f98dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32e381f98dSGreg Roach
33e381f98dSGreg Roachuse function assert;
34e381f98dSGreg Roachuse function in_array;
350bf22806SGreg Roachuse function route;
36e381f98dSGreg Roach
37e381f98dSGreg Roach/**
38e381f98dSGreg Roach * Compose a message from a visitor.
39e381f98dSGreg Roach */
40e381f98dSGreg Roachclass ContactPage implements RequestHandlerInterface
41e381f98dSGreg Roach{
42e381f98dSGreg Roach    use ViewResponseTrait;
43e381f98dSGreg Roach
4470ca9c90SGreg Roach    /** @var CaptchaService */
4570ca9c90SGreg Roach    private $captcha_service;
4670ca9c90SGreg Roach
47e381f98dSGreg Roach    /** @var MessageService */
48e381f98dSGreg Roach    private $message_service;
49e381f98dSGreg Roach
50e381f98dSGreg Roach    /** @var UserService */
51e381f98dSGreg Roach    private $user_service;
52e381f98dSGreg Roach
53e381f98dSGreg Roach    /**
54e381f98dSGreg Roach     * MessagePage constructor.
55e381f98dSGreg Roach     *
5670ca9c90SGreg Roach     * @param CaptchaService $captcha_service
57e381f98dSGreg Roach     * @param MessageService $message_service
58e381f98dSGreg Roach     * @param UserService    $user_service
59e381f98dSGreg Roach     */
6070ca9c90SGreg Roach    public function __construct(
6170ca9c90SGreg Roach        CaptchaService $captcha_service,
6270ca9c90SGreg Roach        MessageService $message_service,
6370ca9c90SGreg Roach        UserService $user_service
6470ca9c90SGreg Roach    ) {
6570ca9c90SGreg Roach        $this->captcha_service = $captcha_service;
66e381f98dSGreg Roach        $this->user_service    = $user_service;
67e381f98dSGreg Roach        $this->message_service = $message_service;
68e381f98dSGreg Roach    }
69e381f98dSGreg Roach
70e381f98dSGreg Roach    /**
71e381f98dSGreg Roach     * @param ServerRequestInterface $request
72e381f98dSGreg Roach     *
73e381f98dSGreg Roach     * @return ResponseInterface
74e381f98dSGreg Roach     */
75e381f98dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
76e381f98dSGreg Roach    {
77e381f98dSGreg Roach        $tree = $request->getAttribute('tree');
78e381f98dSGreg Roach        assert($tree instanceof Tree);
79e381f98dSGreg Roach
80e381f98dSGreg Roach        $params     = $request->getQueryParams();
81e381f98dSGreg Roach        $body       = $params['body'] ?? '';
82e381f98dSGreg Roach        $from_email = $params['from_email'] ?? '';
83e381f98dSGreg Roach        $from_name  = $params['from_name'] ?? '';
84e381f98dSGreg Roach        $subject    = $params['subject'] ?? '';
85e381f98dSGreg Roach        $to         = $params['to'] ?? '';
860bf22806SGreg Roach        $url        = $params['url'] ?? route(HomePage::class);
87e381f98dSGreg Roach
88e381f98dSGreg Roach        $to_user = $this->user_service->findByUserName($to);
89e381f98dSGreg Roach
90e381f98dSGreg Roach        if (!in_array($to_user, $this->message_service->validContacts($tree), false)) {
91d501c45dSGreg Roach            throw new HttpAccessDeniedException('Invalid contact user id');
92e381f98dSGreg Roach        }
93e381f98dSGreg Roach
94e381f98dSGreg Roach        $to_name = $to_user->realName();
95e381f98dSGreg Roach
96e381f98dSGreg Roach        $title = I18N::translate('Send a message');
97e381f98dSGreg Roach
98e381f98dSGreg Roach        return $this->viewResponse('contact-page', [
99e381f98dSGreg Roach            'body'       => $body,
10070ca9c90SGreg Roach            'captcha'    => $this->captcha_service->createCaptcha(),
101e381f98dSGreg Roach            'from_email' => $from_email,
102e381f98dSGreg Roach            'from_name'  => $from_name,
103e381f98dSGreg Roach            'subject'    => $subject,
104e381f98dSGreg Roach            'title'      => $title,
105e381f98dSGreg Roach            'to'         => $to,
106e381f98dSGreg Roach            'to_name'    => $to_name,
107e381f98dSGreg Roach            'tree'       => $tree,
108e381f98dSGreg Roach            'url'        => $url,
109e381f98dSGreg Roach        ]);
110e381f98dSGreg Roach    }
111e381f98dSGreg Roach}
112