Passed
Push — master ( f567c3...4874f7 )
by Greg
06:24
created

ClientIp   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommaSeparatedAttribute() 0 9 2
A process() 0 9 1
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
declare(strict_types=1);
18
19
namespace Fisharebest\Webtrees\Http\Middleware;
20
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
25
use function explode;
26
27
/**
28
 * Middleware to detect the client's IP address.
29
 */
30
class ClientIp extends \Middlewares\ClientIp
31
{
32
    /**
33
     * @param ServerRequestInterface  $request
34
     * @param RequestHandlerInterface $handler
35
     *
36
     * @return ResponseInterface
37
     */
38
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
39
    {
40
        // The configuration comes from config.ini.php, via request attributes.
41
        $trusted_headers = $this->getCommaSeparatedAttribute($request,'trusted_headers');
42
        $trusted_proxies = $this->getCommaSeparatedAttribute($request,'trusted_proxies');
43
44
        $this->proxy($trusted_proxies, $trusted_headers);
45
46
        return parent::process($request, $handler);
47
    }
48
49
    /**
50
     * @param ServerRequestInterface $request
51
     * @param string                 $attribute
52
     *
53
     * @return string[]
54
     */
55
    private function getCommaSeparatedAttribute(ServerRequestInterface $request,  string $attribute): array
56
    {
57
        $value = $request->getAttribute($attribute);
58
59
        if ($value === null) {
60
            return [];
61
        }
62
63
        return explode(',', $value);
64
    }
65
}
66