b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/geoip_plugin/geoip.inc - 727 lines - 30920 bytes - Summary - Text - Print

   1  <?php
   2  /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
   3  /* geoip.inc
   4   *
   5   * Copyright (C) 2007 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  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  22  
  23  define("GEOIP_COUNTRY_BEGIN", 16776960);
  24  define("GEOIP_STATE_BEGIN_REV0", 16700000);
  25  define("GEOIP_STATE_BEGIN_REV1", 16000000);
  26  define("GEOIP_STANDARD", 0);
  27  define("GEOIP_MEMORY_CACHE", 1);
  28  define("GEOIP_SHARED_MEMORY", 2);
  29  define("STRUCTURE_INFO_MAX_SIZE", 20);
  30  define("DATABASE_INFO_MAX_SIZE", 100);
  31  define("GEOIP_COUNTRY_EDITION", 106);
  32  define("GEOIP_PROXY_EDITION", 8);
  33  define("GEOIP_ASNUM_EDITION", 9);
  34  define("GEOIP_NETSPEED_EDITION", 10);
  35  define("GEOIP_REGION_EDITION_REV0", 112);
  36  define("GEOIP_REGION_EDITION_REV1", 3);
  37  define("GEOIP_CITY_EDITION_REV0", 111);
  38  define("GEOIP_CITY_EDITION_REV1", 2);
  39  define("GEOIP_ORG_EDITION", 110);
  40  define("GEOIP_ISP_EDITION", 4);
  41  define("SEGMENT_RECORD_LENGTH", 3);
  42  define("STANDARD_RECORD_LENGTH", 3);
  43  define("ORG_RECORD_LENGTH", 4);
  44  define("MAX_RECORD_LENGTH", 4);
  45  define("MAX_ORG_RECORD_LENGTH", 300);
  46  define("GEOIP_SHM_KEY", 0x4f415401);
  47  define("US_OFFSET", 1);
  48  define("CANADA_OFFSET", 677);
  49  define("WORLD_OFFSET", 1353);
  50  define("FIPS_RANGE", 360);
  51  define("GEOIP_UNKNOWN_SPEED", 0);
  52  define("GEOIP_DIALUP_SPEED", 1);
  53  define("GEOIP_CABLEDSL_SPEED", 2);
  54  define("GEOIP_CORPORATE_SPEED", 3);
  55  define("GEOIP_DOMAIN_EDITION", 11);
  56  define("GEOIP_COUNTRY_EDITION_V6", 12);
  57  define("GEOIP_LOCATIONA_EDITION", 13);
  58  define("GEOIP_ACCURACYRADIUS_EDITION", 14);
  59  define("GEOIP_CITYCOMBINED_EDITION", 15);
  60  define("GEOIP_CITY_EDITION_REV1_V6", 30);
  61  define("GEOIP_CITY_EDITION_REV0_V6",31);
  62  define("GEOIP_NETSPEED_EDITION_REV1",32);
  63  define("GEOIP_NETSPEED_EDITION_REV1_V6",33);
  64  define("GEOIP_USERTYPE_EDITION",28);
  65  define("GEOIP_USERTYPE_EDITION_V6",29);
  66  define("GEOIP_ASNUM_EDITION_V6",21);
  67  define("GEOIP_ISP_EDITION_V6",22);
  68  define("GEOIP_ORG_EDITION_V6",23);
  69  define("GEOIP_DOMAIN_EDITION_V6",24);
  70  
  71  define("CITYCOMBINED_FIXED_RECORD", 7 );
  72  
  73  class GeoIP {
  74      var $flags;
  75      var $filehandle;
  76      var $memory_buffer;
  77      var $databaseType;
  78      var $databaseSegments;
  79      var $record_length;
  80      var $shmid;
  81      var $GEOIP_COUNTRY_CODE_TO_NUMBER = array(
  82  "" => 0, "AP" => 1, "EU" => 2, "AD" => 3, "AE" => 4, "AF" => 5,
  83  "AG" => 6, "AI" => 7, "AL" => 8, "AM" => 9, "AN" => 10, "AO" => 11,
  84  "AQ" => 12, "AR" => 13, "AS" => 14, "AT" => 15, "AU" => 16, "AW" => 17,
  85  "AZ" => 18, "BA" => 19, "BB" => 20, "BD" => 21, "BE" => 22, "BF" => 23,
  86  "BG" => 24, "BH" => 25, "BI" => 26, "BJ" => 27, "BM" => 28, "BN" => 29,
  87  "BO" => 30, "BR" => 31, "BS" => 32, "BT" => 33, "BV" => 34, "BW" => 35,
  88  "BY" => 36, "BZ" => 37, "CA" => 38, "CC" => 39, "CD" => 40, "CF" => 41,
  89  "CG" => 42, "CH" => 43, "CI" => 44, "CK" => 45, "CL" => 46, "CM" => 47,
  90  "CN" => 48, "CO" => 49, "CR" => 50, "CU" => 51, "CV" => 52, "CX" => 53,
  91  "CY" => 54, "CZ" => 55, "DE" => 56, "DJ" => 57, "DK" => 58, "DM" => 59,
  92  "DO" => 60, "DZ" => 61, "EC" => 62, "EE" => 63, "EG" => 64, "EH" => 65,
  93  "ER" => 66, "ES" => 67, "ET" => 68, "FI" => 69, "FJ" => 70, "FK" => 71,
  94  "FM" => 72, "FO" => 73, "FR" => 74, "FX" => 75, "GA" => 76, "GB" => 77,
  95  "GD" => 78, "GE" => 79, "GF" => 80, "GH" => 81, "GI" => 82, "GL" => 83,
  96  "GM" => 84, "GN" => 85, "GP" => 86, "GQ" => 87, "GR" => 88, "GS" => 89,
  97  "GT" => 90, "GU" => 91, "GW" => 92, "GY" => 93, "HK" => 94, "HM" => 95,
  98  "HN" => 96, "HR" => 97, "HT" => 98, "HU" => 99, "ID" => 100, "IE" => 101,
  99  "IL" => 102, "IN" => 103, "IO" => 104, "IQ" => 105, "IR" => 106, "IS" => 107,
 100  "IT" => 108, "JM" => 109, "JO" => 110, "JP" => 111, "KE" => 112, "KG" => 113,
 101  "KH" => 114, "KI" => 115, "KM" => 116, "KN" => 117, "KP" => 118, "KR" => 119,
 102  "KW" => 120, "KY" => 121, "KZ" => 122, "LA" => 123, "LB" => 124, "LC" => 125,
 103  "LI" => 126, "LK" => 127, "LR" => 128, "LS" => 129, "LT" => 130, "LU" => 131,
 104  "LV" => 132, "LY" => 133, "MA" => 134, "MC" => 135, "MD" => 136, "MG" => 137,
 105  "MH" => 138, "MK" => 139, "ML" => 140, "MM" => 141, "MN" => 142, "MO" => 143,
 106  "MP" => 144, "MQ" => 145, "MR" => 146, "MS" => 147, "MT" => 148, "MU" => 149,
 107  "MV" => 150, "MW" => 151, "MX" => 152, "MY" => 153, "MZ" => 154, "NA" => 155,
 108  "NC" => 156, "NE" => 157, "NF" => 158, "NG" => 159, "NI" => 160, "NL" => 161,
 109  "NO" => 162, "NP" => 163, "NR" => 164, "NU" => 165, "NZ" => 166, "OM" => 167,
 110  "PA" => 168, "PE" => 169, "PF" => 170, "PG" => 171, "PH" => 172, "PK" => 173,
 111  "PL" => 174, "PM" => 175, "PN" => 176, "PR" => 177, "PS" => 178, "PT" => 179,
 112  "PW" => 180, "PY" => 181, "QA" => 182, "RE" => 183, "RO" => 184, "RU" => 185,
 113  "RW" => 186, "SA" => 187, "SB" => 188, "SC" => 189, "SD" => 190, "SE" => 191,
 114  "SG" => 192, "SH" => 193, "SI" => 194, "SJ" => 195, "SK" => 196, "SL" => 197,
 115  "SM" => 198, "SN" => 199, "SO" => 200, "SR" => 201, "ST" => 202, "SV" => 203,
 116  "SY" => 204, "SZ" => 205, "TC" => 206, "TD" => 207, "TF" => 208, "TG" => 209,
 117  "TH" => 210, "TJ" => 211, "TK" => 212, "TM" => 213, "TN" => 214, "TO" => 215,
 118  "TL" => 216, "TR" => 217, "TT" => 218, "TV" => 219, "TW" => 220, "TZ" => 221,
 119  "UA" => 222, "UG" => 223, "UM" => 224, "US" => 225, "UY" => 226, "UZ" => 227,
 120  "VA" => 228, "VC" => 229, "VE" => 230, "VG" => 231, "VI" => 232, "VN" => 233,
 121  "VU" => 234, "WF" => 235, "WS" => 236, "YE" => 237, "YT" => 238, "RS" => 239,
 122  "ZA" => 240, "ZM" => 241, "ME" => 242, "ZW" => 243, "A1" => 244, "A2" => 245,
 123  "O1" => 246, "AX" => 247, "GG" => 248, "IM" => 249, "JE" => 250, "BL" => 251,
 124  "MF" => 252
 125  );
 126      var $GEOIP_COUNTRY_CODES = array(
 127  "", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ",
 128  "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH",
 129  "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA",
 130  "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU",
 131  "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG",
 132  "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB",
 133  "GD", "GE", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT",
 134  "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN",
 135  "IO", "IQ", "IR", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM",
 136  "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS",
 137  "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML", "MM", "MN",
 138  "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
 139  "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA",
 140  "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
 141  "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI",
 142  "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TC", "TD",
 143  "TF", "TG", "TH", "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW",
 144  "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN",
 145  "VU", "WF", "WS", "YE", "YT", "RS", "ZA", "ZM", "ME", "ZW", "A1", "A2", "O1",
 146  "AX", "GG", "IM", "JE", "BL", "MF"
 147  );
 148      var $GEOIP_COUNTRY_CODES3 = array(
 149  "","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT","AGO","ATA","ARG",
 150  "ASM","AUT","AUS","ABW","AZE","BIH","BRB","BGD","BEL","BFA","BGR","BHR","BDI",
 151  "BEN","BMU","BRN","BOL","BRA","BHS","BTN","BVT","BWA","BLR","BLZ","CAN","CCK",
 152  "COD","CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI","CUB","CPV",
 153  "CXR","CYP","CZE","DEU","DJI","DNK","DMA","DOM","DZA","ECU","EST","EGY","ESH",
 154  "ERI","ESP","ETH","FIN","FJI","FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD",
 155  "GEO","GUF","GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","SGS","GTM","GUM",
 156  "GNB","GUY","HKG","HMD","HND","HRV","HTI","HUN","IDN","IRL","ISR","IND","IOT",
 157  "IRQ","IRN","ISL","ITA","JAM","JOR","JPN","KEN","KGZ","KHM","KIR","COM","KNA",
 158  "PRK","KOR","KWT","CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
 159  "LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI","MMR","MNG","MAC",
 160  "MNP","MTQ","MRT","MSR","MLT","MUS","MDV","MWI","MEX","MYS","MOZ","NAM","NCL",
 161  "NER","NFK","NGA","NIC","NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER",
 162  "PYF","PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW","PRY","QAT",
 163  "REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN","SWE","SGP","SHN","SVN","SJM",
 164  "SVK","SLE","SMR","SEN","SOM","SUR","STP","SLV","SYR","SWZ","TCA","TCD","ATF",
 165  "TGO","THA","TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN","TZA",
 166  "UKR","UGA","UMI","USA","URY","UZB","VAT","VCT","VEN","VGB","VIR","VNM","VUT",
 167  "WLF","WSM","YEM","MYT","SRB","ZAF","ZMB","MNE","ZWE","A1","A2","O1",
 168  "ALA","GGY","IMN","JEY","BLM","MAF"
 169      );
 170      var $GEOIP_COUNTRY_NAMES = array(
 171  "", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates",
 172  "Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia",
 173  "Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa",
 174  "Austria", "Australia", "Aruba", "Azerbaijan", "Bosnia and Herzegovina",
 175  "Barbados", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Bahrain",
 176  "Burundi", "Benin", "Bermuda", "Brunei Darussalam", "Bolivia", "Brazil",
 177  "Bahamas", "Bhutan", "Bouvet Island", "Botswana", "Belarus", "Belize",
 178  "Canada", "Cocos (Keeling) Islands", "Congo, The Democratic Republic of the",
 179  "Central African Republic", "Congo", "Switzerland", "Cote D'Ivoire", "Cook Islands",
 180  "Chile", "Cameroon", "China", "Colombia", "Costa Rica", "Cuba", "Cape Verde",
 181  "Christmas Island", "Cyprus", "Czech Republic", "Germany", "Djibouti",
 182  "Denmark", "Dominica", "Dominican Republic", "Algeria", "Ecuador", "Estonia",
 183  "Egypt", "Western Sahara", "Eritrea", "Spain", "Ethiopia", "Finland", "Fiji",
 184  "Falkland Islands (Malvinas)", "Micronesia, Federated States of", "Faroe Islands",
 185  "France", "France, Metropolitan", "Gabon", "United Kingdom",
 186  "Grenada", "Georgia", "French Guiana", "Ghana", "Gibraltar", "Greenland",
 187  "Gambia", "Guinea", "Guadeloupe", "Equatorial Guinea", "Greece", "South Georgia and the South Sandwich Islands",
 188  "Guatemala", "Guam", "Guinea-Bissau",
 189  "Guyana", "Hong Kong", "Heard Island and McDonald Islands", "Honduras",
 190  "Croatia", "Haiti", "Hungary", "Indonesia", "Ireland", "Israel", "India",
 191  "British Indian Ocean Territory", "Iraq", "Iran, Islamic Republic of",
 192  "Iceland", "Italy", "Jamaica", "Jordan", "Japan", "Kenya", "Kyrgyzstan",
 193  "Cambodia", "Kiribati", "Comoros", "Saint Kitts and Nevis", "Korea, Democratic People's Republic of",
 194  "Korea, Republic of", "Kuwait", "Cayman Islands",
 195  "Kazakhstan", "Lao People's Democratic Republic", "Lebanon", "Saint Lucia",
 196  "Liechtenstein", "Sri Lanka", "Liberia", "Lesotho", "Lithuania", "Luxembourg",
 197  "Latvia", "Libyan Arab Jamahiriya", "Morocco", "Monaco", "Moldova, Republic of",
 198  "Madagascar", "Marshall Islands", "Macedonia",
 199  "Mali", "Myanmar", "Mongolia", "Macau", "Northern Mariana Islands",
 200  "Martinique", "Mauritania", "Montserrat", "Malta", "Mauritius", "Maldives",
 201  "Malawi", "Mexico", "Malaysia", "Mozambique", "Namibia", "New Caledonia",
 202  "Niger", "Norfolk Island", "Nigeria", "Nicaragua", "Netherlands", "Norway",
 203  "Nepal", "Nauru", "Niue", "New Zealand", "Oman", "Panama", "Peru", "French Polynesia",
 204  "Papua New Guinea", "Philippines", "Pakistan", "Poland", "Saint Pierre and Miquelon",
 205  "Pitcairn Islands", "Puerto Rico", "Palestinian Territory",
 206  "Portugal", "Palau", "Paraguay", "Qatar", "Reunion", "Romania",
 207  "Russian Federation", "Rwanda", "Saudi Arabia", "Solomon Islands",
 208  "Seychelles", "Sudan", "Sweden", "Singapore", "Saint Helena", "Slovenia",
 209  "Svalbard and Jan Mayen", "Slovakia", "Sierra Leone", "San Marino", "Senegal",
 210  "Somalia", "Suriname", "Sao Tome and Principe", "El Salvador", "Syrian Arab Republic",
 211  "Swaziland", "Turks and Caicos Islands", "Chad", "French Southern Territories",
 212  "Togo", "Thailand", "Tajikistan", "Tokelau", "Turkmenistan",
 213  "Tunisia", "Tonga", "Timor-Leste", "Turkey", "Trinidad and Tobago", "Tuvalu",
 214  "Taiwan", "Tanzania, United Republic of", "Ukraine",
 215  "Uganda", "United States Minor Outlying Islands", "United States", "Uruguay",
 216  "Uzbekistan", "Holy See (Vatican City State)", "Saint Vincent and the Grenadines",
 217  "Venezuela", "Virgin Islands, British", "Virgin Islands, U.S.",
 218  "Vietnam", "Vanuatu", "Wallis and Futuna", "Samoa", "Yemen", "Mayotte",
 219  "Serbia", "South Africa", "Zambia", "Montenegro", "Zimbabwe",
 220  "Anonymous Proxy","Satellite Provider","Other",
 221  "Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy","Saint Martin"
 222  );
 223  
 224      var $GEOIP_CONTINENT_CODES = array(
 225    "--", "AS", "EU", "EU", "AS", "AS", "NA", "NA", "EU", "AS",
 226    "NA", "AF", "AN", "SA", "OC", "EU", "OC", "NA", "AS", "EU",
 227    "NA", "AS", "EU", "AF", "EU", "AS", "AF", "AF", "NA", "AS",
 228    "SA", "SA", "NA", "AS", "AN", "AF", "EU", "NA", "NA", "AS",
 229    "AF", "AF", "AF", "EU", "AF", "OC", "SA", "AF", "AS", "SA",
 230    "NA", "NA", "AF", "AS", "AS", "EU", "EU", "AF", "EU", "NA",
 231    "NA", "AF", "SA", "EU", "AF", "AF", "AF", "EU", "AF", "EU",
 232    "OC", "SA", "OC", "EU", "EU", "EU", "AF", "EU", "NA", "AS",
 233    "SA", "AF", "EU", "NA", "AF", "AF", "NA", "AF", "EU", "AN",
 234    "NA", "OC", "AF", "SA", "AS", "AN", "NA", "EU", "NA", "EU",
 235    "AS", "EU", "AS", "AS", "AS", "AS", "AS", "EU", "EU", "NA",
 236    "AS", "AS", "AF", "AS", "AS", "OC", "AF", "NA", "AS", "AS",
 237    "AS", "NA", "AS", "AS", "AS", "NA", "EU", "AS", "AF", "AF",
 238    "EU", "EU", "EU", "AF", "AF", "EU", "EU", "AF", "OC", "EU",
 239    "AF", "AS", "AS", "AS", "OC", "NA", "AF", "NA", "EU", "AF",
 240    "AS", "AF", "NA", "AS", "AF", "AF", "OC", "AF", "OC", "AF",
 241    "NA", "EU", "EU", "AS", "OC", "OC", "OC", "AS", "NA", "SA",
 242    "OC", "OC", "AS", "AS", "EU", "NA", "OC", "NA", "AS", "EU",
 243    "OC", "SA", "AS", "AF", "EU", "EU", "AF", "AS", "OC", "AF",
 244    "AF", "EU", "AS", "AF", "EU", "EU", "EU", "AF", "EU", "AF",
 245    "AF", "SA", "AF", "NA", "AS", "AF", "NA", "AF", "AN", "AF",
 246    "AS", "AS", "OC", "AS", "AF", "OC", "AS", "EU", "NA", "OC",
 247    "AS", "AF", "EU", "AF", "OC", "NA", "SA", "AS", "EU", "NA",
 248    "SA", "NA", "NA", "AS", "OC", "OC", "OC", "AS", "AF", "EU",
 249    "AF", "AF", "EU", "AF", "--", "--", "--", "EU", "EU", "EU",
 250    "EU", "NA", "NA"
 251  );
 252  
 253  }
 254  function geoip_load_shared_mem ($file) {
 255  
 256    $fp = fopen($file, "rb");
 257    if (!$fp) {
 258      print "error opening $file: $php_errormsg\n";
 259      exit;
 260    }
 261    $s_array = fstat($fp);
 262    $size = $s_array['size'];
 263    if ($shmid = @shmop_open (GEOIP_SHM_KEY, "w", 0, 0)) {
 264      shmop_delete ($shmid);
 265      shmop_close ($shmid);
 266    }
 267    $shmid = shmop_open (GEOIP_SHM_KEY, "c", 0644, $size);
 268    shmop_write ($shmid, fread($fp, $size), 0);
 269    shmop_close ($shmid);
 270  }
 271  
 272  function _setup_segments($gi){
 273    $gi->databaseType = GEOIP_COUNTRY_EDITION;
 274    $gi->record_length = STANDARD_RECORD_LENGTH;
 275    if ($gi->flags & GEOIP_SHARED_MEMORY) {
 276      $offset = @shmop_size ($gi->shmid) - 3;
 277      for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
 278          $delim = @shmop_read ($gi->shmid, $offset, 3);
 279          $offset += 3;
 280          if ($delim == (chr(255).chr(255).chr(255))) {
 281              $gi->databaseType = ord(@shmop_read ($gi->shmid, $offset, 1));
 282              $offset++;
 283  
 284              if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
 285                  $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
 286              } else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
 287                  $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
 288          } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)||
 289                       ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
 290                      || ($gi->databaseType == GEOIP_ORG_EDITION)
 291                      || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
 292                      || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
 293                      || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
 294              || ($gi->databaseType == GEOIP_ISP_EDITION)
 295              || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
 296                    || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
 297              || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
 298              || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
 299              || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
 300              || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
 301              || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
 302                      || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
 303                      || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
 304              || ($gi->databaseType == GEOIP_ASNUM_EDITION)
 305              || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
 306                  $gi->databaseSegments = 0;
 307                  $buf = @shmop_read ($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
 308                  for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
 309                      $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
 310                  }
 311                  if (($gi->databaseType == GEOIP_ORG_EDITION)
 312                      || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
 313                          || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
 314                          || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
 315              || ($gi->databaseType == GEOIP_ISP_EDITION)
 316              || ($gi->databaseType == GEOIP_ISP_EDITION_V6)) {
 317                      $gi->record_length = ORG_RECORD_LENGTH;
 318                  }
 319              }
 320              break;
 321          } else {
 322              $offset -= 4;
 323          }
 324      }
 325      if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
 326          ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
 327          ($gi->databaseType == GEOIP_PROXY_EDITION)||
 328          ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
 329          $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
 330      }
 331    } else {
 332      $filepos = ftell($gi->filehandle);
 333      fseek($gi->filehandle, -3, SEEK_END);
 334      for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
 335          $delim = fread($gi->filehandle,3);
 336          if ($delim == (chr(255).chr(255).chr(255))){
 337          $gi->databaseType = ord(fread($gi->filehandle,1));
 338          if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
 339              $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
 340          }
 341          else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
 342          $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
 343                  }  else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)
 344                      || ($gi->databaseType == GEOIP_CITY_EDITION_REV1)
 345                      || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
 346                      || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
 347                      || ($gi->databaseType == GEOIP_ORG_EDITION)
 348                      || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
 349              || ($gi->databaseType == GEOIP_ISP_EDITION)
 350                      || ($gi->databaseType == GEOIP_ORG_EDITION_V6)
 351                      || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6)
 352              || ($gi->databaseType == GEOIP_ISP_EDITION_V6)
 353              || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
 354              || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
 355                      || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6)
 356              || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6)
 357                      || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1)
 358                      || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6)
 359                  || ($gi->databaseType == GEOIP_USERTYPE_EDITION)
 360              || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6)
 361                      || ($gi->databaseType == GEOIP_ASNUM_EDITION)
 362                      || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6)){
 363              $gi->databaseSegments = 0;
 364              $buf = fread($gi->filehandle,SEGMENT_RECORD_LENGTH);
 365              for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
 366              $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
 367              }
 368          if (   ( $gi->databaseType == GEOIP_ORG_EDITION )
 369                  || ( $gi->databaseType == GEOIP_DOMAIN_EDITION )
 370                  || ( $gi->databaseType == GEOIP_ISP_EDITION )
 371                  || ( $gi->databaseType == GEOIP_ORG_EDITION_V6 )
 372                  || ( $gi->databaseType == GEOIP_DOMAIN_EDITION_V6 )
 373                  || ( $gi->databaseType == GEOIP_ISP_EDITION_V6 )) {
 374          $gi->record_length = ORG_RECORD_LENGTH;
 375              }
 376          }
 377          break;
 378          } else {
 379          fseek($gi->filehandle, -4, SEEK_CUR);
 380          }
 381      }
 382      if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
 383          ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6)||
 384          ($gi->databaseType == GEOIP_PROXY_EDITION)||
 385          ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
 386           $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
 387      }
 388      fseek($gi->filehandle,$filepos,SEEK_SET);
 389    }
 390    return $gi;
 391  }
 392  
 393  function geoip_open($filename, $flags) {
 394    $gi = new GeoIP;
 395    $gi->flags = $flags;
 396    if ($gi->flags & GEOIP_SHARED_MEMORY) {
 397      $gi->shmid = @shmop_open (GEOIP_SHM_KEY, "a", 0, 0);
 398      } else {
 399      $gi->filehandle = fopen($filename,"rb") or die( "Can not open $filename\n" );
 400      if ($gi->flags & GEOIP_MEMORY_CACHE) {
 401          $s_array = fstat($gi->filehandle);
 402          $gi->memory_buffer = fread($gi->filehandle, $s_array['size']);
 403      }
 404    }
 405  
 406    $gi = _setup_segments($gi);
 407    return $gi;
 408  }
 409  
 410  function geoip_close($gi) {
 411    if ($gi->flags & GEOIP_SHARED_MEMORY) {
 412      return true;
 413    }
 414  
 415    return fclose($gi->filehandle);
 416  }
 417  
 418  function geoip_country_id_by_name_v6($gi, $name) {
 419    $rec = dns_get_record($name, DNS_AAAA);
 420    if ( !$rec ) {
 421      return false;
 422    }
 423    $addr = $rec[0]["ipv6"];
 424    if (!$addr || $addr == $name) {
 425      return false;
 426    }
 427    return geoip_country_id_by_addr_v6($gi, $addr);
 428  }
 429  
 430  function geoip_country_id_by_name($gi, $name) {
 431    $addr = gethostbyname($name);
 432    if (!$addr || $addr == $name) {
 433      return false;
 434    }
 435    return geoip_country_id_by_addr($gi, $addr);
 436  }
 437  
 438  function geoip_country_code_by_name_v6($gi, $name) {
 439    $country_id = geoip_country_id_by_name_v6($gi,$name);
 440    if ($country_id !== false) {
 441          return $gi->GEOIP_COUNTRY_CODES[$country_id];
 442    }
 443    return false;
 444  }
 445  
 446  function geoip_country_code_by_hostname($gi, $name) {
 447    $country_id = geoip_country_id_by_name($gi,$name);
 448    if ($country_id !== false) {
 449          return $gi->GEOIP_COUNTRY_CODES[$country_id];
 450    }
 451    return false;
 452  }
 453  
 454  function geoip_country_name_by_name_v6($gi, $name) {
 455    $country_id = geoip_country_id_by_name_v6($gi,$name);
 456    if ($country_id !== false) {
 457          return $gi->GEOIP_COUNTRY_NAMES[$country_id];
 458    }
 459    return false;
 460  }
 461  
 462  function geoip_country_name_by_hostname($gi, $name) {
 463    $country_id = geoip_country_id_by_name($gi,$name);
 464    if ($country_id !== false) {
 465          return $gi->GEOIP_COUNTRY_NAMES[$country_id];
 466    }
 467    return false;
 468  }
 469  
 470  function geoip_country_id_by_addr_v6($gi, $addr) {
 471    $ipnum = inet_pton($addr);
 472    return _geoip_seek_country_v6($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
 473  }
 474  
 475  function geoip_country_id_by_addr($gi, $addr) {
 476    $ipnum = ip2long($addr);
 477    return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
 478  }
 479  
 480  function geoip_country_code_by_addr_v6($gi, $addr) {
 481      $country_id = geoip_country_id_by_addr_v6($gi,$addr);
 482      if ($country_id !== false) {
 483        return $gi->GEOIP_COUNTRY_CODES[$country_id];
 484      }
 485    return false;
 486  }
 487  
 488  function geoip_country_code_by_addr($gi, $addr) {
 489    if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
 490      $record = geoip_record_by_addr($gi,$addr);
 491      if ( $record !== false ) {
 492        return $record->country_code;
 493      }
 494    } else {
 495      $country_id = geoip_country_id_by_addr($gi,$addr);
 496      if ($country_id !== false) {
 497        return $gi->GEOIP_COUNTRY_CODES[$country_id];
 498      }
 499    }
 500    return false;
 501  }
 502  
 503  function geoip_country_name_by_addr_v6($gi, $addr) {
 504      $country_id = geoip_country_id_by_addr_v6($gi,$addr);
 505      if ($country_id !== false) {
 506        return $gi->GEOIP_COUNTRY_NAMES[$country_id];
 507      }
 508    return false;
 509  }
 510  
 511  function geoip_country_name_by_addr($gi, $addr) {
 512    if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
 513      $record = geoip_record_by_addr($gi,$addr);
 514      return $record->country_name;
 515    } else {
 516      $country_id = geoip_country_id_by_addr($gi,$addr);
 517      if ($country_id !== false) {
 518        return $gi->GEOIP_COUNTRY_NAMES[$country_id];
 519      }
 520    }
 521    return false;
 522  }
 523  
 524  function _geoip_seek_country_v6($gi, $ipnum) {
 525  
 526    # arrays from unpack start with offset 1
 527    # yet another php mystery. array_merge work around
 528    # this broken behaviour
 529    $v6vec = array_merge(unpack( "C16", $ipnum));
 530  
 531    $offset = 0;
 532    for ($depth = 127; $depth >= 0; --$depth) {
 533      if ($gi->flags & GEOIP_MEMORY_CACHE) {
 534        // workaround php's broken substr, strpos, etc handling with
 535        // mbstring.func_overload and mbstring.internal_encoding
 536        $enc = mb_internal_encoding();
 537         mb_internal_encoding('ISO-8859-1');
 538  
 539        $buf = substr($gi->memory_buffer,
 540                              2 * $gi->record_length * $offset,
 541                              2 * $gi->record_length);
 542  
 543        mb_internal_encoding($enc);
 544      } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
 545        $buf = @shmop_read ($gi->shmid,
 546                              2 * $gi->record_length * $offset,
 547                              2 * $gi->record_length );
 548          } else {
 549        fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
 550          or die("fseek failed");
 551        $buf = fread($gi->filehandle, 2 * $gi->record_length);
 552      }
 553      $x = array(0,0);
 554      for ($i = 0; $i < 2; ++$i) {
 555        for ($j = 0; $j < $gi->record_length; ++$j) {
 556          $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
 557        }
 558      }
 559  
 560      $bnum = 127 - $depth;
 561      $idx = $bnum >> 3;
 562      $b_mask = 1 << ( $bnum & 7 ^ 7 );
 563      if (($v6vec[$idx] & $b_mask) > 0) {
 564        if ($x[1] >= $gi->databaseSegments) {
 565          return $x[1];
 566        }
 567        $offset = $x[1];
 568      } else {
 569        if ($x[0] >= $gi->databaseSegments) {
 570          return $x[0];
 571        }
 572        $offset = $x[0];
 573      }
 574    }
 575    trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
 576    return false;
 577  }
 578  
 579  function _geoip_seek_country($gi, $ipnum) {
 580    $offset = 0;
 581    for ($depth = 31; $depth >= 0; --$depth) {
 582      if ($gi->flags & GEOIP_MEMORY_CACHE) {
 583        // workaround php's broken substr, strpos, etc handling with
 584        // mbstring.func_overload and mbstring.internal_encoding
 585        $enc = mb_internal_encoding();
 586         mb_internal_encoding('ISO-8859-1');
 587  
 588        $buf = substr($gi->memory_buffer,
 589                              2 * $gi->record_length * $offset,
 590                              2 * $gi->record_length);
 591  
 592        mb_internal_encoding($enc);
 593      } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
 594        $buf = @shmop_read ($gi->shmid,
 595                              2 * $gi->record_length * $offset,
 596                              2 * $gi->record_length );
 597          } else {
 598        fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
 599          or die("fseek failed");
 600        $buf = fread($gi->filehandle, 2 * $gi->record_length);
 601      }
 602      $x = array(0,0);
 603      for ($i = 0; $i < 2; ++$i) {
 604        for ($j = 0; $j < $gi->record_length; ++$j) {
 605          $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
 606        }
 607      }
 608      if ($ipnum & (1 << $depth)) {
 609        if ($x[1] >= $gi->databaseSegments) {
 610          return $x[1];
 611        }
 612        $offset = $x[1];
 613          } else {
 614        if ($x[0] >= $gi->databaseSegments) {
 615          return $x[0];
 616        }
 617        $offset = $x[0];
 618      }
 619    }
 620    trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
 621    return false;
 622  }
 623  
 624  function _common_get_org($gi, $seek_org){
 625    $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments;
 626    if ($gi->flags & GEOIP_SHARED_MEMORY) {
 627      $org_buf = @shmop_read ($gi->shmid, $record_pointer, MAX_ORG_RECORD_LENGTH);
 628      } else {
 629      fseek($gi->filehandle, $record_pointer, SEEK_SET);
 630      $org_buf = fread($gi->filehandle,MAX_ORG_RECORD_LENGTH);
 631    }
 632    // workaround php's broken substr, strpos, etc handling with
 633    // mbstring.func_overload and mbstring.internal_encoding
 634    $enc = mb_internal_encoding();
 635    mb_internal_encoding('ISO-8859-1');
 636    $org_buf = substr($org_buf, 0, strpos($org_buf, "\0"));
 637    mb_internal_encoding($enc);
 638    return $org_buf;
 639  }
 640  
 641  function _get_org_v6($gi,$ipnum){
 642    $seek_org = _geoip_seek_country_v6($gi,$ipnum);
 643    if ($seek_org == $gi->databaseSegments) {
 644      return NULL;
 645    }
 646    return _common_get_org($gi, $seek_org);
 647  }
 648  
 649  function _get_org($gi,$ipnum){
 650    $seek_org = _geoip_seek_country($gi,$ipnum);
 651    if ($seek_org == $gi->databaseSegments) {
 652      return NULL;
 653    }
 654    return _common_get_org($gi, $seek_org);
 655  }
 656  
 657  
 658  
 659  function geoip_name_by_addr_v6 ($gi,$addr) {
 660    if ($addr == NULL) {
 661      return 0;
 662    }
 663    $ipnum = inet_pton($addr);
 664    return _get_org_v6($gi, $ipnum);
 665  }
 666  
 667  function geoip_name_by_addr ($gi,$addr) {
 668    if ($addr == NULL) {
 669      return 0;
 670    }
 671    $ipnum = ip2long($addr);
 672    return _get_org($gi, $ipnum);
 673  }
 674  
 675  function geoip_org_by_addr ($gi,$addr) {
 676    return geoip_name_by_addr($gi, $addr);
 677  }
 678  
 679  function _get_region($gi,$ipnum){
 680    if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
 681      $seek_region = _geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV0;
 682      if ($seek_region >= 1000){
 683        $country_code = "US";
 684        $region = chr(($seek_region - 1000)/26 + 65) . chr(($seek_region - 1000)%26 + 65);
 685      } else {
 686              $country_code = $gi->GEOIP_COUNTRY_CODES[$seek_region];
 687        $region = "";
 688      }
 689    return array ($country_code,$region);
 690      }  else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
 691      $seek_region = _geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV1;
 692      //print $seek_region;
 693      if ($seek_region < US_OFFSET){
 694        $country_code = "";
 695        $region = "";
 696          } else if ($seek_region < CANADA_OFFSET) {
 697        $country_code = "US";
 698        $region = chr(($seek_region - US_OFFSET)/26 + 65) . chr(($seek_region - US_OFFSET)%26 + 65);
 699          } else if ($seek_region < WORLD_OFFSET) {
 700        $country_code = "CA";
 701        $region = chr(($seek_region - CANADA_OFFSET)/26 + 65) . chr(($seek_region - CANADA_OFFSET)%26 + 65);
 702      } else {
 703              $country_code = $gi->GEOIP_COUNTRY_CODES[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
 704        $region = "";
 705      }
 706    return array ($country_code,$region);
 707    }
 708  }
 709  
 710  function geoip_region_by_addr ($gi,$addr) {
 711    if ($addr == NULL) {
 712      return 0;
 713    }
 714    $ipnum = ip2long($addr);
 715    return _get_region($gi, $ipnum);
 716  }
 717  
 718  function getdnsattributes ($l,$ip){
 719    $r = new Net_DNS_Resolver();
 720    $r->nameservers = array("ws1.maxmind.com");
 721    $p = $r->search($l."." . $ip .".s.maxmind.com","TXT","IN");
 722    $str = is_object($p->answer[0])?$p->answer[0]->string():'';
 723    $str = substr( $str, 1, -1 );
 724    return $str;
 725  }
 726  
 727  ?>

title

Description

title

Description

title

Description

title

title

Body