Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release branch 2019 feb 1 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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);
}

protected function getCountryISO()
{
$ip = $this->_geoIP->getIpAddress();
$data = $this->_geoIP->getCountryData($ip);
if (isset($data) && !empty($data['iso']) && $data['iso'] === 'US') {
$code = $data['iso'];
} else {
$code = 'global_block';
}

return $code;
}

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

return strtolower('geoip_' . $iso);
}
}
128 changes: 128 additions & 0 deletions app/code/MindArc/GeoIP/Helper/GeoIP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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';
const ISO_BLACK_LIST = 'interactive_geoip/setting/blacklist';

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 getBlackListCodes()
{
return $this->scopeConfig->getValue(self::ISO_BLACK_LIST);
}

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 getIsoCountry($ip)
{
$data = $this->getCountryData($ip);
if ($data && isset($data['iso'])) {
return strtolower($data['iso']);
}

return '';

}

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();
}
}
81 changes: 81 additions & 0 deletions app/code/MindArc/GeoIP/Observer/Locker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace MindArc\GeoIP\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class Locker
implements ObserverInterface
{
protected $helper;

protected $request;

private $responseFactory;

protected $actionFlag;

private $url;

protected $isDenied = false;

protected $_storeManagerInterface;

public function __construct(
\MindArc\GeoIP\Helper\GeoIP $helper,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
\Magento\Framework\UrlInterface $url
)
{
$this->helper = $helper;
$this->request = $request;
$this->responseFactory = $responseFactory;
$this->url = $url;
$this->_storeManagerInterface = $storeManagerInterface;
$this->actionFlag = $actionFlag;
}

public function execute(Observer $observer)
{
$ip = $this->helper->getIpAddress();
$blackList = $this->helper->getBlackListCodes();
if (!$blackList || empty($blackList)) {
return $this;
}
$blackList = explode(',', $blackList);
$isoCountry = $this->helper->getIsoCountry($ip);

$lockedCountry = $this->lockCountry($isoCountry, $blackList);
if ($lockedCountry) {
$this->denyCustomerAccess($observer);
}

return $this;

}

protected function denyCustomerAccess($observer)
{
$response = $observer->getControllerAction()->getResponse();

$response->clearBody()->setStatusCode(\Magento\Framework\App\Response\Http::STATUS_CODE_403);
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
}

protected function lockCountry($isoCountry, $blackList)
{
if ($blackList) {
foreach ($blackList as $iso) {
if (strtolower($isoCountry) === strtolower($iso)) {
return true;
}
}
}

return false;
}
}
52 changes: 52 additions & 0 deletions app/code/MindArc/GeoIP/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace MindArc\GeoIP\Setup;

use Magento\Cms\Model\BlockFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{

private $blockFactory;

public function __construct(BlockFactory $blockFactory)
{
$this->blockFactory = $blockFactory;
}

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '1.0.2', '<')) {
$cmsBlockData = [
'title' => 'Geo IP CMS Block',
'identifier' => 'geoip_global_block',
'content' => "<h1>This is a global Geo IP Block</h1>",
'is_active' => 1,
'stores' => [0],
'sort_order' => 0
];

$block = $this->blockFactory->create();
$block->setData($cmsBlockData)->save();

$cmsBlockData = [
'title' => 'USA Geo IP CMS Block',
'identifier' => 'geoip_us',
'content' => "<h1>This is a USA Geo IP Block</h1>",
'is_active' => 1,
'stores' => [0],
'sort_order' => 0
];

$block = $this->blockFactory->create();
$block->setData($cmsBlockData)->save();
}

$setup->endSetup();
}
}
32 changes: 32 additions & 0 deletions app/code/MindArc/GeoIP/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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>
<field id="blacklist" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>BlackList</label>
<comment>ISO-2 codes splited by comma</comment>
</field>
</group>
</section>
</system>
</config>
12 changes: 12 additions & 0 deletions app/code/MindArc/GeoIP/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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>
<blacklist>RU,CN</blacklist>
</setting>
</interactive_geoip>
</default>
</config>
6 changes: 6 additions & 0 deletions app/code/MindArc/GeoIP/etc/frontend/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="geoip_locker" instance="MindArc\GeoIP\Observer\Locker" />
</event>
</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.2">
</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" cacheable="false"/>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
$block->getChildHtml('countrycode');
$blockId = $block->getBlockID();
try {
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($blockId)->toHtml();
} catch (Exception $e) {
throw new Exception('Block Id "' . $blockId . '" not found, please create a new one from admin panel');
}
?>
Loading