/[web]/data/lib/MaxMind/GeoIP/geoip.inc.php
ViewVC logotype

Contents of /data/lib/MaxMind/GeoIP/geoip.inc.php

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.28