├── var
└── geoip
│ ├── GeoIP.dat
│ ├── GeoIPv6.dat
│ ├── ip2country
│ ├── ip2country.dat
│ └── Ip2Country.php
│ └── geoip.inc
├── modman
├── app
├── etc
│ └── modules
│ │ └── Chapagain_AutoCurrency.xml
└── code
│ └── community
│ └── Chapagain
│ └── AutoCurrency
│ ├── etc
│ ├── config.xml
│ └── system.xml
│ ├── Model
│ ├── System
│ │ └── Config
│ │ │ └── Source
│ │ │ └── Database.php
│ └── Store.php
│ └── Helper
│ └── Data.php
├── README.md
└── LICENSE
/var/geoip/GeoIP.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chapagain/auto-currency-switcher/master/var/geoip/GeoIP.dat
--------------------------------------------------------------------------------
/var/geoip/GeoIPv6.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chapagain/auto-currency-switcher/master/var/geoip/GeoIPv6.dat
--------------------------------------------------------------------------------
/var/geoip/ip2country/ip2country.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chapagain/auto-currency-switcher/master/var/geoip/ip2country/ip2country.dat
--------------------------------------------------------------------------------
/modman:
--------------------------------------------------------------------------------
1 | app/etc/modules/Chapagain_AutoCurrency.xml app/etc/modules/Chapagain_AutoCurrency.xml
2 | app/code/community/Chapagain/AutoCurrency/* app/code/community/Chapagain/AutoCurrency/
3 | var/geoip/* var/geoip/
4 |
--------------------------------------------------------------------------------
/app/etc/modules/Chapagain_AutoCurrency.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | true
6 | community
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/AutoCurrency/etc/config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 0.2.1
6 |
7 |
8 |
9 |
10 |
11 | Chapagain_AutoCurrency_Model
12 |
13 |
14 |
15 | Chapagain_AutoCurrency_Model_Store
16 |
17 |
18 |
19 |
20 |
21 | Chapagain_AutoCurrency_Helper
22 |
23 |
24 |
25 |
26 |
27 |
28 | 1
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/AutoCurrency/Model/System/Config/Source/Database.php:
--------------------------------------------------------------------------------
1 |
9 | * @link http://blog.chapagain.com.np/category/magento/
10 | */
11 | class Chapagain_AutoCurrency_Model_System_Config_Source_Database
12 | {
13 |
14 | /**
15 | * Options getter
16 | *
17 | * @return array
18 | */
19 | public function toOptionArray()
20 | {
21 | return array(
22 | array('value' => 'ip2country', 'label'=>Mage::helper('autocurrency')->__('IP to Country')),
23 | array('value' => 'maxmind', 'label'=>Mage::helper('autocurrency')->__('Max Mind')),
24 | );
25 | }
26 |
27 | /**
28 | * Get options in "key-value" format
29 | *
30 | * @return array
31 | */
32 | public function toArray()
33 | {
34 | return array(
35 | 'ip2country' => Mage::helper('autocurrency')->__('IP to Country'),
36 | 'maxmind' => Mage::helper('autocurrency')->__('Max Mind'),
37 | );
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/AutoCurrency/etc/system.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | text
9 | 10
10 | 1
11 | 1
12 | 1
13 |
14 |
15 |
16 | Enable/Disable extension
17 | select
18 | adminhtml/system_config_source_yesno
19 | 10
20 | 1
21 | 1
22 | 1
23 |
24 |
25 |
26 |
27 | select
28 | autocurrency/system_config_source_database
29 | 20
30 | 1
31 | 1
32 | 1
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Auto Currency Switcher - Magento Extension
2 | ======================
3 |
4 | Auto Currency extension tracks visitor's IP address and automatically changes the store currency to the visitor's location currency. Visitor can switch to his/her desired currency at any time.
5 |
6 | This extension uses two IP Address databases for IP Address lookup. One is `MaxMind's GeoIP` and the other is `Webnet77's Ip2Country` database. Shop admins have the option to choose between these two databases. The default one is `Webnet77's Ip2Country` database.
7 |
8 | ## Installation ##
9 |
10 | Just install the module in your multi-currency enabled Magento shop and the module will work on the fly.
11 |
12 | No extra configuration settings is to be made.
13 |
14 | However, you have the option to `Enable or Disable` the module & `Choose IP address Database Source` from configuration setting:
15 |
16 | `System -> Configuration -> CATALOG -> Catalog -> Auto Currency`
17 |
18 | By default, the module is enabled and the IP address database source is Ip2Country.
19 |
20 | ## Troubleshoot ##
21 |
22 | This extension should run immediately after installing it. If it doesn't work then please check for the following:-
23 |
24 | 1) You should first setup/enable multiple currency on your shop. Here is about [How to setup multiple currency Magento shop?](http://blog.chapagain.com.np/magento-setup-multiple-currency-shop/)
25 |
26 | 2) Sometimes the extension doesn't work due to browser cache. Please clear your browser cache and try reloading your website. Or, try opening your website in another browser.
27 |
28 | 3) If Caching is enabled then Refresh Cache (`System -> Cache Management`)
29 |
30 | 4) If Compilation is enabled then Re-run Compilation (`System -> Tools -> Compilation`)
31 |
32 | **More details**: [Magento Extension: Auto Currency Switcher BLOG](http://blog.chapagain.com.np/magento-extension-auto-currency-switcher-free/)
33 |
34 | ## Updating GeoIP Databases ##
35 |
36 | The GeoIP database should be updated from time to time in order to make this extension work accurately. In this extension, you have option to choose from two databases (Webnet77's ip2country database & MaxMind's GeoIP database). Here are the ways to update these databases for this extension:
37 |
38 | **Updating Webnet77's Database**
39 |
40 | 1) Download [IPV4 CSV](http://software77.net/geo-ip/) file
41 |
42 | 2) Extract the file. This will extract IPtoCountry.csv file.
43 |
44 | 3) Use [Ip2Country lookup classes for PHP](https://github.com/mgefvert/Ip2Country) to create binary-optimized version of the csv file.
45 |
46 | 4) Upload the binary file (`.dat` file) to your `[Magento Folder]/var/geoip/ip2country/` folder.
47 |
48 | **Updating MaxMind GeoIP Database**
49 |
50 | 1) Download [GeoIP Database](http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz) and [GeoIPv6 Database](http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz)
51 |
52 | 2) Extract both `.gz` files
53 |
54 | 3) Upload them to your `[Magento Folder]/var/geoip/` folder
55 |
56 | ## Thanks ##
57 |
58 | * [MaxMind GeoIP Legacy database](http://dev.maxmind.com/geoip/legacy/geolite/)
59 |
60 | * [MaxMind GeoIP Legacy PHP API](https://github.com/maxmind/geoip-api-php)
61 |
62 | * [Webnet77 Ip2Country database](http://software77.net/geo-ip/)
63 |
64 | * [Mats Gefvert's Ip2Country PHP API](https://github.com/mgefvert/Ip2Country)
65 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/AutoCurrency/Model/Store.php:
--------------------------------------------------------------------------------
1 |
10 | * @link http://blog.chapagain.com.np/category/magento/
11 | */
12 |
13 | class Chapagain_AutoCurrency_Model_Store extends Mage_Core_Model_Store
14 | {
15 | /**
16 | * Update default store currency code
17 | *
18 | * @return string
19 | */
20 | public function getDefaultCurrencyCode()
21 | {
22 | if (Mage::helper('autocurrency')->isEnabled()) {
23 | $result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);
24 | return $this->getCurrencyCodeByIp($result);
25 | } else {
26 | return parent::getDefaultCurrencyCode();
27 | }
28 | }
29 |
30 | /**
31 | * Get Currency code by IP Address
32 | *
33 | * @return string
34 | */
35 | public function getCurrencyCodeByIp($result = '')
36 | {
37 | $databaseSource = Mage::helper('autocurrency')->getDatabaseSource();
38 |
39 | if ($databaseSource == 'maxmind') {
40 | $currencyCode = $this->getCurrencyCodeMaxMind();
41 | } else {
42 | $currencyCode = $this->getCurrencyCodeIp2Country();
43 | }
44 |
45 | // if currencyCode is not present in allowedCurrencies
46 | // then return the default currency code
47 | $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
48 | if(!in_array($currencyCode, $allowedCurrencies)) {
49 | return $result;
50 | }
51 |
52 | return $currencyCode;
53 | }
54 |
55 | /**
56 | * Get Currency code by IP Address
57 | * Using Ip2Country Database
58 | *
59 | * @return string
60 | */
61 | public function getCurrencyCodeIp2Country()
62 | {
63 | // load Ip2Country database
64 | $ipc = Mage::helper('autocurrency')->loadIp2Country();
65 |
66 | // get IP Address
67 | $ipAddress = Mage::helper('autocurrency')->getIpAddress();
68 |
69 | // additional valid ip check
70 | // because Ip2Country generates error for invalid IP address
71 | if (!Mage::helper('autocurrency')->checkValidIp($ipAddress)) {
72 | return null;
73 | }
74 |
75 | $countryCode = $ipc->lookup($ipAddress);
76 |
77 | $currencyCode = Mage::helper('autocurrency')->getCurrencyByCountry($countryCode);
78 |
79 | return $currencyCode;
80 | }
81 |
82 | /**
83 | * Get Currency code by IP Address
84 | * Using MaxMind Database
85 | *
86 | * @return string
87 | */
88 | public function getCurrencyCodeMaxMind()
89 | {
90 | // include geoip.inc file
91 | Mage::helper('autocurrency')->loadGeoIpInc();
92 |
93 | // get IP Address
94 | $ipAddress = Mage::helper('autocurrency')->getIpAddress();
95 |
96 | // load GeoIP .dat binary file
97 | if (Mage::helper('autocurrency')->checkIpv6($ipAddress)) {
98 | $geoIp = Mage::helper('autocurrency')->loadGeoIpv6();
99 | } else {
100 | $geoIp = Mage::helper('autocurrency')->loadGeoIpv4();
101 | }
102 |
103 | // get country code from ip address
104 | $countryCode = geoip_country_code_by_addr($geoIp, $ipAddress);
105 |
106 | // get currency code from country code
107 | //$currencyCode = geoip_currency_code_by_country_code($geoIp, $countryCode);
108 | $currencyCode = Mage::helper('autocurrency')->getCurrencyByCountry($countryCode);
109 |
110 | // close the geo database
111 | geoip_close($geoIp);
112 |
113 | return $currencyCode;
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/AutoCurrency/Helper/Data.php:
--------------------------------------------------------------------------------
1 |
9 | * @link http://blog.chapagain.com.np/category/magento/
10 | */
11 |
12 | class Chapagain_AutoCurrency_Helper_Data extends Mage_Core_Helper_Abstract
13 | {
14 | /**
15 | * Check if the module is enabled
16 | *
17 | * @return boolean 0 or 1
18 | */
19 | public function isEnabled()
20 | {
21 | return Mage::getStoreConfig('catalog/autocurrency/enable_disable');
22 | }
23 |
24 | /**
25 | * Check IP database source
26 | *
27 | * @return string
28 | */
29 | public function getDatabaseSource()
30 | {
31 | $db = Mage::getStoreConfig('catalog/autocurrency/database_source');
32 | if ($db == null) {
33 | $db = 'ip2country';
34 | }
35 | return $db;
36 | }
37 |
38 | /**
39 | * Get IP Address
40 | *
41 | * @return string
42 | */
43 | public function getIpAddress()
44 | {
45 | return $_SERVER['REMOTE_ADDR'];
46 | }
47 |
48 | /**
49 | * Include GeoIP Inc file
50 | */
51 | public function loadGeoIpInc()
52 | {
53 | // Load geoip.inc
54 | include_once(Mage::getBaseDir().'/var/geoip/geoip.inc');
55 | }
56 |
57 | /**
58 | * Load GeoIPv4 binary data file
59 | */
60 | public function loadGeoIpv4()
61 | {
62 | $geoIPv4 = geoip_open(Mage::getBaseDir().'/var/geoip/GeoIP.dat',GEOIP_STANDARD);
63 | return $geoIPv4;
64 | }
65 |
66 | /**
67 | * Load GeoIPv6 binary data file
68 | */
69 | public function loadGeoIpv6()
70 | {
71 | $geoIPv6 = geoip_open(Mage::getBaseDir().'/var/geoip/GeoIPv6.dat',GEOIP_STANDARD);
72 | return $geoIPv6;
73 | }
74 |
75 | /**
76 | * Check whether the given IP Address is valid
77 | *
78 | * @param string IP Address
79 | * @return boolean True/False
80 | */
81 | public function checkValidIp($ip)
82 | {
83 | if(!filter_var($ip, FILTER_VALIDATE_IP)) {
84 | return false;
85 | }
86 | return true;
87 | }
88 |
89 | /**
90 | * Check whether the given IP Address is IPv6
91 | *
92 | * @param string IP Address
93 | * @return boolean True/False
94 | */
95 | public function checkIpv6($ip)
96 | {
97 | if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
98 | return false;
99 | }
100 | return true;
101 | }
102 |
103 | /**
104 | * Include IP2Country.php file
105 | * and load ip2country.dat file
106 | *
107 | */
108 | public function loadIp2Country()
109 | {
110 | include_once(Mage::getBaseDir().'/var/geoip/ip2country/Ip2Country.php');
111 | $ipc = new Ip2Country(Mage::getBaseDir().'/var/geoip/ip2country/ip2country.dat');
112 | $ipc->preload();
113 | return $ipc;
114 | }
115 |
116 | /**
117 | * Get Currency code by Country code
118 | *
119 | * @return string
120 | */
121 | public function getCurrencyByCountry($countryCode)
122 | {
123 | $map = array( '' => '',
124 | "EU" => "EUR", "AD" => "EUR", "AE" => "AED", "AF" => "AFN", "AG" => "XCD", "AI" => "XCD",
125 | "AL" => "ALL", "AM" => "AMD", "CW" => "ANG", "AO" => "AOA", "AQ" => "AQD", "AR" => "ARS", "AS" => "EUR",
126 | "AT" => "EUR", "AU" => "AUD", "AW" => "AWG", "AZ" => "AZN", "BA" => "BAM", "BB" => "BBD",
127 | "BD" => "BDT", "BE" => "EUR", "BF" => "XOF", "BG" => "BGL", "BH" => "BHD", "BI" => "BIF",
128 | "BJ" => "XOF", "BM" => "BMD", "BN" => "BND", "BO" => "BOB", "BR" => "BRL", "BS" => "BSD",
129 | "BT" => "BTN", "BV" => "NOK", "BW" => "BWP", "BY" => "BYR", "BZ" => "BZD", "CA" => "CAD",
130 | "CC" => "AUD", "CD" => "CDF", "CF" => "XAF", "CG" => "XAF", "CH" => "CHF", "CI" => "XOF",
131 | "CK" => "NZD", "CL" => "CLP", "CM" => "XAF", "CN" => "CNY", "CO" => "COP", "CR" => "CRC",
132 | "CU" => "CUP", "CV" => "CVE", "CX" => "AUD", "CY" => "EUR", "CZ" => "CZK", "DE" => "EUR",
133 | "DJ" => "DJF", "DK" => "DKK", "DM" => "XCD", "DO" => "DOP", "DZ" => "DZD", "EC" => "ECS",
134 | "EE" => "EEK", "EG" => "EGP", "EH" => "MAD", "ER" => "ETB", "ES" => "EUR", "ET" => "ETB",
135 | "FI" => "EUR", "FJ" => "FJD", "FK" => "FKP", "FM" => "USD", "FO" => "DKK", "FR" => "EUR", "SX" => "ANG",
136 | "GA" => "XAF", "GB" => "GBP", "GD" => "XCD", "GE" => "GEL", "GF" => "EUR", "GH" => "GHS",
137 | "GI" => "GIP", "GL" => "DKK", "GM" => "GMD", "GN" => "GNF", "GP" => "EUR", "GQ" => "XAF",
138 | "GR" => "EUR", "GS" => "GBP", "GT" => "GTQ", "GU" => "USD", "GW" => "XOF", "GY" => "GYD",
139 | "HK" => "HKD", "HM" => "AUD", "HN" => "HNL", "HR" => "HRK", "HT" => "HTG", "HU" => "HUF",
140 | "ID" => "IDR", "IE" => "EUR", "IL" => "ILS", "IN" => "INR", "IO" => "USD", "IQ" => "IQD",
141 | "IR" => "IRR", "IS" => "ISK", "IT" => "EUR", "JM" => "JMD", "JO" => "JOD", "JP" => "JPY",
142 | "KE" => "KES", "KG" => "KGS", "KH" => "KHR", "KI" => "AUD", "KM" => "KMF", "KN" => "XCD",
143 | "KP" => "KPW", "KR" => "KRW", "KW" => "KWD", "KY" => "KYD", "KZ" => "KZT", "LA" => "LAK",
144 | "LB" => "LBP", "LC" => "XCD", "LI" => "CHF", "LK" => "LKR", "LR" => "LRD", "LS" => "LSL",
145 | "LT" => "LTL", "LU" => "EUR", "LV" => "LVL", "LY" => "LYD", "MA" => "MAD", "MC" => "EUR",
146 | "MD" => "MDL", "MG" => "MGF", "MH" => "USD", "MK" => "MKD", "ML" => "XOF", "MM" => "MMK",
147 | "MN" => "MNT", "MO" => "MOP", "MP" => "USD", "MQ" => "EUR", "MR" => "MRO", "MS" => "XCD",
148 | "MT" => "EUR", "MU" => "MUR", "MV" => "MVR", "MW" => "MWK", "MX" => "MXN", "MY" => "MYR",
149 | "MZ" => "MZN", "NA" => "NAD", "NC" => "XPF", "NE" => "XOF", "NF" => "AUD", "NG" => "NGN",
150 | "NI" => "NIO", "NL" => "EUR", "NO" => "NOK", "NP" => "NPR", "NR" => "AUD", "NU" => "NZD",
151 | "NZ" => "NZD", "OM" => "OMR", "PA" => "PAB", "PE" => "PEN", "PF" => "XPF", "PG" => "PGK",
152 | "PH" => "PHP", "PK" => "PKR", "PL" => "PLN", "PM" => "EUR", "PN" => "NZD", "PR" => "USD", "PS" => "ILS", "PT" => "EUR",
153 | "PW" => "USD", "PY" => "PYG", "QA" => "QAR", "RE" => "EUR", "RO" => "RON", "RU" => "RUB",
154 | "RW" => "RWF", "SA" => "SAR", "SB" => "SBD", "SC" => "SCR", "SD" => "SDD", "SE" => "SEK",
155 | "SG" => "SGD", "SH" => "SHP", "SI" => "EUR", "SJ" => "NOK", "SK" => "SKK", "SL" => "SLL",
156 | "SM" => "EUR", "SN" => "XOF", "SO" => "SOS", "SR" => "SRG", "ST" => "STD", "SV" => "SVC",
157 | "SY" => "SYP", "SZ" => "SZL", "TC" => "USD", "TD" => "XAF", "TF" => "EUR", "TG" => "XOF",
158 | "TH" => "THB", "TJ" => "TJS", "TK" => "NZD", "TM" => "TMM", "TN" => "TND", "TO" => "TOP", "TL" => "USD",
159 | "TR" => "TRY", "TT" => "TTD", "TV" => "AUD", "TW" => "TWD", "TZ" => "TZS", "UA" => "UAH",
160 | "UG" => "UGX", "UM" => "USD", "US" => "USD", "UY" => "UYU", "UZ" => "UZS", "VA" => "EUR",
161 | "VC" => "XCD", "VE" => "VEF", "VG" => "USD", "VI" => "USD", "VN" => "VND", "VU" => "VUV",
162 | "WF" => "XPF", "WS" => "EUR", "YE" => "YER", "YT" => "EUR", "RS" => "RSD",
163 | "ZA" => "ZAR", "ZM" => "ZMK", "ME" => "EUR", "ZW" => "ZWD",
164 | "AX" => "EUR", "GG" => "GBP", "IM" => "GBP",
165 | "JE" => "GBP", "BL" => "EUR", "MF" => "EUR", "BQ" => "USD", "SS" => "SSP"
166 | );
167 |
168 | return $map[$countryCode];
169 | }
170 | }
171 |
--------------------------------------------------------------------------------
/var/geoip/ip2country/Ip2Country.php:
--------------------------------------------------------------------------------
1 | fp = fopen($datafile, 'r');
48 |
49 | $header = fread($this->fp, 8);
50 | list(, $indexlen, $this->datalen) = unpack('L2', $header);
51 | $this->offset = 8 + $indexlen;
52 |
53 | $this->index = array_fill_keys(range(0, 8191), null);
54 |
55 | $index = str_split(fread($this->fp, $indexlen), 12);
56 | foreach($index as $ix)
57 | $this->index[self::ipgroup($ix)] .= $ix;
58 |
59 | $lastv = null;
60 | foreach($this->index as $k => &$v)
61 | if ($v)
62 | $lastv = $v;
63 | else
64 | $v = $lastv;
65 | }
66 |
67 | /**
68 | * Destroys the ip 2 country instance.
69 | */
70 | public function __destruct()
71 | {
72 | if ($this->fp)
73 | fclose($this->fp);
74 | }
75 |
76 | /**
77 | * Get the group of an IP address by returning the 13 high bits
78 | */
79 | public static function ipgroup($ipbin)
80 | {
81 | return (ord($ipbin[0]) << 8 | ord($ipbin[1])) >> 3;
82 | }
83 |
84 | /**
85 | * Preloads the entire data file into memory for faster lookups of many IP
86 | * addresses
87 | */
88 | public function preload()
89 | {
90 | if ($this->fp == null)
91 | return;
92 |
93 | fseek($this->fp, $this->offset);
94 | $data = fread($this->fp, $this->datalen);
95 |
96 | foreach($this->index as $index)
97 | foreach(str_split($index, 12) as $ix)
98 | {
99 | list(, , $pos, $len) = unpack('L3', $ix);
100 | if (!isset($this->data[$pos]))
101 | $this->data[$pos] = substr($data, $pos, $len + 6); // Always one extra record, for upper bound
102 | }
103 |
104 | fclose($this->fp);
105 | $this->fp = null;
106 | }
107 |
108 | /**
109 | * Binary search function that searches a string with records for a match.
110 | * Requires that the array is sorted. Since we're operating on 32-bit
111 | * IPs, we match the first four bytes of every string and the
112 | * rest is just payload. Returns the entry "n" for which the relation
113 | * 32bit($data[n]) <= $str < 32bit($data[n+1])
114 | * is true.
115 | *
116 | * Always tries to return 4 extra bytes from the next record, for an upper
117 | * bound. So if $reclen is 12, it tries to return 16 bytes.
118 | */
119 | private function findRecord($data, $str, $reclen)
120 | {
121 | $l = 0;
122 | $count = strlen($data) / $reclen;
123 | $h = $count - 1;
124 | while ($l <= $h)
125 | {
126 | $i = ($l + $h) >> 1;
127 |
128 | // Get the current record (i) and compare
129 | $rec = substr($data, $i * $reclen, $reclen + 4);
130 | $c = strcmp($str, substr($rec, 0, 4));
131 |
132 | // Equal? I guess we found it.
133 | if ($c == 0)
134 | return $rec;
135 | // If it's more, then compare with the next record to see if that one is less
136 | else if ($c > 0)
137 | {
138 | // If this was the last record, then return it
139 | if ($i+1 >= $count)
140 | return $rec;
141 |
142 | $rec1 = substr($data, ($i+1)*$reclen, $reclen + 4);
143 | $c1 = strcmp($str, substr($rec1, 0, 4));
144 |
145 | // Did we stumble upon the right one?
146 | if ($c1 == 0)
147 | // Oops. Equal with the next one. Just return.
148 | return $rec1;
149 | else if ($c1 < 0)
150 | // Yep, this is the right one.
151 | return $rec;
152 |
153 | $l = $i + 1;
154 | }
155 | else
156 | $h = $i - 1;
157 | }
158 |
159 | // Never supposed to end up here - something is wrong.
160 | throw new Exception('Binary find failure');
161 | }
162 |
163 | /**
164 | * Find the index given by the IP address.
165 | */
166 | private function findIndex($ipbin)
167 | {
168 | $group = self::ipgroup($ipbin);
169 |
170 | $ix = $this->index[$group];
171 | if (strcmp($ipbin, substr($ix, 0, 4)) < 0 && $group > 0)
172 | $ix = $this->index[$group-1];
173 |
174 | if (strlen($ix) > 12)
175 | $ix = $this->findRecord($ix, $ipbin, 12);
176 |
177 | return $ix;
178 | }
179 |
180 | /**
181 | * Load the specific data chunk from the main data file and save it
182 | * to the data cache.
183 | */
184 | private function getDataEntry($pos, $len)
185 | {
186 | // If it doesn't exist in the cache, load it
187 | if (!isset($this->data[$pos]))
188 | {
189 | fseek($this->fp, $pos + $this->offset);
190 | $this->data[$pos] = fread($this->fp, $len + 6); // Always one extra record, for upper bound
191 | }
192 |
193 | return $this->data[$pos];
194 | }
195 |
196 | /**
197 | * Lookup a 4-byte binary IP string and return the two-letter ISO country.
198 | */
199 | public function lookupbin($ipbin)
200 | {
201 | if (strlen($this->lastentry) == 10 && strcmp($ipbin, substr($this->lastentry, 0, 4)) >= 0 && strcmp($ipbin, substr($this->lastentry, 6, 4)) < 0)
202 | return substr($this->lastentry, 4, 2);
203 |
204 | list(, , $pos, $len) = unpack('L3', $this->findIndex($ipbin));
205 | $data = $this->getDataEntry($pos, $len);
206 |
207 | $this->lastentry = $this->findRecord($data, $ipbin, 6);
208 |
209 | return substr($this->lastentry, 4, 2);
210 | }
211 |
212 | /**
213 | * Lookup an IP address and return the two-letter ISO country.
214 | */
215 | public function lookup($ip)
216 | {
217 | return $this->lookupbin(inet_pton($ip));
218 | }
219 | }
220 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 2.1, February 1999
3 |
4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc.
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | (This is the first released version of the Lesser GPL. It also counts
10 | as the successor of the GNU Library Public License, version 2, hence
11 | the version number 2.1.)
12 |
13 | Preamble
14 |
15 | The licenses for most software are designed to take away your
16 | freedom to share and change it. By contrast, the GNU General Public
17 | Licenses are intended to guarantee your freedom to share and change
18 | free software--to make sure the software is free for all its users.
19 |
20 | This license, the Lesser General Public License, applies to some
21 | specially designated software packages--typically libraries--of the
22 | Free Software Foundation and other authors who decide to use it. You
23 | can use it too, but we suggest you first think carefully about whether
24 | this license or the ordinary General Public License is the better
25 | strategy to use in any particular case, based on the explanations below.
26 |
27 | When we speak of free software, we are referring to freedom of use,
28 | not price. Our General Public Licenses are designed to make sure that
29 | you have the freedom to distribute copies of free software (and charge
30 | for this service if you wish); that you receive source code or can get
31 | it if you want it; that you can change the software and use pieces of
32 | it in new free programs; and that you are informed that you can do
33 | these things.
34 |
35 | To protect your rights, we need to make restrictions that forbid
36 | distributors to deny you these rights or to ask you to surrender these
37 | rights. These restrictions translate to certain responsibilities for
38 | you if you distribute copies of the library or if you modify it.
39 |
40 | For example, if you distribute copies of the library, whether gratis
41 | or for a fee, you must give the recipients all the rights that we gave
42 | you. You must make sure that they, too, receive or can get the source
43 | code. If you link other code with the library, you must provide
44 | complete object files to the recipients, so that they can relink them
45 | with the library after making changes to the library and recompiling
46 | it. And you must show them these terms so they know their rights.
47 |
48 | We protect your rights with a two-step method: (1) we copyright the
49 | library, and (2) we offer you this license, which gives you legal
50 | permission to copy, distribute and/or modify the library.
51 |
52 | To protect each distributor, we want to make it very clear that
53 | there is no warranty for the free library. Also, if the library is
54 | modified by someone else and passed on, the recipients should know
55 | that what they have is not the original version, so that the original
56 | author's reputation will not be affected by problems that might be
57 | introduced by others.
58 |
59 | Finally, software patents pose a constant threat to the existence of
60 | any free program. We wish to make sure that a company cannot
61 | effectively restrict the users of a free program by obtaining a
62 | restrictive license from a patent holder. Therefore, we insist that
63 | any patent license obtained for a version of the library must be
64 | consistent with the full freedom of use specified in this license.
65 |
66 | Most GNU software, including some libraries, is covered by the
67 | ordinary GNU General Public License. This license, the GNU Lesser
68 | General Public License, applies to certain designated libraries, and
69 | is quite different from the ordinary General Public License. We use
70 | this license for certain libraries in order to permit linking those
71 | libraries into non-free programs.
72 |
73 | When a program is linked with a library, whether statically or using
74 | a shared library, the combination of the two is legally speaking a
75 | combined work, a derivative of the original library. The ordinary
76 | General Public License therefore permits such linking only if the
77 | entire combination fits its criteria of freedom. The Lesser General
78 | Public License permits more lax criteria for linking other code with
79 | the library.
80 |
81 | We call this license the "Lesser" General Public License because it
82 | does Less to protect the user's freedom than the ordinary General
83 | Public License. It also provides other free software developers Less
84 | of an advantage over competing non-free programs. These disadvantages
85 | are the reason we use the ordinary General Public License for many
86 | libraries. However, the Lesser license provides advantages in certain
87 | special circumstances.
88 |
89 | For example, on rare occasions, there may be a special need to
90 | encourage the widest possible use of a certain library, so that it becomes
91 | a de-facto standard. To achieve this, non-free programs must be
92 | allowed to use the library. A more frequent case is that a free
93 | library does the same job as widely used non-free libraries. In this
94 | case, there is little to gain by limiting the free library to free
95 | software only, so we use the Lesser General Public License.
96 |
97 | In other cases, permission to use a particular library in non-free
98 | programs enables a greater number of people to use a large body of
99 | free software. For example, permission to use the GNU C Library in
100 | non-free programs enables many more people to use the whole GNU
101 | operating system, as well as its variant, the GNU/Linux operating
102 | system.
103 |
104 | Although the Lesser General Public License is Less protective of the
105 | users' freedom, it does ensure that the user of a program that is
106 | linked with the Library has the freedom and the wherewithal to run
107 | that program using a modified version of the Library.
108 |
109 | The precise terms and conditions for copying, distribution and
110 | modification follow. Pay close attention to the difference between a
111 | "work based on the library" and a "work that uses the library". The
112 | former contains code derived from the library, whereas the latter must
113 | be combined with the library in order to run.
114 |
115 | GNU LESSER GENERAL PUBLIC LICENSE
116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
117 |
118 | 0. This License Agreement applies to any software library or other
119 | program which contains a notice placed by the copyright holder or
120 | other authorized party saying it may be distributed under the terms of
121 | this Lesser General Public License (also called "this License").
122 | Each licensee is addressed as "you".
123 |
124 | A "library" means a collection of software functions and/or data
125 | prepared so as to be conveniently linked with application programs
126 | (which use some of those functions and data) to form executables.
127 |
128 | The "Library", below, refers to any such software library or work
129 | which has been distributed under these terms. A "work based on the
130 | Library" means either the Library or any derivative work under
131 | copyright law: that is to say, a work containing the Library or a
132 | portion of it, either verbatim or with modifications and/or translated
133 | straightforwardly into another language. (Hereinafter, translation is
134 | included without limitation in the term "modification".)
135 |
136 | "Source code" for a work means the preferred form of the work for
137 | making modifications to it. For a library, complete source code means
138 | all the source code for all modules it contains, plus any associated
139 | interface definition files, plus the scripts used to control compilation
140 | and installation of the library.
141 |
142 | Activities other than copying, distribution and modification are not
143 | covered by this License; they are outside its scope. The act of
144 | running a program using the Library is not restricted, and output from
145 | such a program is covered only if its contents constitute a work based
146 | on the Library (independent of the use of the Library in a tool for
147 | writing it). Whether that is true depends on what the Library does
148 | and what the program that uses the Library does.
149 |
150 | 1. You may copy and distribute verbatim copies of the Library's
151 | complete source code as you receive it, in any medium, provided that
152 | you conspicuously and appropriately publish on each copy an
153 | appropriate copyright notice and disclaimer of warranty; keep intact
154 | all the notices that refer to this License and to the absence of any
155 | warranty; and distribute a copy of this License along with the
156 | Library.
157 |
158 | You may charge a fee for the physical act of transferring a copy,
159 | and you may at your option offer warranty protection in exchange for a
160 | fee.
161 |
162 | 2. You may modify your copy or copies of the Library or any portion
163 | of it, thus forming a work based on the Library, and copy and
164 | distribute such modifications or work under the terms of Section 1
165 | above, provided that you also meet all of these conditions:
166 |
167 | a) The modified work must itself be a software library.
168 |
169 | b) You must cause the files modified to carry prominent notices
170 | stating that you changed the files and the date of any change.
171 |
172 | c) You must cause the whole of the work to be licensed at no
173 | charge to all third parties under the terms of this License.
174 |
175 | d) If a facility in the modified Library refers to a function or a
176 | table of data to be supplied by an application program that uses
177 | the facility, other than as an argument passed when the facility
178 | is invoked, then you must make a good faith effort to ensure that,
179 | in the event an application does not supply such function or
180 | table, the facility still operates, and performs whatever part of
181 | its purpose remains meaningful.
182 |
183 | (For example, a function in a library to compute square roots has
184 | a purpose that is entirely well-defined independent of the
185 | application. Therefore, Subsection 2d requires that any
186 | application-supplied function or table used by this function must
187 | be optional: if the application does not supply it, the square
188 | root function must still compute square roots.)
189 |
190 | These requirements apply to the modified work as a whole. If
191 | identifiable sections of that work are not derived from the Library,
192 | and can be reasonably considered independent and separate works in
193 | themselves, then this License, and its terms, do not apply to those
194 | sections when you distribute them as separate works. But when you
195 | distribute the same sections as part of a whole which is a work based
196 | on the Library, the distribution of the whole must be on the terms of
197 | this License, whose permissions for other licensees extend to the
198 | entire whole, and thus to each and every part regardless of who wrote
199 | it.
200 |
201 | Thus, it is not the intent of this section to claim rights or contest
202 | your rights to work written entirely by you; rather, the intent is to
203 | exercise the right to control the distribution of derivative or
204 | collective works based on the Library.
205 |
206 | In addition, mere aggregation of another work not based on the Library
207 | with the Library (or with a work based on the Library) on a volume of
208 | a storage or distribution medium does not bring the other work under
209 | the scope of this License.
210 |
211 | 3. You may opt to apply the terms of the ordinary GNU General Public
212 | License instead of this License to a given copy of the Library. To do
213 | this, you must alter all the notices that refer to this License, so
214 | that they refer to the ordinary GNU General Public License, version 2,
215 | instead of to this License. (If a newer version than version 2 of the
216 | ordinary GNU General Public License has appeared, then you can specify
217 | that version instead if you wish.) Do not make any other change in
218 | these notices.
219 |
220 | Once this change is made in a given copy, it is irreversible for
221 | that copy, so the ordinary GNU General Public License applies to all
222 | subsequent copies and derivative works made from that copy.
223 |
224 | This option is useful when you wish to copy part of the code of
225 | the Library into a program that is not a library.
226 |
227 | 4. You may copy and distribute the Library (or a portion or
228 | derivative of it, under Section 2) in object code or executable form
229 | under the terms of Sections 1 and 2 above provided that you accompany
230 | it with the complete corresponding machine-readable source code, which
231 | must be distributed under the terms of Sections 1 and 2 above on a
232 | medium customarily used for software interchange.
233 |
234 | If distribution of object code is made by offering access to copy
235 | from a designated place, then offering equivalent access to copy the
236 | source code from the same place satisfies the requirement to
237 | distribute the source code, even though third parties are not
238 | compelled to copy the source along with the object code.
239 |
240 | 5. A program that contains no derivative of any portion of the
241 | Library, but is designed to work with the Library by being compiled or
242 | linked with it, is called a "work that uses the Library". Such a
243 | work, in isolation, is not a derivative work of the Library, and
244 | therefore falls outside the scope of this License.
245 |
246 | However, linking a "work that uses the Library" with the Library
247 | creates an executable that is a derivative of the Library (because it
248 | contains portions of the Library), rather than a "work that uses the
249 | library". The executable is therefore covered by this License.
250 | Section 6 states terms for distribution of such executables.
251 |
252 | When a "work that uses the Library" uses material from a header file
253 | that is part of the Library, the object code for the work may be a
254 | derivative work of the Library even though the source code is not.
255 | Whether this is true is especially significant if the work can be
256 | linked without the Library, or if the work is itself a library. The
257 | threshold for this to be true is not precisely defined by law.
258 |
259 | If such an object file uses only numerical parameters, data
260 | structure layouts and accessors, and small macros and small inline
261 | functions (ten lines or less in length), then the use of the object
262 | file is unrestricted, regardless of whether it is legally a derivative
263 | work. (Executables containing this object code plus portions of the
264 | Library will still fall under Section 6.)
265 |
266 | Otherwise, if the work is a derivative of the Library, you may
267 | distribute the object code for the work under the terms of Section 6.
268 | Any executables containing that work also fall under Section 6,
269 | whether or not they are linked directly with the Library itself.
270 |
271 | 6. As an exception to the Sections above, you may also combine or
272 | link a "work that uses the Library" with the Library to produce a
273 | work containing portions of the Library, and distribute that work
274 | under terms of your choice, provided that the terms permit
275 | modification of the work for the customer's own use and reverse
276 | engineering for debugging such modifications.
277 |
278 | You must give prominent notice with each copy of the work that the
279 | Library is used in it and that the Library and its use are covered by
280 | this License. You must supply a copy of this License. If the work
281 | during execution displays copyright notices, you must include the
282 | copyright notice for the Library among them, as well as a reference
283 | directing the user to the copy of this License. Also, you must do one
284 | of these things:
285 |
286 | a) Accompany the work with the complete corresponding
287 | machine-readable source code for the Library including whatever
288 | changes were used in the work (which must be distributed under
289 | Sections 1 and 2 above); and, if the work is an executable linked
290 | with the Library, with the complete machine-readable "work that
291 | uses the Library", as object code and/or source code, so that the
292 | user can modify the Library and then relink to produce a modified
293 | executable containing the modified Library. (It is understood
294 | that the user who changes the contents of definitions files in the
295 | Library will not necessarily be able to recompile the application
296 | to use the modified definitions.)
297 |
298 | b) Use a suitable shared library mechanism for linking with the
299 | Library. A suitable mechanism is one that (1) uses at run time a
300 | copy of the library already present on the user's computer system,
301 | rather than copying library functions into the executable, and (2)
302 | will operate properly with a modified version of the library, if
303 | the user installs one, as long as the modified version is
304 | interface-compatible with the version that the work was made with.
305 |
306 | c) Accompany the work with a written offer, valid for at
307 | least three years, to give the same user the materials
308 | specified in Subsection 6a, above, for a charge no more
309 | than the cost of performing this distribution.
310 |
311 | d) If distribution of the work is made by offering access to copy
312 | from a designated place, offer equivalent access to copy the above
313 | specified materials from the same place.
314 |
315 | e) Verify that the user has already received a copy of these
316 | materials or that you have already sent this user a copy.
317 |
318 | For an executable, the required form of the "work that uses the
319 | Library" must include any data and utility programs needed for
320 | reproducing the executable from it. However, as a special exception,
321 | the materials to be distributed need not include anything that is
322 | normally distributed (in either source or binary form) with the major
323 | components (compiler, kernel, and so on) of the operating system on
324 | which the executable runs, unless that component itself accompanies
325 | the executable.
326 |
327 | It may happen that this requirement contradicts the license
328 | restrictions of other proprietary libraries that do not normally
329 | accompany the operating system. Such a contradiction means you cannot
330 | use both them and the Library together in an executable that you
331 | distribute.
332 |
333 | 7. You may place library facilities that are a work based on the
334 | Library side-by-side in a single library together with other library
335 | facilities not covered by this License, and distribute such a combined
336 | library, provided that the separate distribution of the work based on
337 | the Library and of the other library facilities is otherwise
338 | permitted, and provided that you do these two things:
339 |
340 | a) Accompany the combined library with a copy of the same work
341 | based on the Library, uncombined with any other library
342 | facilities. This must be distributed under the terms of the
343 | Sections above.
344 |
345 | b) Give prominent notice with the combined library of the fact
346 | that part of it is a work based on the Library, and explaining
347 | where to find the accompanying uncombined form of the same work.
348 |
349 | 8. You may not copy, modify, sublicense, link with, or distribute
350 | the Library except as expressly provided under this License. Any
351 | attempt otherwise to copy, modify, sublicense, link with, or
352 | distribute the Library is void, and will automatically terminate your
353 | rights under this License. However, parties who have received copies,
354 | or rights, from you under this License will not have their licenses
355 | terminated so long as such parties remain in full compliance.
356 |
357 | 9. You are not required to accept this License, since you have not
358 | signed it. However, nothing else grants you permission to modify or
359 | distribute the Library or its derivative works. These actions are
360 | prohibited by law if you do not accept this License. Therefore, by
361 | modifying or distributing the Library (or any work based on the
362 | Library), you indicate your acceptance of this License to do so, and
363 | all its terms and conditions for copying, distributing or modifying
364 | the Library or works based on it.
365 |
366 | 10. Each time you redistribute the Library (or any work based on the
367 | Library), the recipient automatically receives a license from the
368 | original licensor to copy, distribute, link with or modify the Library
369 | subject to these terms and conditions. You may not impose any further
370 | restrictions on the recipients' exercise of the rights granted herein.
371 | You are not responsible for enforcing compliance by third parties with
372 | this License.
373 |
374 | 11. If, as a consequence of a court judgment or allegation of patent
375 | infringement or for any other reason (not limited to patent issues),
376 | conditions are imposed on you (whether by court order, agreement or
377 | otherwise) that contradict the conditions of this License, they do not
378 | excuse you from the conditions of this License. If you cannot
379 | distribute so as to satisfy simultaneously your obligations under this
380 | License and any other pertinent obligations, then as a consequence you
381 | may not distribute the Library at all. For example, if a patent
382 | license would not permit royalty-free redistribution of the Library by
383 | all those who receive copies directly or indirectly through you, then
384 | the only way you could satisfy both it and this License would be to
385 | refrain entirely from distribution of the Library.
386 |
387 | If any portion of this section is held invalid or unenforceable under any
388 | particular circumstance, the balance of the section is intended to apply,
389 | and the section as a whole is intended to apply in other circumstances.
390 |
391 | It is not the purpose of this section to induce you to infringe any
392 | patents or other property right claims or to contest validity of any
393 | such claims; this section has the sole purpose of protecting the
394 | integrity of the free software distribution system which is
395 | implemented by public license practices. Many people have made
396 | generous contributions to the wide range of software distributed
397 | through that system in reliance on consistent application of that
398 | system; it is up to the author/donor to decide if he or she is willing
399 | to distribute software through any other system and a licensee cannot
400 | impose that choice.
401 |
402 | This section is intended to make thoroughly clear what is believed to
403 | be a consequence of the rest of this License.
404 |
405 | 12. If the distribution and/or use of the Library is restricted in
406 | certain countries either by patents or by copyrighted interfaces, the
407 | original copyright holder who places the Library under this License may add
408 | an explicit geographical distribution limitation excluding those countries,
409 | so that distribution is permitted only in or among countries not thus
410 | excluded. In such case, this License incorporates the limitation as if
411 | written in the body of this License.
412 |
413 | 13. The Free Software Foundation may publish revised and/or new
414 | versions of the Lesser General Public License from time to time.
415 | Such new versions will be similar in spirit to the present version,
416 | but may differ in detail to address new problems or concerns.
417 |
418 | Each version is given a distinguishing version number. If the Library
419 | specifies a version number of this License which applies to it and
420 | "any later version", you have the option of following the terms and
421 | conditions either of that version or of any later version published by
422 | the Free Software Foundation. If the Library does not specify a
423 | license version number, you may choose any version ever published by
424 | the Free Software Foundation.
425 |
426 | 14. If you wish to incorporate parts of the Library into other free
427 | programs whose distribution conditions are incompatible with these,
428 | write to the author to ask for permission. For software which is
429 | copyrighted by the Free Software Foundation, write to the Free
430 | Software Foundation; we sometimes make exceptions for this. Our
431 | decision will be guided by the two goals of preserving the free status
432 | of all derivatives of our free software and of promoting the sharing
433 | and reuse of software generally.
434 |
435 | NO WARRANTY
436 |
437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
446 |
447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
456 | DAMAGES.
457 |
458 | END OF TERMS AND CONDITIONS
459 |
460 | How to Apply These Terms to Your New Libraries
461 |
462 | If you develop a new library, and you want it to be of the greatest
463 | possible use to the public, we recommend making it free software that
464 | everyone can redistribute and change. You can do so by permitting
465 | redistribution under these terms (or, alternatively, under the terms of the
466 | ordinary General Public License).
467 |
468 | To apply these terms, attach the following notices to the library. It is
469 | safest to attach them to the start of each source file to most effectively
470 | convey the exclusion of warranty; and each file should have at least the
471 | "copyright" line and a pointer to where the full notice is found.
472 |
473 | {description}
474 | Copyright (C) {year} {fullname}
475 |
476 | This library is free software; you can redistribute it and/or
477 | modify it under the terms of the GNU Lesser General Public
478 | License as published by the Free Software Foundation; either
479 | version 2.1 of the License, or (at your option) any later version.
480 |
481 | This library is distributed in the hope that it will be useful,
482 | but WITHOUT ANY WARRANTY; without even the implied warranty of
483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
484 | Lesser General Public License for more details.
485 |
486 | You should have received a copy of the GNU Lesser General Public
487 | License along with this library; if not, write to the Free Software
488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
489 | USA
490 |
491 | Also add information on how to contact you by electronic and paper mail.
492 |
493 | You should also get your employer (if you work as a programmer) or your
494 | school, if any, to sign a "copyright disclaimer" for the library, if
495 | necessary. Here is a sample; alter the names:
496 |
497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the
498 | library `Frob' (a library for tweaking knobs) written by James Random
499 | Hacker.
500 |
501 | {signature of Ty Coon}, 1 April 1990
502 | Ty Coon, President of Vice
503 |
504 | That's all there is to it!
505 |
506 |
--------------------------------------------------------------------------------
/var/geoip/geoip.inc:
--------------------------------------------------------------------------------
1 | 0,
84 | "AP" => 1,
85 | "EU" => 2,
86 | "AD" => 3,
87 | "AE" => 4,
88 | "AF" => 5,
89 | "AG" => 6,
90 | "AI" => 7,
91 | "AL" => 8,
92 | "AM" => 9,
93 | "CW" => 10,
94 | "AO" => 11,
95 | "AQ" => 12,
96 | "AR" => 13,
97 | "AS" => 14,
98 | "AT" => 15,
99 | "AU" => 16,
100 | "AW" => 17,
101 | "AZ" => 18,
102 | "BA" => 19,
103 | "BB" => 20,
104 | "BD" => 21,
105 | "BE" => 22,
106 | "BF" => 23,
107 | "BG" => 24,
108 | "BH" => 25,
109 | "BI" => 26,
110 | "BJ" => 27,
111 | "BM" => 28,
112 | "BN" => 29,
113 | "BO" => 30,
114 | "BR" => 31,
115 | "BS" => 32,
116 | "BT" => 33,
117 | "BV" => 34,
118 | "BW" => 35,
119 | "BY" => 36,
120 | "BZ" => 37,
121 | "CA" => 38,
122 | "CC" => 39,
123 | "CD" => 40,
124 | "CF" => 41,
125 | "CG" => 42,
126 | "CH" => 43,
127 | "CI" => 44,
128 | "CK" => 45,
129 | "CL" => 46,
130 | "CM" => 47,
131 | "CN" => 48,
132 | "CO" => 49,
133 | "CR" => 50,
134 | "CU" => 51,
135 | "CV" => 52,
136 | "CX" => 53,
137 | "CY" => 54,
138 | "CZ" => 55,
139 | "DE" => 56,
140 | "DJ" => 57,
141 | "DK" => 58,
142 | "DM" => 59,
143 | "DO" => 60,
144 | "DZ" => 61,
145 | "EC" => 62,
146 | "EE" => 63,
147 | "EG" => 64,
148 | "EH" => 65,
149 | "ER" => 66,
150 | "ES" => 67,
151 | "ET" => 68,
152 | "FI" => 69,
153 | "FJ" => 70,
154 | "FK" => 71,
155 | "FM" => 72,
156 | "FO" => 73,
157 | "FR" => 74,
158 | "SX" => 75,
159 | "GA" => 76,
160 | "GB" => 77,
161 | "GD" => 78,
162 | "GE" => 79,
163 | "GF" => 80,
164 | "GH" => 81,
165 | "GI" => 82,
166 | "GL" => 83,
167 | "GM" => 84,
168 | "GN" => 85,
169 | "GP" => 86,
170 | "GQ" => 87,
171 | "GR" => 88,
172 | "GS" => 89,
173 | "GT" => 90,
174 | "GU" => 91,
175 | "GW" => 92,
176 | "GY" => 93,
177 | "HK" => 94,
178 | "HM" => 95,
179 | "HN" => 96,
180 | "HR" => 97,
181 | "HT" => 98,
182 | "HU" => 99,
183 | "ID" => 100,
184 | "IE" => 101,
185 | "IL" => 102,
186 | "IN" => 103,
187 | "IO" => 104,
188 | "IQ" => 105,
189 | "IR" => 106,
190 | "IS" => 107,
191 | "IT" => 108,
192 | "JM" => 109,
193 | "JO" => 110,
194 | "JP" => 111,
195 | "KE" => 112,
196 | "KG" => 113,
197 | "KH" => 114,
198 | "KI" => 115,
199 | "KM" => 116,
200 | "KN" => 117,
201 | "KP" => 118,
202 | "KR" => 119,
203 | "KW" => 120,
204 | "KY" => 121,
205 | "KZ" => 122,
206 | "LA" => 123,
207 | "LB" => 124,
208 | "LC" => 125,
209 | "LI" => 126,
210 | "LK" => 127,
211 | "LR" => 128,
212 | "LS" => 129,
213 | "LT" => 130,
214 | "LU" => 131,
215 | "LV" => 132,
216 | "LY" => 133,
217 | "MA" => 134,
218 | "MC" => 135,
219 | "MD" => 136,
220 | "MG" => 137,
221 | "MH" => 138,
222 | "MK" => 139,
223 | "ML" => 140,
224 | "MM" => 141,
225 | "MN" => 142,
226 | "MO" => 143,
227 | "MP" => 144,
228 | "MQ" => 145,
229 | "MR" => 146,
230 | "MS" => 147,
231 | "MT" => 148,
232 | "MU" => 149,
233 | "MV" => 150,
234 | "MW" => 151,
235 | "MX" => 152,
236 | "MY" => 153,
237 | "MZ" => 154,
238 | "NA" => 155,
239 | "NC" => 156,
240 | "NE" => 157,
241 | "NF" => 158,
242 | "NG" => 159,
243 | "NI" => 160,
244 | "NL" => 161,
245 | "NO" => 162,
246 | "NP" => 163,
247 | "NR" => 164,
248 | "NU" => 165,
249 | "NZ" => 166,
250 | "OM" => 167,
251 | "PA" => 168,
252 | "PE" => 169,
253 | "PF" => 170,
254 | "PG" => 171,
255 | "PH" => 172,
256 | "PK" => 173,
257 | "PL" => 174,
258 | "PM" => 175,
259 | "PN" => 176,
260 | "PR" => 177,
261 | "PS" => 178,
262 | "PT" => 179,
263 | "PW" => 180,
264 | "PY" => 181,
265 | "QA" => 182,
266 | "RE" => 183,
267 | "RO" => 184,
268 | "RU" => 185,
269 | "RW" => 186,
270 | "SA" => 187,
271 | "SB" => 188,
272 | "SC" => 189,
273 | "SD" => 190,
274 | "SE" => 191,
275 | "SG" => 192,
276 | "SH" => 193,
277 | "SI" => 194,
278 | "SJ" => 195,
279 | "SK" => 196,
280 | "SL" => 197,
281 | "SM" => 198,
282 | "SN" => 199,
283 | "SO" => 200,
284 | "SR" => 201,
285 | "ST" => 202,
286 | "SV" => 203,
287 | "SY" => 204,
288 | "SZ" => 205,
289 | "TC" => 206,
290 | "TD" => 207,
291 | "TF" => 208,
292 | "TG" => 209,
293 | "TH" => 210,
294 | "TJ" => 211,
295 | "TK" => 212,
296 | "TM" => 213,
297 | "TN" => 214,
298 | "TO" => 215,
299 | "TL" => 216,
300 | "TR" => 217,
301 | "TT" => 218,
302 | "TV" => 219,
303 | "TW" => 220,
304 | "TZ" => 221,
305 | "UA" => 222,
306 | "UG" => 223,
307 | "UM" => 224,
308 | "US" => 225,
309 | "UY" => 226,
310 | "UZ" => 227,
311 | "VA" => 228,
312 | "VC" => 229,
313 | "VE" => 230,
314 | "VG" => 231,
315 | "VI" => 232,
316 | "VN" => 233,
317 | "VU" => 234,
318 | "WF" => 235,
319 | "WS" => 236,
320 | "YE" => 237,
321 | "YT" => 238,
322 | "RS" => 239,
323 | "ZA" => 240,
324 | "ZM" => 241,
325 | "ME" => 242,
326 | "ZW" => 243,
327 | "A1" => 244,
328 | "A2" => 245,
329 | "O1" => 246,
330 | "AX" => 247,
331 | "GG" => 248,
332 | "IM" => 249,
333 | "JE" => 250,
334 | "BL" => 251,
335 | "MF" => 252,
336 | "BQ" => 253,
337 | "SS" => 254
338 | );
339 |
340 | public $GEOIP_COUNTRY_CODES = array(
341 | "",
342 | "AP",
343 | "EU",
344 | "AD",
345 | "AE",
346 | "AF",
347 | "AG",
348 | "AI",
349 | "AL",
350 | "AM",
351 | "CW",
352 | "AO",
353 | "AQ",
354 | "AR",
355 | "AS",
356 | "AT",
357 | "AU",
358 | "AW",
359 | "AZ",
360 | "BA",
361 | "BB",
362 | "BD",
363 | "BE",
364 | "BF",
365 | "BG",
366 | "BH",
367 | "BI",
368 | "BJ",
369 | "BM",
370 | "BN",
371 | "BO",
372 | "BR",
373 | "BS",
374 | "BT",
375 | "BV",
376 | "BW",
377 | "BY",
378 | "BZ",
379 | "CA",
380 | "CC",
381 | "CD",
382 | "CF",
383 | "CG",
384 | "CH",
385 | "CI",
386 | "CK",
387 | "CL",
388 | "CM",
389 | "CN",
390 | "CO",
391 | "CR",
392 | "CU",
393 | "CV",
394 | "CX",
395 | "CY",
396 | "CZ",
397 | "DE",
398 | "DJ",
399 | "DK",
400 | "DM",
401 | "DO",
402 | "DZ",
403 | "EC",
404 | "EE",
405 | "EG",
406 | "EH",
407 | "ER",
408 | "ES",
409 | "ET",
410 | "FI",
411 | "FJ",
412 | "FK",
413 | "FM",
414 | "FO",
415 | "FR",
416 | "SX",
417 | "GA",
418 | "GB",
419 | "GD",
420 | "GE",
421 | "GF",
422 | "GH",
423 | "GI",
424 | "GL",
425 | "GM",
426 | "GN",
427 | "GP",
428 | "GQ",
429 | "GR",
430 | "GS",
431 | "GT",
432 | "GU",
433 | "GW",
434 | "GY",
435 | "HK",
436 | "HM",
437 | "HN",
438 | "HR",
439 | "HT",
440 | "HU",
441 | "ID",
442 | "IE",
443 | "IL",
444 | "IN",
445 | "IO",
446 | "IQ",
447 | "IR",
448 | "IS",
449 | "IT",
450 | "JM",
451 | "JO",
452 | "JP",
453 | "KE",
454 | "KG",
455 | "KH",
456 | "KI",
457 | "KM",
458 | "KN",
459 | "KP",
460 | "KR",
461 | "KW",
462 | "KY",
463 | "KZ",
464 | "LA",
465 | "LB",
466 | "LC",
467 | "LI",
468 | "LK",
469 | "LR",
470 | "LS",
471 | "LT",
472 | "LU",
473 | "LV",
474 | "LY",
475 | "MA",
476 | "MC",
477 | "MD",
478 | "MG",
479 | "MH",
480 | "MK",
481 | "ML",
482 | "MM",
483 | "MN",
484 | "MO",
485 | "MP",
486 | "MQ",
487 | "MR",
488 | "MS",
489 | "MT",
490 | "MU",
491 | "MV",
492 | "MW",
493 | "MX",
494 | "MY",
495 | "MZ",
496 | "NA",
497 | "NC",
498 | "NE",
499 | "NF",
500 | "NG",
501 | "NI",
502 | "NL",
503 | "NO",
504 | "NP",
505 | "NR",
506 | "NU",
507 | "NZ",
508 | "OM",
509 | "PA",
510 | "PE",
511 | "PF",
512 | "PG",
513 | "PH",
514 | "PK",
515 | "PL",
516 | "PM",
517 | "PN",
518 | "PR",
519 | "PS",
520 | "PT",
521 | "PW",
522 | "PY",
523 | "QA",
524 | "RE",
525 | "RO",
526 | "RU",
527 | "RW",
528 | "SA",
529 | "SB",
530 | "SC",
531 | "SD",
532 | "SE",
533 | "SG",
534 | "SH",
535 | "SI",
536 | "SJ",
537 | "SK",
538 | "SL",
539 | "SM",
540 | "SN",
541 | "SO",
542 | "SR",
543 | "ST",
544 | "SV",
545 | "SY",
546 | "SZ",
547 | "TC",
548 | "TD",
549 | "TF",
550 | "TG",
551 | "TH",
552 | "TJ",
553 | "TK",
554 | "TM",
555 | "TN",
556 | "TO",
557 | "TL",
558 | "TR",
559 | "TT",
560 | "TV",
561 | "TW",
562 | "TZ",
563 | "UA",
564 | "UG",
565 | "UM",
566 | "US",
567 | "UY",
568 | "UZ",
569 | "VA",
570 | "VC",
571 | "VE",
572 | "VG",
573 | "VI",
574 | "VN",
575 | "VU",
576 | "WF",
577 | "WS",
578 | "YE",
579 | "YT",
580 | "RS",
581 | "ZA",
582 | "ZM",
583 | "ME",
584 | "ZW",
585 | "A1",
586 | "A2",
587 | "O1",
588 | "AX",
589 | "GG",
590 | "IM",
591 | "JE",
592 | "BL",
593 | "MF",
594 | "BQ",
595 | "SS",
596 | "O1"
597 | );
598 |
599 | public $GEOIP_COUNTRY_CODES3 = array(
600 | "",
601 | "AP",
602 | "EU",
603 | "AND",
604 | "ARE",
605 | "AFG",
606 | "ATG",
607 | "AIA",
608 | "ALB",
609 | "ARM",
610 | "CUW",
611 | "AGO",
612 | "ATA",
613 | "ARG",
614 | "ASM",
615 | "AUT",
616 | "AUS",
617 | "ABW",
618 | "AZE",
619 | "BIH",
620 | "BRB",
621 | "BGD",
622 | "BEL",
623 | "BFA",
624 | "BGR",
625 | "BHR",
626 | "BDI",
627 | "BEN",
628 | "BMU",
629 | "BRN",
630 | "BOL",
631 | "BRA",
632 | "BHS",
633 | "BTN",
634 | "BVT",
635 | "BWA",
636 | "BLR",
637 | "BLZ",
638 | "CAN",
639 | "CCK",
640 | "COD",
641 | "CAF",
642 | "COG",
643 | "CHE",
644 | "CIV",
645 | "COK",
646 | "CHL",
647 | "CMR",
648 | "CHN",
649 | "COL",
650 | "CRI",
651 | "CUB",
652 | "CPV",
653 | "CXR",
654 | "CYP",
655 | "CZE",
656 | "DEU",
657 | "DJI",
658 | "DNK",
659 | "DMA",
660 | "DOM",
661 | "DZA",
662 | "ECU",
663 | "EST",
664 | "EGY",
665 | "ESH",
666 | "ERI",
667 | "ESP",
668 | "ETH",
669 | "FIN",
670 | "FJI",
671 | "FLK",
672 | "FSM",
673 | "FRO",
674 | "FRA",
675 | "SXM",
676 | "GAB",
677 | "GBR",
678 | "GRD",
679 | "GEO",
680 | "GUF",
681 | "GHA",
682 | "GIB",
683 | "GRL",
684 | "GMB",
685 | "GIN",
686 | "GLP",
687 | "GNQ",
688 | "GRC",
689 | "SGS",
690 | "GTM",
691 | "GUM",
692 | "GNB",
693 | "GUY",
694 | "HKG",
695 | "HMD",
696 | "HND",
697 | "HRV",
698 | "HTI",
699 | "HUN",
700 | "IDN",
701 | "IRL",
702 | "ISR",
703 | "IND",
704 | "IOT",
705 | "IRQ",
706 | "IRN",
707 | "ISL",
708 | "ITA",
709 | "JAM",
710 | "JOR",
711 | "JPN",
712 | "KEN",
713 | "KGZ",
714 | "KHM",
715 | "KIR",
716 | "COM",
717 | "KNA",
718 | "PRK",
719 | "KOR",
720 | "KWT",
721 | "CYM",
722 | "KAZ",
723 | "LAO",
724 | "LBN",
725 | "LCA",
726 | "LIE",
727 | "LKA",
728 | "LBR",
729 | "LSO",
730 | "LTU",
731 | "LUX",
732 | "LVA",
733 | "LBY",
734 | "MAR",
735 | "MCO",
736 | "MDA",
737 | "MDG",
738 | "MHL",
739 | "MKD",
740 | "MLI",
741 | "MMR",
742 | "MNG",
743 | "MAC",
744 | "MNP",
745 | "MTQ",
746 | "MRT",
747 | "MSR",
748 | "MLT",
749 | "MUS",
750 | "MDV",
751 | "MWI",
752 | "MEX",
753 | "MYS",
754 | "MOZ",
755 | "NAM",
756 | "NCL",
757 | "NER",
758 | "NFK",
759 | "NGA",
760 | "NIC",
761 | "NLD",
762 | "NOR",
763 | "NPL",
764 | "NRU",
765 | "NIU",
766 | "NZL",
767 | "OMN",
768 | "PAN",
769 | "PER",
770 | "PYF",
771 | "PNG",
772 | "PHL",
773 | "PAK",
774 | "POL",
775 | "SPM",
776 | "PCN",
777 | "PRI",
778 | "PSE",
779 | "PRT",
780 | "PLW",
781 | "PRY",
782 | "QAT",
783 | "REU",
784 | "ROU",
785 | "RUS",
786 | "RWA",
787 | "SAU",
788 | "SLB",
789 | "SYC",
790 | "SDN",
791 | "SWE",
792 | "SGP",
793 | "SHN",
794 | "SVN",
795 | "SJM",
796 | "SVK",
797 | "SLE",
798 | "SMR",
799 | "SEN",
800 | "SOM",
801 | "SUR",
802 | "STP",
803 | "SLV",
804 | "SYR",
805 | "SWZ",
806 | "TCA",
807 | "TCD",
808 | "ATF",
809 | "TGO",
810 | "THA",
811 | "TJK",
812 | "TKL",
813 | "TKM",
814 | "TUN",
815 | "TON",
816 | "TLS",
817 | "TUR",
818 | "TTO",
819 | "TUV",
820 | "TWN",
821 | "TZA",
822 | "UKR",
823 | "UGA",
824 | "UMI",
825 | "USA",
826 | "URY",
827 | "UZB",
828 | "VAT",
829 | "VCT",
830 | "VEN",
831 | "VGB",
832 | "VIR",
833 | "VNM",
834 | "VUT",
835 | "WLF",
836 | "WSM",
837 | "YEM",
838 | "MYT",
839 | "SRB",
840 | "ZAF",
841 | "ZMB",
842 | "MNE",
843 | "ZWE",
844 | "A1",
845 | "A2",
846 | "O1",
847 | "ALA",
848 | "GGY",
849 | "IMN",
850 | "JEY",
851 | "BLM",
852 | "MAF",
853 | "BES",
854 | "SSD",
855 | "O1"
856 | );
857 |
858 | public $GEOIP_COUNTRY_NAMES = array(
859 | "",
860 | "Asia/Pacific Region",
861 | "Europe",
862 | "Andorra",
863 | "United Arab Emirates",
864 | "Afghanistan",
865 | "Antigua and Barbuda",
866 | "Anguilla",
867 | "Albania",
868 | "Armenia",
869 | "Curacao",
870 | "Angola",
871 | "Antarctica",
872 | "Argentina",
873 | "American Samoa",
874 | "Austria",
875 | "Australia",
876 | "Aruba",
877 | "Azerbaijan",
878 | "Bosnia and Herzegovina",
879 | "Barbados",
880 | "Bangladesh",
881 | "Belgium",
882 | "Burkina Faso",
883 | "Bulgaria",
884 | "Bahrain",
885 | "Burundi",
886 | "Benin",
887 | "Bermuda",
888 | "Brunei Darussalam",
889 | "Bolivia",
890 | "Brazil",
891 | "Bahamas",
892 | "Bhutan",
893 | "Bouvet Island",
894 | "Botswana",
895 | "Belarus",
896 | "Belize",
897 | "Canada",
898 | "Cocos (Keeling) Islands",
899 | "Congo, The Democratic Republic of the",
900 | "Central African Republic",
901 | "Congo",
902 | "Switzerland",
903 | "Cote D'Ivoire",
904 | "Cook Islands",
905 | "Chile",
906 | "Cameroon",
907 | "China",
908 | "Colombia",
909 | "Costa Rica",
910 | "Cuba",
911 | "Cape Verde",
912 | "Christmas Island",
913 | "Cyprus",
914 | "Czech Republic",
915 | "Germany",
916 | "Djibouti",
917 | "Denmark",
918 | "Dominica",
919 | "Dominican Republic",
920 | "Algeria",
921 | "Ecuador",
922 | "Estonia",
923 | "Egypt",
924 | "Western Sahara",
925 | "Eritrea",
926 | "Spain",
927 | "Ethiopia",
928 | "Finland",
929 | "Fiji",
930 | "Falkland Islands (Malvinas)",
931 | "Micronesia, Federated States of",
932 | "Faroe Islands",
933 | "France",
934 | "Sint Maarten (Dutch part)",
935 | "Gabon",
936 | "United Kingdom",
937 | "Grenada",
938 | "Georgia",
939 | "French Guiana",
940 | "Ghana",
941 | "Gibraltar",
942 | "Greenland",
943 | "Gambia",
944 | "Guinea",
945 | "Guadeloupe",
946 | "Equatorial Guinea",
947 | "Greece",
948 | "South Georgia and the South Sandwich Islands",
949 | "Guatemala",
950 | "Guam",
951 | "Guinea-Bissau",
952 | "Guyana",
953 | "Hong Kong",
954 | "Heard Island and McDonald Islands",
955 | "Honduras",
956 | "Croatia",
957 | "Haiti",
958 | "Hungary",
959 | "Indonesia",
960 | "Ireland",
961 | "Israel",
962 | "India",
963 | "British Indian Ocean Territory",
964 | "Iraq",
965 | "Iran, Islamic Republic of",
966 | "Iceland",
967 | "Italy",
968 | "Jamaica",
969 | "Jordan",
970 | "Japan",
971 | "Kenya",
972 | "Kyrgyzstan",
973 | "Cambodia",
974 | "Kiribati",
975 | "Comoros",
976 | "Saint Kitts and Nevis",
977 | "Korea, Democratic People's Republic of",
978 | "Korea, Republic of",
979 | "Kuwait",
980 | "Cayman Islands",
981 | "Kazakhstan",
982 | "Lao People's Democratic Republic",
983 | "Lebanon",
984 | "Saint Lucia",
985 | "Liechtenstein",
986 | "Sri Lanka",
987 | "Liberia",
988 | "Lesotho",
989 | "Lithuania",
990 | "Luxembourg",
991 | "Latvia",
992 | "Libya",
993 | "Morocco",
994 | "Monaco",
995 | "Moldova, Republic of",
996 | "Madagascar",
997 | "Marshall Islands",
998 | "Macedonia",
999 | "Mali",
1000 | "Myanmar",
1001 | "Mongolia",
1002 | "Macau",
1003 | "Northern Mariana Islands",
1004 | "Martinique",
1005 | "Mauritania",
1006 | "Montserrat",
1007 | "Malta",
1008 | "Mauritius",
1009 | "Maldives",
1010 | "Malawi",
1011 | "Mexico",
1012 | "Malaysia",
1013 | "Mozambique",
1014 | "Namibia",
1015 | "New Caledonia",
1016 | "Niger",
1017 | "Norfolk Island",
1018 | "Nigeria",
1019 | "Nicaragua",
1020 | "Netherlands",
1021 | "Norway",
1022 | "Nepal",
1023 | "Nauru",
1024 | "Niue",
1025 | "New Zealand",
1026 | "Oman",
1027 | "Panama",
1028 | "Peru",
1029 | "French Polynesia",
1030 | "Papua New Guinea",
1031 | "Philippines",
1032 | "Pakistan",
1033 | "Poland",
1034 | "Saint Pierre and Miquelon",
1035 | "Pitcairn Islands",
1036 | "Puerto Rico",
1037 | "Palestinian Territory",
1038 | "Portugal",
1039 | "Palau",
1040 | "Paraguay",
1041 | "Qatar",
1042 | "Reunion",
1043 | "Romania",
1044 | "Russian Federation",
1045 | "Rwanda",
1046 | "Saudi Arabia",
1047 | "Solomon Islands",
1048 | "Seychelles",
1049 | "Sudan",
1050 | "Sweden",
1051 | "Singapore",
1052 | "Saint Helena",
1053 | "Slovenia",
1054 | "Svalbard and Jan Mayen",
1055 | "Slovakia",
1056 | "Sierra Leone",
1057 | "San Marino",
1058 | "Senegal",
1059 | "Somalia",
1060 | "Suriname",
1061 | "Sao Tome and Principe",
1062 | "El Salvador",
1063 | "Syrian Arab Republic",
1064 | "Swaziland",
1065 | "Turks and Caicos Islands",
1066 | "Chad",
1067 | "French Southern Territories",
1068 | "Togo",
1069 | "Thailand",
1070 | "Tajikistan",
1071 | "Tokelau",
1072 | "Turkmenistan",
1073 | "Tunisia",
1074 | "Tonga",
1075 | "Timor-Leste",
1076 | "Turkey",
1077 | "Trinidad and Tobago",
1078 | "Tuvalu",
1079 | "Taiwan",
1080 | "Tanzania, United Republic of",
1081 | "Ukraine",
1082 | "Uganda",
1083 | "United States Minor Outlying Islands",
1084 | "United States",
1085 | "Uruguay",
1086 | "Uzbekistan",
1087 | "Holy See (Vatican City State)",
1088 | "Saint Vincent and the Grenadines",
1089 | "Venezuela",
1090 | "Virgin Islands, British",
1091 | "Virgin Islands, U.S.",
1092 | "Vietnam",
1093 | "Vanuatu",
1094 | "Wallis and Futuna",
1095 | "Samoa",
1096 | "Yemen",
1097 | "Mayotte",
1098 | "Serbia",
1099 | "South Africa",
1100 | "Zambia",
1101 | "Montenegro",
1102 | "Zimbabwe",
1103 | "Anonymous Proxy",
1104 | "Satellite Provider",
1105 | "Other",
1106 | "Aland Islands",
1107 | "Guernsey",
1108 | "Isle of Man",
1109 | "Jersey",
1110 | "Saint Barthelemy",
1111 | "Saint Martin",
1112 | "Bonaire, Saint Eustatius and Saba",
1113 | "South Sudan",
1114 | "Other"
1115 | );
1116 |
1117 | public $GEOIP_CONTINENT_CODES = array(
1118 | "--",
1119 | "AS",
1120 | "EU",
1121 | "EU",
1122 | "AS",
1123 | "AS",
1124 | "NA",
1125 | "NA",
1126 | "EU",
1127 | "AS",
1128 | "NA",
1129 | "AF",
1130 | "AN",
1131 | "SA",
1132 | "OC",
1133 | "EU",
1134 | "OC",
1135 | "NA",
1136 | "AS",
1137 | "EU",
1138 | "NA",
1139 | "AS",
1140 | "EU",
1141 | "AF",
1142 | "EU",
1143 | "AS",
1144 | "AF",
1145 | "AF",
1146 | "NA",
1147 | "AS",
1148 | "SA",
1149 | "SA",
1150 | "NA",
1151 | "AS",
1152 | "AN",
1153 | "AF",
1154 | "EU",
1155 | "NA",
1156 | "NA",
1157 | "AS",
1158 | "AF",
1159 | "AF",
1160 | "AF",
1161 | "EU",
1162 | "AF",
1163 | "OC",
1164 | "SA",
1165 | "AF",
1166 | "AS",
1167 | "SA",
1168 | "NA",
1169 | "NA",
1170 | "AF",
1171 | "AS",
1172 | "AS",
1173 | "EU",
1174 | "EU",
1175 | "AF",
1176 | "EU",
1177 | "NA",
1178 | "NA",
1179 | "AF",
1180 | "SA",
1181 | "EU",
1182 | "AF",
1183 | "AF",
1184 | "AF",
1185 | "EU",
1186 | "AF",
1187 | "EU",
1188 | "OC",
1189 | "SA",
1190 | "OC",
1191 | "EU",
1192 | "EU",
1193 | "NA",
1194 | "AF",
1195 | "EU",
1196 | "NA",
1197 | "AS",
1198 | "SA",
1199 | "AF",
1200 | "EU",
1201 | "NA",
1202 | "AF",
1203 | "AF",
1204 | "NA",
1205 | "AF",
1206 | "EU",
1207 | "AN",
1208 | "NA",
1209 | "OC",
1210 | "AF",
1211 | "SA",
1212 | "AS",
1213 | "AN",
1214 | "NA",
1215 | "EU",
1216 | "NA",
1217 | "EU",
1218 | "AS",
1219 | "EU",
1220 | "AS",
1221 | "AS",
1222 | "AS",
1223 | "AS",
1224 | "AS",
1225 | "EU",
1226 | "EU",
1227 | "NA",
1228 | "AS",
1229 | "AS",
1230 | "AF",
1231 | "AS",
1232 | "AS",
1233 | "OC",
1234 | "AF",
1235 | "NA",
1236 | "AS",
1237 | "AS",
1238 | "AS",
1239 | "NA",
1240 | "AS",
1241 | "AS",
1242 | "AS",
1243 | "NA",
1244 | "EU",
1245 | "AS",
1246 | "AF",
1247 | "AF",
1248 | "EU",
1249 | "EU",
1250 | "EU",
1251 | "AF",
1252 | "AF",
1253 | "EU",
1254 | "EU",
1255 | "AF",
1256 | "OC",
1257 | "EU",
1258 | "AF",
1259 | "AS",
1260 | "AS",
1261 | "AS",
1262 | "OC",
1263 | "NA",
1264 | "AF",
1265 | "NA",
1266 | "EU",
1267 | "AF",
1268 | "AS",
1269 | "AF",
1270 | "NA",
1271 | "AS",
1272 | "AF",
1273 | "AF",
1274 | "OC",
1275 | "AF",
1276 | "OC",
1277 | "AF",
1278 | "NA",
1279 | "EU",
1280 | "EU",
1281 | "AS",
1282 | "OC",
1283 | "OC",
1284 | "OC",
1285 | "AS",
1286 | "NA",
1287 | "SA",
1288 | "OC",
1289 | "OC",
1290 | "AS",
1291 | "AS",
1292 | "EU",
1293 | "NA",
1294 | "OC",
1295 | "NA",
1296 | "AS",
1297 | "EU",
1298 | "OC",
1299 | "SA",
1300 | "AS",
1301 | "AF",
1302 | "EU",
1303 | "EU",
1304 | "AF",
1305 | "AS",
1306 | "OC",
1307 | "AF",
1308 | "AF",
1309 | "EU",
1310 | "AS",
1311 | "AF",
1312 | "EU",
1313 | "EU",
1314 | "EU",
1315 | "AF",
1316 | "EU",
1317 | "AF",
1318 | "AF",
1319 | "SA",
1320 | "AF",
1321 | "NA",
1322 | "AS",
1323 | "AF",
1324 | "NA",
1325 | "AF",
1326 | "AN",
1327 | "AF",
1328 | "AS",
1329 | "AS",
1330 | "OC",
1331 | "AS",
1332 | "AF",
1333 | "OC",
1334 | "AS",
1335 | "EU",
1336 | "NA",
1337 | "OC",
1338 | "AS",
1339 | "AF",
1340 | "EU",
1341 | "AF",
1342 | "OC",
1343 | "NA",
1344 | "SA",
1345 | "AS",
1346 | "EU",
1347 | "NA",
1348 | "SA",
1349 | "NA",
1350 | "NA",
1351 | "AS",
1352 | "OC",
1353 | "OC",
1354 | "OC",
1355 | "AS",
1356 | "AF",
1357 | "EU",
1358 | "AF",
1359 | "AF",
1360 | "EU",
1361 | "AF",
1362 | "--",
1363 | "--",
1364 | "--",
1365 | "EU",
1366 | "EU",
1367 | "EU",
1368 | "EU",
1369 | "NA",
1370 | "NA",
1371 | "NA",
1372 | "AF",
1373 | "--"
1374 | );
1375 |
1376 | /**
1377 | * Added for Chapagain_AutoCurrency Magento Module
1378 | * GEOIP_COUNTRY_CODE_TO_CURRENCY
1379 | */
1380 | public $GEOIP_COUNTRY_CODE_TO_CURRENCY = array(
1381 | "EU" => "EUR", "AD" => "EUR", "AE" => "AED", "AF" => "AFN", "AG" => "XCD", "AI" => "XCD",
1382 | "AL" => "ALL", "AM" => "AMD", "CW" => "ANG", "AO" => "AOA", "AQ" => "AQD", "AR" => "ARS", "AS" => "EUR",
1383 | "AT" => "EUR", "AU" => "AUD", "AW" => "AWG", "AZ" => "AZN", "BA" => "BAM", "BB" => "BBD",
1384 | "BD" => "BDT", "BE" => "EUR", "BF" => "XOF", "BG" => "BGL", "BH" => "BHD", "BI" => "BIF",
1385 | "BJ" => "XOF", "BM" => "BMD", "BN" => "BND", "BO" => "BOB", "BR" => "BRL", "BS" => "BSD",
1386 | "BT" => "BTN", "BV" => "NOK", "BW" => "BWP", "BY" => "BYR", "BZ" => "BZD", "CA" => "CAD",
1387 | "CC" => "AUD", "CD" => "CDF", "CF" => "XAF", "CG" => "XAF", "CH" => "CHF", "CI" => "XOF",
1388 | "CK" => "NZD", "CL" => "CLP", "CM" => "XAF", "CN" => "CNY", "CO" => "COP", "CR" => "CRC",
1389 | "CU" => "CUP", "CV" => "CVE", "CX" => "AUD", "CY" => "EUR", "CZ" => "CZK", "DE" => "EUR",
1390 | "DJ" => "DJF", "DK" => "DKK", "DM" => "XCD", "DO" => "DOP", "DZ" => "DZD", "EC" => "ECS",
1391 | "EE" => "EEK", "EG" => "EGP", "EH" => "MAD", "ER" => "ETB", "ES" => "EUR", "ET" => "ETB",
1392 | "FI" => "EUR", "FJ" => "FJD", "FK" => "FKP", "FM" => "USD", "FO" => "DKK", "FR" => "EUR", "SX" => "ANG",
1393 | "GA" => "XAF", "GB" => "GBP", "GD" => "XCD", "GE" => "GEL", "GF" => "EUR", "GH" => "GHS",
1394 | "GI" => "GIP", "GL" => "DKK", "GM" => "GMD", "GN" => "GNF", "GP" => "EUR", "GQ" => "XAF",
1395 | "GR" => "EUR", "GS" => "GBP", "GT" => "GTQ", "GU" => "USD", "GW" => "XOF", "GY" => "GYD",
1396 | "HK" => "HKD", "HM" => "AUD", "HN" => "HNL", "HR" => "HRK", "HT" => "HTG", "HU" => "HUF",
1397 | "ID" => "IDR", "IE" => "EUR", "IL" => "ILS", "IN" => "INR", "IO" => "USD", "IQ" => "IQD",
1398 | "IR" => "IRR", "IS" => "ISK", "IT" => "EUR", "JM" => "JMD", "JO" => "JOD", "JP" => "JPY",
1399 | "KE" => "KES", "KG" => "KGS", "KH" => "KHR", "KI" => "AUD", "KM" => "KMF", "KN" => "XCD",
1400 | "KP" => "KPW", "KR" => "KRW", "KW" => "KWD", "KY" => "KYD", "KZ" => "KZT", "LA" => "LAK",
1401 | "LB" => "LBP", "LC" => "XCD", "LI" => "CHF", "LK" => "LKR", "LR" => "LRD", "LS" => "LSL",
1402 | "LT" => "LTL", "LU" => "EUR", "LV" => "LVL", "LY" => "LYD", "MA" => "MAD", "MC" => "EUR",
1403 | "MD" => "MDL", "MG" => "MGF", "MH" => "USD", "MK" => "MKD", "ML" => "XOF", "MM" => "MMK",
1404 | "MN" => "MNT", "MO" => "MOP", "MP" => "USD", "MQ" => "EUR", "MR" => "MRO", "MS" => "XCD",
1405 | "MT" => "EUR", "MU" => "MUR", "MV" => "MVR", "MW" => "MWK", "MX" => "MXN", "MY" => "MYR",
1406 | "MZ" => "MZN", "NA" => "NAD", "NC" => "XPF", "NE" => "XOF", "NF" => "AUD", "NG" => "NGN",
1407 | "NI" => "NIO", "NL" => "EUR", "NO" => "NOK", "NP" => "NPR", "NR" => "AUD", "NU" => "NZD",
1408 | "NZ" => "NZD", "OM" => "OMR", "PA" => "PAB", "PE" => "PEN", "PF" => "XPF", "PG" => "PGK",
1409 | "PH" => "PHP", "PK" => "PKR", "PL" => "PLN", "PM" => "EUR", "PN" => "NZD", "PR" => "USD", "PS" => "ILS", "PT" => "EUR",
1410 | "PW" => "USD", "PY" => "PYG", "QA" => "QAR", "RE" => "EUR", "RO" => "RON", "RU" => "RUB",
1411 | "RW" => "RWF", "SA" => "SAR", "SB" => "SBD", "SC" => "SCR", "SD" => "SDD", "SE" => "SEK",
1412 | "SG" => "SGD", "SH" => "SHP", "SI" => "EUR", "SJ" => "NOK", "SK" => "SKK", "SL" => "SLL",
1413 | "SM" => "EUR", "SN" => "XOF", "SO" => "SOS", "SR" => "SRG", "ST" => "STD", "SV" => "SVC",
1414 | "SY" => "SYP", "SZ" => "SZL", "TC" => "USD", "TD" => "XAF", "TF" => "EUR", "TG" => "XOF",
1415 | "TH" => "THB", "TJ" => "TJS", "TK" => "NZD", "TM" => "TMM", "TN" => "TND", "TO" => "TOP", "TL" => "USD",
1416 | "TR" => "TRY", "TT" => "TTD", "TV" => "AUD", "TW" => "TWD", "TZ" => "TZS", "UA" => "UAH",
1417 | "UG" => "UGX", "UM" => "USD", "US" => "USD", "UY" => "UYU", "UZ" => "UZS", "VA" => "EUR",
1418 | "VC" => "XCD", "VE" => "VEF", "VG" => "USD", "VI" => "USD", "VN" => "VND", "VU" => "VUV",
1419 | "WF" => "XPF", "WS" => "EUR", "YE" => "YER", "YT" => "EUR", "RS" => "RSD",
1420 | "ZA" => "ZAR", "ZM" => "ZMK", "ME" => "EUR", "ZW" => "ZWD",
1421 | "AX" => "EUR", "GG" => "GBP", "IM" => "GBP",
1422 | "JE" => "GBP", "BL" => "EUR", "MF" => "EUR", "BQ" => "USD", "SS" => "SSP"
1423 | );
1424 | }
1425 |
1426 | /**
1427 | * Added for Chapagain_AutoCurrency Magento Module
1428 | * geoip_currency_code_by_country_code
1429 | */
1430 | function geoip_currency_code_by_country_code($gi, $code) {
1431 | return $gi->GEOIP_COUNTRY_CODE_TO_CURRENCY[$code];
1432 | }
1433 |
1434 | function geoip_load_shared_mem($file)
1435 | {
1436 | $fp = fopen($file, "rb");
1437 | if (!$fp) {
1438 | print "error opening $file: $php_errormsg\n";
1439 | exit;
1440 | }
1441 | $s_array = fstat($fp);
1442 | $size = $s_array['size'];
1443 | if (($shmid = @shmop_open(GEOIP_SHM_KEY, "w", 0, 0))) {
1444 | shmop_delete($shmid);
1445 | shmop_close($shmid);
1446 | }
1447 | $shmid = shmop_open(GEOIP_SHM_KEY, "c", 0644, $size);
1448 | shmop_write($shmid, fread($fp, $size), 0);
1449 | shmop_close($shmid);
1450 | }
1451 |
1452 | function _setup_segments($gi)
1453 | {
1454 | $gi->databaseType = GEOIP_COUNTRY_EDITION;
1455 | $gi->record_length = STANDARD_RECORD_LENGTH;
1456 | if ($gi->flags & GEOIP_SHARED_MEMORY) {
1457 | $offset = @shmop_size($gi->shmid) - 3;
1458 | for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
1459 | $delim = @shmop_read($gi->shmid, $offset, 3);
1460 | $offset += 3;
1461 | if ($delim == (chr(255) . chr(255) . chr(255))) {
1462 | $gi->databaseType = ord(@shmop_read($gi->shmid, $offset, 1));
1463 | if ($gi->databaseType >= 106) {
1464 | $gi->databaseType -= 105;
1465 | }
1466 | $offset++;
1467 |
1468 | if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) {
1469 | $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
1470 | } elseif ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
1471 | $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
1472 | } elseif (($gi->databaseType == GEOIP_CITY_EDITION_REV0)
1473 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
1474 | || ($gi->databaseType == GEOIP_ORG_EDITION)
1475 | || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
1476 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
1477 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
1478 | || ($gi->databaseType == GEOIP_ISP_EDITION)
1479 | || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
1480 | || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
1481 | || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
1482 | || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
1483 | || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
1484 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
1485 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
1486 | || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
1487 | || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
1488 | || ($gi->databaseType == GEOIP_ASNUM_EDITION)
1489 | || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)
1490 | ) {
1491 | $gi->databaseSegments = 0;
1492 | $buf = @shmop_read($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
1493 | for ($j = 0; $j < SEGMENT_RECORD_LENGTH; $j++) {
1494 | $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
1495 | }
1496 | if (($gi->databaseType == GEOIP_ORG_EDITION)
1497 | || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
1498 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
1499 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
1500 | || ($gi->databaseType == GEOIP_ISP_EDITION)
1501 | || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
1502 | ) {
1503 | $gi->record_length = ORG_RECORD_LENGTH;
1504 | }
1505 | }
1506 | break;
1507 | } else {
1508 | $offset -= 4;
1509 | }
1510 | }
1511 | if (($gi->databaseType == GEOIP_COUNTRY_EDITION) ||
1512 | ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6) ||
1513 | ($gi->databaseType == GEOIP_PROXY_EDITION) ||
1514 | ($gi->databaseType == GEOIP_NETSPEED_EDITION)
1515 | ) {
1516 | $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
1517 | }
1518 | } else {
1519 | $filepos = ftell($gi->filehandle);
1520 | fseek($gi->filehandle, -3, SEEK_END);
1521 | for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
1522 | $delim = fread($gi->filehandle, 3);
1523 | if ($delim == (chr(255) . chr(255) . chr(255))) {
1524 | $gi->databaseType = ord(fread($gi->filehandle, 1));
1525 | if ($gi->databaseType >= 106) {
1526 | $gi->databaseType -= 105;
1527 | }
1528 | if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) {
1529 | $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
1530 | } elseif ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
1531 | $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
1532 | } elseif (($gi->databaseType == GEOIP_CITY_EDITION_REV0)
1533 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
1534 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
1535 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
1536 | || ($gi->databaseType == GEOIP_ORG_EDITION)
1537 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
1538 | || ($gi->databaseType == GEOIP_ISP_EDITION)
1539 | || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
1540 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
1541 | || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
1542 | || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
1543 | || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
1544 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
1545 | || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
1546 | || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
1547 | || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
1548 | || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
1549 | || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
1550 | || ($gi->databaseType == GEOIP_ASNUM_EDITION)
1551 | || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)
1552 | ) {
1553 | $gi->databaseSegments = 0;
1554 | $buf = fread($gi->filehandle, SEGMENT_RECORD_LENGTH);
1555 | for ($j = 0; $j < SEGMENT_RECORD_LENGTH; $j++) {
1556 | $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
1557 | }
1558 | if (($gi->databaseType == GEOIP_ORG_EDITION)
1559 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
1560 | || ($gi->databaseType == GEOIP_ISP_EDITION)
1561 | || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
1562 | || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
1563 | || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
1564 | ) {
1565 | $gi->record_length = ORG_RECORD_LENGTH;
1566 | }
1567 | }
1568 | break;
1569 | } else {
1570 | fseek($gi->filehandle, -4, SEEK_CUR);
1571 | }
1572 | }
1573 | if (($gi->databaseType == GEOIP_COUNTRY_EDITION) ||
1574 | ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6) ||
1575 | ($gi->databaseType == GEOIP_PROXY_EDITION) ||
1576 | ($gi->databaseType == GEOIP_NETSPEED_EDITION)
1577 | ) {
1578 | $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
1579 | }
1580 | fseek($gi->filehandle, $filepos, SEEK_SET);
1581 | }
1582 | return $gi;
1583 | }
1584 |
1585 | function geoip_open($filename, $flags)
1586 | {
1587 | $gi = new GeoIP;
1588 | $gi->flags = $flags;
1589 | if ($gi->flags & GEOIP_SHARED_MEMORY) {
1590 | $gi->shmid = @shmop_open(GEOIP_SHM_KEY, "a", 0, 0);
1591 | } else {
1592 | $gi->filehandle = fopen($filename, "rb") or trigger_error("GeoIP API: Can not open $filename\n", E_USER_ERROR);
1593 | if ($gi->flags & GEOIP_MEMORY_CACHE) {
1594 | $s_array = fstat($gi->filehandle);
1595 | $gi->memory_buffer = fread($gi->filehandle, $s_array['size']);
1596 | }
1597 | }
1598 |
1599 | $gi = _setup_segments($gi);
1600 | return $gi;
1601 | }
1602 |
1603 | function geoip_close($gi)
1604 | {
1605 | if ($gi->flags & GEOIP_SHARED_MEMORY) {
1606 | return true;
1607 | }
1608 |
1609 | return fclose($gi->filehandle);
1610 | }
1611 |
1612 | function geoip_country_id_by_name_v6($gi, $name)
1613 | {
1614 | $rec = dns_get_record($name, DNS_AAAA);
1615 | if (!$rec) {
1616 | return false;
1617 | }
1618 | $addr = $rec[0]["ipv6"];
1619 | if (!$addr || $addr == $name) {
1620 | return false;
1621 | }
1622 | return geoip_country_id_by_addr_v6($gi, $addr);
1623 | }
1624 |
1625 | function geoip_country_id_by_name($gi, $name)
1626 | {
1627 | $addr = gethostbyname($name);
1628 | if (!$addr || $addr == $name) {
1629 | return false;
1630 | }
1631 | return geoip_country_id_by_addr($gi, $addr);
1632 | }
1633 |
1634 | function geoip_country_code_by_name_v6($gi, $name)
1635 | {
1636 | $country_id = geoip_country_id_by_name_v6($gi, $name);
1637 | if ($country_id !== false) {
1638 | return $gi->GEOIP_COUNTRY_CODES[$country_id];
1639 | }
1640 | return false;
1641 | }
1642 |
1643 | function geoip_country_code_by_name($gi, $name)
1644 | {
1645 | $country_id = geoip_country_id_by_name($gi, $name);
1646 | if ($country_id !== false) {
1647 | return $gi->GEOIP_COUNTRY_CODES[$country_id];
1648 | }
1649 | return false;
1650 | }
1651 |
1652 | function geoip_country_name_by_name_v6($gi, $name)
1653 | {
1654 | $country_id = geoip_country_id_by_name_v6($gi, $name);
1655 | if ($country_id !== false) {
1656 | return $gi->GEOIP_COUNTRY_NAMES[$country_id];
1657 | }
1658 | return false;
1659 | }
1660 |
1661 | function geoip_country_name_by_name($gi, $name)
1662 | {
1663 | $country_id = geoip_country_id_by_name($gi, $name);
1664 | if ($country_id !== false) {
1665 | return $gi->GEOIP_COUNTRY_NAMES[$country_id];
1666 | }
1667 | return false;
1668 | }
1669 |
1670 | function geoip_country_id_by_addr_v6($gi, $addr)
1671 | {
1672 | $ipnum = inet_pton($addr);
1673 | return _geoip_seek_country_v6($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
1674 | }
1675 |
1676 | function geoip_country_id_by_addr($gi, $addr)
1677 | {
1678 | $ipnum = ip2long($addr);
1679 | return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
1680 | }
1681 |
1682 | function geoip_country_code_by_addr_v6($gi, $addr)
1683 | {
1684 | $country_id = geoip_country_id_by_addr_v6($gi, $addr);
1685 | if ($country_id !== false) {
1686 | return $gi->GEOIP_COUNTRY_CODES[$country_id];
1687 | }
1688 | return false;
1689 | }
1690 |
1691 | function geoip_country_code_by_addr($gi, $addr)
1692 | {
1693 | if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
1694 | $record = geoip_record_by_addr($gi, $addr);
1695 | if ($record !== false) {
1696 | return $record->country_code;
1697 | }
1698 | } else {
1699 | $country_id = geoip_country_id_by_addr($gi, $addr);
1700 | if ($country_id !== false) {
1701 | return $gi->GEOIP_COUNTRY_CODES[$country_id];
1702 | }
1703 | }
1704 | return false;
1705 | }
1706 |
1707 | function geoip_country_name_by_addr_v6($gi, $addr)
1708 | {
1709 | $country_id = geoip_country_id_by_addr_v6($gi, $addr);
1710 | if ($country_id !== false) {
1711 | return $gi->GEOIP_COUNTRY_NAMES[$country_id];
1712 | }
1713 | return false;
1714 | }
1715 |
1716 | function geoip_country_name_by_addr($gi, $addr)
1717 | {
1718 | if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
1719 | $record = geoip_record_by_addr($gi, $addr);
1720 | return $record->country_name;
1721 | } else {
1722 | $country_id = geoip_country_id_by_addr($gi, $addr);
1723 | if ($country_id !== false) {
1724 | return $gi->GEOIP_COUNTRY_NAMES[$country_id];
1725 | }
1726 | }
1727 | return false;
1728 | }
1729 |
1730 | function _geoip_seek_country_v6($gi, $ipnum)
1731 | {
1732 | # arrays from unpack start with offset 1
1733 | # yet another php mystery. array_merge work around
1734 | # this broken behaviour
1735 | $v6vec = array_merge(unpack("C16", $ipnum));
1736 |
1737 | $offset = 0;
1738 | for ($depth = 127; $depth >= 0; --$depth) {
1739 | if ($gi->flags & GEOIP_MEMORY_CACHE) {
1740 | $buf = _safe_substr(
1741 | $gi->memory_buffer,
1742 | 2 * $gi->record_length * $offset,
1743 | 2 * $gi->record_length
1744 | );
1745 | } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
1746 | $buf = @shmop_read(
1747 | $gi->shmid,
1748 | 2 * $gi->record_length * $offset,
1749 | 2 * $gi->record_length
1750 | );
1751 | } else {
1752 | fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
1753 | or trigger_error("GeoIP API: fseek failed", E_USER_ERROR);
1754 | $buf = fread($gi->filehandle, 2 * $gi->record_length);
1755 | }
1756 | $x = array(0, 0);
1757 | for ($i = 0; $i < 2; ++$i) {
1758 | for ($j = 0; $j < $gi->record_length; ++$j) {
1759 | $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
1760 | }
1761 | }
1762 |
1763 | $bnum = 127 - $depth;
1764 | $idx = $bnum >> 3;
1765 | $b_mask = 1 << ($bnum & 7 ^ 7);
1766 | if (($v6vec[$idx] & $b_mask) > 0) {
1767 | if ($x[1] >= $gi->databaseSegments) {
1768 | return $x[1];
1769 | }
1770 | $offset = $x[1];
1771 | } else {
1772 | if ($x[0] >= $gi->databaseSegments) {
1773 | return $x[0];
1774 | }
1775 | $offset = $x[0];
1776 | }
1777 | }
1778 | trigger_error("GeoIP API: Error traversing database - perhaps it is corrupt?", E_USER_ERROR);
1779 | return false;
1780 | }
1781 |
1782 | function _geoip_seek_country($gi, $ipnum)
1783 | {
1784 | $offset = 0;
1785 | for ($depth = 31; $depth >= 0; --$depth) {
1786 | if ($gi->flags & GEOIP_MEMORY_CACHE) {
1787 | $buf = _safe_substr(
1788 | $gi->memory_buffer,
1789 | 2 * $gi->record_length * $offset,
1790 | 2 * $gi->record_length
1791 | );
1792 | } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
1793 | $buf = @shmop_read(
1794 | $gi->shmid,
1795 | 2 * $gi->record_length * $offset,
1796 | 2 * $gi->record_length
1797 | );
1798 | } else {
1799 | fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
1800 | or trigger_error("GeoIP API: fseek failed", E_USER_ERROR);
1801 | $buf = fread($gi->filehandle, 2 * $gi->record_length);
1802 | }
1803 | $x = array(0, 0);
1804 | for ($i = 0; $i < 2; ++$i) {
1805 | for ($j = 0; $j < $gi->record_length; ++$j) {
1806 | $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
1807 | }
1808 | }
1809 | if ($ipnum & (1 << $depth)) {
1810 | if ($x[1] >= $gi->databaseSegments) {
1811 | return $x[1];
1812 | }
1813 | $offset = $x[1];
1814 | } else {
1815 | if ($x[0] >= $gi->databaseSegments) {
1816 | return $x[0];
1817 | }
1818 | $offset = $x[0];
1819 | }
1820 | }
1821 | trigger_error("GeoIP API: Error traversing database - perhaps it is corrupt?", E_USER_ERROR);
1822 | return false;
1823 | }
1824 |
1825 | function _common_get_org($gi, $seek_org)
1826 | {
1827 | $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments;
1828 | if ($gi->flags & GEOIP_SHARED_MEMORY) {
1829 | $org_buf = @shmop_read($gi->shmid, $record_pointer, MAX_ORG_RECORD_LENGTH);
1830 | } else {
1831 | fseek($gi->filehandle, $record_pointer, SEEK_SET);
1832 | $org_buf = fread($gi->filehandle, MAX_ORG_RECORD_LENGTH);
1833 | }
1834 | $org_buf = _safe_substr($org_buf, 0, strpos($org_buf, "\0"));
1835 | return $org_buf;
1836 | }
1837 |
1838 | function _get_org_v6($gi, $ipnum)
1839 | {
1840 | $seek_org = _geoip_seek_country_v6($gi, $ipnum);
1841 | if ($seek_org == $gi->databaseSegments) {
1842 | return null;
1843 | }
1844 | return _common_get_org($gi, $seek_org);
1845 | }
1846 |
1847 | function _get_org($gi, $ipnum)
1848 | {
1849 | $seek_org = _geoip_seek_country($gi, $ipnum);
1850 | if ($seek_org == $gi->databaseSegments) {
1851 | return null;
1852 | }
1853 | return _common_get_org($gi, $seek_org);
1854 | }
1855 |
1856 |
1857 | function geoip_name_by_addr_v6($gi, $addr)
1858 | {
1859 | if ($addr == null) {
1860 | return 0;
1861 | }
1862 | $ipnum = inet_pton($addr);
1863 | return _get_org_v6($gi, $ipnum);
1864 | }
1865 |
1866 | function geoip_name_by_addr($gi, $addr)
1867 | {
1868 | if ($addr == null) {
1869 | return 0;
1870 | }
1871 | $ipnum = ip2long($addr);
1872 | return _get_org($gi, $ipnum);
1873 | }
1874 |
1875 | function geoip_org_by_addr($gi, $addr)
1876 | {
1877 | return geoip_name_by_addr($gi, $addr);
1878 | }
1879 |
1880 | function _get_region($gi, $ipnum)
1881 | {
1882 | if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) {
1883 | $seek_region = _geoip_seek_country($gi, $ipnum) - GEOIP_STATE_BEGIN_REV0;
1884 | if ($seek_region >= 1000) {
1885 | $country_code = "US";
1886 | $region = chr(($seek_region - 1000) / 26 + 65) . chr(($seek_region - 1000) % 26 + 65);
1887 | } else {
1888 | $country_code = $gi->GEOIP_COUNTRY_CODES[$seek_region];
1889 | $region = "";
1890 | }
1891 | return array($country_code, $region);
1892 | } elseif ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
1893 | $seek_region = _geoip_seek_country($gi, $ipnum) - GEOIP_STATE_BEGIN_REV1;
1894 | if ($seek_region < US_OFFSET) {
1895 | $country_code = "";
1896 | $region = "";
1897 | } elseif ($seek_region < CANADA_OFFSET) {
1898 | $country_code = "US";
1899 | $region = chr(($seek_region - US_OFFSET) / 26 + 65) . chr(($seek_region - US_OFFSET) % 26 + 65);
1900 | } elseif ($seek_region < WORLD_OFFSET) {
1901 | $country_code = "CA";
1902 | $region = chr(($seek_region - CANADA_OFFSET) / 26 + 65) . chr(($seek_region - CANADA_OFFSET) % 26 + 65);
1903 | } else {
1904 | $country_code = $gi->GEOIP_COUNTRY_CODES[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
1905 | $region = "";
1906 | }
1907 | return array($country_code, $region);
1908 | }
1909 | }
1910 |
1911 | function geoip_region_by_addr($gi, $addr)
1912 | {
1913 | if ($addr == null) {
1914 | return 0;
1915 | }
1916 | $ipnum = ip2long($addr);
1917 | return _get_region($gi, $ipnum);
1918 | }
1919 |
1920 | function _safe_substr($string, $start, $length)
1921 | {
1922 | // workaround php's broken substr, strpos, etc handling with
1923 | // mbstring.func_overload and mbstring.internal_encoding
1924 | $mbExists = extension_loaded('mbstring');
1925 |
1926 | if ($mbExists) {
1927 | $enc = mb_internal_encoding();
1928 | mb_internal_encoding('ISO-8859-1');
1929 | }
1930 |
1931 | $buf = substr($string, $start, $length);
1932 |
1933 | if ($mbExists) {
1934 | mb_internal_encoding($enc);
1935 | }
1936 |
1937 | return $buf;
1938 | }
1939 |
--------------------------------------------------------------------------------