Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
16 / 16 |
IpValidator | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
14 | |
100.00% |
16 / 16 |
all($ip) | |
100.00% |
1 / 1 |
5 | |
100.00% |
4 / 4 |
|||
ipv4($ip) | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
ipv6($ip) | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
isPrivate($ip) | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
isReserved($ip) | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
isBroadcast($ip) | |
100.00% |
1 / 1 |
3 | |
100.00% |
4 / 4 |
<?php namespace Modulework\Modules\Http\Utilities; | |
/* | |
* (c) Christian Gärtner <christiangaertner.film@googlemail.com> | |
* This file is part of the Modulework Framework | |
* License: View distributed LICENSE file | |
*/ | |
/** | |
* IP Validator | |
* Validates IPv4 and IPv6 address | |
*/ | |
class IpValidator implements IpValidatorInterface | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function all($ip) | |
{ | |
$func = false !== strpos($ip, ':') ? 'ipv6': 'ipv4'; | |
if (self::isPrivate($ip) || self::isReserved($ip) || self::isBroadcast($ip)) { | |
return false; | |
} | |
return self::$func($ip); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function ipv4($ip) | |
{ | |
return (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function ipv6($ip) | |
{ | |
return (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function isPrivate($ip) | |
{ | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function isReserved($ip) | |
{ | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function isBroadcast($ip) | |
{ | |
$segments = explode('.', $ip); | |
if (isset($segments[3])) { | |
return ($segments[3] == '0' || $segments[3] == '255'); | |
} else { | |
return false; | |
} | |
} |