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')
{ {
@ -101,8 +104,16 @@ class DolGeoIP
} }
else else
{ {
if (! function_exists('geoip_country_code_by_addr')) return strtolower(geoip_country_code_by_name($this->gi, $ip)); if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip))
return strtolower(geoip_country_code_by_addr($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));
}
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
@ -11,198 +11,161 @@
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* 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; if ($seek_country == $gi->databaseSegments) {
var $region; return null;
var $regionname; }
var $city; return _common_get_record($gi, $seek_country);
var $postal_code;
var $latitude;
var $longitude;
var $areacode;
var $dmacode;
var $isp;
var $org;
var $metrocode;
} }
function getrecordwithdnsservice($str){ function _common_get_record($gi, $seek_country)
$record = new geoipdnsrecord; {
$keyvalue = explode(";",$str); // workaround php's broken substr, strpos, etc handling with
foreach ($keyvalue as $keyvalue2){ // mbstring.func_overload and mbstring.internal_encoding
list($key,$value) = explode("=",$keyvalue2); $mbExists = extension_loaded('mbstring');
if ($key == "co"){ if ($mbExists) {
$record->country_code = $value; $enc = mb_internal_encoding();
} mb_internal_encoding('ISO-8859-1');
if ($key == "ci"){ }
$record->city = $value;
} $record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments;
if ($key == "re"){
$record->region = $value; if ($gi->flags & GEOIP_MEMORY_CACHE) {
} $record_buf = substr($gi->memory_buffer, $record_pointer, FULL_RECORD_LENGTH);
if ($key == "ac"){ } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
$record->areacode = $value; $record_buf = _sharedMemRead($gi, $record_pointer, FULL_RECORD_LENGTH);
} } else {
if ($key == "dm" || $key == "me" ){ fseek($gi->filehandle, $record_pointer, SEEK_SET);
$record->dmacode = $value; $record_buf = fread($gi->filehandle, FULL_RECORD_LENGTH);
$record->metrocode = $value; }
} $record = new geoiprecord;
if ($key == "is"){ $record_buf_pos = 0;
$record->isp = $value; $char = ord(substr($record_buf, $record_buf_pos, 1));
} $record->country_code = $gi->GEOIP_COUNTRY_CODES[$char];
if ($key == "or"){ $record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char];
$record->org = $value; $record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char];
} $record->continent_code = $gi->GEOIP_CONTINENT_CODES[$char];
if ($key == "zi"){ $record_buf_pos++;
$record->postal_code = $value; $str_length = 0;
}
if ($key == "la"){ // Get region
$record->latitude = $value; $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
} while ($char != 0) {
if ($key == "lo"){ $str_length++;
$record->longitude = $value; $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
} }
} if ($str_length > 0) {
$number = $GLOBALS['GEOIP_COUNTRY_CODE_TO_NUMBER'][$record->country_code]; $record->region = substr($record_buf, $record_buf_pos, $str_length);
$record->country_code3 = $GLOBALS['GEOIP_COUNTRY_CODES3'][$number]; }
$record->country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$number]; $record_buf_pos += $str_length + 1;
if ($record->region != "") { $str_length = 0;
if (($record->country_code == "US") || ($record->country_code == "CA")){ // Get city
$record->regionname = $GLOBALS['ISO'][$record->country_code][$record->region]; $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
} else { while ($char != 0) {
$record->regionname = $GLOBALS['FIPS'][$record->country_code][$record->region]; $str_length++;
} $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
} }
return $record; if ($str_length > 0) {
$record->city = substr($record_buf, $record_buf_pos, $str_length);
}
$record_buf_pos += $str_length + 1;
$str_length = 0;
// Get postal code
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
while ($char != 0) {
$str_length++;
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
}
if ($str_length > 0) {
$record->postal_code = substr($record_buf, $record_buf_pos, $str_length);
}
$record_buf_pos += $str_length + 1;
// Get latitude and longitude
$latitude = 0;
$longitude = 0;
for ($j = 0; $j < 3; ++$j) {
$char = ord(substr($record_buf, $record_buf_pos++, 1));
$latitude += ($char << ($j * 8));
}
$record->latitude = ($latitude / 10000) - 180;
for ($j = 0; $j < 3; ++$j) {
$char = ord(substr($record_buf, $record_buf_pos++, 1));
$longitude += ($char << ($j * 8));
}
$record->longitude = ($longitude / 10000) - 180;
if (GEOIP_CITY_EDITION_REV1 == $gi->databaseType) {
$metroarea_combo = 0;
if ($record->country_code == "US") {
for ($j = 0; $j < 3; ++$j) {
$char = ord(substr($record_buf, $record_buf_pos++, 1));
$metroarea_combo += ($char << ($j * 8));
}
$record->metro_code = $record->dma_code = floor($metroarea_combo / 1000);
$record->area_code = $metroarea_combo % 1000;
}
}
if ($mbExists) {
mb_internal_encoding($enc);
}
return $record;
} }
function _get_record($gi,$ipnum){ function GeoIP_record_by_addr_v6($gi, $addr)
$seek_country = _geoip_seek_country($gi,$ipnum); {
if ($seek_country == $gi->databaseSegments) { if ($addr == null) {
return NULL; return 0;
} }
$record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments; $ipnum = inet_pton($addr);
return _get_record_v6($gi, $ipnum);
if ($gi->flags & GEOIP_MEMORY_CACHE) {
$record_buf = substr($gi->memory_buffer,$record_pointer,FULL_RECORD_LENGTH);
} elseif ($gi->flags & GEOIP_SHARED_MEMORY){
$record_buf = @shmop_read($gi->shmid,$record_pointer,FULL_RECORD_LENGTH);
} else {
fseek($gi->filehandle, $record_pointer, SEEK_SET);
$record_buf = fread($gi->filehandle,FULL_RECORD_LENGTH);
}
$record = new geoiprecord;
$record_buf_pos = 0;
$char = ord(substr($record_buf,$record_buf_pos,1));
$record->country_code = $gi->GEOIP_COUNTRY_CODES[$char];
$record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char];
$record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char];
$record_buf_pos++;
$str_length = 0;
// Get region
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
while ($char != 0){
$str_length++;
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
}
if ($str_length > 0){
$record->region = substr($record_buf,$record_buf_pos,$str_length);
}
$record_buf_pos += $str_length + 1;
$str_length = 0;
// Get city
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
while ($char != 0){
$str_length++;
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
}
if ($str_length > 0){
$record->city = substr($record_buf,$record_buf_pos,$str_length);
}
$record_buf_pos += $str_length + 1;
$str_length = 0;
// Get postal code
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
while ($char != 0){
$str_length++;
$char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
}
if ($str_length > 0){
$record->postal_code = substr($record_buf,$record_buf_pos,$str_length);
}
$record_buf_pos += $str_length + 1;
$str_length = 0;
// Get latitude and longitude
$latitude = 0;
$longitude = 0;
for ($j = 0;$j < 3; ++$j){
$char = ord(substr($record_buf,$record_buf_pos++,1));
$latitude += ($char << ($j * 8));
}
$record->latitude = ($latitude/10000) - 180;
for ($j = 0;$j < 3; ++$j){
$char = ord(substr($record_buf,$record_buf_pos++,1));
$longitude += ($char << ($j * 8));
}
$record->longitude = ($longitude/10000) - 180;
if (GEOIP_CITY_EDITION_REV1 == $gi->databaseType){
$metroarea_combo = 0;
if ($record->country_code == "US"){
for ($j = 0;$j < 3;++$j){
$char = ord(substr($record_buf,$record_buf_pos++,1));
$metroarea_combo += ($char << ($j * 8));
}
$record->metro_code = $record->dma_code = floor($metroarea_combo/1000);
$record->area_code = $metroarea_combo%1000;
}
}
return $record;
} }
function GeoIP_record_by_addr ($gi,$addr){ function _get_record($gi, $ipnum)
if ($addr == NULL){ {
return 0; $seek_country = _geoip_seek_country($gi, $ipnum);
} if ($seek_country == $gi->databaseSegments) {
$ipnum = ip2long($addr); return null;
return _get_record($gi, $ipnum); }
return _common_get_record($gi, $seek_country);
} }
?> function GeoIP_record_by_addr($gi, $addr)
{
if ($addr == null) {
return 0;
}
$ipnum = ip2long($addr);
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