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;
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
$ip='www.google.com';
print '<br>'.$ip.' -> ';

View File

@ -46,7 +46,10 @@ class DolGeoIP
if ($type == 'country')
{
// 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')
{
@ -101,8 +104,16 @@ class DolGeoIP
}
else
{
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));
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));
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;
//$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';
$geoip=new DolGeoIP('country', $datafile);
//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);
}

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
/* geoipcity.inc
*
* Copyright (C) 2004 Maxmind LLC
* Copyright (C) 2013 MaxMind, Inc.
*
* This library is free software; you can redistribute it and/or
* 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,
* 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.
*
* You should have received a copy of the GNU Lesser General Public
* 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 'geoipregionvars.php';
class geoiprecord {
var $country_code;
var $country_code3;
var $country_name;
var $region;
var $city;
var $postal_code;
var $latitude;
var $longitude;
var $area_code;
var $dma_code; # metro and dma code are the same. use metro_code
var $metro_code;
class geoiprecord
{
public $country_code;
public $country_code3;
public $country_name;
public $region;
public $city;
public $postal_code;
public $latitude;
public $longitude;
public $area_code;
public $dma_code; # metro and dma code are the same. use metro_code
public $metro_code;
public $continent_code;
}
class geoipdnsrecord {
var $country_code;
var $country_code3;
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 _get_record_v6($gi, $ipnum)
{
$seek_country = _geoip_seek_country_v6($gi, $ipnum);
if ($seek_country == $gi->databaseSegments) {
return null;
}
return _common_get_record($gi, $seek_country);
}
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 _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;
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 = _sharedMemRead($gi, $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->continent_code = $gi->GEOIP_CONTINENT_CODES[$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;
// 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){
$seek_country = _geoip_seek_country($gi,$ipnum);
if ($seek_country == $gi->databaseSegments) {
return NULL;
}
$record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments;
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_v6($gi, $addr)
{
if ($addr == null) {
return 0;
}
$ipnum = inet_pton($addr);
return _get_record_v6($gi, $ipnum);
}
function GeoIP_record_by_addr ($gi,$addr){
if ($addr == NULL){
return 0;
}
$ipnum = ip2long($addr);
return _get_record($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;
}
$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