xref: /webtrees/app/Http/Middleware/CheckCsrf.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
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
20*6ccdf4f0SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
21094bd4e5SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
22ccb0284cSGreg Roachuse Fisharebest\Webtrees\I18N;
23ccb0284cSGreg Roachuse Fisharebest\Webtrees\Session;
24*6ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
25*6ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26*6ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
27*6ccdf4f0SGreg 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    /**
41*6ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
42*6ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
43ccb0284cSGreg Roach     *
44*6ccdf4f0SGreg Roach     * @return ResponseInterface
45ccb0284cSGreg Roach     */
46*6ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
47c1010edaSGreg Roach    {
48*6ccdf4f0SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
49ba8e6c18SGreg Roach            $route = $request->get('route');
50ba8e6c18SGreg Roach
51310b7a5aSGreg Roach            if (!in_array($route, self::EXCLUDE_ROUTES, true)) {
52*6ccdf4f0SGreg Roach                $client_token  = $request->get('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
58*6ccdf4f0SGreg Roach                    return redirect((string) $request->getUri());
59ccb0284cSGreg Roach                }
60310b7a5aSGreg Roach            }
61310b7a5aSGreg Roach        }
62ccb0284cSGreg Roach
63*6ccdf4f0SGreg Roach        return $handler->handle($request);
64ccb0284cSGreg Roach    }
65ccb0284cSGreg Roach}
66