1
|
|
|
<?php |
2
|
|
|
namespace Respect\Validation\Rules; |
3
|
|
|
|
4
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
5
|
|
|
|
6
|
|
|
class Ip extends AbstractRule |
7
|
|
|
{ |
8
|
|
|
public $ipOptions; |
9
|
|
|
|
10
|
|
|
public $networkRange; |
11
|
|
|
|
12
|
40 |
|
public function __construct($ipOptions = null) |
13
|
|
|
{ |
14
|
40 |
|
if (is_int($ipOptions)) { |
15
|
1 |
|
$this->ipOptions = $ipOptions; |
16
|
|
|
|
17
|
1 |
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
39 |
|
$this->networkRange = $this->parseRange($ipOptions); |
21
|
32 |
|
} |
22
|
|
|
|
23
|
39 |
|
protected function parseRange($input) |
24
|
|
|
{ |
25
|
39 |
|
if ($input === null || $input == '*' || $input == '*.*.*.*' |
26
|
39 |
|
|| $input == '0.0.0.0-255.255.255.255') { |
27
|
9 |
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
30 |
|
$range = array('min' => null, 'max' => null, 'mask' => null); |
31
|
|
|
|
32
|
30 |
|
if (strpos($input, '-') !== false) { |
33
|
8 |
|
list($range['min'], $range['max']) = explode('-', $input); |
34
|
30 |
|
} elseif (strpos($input, '*') !== false) { |
35
|
11 |
|
$this->parseRangeUsingWildcards($input, $range); |
36
|
22 |
|
} elseif (strpos($input, '/') !== false) { |
37
|
9 |
|
$this->parseRangeUsingCidr($input, $range); |
38
|
6 |
|
} else { |
39
|
2 |
|
throw new ComponentException('Invalid network range'); |
40
|
|
|
} |
41
|
|
|
|
42
|
25 |
|
if (!$this->verifyAddress($range['min'])) { |
43
|
|
|
throw new ComponentException('Invalid network range'); |
44
|
|
|
} |
45
|
|
|
|
46
|
25 |
|
if (isset($range['max']) && !$this->verifyAddress($range['max'])) { |
47
|
2 |
|
throw new ComponentException('Invalid network range'); |
48
|
|
|
} |
49
|
|
|
|
50
|
23 |
|
return $range; |
51
|
|
|
} |
52
|
|
|
|
53
|
20 |
|
protected function fillAddress(&$input, $char = '*') |
54
|
|
|
{ |
55
|
20 |
|
while (substr_count($input, '.') < 3) { |
56
|
5 |
|
$input .= '.'.$char; |
57
|
5 |
|
} |
58
|
20 |
|
} |
59
|
|
|
|
60
|
11 |
|
protected function parseRangeUsingWildcards($input, &$range) |
61
|
|
|
{ |
62
|
11 |
|
$this->fillAddress($input); |
63
|
|
|
|
64
|
11 |
|
$range['min'] = strtr($input, '*', '0'); |
65
|
11 |
|
$range['max'] = str_replace('*', '255', $input); |
66
|
11 |
|
} |
67
|
|
|
|
68
|
9 |
|
protected function parseRangeUsingCidr($input, &$range) |
69
|
|
|
{ |
70
|
9 |
|
$input = explode('/', $input); |
71
|
9 |
|
$this->fillAddress($input[0], '0'); |
72
|
|
|
|
73
|
9 |
|
$range['min'] = $input[0]; |
74
|
9 |
|
$isAddressMask = strpos($input[1], '.') !== false; |
75
|
|
|
|
76
|
9 |
|
if ($isAddressMask && $this->verifyAddress($input[1])) { |
77
|
2 |
|
$range['mask'] = sprintf('%032b', ip2long($input[1])); |
78
|
|
|
|
79
|
2 |
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
7 |
|
if ($isAddressMask || $input[1] < 8 || $input[1] > 30) { |
83
|
3 |
|
throw new ComponentException('Invalid network mask'); |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
$range['mask'] = sprintf('%032b', ip2long(long2ip(~(pow(2, (32 - $input[1])) - 1)))); |
87
|
4 |
|
} |
88
|
|
|
|
89
|
32 |
|
public function validate($input) |
90
|
|
|
{ |
91
|
32 |
|
return $this->verifyAddress($input) && $this->verifyNetwork($input); |
92
|
|
|
} |
93
|
|
|
|
94
|
36 |
|
protected function verifyAddress($address) |
95
|
|
|
{ |
96
|
36 |
|
return (boolean) filter_var( |
97
|
36 |
|
$address, |
98
|
36 |
|
FILTER_VALIDATE_IP, |
99
|
|
|
array( |
100
|
36 |
|
'flags' => $this->ipOptions, |
101
|
|
|
) |
102
|
36 |
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
27 |
|
protected function verifyNetwork($input) |
106
|
|
|
{ |
107
|
27 |
|
if ($this->networkRange === null) { |
108
|
4 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
23 |
|
if (isset($this->networkRange['mask'])) { |
112
|
6 |
|
return $this->belongsToSubnet($input); |
113
|
|
|
} |
114
|
|
|
|
115
|
17 |
|
$input = sprintf('%u', ip2long($input)); |
116
|
|
|
|
117
|
17 |
|
return bccomp($input, sprintf('%u', ip2long($this->networkRange['min']))) >= 0 |
118
|
17 |
|
&& bccomp($input, sprintf('%u', ip2long($this->networkRange['max']))) <= 0; |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
protected function belongsToSubnet($input) |
122
|
|
|
{ |
123
|
6 |
|
$range = $this->networkRange; |
124
|
6 |
|
$min = sprintf('%032b', ip2long($range['min'])); |
125
|
6 |
|
$input = sprintf('%032b', ip2long($input)); |
126
|
|
|
|
127
|
6 |
|
return ($input & $range['mask']) === ($min & $range['mask']); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|