xref: /webtrees/app/Http/RequestHandlers/MessageAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
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
221fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
23e381f98dSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
25e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26e381f98dSGreg Roachuse Fisharebest\Webtrees\I18N;
27e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
28e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
298d9c2b68SGreg Roachuse Fisharebest\Webtrees\Validator;
30e381f98dSGreg Roachuse Psr\Http\Message\ResponseInterface;
31e381f98dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32e381f98dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33e381f98dSGreg Roach
34e381f98dSGreg Roachuse function e;
35e381f98dSGreg Roachuse function redirect;
36e381f98dSGreg Roachuse function route;
37e381f98dSGreg Roach
38e381f98dSGreg Roach/**
39e381f98dSGreg Roach * Send a message from a logged-in user.
40e381f98dSGreg Roach */
41e381f98dSGreg Roachclass MessageAction implements RequestHandlerInterface
42e381f98dSGreg Roach{
43e381f98dSGreg Roach    use ViewResponseTrait;
44e381f98dSGreg Roach
45c4943cffSGreg Roach    private MessageService $message_service;
46e381f98dSGreg Roach
47c4943cffSGreg Roach    private UserService $user_service;
48e381f98dSGreg Roach
49e381f98dSGreg Roach    /**
50e381f98dSGreg Roach     * MessagePage constructor.
51e381f98dSGreg Roach     *
52e381f98dSGreg Roach     * @param MessageService $message_service
53e381f98dSGreg Roach     * @param UserService    $user_service
54e381f98dSGreg Roach     */
55e381f98dSGreg Roach    public function __construct(MessageService $message_service, UserService $user_service)
56e381f98dSGreg Roach    {
57e381f98dSGreg Roach        $this->user_service    = $user_service;
58e381f98dSGreg Roach        $this->message_service = $message_service;
59e381f98dSGreg Roach    }
60e381f98dSGreg Roach
61e381f98dSGreg Roach    /**
62e381f98dSGreg Roach     * @param ServerRequestInterface $request
63e381f98dSGreg Roach     *
64e381f98dSGreg Roach     * @return ResponseInterface
65e381f98dSGreg Roach     */
66e381f98dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
67e381f98dSGreg Roach    {
68b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
69b55cbc6bSGreg Roach        $user     = Validator::attributes($request)->user();
70b55cbc6bSGreg Roach        $ip       = Validator::attributes($request)->string('client-ip');
71b55cbc6bSGreg Roach        $base_url = Validator::attributes($request)->string('base_url');
721e60ebf4SGreg Roach        $body     = Validator::parsedBody($request)->string('body');
731e60ebf4SGreg Roach        $subject  = Validator::parsedBody($request)->string('subject');
741e60ebf4SGreg Roach        $to       = Validator::parsedBody($request)->string('to');
751e60ebf4SGreg Roach        $to_user  = $this->user_service->findByUserName($to);
76f507cef9SGreg Roach        $url      = Validator::parsedBody($request)->isLocalUrl()->string('url', $base_url);
77e381f98dSGreg Roach
788cfb5e7bSGreg Roach        if ($to_user === null || $to_user->getPreference(UserInterface::PREF_CONTACT_METHOD) === MessageService::CONTACT_METHOD_NONE) {
79d501c45dSGreg Roach            throw new HttpAccessDeniedException('Invalid contact user id');
80e381f98dSGreg Roach        }
81e381f98dSGreg Roach
82e381f98dSGreg Roach        if ($body === '' || $subject === '') {
83e381f98dSGreg Roach            return redirect(route(MessagePage::class, [
84e381f98dSGreg Roach                'body'    => $body,
85e381f98dSGreg Roach                'subject' => $subject,
86e381f98dSGreg Roach                'to'      => $to,
87e381f98dSGreg Roach                'tree'    => $tree->name(),
88e381f98dSGreg Roach                'url'     => $url,
89e381f98dSGreg Roach            ]));
90e381f98dSGreg Roach        }
91e381f98dSGreg Roach
92e381f98dSGreg Roach        if ($this->message_service->deliverMessage($user, $to_user, $subject, $body, $url, $ip)) {
93e381f98dSGreg Roach            FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 'success');
94e381f98dSGreg Roach
95e381f98dSGreg Roach            return redirect($url);
96e381f98dSGreg Roach        }
97e381f98dSGreg Roach
98e381f98dSGreg Roach        FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
99e381f98dSGreg Roach
100e381f98dSGreg Roach        return redirect(route(MessagePage::class, [
101e381f98dSGreg Roach            'body'    => $body,
102e381f98dSGreg Roach            'subject' => $subject,
103e381f98dSGreg Roach            'to'      => $to,
104e381f98dSGreg Roach            'tree'    => $tree->name(),
105e381f98dSGreg Roach            'url'     => $url,
106e381f98dSGreg Roach        ]));
107e381f98dSGreg Roach    }
108e381f98dSGreg Roach}
109