1ccb0284cSGreg Roach<?php 2ccb0284cSGreg Roach/** 3ccb0284cSGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5ccb0284cSGreg Roach * This program is free software: you can redistribute it and/or modify 6ccb0284cSGreg Roach * it under the terms of the GNU General Public License as published by 7ccb0284cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 8ccb0284cSGreg Roach * (at your option) any later version. 9ccb0284cSGreg Roach * This program is distributed in the hope that it will be useful, 10ccb0284cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11ccb0284cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12ccb0284cSGreg Roach * GNU General Public License for more details. 13ccb0284cSGreg Roach * You should have received a copy of the GNU General Public License 14ccb0284cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15ccb0284cSGreg Roach */ 16ccb0284cSGreg Roachdeclare(strict_types=1); 17ccb0284cSGreg Roach 18ccb0284cSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 19ccb0284cSGreg Roach 206ccdf4f0SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 21094bd4e5SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 22ccb0284cSGreg Roachuse Fisharebest\Webtrees\I18N; 23ccb0284cSGreg Roachuse Fisharebest\Webtrees\Session; 246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface; 276ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 28ba8e6c18SGreg Roachuse function in_array; 29ccb0284cSGreg Roach 30ccb0284cSGreg Roach/** 31ccb0284cSGreg Roach * Middleware to wrap a request in a transaction. 32ccb0284cSGreg Roach */ 33c1010edaSGreg Roachclass CheckCsrf implements MiddlewareInterface 34c1010edaSGreg Roach{ 35ba8e6c18SGreg Roach private const EXCLUDE_ROUTES = [ 36ba8e6c18SGreg Roach 'language', 37ba8e6c18SGreg Roach 'theme', 38ba8e6c18SGreg Roach ]; 39ba8e6c18SGreg Roach 40ccb0284cSGreg Roach /** 416ccdf4f0SGreg Roach * @param ServerRequestInterface $request 426ccdf4f0SGreg Roach * @param RequestHandlerInterface $handler 43ccb0284cSGreg Roach * 446ccdf4f0SGreg Roach * @return ResponseInterface 45ccb0284cSGreg Roach */ 466ccdf4f0SGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 47c1010edaSGreg Roach { 486ccdf4f0SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 49*4f2a4a62SGreg Roach $route = $request->getQueryParams()['route'] ?? ''; 50ba8e6c18SGreg Roach 51310b7a5aSGreg Roach if (!in_array($route, self::EXCLUDE_ROUTES, true)) { 52*4f2a4a62SGreg Roach $client_token = $request->getParsedBody()['csrf'] ?? $request->getHeaderLine('X_CSRF_TOKEN'); 53ccb0284cSGreg Roach $session_token = Session::get('CSRF_TOKEN'); 54ccb0284cSGreg Roach 55310b7a5aSGreg Roach if ($client_token !== $session_token) { 56094bd4e5SGreg Roach FlashMessages::addMessage(I18N::translate('This form has expired. Try again.')); 57094bd4e5SGreg Roach 586ccdf4f0SGreg Roach return redirect((string) $request->getUri()); 59ccb0284cSGreg Roach } 60310b7a5aSGreg Roach } 61310b7a5aSGreg Roach } 62ccb0284cSGreg Roach 636ccdf4f0SGreg Roach return $handler->handle($request); 64ccb0284cSGreg Roach } 65ccb0284cSGreg Roach} 66