1 |
<?php |
2 |
|
3 |
/* geoipcity.inc |
4 |
* |
5 |
* Copyright (C) 2004 Maxmind LLC |
6 |
* |
7 |
* This library is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU Lesser General Public |
9 |
* License as published by the Free Software Foundation; either |
10 |
* version 2.1 of the License, or (at your option) any later version. |
11 |
* |
12 |
* This library is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* Lesser General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU Lesser General Public |
18 |
* License along with this library; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
|
22 |
/* |
23 |
* Changelog: |
24 |
* |
25 |
* 2005-01-13 Andrew Hill, Awarez Ltd. (http://www.awarez.net) |
26 |
* Formatted file according to PEAR library standards. |
27 |
* Changed inclusion of geoip.inc file to require_once, so that |
28 |
* this library can be used in the same script as geoip.inc. |
29 |
*/ |
30 |
|
31 |
define("FULL_RECORD_LENGTH",50); |
32 |
|
33 |
require_once 'geoip.inc.php'; |
34 |
require_once 'geoipregionvars.php'; |
35 |
|
36 |
class geoiprecord { |
37 |
var $country_code; |
38 |
var $country_code3; |
39 |
var $country_name; |
40 |
var $region; |
41 |
var $city; |
42 |
var $postal_code; |
43 |
var $latitude; |
44 |
var $longitude; |
45 |
var $area_code; |
46 |
var $dma_code; # metro and dma code are the same. use metro_code |
47 |
var $metro_code; |
48 |
var $continent_code; |
49 |
} |
50 |
|
51 |
class geoipdnsrecord { |
52 |
var $country_code; |
53 |
var $country_code3; |
54 |
var $country_name; |
55 |
var $region; |
56 |
var $regionname; |
57 |
var $city; |
58 |
var $postal_code; |
59 |
var $latitude; |
60 |
var $longitude; |
61 |
var $areacode; |
62 |
var $dmacode; |
63 |
var $isp; |
64 |
var $org; |
65 |
var $metrocode; |
66 |
} |
67 |
|
68 |
function getrecordwithdnsservice($str){ |
69 |
$record = new geoipdnsrecord; |
70 |
$keyvalue = explode(";",$str); |
71 |
foreach ($keyvalue as $keyvalue2){ |
72 |
list($key,$value) = explode("=",$keyvalue2); |
73 |
if ($key == "co"){ |
74 |
$record->country_code = $value; |
75 |
} |
76 |
if ($key == "ci"){ |
77 |
$record->city = $value; |
78 |
} |
79 |
if ($key == "re"){ |
80 |
$record->region = $value; |
81 |
} |
82 |
if ($key == "ac"){ |
83 |
$record->areacode = $value; |
84 |
} |
85 |
if ($key == "dm" || $key == "me" ){ |
86 |
$record->dmacode = $value; |
87 |
$record->metrocode = $value; |
88 |
} |
89 |
if ($key == "is"){ |
90 |
$record->isp = $value; |
91 |
} |
92 |
if ($key == "or"){ |
93 |
$record->org = $value; |
94 |
} |
95 |
if ($key == "zi"){ |
96 |
$record->postal_code = $value; |
97 |
} |
98 |
if ($key == "la"){ |
99 |
$record->latitude = $value; |
100 |
} |
101 |
if ($key == "lo"){ |
102 |
$record->longitude = $value; |
103 |
} |
104 |
} |
105 |
$number = $GLOBALS['GEOIP_COUNTRY_CODE_TO_NUMBER'][$record->country_code]; |
106 |
$record->country_code3 = $GLOBALS['GEOIP_COUNTRY_CODES3'][$number]; |
107 |
$record->country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$number]; |
108 |
if ($record->region != "") { |
109 |
if (($record->country_code == "US") || ($record->country_code == "CA")){ |
110 |
$record->regionname = $GLOBALS['ISO'][$record->country_code][$record->region]; |
111 |
} else { |
112 |
$record->regionname = $GLOBALS['FIPS'][$record->country_code][$record->region]; |
113 |
} |
114 |
} |
115 |
return $record; |
116 |
} |
117 |
|
118 |
|
119 |
function _get_record_v6($gi,$ipnum){ |
120 |
$seek_country = _geoip_seek_country_v6($gi,$ipnum); |
121 |
if ($seek_country == $gi->databaseSegments) { |
122 |
return NULL; |
123 |
} |
124 |
return _common_get_record($gi, $seek_country); |
125 |
} |
126 |
|
127 |
function _common_get_record($gi, $seek_country){ |
128 |
// workaround php's broken substr, strpos, etc handling with |
129 |
// mbstring.func_overload and mbstring.internal_encoding |
130 |
$enc = mb_internal_encoding(); |
131 |
mb_internal_encoding('ISO-8859-1'); |
132 |
|
133 |
$record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments; |
134 |
|
135 |
if ($gi->flags & GEOIP_MEMORY_CACHE) { |
136 |
$record_buf = substr($gi->memory_buffer,$record_pointer,FULL_RECORD_LENGTH); |
137 |
} elseif ($gi->flags & GEOIP_SHARED_MEMORY){ |
138 |
$record_buf = @shmop_read($gi->shmid,$record_pointer,FULL_RECORD_LENGTH); |
139 |
} else { |
140 |
fseek($gi->filehandle, $record_pointer, SEEK_SET); |
141 |
$record_buf = fread($gi->filehandle,FULL_RECORD_LENGTH); |
142 |
} |
143 |
$record = new geoiprecord; |
144 |
$record_buf_pos = 0; |
145 |
$char = ord(substr($record_buf,$record_buf_pos,1)); |
146 |
$record->country_code = $gi->GEOIP_COUNTRY_CODES[$char]; |
147 |
$record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char]; |
148 |
$record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char]; |
149 |
$record->continent_code = $gi->GEOIP_CONTINENT_CODES[$char]; |
150 |
$record_buf_pos++; |
151 |
$str_length = 0; |
152 |
// Get region |
153 |
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1)); |
154 |
while ($char != 0){ |
155 |
$str_length++; |
156 |
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1)); |
157 |
} |
158 |
if ($str_length > 0){ |
159 |
$record->region = substr($record_buf,$record_buf_pos,$str_length); |
160 |
} |
161 |
$record_buf_pos += $str_length + 1; |
162 |
$str_length = 0; |
163 |
// Get city |
164 |
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1)); |
165 |
while ($char != 0){ |
166 |
$str_length++; |
167 |
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1)); |
168 |
} |
169 |
if ($str_length > 0){ |
170 |
$record->city = substr($record_buf,$record_buf_pos,$str_length); |
171 |
} |
172 |
$record_buf_pos += $str_length + 1; |
173 |
$str_length = 0; |
174 |
// Get postal code |
175 |
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1)); |
176 |
while ($char != 0){ |
177 |
$str_length++; |
178 |
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1)); |
179 |
} |
180 |
if ($str_length > 0){ |
181 |
$record->postal_code = substr($record_buf,$record_buf_pos,$str_length); |
182 |
} |
183 |
$record_buf_pos += $str_length + 1; |
184 |
$str_length = 0; |
185 |
// Get latitude and longitude |
186 |
$latitude = 0; |
187 |
$longitude = 0; |
188 |
for ($j = 0;$j < 3; ++$j){ |
189 |
$char = ord(substr($record_buf,$record_buf_pos++,1)); |
190 |
$latitude += ($char << ($j * 8)); |
191 |
} |
192 |
$record->latitude = ($latitude/10000) - 180; |
193 |
for ($j = 0;$j < 3; ++$j){ |
194 |
$char = ord(substr($record_buf,$record_buf_pos++,1)); |
195 |
$longitude += ($char << ($j * 8)); |
196 |
} |
197 |
$record->longitude = ($longitude/10000) - 180; |
198 |
if (GEOIP_CITY_EDITION_REV1 == $gi->databaseType){ |
199 |
$metroarea_combo = 0; |
200 |
if ($record->country_code == "US"){ |
201 |
for ($j = 0;$j < 3;++$j){ |
202 |
$char = ord(substr($record_buf,$record_buf_pos++,1)); |
203 |
$metroarea_combo += ($char << ($j * 8)); |
204 |
} |
205 |
$record->metro_code = $record->dma_code = floor($metroarea_combo/1000); |
206 |
$record->area_code = $metroarea_combo%1000; |
207 |
} |
208 |
} |
209 |
mb_internal_encoding($enc); |
210 |
return $record; |
211 |
} |
212 |
|
213 |
function GeoIP_record_by_addr_v6 ($gi,$addr){ |
214 |
if ($addr == NULL){ |
215 |
return 0; |
216 |
} |
217 |
$ipnum = inet_pton($addr); |
218 |
return _get_record_v6($gi, $ipnum); |
219 |
} |
220 |
|
221 |
function _get_record($gi,$ipnum){ |
222 |
$seek_country = _geoip_seek_country($gi,$ipnum); |
223 |
if ($seek_country == $gi->databaseSegments) { |
224 |
return NULL; |
225 |
} |
226 |
return _common_get_record($gi, $seek_country); |
227 |
} |
228 |
|
229 |
function GeoIP_record_by_addr ($gi,$addr){ |
230 |
if ($addr == NULL){ |
231 |
return 0; |
232 |
} |
233 |
$ipnum = ip2long($addr); |
234 |
return _get_record($gi, $ipnum); |
235 |
} |
236 |
|
237 |
?> |