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
52 changes: 52 additions & 0 deletions app/code/Mindarc/GeoIP/Block/Product/View/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Mindarc Pty Ltd.
*
* @category Mindarc
* @package Mindarc_GeoIP
* @author Mindarc Team <hello@mindarc.com.au>
* @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/)
*/

namespace Mindarc\GeoIP\Block\Product\View;

/**
* Class Info
* @package Mindarc\GeoIP\Model
*/
class Info implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
/**
* @var \Mindarc\GeoIP\Model\GeoIP
*/
private $geoIP;

/**
* Info constructor.
* @param \Mindarc\GeoIP\Model\GeoIP $geoIP
*/
public function __construct(
\Mindarc\GeoIP\Model\GeoIP $geoIP
) {
$this->geoIP = $geoIP;
}

/**
* is the accessing user from US
*
* @return bool
*/
public function isUsUser()
{
$countryCode = $this->geoIP->getCountryCode();
return $countryCode == 'US';
}

/**
* @return string
*/
public function getCountryCode()
{
return $countryCode = $this->geoIP->getCountryCode();
}
}
125 changes: 125 additions & 0 deletions app/code/Mindarc/GeoIP/Model/GeoIP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Mindarc Pty Ltd.
*
* @category Mindarc
* @package Mindarc_GeoIP
* @author Mindarc Team <hello@mindarc.com.au>
* @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/)
*/

namespace Mindarc\GeoIP\Model;

/**
* Class GeoIP
* @package Mindarc\GeoIP\Model
*/
class GeoIP
{
/**
* ip check service
*/
const GEO_IP_SERVICE_URL = 'http://ip-api.com/json/';

/**
* curl connection timeout
*/
const CURL_CONNECTION_TIMEOUT = '15';

/**
* curl reponse timeout
*/
const CURL_RESPONSE_TIMEOUT = '15';

/**
* @var array
*/
private $countryData;

/**
* get country code
*
* @return mixed|null
*/
public function getCountryCode()
{
if (empty($this->countryData)) {
$this->getUserCountry();
}
if (!empty($this->countryData['countryCode'])) {
return $this->countryData['countryCode'];
}
return null;
}

/**
* get and store country data
*/
private function getUserCountry()
{
$ip = $this->getIP();
$this->countryData = $this->getCountryByIP($ip);
}

/**
* get the IP address of the user
*
* @return string
*/
private function getIP()
{
$ip = '';
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
$ip = $_SERVER['HTTP_X_FORWARDED'];
} elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_FORWARDED'])) {
$ip = $_SERVER['HTTP_FORWARDED'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

/**
* retrieve country data for the ip
*
* @param $ip
* @return mixed|string
*/
private function getCountryByIP($ip)
{
$country = '';
try {
$ch = curl_init();
if (false === $ch) {
$error = __('failed to initialize service');
}

curl_setopt($ch, CURLOPT_URL, self::GEO_IP_SERVICE_URL . $ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, self::CURL_RESPONSE_TIMEOUT);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CURL_CONNECTION_TIMEOUT);

$content = curl_exec($ch);
if ($content === false) {
$error = __('Service not responding');
}

if ($content) {
$country = json_decode($content, true);
} else {
$error = __('Encode Error');
}
curl_close($ch);
} catch (\Exception $e) {
$error = sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage());
}

return $country;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Mindarc Pty Ltd.
*
* @category Mindarc
* @package Mindarc_GeoIP
* @author Mindarc Team <hello@mindarc.com.au>
* @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/)
*/

namespace Mindarc\GeoIP\Observer;

