xref: /webtrees/app/Http/RequestHandlers/ContactPage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1e381f98dSGreg Roach<?php
2e381f98dSGreg Roach
3e381f98dSGreg Roach/**
4e381f98dSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg 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
2281b729d3SGreg Roachuse Fisharebest\Webtrees\Http\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;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29e381f98dSGreg Roachuse Psr\Http\Message\ResponseInterface;
30e381f98dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31e381f98dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32e381f98dSGreg Roach
33e381f98dSGreg Roachuse function in_array;
340bf22806SGreg Roachuse function route;
35e381f98dSGreg Roach
36e381f98dSGreg Roach/**
37e381f98dSGreg Roach * Compose a message from a visitor.
38e381f98dSGreg Roach */
39e381f98dSGreg Roachclass ContactPage implements RequestHandlerInterface
40e381f98dSGreg Roach{
41e381f98dSGreg Roach    use ViewResponseTrait;
42e381f98dSGreg Roach
43c4943cffSGreg Roach    private CaptchaService $captcha_service;
4470ca9c90SGreg Roach
45c4943cffSGreg Roach    private MessageService $message_service;
46e381f98dSGreg Roach
47c4943cffSGreg Roach    private UserService $user_service;
48e381f98dSGreg Roach
49e381f98dSGreg Roach    /**
5070ca9c90SGreg Roach     * @param CaptchaService $captcha_service
51e381f98dSGreg Roach     * @param MessageService $message_service
52e381f98dSGreg Roach     * @param UserService    $user_service
53e381f98dSGreg Roach     */
5470ca9c90SGreg Roach    public function __construct(
5570ca9c90SGreg Roach        CaptchaService $captcha_service,
5670ca9c90SGreg Roach        MessageService $message_service,
5770ca9c90SGreg Roach        UserService $user_service
5870ca9c90SGreg Roach    ) {
5970ca9c90SGreg Roach        $this->captcha_service = $captcha_service;
60e381f98dSGreg Roach        $this->user_service    = $user_service;
61e381f98dSGreg Roach        $this->message_service = $message_service;
62e381f98dSGreg Roach    }
63e381f98dSGreg Roach
64e381f98dSGreg Roach    /**
65e381f98dSGreg Roach     * @param ServerRequestInterface $request
66e381f98dSGreg Roach     *
67e381f98dSGreg Roach     * @return ResponseInterface
68e381f98dSGreg Roach     */
69e381f98dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
70e381f98dSGreg Roach    {
71b55cbc6bSGreg Roach        $tree       = Validator::attributes($request)->tree();
729f0bdfcdSGreg Roach        $body       = Validator::queryParams($request)->string('body', '');
739f0bdfcdSGreg Roach        $from_email = Validator::queryParams($request)->string('from_email', '');
749f0bdfcdSGreg Roach        $from_name  = Validator::queryParams($request)->string('from_name', '');
759f0bdfcdSGreg Roach        $subject    = Validator::queryParams($request)->string('subject', '');
769f0bdfcdSGreg Roach        $to         = Validator::queryParams($request)->string('to', '');
779f0bdfcdSGreg Roach        $url        = Validator::queryParams($request)->isLocalUrl()->string('url', route(HomePage::class));
78e381f98dSGreg Roach
79e381f98dSGreg Roach        $to_user = $this->user_service->findByUserName($to);
80e381f98dSGreg Roach
819f0bdfcdSGreg Roach        if ($to_user === null || !in_array($to_user, $this->message_service->validContacts($tree), false)) {
82d501c45dSGreg Roach            throw new HttpAccessDeniedException('Invalid contact user id');
83e381f98dSGreg Roach        }
84e381f98dSGreg Roach
85e381f98dSGreg Roach        $to_name = $to_user->realName();
86e381f98dSGreg Roach
87e381f98dSGreg Roach        $title = I18N::translate('Send a message');
88e381f98dSGreg Roach
89e381f98dSGreg Roach        return $this->viewResponse('contact-page', [
90e381f98dSGreg Roach            'body'       => $body,
9170ca9c90SGreg Roach            'captcha'    => $this->captcha_service->createCaptcha(),
92e381f98dSGreg Roach            'from_email' => $from_email,
93e381f98dSGreg Roach            'from_name'  => $from_name,
94e381f98dSGreg Roach            'subject'    => $subject,
95e381f98dSGreg Roach            'title'      => $title,
96e381f98dSGreg Roach            'to'         => $to,
97e381f98dSGreg Roach            'to_name'    => $to_name,
98e381f98dSGreg Roach            'tree'       => $tree,
99e381f98dSGreg Roach            'url'        => $url,
100e381f98dSGreg Roach        ]);
101e381f98dSGreg Roach    }
102e381f98dSGreg Roach}
103