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 Fisharebest\Webtrees\User; 29use Psr\Http\Message\ResponseInterface; 30use Psr\Http\Message\ServerRequestInterface; 31use Psr\Http\Server\RequestHandlerInterface; 32use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 33 34use function assert; 35use function e; 36use function redirect; 37use function route; 38 39/** 40 * Send a message from a logged-in user. 41 */ 42class MessageAction implements RequestHandlerInterface 43{ 44 use ViewResponseTrait; 45 46 /** @var MessageService */ 47 private $message_service; 48 49 /** @var UserService */ 50 private $user_service; 51 52 /** 53 * MessagePage constructor. 54 * 55 * @param MessageService $message_service 56 * @param UserService $user_service 57 */ 58 public function __construct(MessageService $message_service, UserService $user_service) 59 { 60 $this->user_service = $user_service; 61 $this->message_service = $message_service; 62 } 63 64 /** 65 * @param ServerRequestInterface $request 66 * 67 * @return ResponseInterface 68 */ 69 public function handle(ServerRequestInterface $request): ResponseInterface 70 { 71 $tree = $request->getAttribute('tree'); 72 assert($tree instanceof Tree); 73 74 $user = $request->getAttribute('user'); 75 $params = $request->getParsedBody(); 76 $body = $params['body']; 77 $subject = $params['subject']; 78 $to = $params['to']; 79 $url = $params['url']; 80 $to_user = $this->user_service->findByUserName($to); 81 $ip = $request->getAttribute('client-ip'); 82 83 if ($to_user === null || $to_user->getPreference(User::PREF_CONTACT_METHOD) === 'none') { 84 throw new AccessDeniedHttpException('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 $url = $url ?: route('tree-page', ['tree' => $tree->name()]); 101 102 return redirect($url); 103 } 104 105 FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger'); 106 107 return redirect(route(MessagePage::class, [ 108 'body' => $body, 109 'subject' => $subject, 110 'to' => $to, 111 'tree' => $tree->name(), 112 'url' => $url, 113 ])); 114 } 115} 116