diff --git a/app/code/MindArc/GeoIP/Block/Catalog/Product/View/CountryCode.php b/app/code/MindArc/GeoIP/Block/Catalog/Product/View/CountryCode.php new file mode 100644 index 0000000..ce78263 --- /dev/null +++ b/app/code/MindArc/GeoIP/Block/Catalog/Product/View/CountryCode.php @@ -0,0 +1,40 @@ +_geoIP = $geoIp; + parent::__construct($context, $data); + } + + public function getCountryISO() + { + $ip = $this->_geoIP->getIpAddress(); + $data = $this->_geoIP->getCountryData($ip); + if ($data !== null && !empty($data['iso'])) { + $code = $data['iso']; + } else { + $code = "It coudn\\'t get the country Iso code "; + } + + return $code; + } + + public function getBlockID() + { + $iso = $this->getCountryISO(); + + return strtolower('geoip_' . $iso); + } +} diff --git a/app/code/MindArc/GeoIP/Helper/GeoIP.php b/app/code/MindArc/GeoIP/Helper/GeoIP.php new file mode 100644 index 0000000..8fca8e2 --- /dev/null +++ b/app/code/MindArc/GeoIP/Helper/GeoIP.php @@ -0,0 +1,111 @@ +_directoryList = $directoryList; + parent::__construct($context, $objectManager, $storeManager); + } + + public function getDataBaseFile() + { + return $this->scopeConfig->getValue(self::DATABASE_PATH); + } + + public function simulationMode() + { + return $this->scopeConfig->getValue(self::SIMULATION_MODE); + } + + public function myIP() + { + return $this->scopeConfig->getValue(self::MY_IP); + } + + public function checkDB() + { + $path = $this->_directoryList->getPath('base') . '/' . $this->getDataBaseFile(); + if (!file_exists($path)) { + return false; + } + $folder = scandir($path, true); + $pathFile = $path . '/' . $folder[0]; + if (!file_exists($pathFile)) { + return false; + } + + return $pathFile; + } + + public function getCountryData($ip) + { + try { + $libPath = $this->checkDB(); + if ($libPath && class_exists('GeoIp2\Database\Reader')) { + $geoIp = new \GeoIp2\Database\Reader($libPath); + $record = $geoIp->country($ip); + $geoIpData = [ + 'name' => $record->country->name, + 'iso' => $record->country->isoCode, + ]; + } else { + $geoIpData = []; + } + } catch (\Exception $e) { + $geoIpData = [ + 'name' => 'NONE', + 'iso' => 'NONE', + ]; + } + + return $geoIpData; + } + + public function getIpAddress() + { + if ($this->simulationMode()) { + return $this->myIP(); + } + + $server = $this->_getRequest()->getServer(); + if (!empty($server['HTTP_CLIENT_IP'])) { + $ip = $server['HTTP_CLIENT_IP']; + } else if (!empty($server['HTTP_X_FORWARDED_FOR'])) { + $ip = $server['HTTP_X_FORWARDED_FOR']; + } else if (!empty($server['REMOTE_ADDR'])) { + $ip = $server['REMOTE_ADDR']; + } else { + $ip = $this->getIpFromMagento(); + } + $ips = explode(',', $ip); + $ip = $ips[count($ips) - 1]; + + return trim($ip); + } + + protected function getIpFromMagento() + { + return $this->_remoteAddress->getRemoteAddress(); + } +} \ No newline at end of file diff --git a/app/code/MindArc/GeoIP/etc/adminhtml/system.xml b/app/code/MindArc/GeoIP/etc/adminhtml/system.xml new file mode 100644 index 0000000..25441a1 --- /dev/null +++ b/app/code/MindArc/GeoIP/etc/adminhtml/system.xml @@ -0,0 +1,28 @@ + + + + +
+ + general + Summa_Paypal::lco_paypal + + + + + + + + Magento\Config\Model\Config\Source\Yesno + + + + Insert a new Ip for simulation mode + + 1 + + + +
+
+
diff --git a/app/code/MindArc/GeoIP/etc/config.xml b/app/code/MindArc/GeoIP/etc/config.xml new file mode 100644 index 0000000..d553803 --- /dev/null +++ b/app/code/MindArc/GeoIP/etc/config.xml @@ -0,0 +1,11 @@ + + + + + + + var/GeoIP/DataBase + + + + diff --git a/app/code/MindArc/GeoIP/etc/module.xml b/app/code/MindArc/GeoIP/etc/module.xml new file mode 100644 index 0000000..0add5d1 --- /dev/null +++ b/app/code/MindArc/GeoIP/etc/module.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/code/MindArc/GeoIP/registration.php b/app/code/MindArc/GeoIP/registration.php new file mode 100644 index 0000000..1dedeb8 --- /dev/null +++ b/app/code/MindArc/GeoIP/registration.php @@ -0,0 +1,6 @@ + + + + + + + + + diff --git a/app/code/MindArc/GeoIP/view/frontend/templates/catalog/product/view/countrycode.phtml b/app/code/MindArc/GeoIP/view/frontend/templates/catalog/product/view/countrycode.phtml new file mode 100644 index 0000000..951d39c --- /dev/null +++ b/app/code/MindArc/GeoIP/view/frontend/templates/catalog/product/view/countrycode.phtml @@ -0,0 +1,5 @@ +getChildHtml('countrycode'); + $countryCode = $block->getCountryISO(); + echo $countryCode; +?> diff --git a/app/code/MindArc/README.md b/app/code/MindArc/README.md new file mode 100644 index 0000000..d6ecf63 --- /dev/null +++ b/app/code/MindArc/README.md @@ -0,0 +1,10 @@ +For working of GeoIP module the file 'database.tar.gz' must be unzip under var/ folder of the Magento project +and install GeoIP2 PHP API run in your project root: + + php composer.phar require geoip2/geoip2:~2.0 + + +For testing the functionality, you can simulate an IP in Store->Configuration->general->GeoIP. + 1. Enable simulation mode + 2. Set an IP. For instance: US IP, 72.229.28.185 + 3. Clear config cache \ No newline at end of file diff --git a/app/code/MindArc/database.tar.gz b/app/code/MindArc/database.tar.gz new file mode 100644 index 0000000..a60fa4b Binary files /dev/null and b/app/code/MindArc/database.tar.gz differ