| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * Horde optimized interface to the MaxMind IP Address->Country 4 * listing. 5 * 6 * $Horde: framework/NLS/NLS/GeoIP.php,v 1.10.10.7 2006/06/27 15:19:12 slusarz Exp $ 7 * 8 * Based on PHP geoip.inc library by MaxMind LLC: 9 * http://www.maxmind.com/download/geoip/api/php/ 10 * 11 * Originally based on php version of the geoip library written in May 12 * 2002 by jim winstead <jimw@apache.org> 13 * 14 * Copyright (C) 2003 MaxMind LLC 15 * Copyright 2003-2006 Michael Slusarz <slusarz@bigworm.colorado.edu> 16 * 17 * This library is free software; you can redistribute it and/or 18 * modify it under the terms of the GNU Lesser General Public 19 * License as published by the Free Software Foundation; either 20 * version 2.1 of the License, or (at your option) any later version. 21 * 22 * See the enclosed file COPYING for license information (LGPL). If you 23 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 24 * 25 * @author Michael Slusarz <slusarz@bigworm.colorado.edu> 26 * @since Horde 3.0 27 * @package Horde_NLS 28 */ 29 30 /* Country list. */ 31 $GLOBALS['GEOIP_COUNTRY_CODES'] = array( 32 '', 'AP', 'EU', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ', 33 'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 34 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 35 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 36 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 37 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'FX', 'GA', 'UK', 38 'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 39 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 40 'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 41 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 42 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 43 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 44 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 45 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 46 'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 47 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 48 'TF', 'TG', 'TH', 'TJ', 'TK', 'TM', 'TN', 'TO', 'TP', 'TR', 'TT', 'TV', 'TW', 49 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 50 'VU', 'WF', 'WS', 'YE', 'YT', 'YU', 'ZA', 'ZM', 'ZR', 'ZW', 'A1', 'A2', 'O1' 51 ); 52 53 /* Country Names. */ 54 $GLOBALS['GEOIP_COUNTRY_NAMES'] = array( 55 "", _("Asia/Pacific Region"), _("Europe"), _("Andorra"), 56 _("United Arab Emirates"), _("Afghanistan"), _("Antigua and Barbuda"), 57 _("Anguilla"), _("Albania"), _("Armenia"), _("Netherlands Antilles"), 58 _("Angola"), _("Antarctica"), _("Argentina"), _("American Samoa"), 59 _("Austria"), _("Australia"), _("Aruba"), _("Azerbaijan"), 60 _("Bosnia and Herzegovina"), _("Barbados"), _("Bangladesh"), _("Belgium"), 61 _("Burkina Faso"), _("Bulgaria"), _("Bahrain"), _("Burundi"), _("Benin"), 62 _("Bermuda"), _("Brunei Darussalam"), _("Bolivia"), _("Brazil"), _("Bahamas"), 63 _("Bhutan"), _("Bouvet Island"), _("Botswana"), _("Belarus"), _("Belize"), 64 _("Canada"), _("Cocos (Keeling) Islands"), 65 _("Congo, The Democratic Republic of the"), _("Central African Republic"), 66 _("Congo"), _("Switzerland"), _("Cote d'Ivoire"), _("Cook Islands"), 67 _("Chile"), _("Cameroon"), _("China"), _("Colombia"), _("Costa Rica"), 68 _("Cuba"), _("Cape Verde"), _("Christmas Island"), _("Cyprus"), 69 _("Czech Republic"), _("Germany"), _("Djibouti"), _("Denmark"), _("Dominica"), 70 _("Dominican Republic"), _("Algeria"), _("Ecuador"), _("Estonia"), _("Egypt"), 71 _("Western Sahara"), _("Eritrea"), _("Spain"), _("Ethiopia"), _("Finland"), 72 _("Fiji"), _("Falkland Islands (Malvinas)"), _("Micronesia, Federated States of"), 73 _("Faroe Islands"), _("France"), _("France, Metropolitan"), _("Gabon"), 74 _("United Kingdom"), _("Grenada"), _("Georgia"), _("French Guiana"), 75 _("Ghana"), _("Gibraltar"), _("Greenland"), _("Gambia"), _("Guinea"), 76 _("Guadeloupe"), _("Equatorial Guinea"), _("Greece"), 77 _("South Georgia and the South Sandwich Islands"), _("Guatemala"), _("Guam"), 78 _("Guinea-Bissau"), _("Guyana"), _("Hong Kong"), 79 _("Heard Island and McDonald Islands"), _("Honduras"), _("Croatia"), 80 _("Haiti"), _("Hungary"), _("Indonesia"), _("Ireland"), _("Israel"), 81 _("India"), _("British Indian Ocean Territory"), _("Iraq"), 82 _("Iran, Islamic Republic of"), _("Iceland"), _("Italy"), _("Jamaica"), 83 _("Jordan"), _("Japan"), _("Kenya"), _("Kyrgyzstan"), _("Cambodia"), 84 _("Kiribati"), _("Comoros"), _("Saint Kitts and Nevis"), 85 _("Korea, Democratic People's Republic of"), _("Korea, Republic of"), 86 _("Kuwait"), _("Cayman Islands"), _("Kazakhstan"), 87 _("Lao People's Democratic Republic"), _("Lebanon"), _("Saint Lucia"), 88 _("Liechtenstein"), _("Sri Lanka"), _("Liberia"), _("Lesotho"), 89 _("Lithuania"), _("Luxembourg"), _("Latvia"), _("Libyan Arab Jamahiriya"), 90 _("Morocco"), _("Monaco"), _("Moldova, Republic of"), _("Madagascar"), 91 _("Marshall Islands"), _("Macedonia, The Former Yugoslav Republic of"), 92 _("Mali"), _("Myanmar"), _("Mongolia"), _("Macao"), 93 _("Northern Mariana Islands"), _("Martinique"), _("Mauritania"), 94 _("Montserrat"), _("Malta"), _("Mauritius"), _("Maldives"), _("Malawi"), 95 _("Mexico"), _("Malaysia"), _("Mozambique"), _("Namibia"), _("New Caledonia"), 96 _("Niger"), _("Norfolk Island"), _("Nigeria"), _("Nicaragua"), 97 _("Netherlands"), _("Norway"), _("Nepal"), _("Nauru"), _("Niue"), 98 _("New Zealand"), _("Oman"), _("Panama"), _("Peru"), _("French Polynesia"), 99 _("Papua New Guinea"), _("Philippines"), _("Pakistan"), _("Poland"), 100 _("Saint Pierre and Miquelon"), _("Pitcairn"), _("Puerto Rico"), 101 _("Palestinian Territory, Occupied"), _("Portugal"), _("Palau"), 102 _("Paraguay"), _("Qatar"), _("Reunion"), _("Romania"), 103 _("Russian Federation"), _("Rwanda"), _("Saudi Arabia"), _("Solomon Islands"), 104 _("Seychelles"), _("Sudan"), _("Sweden"), _("Singapore"), _("Saint Helena"), 105 _("Slovenia"), _("Svalbard and Jan Mayen"), _("Slovakia"), _("Sierra Leone"), 106 _("San Marino"), _("Senegal"), _("Somalia"), _("Suriname"), 107 _("Sao Tome and Principe"), _("El Salvador"), _("Syrian Arab Republic"), 108 _("Swaziland"), _("Turks and Caicos Islands"), _("Chad"), 109 _("French Southern Territories"), _("Togo"), _("Thailand"), _("Tajikistan"), 110 _("Tokelau"), _("Turkmenistan"), _("Tunisia"), _("Tonga"), _("Timor-Leste"), 111 _("Turkey"), _("Trinidad and Tobago"), _("Tuvalu"), _("Taiwan"), 112 _("Tanzania, United Republic of"), _("Ukraine"), _("Uganda"), 113 _("United States Minor Outlying Islands"), _("United States"), _("Uruguay"), 114 _("Uzbekistan"), _("Holy See (Vatican City State)"), 115 _("Saint Vincent and the Grenadines"), _("Venezuela"), 116 _("Virgin Islands, British"), _("Virgin Islands, U.S."), _("Viet Nam"), 117 _("Vanuatu"), _("Wallis and Futuna"), _("Samoa"), _("Yemen"), _("Mayotte"), 118 _("Yugoslavia"), _("South Africa"), _("Zambia"), _("Zaire"), _("Zimbabwe"), 119 _("Anonymous Proxy"), _("Satellite Provider"), _("Other") 120 ); 121 122 define('GEOIP_COUNTRY_BEGIN', 16776960); 123 define('STRUCTURE_INFO_MAX_SIZE', 20); 124 define('STANDARD_RECORD_LENGTH', 3); 125 126 class NLS_GeoIP { 127 128 /** 129 * The location of the GeoIP database. 130 * 131 * @var string 132 */ 133 var $_datafile; 134 135 /** 136 * The open filehandle to the GeoIP database. 137 * 138 * @var resource 139 */ 140 var $_filehandle; 141 142 /** 143 * Returns a reference to the global NLS_GeoIP object, only creating it 144 * if it doesn't already exist. 145 * 146 * This method must be invoked as: 147 * $geoip = &GeoIP::singleton($datafile); 148 * 149 * @param string $datafile The location of the GeoIP database. 150 * 151 * @return object NLS_GeoIP The NLS_GeoIP instance. 152 */ 153 function &singleton($datafile) 154 { 155 static $instance; 156 157 if (!isset($instance)) { 158 $instance = new NLS_GeoIP($datafile); 159 } 160 161 return $instance; 162 } 163 164 /** 165 * Create a NLS_GeoIP instance (Constructor). 166 * 167 * @param string $datafile The location of the GeoIP database. 168 */ 169 function NLS_GeoIP($datafile) 170 { 171 $this->_datafile = $datafile; 172 } 173 174 /** 175 * Open the GeoIP database. 176 * 177 * @access private 178 * 179 * @return boolean False on error. 180 */ 181 function _open() 182 { 183 /* Return if we already have an object. */ 184 if (!empty($this->_filehandle)) { 185 return true; 186 } 187 188 /* Return if no datafile specified. */ 189 if (empty($this->_datafile)) { 190 return false; 191 } 192 193 $this->_filehandle = fopen($this->_datafile, 'rb'); 194 195 $filepos = ftell($this->_filehandle); 196 fseek($this->_filehandle, -3, SEEK_END); 197 for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) { 198 $delim = fread($this->_filehandle, 3); 199 if ($delim == (chr(255) . chr(255) . chr(255))) { 200 break; 201 } else { 202 fseek($this->_filehandle, -4, SEEK_CUR); 203 } 204 } 205 fseek($this->_filehandle, $filepos, SEEK_SET); 206 207 return true; 208 } 209 210 /** 211 * Returns the country ID for a hostname. 212 * 213 * @param string $name The hostname. 214 * 215 * @return integer The GeoIP country ID. 216 */ 217 function countryIdByName($name) 218 { 219 if (!$this->_open()) { 220 return false; 221 } 222 $addr = gethostbyname($name); 223 if (!$addr || ($addr == $name)) { 224 return false; 225 } 226 return $this->countryIdByAddr($addr); 227 } 228 229 /** 230 * Returns the country abbreviation (2-letter) for a hostname. 231 * 232 * @param string $name The hostname. 233 * 234 * @return integer The country abbreviation. 235 */ 236 function countryCodeByName($name) 237 { 238 if ($this->_open()) { 239 $country_id = $this->countryIdByName($name); 240 if ($country_id !== false) { 241 return $GLOBALS['GEOIP_COUNTRY_CODES'][$country_id]; 242 } 243 } 244 return false; 245 } 246 247 /** 248 * Returns the country name for a hostname. 249 * 250 * @param string $name The hostname. 251 * 252 * @return integer The country name. 253 */ 254 function countryNameByName($name) 255 { 256 if ($this->_open()) { 257 $country_id = $this->countryIdByName($name); 258 if ($country_id !== false) { 259 return $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id]; 260 } 261 } 262 return false; 263 } 264 265 /** 266 * Returns the country ID for an IP Address. 267 * 268 * @param string $addr The IP Address. 269 * 270 * @return integer The GeoIP country ID. 271 */ 272 function countryIdByAddr($addr) 273 { 274 if (!$this->_open()) { 275 return false; 276 } 277 $ipnum = ip2long($addr); 278 return ($this->_seekCountry($ipnum) - GEOIP_COUNTRY_BEGIN); 279 } 280 281 /** 282 * Returns the country abbreviation (2-letter) for an IP Address. 283 * 284 * @param string $addr The IP Address. 285 * 286 * @return integer The country abbreviation. 287 */ 288 function countryCodeByAddr($addr) 289 { 290 if ($this->_open()) { 291 $country_id = $this->countryIdByAddr($addr); 292 if ($country_id !== false) { 293 return $GLOBALS['GEOIP_COUNTRY_CODES'][$country_id]; 294 } 295 } 296 return false; 297 } 298 299 /** 300 * Returns the country name for an IP address. 301 * 302 * @param string $addr The IP address. 303 * 304 * @return mixed The country name. 305 */ 306 function countryNameByAddr($addr) 307 { 308 if ($this->_open()) { 309 $country_id = $this->countryIdByAddr($addr); 310 if ($country_id !== false) { 311 return $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id]; 312 } 313 } 314 return false; 315 } 316 317 /** 318 * Finds a country by IP Address in the GeoIP database. 319 * 320 * @access private 321 * 322 * @param string $ipnum The IP Address to search for. 323 * 324 * @return mixed The country ID or false if not found. 325 * Returns PEAR_Error on error. 326 */ 327 function _seekCountry($ipnum) 328 { 329 $offset = 0; 330 for ($depth = 31; $depth >= 0; --$depth) { 331 if (fseek($this->_filehandle, 2 * STANDARD_RECORD_LENGTH * $offset, SEEK_SET) != 0) { 332 return PEAR::raiseError('fseek failed'); 333 } 334 $buf = fread($this->_filehandle, 2 * STANDARD_RECORD_LENGTH); 335 $x = array(0, 0); 336 for ($i = 0; $i < 2; ++$i) { 337 for ($j = 0; $j < STANDARD_RECORD_LENGTH; ++$j) { 338 $x[$i] += ord($buf[STANDARD_RECORD_LENGTH * $i + $j]) << ($j * 8); 339 } 340 } 341 if ($ipnum & (1 << $depth)) { 342 if ($x[1] >= GEOIP_COUNTRY_BEGIN) { 343 return $x[1]; 344 } 345 $offset = $x[1]; 346 } else { 347 if ($x[0] >= GEOIP_COUNTRY_BEGIN) { 348 return $x[0]; 349 } 350 $offset = $x[0]; 351 } 352 } 353 354 return false; 355 } 356 357 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |