Skip to content

Commit f90d88c

Browse files
author
Jerry Bramstång
committed
Rewrote module to use the MaxMind GeoLite2 database
1 parent 564b8b4 commit f90d88c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3961
-52
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Webbhuset GeoIP
22

33
## Installation
4-
This module depends on the geoip library by [Sandfox][1] located on GitHub, and of course the Magento hackaton installer.
4+
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.
55

66
Add the module to your composer file
77

@@ -12,7 +12,7 @@ Add the module to your composer file
1212
"repositories": [
1313
{
1414
"type": "vcs",
15-
"url": "https://github.com/magento-hackathon/magento-composer-installer"
15+
"url": "https://github.com/Cotya/magento-composer-installer"
1616
},
1717
{
1818
"type":"vcs",
@@ -25,11 +25,22 @@ Then simply update composer by running the update command.
2525
composer update
2626

2727
## Getting started
28-
First you need to sync the geoip library by going to **System > Configuration > General > GeoIP Database Downloaded**, and then click the "**Synchronize**" button.
29-
30-
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.
31-
32-
### Configure redirect
28+
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/.
29+
30+
Then under **System > Configuration > Webbhuset > Geoip > General** you have four settings:
31+
32+
### Enable Geoip Redirect
33+
Enable/Disable redirect of customer to the first store which the country is allowed in.
34+
If no match is found see Fallback store below.
35+
### Lock user to store
36+
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
37+
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.
38+
### Fallback store for not allowed countries
39+
If the customers country does not match any stores allowed countries the customer will be redirected to this store.
40+
### Don't redirect
41+
The controllers/Actions that we don't want to redirect on.
42+
43+
## Configure redirect
3344
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.
3445

3546
[1]: https://github.com/tim-bezhashvyly/Sandfox_GeoIP
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
class Webbhuse_Geoip_Block_Adminhtml_Notifications
4+
extends Mage_Adminhtml_Block_Template
5+
{
6+
public function checkFilePermissions()
7+
{
8+
$database = Mage::getModel('webbhusetgeoip/database');
9+
10+
return $database->checkFilePermissions();
11+
}
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Webbhuset GeoIP Status Block
4+
*
5+
* @category Webbhuset
6+
* @package Webbhuset_Geoip
7+
* @author Webbhuset <[email protected]>
8+
*/
9+
class Webbhuset_Geoip_Block_System_Config_Status
10+
extends Mage_Adminhtml_Block_System_Config_Form_Field
11+
{
12+
/**
13+
* Remove scope label
14+
*
15+
* @param Varien_Data_Form_Element_Abstract $element
16+
* @return string
17+
*/
18+
public function render(Varien_Data_Form_Element_Abstract $element)
19+
{
20+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
21+
22+
return parent::render($element);
23+
}
24+
25+
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
26+
{
27+
$database = Mage::getModel('webbhusetgeoip/database');
28+
if ($date = $database->getDatFileDownloadDate()) {
29+
$format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
30+
$date = Mage::app()->getLocale()->date(intval($date))->toString($format);
31+
} else {
32+
$date = '-';
33+
}
34+
35+
return '<div id="sync_update_date">' . $date . '</div>';
36+
}
37+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Webbhuset GeoIP Syncronize Block
4+
*
5+
* @category Webbhuset
6+
* @package Webbhuset_Geoip
7+
* @author Webbhuset <[email protected]>
8+
*/
9+
class Webbhuset_Geoip_Block_System_Config_Synchronize extends Mage_Adminhtml_Block_System_Config_Form_Field
10+
{
11+
/*
12+
* Set template
13+
*/
14+
protected function _construct()
15+
{
16+
parent::_construct();
17+
$this->setTemplate('webbhuset/geoip/system/config/synchronize.phtml');
18+
}
19+
20+
/**
21+
* Remove scope label
22+
*
23+
* @param Varien_Data_Form_Element_Abstract $element
24+
* @return string
25+
*/
26+
public function render(Varien_Data_Form_Element_Abstract $element)
27+
{
28+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
29+
30+
return parent::render($element);
31+
}
32+
33+
/**
34+
* Return element html
35+
*
36+
* @param Varien_Data_Form_Element_Abstract $element
37+
* @return string
38+
*/
39+
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
40+
{
41+
return $this->_toHtml();
42+
}
43+
44+
/**
45+
* Return ajax url for synchronize button
46+
*
47+
* @return string
48+
*/
49+
public function getAjaxSyncUrl()
50+
{
51+
return Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/whgeoip/synchronize');
52+
}
53+
54+
/**
55+
* Return ajax url for synchronize button
56+
*
57+
* @return string
58+
*/
59+
public function getAjaxStatusUpdateUrl()
60+
{
61+
return Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/whgeoip/status');
62+
}
63+
64+
/**
65+
* Generate synchronize button html
66+
*
67+
* @return string
68+
*/
69+
public function getButtonHtml()
70+
{
71+
/** @var $button Mage_Adminhtml_Block_Widget_Button */
72+
$button = $this->getLayout()->createBlock('adminhtml/widget_button');
73+
$button->setData([
74+
'id' => 'synchronize_button',
75+
'label' => $this->helper('adminhtml')->__('Synchronize'),
76+
'onclick' => 'javascript:synchronize(); return false;'
77+
]);
78+
79+
return $button->toHtml();
80+
}
81+
82+
/**
83+
* Retrieve last sync params settings
84+
*
85+
* Return array format:
86+
* array (
87+
* => storage_type int,
88+
* => connection_name string
89+
* )
90+
*
91+
* @return array
92+
*/
93+
public function getSyncStorageParams()
94+
{
95+
$flag = Mage::getSingleton('core/file_storage')->getSyncFlag();
96+
$flagData = $flag->getFlagData();
97+
98+
if ($flag->getState() == Mage_Core_Model_File_Storage_Flag::STATE_NOTIFIED
99+
&& is_array($flagData)
100+
&& isset($flagData['destination_storage_type']) && $flagData['destination_storage_type'] != ''
101+
&& isset($flagData['destination_connection_name'])
102+
) {
103+
$storageType = $flagData['destination_storage_type'];
104+
$connectionName = $flagData['destination_connection_name'];
105+
} else {
106+
$storageType = Mage_Core_Model_File_Storage::STORAGE_MEDIA_FILE_SYSTEM;
107+
$connectionName = '';
108+
}
109+
110+
return [
111+
'storage_type' => $storageType,
112+
'connection_name' => $connectionName
113+
];
114+
}
115+
}

app/code/community/Webbhuset/Geoip/Helper/Data.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,61 @@ public function isCountryAllowed($country, $store)
3131

3232
return false;
3333
}
34+
/**
35+
* Get size of remote file
36+
*
37+
* @param $file
38+
* @return mixed
39+
*/
40+
public function getSize($file)
41+
{
42+
$ch = curl_init();
43+
curl_setopt($ch, CURLOPT_URL, $file);
44+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
45+
curl_setopt($ch, CURLOPT_HEADER, true);
46+
curl_setopt($ch, CURLOPT_NOBODY, true);
47+
curl_exec($ch);
48+
49+
return curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
50+
}
51+
52+
/**
53+
* Extracts .mmdb database file from .tar.gz archive
54+
*
55+
* @param $archive
56+
* @param $destination
57+
* @return int
58+
*/
59+
public function extract($archive, $destination, $files)
60+
{
61+
if (!is_array($files)) {
62+
$files = [$files];
63+
}
64+
65+
try {
66+
$archive = new PharData($archive);
67+
68+
foreach (new RecursiveIteratorIterator($archive) as $file) {
69+
if (!in_array($file->getFileName(), $files)) {
70+
continue;
71+
}
72+
73+
$path = basename($archive->current()->getPathName());
74+
$fileName = $file->getFileName();
75+
$fullPath = "{$destination}/{$path}";
76+
77+
if ($archive->extractTo($destination, "{$path}/{$fileName}", true)) {
78+
rename("{$fullPath}/$fileName", "{$destination}/{$fileName}");
79+
rmdir($fullPath);
80+
81+
return filesize("{$destination}/{$fileName}");
82+
}
83+
}
84+
85+
} catch (Exception $e) {
86+
Mage::logException($e);
87+
}
88+
89+
return 0;
90+
}
3491
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
class Webbhuset_GeoIP_Model_Abstract
3+
{
4+
protected $localDir, $localFile, $record, $localArchive, $remoteArchive;
5+
6+
public function __construct()
7+
{
8+
$this->localDir = 'geoip';
9+
$this->localFile = Mage::getBaseDir('var') . '/' . $this->localDir . '/GeoLite2-Country.mmdb';
10+
11+
$this->localArchive = Mage::getBaseDir('var') . '/' . $this->localDir . '/GeoLite2-Country.tar.gz';
12+
$this->remoteArchive = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz';
13+
}
14+
15+
}

0 commit comments

Comments
 (0)