Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Updates (#1)
Browse files Browse the repository at this point in the history
* Update IpLocator.php

* Create LocalIpAddressException.php
  • Loading branch information
Dan Cryer authored Mar 12, 2018
1 parent ec8d9bb commit d2d8594
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/AddressLocator/IpLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/LocalIpAddressException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Block8\Geolocation\Exception;

class LocalIpAddressException extends \Exception
{

}

0 comments on commit d2d8594

Please sign in to comment.