Juicy_GeoIP - Version 1.1.0

Version Notes

Removed dependency on the Apache GeoIP module. You can now include your own geoip.dat file.
Also added private IP detection

Download this release

Release Info

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


Code changes from version 1.0.0 to 1.1.0

app/code/local/Juicy/Geoip/Model/Geoip.php CHANGED
@@ -1,12 +1,19 @@
1
  <?php
2
 
3
- define("GEOIP_STANDARD", 0);
 
 
 
 
4
 
5
  class Juicy_Geoip_Model_Geoip
6
- {
7
-
8
  public function runGeoip(){
 
9
  $countryCode = $this->_getCountryCode();
 
 
 
10
  $pairArr = $this->_getPairArray();
11
  foreach($pairArr as $searchArr){
12
  if(in_array($countryCode, $searchArr)){
@@ -38,9 +45,13 @@ class Juicy_Geoip_Model_Geoip
38
  }
39
  protected function _getCountryCode()
40
  {
41
- return Mage::helper('geoip')->isTestMode()
42
- ? Mage::helper('geoip')->testOverrideCountry()
43
- : $this->_getCountryCodeFromIp($this->_getIp());
 
 
 
 
44
  }
45
  protected function _getPairArray()
46
  {
@@ -51,23 +62,39 @@ class Juicy_Geoip_Model_Geoip
51
  //Using Mage HTTP helper because Varnish can confuse PHP method
52
  return Mage::helper('core/http')->getRemoteAddr();
53
  }
 
 
 
 
 
 
 
 
 
 
 
 
54
  protected function _getCountryCodeFromIp($ip){
55
- //If its 0, that means we're using Apache else we're using a file
56
- if(Mage::helper('geoip')->getConfig('general/apache_or_file') == 0){
57
- return geoip_country_code_by_name($ip);
58
- }else{
59
- /*
60
- try{
61
- $fileName = Mage::helper('geoip')->getConfig('general/file_location');
62
- *
63
- *
64
- return $country;
65
- }catch(Exception $e){
66
- Mage::throwException($e->getMessage());
 
 
67
  }
68
- */
69
- return geoip_country_code_by_name($ip);
70
- }
 
 
71
  }
72
  }
73
 
1
  <?php
2
 
3
+ if(!function_exists('geoip_country_code_by_name') && Mage::helper("geoip")->getConfig('general/apache_or_file') == 1){
4
+ define('GEOIP_LOCAL',1);
5
+ $geoIpInc = Mage::getBaseDir('lib').DS.'Juicy'.DS.'Geoip'.DS.'geoip.inc';
6
+ include($geoIpInc);
7
+ }
8
 
9
  class Juicy_Geoip_Model_Geoip
10
+ {
 
11
  public function runGeoip(){
12
+
13
  $countryCode = $this->_getCountryCode();
14
+ if(empty($countryCode)){
15
+ Mage::log("Country code returned empty. Please ensure you have at least one GeoIP method installed/enabled", null, "juicy_geoip.log");
16
+ }
17
  $pairArr = $this->_getPairArray();
18
  foreach($pairArr as $searchArr){
19
  if(in_array($countryCode, $searchArr)){
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());
55
  }
56
  protected function _getPairArray()
57
  {
62
  //Using Mage HTTP helper because Varnish can confuse PHP method
63
  return Mage::helper('core/http')->getRemoteAddr();
64
  }
65
+
66
+ //UNUSED
67
+ protected function _getModGeoIp(){
68
+ if (function_exists('apache_get_modules')) {
69
+ $modules = apache_get_modules();
70
+ $mod_geoip = in_array('mod_geoip', $modules);
71
+ } else {
72
+ $mod_geoip = getenv('HTTP_MOD_GEOIP')=='On' ? true : false ;
73
+ }
74
+ return $mod_geoip;
75
+ }
76
+
77
  protected function _getCountryCodeFromIp($ip){
78
+ //GeoIP .dat file
79
+ $file = Mage::getBaseDir().DS.'lib'.DS.'Juicy'.DS.'Geoip'.DS.'Data'.DS.Mage::helper("geoip")->getConfig('general/file_location');
80
+ try{
81
+ if(file_exists($file)){
82
+ if(defined('GEOIP_LOCAL')){
83
+ $gi=geoip_open($file,GEOIP_STANDARD);
84
+ $location = geoip_country_code_by_addr($gi, $ip);
85
+ geoip_close($gi);
86
+ return $location;
87
+ }else{
88
+ Mage::log(".dat file detected, but you haven't enabled the option to use it ('Use my own GeoIP file' in config)", null, "juicy_geoip.log");
89
+ }
90
+ }else{
91
+ return geoip_country_code_by_name($ip);
92
  }
93
+ }catch(Exception $e){
94
+ Mage::log("Warning: Could not find GeoIP Country Code. Please check your GeoIP data - Have you included a geoip.dat file?", null, "juicy_geoip.log");
95
+ Mage::log($e->getMessage(), null, "juicy_geoip.log");
96
+ return Mage::getStoreConfig('general/country/default');
97
+ }
98
  }
99
  }
100
 
app/code/local/Juicy/Geoip/etc/config.xml CHANGED
@@ -4,7 +4,7 @@
4
  <config>
5
  <modules>
6
  <Juicy_Geoip>
7
- <version>0.2.0</version>
8
  </Juicy_Geoip>
9
  </modules>
10
  <global>
@@ -45,7 +45,7 @@
45
  <switch_currency>0</switch_currency>
46
  <switch_store>0</switch_store>
47
  <apache_or_file>0</apache_or_file>
48
- <file_location>EXAMPLE: geoip.dat</file_location>
49
  </general>
50
  <geoipset>
51
 
4
  <config>
5
  <modules>
6
  <Juicy_Geoip>
7
+ <version>1.1.0</version>
8
  </Juicy_Geoip>
9
  </modules>
10
  <global>
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
 
app/code/local/Juicy/Geoip/etc/system.xml CHANGED
@@ -43,7 +43,7 @@ and open the template in the editor.
43
  <testing translate="label">
44
  <label>GeoIP Testing Mode</label>
45
  <frontend_type>select</frontend_type>
46
- <comment>Enable/Disable testing mode</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>
@@ -54,7 +54,7 @@ and open the template in the editor.
54
  <country_override translate="label">
55
  <label>Country Override</label>
56
  <frontend_type>text</frontend_type>
57
- <comment>ISO 3166 country code to mimic. Must be filled in for testing mode to work.</comment>
58
  <sort_order>30</sort_order>
59
  <show_in_default>1</show_in_default>
60
  <show_in_website>1</show_in_website>
@@ -92,13 +92,12 @@ and open the template in the editor.
92
  <show_in_store>0</show_in_store>
93
  <depends><status>1</status></depends>
94
  </switch_store>
95
- <!--
96
  <apache_or_file translate="label">
97
  <label>Use my own GeoIP file</label>
98
  <frontend_type>select</frontend_type>
99
  <source_model>adminhtml/system_config_source_enabledisable</source_model>
100
  <sort_order>70</sort_order>
101
- <comment> <![CDATA[ <span>Enable if you do not need/want to use the Apache GeoIP module <b style="color:red;">EXPERIMENTAL ONLY</b></span> ]]></comment>
102
  <show_in_default>1</show_in_default>
103
  <show_in_website>1</show_in_website>
104
  <show_in_store>0</show_in_store>
@@ -108,13 +107,12 @@ and open the template in the editor.
108
  <label>GeoIP data Override file name</label>
109
  <frontend_type>text</frontend_type>
110
  <sort_order>80</sort_order>
111
- <comment>Place .dat file in [Mage Dir]/var/juicy/geoip</comment>
112
  <show_in_default>1</show_in_default>
113
  <show_in_website>1</show_in_website>
114
  <show_in_store>0</show_in_store>
115
  <depends><apache_or_file>1</apache_or_file></depends>
116
  </file_location>
117
- -->
118
  </fields>
119
  </general>
120
  <geoipset translate="label" module="geoip">
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>
54
  <country_override translate="label">
55
  <label>Country Override</label>
56
  <frontend_type>text</frontend_type>
57
+ <comment>ISO 3166 country code to mimic. Can be left blank to disable session only</comment>
58
  <sort_order>30</sort_order>
59
  <show_in_default>1</show_in_default>
60
  <show_in_website>1</show_in_website>
92
  <show_in_store>0</show_in_store>
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>
107
  <label>GeoIP data Override file name</label>
108
  <frontend_type>text</frontend_type>
109
  <sort_order>80</sort_order>
110
+ <comment>Place .dat file in [Mage Dir]/lib/Juicy/Geoip/Data</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>
114
  <depends><apache_or_file>1</apache_or_file></depends>
115
  </file_location>
 
116
  </fields>
117
  </general>
118
  <geoipset translate="label" module="geoip">
app/design/adminhtml/default/default/template/juicy/geoip/system/config/form/field/array.phtml CHANGED
@@ -1,28 +1,9 @@
1
  <?php
2
- /**
3
- * Magento
4
- *
5
- * NOTICE OF LICENSE
6
- *
7
- * This source file is subject to the Academic Free License (AFL 3.0)
8
- * that is bundled with this package in the file LICENSE_AFL.txt.
9
- * It is also available through the world-wide-web at this URL:
10
- * http://opensource.org/licenses/afl-3.0.php
11
- * If you did not receive a copy of the license and are unable to
12
- * obtain it through the world-wide-web, please send an email
13
- * to license@magentocommerce.com so we can send you a copy immediately.
14
- *
15
- * DISCLAIMER
16
- *
17
- * Do not edit or add to this file if you wish to upgrade Magento to newer
18
- * versions in the future. If you wish to customize Magento for your
19
- * needs please refer to http://www.magentocommerce.com for more information.
20
- *
21
- * @category design
22
- * @package default_default
23
- * @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
24
- * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
25
- */
26
  ?>
27
 
28
  <?php
1
  <?php
2
+ /*
3
+ *
4
+ * This file is required for the pairing table
5
+ *
6
+ */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  ?>
8
 
9
  <?php
lib/Juicy/Geoip/Data/geoip.dat ADDED
Binary file
lib/Juicy/Geoip/geoip.inc ADDED
@@ -0,0 +1,691 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if (!defined('GEOIP_COUNTRY_BEGIN')) define("GEOIP_COUNTRY_BEGIN", 16776960);
4
+ if (!defined('GEOIP_STATE_BEGIN_REV0')) define("GEOIP_STATE_BEGIN_REV0", 16700000);
5
+ if (!defined('GEOIP_STATE_BEGIN_REV1')) define("GEOIP_STATE_BEGIN_REV1", 16000000);
6
+ if (!defined('GEOIP_STANDARD')) define("GEOIP_STANDARD", 0);
7
+ if (!defined('GEOIP_MEMORY_CACHE')) define("GEOIP_MEMORY_CACHE", 1);
8
+ if (!defined('GEOIP_SHARED_MEMORY')) define("GEOIP_SHARED_MEMORY", 2);
9
+ if (!defined('STRUCTURE_INFO_MAX_SIZE')) define("STRUCTURE_INFO_MAX_SIZE", 20);
10
+ if (!defined('DATABASE_INFO_MAX_SIZE')) define("DATABASE_INFO_MAX_SIZE", 100);
11
+ if (!defined('GEOIP_COUNTRY_EDITION')) define("GEOIP_COUNTRY_EDITION", 106);
12
+ if (!defined('GEOIP_PROXY_EDITION')) define("GEOIP_PROXY_EDITION", 8);
13
+ if (!defined('GEOIP_ASNUM_EDITION')) define("GEOIP_ASNUM_EDITION", 9);
14
+ if (!defined('GEOIP_NETSPEED_EDITION')) define("GEOIP_NETSPEED_EDITION", 10);
15
+ if (!defined('GEOIP_REGION_EDITION_REV0')) define("GEOIP_REGION_EDITION_REV0", 112);
16
+ if (!defined('GEOIP_REGION_EDITION_REV1')) define("GEOIP_REGION_EDITION_REV1", 3);
17
+ if (!defined('GEOIP_CITY_EDITION_REV0')) define("GEOIP_CITY_EDITION_REV0", 111);
18
+ if (!defined('GEOIP_CITY_EDITION_REV1')) define("GEOIP_CITY_EDITION_REV1", 2);
19
+ if (!defined('GEOIP_ORG_EDITION')) define("GEOIP_ORG_EDITION", 110);
20
+ if (!defined('GEOIP_ISP_EDITION')) define("GEOIP_ISP_EDITION", 4);
21
+ if (!defined('SEGMENT_RECORD_LENGTH')) define("SEGMENT_RECORD_LENGTH", 3);
22
+ if (!defined('STANDARD_RECORD_LENGTH')) define("STANDARD_RECORD_LENGTH", 3);
23
+ if (!defined('ORG_RECORD_LENGTH')) define("ORG_RECORD_LENGTH", 4);
24
+ if (!defined('MAX_RECORD_LENGTH')) define("MAX_RECORD_LENGTH", 4);
25
+ if (!defined('MAX_ORG_RECORD_LENGTH')) define("MAX_ORG_RECORD_LENGTH", 300);
26
+ if (!defined('GEOIP_SHM_KEY')) define("GEOIP_SHM_KEY", 0x4f415401);
27
+ if (!defined('US_OFFSET')) define("US_OFFSET", 1);
28
+ if (!defined('CANADA_OFFSET')) define("CANADA_OFFSET", 677);
29
+ if (!defined('WORLD_OFFSET')) define("WORLD_OFFSET", 1353);
30
+ if (!defined('FIPS_RANGE')) define("FIPS_RANGE", 360);
31
+ if (!defined('GEOIP_UNKNOWN_SPEED')) define("GEOIP_UNKNOWN_SPEED", 0);
32
+ if (!defined('GEOIP_DIALUP_SPEED')) define("GEOIP_DIALUP_SPEED", 1);
33
+ if (!defined('GEOIP_CABLEDSL_SPEED')) define("GEOIP_CABLEDSL_SPEED", 2);
34
+ if (!defined('GEOIP_CORPORATE_SPEED')) define("GEOIP_CORPORATE_SPEED", 3);
35
+ if (!defined('GEOIP_DOMAIN_EDITION')) define("GEOIP_DOMAIN_EDITION", 11);
36
+ if (!defined('GEOIP_COUNTRY_EDITION_V6')) define("GEOIP_COUNTRY_EDITION_V6", 12);
37
+ if (!defined('GEOIP_LOCATIONA_EDITION')) define("GEOIP_LOCATIONA_EDITION", 13);
38
+ if (!defined('GEOIP_ACCURACYRADIUS_EDITION')) define("GEOIP_ACCURACYRADIUS_EDITION", 14);
39
+ if (!defined('GEOIP_CITYCOMBINED_EDITION')) define("GEOIP_CITYCOMBINED_EDITION", 15);
40
+ if (!defined('GEOIP_CITY_EDITION_REV1_V6')) define("GEOIP_CITY_EDITION_REV1_V6", 30);
41
+ if (!defined('GEOIP_CITY_EDITION_REV0_V6')) define("GEOIP_CITY_EDITION_REV0_V6",31);
42
+ if (!defined('GEOIP_NETSPEED_EDITION_REV1')) define("GEOIP_NETSPEED_EDITION_REV1",32);
43
+ if (!defined('GEOIP_NETSPEED_EDITION_REV1_V6')) define("GEOIP_NETSPEED_EDITION_REV1_V6",33);
44
+ if (!defined('GEOIP_USERTYPE_EDITION')) define("GEOIP_USERTYPE_EDITION",28);
45
+ if (!defined('GEOIP_USERTYPE_EDITION_V6')) define("GEOIP_USERTYPE_EDITION_V6",29);
46
+ if (!defined('GEOIP_ASNUM_EDITION_V6')) define("GEOIP_ASNUM_EDITION_V6",21);
47
+ if (!defined('GEOIP_ISP_EDITION_V6')) define("GEOIP_ISP_EDITION_V6",22);
48
+ if (!defined('GEOIP_ORG_EDITION_V6')) define("GEOIP_ORG_EDITION_V6",23);
49
+ if (!defined('GEOIP_DOMAIN_EDITION_V6')) define("GEOIP_DOMAIN_EDITION_V6",24);
50
+
51
+ if (!defined('CITYCOMBINED_FIXED_RECORD')) define("CITYCOMBINED_FIXED_RECORD", 7 );
52
+
53
+ class GeoIP {
54
+ var $flags;
55
+ var $filehandle;
56
+ var $memory_buffer;
57
+ var $databaseType;
58
+ var $databaseSegments;
59
+ var $record_length;
60
+ var $shmid;
61
+ var $GEOIP_COUNTRY_CODE_TO_NUMBER = array(
62
+ "" => 0, "AP" => 1, "EU" => 2, "AD" => 3, "AE" => 4, "AF" => 5,
63
+ "AG" => 6, "AI" => 7, "AL" => 8, "AM" => 9, "CW" => 10, "AO" => 11,
64
+ "AQ" => 12, "AR" => 13, "AS" => 14, "AT" => 15, "AU" => 16, "AW" => 17,
65
+ "AZ" => 18, "BA" => 19, "BB" => 20, "BD" => 21, "BE" => 22, "BF" => 23,
66
+ "BG" => 24, "BH" => 25, "BI" => 26, "BJ" => 27, "BM" => 28, "BN" => 29,
67
+ "BO" => 30, "BR" => 31, "BS" => 32, "BT" => 33, "BV" => 34, "BW" => 35,
68
+ "BY" => 36, "BZ" => 37, "CA" => 38, "CC" => 39, "CD" => 40, "CF" => 41,
69
+ "CG" => 42, "CH" => 43, "CI" => 44, "CK" => 45, "CL" => 46, "CM" => 47,
70
+ "CN" => 48, "CO" => 49, "CR" => 50, "CU" => 51, "CV" => 52, "CX" => 53,
71
+ "CY" => 54, "CZ" => 55, "DE" => 56, "DJ" => 57, "DK" => 58, "DM" => 59,
72
+ "DO" => 60, "DZ" => 61, "EC" => 62, "EE" => 63, "EG" => 64, "EH" => 65,
73
+ "ER" => 66, "ES" => 67, "ET" => 68, "FI" => 69, "FJ" => 70, "FK" => 71,
74
+ "FM" => 72, "FO" => 73, "FR" => 74, "SX" => 75, "GA" => 76, "GB" => 77,
75
+ "GD" => 78, "GE" => 79, "GF" => 80, "GH" => 81, "GI" => 82, "GL" => 83,
76
+ "GM" => 84, "GN" => 85, "GP" => 86, "GQ" => 87, "GR" => 88, "GS" => 89,
77
+ "GT" => 90, "GU" => 91, "GW" => 92, "GY" => 93, "HK" => 94, "HM" => 95,
78
+ "HN" => 96, "HR" => 97, "HT" => 98, "HU" => 99, "ID" => 100, "IE" => 101,
79
+ "IL" => 102, "IN" => 103, "IO" => 104, "IQ" => 105, "IR" => 106, "IS" => 107,
80
+ "IT" => 108, "JM" => 109, "JO" => 110, "JP" => 111, "KE" => 112, "KG" => 113,
81
+ "KH" => 114, "KI" => 115, "KM" => 116, "KN" => 117, "KP" => 118, "KR" => 119,
82
+ "KW" => 120, "KY" => 121, "KZ" => 122, "LA" => 123, "LB" => 124, "LC" => 125,
83
+ "LI" => 126, "LK" => 127, "LR" => 128, "LS" => 129, "LT" => 130, "LU" => 131,
84
+ "LV" => 132, "LY" => 133, "MA" => 134, "MC" => 135, "MD" => 136, "MG" => 137,
85
+ "MH" => 138, "MK" => 139, "ML" => 140, "MM" => 141, "MN" => 142, "MO" => 143,
86
+ "MP" => 144, "MQ" => 145, "MR" => 146, "MS" => 147, "MT" => 148, "MU" => 149,
87
+ "MV" => 150, "MW" => 151, "MX" => 152, "MY" => 153, "MZ" => 154, "NA" => 155,
88
+ "NC" => 156, "NE" => 157, "NF" => 158, "NG" => 159, "NI" => 160, "NL" => 161,
89
+ "NO" => 162, "NP" => 163, "NR" => 164, "NU" => 165, "NZ" => 166, "OM" => 167,
90
+ "PA" => 168, "PE" => 169, "PF" => 170, "PG" => 171, "PH" => 172, "PK" => 173,
91
+ "PL" => 174, "PM" => 175, "PN" => 176, "PR" => 177, "PS" => 178, "PT" => 179,
92
+ "PW" => 180, "PY" => 181, "QA" => 182, "RE" => 183, "RO" => 184, "RU" => 185,
93
+ "RW" => 186, "SA" => 187, "SB" => 188, "SC" => 189, "SD" => 190, "SE" => 191,
94
+ "SG" => 192, "SH" => 193, "SI" => 194, "SJ" => 195, "SK" => 196, "SL" => 197,
95
+ "SM" => 198, "SN" => 199, "SO" => 200, "SR" => 201, "ST" => 202, "SV" => 203,
96
+ "SY" => 204, "SZ" => 205, "TC" => 206, "TD" => 207, "TF" => 208, "TG" => 209,
97
+ "TH" => 210, "TJ" => 211, "TK" => 212, "TM" => 213, "TN" => 214, "TO" => 215,
98
+ "TL" => 216, "TR" => 217, "TT" => 218, "TV" => 219, "TW" => 220, "TZ" => 221,
99
+ "UA" => 222, "UG" => 223, "UM" => 224, "US" => 225, "UY" => 226, "UZ" => 227,
100
+ "VA" => 228, "VC" => 229, "VE" => 230, "VG" => 231, "VI" => 232, "VN" => 233,
101
+ "VU" => 234, "WF" => 235, "WS" => 236, "YE" => 237, "YT" => 238, "RS" => 239,
102
+ "ZA" => 240, "ZM" => 241, "ME" => 242, "ZW" => 243, "A1" => 244, "A2" => 245,
103
+ "O1" => 246, "AX" => 247, "GG" => 248, "IM" => 249, "JE" => 250, "BL" => 251,
104
+ "MF" => 252, "BQ" => 253,
105
+ );
106
+ var $GEOIP_COUNTRY_CODES = array(
107
+ "","AP","EU","AD","AE","AF","AG","AI","AL","AM","CW",
108
+ "AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB",
109
+ "BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO",
110
+ "BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD",
111
+ "CF","CG","CH","CI","CK","CL","CM","CN","CO","CR",
112
+ "CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO",
113
+ "DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ",
114
+ "FK","FM","FO","FR","SX","GA","GB","GD","GE","GF",
115
+ "GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT",
116
+ "GU","GW","GY","HK","HM","HN","HR","HT","HU","ID",
117
+ "IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO",
118
+ "JP","KE","KG","KH","KI","KM","KN","KP","KR","KW",
119
+ "KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT",
120
+ "LU","LV","LY","MA","MC","MD","MG","MH","MK","ML",
121
+ "MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV",
122
+ "MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI",
123
+ "NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF",
124
+ "PG","PH","PK","PL","PM","PN","PR","PS","PT","PW",
125
+ "PY","QA","RE","RO","RU","RW","SA","SB","SC","SD",
126
+ "SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO",
127
+ "SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH",
128
+ "TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW",
129
+ "TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE",
130
+ "VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA",
131
+ "ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE",
132
+ "BL","MF", "BQ");
133
+ var $GEOIP_COUNTRY_CODES3 = array(
134
+ "","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","CUW",
135
+ "AGO","ATA","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB",
136
+ "BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL",
137
+ "BRA","BHS","BTN","BVT","BWA","BLR","BLZ","CAN","CCK","COD",
138
+ "CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI",
139
+ "CUB","CPV","CXR","CYP","CZE","DEU","DJI","DNK","DMA","DOM",
140
+ "DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI",
141
+ "FLK","FSM","FRO","FRA","SXM","GAB","GBR","GRD","GEO","GUF",
142
+ "GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","SGS","GTM",
143
+ "GUM","GNB","GUY","HKG","HMD","HND","HRV","HTI","HUN","IDN",
144
+ "IRL","ISR","IND","IOT","IRQ","IRN","ISL","ITA","JAM","JOR",
145
+ "JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT",
146
+ "CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
147
+ "LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI",
148
+ "MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV",
149
+ "MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC",
150
+ "NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF",
151
+ "PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW",
152
+ "PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN",
153
+ "SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM",
154
+ "SUR","STP","SLV","SYR","SWZ","TCA","TCD","ATF","TGO","THA",
155
+ "TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN",
156
+ "TZA","UKR","UGA","UMI","USA","URY","UZB","VAT","VCT","VEN",
157
+ "VGB","VIR","VNM","VUT","WLF","WSM","YEM","MYT","SRB","ZAF",
158
+ "ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY",
159
+ "BLM","MAF", "BES"
160
+ );
161
+ var $GEOIP_COUNTRY_NAMES = array(
162
+ "","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Curcao",
163
+ "Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados",
164
+ "Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia",
165
+ "Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the",
166
+ "Central African Republic","Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica",
167
+ "Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic",
168
+ "Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji",
169
+ "Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France","Sint Maarten (Dutch part)","Gabon","United Kingdom","Grenada","Georgia","French Guiana",
170
+ "Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala",
171
+ "Guam","Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia",
172
+ "Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan",
173
+ "Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait",
174
+ "Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania",
175
+ "Luxembourg","Latvia","Libya","Morocco","Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia","Mali",
176
+ "Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives",
177
+ "Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua",
178
+ "Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia",
179
+ "Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau",
180
+ "Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles","Sudan",
181
+ "Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname",
182
+ "Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand",
183
+ "Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan",
184
+ "Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines","Venezuela",
185
+ "Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa",
186
+ "Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey",
187
+ "Saint Barthelemy","Saint Martin", "Bonaire, Saint Eustatius and Saba"
188
+ );
189
+
190
+ var $GEOIP_CONTINENT_CODES = array(
191
+ "--", "AS","EU","EU","AS","AS","NA","NA","EU","AS","NA",
192
+ "AF","AN","SA","OC","EU","OC","NA","AS","EU","NA",
193
+ "AS","EU","AF","EU","AS","AF","AF","NA","AS","SA",
194
+ "SA","NA","AS","AN","AF","EU","NA","NA","AS","AF",
195
+ "AF","AF","EU","AF","OC","SA","AF","AS","SA","NA",
196
+ "NA","AF","AS","AS","EU","EU","AF","EU","NA","NA",
197
+ "AF","SA","EU","AF","AF","AF","EU","AF","EU","OC",
198
+ "SA","OC","EU","EU","NA","AF","EU","NA","AS","SA",
199
+ "AF","EU","NA","AF","AF","NA","AF","EU","AN","NA",
200
+ "OC","AF","SA","AS","AN","NA","EU","NA","EU","AS",
201
+ "EU","AS","AS","AS","AS","AS","EU","EU","NA","AS",
202
+ "AS","AF","AS","AS","OC","AF","NA","AS","AS","AS",
203
+ "NA","AS","AS","AS","NA","EU","AS","AF","AF","EU",
204
+ "EU","EU","AF","AF","EU","EU","AF","OC","EU","AF",
205
+ "AS","AS","AS","OC","NA","AF","NA","EU","AF","AS",
206
+ "AF","NA","AS","AF","AF","OC","AF","OC","AF","NA",
207
+ "EU","EU","AS","OC","OC","OC","AS","NA","SA","OC",
208
+ "OC","AS","AS","EU","NA","OC","NA","AS","EU","OC",
209
+ "SA","AS","AF","EU","EU","AF","AS","OC","AF","AF",
210
+ "EU","AS","AF","EU","EU","EU","AF","EU","AF","AF",
211
+ "SA","AF","NA","AS","AF","NA","AF","AN","AF","AS",
212
+ "AS","OC","AS","AF","OC","AS","EU","NA","OC","AS",
213
+ "AF","EU","AF","OC","NA","SA","AS","EU","NA","SA",
214
+ "NA","NA","AS","OC","OC","OC","AS","AF","EU","AF",
215
+ "AF","EU","AF","--","--","--","EU","EU","EU","EU",
216
+ "NA","NA","NA"
217
+ );
218
+
219
+ }
220
+ function geoip_load_shared_mem ($file) {
221
+
222
+ $fp = fopen($file, "rb");
223
+ if (!$fp) {
224
+ print "error opening $file: $php_errormsg\n";
225
+ exit;
226
+ }
227
+ $s_array = fstat($fp);
228
+ $size = $s_array['size'];
229
+ if ($shmid = @shmop_open (GEOIP_SHM_KEY, "w", 0, 0)) {
230
+ shmop_delete ($shmid);
231
+ shmop_close ($shmid);
232
+ }
233
+ $shmid = shmop_open (GEOIP_SHM_KEY, "c", 0644, $size);
234
+ shmop_write ($shmid, fread($fp, $size), 0);
235
+ shmop_close ($shmid);
236
+ }
237
+
238
+ function _setup_segments($gi){
239
+ $gi->databaseType = GEOIP_COUNTRY_EDITION;
240
+ $gi->record_length = STANDARD_RECORD_LENGTH;
241
+ if ($gi->flags & GEOIP_SHARED_MEMORY) {
242
+ $offset = @shmop_size ($gi->shmid) - 3;
243
+ for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
244
+ $delim = @shmop_read ($gi->shmid, $offset, 3);
245
+ $offset += 3;
246
+ if ($delim == (chr(255).chr(255).chr(255))) {
247
+ $gi->databaseType = ord(@shmop_read ($gi->shmid, $offset, 1));
248
+ $offset++;
249
+
250
+ if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
251
+ $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
252
+ } else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
253
+ $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
254
+ } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)||
255
+ ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
256
+ || ($gi->databaseType == GEOIP_ORG_EDITION)
257
+ || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
258
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
259
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
260
+ || ($gi->databaseType == GEOIP_ISP_EDITION)
261
+ || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
262
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
263
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
264
+ || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
265
+ || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
266
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
267
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
268
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
269
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
270
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION)
271
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
272
+ $gi->databaseSegments = 0;
273
+ $buf = @shmop_read ($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
274
+ for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
275
+ $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
276
+ }
277
+ if (($gi->databaseType == GEOIP_ORG_EDITION)
278
+ || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
279
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
280
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
281
+ || ($gi->databaseType == GEOIP_ISP_EDITION)
282
+ || ($gi->databaseType == GEOIP_ISP_EDITION_V6)) {
283
+ $gi->record_length = ORG_RECORD_LENGTH;
284
+ }
285
+ }
286
+ break;
287
+ } else {
288
+ $offset -= 4;
289
+ }
290
+ }
291
+ if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
292
+ ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
293
+ ($gi->databaseType == GEOIP_PROXY_EDITION)||
294
+ ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
295
+ $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
296
+ }
297
+ } else {
298
+ $filepos = ftell($gi->filehandle);
299
+ fseek($gi->filehandle, -3, SEEK_END);
300
+ for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
301
+ $delim = fread($gi->filehandle,3);
302
+ if ($delim == (chr(255).chr(255).chr(255))){
303
+ $gi->databaseType = ord(fread($gi->filehandle,1));
304
+ if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
305
+ $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
306
+ }
307
+ else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
308
+ $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
309
+ } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)
310
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
311
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
312
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
313
+ || ($gi->databaseType == GEOIP_ORG_EDITION)
314
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
315
+ || ($gi->databaseType == GEOIP_ISP_EDITION)
316
+ || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
317
+ || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
318
+ || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
319
+ || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
320
+ || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
321
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
322
+ || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
323
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
324
+ || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
325
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
326
+ || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
327
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION)
328
+ || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
329
+ $gi->databaseSegments = 0;
330
+ $buf = fread($gi->filehandle,SEGMENT_RECORD_LENGTH);
331
+ for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
332
+ $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
333
+ }
334
+ if ( ( $gi->databaseType == GEOIP_ORG_EDITION )
335
+ || ( $gi->databaseType == GEOIP_DOMAIN_EDITION )
336
+ || ( $gi->databaseType == GEOIP_ISP_EDITION )
337
+ || ( $gi->databaseType == GEOIP_ORG_EDITION_V6 )
338
+ || ( $gi->databaseType == GEOIP_DOMAIN_EDITION_V6 )
339
+ || ( $gi->databaseType == GEOIP_ISP_EDITION_V6 )) {
340
+ $gi->record_length = ORG_RECORD_LENGTH;
341
+ }
342
+ }
343
+ break;
344
+ } else {
345
+ fseek($gi->filehandle, -4, SEEK_CUR);
346
+ }
347
+ }
348
+ if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
349
+ ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
350
+ ($gi->databaseType == GEOIP_PROXY_EDITION)||
351
+ ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
352
+ $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
353
+ }
354
+ fseek($gi->filehandle,$filepos,SEEK_SET);
355
+ }
356
+ return $gi;
357
+ }
358
+
359
+ function geoip_open($filename, $flags) {
360
+ $gi = new GeoIP;
361
+ $gi->flags = $flags;
362
+ if ($gi->flags & GEOIP_SHARED_MEMORY) {
363
+ $gi->shmid = @shmop_open (GEOIP_SHM_KEY, "a", 0, 0);
364
+ } else {
365
+ $gi->filehandle = fopen($filename,"rb") or die( "Can not open $filename\n" );
366
+ if ($gi->flags & GEOIP_MEMORY_CACHE) {
367
+ $s_array = fstat($gi->filehandle);
368
+ $gi->memory_buffer = fread($gi->filehandle, $s_array['size']);
369
+ }
370
+ }
371
+
372
+ $gi = _setup_segments($gi);
373
+ return $gi;
374
+ }
375
+
376
+ function geoip_close($gi) {
377
+ if ($gi->flags & GEOIP_SHARED_MEMORY) {
378
+ return true;
379
+ }
380
+
381
+ return fclose($gi->filehandle);
382
+ }
383
+
384
+ function geoip_country_id_by_name_v6($gi, $name) {
385
+ $rec = dns_get_record($name, DNS_AAAA);
386
+ if ( !$rec ) {
387
+ return false;
388
+ }
389
+ $addr = $rec[0]["ipv6"];
390
+ if (!$addr || $addr == $name) {
391
+ return false;
392
+ }
393
+ return geoip_country_id_by_addr_v6($gi, $addr);
394
+ }
395
+
396
+ function geoip_country_id_by_name($gi, $name) {
397
+ $addr = gethostbyname($name);
398
+ if (!$addr || $addr == $name) {
399
+ return false;
400
+ }
401
+ return geoip_country_id_by_addr($gi, $addr);
402
+ }
403
+
404
+ function geoip_country_code_by_name_v6($gi, $name) {
405
+ $country_id = geoip_country_id_by_name_v6($gi,$name);
406
+ if ($country_id !== false) {
407
+ return $gi->GEOIP_COUNTRY_CODES[$country_id];
408
+ }
409
+ return false;
410
+ }
411
+ function geoip_country_code_by_name($gi, $name) {
412
+ $country_id = geoip_country_id_by_name($gi,$name);
413
+ if ($country_id !== false) {
414
+ return $gi->GEOIP_COUNTRY_CODES[$country_id];
415
+ }
416
+ return false;
417
+ }
418
+
419
+ function geoip_country_name_by_name_v6($gi, $name) {
420
+ $country_id = geoip_country_id_by_name_v6($gi,$name);
421
+ if ($country_id !== false) {
422
+ return $gi->GEOIP_COUNTRY_NAMES[$country_id];
423
+ }
424
+ return false;
425
+ }
426
+ function geoip_country_name_by_name($gi, $name) {
427
+ $country_id = geoip_country_id_by_name($gi,$name);
428
+ if ($country_id !== false) {
429
+ return $gi->GEOIP_COUNTRY_NAMES[$country_id];
430
+ }
431
+ return false;
432
+ }
433
+
434
+ function geoip_country_id_by_addr_v6($gi, $addr) {
435
+ $ipnum = inet_pton($addr);
436
+ return _geoip_seek_country_v6($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
437
+ }
438
+
439
+ function geoip_country_id_by_addr($gi, $addr) {
440
+ $ipnum = ip2long($addr);
441
+ return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
442
+ }
443
+
444
+ function geoip_country_code_by_addr_v6($gi, $addr) {
445
+ $country_id = geoip_country_id_by_addr_v6($gi,$addr);
446
+ if ($country_id !== false) {
447
+ return $gi->GEOIP_COUNTRY_CODES[$country_id];
448
+ }
449
+ return false;
450
+ }
451
+
452
+ function geoip_country_code_by_addr($gi, $addr) {
453
+ if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
454
+ $record = geoip_record_by_addr($gi,$addr);
455
+ if ( $record !== false ) {
456
+ return $record->country_code;
457
+ }
458
+ } else {
459
+ $country_id = geoip_country_id_by_addr($gi,$addr);
460
+ if ($country_id !== false) {
461
+ return $gi->GEOIP_COUNTRY_CODES[$country_id];
462
+ }
463
+ }
464
+ return false;
465
+ }
466
+
467
+ function geoip_country_name_by_addr_v6($gi, $addr) {
468
+ $country_id = geoip_country_id_by_addr_v6($gi,$addr);
469
+ if ($country_id !== false) {
470
+ return $gi->GEOIP_COUNTRY_NAMES[$country_id];
471
+ }
472
+ return false;
473
+ }
474
+
475
+ function geoip_country_name_by_addr($gi, $addr) {
476
+ if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
477
+ $record = geoip_record_by_addr($gi,$addr);
478
+ return $record->country_name;
479
+ } else {
480
+ $country_id = geoip_country_id_by_addr($gi,$addr);
481
+ if ($country_id !== false) {
482
+ return $gi->GEOIP_COUNTRY_NAMES[$country_id];
483
+ }
484
+ }
485
+ return false;
486
+ }
487
+
488
+ function _geoip_seek_country_v6($gi, $ipnum) {
489
+
490
+ # arrays from unpack start with offset 1
491
+ # yet another php mystery. array_merge work around
492
+ # this broken behaviour
493
+ $v6vec = array_merge(unpack( "C16", $ipnum));
494
+
495
+ $offset = 0;
496
+ for ($depth = 127; $depth >= 0; --$depth) {
497
+ if ($gi->flags & GEOIP_MEMORY_CACHE) {
498
+ // workaround php's broken substr, strpos, etc handling with
499
+ // mbstring.func_overload and mbstring.internal_encoding
500
+ $enc = mb_internal_encoding();
501
+ mb_internal_encoding('ISO-8859-1');
502
+
503
+ $buf = substr($gi->memory_buffer,
504
+ 2 * $gi->record_length * $offset,
505
+ 2 * $gi->record_length);
506
+
507
+ mb_internal_encoding($enc);
508
+ } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
509
+ $buf = @shmop_read ($gi->shmid,
510
+ 2 * $gi->record_length * $offset,
511
+ 2 * $gi->record_length );
512
+ } else {
513
+ fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
514
+ or die("fseek failed");
515
+ $buf = fread($gi->filehandle, 2 * $gi->record_length);
516
+ }
517
+ $x = array(0,0);
518
+ for ($i = 0; $i < 2; ++$i) {
519
+ for ($j = 0; $j < $gi->record_length; ++$j) {
520
+ $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
521
+ }
522
+ }
523
+
524
+ $bnum = 127 - $depth;
525
+ $idx = $bnum >> 3;
526
+ $b_mask = 1 << ( $bnum & 7 ^ 7 );
527
+ if (($v6vec[$idx] & $b_mask) > 0) {
528
+ if ($x[1] >= $gi->databaseSegments) {
529
+ return $x[1];
530
+ }
531
+ $offset = $x[1];
532
+ } else {
533
+ if ($x[0] >= $gi->databaseSegments) {
534
+ return $x[0];
535
+ }
536
+ $offset = $x[0];
537
+ }
538
+ }
539
+ trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
540
+ return false;
541
+ }
542
+
543
+ function _geoip_seek_country($gi, $ipnum) {
544
+ $offset = 0;
545
+ for ($depth = 31; $depth >= 0; --$depth) {
546
+ if ($gi->flags & GEOIP_MEMORY_CACHE) {
547
+ // workaround php's broken substr, strpos, etc handling with
548
+ // mbstring.func_overload and mbstring.internal_encoding
549
+ $enc = mb_internal_encoding();
550
+ mb_internal_encoding('ISO-8859-1');
551
+
552
+ $buf = substr($gi->memory_buffer,
553
+ 2 * $gi->record_length * $offset,
554
+ 2 * $gi->record_length);
555
+
556
+ mb_internal_encoding($enc);
557
+ } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
558
+ $buf = @shmop_read ($gi->shmid,
559
+ 2 * $gi->record_length * $offset,
560
+ 2 * $gi->record_length );
561
+ } else {
562
+ fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
563
+ or die("fseek failed");
564
+ $buf = fread($gi->filehandle, 2 * $gi->record_length);
565
+ }
566
+ $x = array(0,0);
567
+ for ($i = 0; $i < 2; ++$i) {
568
+ for ($j = 0; $j < $gi->record_length; ++$j) {
569
+ $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
570
+ }
571
+ }
572
+ if ($ipnum & (1 << $depth)) {
573
+ if ($x[1] >= $gi->databaseSegments) {
574
+ return $x[1];
575
+ }
576
+ $offset = $x[1];
577
+ } else {
578
+ if ($x[0] >= $gi->databaseSegments) {
579
+ return $x[0];
580
+ }
581
+ $offset = $x[0];
582
+ }
583
+ }
584
+ trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
585
+ return false;
586
+ }
587
+
588
+ function _common_get_org($gi, $seek_org){
589
+ $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments;
590
+ if ($gi->flags & GEOIP_SHARED_MEMORY) {
591
+ $org_buf = @shmop_read ($gi->shmid, $record_pointer, MAX_ORG_RECORD_LENGTH);
592
+ } else {
593
+ fseek($gi->filehandle, $record_pointer, SEEK_SET);
594
+ $org_buf = fread($gi->filehandle,MAX_ORG_RECORD_LENGTH);
595
+ }
596
+ // workaround php's broken substr, strpos, etc handling with
597
+ // mbstring.func_overload and mbstring.internal_encoding
598
+ $enc = mb_internal_encoding();
599
+ mb_internal_encoding('ISO-8859-1');
600
+ $org_buf = substr($org_buf, 0, strpos($org_buf, "\0"));
601
+ mb_internal_encoding($enc);
602
+ return $org_buf;
603
+ }
604
+
605
+ function _get_org_v6($gi,$ipnum){
606
+ $seek_org = _geoip_seek_country_v6($gi,$ipnum);
607
+ if ($seek_org == $gi->databaseSegments) {
608
+ return NULL;
609
+ }
610
+ return _common_get_org($gi, $seek_org);
611
+ }
612
+
613
+ function _get_org($gi,$ipnum){
614
+ $seek_org = _geoip_seek_country($gi,$ipnum);
615
+ if ($seek_org == $gi->databaseSegments) {
616
+ return NULL;
617
+ }
618
+ return _common_get_org($gi, $seek_org);
619
+ }
620
+
621
+
622
+
623
+ function geoip_name_by_addr_v6 ($gi,$addr) {
624
+ if ($addr == NULL) {
625
+ return 0;
626
+ }
627
+ $ipnum = inet_pton($addr);
628
+ return _get_org_v6($gi, $ipnum);
629
+ }
630
+
631
+ function geoip_name_by_addr ($gi,$addr) {
632
+ if ($addr == NULL) {
633
+ return 0;
634
+ }
635
+ $ipnum = ip2long($addr);
636
+ return _get_org($gi, $ipnum);
637
+ }
638
+
639
+ function geoip_org_by_addr ($gi,$addr) {
640
+ return geoip_name_by_addr($gi, $addr);
641
+ }
642
+
643
+ function _get_region($gi,$ipnum){
644
+ if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
645
+ $seek_region = _geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV0;
646
+ if ($seek_region >= 1000){
647
+ $country_code = "US";
648
+ $region = chr(($seek_region - 1000)/26 + 65) . chr(($seek_region - 1000)%26 + 65);
649
+ } else {
650
+ $country_code = $gi->GEOIP_COUNTRY_CODES[$seek_region];
651
+ $region = "";
652
+ }
653
+ return array ($country_code,$region);
654
+ } else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
655
+ $seek_region = _geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV1;
656
+ //print $seek_region;
657
+ if ($seek_region < US_OFFSET){
658
+ $country_code = "";
659
+ $region = "";
660
+ } else if ($seek_region < CANADA_OFFSET) {
661
+ $country_code = "US";
662
+ $region = chr(($seek_region - US_OFFSET)/26 + 65) . chr(($seek_region - US_OFFSET)%26 + 65);
663
+ } else if ($seek_region < WORLD_OFFSET) {
664
+ $country_code = "CA";
665
+ $region = chr(($seek_region - CANADA_OFFSET)/26 + 65) . chr(($seek_region - CANADA_OFFSET)%26 + 65);
666
+ } else {
667
+ $country_code = $gi->GEOIP_COUNTRY_CODES[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
668
+ $region = "";
669
+ }
670
+ return array ($country_code,$region);
671
+ }
672
+ }
673
+
674
+ function geoip_region_by_addr ($gi,$addr) {
675
+ if ($addr == NULL) {
676
+ return 0;
677
+ }
678
+ $ipnum = ip2long($addr);
679
+ return _get_region($gi, $ipnum);
680
+ }
681
+
682
+ function getdnsattributes ($l,$ip){
683
+ $r = new Net_DNS_Resolver();
684
+ $r->nameservers = array("ws1.maxmind.com");
685
+ $p = $r->search($l."." . $ip .".s.maxmind.com","TXT","IN");
686
+ $str = is_object($p->answer[0])?$p->answer[0]->string():'';
687
+ $str = substr( $str, 1, -1 );
688
+ return $str;
689
+ }
690
+
691
+ ?>
package.xml CHANGED
@@ -1,18 +1,19 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Juicy_GeoIP</name>
4
- <version>1.0.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>Stable Release</notes>
 
12
  <authors><author><name>Jonathan Webb</name><user>JuicyMedia</user><email>jonathan.webb@juicymedia.co.uk</email></author></authors>
13
- <date>2015-01-19</date>
14
- <time>11:03:07</time>
15
- <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="537242b732c5bcd2e087137c864359e9"/><file name="Observer.php" hash="176a89109b18c4322ec109c465067e8a"/></dir><dir name="etc"><file name="adminhtml.xml" hash="5ac9ab6f0f5b9bb998fa7666dbe54803"/><file name="config.xml" hash="8926d6a0f8d457c837b5972fbd42c90c"/><file name="system.xml" hash="91e1e5163d0b725fd810e41a1339a875"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Juicy_Geoip.xml" hash="c4ebc7b11208b6c6b6020cb581ba9b12"/></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="83994a57e699b206fe5fa898d1f99690"/></dir></dir></dir></dir></dir></dir></dir></dir></dir></dir></target></contents>
16
  <compatible/>
17
- <dependencies><required><php><min>5.0.0</min><max>6.0.0</max></php><extension><name>geoip</name><min/><max>100</max></extension></required></dependencies>
18
  </package>
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>