1 | <?php |
||
34 | class Ipv4Range implements IpRange |
||
35 | { |
||
36 | |||
37 | /** the lower value of the range (as a long integer) */ |
||
38 | private $lowerInt; |
||
39 | /** the upper value of the range (as a long integer) */ |
||
40 | private $upperInt; |
||
41 | |||
42 | /** |
||
43 | * Constructor for the class. |
||
44 | * @param string $range A valid IPv4 range as a string. Supported range styles: |
||
45 | * - CIDR notation (127.0.0.1/24) |
||
46 | * - hyphen notation (127.0.0.1-127.0.0.255) |
||
47 | * - wildcard notation (127.0.0.*) |
||
48 | * - a single specific IP address (127.0.0.1) |
||
49 | */ |
||
50 | 9 | public function __construct($range) |
|
54 | |||
55 | /** |
||
56 | * Returns the lower value of the IPv4 range as a long integer. |
||
57 | * @return int The lower value of the IPv4 range. |
||
58 | */ |
||
59 | 7 | public function getLowerInt() |
|
63 | |||
64 | /** |
||
65 | * Returns the upper value of the IPv4 range as a long integer. |
||
66 | * @return int The upper value of the IPv4 range. |
||
67 | */ |
||
68 | 6 | public function getUpperInt() |
|
72 | |||
73 | /** |
||
74 | * Returns whether or not a given IP address falls within this range. |
||
75 | * @param string $ipAddress The given IP address. |
||
76 | * @return boolean Returns true if the IP address falls within the range |
||
77 | * and false otherwise. |
||
78 | */ |
||
79 | 7 | public function containsIp($ipAddress) |
|
80 | { |
||
81 | 7 | $ipLong = ip2long($ipAddress); |
|
82 | 7 | return ($this->getLowerInt() <= $ipLong) && ($this->getUpperInt() >= $ipLong); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Computes the lower and upper bounds of the IPv4 range by parsing the |
||
87 | * range string. |
||
88 | * @param string $range The IPv4 range as a string. |
||
89 | */ |
||
90 | 9 | private function computeLowerAndUpperBounds($range) |
|
91 | { |
||
92 | 9 | if (strpos($range, '/') !== false) { |
|
93 | // support CIDR notation |
||
94 | 3 | list($this->lowerInt, $this->upperInt) = $this->parseCidrRange($range); |
|
95 | 6 | } elseif (strpos($range, '-') !== false) { |
|
96 | // support for IP ranges like '10.0.0.0-10.0.0.255' |
||
97 | 1 | list($this->lowerInt, $this->upperInt) = $this->parseHyphenRange($range); |
|
98 | 5 | } elseif (($pos = strpos($range, '*')) !== false) { |
|
99 | // support for IP ranges like '10.0.*' |
||
100 | 1 | list($this->lowerInt, $this->upperInt) = $this->parseWildcardRange($range, $pos); |
|
101 | } else { |
||
102 | // assume we have a single address |
||
103 | 4 | $this->lowerInt = ip2long($range); |
|
104 | 4 | $this->upperInt = $this->lowerInt; |
|
105 | } |
||
106 | 9 | } |
|
107 | |||
108 | /** |
||
109 | * Parses a CIDR notation range. |
||
110 | * @param string $range The CIDR range. |
||
111 | * @return array Returns an array with the first element being the lower |
||
112 | * bound of the range and second element being the upper bound. |
||
113 | */ |
||
114 | 3 | private function parseCidrRange($range) |
|
115 | { |
||
116 | 3 | list($address, $mask) = explode('/', $range); |
|
117 | 3 | $longAddress = ip2long($address); |
|
118 | return array( |
||
119 | 3 | $longAddress & (((1 << $mask) - 1) << (32 - $mask)), |
|
120 | 3 | $longAddress | ((1 << (32 - $mask)) - 1) |
|
121 | ); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Parses a hyphen notation range. |
||
126 | * @param string $range The hyphen notation range. |
||
127 | * @return array Returns an array with the first element being the lower |
||
128 | * bound of the range and second element being the upper bound. |
||
129 | */ |
||
130 | 1 | private function parseHyphenRange($range) |
|
134 | |||
135 | /** |
||
136 | * Parses a wildcard notation range. |
||
137 | * @param string $range The wildcard notation range. |
||
138 | * @param int $pos The integer position of the wildcard within the range string. |
||
139 | * @return array Returns an array with the first element being the lower |
||
140 | * bound of the range and second element being the upper bound. |
||
141 | */ |
||
142 | 1 | private function parseWildcardRange($range, $pos) |
|
152 | } |
||
153 |