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

Ex 00 ip restrict block country #3

Open
wants to merge 3 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
20 changes: 20 additions & 0 deletions app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace MyCompany\IpRestrict\Block\Product;

class CountryCode extends \Magento\Framework\View\Element\Template
{
protected $modelCountry = null;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\MyCompany\IpRestrict\Model\Country $country,
array $data = []
) {
$this->modelCountry = $country;
parent::__construct($context, $data);
}
public function getCountryCode()
{
return $this->modelCountry->getCountryCode();;
}
}
34 changes: 34 additions & 0 deletions app/code/MyCompany/IpRestrict/Model/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace MyCompany\IpRestrict\Model;


class Country extends \Magento\Framework\Model\AbstractModel
{

public function getCountryCode()
{
$ip = $this->get_client_ip();
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));

return $ipdat;
}

protected function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
}
27 changes: 27 additions & 0 deletions app/code/MyCompany/IpRestrict/Observer/CountryObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace MyCompany\IpRestrict\Observer;


class CountryObserver implements \Magento\Framework\Event\ObserverInterface

{
public function __construct(
\MyCompany\IpRestrict\Model\Country $country
) {
$this->modelCountry = $country;
}
/**
* Below is the method that will fire whenever the event runs!
*
* @param Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$objCountry = $this->modelCountry->getCountryCode();
if($objCountry->geoplugin_countryCode=='CN' || $objCountry->geoplugin_countryCode=='RU')
{
echo "<h1>You are not allowed to access website</h1>";
exit();
}
}
}
6 changes: 6 additions & 0 deletions app/code/MyCompany/IpRestrict/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="MyCompany_IpRestrict_Restriction" instance="MyCompany\IpRestrict\Observer\CountryObserver" />
</event>
</config>
6 changes: 6 additions & 0 deletions app/code/MyCompany/IpRestrict/etc/module.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:Module/etc/module.xsd">
<module name="MyCompany_IpRestrict" setup_version="1.0.0">
</module>
</config>
7 changes: 7 additions & 0 deletions app/code/MyCompany/IpRestrict/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MyCompany_IpRestrict',
__DIR__
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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="product.info.main">
<container name="product.info.country" label="Product info country container" htmlTag="div" htmlClass="product-info-country" before="-">
<block class="MyCompany\IpRestrict\Block\Product\CountryCode" name="iprestrict_country" cacheable="false" template="MyCompany_IpRestrict::product/country.phtml" before="product.info.price"/>
</container>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
if(is_object($this->getCountryCode()))
{
$countryName = $this->getCountryCode()->geoplugin_countryName;
$countryCode = $this->getCountryCode()->geoplugin_countryCode;
echo "Your Country: ".$countryName." (".$countryCode.")";
if(isset($countryCode) && $countryCode!='')
{
if($countryCode=='US'):
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('country_block_us')->toHtml();
else:
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('country_block_global')->toHtml();
endif;
}
}