Skip to content

Commit 61630eb

Browse files
author
Jerry Bramstång
committed
Rewrote module to use the MaxMind GeoLite2 database
1 parent 07e710c commit 61630eb

File tree

17 files changed

+662
-30
lines changed

17 files changed

+662
-30
lines changed

Geoip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/var/www/modules/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+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
use GeoIp2\Database\Reader;
4+
5+
/**
6+
* Webbhsuet Geoip Observer
7+
*
8+
* @category Webbhuset
9+
* @package Webbhuset_Geoip
10+
* @author Webbhuset <[email protected]>
11+
*/
12+
class Webbhuset_Geoip_Model_Country
13+
extends Webbhuset_Geoip_Model_Abstract
14+
{
15+
protected $country, $reader;
16+
protected $allowedCountries = [];
17+
18+
public function __construct()
19+
{
20+
parent::__construct();
21+
22+
$this->_initReader();
23+
24+
if (!$this->record && $this->reader) {
25+
$this->record = $this->reader->country(Mage::helper('core/http')->getRemoteAddr());
26+
$allowCountries = explode(',', (string)Mage::getStoreConfig('general/country/allow'));
27+
$this->addAllowedCountry($allowCountries);
28+
}
29+
}
30+
31+
protected function _initReader() {
32+
try {
33+
$this->reader = new Reader($this->localFile);
34+
} catch (Exception $e) {
35+
Mage::logException($e);
36+
}
37+
}
38+
39+
/**
40+
* Fetches a new country record from reader by IP and returns the iso code
41+
*
42+
* @param string $ip
43+
*
44+
* @return boolean
45+
*/
46+
public function getCountryByIp($ip)
47+
{
48+
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
49+
return false;
50+
}
51+
52+
if (!$this->reader) {
53+
return false;
54+
}
55+
56+
try {
57+
if ($this->record
58+
&& $this->record->traits
59+
&& $this->record->traits->ipAddress != $ip
60+
) {
61+
$this->record = $this->reader->country($ip);
62+
} else if (!$this->record) {
63+
$this->record = $this->reader->country($ip);
64+
}
65+
66+
} catch (Exception $e) {
67+
Mage::logException($e);
68+
return false;
69+
}
70+
71+
return $this->getCountry();
72+
}
73+
74+
/**
75+
* Returns the curent records isoCode
76+
*
77+
* @return string
78+
*/
79+
public function getCountry()
80+
{
81+
if (!$this->record) {
82+
return false;
83+
}
84+
85+
if (!$this->record->country) {
86+
return false;
87+
}
88+
89+
return $this->record->country->isoCode;
90+
}
91+
92+
/**
93+
* Checks if current or supplied country is allowed in current store
94+
*
95+
* @param string $country
96+
*
97+
* @return boolean
98+
*/
99+
public function isCountryAllowed($country = null)
100+
{
101+
$country = $country ?? $this->getCountry();
102+
103+
if (count($this->allowedCountries) && $country) {
104+
return in_array($country, $this->allowedCountries);
105+
}
106+
107+
return false;
108+
}
109+
110+
/**
111+
* Adds a list of countries to the list of allowed countries
112+
*
113+
* @param array $countries
114+
*
115+
* @return boolean
116+
*/
117+
public function addAllowedCountry($countries)
118+
{
119+
if (!is_array($countries)) {
120+
$countries = [$countries];
121+
}
122+
123+
$this->allowedCountries = array_merge($this->allowedCountries, $countries);
124+
return $this;
125+
}
126+
127+
/**
128+
* Adds a list of countries to the list of allowed countries
129+
*
130+
*
131+
* @return array $countries
132+
*/
133+
public function getAllowedCountries()
134+
{
135+
return $this->allowedCountries;
136+
}
137+
}

0 commit comments

Comments
 (0)