Skip to content

Commit

Permalink
Merge pull request #1 from c-wolfe/development
Browse files Browse the repository at this point in the history
Update to 1.0.1
  • Loading branch information
c-wolfe committed Apr 2, 2020
2 parents 984f34a + 7e5de95 commit e46a14f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 25 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cwolfe/iphub",
"description": "VPN/Proxy Detection API with Redis caching",
"version": "1.0.0",
"version": "1.0.1",
"type": "library",
"license": "MIT",
"authors": [
Expand All @@ -12,7 +12,9 @@
],
"require": {
"php": ">=5.3.9",
"predis/predis": "*"
"predis/predis": "*",
"ext-curl": "*",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
136 changes: 113 additions & 23 deletions src/CWolfe/IPHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ class IPHub {
public const RESIDENTIAL = 0;
public const NON_RESIDENTIAL = 1;
public const NON_RESIDENTIAL_AND_RESIDENTIAL = 2;

/**
* @var string
*/
private static $key;

/**
* @var Client
*/
private $predis;

/**
* @var string
*/
Expand All @@ -42,31 +45,36 @@ public function __construct($apiKey, $connectionUrl, $prefix = "iphub") {
/**
* @param string $ip
* @param int $blevel
* @param bool $strict
* @return bool Whether the IP should be allowed or not
* @throws RatelimitException
*/
public function isAllowed($ip, $blevel = IPHub::RESIDENTIAL) {
$level = $this->getIpLevel($ip);
public function isAllowed($ip, $blevel = IPHub::RESIDENTIAL, $strict = false) {
try {
$level = $this->getIpInformation($ip)['block'];
} catch (Exception $exception) {
return !$strict;
}

if ($level == IPHub::NON_RESIDENTIAL) {
return $level == IPHub::NON_RESIDENTIAL || $blevel == IPHub::RESIDENTIAL;
}
return $blevel == $level;
}

/**
* @param $ip
* @return int
* @param string $ip
* @return array
* @throws RatelimitException
* @throws Exception
*/
public function getIpLevel($ip) {
public function getIpInformation($ip) {

if (!$this->predis->isConnected()) {
$this->connect();
$this->predis->connect();
}

if ($this->existsInCache($ip)) {
return $this->predis->get($ip);
return json_decode($this->predis->get("$this->prefix:$ip"), true);
} else {

$ch = curl_init();
Expand All @@ -83,9 +91,9 @@ public function getIpLevel($ip) {
if ($responseHttpCode == 200) {
$responseObject = json_decode($response);

if (isset($responseObject->block)) {
$this->pushToCache($ip, $responseObject->block);
return $responseObject->block;
if (isset($responseObject->ip)) {
$this->pushToCache($ip, $responseObject);
return $responseObject;
}

} else if ($responseHttpCode == 429) {
Expand All @@ -95,14 +103,7 @@ public function getIpLevel($ip) {

}

throw new Exception();
}

/**
* Connect to Redis
*/
public function connect() {
$this->predis->connect();
return [];
}

/**
Expand All @@ -115,15 +116,104 @@ public function existsInCache($ip) {

/**
* @param string $ip
* @param int $level
* @param array $information
* @param int $ttl
*/
public function pushToCache($ip, $level, $ttl = 3600) {
$this->predis->set("$this->prefix:$ip", $level, 'EX', $ttl);
public function pushToCache($ip, $information, $ttl = 3600) {
$this->predis->set("$this->prefix:$ip", json_encode($information), 'EX', $ttl);
}

/**
* @param string $ip
* @return int
* @throws RatelimitException
* @deprecated
*/
public function getIpLevel($ip) {
return $this->getLevel($ip);
}


/**
* @param string $ip
* @return int
* @throws RatelimitException
*/
public function getLevel($ip) {
$information = $this->getIpInformation($ip);
if (is_array($information) && isset($information['block'])) {
return $information['block'];
}
return 0;
}

/**
* @param string $ip
* @return string
* @throws RatelimitException
*/
public function getCountryCode($ip) {
$information = $this->getIpInformation($ip);
if (is_array($information) && isset($information['countryCode'])) {
return $information['countryCode'];
}
return 0;
}

/**
* @param $ip
* @param string $ip
* @return string
* @throws RatelimitException
*/
public function getCountryName($ip) {
$information = $this->getIpInformation($ip);
if (is_array($information) && isset($information['countryName'])) {
return $information['countryName'];
}
return 0;
}

/**
* @param string $ip
* @return string
* @throws RatelimitException
*/
public function getASN($ip) {
$information = $this->getIpInformation($ip);
if (is_array($information) && isset($information['asn'])) {
return $information['asn'];
}
return 0;
}

/**
* @param string $ip
* @return string
* @throws RatelimitException
*/
public function getISP($ip) {
$information = $this->getIpInformation($ip);
if (is_array($information) && isset($information['isp'])) {
return $information['isp'];
}
return 0;
}

/**
* @param string $ip
* @return string
* @throws RatelimitException
*/
public function getHostname($ip) {
$information = $this->getIpInformation($ip);
if (is_array($information) && isset($information['hostname'])) {
return $information['hostname'];
}
return 0;
}

/**
* @param string $ip
*/
public function removeFromCache($ip) {
$this->predis->del(["$this->prefix:$ip"]);
Expand Down

0 comments on commit e46a14f

Please sign in to comment.