Skip to content

Commit b924e81

Browse files
author
Michele Orselli
committed
[wip] adds isLocal and isRemote
1 parent 25fbc53 commit b924e81

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Ip.php

+31
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,37 @@ 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+
if (self::isValidv4($ip)) {
85+
return self::match($ip, '10.*.*.*') ||
86+
self::match($ip, '127.*.*.*') ||
87+
self::match($ip, '192.168.*.*') ||
88+
self::match($ip, '169.254.*.*') ||
89+
self::match($ip, '172.16.*.*') ||
90+
self::match($ip, '224.*.*.*');
91+
}
92+
93+
return false;
94+
}
95+
96+
/**
97+
* Checks if an IP is remot
98+
*
99+
* @param string $ip IP
100+
* @return boolean true if the IP is remote, otherwise false
101+
*/
102+
public static function isRemote($ip)
103+
{
104+
return !self::isLocal($ip);
105+
}
106+
76107
/**
77108
* Checks if an IP is part of an IP range.
78109
*

tests/Unit/IpTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,22 @@ 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+
202+
/**
203+
* @test
204+
*/
205+
public function testRemote()
206+
{
207+
$status = Ip::isRemote('8.8.8.8');
208+
$this->assertTrue($status);
209+
}
210+
193211
}

0 commit comments

Comments
 (0)