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 * @param MessageService $message_service 51e381f98dSGreg Roach * @param UserService $user_service 52e381f98dSGreg Roach */ 53e381f98dSGreg Roach public function __construct(MessageService $message_service, UserService $user_service) 54e381f98dSGreg Roach { 55e381f98dSGreg Roach $this->user_service = $user_service; 56e381f98dSGreg Roach $this->message_service = $message_service; 57e381f98dSGreg Roach } 58e381f98dSGreg Roach 59e381f98dSGreg Roach /** 60e381f98dSGreg Roach * @param ServerRequestInterface $request 61e381f98dSGreg Roach * 62e381f98dSGreg Roach * @return ResponseInterface 63e381f98dSGreg Roach */ 64e381f98dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 65e381f98dSGreg Roach { 66b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 67b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 68b55cbc6bSGreg Roach $ip = Validator::attributes($request)->string('client-ip'); 69b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 701e60ebf4SGreg Roach $body = Validator::parsedBody($request)->string('body'); 711e60ebf4SGreg Roach $subject = Validator::parsedBody($request)->string('subject'); 721e60ebf4SGreg Roach $to = Validator::parsedBody($request)->string('to'); 731e60ebf4SGreg Roach $to_user = $this->user_service->findByUserName($to); 74f507cef9SGreg Roach $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $base_url); 75e381f98dSGreg Roach 768cfb5e7bSGreg Roach if ($to_user === null || $to_user->getPreference(UserInterface::PREF_CONTACT_METHOD) === MessageService::CONTACT_METHOD_NONE) { 77d501c45dSGreg Roach throw new HttpAccessDeniedException('Invalid contact user id'); 78e381f98dSGreg Roach } 79e381f98dSGreg Roach 80e381f98dSGreg Roach if ($body === '' || $subject === '') { 81e381f98dSGreg Roach return redirect(route(MessagePage::class, [ 82e381f98dSGreg Roach 'body' => $body, 83e381f98dSGreg Roach 'subject' => $subject, 84e381f98dSGreg Roach 'to' => $to, 85e381f98dSGreg Roach 'tree' => $tree->name(), 86e381f98dSGreg Roach 'url' => $url, 87e381f98dSGreg Roach ])); 88e381f98dSGreg Roach } 89e381f98dSGreg Roach 90e381f98dSGreg Roach if ($this->message_service->deliverMessage($user, $to_user, $subject, $body, $url, $ip)) { 91e381f98dSGreg Roach FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 'success'); 92e381f98dSGreg Roach 93e381f98dSGreg Roach return redirect($url); 94e381f98dSGreg Roach } 95e381f98dSGreg Roach 96e381f98dSGreg Roach FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger'); 97e381f98dSGreg Roach 98e381f98dSGreg Roach return redirect(route(MessagePage::class, [ 99e381f98dSGreg Roach 'body' => $body, 100e381f98dSGreg Roach 'subject' => $subject, 101e381f98dSGreg Roach 'to' => $to, 102e381f98dSGreg Roach 'tree' => $tree->name(), 103e381f98dSGreg Roach 'url' => $url, 104e381f98dSGreg Roach ])); 105e381f98dSGreg Roach } 106e381f98dSGreg Roach} 107