/**
* Class ControllerActionPreDispatchObserver
* @package Mindarc\GeoIP\Observer
*/
class ControllerActionPreDispatchObserver implements \Magento\Framework\Event\ObserverInterface
{
/**
* scope config xml path for restriction status
*/
const XML_PATH_GEOIP_RESTRICTION_ENABLED = 'geoip/restriction/enabled';

/**
* scope config xml path for restricted countries
*/
const XML_PATH_GEOIP_RESTRICtED_COUNTRIES = 'geoip/restriction/countries';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @var \Mindarc\GeoIP\Model\GeoIP
*/
private $geoIP;

/**
* @var \Magento\Framework\App\Response\Http
*/
private $response;

/**
* @var \Magento\Framework\App\ActionFlag
*/
private $actionFlag;

/**
* ControllerActionPreDispatchObserver constructor.
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Mindarc\GeoIP\Model\GeoIP $geoIP
* @param \Magento\Framework\App\Response\Http $response
* @param \Magento\Framework\App\ActionFlag $actionFlag
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Mindarc\GeoIP\Model\GeoIP $geoIP,
\Magento\Framework\App\Response\Http $response,
\Magento\Framework\App\ActionFlag $actionFlag
) {
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->geoIP = $geoIP;
$this->response = $response;
$this->actionFlag = $actionFlag;
}

/**
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Backend\Model\View\Result\Forward|void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($this->scopeConfig->isSetFlag(
self::XML_PATH_GEOIP_RESTRICTION_ENABLED,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()->getId())
) {
// get the list of countries from the store configurations
$restrictedCountries = $this->scopeConfig->getValue(
self::XML_PATH_GEOIP_RESTRICtED_COUNTRIES,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()->getId()
);

if (!empty($restrictedCountries)) {
$restrictedCountries = explode(',', $restrictedCountries);
$userCountryCode = $this->geoIP->getCountryCode();

$requestUri = $observer->getRequest()->getUriString();
// if the country is a restricted one and the url is not the 404 page
if (in_array($userCountryCode, $restrictedCountries) && strpos($requestUri, 'no-route') === false) {
// redirect to the 404 page
$this->response->setRedirect('/no-route');
$this->actionFlag->set('', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH, true);

return;
}
}
}
}
}
3 changes: 3 additions & 0 deletions app/code/Mindarc/GeoIP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>Mindarc_GeoIP Module</h2>

This module will manage the content based on the IP and country where the users are accessing from.
76 changes: 76 additions & 0 deletions app/code/Mindarc/GeoIP/Setup/InstallData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Mindarc Pty Ltd.
*
* @category Mindarc
* @package Mindarc_GeoIP
* @author Mindarc Team <hello@mindarc.com.au>
* @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/)
*/

namespace Mindarc\GeoIP\Setup;

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

/**
* Class InstallData
* @package Mindarc\GeoIP\Setup
*/
class InstallData implements InstallDataInterface
{
/**
* @var BlockFactory
*/
private $blockFactory;

/**
* InstallData constructor.
* @param BlockFactory $modelBlockFactory
*/
public function __construct(
BlockFactory $modelBlockFactory
) {
$this->blockFactory = $modelBlockFactory;
}

/**
* {@inheritdoc}
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

$cmsBlocks = [
[
'title' => 'Product Information - US',
'identifier' => 'product_information_us',
'content' => '<div>Static block content for US users</div>',

],
[
'title' => 'Product Information - Global',
'identifier' => 'product_information_global',
'content' => '<div>Static block content for Global users</div>',
],

];

foreach ($cmsBlocks as $data) {
$cmsBlock = $this->blockFactory->create();
$cmsBlock->getResource()->load($cmsBlock, $data['identifier']);
if (!$cmsBlock->getData()) {
$cmsBlock->setData($data);
} else {
$cmsBlock->addData($data);
}
$cmsBlock->setStores([\Magento\Store\Model\Store::DEFAULT_STORE_ID]);
$cmsBlock->setIsActive(1);
$cmsBlock->save();
}

$setup->endSetup();
}
}
24 changes: 24 additions & 0 deletions app/code/Mindarc/GeoIP/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "mindarc/module-geoip",
"description": "Mindarc Magento 2 module for IP restrictions",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0|~7.1.0",
"magento/module-config": "*",
"magento/framework": "*",
"geoip/geoip": "~1.16"
},
"type": "magento2-module",
"version": "100.23.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Mindarc\\GeoIP\\": ""
}
}
}
Loading