diff --git a/app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php b/app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php new file mode 100644 index 0000000..646b3cb --- /dev/null +++ b/app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php @@ -0,0 +1,20 @@ +modelCountry = $country; + parent::__construct($context, $data); + } + public function getCountryCode() + { + return $this->modelCountry->getCountryCode();; + } +} \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/Model/Country.php b/app/code/MyCompany/IpRestrict/Model/Country.php new file mode 100644 index 0000000..00f9a7b --- /dev/null +++ b/app/code/MyCompany/IpRestrict/Model/Country.php @@ -0,0 +1,34 @@ +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; + } +} \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/Observer/CountryObserver.php b/app/code/MyCompany/IpRestrict/Observer/CountryObserver.php new file mode 100644 index 0000000..7973a75 --- /dev/null +++ b/app/code/MyCompany/IpRestrict/Observer/CountryObserver.php @@ -0,0 +1,27 @@ +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 "

You are not allowed to access website

"; + exit(); + } + } + } \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/etc/frontend/events.xml b/app/code/MyCompany/IpRestrict/etc/frontend/events.xml new file mode 100644 index 0000000..823bcaa --- /dev/null +++ b/app/code/MyCompany/IpRestrict/etc/frontend/events.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/etc/module.xml b/app/code/MyCompany/IpRestrict/etc/module.xml new file mode 100644 index 0000000..3c1bb3f --- /dev/null +++ b/app/code/MyCompany/IpRestrict/etc/module.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/registration.php b/app/code/MyCompany/IpRestrict/registration.php new file mode 100644 index 0000000..a971ba9 --- /dev/null +++ b/app/code/MyCompany/IpRestrict/registration.php @@ -0,0 +1,7 @@ + + + + + + + + + + + diff --git a/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml b/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml new file mode 100644 index 0000000..e7fc84f --- /dev/null +++ b/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml @@ -0,0 +1,15 @@ +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; + } +} \ No newline at end of file