1 | <?php |
||
24 | class IPAddress |
||
25 | { |
||
26 | |||
27 | /** @var false|string presentation form of ip address, or false if invalid */ |
||
28 | protected $ip; |
||
29 | |||
30 | /** |
||
31 | * IPAddress constructor. |
||
32 | * @param string $ip IP address |
||
33 | */ |
||
34 | 5 | public function __construct($ip) |
|
35 | { |
||
36 | 5 | if (!filter_var((string) $ip, FILTER_VALIDATE_IP)) { |
|
37 | 1 | $this->ip = false; |
|
38 | } else { |
||
39 | 5 | $this->ip = $this->normalize($ip); |
|
40 | } |
||
41 | 5 | } |
|
42 | |||
43 | /** |
||
44 | * Get IP address from the request server data |
||
45 | * |
||
46 | * @return IPAddress |
||
47 | */ |
||
48 | 1 | public static function fromRequest() |
|
55 | |||
56 | /** |
||
57 | * convert IP address into a normalized condensed notation |
||
58 | * |
||
59 | * @param string $ip ip address to normalize |
||
60 | * |
||
61 | * @return string|false normalized address or false on failure |
||
62 | */ |
||
63 | 5 | protected function normalize($ip) |
|
64 | { |
||
65 | 5 | $normal = inet_ntop(inet_pton($ip)); |
|
66 | 5 | return $normal; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * return presentation form of address |
||
71 | * |
||
72 | * @return string|false |
||
73 | */ |
||
74 | 3 | public function asReadable() |
|
78 | |||
79 | /** |
||
80 | * get network (binary) form of address |
||
81 | * |
||
82 | * @return string|false |
||
83 | */ |
||
84 | 2 | public function asBinary() |
|
92 | |||
93 | /** |
||
94 | * get the ip version, 4 or 6, of address |
||
95 | * |
||
96 | * @return int|false integer 4 for IPV4, 6 for IPV6, or false if invalid |
||
97 | */ |
||
98 | 2 | public function ipVersion() |
|
109 | |||
110 | /** |
||
111 | * Is this IP in the same subnet as the supplied address? |
||
112 | * |
||
113 | * Accepts net masks for both IPV4 and IPV6 and will select the appropriate one, to |
||
114 | * allow checking policy against request input with minimal method calls. |
||
115 | * |
||
116 | * @param string $matchIp presentation form ip address to compare |
||
117 | * @param int $netMask4 network mask, bits to match <= 32 for IPV4 |
||
118 | * @param int $netMask6 network mask, bits to match <=128 for IPV6 |
||
119 | * |
||
120 | * @return bool true if $this->ip and $matchIp are both in the specified subnet |
||
121 | */ |
||
122 | 1 | public function sameSubnet($matchIp, $netMask4, $netMask6) |
|
142 | |||
143 | /** |
||
144 | * Convert an IP address to a binary character string (i.e. "01111111000000000000000000000001") |
||
145 | * |
||
146 | * @param IPAddress $ip address object |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 1 | protected function asBinaryString(IPAddress $ip) |
|
161 | } |
||
162 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: