From d2d8594659b98ec1cb12ae8801152b67811095ba Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Mon, 12 Mar 2018 11:58:48 +0000 Subject: [PATCH] Updates (#1) * Update IpLocator.php * Create LocalIpAddressException.php --- src/AddressLocator/IpLocator.php | 14 ++++++++++++-- src/Exception/LocalIpAddressException.php | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/Exception/LocalIpAddressException.php diff --git a/src/AddressLocator/IpLocator.php b/src/AddressLocator/IpLocator.php index 2b26d6e..a53dbf4 100644 --- a/src/AddressLocator/IpLocator.php +++ b/src/AddressLocator/IpLocator.php @@ -5,6 +5,7 @@ use Block8\Geolocation\Coordinates; use Block8\Geolocation\Exception\BadRequestException; use Block8\Geolocation\Exception\HttpException; +use Block8\Geolocation\Exception\LocalIpAddressException; use GuzzleHttp\Client; use Block8\Geolocation\Address; @@ -21,15 +22,24 @@ public function __construct(string $ip) throw new BadRequestException($ip . ' is not a valid IP address.'); } + if (self::isLocalIp($ip)) { + throw new LocalIpAddressException(); + } + $this->ip = $this->cleanIp($ip); } - public static function isValidIp(string $ip) + public static function isValidIp(string $ip) : bool { return !(filter_var($ip, FILTER_VALIDATE_IP) === false); } - protected function cleanIp(string $ip) + public static function isLocalIp(string $ip) : bool + { + return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); + } + + protected function cleanIp(string $ip) : string { if (preg_match('/\:\:ffff\:([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/', $ip)) { $ip = substr($ip, 7); diff --git a/src/Exception/LocalIpAddressException.php b/src/Exception/LocalIpAddressException.php new file mode 100644 index 0000000..d5ae95f --- /dev/null +++ b/src/Exception/LocalIpAddressException.php @@ -0,0 +1,8 @@ +