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