xref: /webtrees/app/Http/Middleware/ClientIp.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
14874f72dSGreg Roach<?php
24874f72dSGreg Roach
34874f72dSGreg Roach/**
44874f72dSGreg Roach * webtrees: online genealogy
54874f72dSGreg Roach * Copyright (C) 2019 webtrees development team
64874f72dSGreg Roach * This program is free software: you can redistribute it and/or modify
74874f72dSGreg Roach * it under the terms of the GNU General Public License as published by
84874f72dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
94874f72dSGreg Roach * (at your option) any later version.
104874f72dSGreg Roach * This program is distributed in the hope that it will be useful,
114874f72dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124874f72dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134874f72dSGreg Roach * GNU General Public License for more details.
144874f72dSGreg Roach * You should have received a copy of the GNU General Public License
154874f72dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
164874f72dSGreg Roach */
17*fcfa147eSGreg Roach
184874f72dSGreg Roachdeclare(strict_types=1);
194874f72dSGreg Roach
204874f72dSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
214874f72dSGreg Roach
224874f72dSGreg Roachuse Psr\Http\Message\ResponseInterface;
234874f72dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
244874f72dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
254874f72dSGreg Roach
264874f72dSGreg Roachuse function explode;
274874f72dSGreg Roach
284874f72dSGreg Roach/**
294874f72dSGreg Roach * Middleware to detect the client's IP address.
304874f72dSGreg Roach */
314874f72dSGreg Roachclass ClientIp extends \Middlewares\ClientIp
324874f72dSGreg Roach{
334874f72dSGreg Roach    /**
344874f72dSGreg Roach     * @param ServerRequestInterface  $request
354874f72dSGreg Roach     * @param RequestHandlerInterface $handler
364874f72dSGreg Roach     *
374874f72dSGreg Roach     * @return ResponseInterface
384874f72dSGreg Roach     */
394874f72dSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
404874f72dSGreg Roach    {
414874f72dSGreg Roach        // The configuration comes from config.ini.php, via request attributes.
424874f72dSGreg Roach        $trusted_headers = $this->getCommaSeparatedAttribute($request, 'trusted_headers');
434874f72dSGreg Roach        $trusted_proxies = $this->getCommaSeparatedAttribute($request, 'trusted_proxies');
444874f72dSGreg Roach
454874f72dSGreg Roach        $this->proxy($trusted_proxies, $trusted_headers);
464874f72dSGreg Roach
474874f72dSGreg Roach        return parent::process($request, $handler);
484874f72dSGreg Roach    }
494874f72dSGreg Roach
504874f72dSGreg Roach    /**
514874f72dSGreg Roach     * @param ServerRequestInterface $request
524874f72dSGreg Roach     * @param string                 $attribute
534874f72dSGreg Roach     *
544874f72dSGreg Roach     * @return string[]
554874f72dSGreg Roach     */
564874f72dSGreg Roach    private function getCommaSeparatedAttribute(ServerRequestInterface $request, string $attribute): array
574874f72dSGreg Roach    {
584874f72dSGreg Roach        $value = $request->getAttribute($attribute);
594874f72dSGreg Roach
604874f72dSGreg Roach        if ($value === null) {
614874f72dSGreg Roach            return [];
624874f72dSGreg Roach        }
634874f72dSGreg Roach
644874f72dSGreg Roach        return explode(',', $value);
654874f72dSGreg Roach    }
664874f72dSGreg Roach}
67