Skip to content

Commit

Permalink
Bag fix if Sypex not answer
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpsan committed Mar 4, 2022
1 parent 6c59b4a commit 0e79fe7
Showing 1 changed file with 49 additions and 28 deletions.
77 changes: 49 additions & 28 deletions GeoIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
namespace scorpsan\geoip;

use yii\base\Component;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use IP2Location\Database;
use Yii;
use yii\base\Exception;

class GeoIp extends Component
{
Expand Down Expand Up @@ -99,25 +101,34 @@ public function getInfo($ip = null)
);
if ($is_bot) return false;

if ($ip)
$userip = $ip;
elseif (in_array(Yii::$app->request->userIP, $this->localIp))
$userip = '';
else
$userip = Yii::$app->request->userIP;
$userip = '';
$this->httpClient = new \GuzzleHttp\Client([
'base_uri' => self::URL_API,
'timeout' => 36000,
'verify' => false,
]);
$response = $this->httpClient->get((($this->keySypex) ? $this->keySypex.'/' : '') . 'json/' . $userip);
try {
if ($ip)
$userip = $ip;
elseif (in_array(Yii::$app->request->userIP, $this->localIp))
$userip = '';
else
$userip = Yii::$app->request->userIP;

// all IP detected by Sypex
$userip = '';

$result = json_decode($response->getBody(), true);
$this->httpClient = new Client([
'base_uri' => self::URL_API,
'timeout' => 36000,
'verify' => false,
]);
$response = $this->httpClient->get((($this->keySypex) ? $this->keySypex.'/' : '') . 'json/' . $userip);

if (!empty($result['ip'])) return $result;
$result = json_decode($response->getBody(), true);

if (empty($result['ip'])) throw new Exception('IP address not found or Sypex error');

} catch (Exception $exception) {
Yii::error($exception->getMessage());
return false;
}

return false;
return $result;
}

/**
Expand All @@ -133,12 +144,14 @@ public function getInfoDb($ip = null)
if ($ip)
$userip = $ip;
else
$userip = $this->ip;
$userip = $this->getIp();

$response = new Database(Yii::getAlias('@vendor') . '/ip2location/ip2location-php/databases/IP2LOCATION-LITE-DB1.BIN');
$result = $response->lookup($userip);

if ($result['countryCode'] == 'Invalid IP address.')
return false;

return $result;
}

Expand All @@ -154,20 +167,28 @@ public function getIp()
);
if ($is_bot) return false;

if (in_array(Yii::$app->request->userIP, $this->localIp)) {
$this->httpClient = new \GuzzleHttp\Client([
'base_uri' => self::URL_API,
'timeout' => 36000,
'verify' => false,
]);
$response = $this->httpClient->get((($this->keySypex) ? $this->keySypex . '/' : '') . 'json/');
if (!in_array(Yii::$app->request->userIP, $this->localIp)) {
return Yii::$app->request->userIP;
}

try {
$this->httpClient = new Client([
'base_uri' => self::URL_API,
'timeout' => 36000,
'verify' => false,
]);
$response = $this->httpClient->get((($this->keySypex) ? $this->keySypex . '/' : '') . 'json/');

$result = json_decode($response->getBody(), true);
$result = json_decode($response->getBody(), true);

if (!empty($result['ip'])) return $result['ip'];
}
if (empty($result['ip'])) throw new Exception('IP address not found or Sypex error');

} catch (Exception $exception) {
Yii::error($exception->getMessage());
return false;
}

return Yii::$app->request->userIP;
return $result['ip'];
}

}

0 comments on commit 0e79fe7

Please sign in to comment.