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