5 * Copyright (C) 2004 Maxmind LLC
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.
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.
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
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.
31 define("FULL_RECORD_LENGTH",50);
33 require_once 'geoip.inc';
34 require_once 'geoipregionvars.php';
49 class geoipdnsrecord {
65 function getrecordwithdnsservice($str){
66 $record = new geoipdnsrecord;
67 $keyvalue = split(";",$str);
68 foreach ($keyvalue as $keyvalue2){
69 list($key,$value) = split("=",$keyvalue2);
71 $record->country_code = $value;
74 $record->city = $value;
77 $record->region = $value;
80 $record->areacode = $value;
83 $record->dmacode = $value;
86 $record->isp = $value;
89 $record->org = $value;
92 $record->postal_code = $value;
95 $record->latitude = $value;
98 $record->longitude = $value;
101 $number = $GLOBALS['GEOIP_COUNTRY_CODE_TO_NUMBER'][$record->country_code];
102 $record->country_code3 = $GLOBALS['GEOIP_COUNTRY_CODES3'][$number];
103 $record->country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$number];
104 if ($record->region != "") {
105 if (($record->country_code == "US") || ($record->country_code == "CA")){
106 $record->regionname = $GLOBALS['ISO'][$record->country_code][$record->region];
108 $record->regionname = $GLOBALS['FIPS'][$record->country_code][$record->region];
114 function _get_record($gi,$ipnum){
115 $seek_country = _geoip_seek_country($gi,$ipnum);
116 if ($seek_country == $gi->databaseSegments) {
119 $record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments;
121 if ($gi->flags & GEOIP_MEMORY_CACHE) {
122 $record_buf = substr($gi->memory_buffer,$record_pointer,FULL_RECORD_LENGTH);
123 } elseif ($gi->flags & GEOIP_SHARED_MEMORY){
124 $record_buf = @shmop_read($gi->shmid,$record_pointer,FULL_RECORD_LENGTH);
126 fseek($gi->filehandle, $record_pointer, SEEK_SET);
127 $record_buf = fread($gi->filehandle,FULL_RECORD_LENGTH);
129 $record = new geoiprecord;
131 $char = ord(substr($record_buf,$record_buf_pos,1));
132 $record->country_code = $gi->GEOIP_COUNTRY_CODES[$char];
133 $record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char];
134 $record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char];
138 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
141 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
143 if ($str_length > 0){
144 $record->region = substr($record_buf,$record_buf_pos,$str_length);
146 $record_buf_pos += $str_length + 1;
149 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
152 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
154 if ($str_length > 0){
155 $record->city = substr($record_buf,$record_buf_pos,$str_length);
157 $record_buf_pos += $str_length + 1;
160 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
163 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
165 if ($str_length > 0){
166 $record->postal_code = substr($record_buf,$record_buf_pos,$str_length);
168 $record_buf_pos += $str_length + 1;
170 // Get latitude and longitude
173 for ($j = 0;$j < 3; ++$j){
174 $char = ord(substr($record_buf,$record_buf_pos++,1));
175 $latitude += ($char << ($j * 8));
177 $record->latitude = ($latitude/10000) - 180;
178 for ($j = 0;$j < 3; ++$j){
179 $char = ord(substr($record_buf,$record_buf_pos++,1));
180 $longitude += ($char << ($j * 8));
182 $record->longitude = ($longitude/10000) - 180;
183 if (GEOIP_CITY_EDITION_REV1 == $gi->databaseType){
185 if ($record->country_code == "US"){
186 for ($j = 0;$j < 3;++$j){
187 $char = ord(substr($record_buf,$record_buf_pos++,1));
188 $dmaarea_combo += ($char << ($j * 8));
190 $record->dma_code = floor($dmaarea_combo/1000);
191 $record->area_code = $dmaarea_combo%1000;
197 function GeoIP_record_by_addr ($gi,$addr){
201 $ipnum = ip2long($addr);
202 return _get_record($gi, $ipnum);