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\Exceptions\HttpAccessDeniedException; 23use Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 24use Fisharebest\Webtrees\FlashMessages; 25use Fisharebest\Webtrees\GuestUser; 26use Fisharebest\Webtrees\Http\ViewResponseTrait; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Services\CaptchaService; 29use Fisharebest\Webtrees\Services\EmailService; 30use Fisharebest\Webtrees\Services\MessageService; 31use Fisharebest\Webtrees\Services\UserService; 32use Fisharebest\Webtrees\Tree; 33use Psr\Http\Message\ResponseInterface; 34use Psr\Http\Message\ServerRequestInterface; 35use Psr\Http\Server\RequestHandlerInterface; 36 37use function assert; 38use function e; 39use function in_array; 40use function preg_match; 41use function preg_quote; 42use function redirect; 43use function route; 44 45/** 46 * Send a message from a visitor. 47 */ 48class ContactAction implements RequestHandlerInterface 49{ 50 use ViewResponseTrait; 51 52 private CaptchaService $captcha_service; 53 54 private EmailService $email_service; 55 56 private MessageService $message_service; 57 58 private UserService $user_service; 59 60 /** 61 * MessagePage constructor. 62 * 63 * @param CaptchaService $captcha_service 64 * @param EmailService $email_service 65 * @param MessageService $message_service 66 * @param UserService $user_service 67 */ 68 public function __construct( 69 CaptchaService $captcha_service, 70 EmailService $email_service, 71 MessageService $message_service, 72 UserService $user_service 73 ) { 74 $this->captcha_service = $captcha_service; 75 $this->email_service = $email_service; 76 $this->user_service = $user_service; 77 $this->message_service = $message_service; 78 } 79 80 /** 81 * @param ServerRequestInterface $request 82 * 83 * @return ResponseInterface 84 */ 85 public function handle(ServerRequestInterface $request): ResponseInterface 86 { 87 $tree = $request->getAttribute('tree'); 88 assert($tree instanceof Tree); 89 90 $params = (array) $request->getParsedBody(); 91 $body = $params['body']; 92 $from_email = $params['from_email']; 93 $from_name = $params['from_name']; 94 $subject = $params['subject']; 95 $to = $params['to']; 96 $url = $params['url']; 97 $ip = $request->getAttribute('client-ip'); 98 $to_user = $this->user_service->findByUserName($to); 99 100 if ($to_user === null) { 101 throw new HttpNotFoundException(); 102 } 103 104 if (!in_array($to_user, $this->message_service->validContacts($tree), false)) { 105 throw new HttpAccessDeniedException('Invalid contact user id'); 106 } 107 108 $errors = $body === '' || $subject === '' || $from_email === '' || $from_name === ''; 109 110 if ($this->captcha_service->isRobot($request)) { 111 FlashMessages::addMessage(I18N::translate('Please try again.'), 'danger'); 112 $errors = true; 113 } 114 115 if (!$this->email_service->isValidEmail($from_email)) { 116 FlashMessages::addMessage(I18N::translate('Please enter a valid email address.'), 'danger'); 117 $errors = true; 118 } 119 120 $base_url = $request->getAttribute('base_url'); 121 122 if (preg_match('/(?!' . preg_quote($base_url, '/') . ')(((?:ftp|http|https):\/\/)[a-zA-Z0-9.-]+)/', $subject . $body, $match)) { 123 FlashMessages::addMessage(I18N::translate('You are not allowed to send messages that contain external links.') . ' ' . /* I18N: e.g. ‘You should delete the “http://” from “http://www.example.com” and try again.’ */ 124 I18N::translate('You should delete the “%1$s” from “%2$s” and try again.', $match[2], $match[1]), 'danger'); 125 $errors = true; 126 } 127 128 if ($errors) { 129 return redirect(route(ContactPage::class, [ 130 'body' => $body, 131 'from_email' => $from_email, 132 'from_name' => $from_name, 133 'subject' => $subject, 134 'to' => $to, 135 'tree' => $tree->name(), 136 'url' => $url, 137 ])); 138 } 139 140 $sender = new GuestUser($from_email, $from_name); 141 142 if ($this->message_service->deliverMessage($sender, $to_user, $subject, $body, $url, $ip)) { 143 FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 'success'); 144 145 $url = $url ?: route(TreePage::class, ['tree' => $tree->name()]); 146 147 return redirect($url); 148 } 149 150 FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger'); 151 152 $redirect_url = route(ContactPage::class, [ 153 'body' => $body, 154 'from_email' => $from_email, 155 'from_name' => $from_name, 156 'subject' => $subject, 157 'to' => $to, 158 'tree' => $tree->name(), 159 'url' => $url, 160 ]); 161 162 return redirect($redirect_url); 163 } 164} 165