PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/geoip/geoipcity.inc

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