Update geoip v1

This commit is contained in:
Laurent Destailleur 2019-08-21 18:26:41 +02:00
parent 689aa4979e
commit bfbabbf626
7 changed files with 8840 additions and 5052 deletions

View File

@ -144,6 +144,13 @@ if ($geoip)
if ($result) print $result; if ($result) print $result;
else print $langs->trans("Error"); else print $langs->trans("Error");
$ip='2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
print '<br>'.$ip.' -> ';
$result=dol_print_ip($ip, 1);
if ($result) print $result;
else print $langs->trans("Error");
/* We disable this test because dol_print_ip need an ip as input /* We disable this test because dol_print_ip need an ip as input
$ip='www.google.com'; $ip='www.google.com';
print '<br>'.$ip.' -> '; print '<br>'.$ip.' -> ';

View File

@ -46,7 +46,10 @@ class DolGeoIP
if ($type == 'country') if ($type == 'country')
{ {
// geoip may have been already included with PEAR // geoip may have been already included with PEAR
if (! function_exists('geoip_country_code_by_name')) $res=include_once GEOIP_PATH.'geoip.inc'; if (! function_exists('geoip_country_code_by_name'))
{
$res=include_once GEOIP_PATH.'geoip.inc';
}
} }
elseif ($type == 'city') elseif ($type == 'city')
{ {
@ -100,10 +103,18 @@ class DolGeoIP
return strtolower(geoip_country_code_by_name($ip)); return strtolower(geoip_country_code_by_name($ip));
} }
else else
{
if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip))
{ {
if (! function_exists('geoip_country_code_by_addr')) return strtolower(geoip_country_code_by_name($this->gi, $ip)); if (! function_exists('geoip_country_code_by_addr')) return strtolower(geoip_country_code_by_name($this->gi, $ip));
return strtolower(geoip_country_code_by_addr($this->gi, $ip)); return strtolower(geoip_country_code_by_addr($this->gi, $ip));
} }
else
{
if (! function_exists('geoip_country_code_by_addr_v6')) return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
}
}
} }
/** /**

View File

@ -2692,12 +2692,10 @@ function dolGetCountryCodeFromIp($ip)
{ {
$datafile=$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE; $datafile=$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE;
//$ip='24.24.24.24'; //$ip='24.24.24.24';
//$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat'; Note that this must be downloaded datafile (not same than datafile provided with ubuntu packages) //$datafile='/usr/share/GeoIP/GeoIP.dat'; Note that this must be downloaded datafile (not same than datafile provided with ubuntu packages)
include_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php'; include_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
$geoip=new DolGeoIP('country', $datafile); $geoip=new DolGeoIP('country', $datafile);
//print 'ip='.$ip.' databaseType='.$geoip->gi->databaseType." GEOIP_CITY_EDITION_REV1=".GEOIP_CITY_EDITION_REV1."\n"; //print 'ip='.$ip.' databaseType='.$geoip->gi->databaseType." GEOIP_CITY_EDITION_REV1=".GEOIP_CITY_EDITION_REV1."\n";
//print "geoip_country_id_by_addr=".geoip_country_id_by_addr($geoip->gi,$ip)."\n";
$countrycode=$geoip->getCountryCodeFromIP($ip); $countrycode=$geoip->getCountryCodeFromIP($ip);
} }

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
/* geoipcity.inc /* geoipcity.inc
* *
* Copyright (C) 2004 Maxmind LLC * Copyright (C) 2013 MaxMind, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -19,112 +19,53 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
/*
* Changelog:
*
* 2005-01-13 Andrew Hill, Awarez Ltd. (http://www.awarez.net)
* Formatted file according to PEAR library standards.
* Changed inclusion of geoip.inc file to require_once, so that
* this library can be used in the same script as geoip.inc.
*/
define("FULL_RECORD_LENGTH", 50); define("FULL_RECORD_LENGTH", 50);
require_once 'geoip.inc'; require_once 'geoip.inc';
require_once 'geoipregionvars.php'; require_once 'geoipregionvars.php';
class geoiprecord { class geoiprecord
var $country_code; {
var $country_code3; public $country_code;
var $country_name; public $country_code3;
var $region; public $country_name;
var $city; public $region;
var $postal_code; public $city;
var $latitude; public $postal_code;
var $longitude; public $latitude;
var $area_code; public $longitude;
var $dma_code; # metro and dma code are the same. use metro_code public $area_code;
var $metro_code; public $dma_code; # metro and dma code are the same. use metro_code
public $metro_code;
public $continent_code;
} }
class geoipdnsrecord { function _get_record_v6($gi, $ipnum)
var $country_code; {
var $country_code3; $seek_country = _geoip_seek_country_v6($gi, $ipnum);
var $country_name;
var $region;
var $regionname;
var $city;
var $postal_code;
var $latitude;
var $longitude;
var $areacode;
var $dmacode;
var $isp;
var $org;
var $metrocode;
}
function getrecordwithdnsservice($str){
$record = new geoipdnsrecord;
$keyvalue = explode(";",$str);
foreach ($keyvalue as $keyvalue2){
list($key,$value) = explode("=",$keyvalue2);
if ($key == "co"){
$record->country_code = $value;
}
if ($key == "ci"){
$record->city = $value;
}
if ($key == "re"){
$record->region = $value;
}
if ($key == "ac"){
$record->areacode = $value;
}
if ($key == "dm" || $key == "me" ){
$record->dmacode = $value;
$record->metrocode = $value;
}
if ($key == "is"){
$record->isp = $value;
}
if ($key == "or"){
$record->org = $value;
}
if ($key == "zi"){
$record->postal_code = $value;
}
if ($key == "la"){
$record->latitude = $value;
}
if ($key == "lo"){
$record->longitude = $value;
}
}
$number = $GLOBALS['GEOIP_COUNTRY_CODE_TO_NUMBER'][$record->country_code];
$record->country_code3 = $GLOBALS['GEOIP_COUNTRY_CODES3'][$number];
$record->country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$number];
if ($record->region != "") {
if (($record->country_code == "US") || ($record->country_code == "CA")){
$record->regionname = $GLOBALS['ISO'][$record->country_code][$record->region];
} else {
$record->regionname = $GLOBALS['FIPS'][$record->country_code][$record->region];
}
}
return $record;
}
function _get_record($gi,$ipnum){
$seek_country = _geoip_seek_country($gi,$ipnum);
if ($seek_country == $gi->databaseSegments) { if ($seek_country == $gi->databaseSegments) {
return NULL; return null;
} }
return _common_get_record($gi, $seek_country);
}
function _common_get_record($gi, $seek_country)
{
// workaround php's broken substr, strpos, etc handling with
// mbstring.func_overload and mbstring.internal_encoding
$mbExists = extension_loaded('mbstring');
if ($mbExists) {
$enc = mb_internal_encoding();
mb_internal_encoding('ISO-8859-1');
}
$record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments; $record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments;
if ($gi->flags & GEOIP_MEMORY_CACHE) { if ($gi->flags & GEOIP_MEMORY_CACHE) {
$record_buf = substr($gi->memory_buffer, $record_pointer, FULL_RECORD_LENGTH); $record_buf = substr($gi->memory_buffer, $record_pointer, FULL_RECORD_LENGTH);
} elseif ($gi->flags & GEOIP_SHARED_MEMORY) { } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
$record_buf = @shmop_read($gi->shmid,$record_pointer,FULL_RECORD_LENGTH); $record_buf = _sharedMemRead($gi, $record_pointer, FULL_RECORD_LENGTH);
} else { } else {
fseek($gi->filehandle, $record_pointer, SEEK_SET); fseek($gi->filehandle, $record_pointer, SEEK_SET);
$record_buf = fread($gi->filehandle, FULL_RECORD_LENGTH); $record_buf = fread($gi->filehandle, FULL_RECORD_LENGTH);
@ -135,8 +76,10 @@ function _get_record($gi,$ipnum){
$record->country_code = $gi->GEOIP_COUNTRY_CODES[$char]; $record->country_code = $gi->GEOIP_COUNTRY_CODES[$char];
$record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char]; $record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char];
$record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char]; $record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char];
$record->continent_code = $gi->GEOIP_CONTINENT_CODES[$char];
$record_buf_pos++; $record_buf_pos++;
$str_length = 0; $str_length = 0;
// Get region // Get region
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1)); $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
while ($char != 0) { while ($char != 0) {
@ -169,7 +112,7 @@ function _get_record($gi,$ipnum){
$record->postal_code = substr($record_buf, $record_buf_pos, $str_length); $record->postal_code = substr($record_buf, $record_buf_pos, $str_length);
} }
$record_buf_pos += $str_length + 1; $record_buf_pos += $str_length + 1;
$str_length = 0;
// Get latitude and longitude // Get latitude and longitude
$latitude = 0; $latitude = 0;
$longitude = 0; $longitude = 0;
@ -194,15 +137,35 @@ function _get_record($gi,$ipnum){
$record->area_code = $metroarea_combo % 1000; $record->area_code = $metroarea_combo % 1000;
} }
} }
if ($mbExists) {
mb_internal_encoding($enc);
}
return $record; return $record;
} }
function GeoIP_record_by_addr ($gi,$addr){ function GeoIP_record_by_addr_v6($gi, $addr)
if ($addr == NULL){ {
if ($addr == null) {
return 0;
}
$ipnum = inet_pton($addr);
return _get_record_v6($gi, $ipnum);
}
function _get_record($gi, $ipnum)
{
$seek_country = _geoip_seek_country($gi, $ipnum);
if ($seek_country == $gi->databaseSegments) {
return null;
}
return _common_get_record($gi, $seek_country);
}
function GeoIP_record_by_addr($gi, $addr)
{
if ($addr == null) {
return 0; return 0;
} }
$ipnum = ip2long($addr); $ipnum = ip2long($addr);
return _get_record($gi, $ipnum); return _get_record($gi, $ipnum);
} }
?>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff