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\Http\ViewResponseTrait; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Services\MessageService; 25use Fisharebest\Webtrees\Services\UserService; 26use Fisharebest\Webtrees\Tree; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29use Psr\Http\Server\RequestHandlerInterface; 30use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 31 32use function assert; 33use function in_array; 34 35/** 36 * Compose a message from a visitor. 37 */ 38class ContactPage implements RequestHandlerInterface 39{ 40 use ViewResponseTrait; 41 42 /** @var MessageService */ 43 private $message_service; 44 45 /** @var UserService */ 46 private $user_service; 47 48 /** 49 * MessagePage constructor. 50 * 51 * @param MessageService $message_service 52 * @param UserService $user_service 53 */ 54 public function __construct(MessageService $message_service, UserService $user_service) 55 { 56 $this->user_service = $user_service; 57 $this->message_service = $message_service; 58 } 59 60 /** 61 * @param ServerRequestInterface $request 62 * 63 * @return ResponseInterface 64 */ 65 public function handle(ServerRequestInterface $request): ResponseInterface 66 { 67 $tree = $request->getAttribute('tree'); 68 assert($tree instanceof Tree); 69 70 $referer = $request->getHeaderLine('referer'); 71 $params = $request->getQueryParams(); 72 $body = $params['body'] ?? ''; 73 $from_email = $params['from_email'] ?? ''; 74 $from_name = $params['from_name'] ?? ''; 75 $subject = $params['subject'] ?? ''; 76 $to = $params['to'] ?? ''; 77 $url = $params['url'] ?? $referer; 78 79 $to_user = $this->user_service->findByUserName($to); 80 81 if (!in_array($to_user, $this->message_service->validContacts($tree), false)) { 82 throw new AccessDeniedHttpException('Invalid contact user id'); 83 } 84 85 $to_name = $to_user->realName(); 86 87 $title = I18N::translate('Send a message'); 88 89 return $this->viewResponse('contact-page', [ 90 'body' => $body, 91 'from_email' => $from_email, 92 'from_name' => $from_name, 93 'subject' => $subject, 94 'title' => $title, 95 'to' => $to, 96 'to_name' => $to_name, 97 'tree' => $tree, 98 'url' => $url, 99 ]); 100 } 101} 102