/usr/share/php/Horde/Nls/Geoip.php is in php-horde-nls 2.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | <?php
/**
* Horde optimized interface to the MaxMind IP Address->Country listing.
*
* Based on PHP geoip.inc library by MaxMind LLC:
* http://www.maxmind.com/download/geoip/api/php/
*
* Originally based on php version of the geoip library written in May
* 2002 by jim winstead <jimw@apache.org>
*
* Copyright 2003 MaxMind LLC
* Copyright 2003-2016 Horde LLC (http://www.horde.org/)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @package Nls
*/
class Horde_Nls_Geoip
{
/* TODO */
const GEOIP_COUNTRY_BEGIN = 16776960;
const STRUCTURE_INFO_MAX_SIZE = 20;
const STANDARD_RECORD_LENGTH = 3;
/**
* Country list.
*
* @var array
*/
protected $_countryCodes = array(
'', 'AP', 'EU', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO',
'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE',
'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT',
'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI',
'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ',
'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER',
'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'FX', 'GA', 'UK',
'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR',
'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU',
'ID', 'IE', 'IL', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO',
'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY',
'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV',
'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO',
'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ',
'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU',
'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN',
'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RU', 'RW', 'SA',
'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM',
'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG',
'TH', 'TJ', 'TK', 'TM', 'TN', 'TO', 'TP', 'TR', 'TT', 'TV', 'TW',
'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG',
'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'YU', 'ZA', 'ZM', 'ZR',
'ZW', 'A1', 'A2', 'O1'
);
/**
* The location of the GeoIP database.
*
* @var string
*/
protected $_datafile;
/**
* The open filehandle to the GeoIP database.
*
* @var resource
*/
protected $_fh;
/**
* Constructor.
*
* @param string $datafile The location of the GeoIP database.
*/
public function __construct($datafile)
{
$this->_datafile = $datafile;
}
/**
* Open the GeoIP database.
*
* @return boolean False on error.
*/
protected function _open()
{
/* Return if we already have an object. */
if (!empty($this->_fh)) {
return true;
}
/* Return if no datafile specified. */
if (empty($this->_datafile)) {
return false;
}
$this->_fh = fopen($this->_datafile, 'rb');
if (!$this->_fh) {
return false;
}
$filepos = ftell($this->_fh);
fseek($this->_fh, -3, SEEK_END);
for ($i = 0; $i < self::STRUCTURE_INFO_MAX_SIZE; ++$i) {
$delim = fread($this->_fh, 3);
if ($delim == (chr(255) . chr(255) . chr(255))) {
break;
} else {
fseek($this->_fh, -4, SEEK_CUR);
}
}
fseek($this->_fh, $filepos, SEEK_SET);
return true;
}
/**
* Returns the country ID and Name for a given hostname.
*
* @param string $name The hostname.
*
* @return mixed An array with 'code' as the country code and 'name' as
* the country name, or false if not found.
*/
public function getCountryInfo($name)
{
if (Horde_Util::extensionExists('geoip')) {
$id = @geoip_country_code_by_name($name);
$cname = @geoip_country_name_by_name($name);
return (!empty($id) && !empty($cname)) ?
array('code' => Horde_String::lower($id), 'name' => $cname):
false;
}
$id = $this->countryIdByName($name);
if (!empty($id)) {
$code = $this->_countryCodes[$id];
return array(
'code' => Horde_String::lower($code),
'name' => $this->_getName($code)
);
}
return false;
}
/**
* Returns the country ID for a hostname.
*
* @param string $name The hostname.
*
* @return integer The GeoIP country ID.
*/
public function countryIdByName($name)
{
if (!$this->_open()) {
return false;
}
$addr = gethostbyname($name);
if (!$addr || ($addr == $name)) {
return false;
}
return $this->countryIdByAddr($addr);
}
/**
* Returns the country abbreviation (2-letter) for a hostname.
*
* @param string $name The hostname.
*
* @return integer The country abbreviation.
*/
public function countryCodeByName($name)
{
if ($this->_open()) {
$country_id = $this->countryIdByName($name);
if ($country_id !== false) {
return $this->_countryCodes[$country_id];
}
}
return false;
}
/**
* Returns the country name for a hostname.
*
* @param string $name The hostname.
*
* @return integer The country name.
*/
public function countryNameByName($name)
{
if ($this->_open()) {
$country_id = $this->countryCodeByName($name);
if ($country_id !== false) {
return $this->_getName($country_id);
}
}
return false;
}
/**
* Returns the country ID for an IP Address.
*
* @param string $addr The IP Address.
*
* @return integer The GeoIP country ID.
*/
public function countryIdByAddr($addr)
{
if (!$this->_open()) {
return false;
}
$ipnum = ip2long($addr);
$country = $this->_seekCountry($ipnum);
return ($country === false)
? ''
: ($this->_seekCountry($ipnum) - self::GEOIP_COUNTRY_BEGIN);
}
/**
* Returns the country abbreviation (2-letter) for an IP Address.
*
* @param string $addr The IP Address.
*
* @return integer The country abbreviation.
*/
public function countryCodeByAddr($addr)
{
if ($this->_open()) {
$country_id = $this->countryIdByAddr($addr);
if ($country_id !== false) {
return $this->_countryCodes[$country_id];
}
}
return false;
}
/**
* Returns the country name for an IP address.
*
* @param string $addr The IP address.
*
* @return mixed The country name.
*/
public function countryNameByAddr($addr)
{
if ($this->_open()) {
$country_id = $this->countryCodeByAddr($addr);
if ($country_id !== false) {
return $this->_getName($country_id);
}
}
return false;
}
/**
* Finds a country by IP Address in the GeoIP database.
*
* @param string $ipnum The IP Address to search for.
*
* @return mixed The country ID or false if not found.
*/
protected function _seekCountry($ipnum)
{
$offset = 0;
for ($depth = 31; $depth >= 0; --$depth) {
if (fseek($this->_fh, 2 * self::STANDARD_RECORD_LENGTH * $offset, SEEK_SET) != 0) {
return false;
}
$buf = fread($this->_fh, 2 * self::STANDARD_RECORD_LENGTH);
$x = array(0, 0);
for ($i = 0; $i < 2; ++$i) {
for ($j = 0; $j < self::STANDARD_RECORD_LENGTH; ++$j) {
$x[$i] += ord($buf[self::STANDARD_RECORD_LENGTH * $i + $j]) << ($j * 8);
}
}
if ($ipnum & (1 << $depth)) {
if ($x[1] >= self::GEOIP_COUNTRY_BEGIN) {
return $x[1];
}
$offset = $x[1];
} else {
if ($x[0] >= self::GEOIP_COUNTRY_BEGIN) {
return $x[0];
}
$offset = $x[0];
}
}
return false;
}
/**
* Given a 2-letter country code, returns a country string.
*
* @param string $code The country code.
*
* @return string The country string.
*/
protected function _getName($code)
{
$code = Horde_String::upper($code);
$geoip_codes = array(
'AP' => Horde_Nls_Translation::t("Asia/Pacific Region"),
'EU' => Horde_Nls_Translation::t("Europe"),
'A1' => Horde_Nls_Translation::t("Anonymous Proxy"),
'A2' => Horde_Nls_Translation::t("Satellite Provider"),
'O1' => Horde_Nls_Translation::t("Other")
);
return isset($geoip_codes[$code])
? $geoip_codes[$code]
: strval(Horde_Nls::getCountryISO($code));
}
}
|