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;
}
}
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\\": ""
}
}
}
14 changes: 14 additions & 0 deletions app/code/Mindarc/GeoIP/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* 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/)
*/
-->
<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="2.0.0" />
</config>
14 changes: 14 additions & 0 deletions app/code/Mindarc/GeoIP/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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/)
*/
\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,32 @@
<?xml version="1.0"?>
<!--
/**
* 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/)
*/
-->
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block name="product.geo.info" template="Mindarc_GeoIP::product/view/info.phtml" before="-" cacheable="false">
<arguments>
<argument name="viewModel" xsi:type="object">Mindarc\GeoIP\Block\Product\View\Info</argument>
</arguments>
<block class="Magento\Cms\Block\Block" name="product.geo.info.us">
<arguments>
<argument name="block_id" xsi:type="string">product_information_us</argument>
</arguments>
</block>
<block class="Magento\Cms\Block\Block" name="product.geo.info.global">
<arguments>
<argument name="block_id" xsi:type="string">product_information_global</argument>
</arguments>
</block>
</block>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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/)
*/

/** @var $viewModel \Mindarc\GeoIP\Block\Product\View\Info */
$viewModel = $block->getData('viewModel');
$countryCode = $viewModel->getCountryCode();
if (!empty($countryCode)):
?>
<div>
<span><?= __('Country Code: %1', $viewModel->getCountryCode()) ?></span>
<span>
<?php if ($viewModel->isUsUser()): ?>
<?= $block->getChildHtml('product.geo.info.us') ?>
<?php else: ?>
<?= $block->getChildHtml('product.geo.info.global') ?>
<?php endif; ?>
</span>
</div>
<?php endif; ?>