1 | <?php |
||
18 | class IP implements ExcluderInterface |
||
19 | { |
||
20 | /** |
||
21 | * List of IPs to be excluded. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $ips = []; |
||
26 | |||
27 | /** |
||
28 | * Allowed proxies. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $trustedProxies = []; |
||
33 | |||
34 | /** |
||
35 | * @param string|array|null $ips |
||
36 | * @param string|array|null $trustedProxies |
||
37 | * |
||
38 | * @throws \InvalidArgumentException |
||
39 | */ |
||
40 | public function __construct($ips = null, $trustedProxies = null) |
||
41 | { |
||
42 | if (!is_array($ips)) { |
||
43 | $ips = [$ips]; |
||
44 | } |
||
45 | |||
46 | foreach ($ips as $ipAddress) { |
||
47 | $this->addIP($ipAddress); |
||
48 | } |
||
49 | |||
50 | if (!is_array($trustedProxies)) { |
||
51 | $trustedProxies = [$trustedProxies]; |
||
52 | } |
||
53 | |||
54 | foreach ($trustedProxies as $ipAddress) { |
||
55 | $this->addTrustedProxy($ipAddress); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Add IP. |
||
61 | * |
||
62 | * @param string $ipAddress |
||
63 | * |
||
64 | * @throws \InvalidArgumentException |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function addIP($ipAddress) |
||
69 | { |
||
70 | if (trim($ipAddress) !== '') { |
||
71 | if (!$this->isValidIp($ipAddress)) { |
||
72 | throw new \InvalidArgumentException(sprintf('"%s" is not a valid IP address', $ipAddress)); |
||
73 | } |
||
74 | |||
75 | $this->ips[] = $ipAddress; |
||
76 | } |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Add Trusted proxy. |
||
83 | * |
||
84 | * @param string $ipAddress |
||
85 | * |
||
86 | * @throws \InvalidArgumentException |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function addTrustedProxy($ipAddress) |
||
91 | { |
||
92 | if (trim($ipAddress) !== '') { |
||
93 | if (!$this->isValidIp($ipAddress)) { |
||
94 | throw new \InvalidArgumentException(sprintf('"%s" is not a valid IP address', $ipAddress)); |
||
95 | } |
||
96 | |||
97 | $this->trustedProxies[] = $ipAddress; |
||
98 | } |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function isExcluded(ServerRequestInterface $request) |
||
118 | |||
119 | /** |
||
120 | * Find client's IP. |
||
121 | * |
||
122 | * @param ServerRequestInterface $request |
||
123 | * |
||
124 | * @return string|null |
||
125 | */ |
||
126 | protected function determineCurrentIp(ServerRequestInterface $request) |
||
158 | |||
159 | /** |
||
160 | * Return current IP retrieved from server parameters. |
||
161 | * |
||
162 | * @param ServerRequestInterface $request |
||
163 | * |
||
164 | * @return string|null |
||
165 | */ |
||
166 | private function getIpFromServerParams(ServerRequestInterface $request) |
||
177 | |||
178 | /** |
||
179 | * Check IP validity. |
||
180 | * |
||
181 | * @param string $ipAddress |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | private function isValidIp($ipAddress) |
||
189 | } |
||
190 |