1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\FlashMessages; 23use Fisharebest\Webtrees\Http\ViewResponseTrait; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\MessageService; 26use Fisharebest\Webtrees\Services\UserService; 27use Fisharebest\Webtrees\Tree; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Message\ServerRequestInterface; 30use Psr\Http\Server\RequestHandlerInterface; 31use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 32 33use function assert; 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 /** @var MessageService */ 46 private $message_service; 47 48 /** @var UserService */ 49 private $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 = $request->getParsedBody(); 75 $body = $params['body']; 76 $subject = $params['subject']; 77 $to = $params['to']; 78 $url = $params['url']; 79 $to_user = $this->user_service->findByUserName($to); 80 $ip = $request->getAttribute('client-ip'); 81 82 if ($to_user === null || $to_user->getPreference('contactmethod') === 'none') { 83 throw new AccessDeniedHttpException('Invalid contact user id'); 84 } 85 86 if ($body === '' || $subject === '') { 87 return redirect(route(MessagePage::class, [ 88 'body' => $body, 89 'subject' => $subject, 90 'to' => $to, 91 'tree' => $tree->name(), 92 'url' => $url, 93 ])); 94 } 95 96 if ($this->message_service->deliverMessage($user, $to_user, $subject, $body, $url, $ip)) { 97 FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 'success'); 98 99 $url = $url ?: route('tree-page', ['tree' => $tree->name()]); 100 101 return redirect($url); 102 } 103 104 FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger'); 105 106 return redirect(route(MessagePage::class, [ 107 'body' => $body, 108 'subject' => $subject, 109 'to' => $to, 110 'tree' => $tree->name(), 111 'url' => $url, 112 ])); 113 } 114} 115