Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/code/MindArc/GeoIP/Block/Catalog/Product/View/CountryCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace MindArc\GeoIP\Block\Catalog\Product\View;

class CountryCode
extends \Magento\Framework\View\Element\Template
{
protected $_geoIpHelper;
protected $_geoIP;

public function __construct(
\MindArc\GeoIP\Helper\GeoIP $geoIp,
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
)
{
$this->_geoIP = $geoIp;
parent::__construct($context, $data);
}

public function getCountryISO()
{
$ip = $this->_geoIP->getIpAddress();
$data = $this->_geoIP->getCountryData($ip);
if ($data !== null && !empty($data['iso'])) {
$code = $data['iso'];
} else {
$code = "It coudn\\'t get the country Iso code ";
}

return $code;
}

public function getBlockID()
{
$iso = $this->getCountryISO();

return strtolower('geoip_' . $iso);
}
}
111 changes: 111 additions & 0 deletions app/code/MindArc/GeoIP/Helper/GeoIP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace MindArc\GeoIP\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Filesystem\DirectoryList;

class GeoIP
extends \Magento\Framework\App\Helper\AbstractHelper
{

const DATABASE_PATH = 'interactive_geoip/setting/file';
const SIMULATION_MODE = 'interactive_geoip/setting/simulationmode';
const MY_IP = 'interactive_geoip/setting/myip';

protected $_directoryList;

public function __construct(
DirectoryList $directoryList,
Context $context,
ObjectManagerInterface $objectManager,
StoreManagerInterface $storeManager
)
{
$this->_directoryList = $directoryList;
parent::__construct($context, $objectManager, $storeManager);
}

public function getDataBaseFile()
{
return $this->scopeConfig->getValue(self::DATABASE_PATH);
}

public function simulationMode()
{
return $this->scopeConfig->getValue(self::SIMULATION_MODE);
}

public function myIP()
{
return $this->scopeConfig->getValue(self::MY_IP);
}

public function checkDB()
{
$path = $this->_directoryList->getPath('base') . '/' . $this->getDataBaseFile();
if (!file_exists($path)) {
return false;
}
$folder = scandir($path, true);
$pathFile = $path . '/' . $folder[0];
if (!file_exists($pathFile)) {
return false;
}

return $pathFile;
}

public function getCountryData($ip)
{
try {
$libPath = $this->checkDB();
if ($libPath && class_exists('GeoIp2\Database\Reader')) {
$geoIp = new \GeoIp2\Database\Reader($libPath);
$record = $geoIp->country($ip);
$geoIpData = [
'name' => $record->country->name,
'iso' => $record->country->isoCode,
];
} else {
$geoIpData = [];
}
} catch (\Exception $e) {
$geoIpData = [
'name' => 'NONE',
'iso' => 'NONE',
];
}

return $geoIpData;
}

public function getIpAddress()
{
if ($this->simulationMode()) {
return $this->myIP();
}

$server = $this->_getRequest()->getServer();
if (!empty($server['HTTP_CLIENT_IP'])) {
$ip = $server['HTTP_CLIENT_IP'];
} else if (!empty($server['HTTP_X_FORWARDED_FOR'])) {
$ip = $server['HTTP_X_FORWARDED_FOR'];
} else if (!empty($server['REMOTE_ADDR'])) {
$ip = $server['REMOTE_ADDR'];
} else {
$ip = $this->getIpFromMagento();
}
$ips = explode(',', $ip);
$ip = $ips[count($ips) - 1];

return trim($ip);
}

protected function getIpFromMagento()
{
return $this->_remoteAddress->getRemoteAddress();
}
}
28 changes: 28 additions & 0 deletions app/code/MindArc/GeoIP/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="interactive_geoip" translate="label" type="text" sortOrder="16" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Geo IP</label>
<tab>general</tab>
<resource>Summa_Paypal::lco_paypal</resource>
<group id="setting" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Geo IP Settings</label>
<field id="file" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Databasbe path</label>
</field>
<field id="simulationmode" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable IP Simulation</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="myip" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>My new IP address</label>
<comment>Insert a new Ip for simulation mode</comment>
<depends>
<field id="interactive_geoip/setting/simulationmode">1</field>
</depends>
</field>
</group>
</section>
</system>
</config>
11 changes: 11 additions & 0 deletions app/code/MindArc/GeoIP/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<interactive_geoip>
<setting>
<file>var/GeoIP/DataBase</file>
</setting>
</interactive_geoip>
</default>
</config>
5 changes: 5 additions & 0 deletions app/code/MindArc/GeoIP/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MindArc_GeoIP" setup_version="1.0.1">
</module>
</config>
6 changes: 6 additions & 0 deletions app/code/MindArc/GeoIP/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MindArc_GeoIP',
__DIR__
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.media">
<block class="MindArc\GeoIP\Block\Catalog\Product\View\CountryCode" name="product.view.countrycode"
template="MindArc_GeoIP::catalog/product/view/countrycode.phtml"/>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
$block->getChildHtml('countrycode');
$countryCode = $block->getCountryISO();
echo $countryCode;
?>
10 changes: 10 additions & 0 deletions app/code/MindArc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
For working of GeoIP module the file 'database.tar.gz' must be unzip under var/ folder of the Magento project
and install GeoIP2 PHP API run in your project root:

php composer.phar require geoip2/geoip2:~2.0


For testing the functionality, you can simulate an IP in Store->Configuration->general->GeoIP.
1. Enable simulation mode
2. Set an IP. For instance: US IP, 72.229.28.185
3. Clear config cache
Binary file added app/code/MindArc/database.tar.gz
Binary file not shown.