timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / lib / geoip / geoipcity.inc
blob836ae93231f4411640f3e54115fe98ebd6203213
1 <?php
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  */
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  */
31 define("FULL_RECORD_LENGTH",50);
33 require_once 'geoip.inc';
34 require_once 'geoipregionvars.php';
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;
49 class geoipdnsrecord {
50   var $country_code;
51   var $country_code3;
52   var $country_name;
53   var $region;
54   var $regionname;
55   var $city;
56   var $postal_code;
57   var $latitude;
58   var $longitude;
59   var $areacode;
60   var $dmacode;
61   var $isp;
62   var $org;
65 function getrecordwithdnsservice($str){
66   $record = new geoipdnsrecord;
67   $keyvalue = split(";",$str);
68   foreach ($keyvalue as $keyvalue2){
69     list($key,$value) = split("=",$keyvalue2);
70     if ($key == "co"){
71       $record->country_code = $value;
72     }
73     if ($key == "ci"){
74       $record->city = $value;
75     }
76     if ($key == "re"){
77       $record->region = $value;
78     }
79     if ($key == "ac"){
80       $record->areacode = $value;
81     }
82     if ($key == "dm"){
83       $record->dmacode = $value;
84     }
85     if ($key == "is"){
86       $record->isp = $value;
87     }
88     if ($key == "or"){
89       $record->org = $value;
90     }
91     if ($key == "zi"){
92       $record->postal_code = $value;
93     }
94     if ($key == "la"){
95       $record->latitude = $value;
96     }
97     if ($key == "lo"){
98       $record->longitude = $value;
99     }
100   }
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];
107     } else {
108       $record->regionname = $GLOBALS['FIPS'][$record->country_code][$record->region];
109     }
110   }
111   return $record;
114 function _get_record($gi,$ipnum){
115   $seek_country = _geoip_seek_country($gi,$ipnum);
116   if ($seek_country == $gi->databaseSegments) {
117     return NULL;
118   }
119   $record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments;
120   
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);
125   } else {
126     fseek($gi->filehandle, $record_pointer, SEEK_SET);
127     $record_buf = fread($gi->filehandle,FULL_RECORD_LENGTH);
128   }
129   $record = new geoiprecord;
130   $record_buf_pos = 0;
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];
135   $record_buf_pos++;
136   $str_length = 0;
137     // Get region
138   $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
139   while ($char != 0){
140     $str_length++;
141     $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
142   }
143   if ($str_length > 0){
144     $record->region = substr($record_buf,$record_buf_pos,$str_length);
145   }
146   $record_buf_pos += $str_length + 1;
147   $str_length = 0;
148     // Get city
149   $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
150   while ($char != 0){
151     $str_length++;
152     $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
153   }
154   if ($str_length > 0){
155     $record->city = substr($record_buf,$record_buf_pos,$str_length);
156   }
157   $record_buf_pos += $str_length + 1;
158   $str_length = 0;
159     // Get postal code
160   $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
161   while ($char != 0){
162     $str_length++;
163     $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
164   }
165   if ($str_length > 0){
166     $record->postal_code = substr($record_buf,$record_buf_pos,$str_length);
167   }
168   $record_buf_pos += $str_length + 1;
169   $str_length = 0;
170     // Get latitude and longitude
171   $latitude = 0;
172   $longitude = 0;
173   for ($j = 0;$j < 3; ++$j){
174     $char = ord(substr($record_buf,$record_buf_pos++,1));
175     $latitude += ($char << ($j * 8));
176   }
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));
181   }
182   $record->longitude = ($longitude/10000) - 180;
183   if (GEOIP_CITY_EDITION_REV1 == $gi->databaseType){
184     $dmaarea_combo = 0;
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));
189       }
190       $record->dma_code = floor($dmaarea_combo/1000);
191       $record->area_code = $dmaarea_combo%1000;
192     }
193   }
194   return $record;
197 function GeoIP_record_by_addr ($gi,$addr){
198   if ($addr == NULL){
199      return 0;
200   }
201   $ipnum = ip2long($addr);
202   return _get_record($gi, $ipnum);