1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
The MIT License (MIT) |
5
|
|
|
|
6
|
|
|
Copyright (c) 2015 Vectorface, Inc. |
7
|
|
|
|
8
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
in the Software without restriction, including without limitation the rights |
11
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
furnished to do so, subject to the following conditions: |
14
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be included in |
16
|
|
|
all copies or substantial portions of the Software. |
17
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Vectorface\Whip\IpRange; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* A class representing an IPv4 address range. |
31
|
|
|
* @copyright Vectorface, Inc 2015 |
32
|
|
|
* @author Daniel Bruce <[email protected]> |
33
|
|
|
*/ |
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) |
51
|
|
|
{ |
52
|
9 |
|
$this->computeLowerAndUpperBounds($range); |
53
|
9 |
|
} |
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() |
60
|
|
|
{ |
61
|
7 |
|
return $this->lowerInt; |
62
|
|
|
} |
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() |
69
|
|
|
{ |
70
|
6 |
|
return $this->upperInt; |
71
|
|
|
} |
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) |
131
|
|
|
{ |
132
|
1 |
|
return array_map('ip2long', explode('-', $range)); |
133
|
|
|
} |
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) |
143
|
|
|
{ |
144
|
1 |
|
$prefix = substr($range, 0, $pos - 1); |
145
|
1 |
|
$parts = explode('.', $prefix); |
146
|
1 |
|
$partsCount = 4 - count($parts); |
147
|
|
|
return array( |
148
|
1 |
|
ip2long(implode('.', array_merge($parts, array_fill(0, $partsCount, 0)))), |
149
|
1 |
|
ip2long(implode('.', array_merge($parts, array_fill(0, $partsCount, 255)))) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|