-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIp.php
More file actions
148 lines (120 loc) · 3.97 KB
/
Ip.php
File metadata and controls
148 lines (120 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Created by PhpStorm.
* User: Bőr Attila
* Date: 11/28/14
* Time: 9:06 PM
*/
class NoValidIp extends Exception{
function __contruct( $e ){
echo $e;
}
}
class Ip {
private $isPrivate = false;
private $ip = null;
private $ipbinary = [];
private $isIp4 = true;
public function __construct( $ip = null ) {
if(is_null($ip)){
$ip = $_SERVER['REMOTE_ADDR'];
}
if( !is_string($ip) ){
throw new NoValidIp("The ip must be string");
}
if($ip == "localhost"){
$this->ip = "127.0.0.1";
$this->setIpBinary();
$this->isPrivate = true;
return;
}
if( self::isValidIp4($ip) || self::isValidIp6($ip) ){
$this->ip = $ip;
$this->setIpBinary();
} else {
throw new NoValidIp("Invalid IP");
}
}
private function setIpBinary(){
if( self::isValidIp4($this->ip) ){
foreach( explode(".",$this->ip) as $ip ){
array_push($this->ipbinary,$this->addLeadingZeros($ip));
}
$this->setPrivate();
}else{
$this->isIp4 = false;
$this->isPrivate = $this->ip == "::1";
foreach( explode(":",$this->ip) as $ip ){
if( $ip == "" ){
array_push($this->ipbinary,"0000000000000000");
array_push($this->ipbinary,"0000000000000000");
}else{
array_push($this->ipbinary,$this->addLeadingZeros($ip));
}
}
if( count($this->ipbinary) != 8 ){
$last = $this->ipbinary[count($this->ipbinary)-1];
for($i = count($this->ipbinary) - 2; $i < 8; $i++){
$this->ipbinary[$i] = "0000000000000000";
}
$this->ipbinary[7] = $last;
}
}
}
public function __toString(){
return implode("",$this->ipbinary);
}
public function getString(){
return implode("",$this->ipbinary);
}
public function isInNetwork( $cidr ){
if(!(preg_match("/[0-9\.]+\/[0-9]{1,2}/",$cidr) || preg_match("/[0-9A-F\:]+\/[0-9]{1,2}/",$cidr))){
throw new NoValidIp("Invalid CIDR.");
}
$address = explode("/",$cidr);
if(
count($address) != 2 ||
!((self::isValidIp4($address[0]) && (int)$address[1] < 33) ||
(self::isValidIp6($address[0]) && (int)$address[1] < 129))
){
throw new NoValidIp("Invalid CIDR.");
}
$network = new Ip($address[0]);
if( $this->isIp4 != $network->isIp4 ){
throw new NoValidIp("Incompatible IP addresses.");
}
return substr($network->getString(),0,(int)$address[1]) == substr($this->getString(),0,(int)$address[1]);
}
private function setPrivate(){
$this->isPrivate =
substr($this->getString(),0,12) == "101011000001" ||
substr($this->getString(),0,8) == "00001010" ||
substr($this->getString(),0,16) == "1100000010101000";
}
public function getClass(){
if( !$this->isIp4 )
return null;
$binary = $this->getString();
if( $binary[0] == "0" )
return "A";
elseif( substr($binary,0,2) == "10" )
return "B";
elseif( substr($binary,0,3) == "110" )
return "C";
}
public function isPrivate(){
return $this->isPrivate;
}
private function addLeadingZeros( $binary ){
return ($this->isIp4) ? sprintf('%1$08b',$binary) : sprintf('%1$016b',hexdec($binary));
}
public static function isValidIp4($ip){
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
}
public static function isValidIp6($ip){
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
}
public function getIp(){
return $this->ip;
}
}