MDL-19874 updated geoip to 1.80 and fixed iplookup regressions
[moodle.git] / lib / geoip / geoipcity.inc
CommitLineData
7c09710c 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
31define("FULL_RECORD_LENGTH",50);
32
33require_once 'geoip.inc';
34require_once 'geoipregionvars.php';
35
36class 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;
48fb4cd6 46 var $dma_code; # metro and dma code are the same. use metro_code
47 var $metro_code;
7c09710c 48}
49
50class geoipdnsrecord {
51 var $country_code;
52 var $country_code3;
53 var $country_name;
54 var $region;
55 var $regionname;
56 var $city;
57 var $postal_code;
58 var $latitude;
59 var $longitude;
60 var $areacode;
61 var $dmacode;
62 var $isp;
63 var $org;
48fb4cd6 64 var $metrocode;
7c09710c 65}
66
67function getrecordwithdnsservice($str){
68 $record = new geoipdnsrecord;
69 $keyvalue = split(";",$str);
70 foreach ($keyvalue as $keyvalue2){
71 list($key,$value) = split("=",$keyvalue2);
72 if ($key == "co"){
73 $record->country_code = $value;
74 }
75 if ($key == "ci"){
76 $record->city = $value;
77 }
78 if ($key == "re"){
79 $record->region = $value;
80 }
81 if ($key == "ac"){
82 $record->areacode = $value;
83 }
48fb4cd6 84 if ($key == "dm" || $key == "me" ){
85 $record->dmacode = $value;
86 $record->metrocode = $value;
7c09710c 87 }
88 if ($key == "is"){
89 $record->isp = $value;
90 }
91 if ($key == "or"){
92 $record->org = $value;
93 }
94 if ($key == "zi"){
95 $record->postal_code = $value;
96 }
97 if ($key == "la"){
98 $record->latitude = $value;
99 }
100 if ($key == "lo"){
101 $record->longitude = $value;
102 }
103 }
104 $number = $GLOBALS['GEOIP_COUNTRY_CODE_TO_NUMBER'][$record->country_code];
105 $record->country_code3 = $GLOBALS['GEOIP_COUNTRY_CODES3'][$number];
106 $record->country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$number];
107 if ($record->region != "") {
108 if (($record->country_code == "US") || ($record->country_code == "CA")){
109 $record->regionname = $GLOBALS['ISO'][$record->country_code][$record->region];
110 } else {
111 $record->regionname = $GLOBALS['FIPS'][$record->country_code][$record->region];
112 }
113 }
114 return $record;
115}
116
117function _get_record($gi,$ipnum){
118 $seek_country = _geoip_seek_country($gi,$ipnum);
119 if ($seek_country == $gi->databaseSegments) {
120 return NULL;
121 }
122 $record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments;
123
124 if ($gi->flags & GEOIP_MEMORY_CACHE) {
125 $record_buf = substr($gi->memory_buffer,$record_pointer,FULL_RECORD_LENGTH);
126 } elseif ($gi->flags & GEOIP_SHARED_MEMORY){
127 $record_buf = @shmop_read($gi->shmid,$record_pointer,FULL_RECORD_LENGTH);
128 } else {
129 fseek($gi->filehandle, $record_pointer, SEEK_SET);
130 $record_buf = fread($gi->filehandle,FULL_RECORD_LENGTH);
131 }
132 $record = new geoiprecord;
133 $record_buf_pos = 0;
134 $char = ord(substr($record_buf,$record_buf_pos,1));
135 $record->country_code = $gi->GEOIP_COUNTRY_CODES[$char];
136 $record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char];
137 $record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char];
138 $record_buf_pos++;
139 $str_length = 0;
140 // Get region
141 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
142 while ($char != 0){
143 $str_length++;
144 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
145 }
146 if ($str_length > 0){
147 $record->region = substr($record_buf,$record_buf_pos,$str_length);
148 }
149 $record_buf_pos += $str_length + 1;
150 $str_length = 0;
151 // Get city
152 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
153 while ($char != 0){
154 $str_length++;
155 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
156 }
157 if ($str_length > 0){
158 $record->city = substr($record_buf,$record_buf_pos,$str_length);
159 }
160 $record_buf_pos += $str_length + 1;
161 $str_length = 0;
162 // Get postal code
163 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
164 while ($char != 0){
165 $str_length++;
166 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
167 }
168 if ($str_length > 0){
169 $record->postal_code = substr($record_buf,$record_buf_pos,$str_length);
170 }
171 $record_buf_pos += $str_length + 1;
172 $str_length = 0;
173 // Get latitude and longitude
174 $latitude = 0;
175 $longitude = 0;
176 for ($j = 0;$j < 3; ++$j){
177 $char = ord(substr($record_buf,$record_buf_pos++,1));
178 $latitude += ($char << ($j * 8));
179 }
180 $record->latitude = ($latitude/10000) - 180;
181 for ($j = 0;$j < 3; ++$j){
182 $char = ord(substr($record_buf,$record_buf_pos++,1));
183 $longitude += ($char << ($j * 8));
184 }
185 $record->longitude = ($longitude/10000) - 180;
186 if (GEOIP_CITY_EDITION_REV1 == $gi->databaseType){
48fb4cd6 187 $metroarea_combo = 0;
7c09710c 188 if ($record->country_code == "US"){
189 for ($j = 0;$j < 3;++$j){
190 $char = ord(substr($record_buf,$record_buf_pos++,1));
48fb4cd6 191 $metroarea_combo += ($char << ($j * 8));
7c09710c 192 }
48fb4cd6 193 $record->metro_code = $record->dma_code = floor($metroarea_combo/1000);
194 $record->area_code = $metroarea_combo%1000;
7c09710c 195 }
196 }
197 return $record;
198}
199
200function GeoIP_record_by_addr ($gi,$addr){
201 if ($addr == NULL){
202 return 0;
203 }
204 $ipnum = ip2long($addr);
205 return _get_record($gi, $ipnum);
206}
207
208?>