Skip to content

Commit 02fc102

Browse files
authored
Merge pull request #4 from micheleorselli/feature_islocal
Adds isLocal and isRemote methods
2 parents 25fbc53 + d18c3a0 commit 02fc102

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/Ip.php

+45
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,51 @@ public static function isValidv6($ip)
7373
return (bool)filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
7474
}
7575

76+
/**
77+
* Checks if an IP is local
78+
*
79+
* @param string $ip IP
80+
* @return boolean true if the IP is local, otherwise false
81+
*/
82+
public static function isLocal($ip)
83+
{
84+
$localIpv4Ranges = array(
85+
'10.*.*.*',
86+
'127.*.*.*',
87+
'192.168.*.*',
88+
'169.254.*.*',
89+
'172.16.0.0-172.31.255.255',
90+
'224.*.*.*',
91+
);
92+
93+
$localIpv6Ranges = array(
94+
'fe80::/10',
95+
'::1/128',
96+
'fc00::/7'
97+
);
98+
99+
if (self::isValidv4($ip)) {
100+
return self::match($ip, $localIpv4Ranges);
101+
}
102+
103+
if (self::isValidv6($ip)) {
104+
return self::match($ip, $localIpv6Ranges);
105+
}
106+
107+
return false;
108+
}
109+
110+
/**
111+
* Checks if an IP is remot
112+
*
113+
* @param string $ip IP
114+
* @return boolean true if the IP is remote, otherwise false
115+
*/
116+
public static function isRemote($ip)
117+
{
118+
return !self::isLocal($ip);
119+
}
120+
76121
/**
77122
* Checks if an IP is part of an IP range.
78123
*

tests/Unit/IpTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,25 @@ public function test9()
190190
$this->assertEquals('fe80::202:b3ff:fe1e:8329', $dec);
191191
}
192192

193+
/**
194+
* @test
195+
*/
196+
public function testLocal()
197+
{
198+
$status = Ip::isLocal('192.168.5.5');
199+
$this->assertTrue($status);
200+
201+
$status = Ip::isLocal('fe80::202:b3ff:fe1e:8329');
202+
$this->assertTrue($status);
203+
}
204+
205+
/**
206+
* @test
207+
*/
208+
public function testRemote()
209+
{
210+
$status = Ip::isRemote('8.8.8.8');
211+
$this->assertTrue($status);
212+
}
213+
193214
}

0 commit comments

Comments
 (0)