|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message\Entity; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Import functions |
|
16
|
|
|
*/ |
|
17
|
|
|
use function filter_var; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Import constants |
|
21
|
|
|
*/ |
|
22
|
|
|
use const FILTER_FLAG_IPV4; |
|
23
|
|
|
use const FILTER_FLAG_IPV6; |
|
24
|
|
|
use const FILTER_FLAG_NO_PRIV_RANGE; |
|
25
|
|
|
use const FILTER_FLAG_NO_RES_RANGE; |
|
26
|
|
|
use const FILTER_VALIDATE_IP; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* IP address |
|
30
|
|
|
*/ |
|
31
|
|
|
final class IpAddress |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The IP address value |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private string $value; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The list of proxies in front of this IP address |
|
43
|
|
|
* |
|
44
|
|
|
* @var list<string> |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
private array $proxies; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructor of the class |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $value |
|
52
|
|
|
* @param list<string> $proxies |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(string $value, array $proxies = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->value = $value; |
|
57
|
|
|
$this->proxies = $proxies; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Gets the IP address value |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getValue(): string |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Gets the list of proxies in front of this IP address |
|
72
|
|
|
* |
|
73
|
|
|
* @return list<string> |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getProxies(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->proxies; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Checks if the IP address is valid |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function isValid(): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return false !== filter_var($this->value, FILTER_VALIDATE_IP); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Checks if the IP address is IPv4 |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function isVersion4(): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Checks if the IP address is IPv6 |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function isVersion6(): bool |
|
106
|
|
|
{ |
|
107
|
|
|
return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Checks if the IP address is in the private range |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function isInPrivateRange(): bool |
|
116
|
|
|
{ |
|
117
|
|
|
if (!$this->isValid()) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Checks if the IP address is in the reserved range |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function isInReservedRange(): bool |
|
130
|
|
|
{ |
|
131
|
|
|
if (!$this->isValid()) { |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths