From b0689987815125d2a6d213df6626802a6cedbc2a Mon Sep 17 00:00:00 2001 From: Kai Date: Thu, 30 Aug 2018 23:05:20 +1000 Subject: [PATCH 1/9] First Commit, GeoIP detection with ipstack and display block --- Kai/GeoIP/Block/Display.php | 46 +++++++++++ Kai/GeoIP/Model/GeoHandler.php | 81 +++++++++++++++++++ Kai/GeoIP/etc/module.xml | 4 + Kai/GeoIP/readme.md | 11 +++ Kai/GeoIP/registration.php | 6 ++ .../frontend/layout/catalog_product_view.xml | 8 ++ .../view/frontend/templates/global.phtml | 1 + 7 files changed, 157 insertions(+) create mode 100644 Kai/GeoIP/Block/Display.php create mode 100644 Kai/GeoIP/Model/GeoHandler.php create mode 100644 Kai/GeoIP/etc/module.xml create mode 100644 Kai/GeoIP/readme.md create mode 100644 Kai/GeoIP/registration.php create mode 100644 Kai/GeoIP/view/frontend/layout/catalog_product_view.xml create mode 100644 Kai/GeoIP/view/frontend/templates/global.phtml diff --git a/Kai/GeoIP/Block/Display.php b/Kai/GeoIP/Block/Display.php new file mode 100644 index 0000000..37271e3 --- /dev/null +++ b/Kai/GeoIP/Block/Display.php @@ -0,0 +1,46 @@ + + * @see https://github.com/bugcskai/ + */ +class Display extends \Magento\Framework\View\Element\Template +{ + private $geoHandler; + + private $countryCode; + + public function __construct( + \Magento\Framework\View\Element\Template\Context $context, + \Kai\GeoIP\Model\GeoHandler $geoHandler + ) + { + $this->geoHandler = $geoHandler; + + $this->setCurrentBlockTemplate(); + + parent::__construct($context); + } + + public function getCountryCode(){ + return $this->countryCode; + } + + protected function _toHtml() + { + if (!$this->getTemplate()) { + return __('Nothing to Render'); + } + return $this->fetchView($this->getTemplateFile()); + } + + private function setCurrentBlockTemplate(){ + $this->setTemplate('Kai_GeoIP::global.phtml'); + } +} \ No newline at end of file diff --git a/Kai/GeoIP/Model/GeoHandler.php b/Kai/GeoIP/Model/GeoHandler.php new file mode 100644 index 0000000..01f1e49 --- /dev/null +++ b/Kai/GeoIP/Model/GeoHandler.php @@ -0,0 +1,81 @@ + + * @see https://github.com/bugcskai/ + */ +class GeoHandler +{ + const IPSTACK_URL = 'http://api.ipstack.com/'; + + const IPSTACK_API_KEY = 'bf3b3ee9a0affbce479f174fd1a42041'; //this should be enviroment variable but. + + private $httpClient, $logger; + + public function __construct( + \Magento\Framework\HTTP\Client\Curl $curl, + \Psr\Log\LoggerInterface $logger + ){ + $this->httpClient = $curl; + $this->logger = $logger; + } + + + /** + * Get The location of the current user + * + * @return string country code + */ + public function getUserCountryCode(){ + + $ipAddress = $this->getIPAddress(); + + $params = $this->buildParameters(); + + $url = self::IPSTACK_URL . $ipAddress . '?' . http_build_query($params); + + $this->httpClient->get($url); + + try { + $response = json_decode( $this->httpClient->getBody() ); + return $response->country_code; + } catch (\Exception $e) { + $this->logger->error(__('Problem with ipstack')); + $this->logger->error( $e->getMessage() ); + } + + } + + public function getIPAddress() { + $ipaddress = ''; + + if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) + $ipaddress = $_SERVER['HTTP_CLIENT_IP']; + else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) + $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; + else if (array_key_exists('HTTP_X_FORWARDED', $_SERVER)) + $ipaddress = $_SERVER['HTTP_X_FORWARDED']; + else if (array_key_exists('HTTP_FORWARDED_FOR', $_SERVER)) + $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; + else if (array_key_exists('HTTP_FORWARDED', $_SERVER)) + $ipaddress = $_SERVER['HTTP_FORWARDED']; + else if (array_key_exists('REMOTE_ADDR', $_SERVER)) + $ipaddress = $_SERVER['REMOTE_ADDR']; + + return $ipaddress; + } + + private function buildParameters() { + return [ + 'access_key' => self::IPSTACK_API_KEY, + 'format' => '1', + ]; + } + + +} \ No newline at end of file diff --git a/Kai/GeoIP/etc/module.xml b/Kai/GeoIP/etc/module.xml new file mode 100644 index 0000000..4d97d76 --- /dev/null +++ b/Kai/GeoIP/etc/module.xml @@ -0,0 +1,4 @@ + + + + diff --git a/Kai/GeoIP/readme.md b/Kai/GeoIP/readme.md new file mode 100644 index 0000000..94c4331 --- /dev/null +++ b/Kai/GeoIP/readme.md @@ -0,0 +1,11 @@ +## User GeoIP and Restriction demo extension for magento 2 + +by Cheng Shea Kai + +https://github.com/bugcskai/ + +- Usage: adds text block in "content" xml section to indicate country code and display a different template according to country code + +- Restrict access for IP addresses from restricted countries + +- Uses ipstack.com for IP location checks \ No newline at end of file diff --git a/Kai/GeoIP/registration.php b/Kai/GeoIP/registration.php new file mode 100644 index 0000000..23a1f1f --- /dev/null +++ b/Kai/GeoIP/registration.php @@ -0,0 +1,6 @@ + + + + + + + + \ No newline at end of file diff --git a/Kai/GeoIP/view/frontend/templates/global.phtml b/Kai/GeoIP/view/frontend/templates/global.phtml new file mode 100644 index 0000000..ad6ae34 --- /dev/null +++ b/Kai/GeoIP/view/frontend/templates/global.phtml @@ -0,0 +1 @@ +

getCountryCode(); ?>,

\ No newline at end of file From 3a31767abd1770cd400461c05a62777316022612 Mon Sep 17 00:00:00 2001 From: Kai Date: Thu, 30 Aug 2018 23:07:04 +1000 Subject: [PATCH 2/9] Update: Add Us static block and display logic --- Kai/GeoIP/Block/Display.php | 15 ++++++++++++++- Kai/GeoIP/view/frontend/templates/us.phtml | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Kai/GeoIP/view/frontend/templates/us.phtml diff --git a/Kai/GeoIP/Block/Display.php b/Kai/GeoIP/Block/Display.php index 37271e3..9163dae 100644 --- a/Kai/GeoIP/Block/Display.php +++ b/Kai/GeoIP/Block/Display.php @@ -12,6 +12,8 @@ */ class Display extends \Magento\Framework\View\Element\Template { + const USA = 'US'; + private $geoHandler; private $countryCode; @@ -41,6 +43,17 @@ protected function _toHtml() } private function setCurrentBlockTemplate(){ - $this->setTemplate('Kai_GeoIP::global.phtml'); + + $this->countryCode = $this->geoHandler->getUserCountryCode(); + + switch ($this->countryCode) { + case self::USA: + $this->setTemplate('Kai_GeoIP::us.phtml'); + break; + + default: + $this->setTemplate('Kai_GeoIP::global.phtml'); + break; + } } } \ No newline at end of file diff --git a/Kai/GeoIP/view/frontend/templates/us.phtml b/Kai/GeoIP/view/frontend/templates/us.phtml new file mode 100644 index 0000000..2210083 --- /dev/null +++ b/Kai/GeoIP/view/frontend/templates/us.phtml @@ -0,0 +1 @@ +

getCountryCode(); ?>

