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