Juicy_GeoIP - Version 1.2.0

Version Notes

Added Crawler and Bot detection for SEO purposes. This will disable the module if a crawler is detected.
Added multiple testing options, including Admin only, Specific IP and Everyone.
Added localhost bypass. Localhost servers are not able to run the GeoIP module correctly, but this will allow you to try.

Download this release

Release Info

Developer Juicy Media
Extension Juicy_GeoIP
Version 1.2.0
Comparing to
See all releases


Code changes from version 1.1.0 to 1.2.0

app/code/local/Juicy/Geoip/Helper/Data.php CHANGED
@@ -3,8 +3,79 @@
3
  class Juicy_Geoip_Helper_Data extends Mage_Core_Helper_Abstract {
4
  public function isPrivateIp()
5
  {
 
 
 
6
  return !filter_var(Mage::helper('core/http')->getRemoteAddr(), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
7
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  public function getConfig($key)
9
  {
10
  return Mage::getStoreConfig('geoip/'.$key);
@@ -13,7 +84,7 @@ class Juicy_Geoip_Helper_Data extends Mage_Core_Helper_Abstract {
13
  {
14
  return Mage::getStoreConfig('geoip/general/status');
15
  }
16
- public function isTestMode()
17
  {
18
  return Mage::getStoreConfig('geoip/general/testing');
19
  }
@@ -26,7 +97,8 @@ class Juicy_Geoip_Helper_Data extends Mage_Core_Helper_Abstract {
26
  return Mage::getStoreConfig('geoip/general/switch_'.$key);
27
  }
28
 
29
- public function getCountryList(){
 
30
  return array (
31
  'AF' => 'Afghanistan',
32
  'AL' => 'Albania',
3
  class Juicy_Geoip_Helper_Data extends Mage_Core_Helper_Abstract {
4
  public function isPrivateIp()
5
  {
6
+ if($this->getConfig('general/private_bypass')){
7
+ return false;
8
+ }
9
  return !filter_var(Mage::helper('core/http')->getRemoteAddr(), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
10
  }
11
+ public function isCrawler()
12
+ {
13
+ if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])){
14
+ return true;
15
+ }
16
+ return false;
17
+ }
18
+ public function enableTestMode()
19
+ {
20
+ $type = $this->getTestingType();
21
+ switch ($type){
22
+ case 0:
23
+ $ret = false;
24
+ break;
25
+ case 1:
26
+ $ret = $this->adminActive();
27
+ break;
28
+ case 2:
29
+ $ret = $this->adminIpMatch();
30
+ break;
31
+ case 3:
32
+ $ret = true;
33
+ break;
34
+ default:
35
+ $ret = false;
36
+ break;
37
+ }
38
+ return $ret;
39
+ }
40
+ public function adminActive()
41
+ {
42
+ if(array_key_exists('adminhtml', $_COOKIE)){
43
+ $sessionFilePath = Mage::getBaseDir('session').DS.'sess_'.$_COOKIE['adminhtml'];
44
+ $sessionFile = file_get_contents($sessionFilePath);
45
+ $oldSession = $_SESSION;
46
+ session_decode($sessionFile);
47
+ $adminSessionData = $_SESSION;
48
+ $_SESSION = $oldSession;
49
+
50
+ } if(array_key_exists('user', $adminSessionData['admin'])){
51
+ $adminUserObj = $adminSessionData['admin']['user'];
52
+ }
53
+ if(isset($adminUserObj)){
54
+ return $adminUserObj->getId() && $adminUserObj->getIsActive();
55
+ }
56
+ return false;
57
+ }
58
+ public function adminIpMatch()
59
+ {
60
+ $ipList = str_replace(" ", "", $this->getConfig('general/ip_whitelist'));
61
+ $ipArr = explode(",", $ipList);
62
+ $this->switchRemoteHeaders();
63
+ $remoteAddr = Mage::helper('core/http')->getRemoteAddr();
64
+ if(in_array($remoteAddr, $ipArr)){
65
+ return true;
66
+ }
67
+ return false;
68
+ }
69
+ public function switchRemoteHeaders()
70
+ {
71
+ if($this->getConfig('general/varnish_enabled')){
72
+ $headers = str_replace(" ", "", $this->getConfig('general/varnish_headers'));
73
+ $headerArr = explode(",", $headers);
74
+ for($i=0; $i<count($headerArr);$i++){
75
+ Mage::getConfig()->setNode('global/remote_addr_headers/header'.$i+1, $headerArr[$i]);
76
+ }
77
+ }
78
+ }
79
  public function getConfig($key)
80
  {
81
  return Mage::getStoreConfig('geoip/'.$key);
84
  {
85
  return Mage::getStoreConfig('geoip/general/status');
86
  }
87
+ public function getTestingType()
88
  {
89
  return Mage::getStoreConfig('geoip/general/testing');
90
  }
97
  return Mage::getStoreConfig('geoip/general/switch_'.$key);
98
  }
99
 
100
+ public function getCountryList()
101
+ {
102
  return array (
103
  'AF' => 'Afghanistan',
104
  'AL' => 'Albania',
app/code/local/Juicy/Geoip/Model/Adminhtml/Source/TestingMode.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Juicy_Geoip_Model_Adminhtml_Source_TestingMode
4
+ {
5
+ public function toOptionArray()
6
+ {
7
+ $array = array(
8
+ array('value' => '0', 'label' => Mage::helper('barclayspspid')->__('No one (Disabled)')),
9
+ array('value' => '1', 'label' => Mage::helper('barclayspspid')->__('Current Administrators')),
10
+ array('value' => '2', 'label' => Mage::helper('barclayspspid')->__('Specific IP Addresses')),
11
+ array('value' => '3', 'label' => Mage::helper('barclayspspid')->__('Everyone')),
12
+ );
13
+ return $array;
14
+ }
15
+ }
app/code/local/Juicy/Geoip/Model/Geoip.php CHANGED
@@ -45,10 +45,10 @@ class Juicy_Geoip_Model_Geoip
45
  }
46
  protected function _getCountryCode()
47
  {
48
- if(Mage::helper('geoip')->isTestMode()){
49
  $overrideCountry = Mage::helper('geoip')->testOverrideCountry();
50
  if(!empty($overrideCountry)){
51
- return Mage::helper('geoip')->testOverrideCountry();
52
  }
53
  }
54
  return $this->_getCountryCodeFromIp($this->_getIp());
@@ -60,6 +60,7 @@ class Juicy_Geoip_Model_Geoip
60
 
61
  protected function _getIp(){
62
  //Using Mage HTTP helper because Varnish can confuse PHP method
 
63
  return Mage::helper('core/http')->getRemoteAddr();
64
  }
65
 
45
  }
46
  protected function _getCountryCode()
47
  {
48
+ if(Mage::helper('geoip')->enableTestMode()){
49
  $overrideCountry = Mage::helper('geoip')->testOverrideCountry();
50
  if(!empty($overrideCountry)){
51
+ return $overrideCountry;
52
  }
53
  }
54
  return $this->_getCountryCodeFromIp($this->_getIp());
60
 
61
  protected function _getIp(){
62
  //Using Mage HTTP helper because Varnish can confuse PHP method
63
+ Mage::helper('geoip')->switchRemoteHeaders();
64
  return Mage::helper('core/http')->getRemoteAddr();
65
  }
66
 
app/code/local/Juicy/Geoip/Model/Observer.php CHANGED
@@ -4,18 +4,16 @@ class Juicy_Geoip_Model_Observer
4
  {
5
  public function controllerActionPredispatch($e)
6
  {
7
- if(Mage::helper("geoip")->isModuleEnabled() == 1 && !Mage::helper("geoip")->isPrivateIp()){
8
- if(Mage::helper("geoip")->isTestMode() == 1){
9
  Mage::getModel('core/session')->unsGeoipChecked();
10
  }
11
  $session = Mage::getModel('core/session')->getGeoipChecked();
12
- if(!isset($session) || $session == false){
13
-
14
- $redirStore = Mage::getModel('geoip/geoip')->runGeoip();
15
  if($redirStore){
16
  $e->getControllerAction()->getResponse()->setRedirect($redirStore);
17
  }
18
- Mage::getSingleton("core/session")->setGeoipChecked(true);
19
  }
20
  }
21
  }
4
  {
5
  public function controllerActionPredispatch($e)
6
  {
7
+ if(Mage::helper("geoip")->isModuleEnabled() == 1 && !Mage::helper("geoip")->isPrivateIp() && !Mage::helper("geoip")->isCrawler()){
8
+ if(Mage::helper("geoip")->enableTestMode()){
9
  Mage::getModel('core/session')->unsGeoipChecked();
10
  }
11
  $session = Mage::getModel('core/session')->getGeoipChecked();
12
+ if(!isset($session) || $session == false){
13
+ $redirStore = Mage::getModel('geoip/geoip')->runGeoip();
 
14
  if($redirStore){
15
  $e->getControllerAction()->getResponse()->setRedirect($redirStore);
16
  }
 
17
  }
18
  }
19
  }
app/code/local/Juicy/Geoip/etc/config.xml CHANGED
@@ -45,10 +45,12 @@
45
  <switch_currency>0</switch_currency>
46
  <switch_store>0</switch_store>
47
  <apache_or_file>0</apache_or_file>
48
- <file_location>geoip.dat</file_location>
 
 
 
49
  </general>
50
- <geoipset>
51
-
52
  </geoipset>
53
  </geoip>
54
  </default>
45
  <switch_currency>0</switch_currency>
46
  <switch_store>0</switch_store>
47
  <apache_or_file>0</apache_or_file>
48
+ <file_location>geoip.dat</file_location>
49
+ <varnish_enabled>0</varnish_enabled>
50
+ <varnish_headers><![CDATA[HTTP_X_REAL_IP, HTTP_X_FORWARDED_FOR, REMOTE_ADDR]]></varnish_headers>
51
+ <private_bypass>0</private_bypass>
52
  </general>
53
+ <geoipset>
 
54
  </geoipset>
55
  </geoip>
56
  </default>
app/code/local/Juicy/Geoip/etc/system.xml CHANGED
@@ -41,16 +41,26 @@ and open the template in the editor.
41
  <show_in_store>0</show_in_store>
42
  </status>
43
  <testing translate="label">
44
- <label>GeoIP Testing Mode</label>
45
  <frontend_type>select</frontend_type>
46
  <comment>Enable/Disable testing mode. This will disable session storage for the users location</comment>
47
- <source_model>adminhtml/system_config_source_enabledisable</source_model>
48
  <sort_order>25</sort_order>
49
  <show_in_default>1</show_in_default>
50
  <show_in_website>1</show_in_website>
51
  <show_in_store>0</show_in_store>
52
  <depends><status>1</status></depends>
53
  </testing>
 
 
 
 
 
 
 
 
 
 
54
  <country_override translate="label">
55
  <label>Country Override</label>
56
  <frontend_type>text</frontend_type>
@@ -59,7 +69,7 @@ and open the template in the editor.
59
  <show_in_default>1</show_in_default>
60
  <show_in_website>1</show_in_website>
61
  <show_in_store>0</show_in_store>
62
- <depends><testing>1</testing></depends>
63
  </country_override>
64
  <switch_currency translate="label">
65
  <label>Allow Currency Switching</label>
@@ -93,11 +103,11 @@ and open the template in the editor.
93
  <depends><status>1</status></depends>
94
  </switch_store>
95
  <apache_or_file translate="label">
96
- <label>Use my own GeoIP file</label>
97
  <frontend_type>select</frontend_type>
98
  <source_model>adminhtml/system_config_source_enabledisable</source_model>
99
  <sort_order>70</sort_order>
100
- <comment> <![CDATA[ <span>Enable if you do not have to use the Apache GeoIP module</span> ]]></comment>
101
  <show_in_default>1</show_in_default>
102
  <show_in_website>1</show_in_website>
103
  <show_in_store>0</show_in_store>
@@ -112,7 +122,39 @@ and open the template in the editor.
112
  <show_in_website>1</show_in_website>
113
  <show_in_store>0</show_in_store>
114
  <depends><apache_or_file>1</apache_or_file></depends>
115
- </file_location>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  </fields>
117
  </general>
118
  <geoipset translate="label" module="geoip">
41
  <show_in_store>0</show_in_store>
42
  </status>
43
  <testing translate="label">
44
+ <label>Enable Testing Mode for:</label>
45
  <frontend_type>select</frontend_type>
46
  <comment>Enable/Disable testing mode. This will disable session storage for the users location</comment>
47
+ <source_model>geoip/adminhtml_source_testingMode</source_model>
48
  <sort_order>25</sort_order>
49
  <show_in_default>1</show_in_default>
50
  <show_in_website>1</show_in_website>
51
  <show_in_store>0</show_in_store>
52
  <depends><status>1</status></depends>
53
  </testing>
54
+ <ip_whitelist translate="label">
55
+ <label>IP Whitelist</label>
56
+ <frontend_type>text</frontend_type>
57
+ <comment>List of comma seperated IP addresses that are immune from session storage</comment>
58
+ <sort_order>28</sort_order>
59
+ <show_in_default>1</show_in_default>
60
+ <show_in_website>1</show_in_website>
61
+ <show_in_store>0</show_in_store>
62
+ <depends><testing>2</testing></depends>
63
+ </ip_whitelist>
64
  <country_override translate="label">
65
  <label>Country Override</label>
66
  <frontend_type>text</frontend_type>
69
  <show_in_default>1</show_in_default>
70
  <show_in_website>1</show_in_website>
71
  <show_in_store>0</show_in_store>
72
+ <depends><testing separator=",">1,2,3</testing></depends>
73
  </country_override>
74
  <switch_currency translate="label">
75
  <label>Allow Currency Switching</label>
103
  <depends><status>1</status></depends>
104
  </switch_store>
105
  <apache_or_file translate="label">
106
+ <label>Use local GeoIP file</label>
107
  <frontend_type>select</frontend_type>
108
  <source_model>adminhtml/system_config_source_enabledisable</source_model>
109
  <sort_order>70</sort_order>
110
+ <comment> <![CDATA[ <span>Enable if you cannot use the Apache GeoIP module</span> ]]></comment>
111
  <show_in_default>1</show_in_default>
112
  <show_in_website>1</show_in_website>
113
  <show_in_store>0</show_in_store>
122
  <show_in_website>1</show_in_website>
123
  <show_in_store>0</show_in_store>
124
  <depends><apache_or_file>1</apache_or_file></depends>
125
+ </file_location>
126
+ <!--
127
+ <varnish_enabled translate="label">
128
+ <label>Enable Remote Header rewrite for IP detection (for Varnish/CDNs)</label>
129
+ <frontend_type>select</frontend_type>
130
+ <source_model>adminhtml/system_config_source_enabledisable</source_model>
131
+ <sort_order>90</sort_order>
132
+ <comment>Enabling this will rewrite the "remote_addr_headers" to allow you to use Varnish and CDN's</comment>
133
+ <show_in_default>1</show_in_default>
134
+ <show_in_website>1</show_in_website>
135
+ <show_in_store>0</show_in_store>
136
+ </varnish_enabled>
137
+ <varnish_headers translate="label">
138
+ <label>Server address headers</label>
139
+ <frontend_type>text</frontend_type>
140
+ <sort_order>95</sort_order>
141
+ <comment>Comma seperated list of server address headers in order or priority. <![CDATA[<strong style="color:red;">ADVANCED USERS ONLY - DO NOT EDIT</strong>]]></comment>
142
+ <show_in_default>1</show_in_default>
143
+ <show_in_website>1</show_in_website>
144
+ <show_in_store>0</show_in_store>
145
+ <depends><varnish_enabled>1</varnish_enabled></depends>
146
+ </varnish_headers>
147
+ -->
148
+ <private_bypass translate="label">
149
+ <label>Enable localhost bypass</label>
150
+ <frontend_type>select</frontend_type>
151
+ <source_model>adminhtml/system_config_source_enabledisable</source_model>
152
+ <sort_order>100</sort_order>
153
+ <comment>Extension does not work correctly on localhost. Enable this to bypass checks and attempt to run as normal anyway</comment>
154
+ <show_in_default>1</show_in_default>
155
+ <show_in_website>1</show_in_website>
156
+ <show_in_store>0</show_in_store>
157
+ </private_bypass>
158
  </fields>
159
  </general>
160
  <geoipset translate="label" module="geoip">
package.xml CHANGED
@@ -1,19 +1,20 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Juicy_GeoIP</name>
4
- <version>1.1.0</version>
5
  <stability>stable</stability>
6
  <license>GPL</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>Change currency using the visitors IP address and the PHP GeoIP module</summary>
10
  <description>Gets the customers current country using PHP's GeoIP module, filters through a list of countries and grabs the associated currency code for Magento</description>
11
- <notes>Removed dependency on the Apache GeoIP module. You can now include your own geoip.dat file.&#xD;
12
- Also added private IP detection</notes>
 
13
  <authors><author><name>Jonathan Webb</name><user>JuicyMedia</user><email>jonathan.webb@juicymedia.co.uk</email></author></authors>
14
- <date>2015-01-26</date>
15
- <time>12:22:31</time>
16
- <contents><target name="magelocal"><dir name="Juicy"><dir name="Geoip"><dir name="Block"><file name="Countrydefine.php" hash="0032d7797b0dc464fe0edc99d6a7fe7e"/></dir><dir name="Helper"><file name="Data.php" hash="d724ae9ca937562e91e8b45a052beff2"/></dir><dir name="Model"><file name="Geoip.php" hash="13e81cd752094981f43a3246014c8553"/><file name="Observer.php" hash="176a89109b18c4322ec109c465067e8a"/></dir><dir name="etc"><file name="adminhtml.xml" hash="5ac9ab6f0f5b9bb998fa7666dbe54803"/><file name="config.xml" hash="92cebe87d820324b0ce9d9add1a72498"/><file name="system.xml" hash="c68868a1dd0f57d7085f90dc0ed8d386"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Juicy_Geoip.xml" hash="c4ebc7b11208b6c6b6020cb581ba9b12"/></dir></target><target name="magelib"><dir name="Juicy"><dir name="Geoip"><dir name="Data"><file name="geoip.dat" hash="76476935f1cfea10145cf52d3798f801"/></dir><file name="geoip.inc" hash="09b0a2dd03361a2cb00ef5ec68b4edea"/></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="juicy"><dir name="geoip"><dir name="system"><dir name="config"><dir name="form"><dir name="field"><file name="array.phtml" hash="6335d9d4d1ce8a63793a1dede6061010"/></dir></dir></dir></dir></dir></dir></dir></dir></dir></dir></target></contents>
17
  <compatible/>
18
  <dependencies><required><php><min>5.0.0</min><max>6.0.0</max></php></required></dependencies>
19
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Juicy_GeoIP</name>
4
+ <version>1.2.0</version>
5
  <stability>stable</stability>
6
  <license>GPL</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>Change currency using the visitors IP address and the PHP GeoIP module</summary>
10
  <description>Gets the customers current country using PHP's GeoIP module, filters through a list of countries and grabs the associated currency code for Magento</description>
11
+ <notes>Added Crawler and Bot detection for SEO purposes. This will disable the module if a crawler is detected. &#xD;
12
+ Added multiple testing options, including Admin only, Specific IP and Everyone. &#xD;
13
+ Added localhost bypass. Localhost servers are not able to run the GeoIP module correctly, but this will allow you to try.</notes>
14
  <authors><author><name>Jonathan Webb</name><user>JuicyMedia</user><email>jonathan.webb@juicymedia.co.uk</email></author></authors>
15
+ <date>2015-04-21</date>
16
+ <time>11:27:15</time>
17
+ <contents><target name="magelocal"><dir name="Juicy"><dir name="Geoip"><dir name="Block"><file name="Countrydefine.php" hash="0032d7797b0dc464fe0edc99d6a7fe7e"/></dir><dir name="Helper"><file name="Data.php" hash="8546d1ddfd020b49eb1a5cdba2c1677c"/></dir><dir name="Model"><dir name="Adminhtml"><dir name="Source"><file name="TestingMode.php" hash="38e6931166fc53782940e80e1af2257b"/></dir></dir><file name="Geoip.php" hash="839209264d547ecce878609430efc3fd"/><file name="Observer.php" hash="8da0cfe6f653a32498deb1ce2be4c616"/></dir><dir name="etc"><file name="adminhtml.xml" hash="5ac9ab6f0f5b9bb998fa7666dbe54803"/><file name="config.xml" hash="d79f79cd2cb96d11c27d97e52be61916"/><file name="system.xml" hash="670761821f542c713b116dd8aa20bee5"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Juicy_Geoip.xml" hash="c4ebc7b11208b6c6b6020cb581ba9b12"/></dir></target><target name="magelib"><dir name="Juicy"><dir name="Geoip"><dir name="Data"><file name="geoip.dat" hash="76476935f1cfea10145cf52d3798f801"/></dir><file name="geoip.inc" hash="09b0a2dd03361a2cb00ef5ec68b4edea"/></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="juicy"><dir name="geoip"><dir name="system"><dir name="config"><dir name="form"><dir name="field"><file name="array.phtml" hash="6335d9d4d1ce8a63793a1dede6061010"/></dir></dir></dir></dir></dir></dir></dir></dir></dir></dir></target></contents>
18
  <compatible/>
19
  <dependencies><required><php><min>5.0.0</min><max>6.0.0</max></php></required></dependencies>
20
  </package>