Files
ltsecurityinc/app/code/Plumrocket/GeoIPLookup/Model/Services/IpApi.php
T

223 lines
6.3 KiB
PHP
Executable File

<?php
/**
* Plumrocket Inc.
*
* NOTICE OF LICENSE
*
* This source file is subject to the End-user License Agreement
* that is available through the world-wide-web at this URL:
* http://wiki.plumrocket.net/wiki/EULA
* If you are unable to obtain it through the world-wide-web, please
* send an email to support@plumrocket.com so we can send you a copy immediately.
*
* @package Plumrocket_GeoIPLookup
* @copyright Copyright (c) 2018 Plumrocket Inc. (http://www.plumrocket.com)
* @license http://wiki.plumrocket.net/wiki/EULA End-user License Agreement
*/
namespace Plumrocket\GeoIPLookup\Model\Services;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\Serialize\Serializer\Json;
use Plumrocket\GeoIPLookup\Api\GeoIpServiceInterface;
use Plumrocket\GeoIPLookup\Helper\Config;
use Plumrocket\GeoIPLookup\Model\Cache\GeoIpInterface;
/**
* @since 2.0.0
*/
class IpApi implements GeoIpServiceInterface
{
/**
* @var \Magento\Framework\HTTP\Client\Curl
*/
private $curl;
/**
* @var string
*/
private $dataName = 'Ipapi.Com';
/**
* @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
*/
private $remoteAddress;
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $jsonSerializer;
/**
* @var \Plumrocket\GeoIPLookup\Helper\Config
*/
private $configHelper;
/**
* @var \Plumrocket\GeoIPLookup\Model\Cache\GeoIpInterface
*/
private $geoIpCache;
/**
* @param \Magento\Framework\HTTP\Client\Curl $curl
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
* @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer
* @param \Plumrocket\GeoIPLookup\Helper\Config $configHelper
* @param \Plumrocket\GeoIPLookup\Model\Cache\GeoIpInterface $geoIpCache
*/
public function __construct(
Curl $curl,
RemoteAddress $remoteAddress,
Json $jsonSerializer,
Config $configHelper,
GeoIpInterface $geoIpCache
) {
$this->curl = $curl;
$this->remoteAddress = $remoteAddress;
$this->jsonSerializer = $jsonSerializer;
$this->configHelper = $configHelper;
$this->geoIpCache = $geoIpCache;
}
/**
* @inheritDoc
*/
public function getGeoLocation(?string $ip): ?array
{
$result = null;
if (! $ip) {
$ip = $this->remoteAddress->getRemoteAddress();
}
if ($ip) {
$cacheKey = ip2long($ip) . '_gl_ipapi';
$geoipCache = $this->geoIpCache->get();
if (isset($geoipCache[$cacheKey])) {
return $geoipCache[$cacheKey];
}
$curlResult = $this->curlExec($ip);
if ($curlResult) {
$curlResultArr = $this->jsonSerializer->unserialize($curlResult);
if (isset($curlResultArr['error'])) {
return $result;
}
$result = [
'country_code' => $curlResultArr['country_code'] ?? '',
'country_name' => $curlResultArr['country_name'] ?? '',
'region_name' => $curlResultArr['region_name'] ?? '',
'city_name' => $curlResultArr['city'] ?? '',
'latitude' => $curlResultArr['latitude'] ?? '',
'longitude' => $curlResultArr['longitude'] ?? '',
'database_name' => $this->dataName
];
}
$result = (!empty($result)) ? $result : null;
$geoipCache[$cacheKey] = $result;
$this->geoIpCache->set($geoipCache);
}
return $result;
}
/**
* @inheritDoc
*/
public function getCountryCode(?string $ip): ?string
{
$result = null;
if (! $ip) {
$ip = $this->remoteAddress->getRemoteAddress();
}
if ($ip) {
$cacheKey = ip2long($ip) . '_cc_ipapi';
$geoipCache = $this->geoIpCache->get();
if (isset($geoipCache[$cacheKey])) {
return $geoipCache[$cacheKey];
}
$curlResult = $this->curlExec($ip);
if ($curlResult) {
$curlResultArr = $this->jsonSerializer->unserialize($curlResult);
if (isset($curlResultArr['error'])) {
return $result;
} else {
$result = $curlResultArr['country_code'];
}
}
$result = (!empty($result)) ? $result : null;
$geoipCache[$cacheKey] = $result;
$this->geoIpCache->set($geoipCache);
}
return $result;
}
/**
* @inheritDoc
*/
public function getCurrentCountryState(?string $ip): ?string
{
$result = null;
if (! $ip) {
$ip = $this->remoteAddress->getRemoteAddress();
}
if ($ip) {
$cacheKey = ip2long($ip) . '_cc_ipapi_cstate';
$geoipCache = $this->geoIpCache->get();
if (isset($geoipCache[$cacheKey])) {
return $geoipCache[$cacheKey];
}
$curlResult = $this->curlExec($ip);
if ($curlResult) {
$curlResultArr = $this->jsonSerializer->unserialize($curlResult);
if (isset($curlResultArr['error'])) {
return $result;
} else {
$result = $curlResultArr['region_name'];
}
}
$result = (!empty($result)) ? $result : null;
$geoipCache[$cacheKey] = $result;
$this->geoIpCache->set($geoipCache);
}
return $result;
}
/**
* @param $ip
* @return string
*/
private function curlExec($ip): string
{
$url = $this->configHelper->useIpApiHttps() ? "https://" : "http://";
$url .= $this->configHelper->getIpApiUrl($ip);
$options = [
CURLOPT_HEADER => 0,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
];
$this->curl->post($url, $options);
return $this->curl->getBody();
}
}