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
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Webbhuset GeoIP

## Installation
This module depends on the geoip library by [Sandfox][1] located on GitHub, and of course the Magento hackaton installer.
This module uses the MaxMind GeoLite 2 country database through their API [GeoIP2 PHP API](https://github.com/maxmind/GeoIP2-php) located on GitHub, and of course the Magento hackaton installer.

Add the module to your composer file

Expand All @@ -12,7 +12,7 @@ Add the module to your composer file
"repositories": [
{
"type": "vcs",
"url": "https://github.com/magento-hackathon/magento-composer-installer"
"url": "https://github.com/Cotya/magento-composer-installer"
},
{
"type":"vcs",
Expand All @@ -25,11 +25,22 @@ Then simply update composer by running the update command.
composer update

## Getting started
First you need to sync the geoip library by going to **System > Configuration > General > GeoIP Database Downloaded**, and then click the "**Synchronize**" button.

Then under **System > Configuration > Webbhuset > Geoip > General** you have two settings. The first one is simply to enable geoip redirect. The second one is for locking your country location to the store selected by the geoip module. If that is turned on you will not be able to switch to a different store. You will always be redirected. If it is turned off, you will only be redirected the first time you enter the site (once per session). After that you can switch to whichever store you want.

### Configure redirect
First you need to sync the geoip library by going to **System > Configuration > Webbhuset > GeoIP > General**, and then click the "**Synchronize**" button which downloads the country database to var/geoip/.

Then under **System > Configuration > Webbhuset > Geoip > General** you have four settings:

### Enable Geoip Redirect
Enable/Disable redirect of customer to the first store which the country is allowed in.
If no match is found see Fallback store below.
### Lock user to store
Allows you to lock customers to the store matching their country. With this enabled they will not be able to switch to a different store
and will always be redirected. If it is turned off, you will only be redirected the first time you enter the site (once per session) but is still able to switch store manually.
### Fallback store for not allowed countries
If the customers country does not match any stores allowed countries the customer will be redirected to this store.
### Don't redirect
The controllers/Actions that we don't want to redirect on.

## Configure redirect
The module looks at the *"Default country"* and *"Allowed Countries"* settings under **System > Configuration > General**. You need to set which countries are allowed on each store. Default Country has priority, so if your country is the *Default country* on some store, that store will be selected. If no default country was matched, it looks at *Allowed countries* and selects the first store you are allowed in and redirects to it.

[1]: https://github.com/tim-bezhashvyly/Sandfox_GeoIP
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class Webbhuset_Geoip_Block_Adminhtml_Notifications
extends Mage_Adminhtml_Block_Template
{
public function checkFilePermissions()
{
$database = Mage::getModel('webbhusetgeoip/database');

return $database->checkFilePermissions();
}
}
46 changes: 46 additions & 0 deletions app/code/community/Webbhuset/Geoip/Block/System/Config/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Webbhuset GeoIP Info Block
*
* @category Webbhuset
* @package Webbhuset_Geoip
* @author Webbhuset <[email protected]>
*/
class Webbhuset_Geoip_Block_System_Config_Info
extends Mage_Adminhtml_Block_System_Config_Form_Field
{
/**
* Remove scope label
*
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
public function render(Varien_Data_Form_Element_Abstract $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();

return parent::render($element);
}

protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$ip = Mage::helper('webbhusetgeoip')->getIp();
$geoIP = Mage::getSingleton('webbhusetgeoip/country');
$currentCountry = $geoIP->getCountry();

if (!$currentCountry) {
$currentCountry = "no match for IP";
} else {
$country = Mage::getModel('directory/country')->loadByCode($currentCountry);
$countryName = $country->getName();
$currentCountry = "{$countryName} ({$currentCountry})";
}

$html = "<table><tr><td><td><td></tr>";
$html .="<tr><td>Country:<td><td>{$currentCountry}<td></tr>";
$html .="<tr><td>Current IP:<td><td>{$ip}<td></tr>";
$html .= "</table>";

return $html;
}
}
37 changes: 37 additions & 0 deletions app/code/community/Webbhuset/Geoip/Block/System/Config/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Webbhuset GeoIP Status Block
*
* @category Webbhuset
* @package Webbhuset_Geoip
* @author Webbhuset <[email protected]>
*/
class Webbhuset_Geoip_Block_System_Config_Status
extends Mage_Adminhtml_Block_System_Config_Form_Field
{
/**
* Remove scope label
*
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
public function render(Varien_Data_Form_Element_Abstract $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();

return parent::render($element);
}

protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$database = Mage::getModel('webbhusetgeoip/database');
if ($date = $database->getDatFileDownloadDate()) {
$format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
$date = Mage::app()->getLocale()->date(intval($date))->toString($format);
} else {
$date = '-';
}

return '<div id="sync_update_date">' . $date . '</div>';
}
}
115 changes: 115 additions & 0 deletions app/code/community/Webbhuset/Geoip/Block/System/Config/Synchronize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Webbhuset GeoIP Syncronize Block
*
* @category Webbhuset
* @package Webbhuset_Geoip
* @author Webbhuset <[email protected]>
*/
class Webbhuset_Geoip_Block_System_Config_Synchronize extends Mage_Adminhtml_Block_System_Config_Form_Field
{
/*
* Set template
*/
protected function _construct()
{
parent::_construct();
$this->setTemplate('webbhuset/geoip/system/config/synchronize.phtml');
}

/**
* Remove scope label
*
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
public function render(Varien_Data_Form_Element_Abstract $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();

return parent::render($element);
}

/**
* Return element html
*
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
return $this->_toHtml();
}

/**
* Return ajax url for synchronize button
*
* @return string
*/
public function getAjaxSyncUrl()
{
return Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/whgeoip/synchronize');
}

/**
* Return ajax url for synchronize button
*
* @return string
*/
public function getAjaxStatusUpdateUrl()
{
return Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/whgeoip/status');
}

/**
* Generate synchronize button html
*
* @return string
*/
public function getButtonHtml()
{
/** @var $button Mage_Adminhtml_Block_Widget_Button */
$button = $this->getLayout()->createBlock('adminhtml/widget_button');
$button->setData([
'id' => 'synchronize_button',
'label' => $this->helper('adminhtml')->__('Synchronize'),
'onclick' => 'javascript:synchronize(); return false;'
]);

return $button->toHtml();
}

/**
* Retrieve last sync params settings
*
* Return array format:
* array (
* => storage_type int,
* => connection_name string
* )
*
* @return array
*/
public function getSyncStorageParams()
{
$flag = Mage::getSingleton('core/file_storage')->getSyncFlag();
$flagData = $flag->getFlagData();

if ($flag->getState() == Mage_Core_Model_File_Storage_Flag::STATE_NOTIFIED
&& is_array($flagData)
&& isset($flagData['destination_storage_type']) && $flagData['destination_storage_type'] != ''
&& isset($flagData['destination_connection_name'])
) {
$storageType = $flagData['destination_storage_type'];
$connectionName = $flagData['destination_connection_name'];
} else {
$storageType = Mage_Core_Model_File_Storage::STORAGE_MEDIA_FILE_SYSTEM;
$connectionName = '';
}

return [
'storage_type' => $storageType,
'connection_name' => $connectionName
];
}
}
75 changes: 75 additions & 0 deletions app/code/community/Webbhuset/Geoip/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,79 @@ public function isCountryAllowed($country, $store)

return false;
}
/**
* Get size of remote file
*
* @param $file
* @return mixed
*/
public function getSize($file)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);

return curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
}

/**
* Extracts .mmdb database file from .tar.gz archive
*
* @param $archive
* @param $destination
* @return int
*/
public function extract($archive, $destination, $files)
{
if (!is_array($files)) {
$files = [$files];
}

try {
$archive = new PharData($archive);

foreach (new RecursiveIteratorIterator($archive) as $file) {
if (!in_array($file->getFileName(), $files)) {
continue;
}

$path = basename($archive->current()->getPathName());
$fileName = $file->getFileName();
$fullPath = "{$destination}/{$path}";

if ($archive->extractTo($destination, "{$path}/{$fileName}", true)) {
rename("{$fullPath}/$fileName", "{$destination}/{$fileName}");
rmdir($fullPath);

return filesize("{$destination}/{$fileName}");
}
}

} catch (Exception $e) {
Mage::logException($e);
}

return 0;
}

/**
* Returns remote IP or if configured replaces it with configured mock IP
*
* @return string
*/ public function getIp()
{
$remoteIp = Mage::helper('core/http')->getRemoteAddr();
$localIp = Mage::getStoreConfig('webbhusetgeoip/debug/local_ip');
$devIp = Mage::getStoreConfig('webbhusetgeoip/debug/mock_ip');
$debug = Mage::getStoreConfig('webbhusetgeoip/debug/enabled');

if ($localIp == $remoteIp && $devIp && $debug) {
return $devIp;
}

return $remoteIp;
}
}
15 changes: 15 additions & 0 deletions app/code/community/Webbhuset/Geoip/Model/Abstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
class Webbhuset_GeoIP_Model_Abstract
{
protected $localDir, $localFile, $record, $localArchive, $remoteArchive;

public function __construct()
{
$this->localDir = 'geoip';
$this->localFile = Mage::getBaseDir('var') . '/' . $this->localDir . '/GeoLite2-Country.mmdb';

$this->localArchive = Mage::getBaseDir('var') . '/' . $this->localDir . '/GeoLite2-Country.tar.gz';
$this->remoteArchive = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz';
}

}
Loading