diff --git a/Api/IpToCountryRepositoryInterface.php b/Api/IpToCountryRepositoryInterface.php
index 8b977bc..c9ca046 100644
--- a/Api/IpToCountryRepositoryInterface.php
+++ b/Api/IpToCountryRepositoryInterface.php
@@ -20,4 +20,10 @@ public function getCountryCode($ip);
* @return mixed
*/
public function getVisitorCountryCode();
+
+ /**
+ * Retrieve current IP
+ * @return string
+ */
+ public function getRemoteAddress();
}
\ No newline at end of file
diff --git a/Api/IpToRegionRepositoryInterface.php b/Api/IpToRegionRepositoryInterface.php
index 8875979..a973391 100644
--- a/Api/IpToRegionRepositoryInterface.php
+++ b/Api/IpToRegionRepositoryInterface.php
@@ -11,8 +11,19 @@
interface IpToRegionRepositoryInterface
{
/**
- * @param string $ip
+ * @param $ip
+ * @return mixed
+ */
+ public function getRegionCode($ip);
+
+ /**
+ * @return mixed
+ */
+ public function getVisitorRegionCode();
+
+ /**
+ * Retrieve current IP
* @return string
*/
- public function getRegionCode(string $ip = ''): string;
+ public function getRemoteAddress();
}
\ No newline at end of file
diff --git a/Block/Adminhtml/System/Config/Form/IpInfo.php b/Block/Adminhtml/System/Config/Form/IpInfo.php
index 62e0101..941c1e1 100644
--- a/Block/Adminhtml/System/Config/Form/IpInfo.php
+++ b/Block/Adminhtml/System/Config/Form/IpInfo.php
@@ -54,9 +54,9 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
if ($country == "ZZ") {
$country = 'Undefined';
}
-
+
$ip = $this->ipToCountryRepository->getRemoteAddress();
- $regionId = $this->ipToRegionRepository->getRegionCode();
+ $regionId = $this->ipToRegionRepository->getVisitorRegionCode();
$html = '
Your IP Address: ' . $ip . '
diff --git a/Model/IpToCountryRepository.php b/Model/IpToCountryRepository.php
index 9eef37f..70470c1 100644
--- a/Model/IpToCountryRepository.php
+++ b/Model/IpToCountryRepository.php
@@ -4,8 +4,9 @@
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/
-namespace Magefan\GeoIp\Model;
+declare(strict_types=1);
+namespace Magefan\GeoIp\Model;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Filesystem\DirectoryList;
@@ -15,10 +16,6 @@
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magefan\GeoIp\Api\IpToCountryRepositoryInterface;
-/**
- * Class IpToCountryRepository
- * @package Magefan\GeoIp\Model
- */
class IpToCountryRepository implements IpToCountryRepositoryInterface
{
/**
@@ -67,7 +64,6 @@ class IpToCountryRepository implements IpToCountryRepositoryInterface
private $moduleDir;
/**
- * IpToCountryRepository constructor.
* @param RemoteAddress $remoteAddress
* @param DirectoryList $directoryList
* @param ModuleDir $moduleDir
@@ -89,9 +85,9 @@ public function __construct(
}
/**
- * [getCountryCode description]
- * @param string $ip
- * @return string | false
+ * Get Country Code by IP
+ * @param string $ip
+ * @return mixed
*/
public function getCountryCode($ip)
{
@@ -139,6 +135,7 @@ public function getCountryCode($ip)
$datFile = $filename;
} else {
$datFile = $this->moduleDir->getDir('Magefan_GeoIp') . '/data/GeoLite2-Country.mmdb';
+ //throw new \Exception('No .mmdb file');
}
$reader = new \GeoIp2\Database\Reader($datFile);
$record = $reader->country($ip);
diff --git a/Model/IpToRegionRepository.php b/Model/IpToRegionRepository.php
index 96b89fb..5641d65 100644
--- a/Model/IpToRegionRepository.php
+++ b/Model/IpToRegionRepository.php
@@ -8,13 +8,13 @@
namespace Magefan\GeoIp\Model;
-use Magefan\GeoIp\Api\IpToRegionRepositoryInterface;
-use Magento\Framework\App\Config\ScopeConfigInterface;
-use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
-use Magento\Framework\App\RequestInterface;
+use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Module\Dir as ModuleDir;
-use Magento\Store\Model\ScopeInterface;
+use Magento\Framework\App\RequestInterface;
+use Magento\Framework\App\Config\ScopeConfigInterface;
+use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
+use Magefan\GeoIp\Api\IpToRegionRepositoryInterface;
class IpToRegionRepository implements IpToRegionRepositoryInterface
{
@@ -46,46 +46,42 @@ class IpToRegionRepository implements IpToRegionRepositoryInterface
/**
* @var DirectoryList
*/
- protected $directoryList;
+ private $directoryList;
/**
* @var ModuleDir
*/
- protected $moduleDir;
+ private $moduleDir;
/**
- * @param ScopeConfigInterface $config
* @param RemoteAddress $remoteAddress
- * @param RequestInterface $request
* @param DirectoryList $directoryList
* @param ModuleDir $moduleDir
+ * @param ScopeConfigInterface $config
+ * @param RequestInterface $httpRequest
*/
public function __construct(
- ScopeConfigInterface $config,
RemoteAddress $remoteAddress,
- RequestInterface $request,
DirectoryList $directoryList,
- ModuleDir $moduleDir
- )
- {
- $this->config = $config;
+ ModuleDir $moduleDir,
+ ScopeConfigInterface $config,
+ RequestInterface $httpRequest
+ ) {
$this->remoteAddress = $remoteAddress;
- $this->request = $request;
$this->directoryList = $directoryList;
$this->moduleDir = $moduleDir;
+ $this->config = $config;
+ $this->request = $httpRequest;
}
/**
+ * Get Region Code by IP
* @param string $ip
- * @return string
+ * @return mixed
*/
- public function getRegionCode(string $ip = ''): string
+ public function getRegionCode($ip)
{
- if (!$ip) {
- $ip = $this->getIp();
- }
-
- if (!isset($this->ipToRegion[$ip])) {
+ if (!isset($this->ipToRegion[$ip])) {
$this->ipToRegion[$ip] = '';
try {
@@ -94,11 +90,12 @@ public function getRegionCode(string $ip = ''): string
$datFile = $filename;
} else {
$datFile = $this->moduleDir->getDir('Magefan_GeoIp') . '/data/GeoLite2-City.mmdb';
+ //throw new \Exception('No .mmdb file');
}
$reader = new \GeoIp2\Database\Reader($datFile);
$record = $reader->city($ip);
- if (isset($record->subdivisions[0])) {
+ if ($record && $record->subdivisions && isset($record->subdivisions[0])) {
$this->ipToRegion[$ip] = $record->subdivisions[0]->isoCode;
}
} catch (\Exception $e) {}
@@ -108,15 +105,20 @@ public function getRegionCode(string $ip = ''): string
}
/**
+ * Retrieve current visitor country code by IP
+ * @return string | false
+ */
+ public function getVisitorRegionCode()
+ {
+ return $this->getRegionCode($this->getRemoteAddress());
+ }
+
+ /**
+ * Retrieve current IP
* @return string
*/
- private function getIp(): string
+ public function getRemoteAddress()
{
- $cloudflareEnable = $this->config->getValue(self::XML_PATH_CLOUDFLARE_ENABLED, ScopeInterface::SCOPE_STORE);
- if ($cloudflareEnable) {
- return (string)$this->request->getServer('HTTP_CF_CONNECTING_IP');
- } else {
- return (string)$this->remoteAddress->getRemoteAddress();
- }
+ return $this->remoteAddress->getRemoteAddress();
}
-}
\ No newline at end of file
+}
diff --git a/composer.json b/composer.json
index 644b040..956c49c 100644
--- a/composer.json
+++ b/composer.json
@@ -2,7 +2,7 @@
"name": "magefan/module-geoip",
"description": "Allows to get country code by IP",
"require": {
- "magefan/module-community" : ">=2.1.16",
+ "magefan/module-community" : ">=2.1.21",
"geoip2/geoip2": "^2.9.0"
},
"suggest": {
@@ -10,7 +10,7 @@
"magefan/module-auto-language-switcher": "Install GeoIP Auto Language/Store Switcher Extension for Magento 2 (https://magefan.com/magento-2-auto-language-switcher). Use coupon code COMPOSER-FAN to get a 10% discount on magefan.com."
},
"type": "magento2-module",
- "version": "2.2.3",
+ "version": "2.3.0",
"autoload": {
"psr-4": {
"Magefan\\GeoIp\\": ""
diff --git a/data/GeoLite2-Country.mmdb b/data/GeoLite2-Country.mmdb
index d32677c..f77c914 100644
Binary files a/data/GeoLite2-Country.mmdb and b/data/GeoLite2-Country.mmdb differ