1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: |
|
11: |
|
12: | namespace Xmf;
|
13: |
|
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: |
|
24: | class IPAddress
|
25: | {
|
26: |
|
27: |
|
28: | protected $ip;
|
29: |
|
30: | |
31: | |
32: | |
33: |
|
34: | public function __construct($ip)
|
35: | {
|
36: | if (!filter_var((string) $ip, FILTER_VALIDATE_IP)) {
|
37: | $this->ip = false;
|
38: | } else {
|
39: | $this->ip = $this->normalize($ip);
|
40: | }
|
41: | }
|
42: |
|
43: | |
44: | |
45: | |
46: | |
47: |
|
48: | public static function fromRequest()
|
49: | {
|
50: | $ip = (array_key_exists('REMOTE_ADDR', $_SERVER)) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
|
51: | $class = get_called_class();
|
52: | $proxyCheck = new ProxyCheck();
|
53: | $proxyIP = $proxyCheck->get();
|
54: | $ip = (false === $proxyIP) ? $ip : $proxyIP;
|
55: | return new $class($ip);
|
56: | }
|
57: |
|
58: | |
59: | |
60: | |
61: | |
62: | |
63: | |
64: |
|
65: | protected function normalize($ip)
|
66: | {
|
67: | return inet_ntop(inet_pton($ip));
|
68: | }
|
69: |
|
70: | |
71: | |
72: | |
73: | |
74: |
|
75: | public function asReadable()
|
76: | {
|
77: | return $this->ip;
|
78: | }
|
79: |
|
80: | |
81: | |
82: | |
83: | |
84: |
|
85: | public function asBinary()
|
86: | {
|
87: | if (false === $this->ip) {
|
88: | return false;
|
89: | }
|
90: | return inet_pton($this->ip);
|
91: | }
|
92: |
|
93: | |
94: | |
95: | |
96: | |
97: |
|
98: | public function ipVersion()
|
99: | {
|
100: | $return = false;
|
101: | if (false === $this->ip) {
|
102: | $return = false;
|
103: | } elseif (false !== filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
104: | $return = 4;
|
105: | } elseif (false !== filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
106: | $return = 6;
|
107: | }
|
108: | return $return;
|
109: | }
|
110: |
|
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: |
|
123: | public function sameSubnet($matchIp, $netMask4, $netMask6)
|
124: | {
|
125: | $match = new IPAddress($matchIp);
|
126: | if (false !== $this->ipVersion() && ($this->ipVersion() === $match->ipVersion())) {
|
127: | switch ($this->ipVersion()) {
|
128: | case 4:
|
129: | $mask = (-1) << (32 - $netMask4);
|
130: | return ((ip2long($this->ip) & $mask) === (ip2long($match->asReadable()) & $mask));
|
131: | case 6:
|
132: | $ipBits = $this->asBinaryString($this);
|
133: | $matchBits = $this->asBinaryString($match);
|
134: | return (0 === strncmp($ipBits, $matchBits, $netMask6));
|
135: | default:
|
136: | break;
|
137: | }
|
138: | }
|
139: | return false;
|
140: | }
|
141: |
|
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: |
|
149: | protected function asBinaryString(IPAddress $ip)
|
150: | {
|
151: | $length = (4 === $ip->ipVersion()) ? 4 : 16;
|
152: | $binaryIp = $ip->asBinary();
|
153: | $bits = '';
|
154: | for ($i = 0; $i < $length; ++$i) {
|
155: | $byte = decbin(ord($binaryIp[$i]));
|
156: | $bits .= substr("00000000" . $byte, -8);
|
157: | }
|
158: | return $bits;
|
159: | }
|
160: | }
|
161: | |