\ No newline at end of file From c4cad63e53f1e0fd5dfe49d413ee721bb2fbd3c7 Mon Sep 17 00:00:00 2001 From: Kai Date: Thu, 30 Aug 2018 23:08:07 +1000 Subject: [PATCH 3/9] Update: Add plugin around controller to detect restrict country access --- Kai/GeoIP/Plugin/BlockCountry.php | 52 +++++++++++++++++++++++++++++++ Kai/GeoIP/etc/di.xml | 5 +++ 2 files changed, 57 insertions(+) create mode 100644 Kai/GeoIP/Plugin/BlockCountry.php create mode 100644 Kai/GeoIP/etc/di.xml diff --git a/Kai/GeoIP/Plugin/BlockCountry.php b/Kai/GeoIP/Plugin/BlockCountry.php new file mode 100644 index 0000000..ac2b04b --- /dev/null +++ b/Kai/GeoIP/Plugin/BlockCountry.php @@ -0,0 +1,52 @@ + + * @see https://github.com/bugcskai/ + */ +class BlockCountry +{ + private $geoHandler, $logger; + + private $restrictedCountries; + + public function __construct( + \Kai\GeoIP\Model\GeoHandler $geoHandler, + \Psr\Log\LoggerInterface $logger + ){ + $this->geoHandler = $geoHandler; + $this->logger = $logger; + + $this->restrictedCountries = ['RU','CN']; + } + + + public function aroundDispatch( + \Magento\Framework\App\FrontControllerInterface $subject, + callable $proceed, + \Magento\Framework\App\RequestInterface $request + ) { + + $countryCode = $this->geoHandler->getUserCountryCode(); + + if ( in_array($countryCode, $this->restrictedCountries) ) + { + $this->logger->info(__('User barred from site'.$this->geoHandler->getIPAddress())); + + throw new \Exception(__('You are blocked from accessing the site')); + } + + return $proceed($request); + + } + +} \ No newline at end of file diff --git a/Kai/GeoIP/etc/di.xml b/Kai/GeoIP/etc/di.xml new file mode 100644 index 0000000..a93487e --- /dev/null +++ b/Kai/GeoIP/etc/di.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From 942c8047f7ca7095794d6e8c80453db4ea3b9092 Mon Sep 17 00:00:00 2001 From: Kai Date: Fri, 31 Aug 2018 00:17:28 +1000 Subject: [PATCH 4/9] Cleanup: VsCode spacing and move to correct folder --- {Kai => app/code/Kai}/GeoIP/Block/Display.php | 18 +++++----- .../code/Kai}/GeoIP/Model/GeoHandler.php | 34 +++++++++---------- .../code/Kai}/GeoIP/Plugin/BlockCountry.php | 1 - {Kai => app/code/Kai}/GeoIP/etc/di.xml | 0 {Kai => app/code/Kai}/GeoIP/etc/module.xml | 0 {Kai => app/code/Kai}/GeoIP/readme.md | 0 {Kai => app/code/Kai}/GeoIP/registration.php | 0 .../frontend/layout/catalog_product_view.xml | 6 ++-- .../view/frontend/templates/global.phtml | 0 .../GeoIP/view/frontend/templates/us.phtml | 0 10 files changed, 29 insertions(+), 30 deletions(-) rename {Kai => app/code/Kai}/GeoIP/Block/Display.php (78%) rename {Kai => app/code/Kai}/GeoIP/Model/GeoHandler.php (63%) rename {Kai => app/code/Kai}/GeoIP/Plugin/BlockCountry.php (99%) rename {Kai => app/code/Kai}/GeoIP/etc/di.xml (100%) rename {Kai => app/code/Kai}/GeoIP/etc/module.xml (100%) rename {Kai => app/code/Kai}/GeoIP/readme.md (100%) rename {Kai => app/code/Kai}/GeoIP/registration.php (100%) rename {Kai => app/code/Kai}/GeoIP/view/frontend/layout/catalog_product_view.xml (56%) rename {Kai => app/code/Kai}/GeoIP/view/frontend/templates/global.phtml (100%) rename {Kai => app/code/Kai}/GeoIP/view/frontend/templates/us.phtml (100%) diff --git a/Kai/GeoIP/Block/Display.php b/app/code/Kai/GeoIP/Block/Display.php similarity index 78% rename from Kai/GeoIP/Block/Display.php rename to app/code/Kai/GeoIP/Block/Display.php index 9163dae..43439f8 100644 --- a/Kai/GeoIP/Block/Display.php +++ b/app/code/Kai/GeoIP/Block/Display.php @@ -36,10 +36,10 @@ public function getCountryCode(){ protected function _toHtml() { - if (!$this->getTemplate()) { - return __('Nothing to Render'); - } - return $this->fetchView($this->getTemplateFile()); + if (!$this->getTemplate()) { + return __('Nothing to Render'); + } + return $this->fetchView($this->getTemplateFile()); } private function setCurrentBlockTemplate(){ @@ -48,12 +48,12 @@ private function setCurrentBlockTemplate(){ switch ($this->countryCode) { case self::USA: - $this->setTemplate('Kai_GeoIP::us.phtml'); - break; - + $this->setTemplate('Kai_GeoIP::us.phtml'); + break; + default: - $this->setTemplate('Kai_GeoIP::global.phtml'); - break; + $this->setTemplate('Kai_GeoIP::global.phtml'); + break; } } } \ No newline at end of file diff --git a/Kai/GeoIP/Model/GeoHandler.php b/app/code/Kai/GeoIP/Model/GeoHandler.php similarity index 63% rename from Kai/GeoIP/Model/GeoHandler.php rename to app/code/Kai/GeoIP/Model/GeoHandler.php index 01f1e49..5b1f58c 100644 --- a/Kai/GeoIP/Model/GeoHandler.php +++ b/app/code/Kai/GeoIP/Model/GeoHandler.php @@ -29,8 +29,8 @@ public function __construct( /** * Get The location of the current user * - * @return string country code - */ + * @return string country code + */ public function getUserCountryCode(){ $ipAddress = $this->getIPAddress(); @@ -54,24 +54,24 @@ public function getUserCountryCode(){ public function getIPAddress() { $ipaddress = ''; - if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) - $ipaddress = $_SERVER['HTTP_CLIENT_IP']; - else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) - $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; - else if (array_key_exists('HTTP_X_FORWARDED', $_SERVER)) - $ipaddress = $_SERVER['HTTP_X_FORWARDED']; - else if (array_key_exists('HTTP_FORWARDED_FOR', $_SERVER)) - $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; - else if (array_key_exists('HTTP_FORWARDED', $_SERVER)) - $ipaddress = $_SERVER['HTTP_FORWARDED']; - else if (array_key_exists('REMOTE_ADDR', $_SERVER)) - $ipaddress = $_SERVER['REMOTE_ADDR']; - - return $ipaddress; + if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) + $ipaddress = $_SERVER['HTTP_CLIENT_IP']; + else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) + $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; + else if (array_key_exists('HTTP_X_FORWARDED', $_SERVER)) + $ipaddress = $_SERVER['HTTP_X_FORWARDED']; + else if (array_key_exists('HTTP_FORWARDED_FOR', $_SERVER)) + $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; + else if (array_key_exists('HTTP_FORWARDED', $_SERVER)) + $ipaddress = $_SERVER['HTTP_FORWARDED']; + else if (array_key_exists('REMOTE_ADDR', $_SERVER)) + $ipaddress = $_SERVER['REMOTE_ADDR']; + + return $ipaddress; } private function buildParameters() { - return [ + return [ 'access_key' => self::IPSTACK_API_KEY, 'format' => '1', ]; diff --git a/Kai/GeoIP/Plugin/BlockCountry.php b/app/code/Kai/GeoIP/Plugin/BlockCountry.php similarity index 99% rename from Kai/GeoIP/Plugin/BlockCountry.php rename to app/code/Kai/GeoIP/Plugin/BlockCountry.php index ac2b04b..903eca4 100644 --- a/Kai/GeoIP/Plugin/BlockCountry.php +++ b/app/code/Kai/GeoIP/Plugin/BlockCountry.php @@ -2,7 +2,6 @@ namespace Kai\GeoIP\Plugin; - /** * Plugin BlockCountry * diff --git a/Kai/GeoIP/etc/di.xml b/app/code/Kai/GeoIP/etc/di.xml similarity index 100% rename from Kai/GeoIP/etc/di.xml rename to app/code/Kai/GeoIP/etc/di.xml diff --git a/Kai/GeoIP/etc/module.xml b/app/code/Kai/GeoIP/etc/module.xml similarity index 100% rename from Kai/GeoIP/etc/module.xml rename to app/code/Kai/GeoIP/etc/module.xml diff --git a/Kai/GeoIP/readme.md b/app/code/Kai/GeoIP/readme.md similarity index 100% rename from Kai/GeoIP/readme.md rename to app/code/Kai/GeoIP/readme.md diff --git a/Kai/GeoIP/registration.php b/app/code/Kai/GeoIP/registration.php similarity index 100% rename from Kai/GeoIP/registration.php rename to app/code/Kai/GeoIP/registration.php diff --git a/Kai/GeoIP/view/frontend/layout/catalog_product_view.xml b/app/code/Kai/GeoIP/view/frontend/layout/catalog_product_view.xml similarity index 56% rename from Kai/GeoIP/view/frontend/layout/catalog_product_view.xml rename to app/code/Kai/GeoIP/view/frontend/layout/catalog_product_view.xml index 691358d..3e72914 100644 --- a/Kai/GeoIP/view/frontend/layout/catalog_product_view.xml +++ b/app/code/Kai/GeoIP/view/frontend/layout/catalog_product_view.xml @@ -1,8 +1,8 @@ - - - + + + \ No newline at end of file diff --git a/Kai/GeoIP/view/frontend/templates/global.phtml b/app/code/Kai/GeoIP/view/frontend/templates/global.phtml similarity index 100% rename from Kai/GeoIP/view/frontend/templates/global.phtml rename to app/code/Kai/GeoIP/view/frontend/templates/global.phtml diff --git a/Kai/GeoIP/view/frontend/templates/us.phtml b/app/code/Kai/GeoIP/view/frontend/templates/us.phtml similarity index 100% rename from Kai/GeoIP/view/frontend/templates/us.phtml rename to app/code/Kai/GeoIP/view/frontend/templates/us.phtml From d0eeb66e3802ca762d3286e2a584727c7904fbab Mon Sep 17 00:00:00 2001 From: Kai Date: Fri, 31 Aug 2018 08:02:49 +1000 Subject: [PATCH 5/9] Cleanup: tidy viariables --- app/code/Kai/GeoIP/Block/Display.php | 8 ++++---- app/code/Kai/GeoIP/Model/GeoHandler.php | 4 +++- app/code/Kai/GeoIP/Plugin/BlockCountry.php | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/code/Kai/GeoIP/Block/Display.php b/app/code/Kai/GeoIP/Block/Display.php index 43439f8..0c09a97 100644 --- a/app/code/Kai/GeoIP/Block/Display.php +++ b/app/code/Kai/GeoIP/Block/Display.php @@ -48,12 +48,12 @@ private function setCurrentBlockTemplate(){ switch ($this->countryCode) { case self::USA: - $this->setTemplate('Kai_GeoIP::us.phtml'); - break; + $this->setTemplate('Kai_GeoIP::us.phtml'); + break; default: - $this->setTemplate('Kai_GeoIP::global.phtml'); - break; + $this->setTemplate('Kai_GeoIP::global.phtml'); + break; } } } \ No newline at end of file diff --git a/app/code/Kai/GeoIP/Model/GeoHandler.php b/app/code/Kai/GeoIP/Model/GeoHandler.php index 5b1f58c..b77cfd4 100644 --- a/app/code/Kai/GeoIP/Model/GeoHandler.php +++ b/app/code/Kai/GeoIP/Model/GeoHandler.php @@ -15,7 +15,9 @@ class GeoHandler const IPSTACK_API_KEY = 'bf3b3ee9a0affbce479f174fd1a42041'; //this should be enviroment variable but. - private $httpClient, $logger; + private $httpClient; + + private $logger; public function __construct( \Magento\Framework\HTTP\Client\Curl $curl, diff --git a/app/code/Kai/GeoIP/Plugin/BlockCountry.php b/app/code/Kai/GeoIP/Plugin/BlockCountry.php index 903eca4..700512f 100644 --- a/app/code/Kai/GeoIP/Plugin/BlockCountry.php +++ b/app/code/Kai/GeoIP/Plugin/BlockCountry.php @@ -14,7 +14,9 @@ */ class BlockCountry { - private $geoHandler, $logger; + private $geoHandler; + + private $logger; private $restrictedCountries; From 33ca12c8d1c21d16c2b649e57c172aac430a50ba Mon Sep 17 00:00:00 2001 From: Kai Date: Fri, 31 Aug 2018 08:24:17 +1000 Subject: [PATCH 6/9] Cleanup: commenting --- app/code/Kai/GeoIP/Model/GeoHandler.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/code/Kai/GeoIP/Model/GeoHandler.php b/app/code/Kai/GeoIP/Model/GeoHandler.php index b77cfd4..77806dd 100644 --- a/app/code/Kai/GeoIP/Model/GeoHandler.php +++ b/app/code/Kai/GeoIP/Model/GeoHandler.php @@ -31,7 +31,7 @@ public function __construct( /** * Get The location of the current user * - * @return string country code + * @return string country code in ISO 3166-A2 format */ public function getUserCountryCode(){ @@ -52,7 +52,12 @@ public function getUserCountryCode(){ } } - + + /** + * Return IP Address of user + * + * @return string IP address + */ public function getIPAddress() { $ipaddress = ''; From f431327e48fe1de0cba6ae295944b69a3147fe54 Mon Sep 17 00:00:00 2001 From: Kai Date: Fri, 31 Aug 2018 08:54:57 +1000 Subject: [PATCH 7/9] Cleanup: for psr2 --- app/code/Kai/GeoIP/Block/Display.php | 90 ++++++------- app/code/Kai/GeoIP/Model/GeoHandler.php | 122 +++++++++--------- app/code/Kai/GeoIP/Plugin/BlockCountry.php | 74 +++++------ app/code/Kai/GeoIP/etc/di.xml | 6 +- app/code/Kai/GeoIP/etc/module.xml | 2 +- app/code/Kai/GeoIP/registration.php | 8 +- .../frontend/layout/catalog_product_view.xml | 10 +- 7 files changed, 156 insertions(+), 156 deletions(-) diff --git a/app/code/Kai/GeoIP/Block/Display.php b/app/code/Kai/GeoIP/Block/Display.php index 0c09a97..5c35af4 100644 --- a/app/code/Kai/GeoIP/Block/Display.php +++ b/app/code/Kai/GeoIP/Block/Display.php @@ -12,48 +12,48 @@ */ class Display extends \Magento\Framework\View\Element\Template { - const USA = 'US'; - - private $geoHandler; - - private $countryCode; - - public function __construct( - \Magento\Framework\View\Element\Template\Context $context, - \Kai\GeoIP\Model\GeoHandler $geoHandler - ) - { - $this->geoHandler = $geoHandler; - - $this->setCurrentBlockTemplate(); - - parent::__construct($context); - } - - public function getCountryCode(){ - return $this->countryCode; - } - - protected function _toHtml() - { - if (!$this->getTemplate()) { - return __('Nothing to Render'); - } - return $this->fetchView($this->getTemplateFile()); - } - - private function setCurrentBlockTemplate(){ - - $this->countryCode = $this->geoHandler->getUserCountryCode(); - - switch ($this->countryCode) { - case self::USA: - $this->setTemplate('Kai_GeoIP::us.phtml'); - break; - - default: - $this->setTemplate('Kai_GeoIP::global.phtml'); - break; - } - } -} \ No newline at end of file + const USA = 'US'; + + private $geoHandler; + + private $countryCode; + + public function __construct( + \Magento\Framework\View\Element\Template\Context $context, + \Kai\GeoIP\Model\GeoHandler $geoHandler + ) + { + $this->geoHandler = $geoHandler; + + $this->setCurrentBlockTemplate(); + + parent::__construct($context); + } + + public function getCountryCode(){ + return $this->countryCode; + } + + protected function _toHtml() + { + if (!$this->getTemplate()) { + return __('Nothing to Render'); + } + return $this->fetchView($this->getTemplateFile()); + } + + private function setCurrentBlockTemplate(){ + + $this->countryCode = $this->geoHandler->getUserCountryCode(); + + switch ($this->countryCode) { + case self::USA: + $this->setTemplate('Kai_GeoIP::us.phtml'); + break; + + default: + $this->setTemplate('Kai_GeoIP::global.phtml'); + break; + } + } +} diff --git a/app/code/Kai/GeoIP/Model/GeoHandler.php b/app/code/Kai/GeoIP/Model/GeoHandler.php index 77806dd..b5cb830 100644 --- a/app/code/Kai/GeoIP/Model/GeoHandler.php +++ b/app/code/Kai/GeoIP/Model/GeoHandler.php @@ -11,78 +11,78 @@ */ class GeoHandler { - const IPSTACK_URL = 'http://api.ipstack.com/'; + const IPSTACK_URL = 'http://api.ipstack.com/'; - const IPSTACK_API_KEY = 'bf3b3ee9a0affbce479f174fd1a42041'; //this should be enviroment variable but. + const IPSTACK_API_KEY = 'bf3b3ee9a0affbce479f174fd1a42041'; //this should be enviroment variable but. - private $httpClient; - - private $logger; + private $httpClient; + + private $logger; - public function __construct( - \Magento\Framework\HTTP\Client\Curl $curl, - \Psr\Log\LoggerInterface $logger - ){ - $this->httpClient = $curl; - $this->logger = $logger; - } + public function __construct( + \Magento\Framework\HTTP\Client\Curl $curl, + \Psr\Log\LoggerInterface $logger + ){ + $this->httpClient = $curl; + $this->logger = $logger; + } - /** - * Get The location of the current user - * - * @return string country code in ISO 3166-A2 format - */ - public function getUserCountryCode(){ + /** + * Get The location of the current user + * + * @return string country code in ISO 3166-A2 format + */ + public function getUserCountryCode(){ - $ipAddress = $this->getIPAddress(); + $ipAddress = $this->getIPAddress(); - $params = $this->buildParameters(); + $params = $this->buildParameters(); - $url = self::IPSTACK_URL . $ipAddress . '?' . http_build_query($params); - - $this->httpClient->get($url); + $url = self::IPSTACK_URL . $ipAddress . '?' . http_build_query($params); + + $this->httpClient->get($url); - try { - $response = json_decode( $this->httpClient->getBody() ); - return $response->country_code; - } catch (\Exception $e) { - $this->logger->error(__('Problem with ipstack')); - $this->logger->error( $e->getMessage() ); - } + try { + $response = json_decode( $this->httpClient->getBody() ); + return $response->country_code; + } catch (\Exception $e) { + $this->logger->error(__('Problem with ipstack')); + $this->logger->error( $e->getMessage() ); + } - } - - /** - * Return IP Address of user - * - * @return string IP address - */ - public function getIPAddress() { - $ipaddress = ''; - - if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) - $ipaddress = $_SERVER['HTTP_CLIENT_IP']; - else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) - $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; - else if (array_key_exists('HTTP_X_FORWARDED', $_SERVER)) - $ipaddress = $_SERVER['HTTP_X_FORWARDED']; - else if (array_key_exists('HTTP_FORWARDED_FOR', $_SERVER)) - $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; - else if (array_key_exists('HTTP_FORWARDED', $_SERVER)) - $ipaddress = $_SERVER['HTTP_FORWARDED']; - else if (array_key_exists('REMOTE_ADDR', $_SERVER)) - $ipaddress = $_SERVER['REMOTE_ADDR']; + } + + /** + * Return IP Address of user + * + * @return string IP address + */ + public function getIPAddress() { + $ipaddress = ''; + + if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) + $ipaddress = $_SERVER['HTTP_CLIENT_IP']; + else if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) + $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; + else if (array_key_exists('HTTP_X_FORWARDED', $_SERVER)) + $ipaddress = $_SERVER['HTTP_X_FORWARDED']; + else if (array_key_exists('HTTP_FORWARDED_FOR', $_SERVER)) + $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; + else if (array_key_exists('HTTP_FORWARDED', $_SERVER)) + $ipaddress = $_SERVER['HTTP_FORWARDED']; + else if (array_key_exists('REMOTE_ADDR', $_SERVER)) + $ipaddress = $_SERVER['REMOTE_ADDR']; - return $ipaddress; - } + return $ipaddress; + } - private function buildParameters() { - return [ - 'access_key' => self::IPSTACK_API_KEY, - 'format' => '1', - ]; - } + private function buildParameters() { + return [ + 'access_key' => self::IPSTACK_API_KEY, + 'format' => '1', + ]; + } -} \ No newline at end of file +} diff --git a/app/code/Kai/GeoIP/Plugin/BlockCountry.php b/app/code/Kai/GeoIP/Plugin/BlockCountry.php index 700512f..b7dd45e 100644 --- a/app/code/Kai/GeoIP/Plugin/BlockCountry.php +++ b/app/code/Kai/GeoIP/Plugin/BlockCountry.php @@ -14,40 +14,40 @@ */ class BlockCountry { - private $geoHandler; - - private $logger; - - private $restrictedCountries; - - public function __construct( - \Kai\GeoIP\Model\GeoHandler $geoHandler, - \Psr\Log\LoggerInterface $logger - ){ - $this->geoHandler = $geoHandler; - $this->logger = $logger; - - $this->restrictedCountries = ['RU','CN']; - } - - - public function aroundDispatch( - \Magento\Framework\App\FrontControllerInterface $subject, - callable $proceed, - \Magento\Framework\App\RequestInterface $request - ) { - - $countryCode = $this->geoHandler->getUserCountryCode(); - - if ( in_array($countryCode, $this->restrictedCountries) ) - { - $this->logger->info(__('User barred from site'.$this->geoHandler->getIPAddress())); - - throw new \Exception(__('You are blocked from accessing the site')); - } - - return $proceed($request); - - } - -} \ No newline at end of file + private $geoHandler; + + private $logger; + + private $restrictedCountries; + + public function __construct( + \Kai\GeoIP\Model\GeoHandler $geoHandler, + \Psr\Log\LoggerInterface $logger + ){ + $this->geoHandler = $geoHandler; + $this->logger = $logger; + + $this->restrictedCountries = ['RU','CN']; + } + + + public function aroundDispatch( + \Magento\Framework\App\FrontControllerInterface $subject, + callable $proceed, + \Magento\Framework\App\RequestInterface $request + ) { + + $countryCode = $this->geoHandler->getUserCountryCode(); + + if ( in_array($countryCode, $this->restrictedCountries) ) + { + $this->logger->info(__('User barred from site'.$this->geoHandler->getIPAddress())); + + throw new \Exception(__('You are blocked from accessing the site')); + } + + return $proceed($request); + + } + +} diff --git a/app/code/Kai/GeoIP/etc/di.xml b/app/code/Kai/GeoIP/etc/di.xml index a93487e..5072bc5 100644 --- a/app/code/Kai/GeoIP/etc/di.xml +++ b/app/code/Kai/GeoIP/etc/di.xml @@ -1,5 +1,5 @@ - - - + + + \ No newline at end of file diff --git a/app/code/Kai/GeoIP/etc/module.xml b/app/code/Kai/GeoIP/etc/module.xml index 4d97d76..cc4a4c2 100644 --- a/app/code/Kai/GeoIP/etc/module.xml +++ b/app/code/Kai/GeoIP/etc/module.xml @@ -1,4 +1,4 @@ - + diff --git a/app/code/Kai/GeoIP/registration.php b/app/code/Kai/GeoIP/registration.php index 23a1f1f..4437270 100644 --- a/app/code/Kai/GeoIP/registration.php +++ b/app/code/Kai/GeoIP/registration.php @@ -1,6 +1,6 @@ - - - - - + + + + + \ No newline at end of file From 1e7193ec9bcdbf1fa2fd100349f5f1ecc6fc52d2 Mon Sep 17 00:00:00 2001 From: Cheng Date: Fri, 31 Aug 2018 13:43:31 +1000 Subject: [PATCH 8/9] Fix: update according to magento 2 guidelines --- app/code/Kai/GeoIP/Block/Display.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/code/Kai/GeoIP/Block/Display.php b/app/code/Kai/GeoIP/Block/Display.php index 5c35af4..80f62d2 100644 --- a/app/code/Kai/GeoIP/Block/Display.php +++ b/app/code/Kai/GeoIP/Block/Display.php @@ -2,10 +2,9 @@ namespace Kai\GeoIP\Block; /** - * class GeoHandler + * class Display * - * Does the IP checks, geolocation checks, and heavy lifting, requires http://ipstack.com API - * Functions are pretty self explainatory + * Block class to display relevant templates * * @author Cheng Shea kai * @see https://github.com/bugcskai/ @@ -25,8 +24,6 @@ public function __construct( { $this->geoHandler = $geoHandler; - $this->setCurrentBlockTemplate(); - parent::__construct($context); } @@ -36,12 +33,20 @@ public function getCountryCode(){ protected function _toHtml() { - if (!$this->getTemplate()) { - return __('Nothing to Render'); - } - return $this->fetchView($this->getTemplateFile()); + + $this->setCurrentBlockTemplate(); + if (!$this->getTemplate()) { + return __('Nothing to Render'); + } + + return $this->fetchView($this->getTemplateFile()); } + /** + * Check the Country code and assign template + * + * @return void + */ private function setCurrentBlockTemplate(){ $this->countryCode = $this->geoHandler->getUserCountryCode(); From 3cc19e8f46ed1cce9d38f1c550173a0e9be07c5e Mon Sep 17 00:00:00 2001 From: Kai Date: Mon, 3 Sep 2018 16:37:57 +1000 Subject: [PATCH 9/9] Update: Usage magento core to store API key --- app/code/Kai/GeoIP/Model/GeoHandler.php | 13 ++++++++----- app/code/Kai/GeoIP/etc/adminhtml/system.xml | 21 +++++++++++++++++++++ app/code/Kai/GeoIP/etc/config.xml | 10 ++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 app/code/Kai/GeoIP/etc/adminhtml/system.xml create mode 100644 app/code/Kai/GeoIP/etc/config.xml diff --git a/app/code/Kai/GeoIP/Model/GeoHandler.php b/app/code/Kai/GeoIP/Model/GeoHandler.php index b5cb830..c35d0dc 100644 --- a/app/code/Kai/GeoIP/Model/GeoHandler.php +++ b/app/code/Kai/GeoIP/Model/GeoHandler.php @@ -13,18 +13,20 @@ class GeoHandler { const IPSTACK_URL = 'http://api.ipstack.com/'; - const IPSTACK_API_KEY = 'bf3b3ee9a0affbce479f174fd1a42041'; //this should be enviroment variable but. - private $httpClient; + + private $scopeConfig; private $logger; public function __construct( \Magento\Framework\HTTP\Client\Curl $curl, - \Psr\Log\LoggerInterface $logger + \Psr\Log\LoggerInterface $logger, + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ){ $this->httpClient = $curl; $this->logger = $logger; + $this->scopeConfig = $scopeConfig; } @@ -78,11 +80,12 @@ public function getIPAddress() { } private function buildParameters() { + $api_key = $this->scopeConfig->getValue( 'geoip/general/geoapikey', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); + return [ - 'access_key' => self::IPSTACK_API_KEY, + 'access_key' => $api_key, 'format' => '1', ]; } - } diff --git a/app/code/Kai/GeoIP/etc/adminhtml/system.xml b/app/code/Kai/GeoIP/etc/adminhtml/system.xml new file mode 100644 index 0000000..cffb60e --- /dev/null +++ b/app/code/Kai/GeoIP/etc/adminhtml/system.xml @@ -0,0 +1,21 @@ + + + + + + +
+ separator-top + + kaie + Kai_GeoIP::geoip + + + + + This API to be used with ipstack.com. + + +
+
+
\ No newline at end of file diff --git a/app/code/Kai/GeoIP/etc/config.xml b/app/code/Kai/GeoIP/etc/config.xml new file mode 100644 index 0000000..6c1b64f --- /dev/null +++ b/app/code/Kai/GeoIP/etc/config.xml @@ -0,0 +1,10 @@ + + + + + + bf3b3ee9a0affbce479f174fd1a42041 + + + + \ No newline at end of file