1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xmf;
13:
14: /**
15: * Language
16: *
17: * @category Xmf\IPAddress
18: * @package Xmf
19: * @author trabis <lusopoemas@gmail.com>
20: * @copyright 2018-2021 XOOPS Project (https://xoops.org)
21: * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22: * @link https://xoops.org
23: */
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: 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: * Get IP address from the request server data
45: *
46: * @return IPAddress
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: * convert IP address into a normalized condensed notation
60: *
61: * @param string $ip ip address to normalize
62: *
63: * @return string|false normalized address or false on failure
64: */
65: protected function normalize($ip)
66: {
67: return inet_ntop(inet_pton($ip));
68: }
69:
70: /**
71: * return presentation form of address
72: *
73: * @return string|false
74: */
75: public function asReadable()
76: {
77: return $this->ip;
78: }
79:
80: /**
81: * get network (binary) form of address
82: *
83: * @return string|false
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: * 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: 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: * Is this IP in the same subnet as the supplied address?
113: *
114: * Accepts net masks for both IPV4 and IPV6 and will select the appropriate one, to
115: * allow checking policy against request input with minimal method calls.
116: *
117: * @param string $matchIp presentation form ip address to compare
118: * @param int $netMask4 network mask, bits to match <= 32 for IPV4
119: * @param int $netMask6 network mask, bits to match <=128 for IPV6
120: *
121: * @return bool true if $this->ip and $matchIp are both in the specified subnet
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: * Convert an IP address to a binary character string (i.e. "01111111000000000000000000000001")
144: *
145: * @param IPAddress $ip address object
146: *
147: * @return string
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: