NEW GeoIP v2 support is natively provided.

This commit is contained in:
Laurent Destailleur 2019-08-21 19:38:45 +02:00
parent bfbabbf626
commit f5aede8256
4 changed files with 125 additions and 26 deletions

View File

@ -53,8 +53,11 @@ if ($action == 'set')
if (! $error)
{
$res = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
if (! $res > 0) $error++;
$res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity);
if (! $res1 > 0) $error++;
$res2 = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
if (! $res2 > 0) $error++;
if (! $error)
{
@ -67,6 +70,8 @@ if ($action == 'set')
}
}
if (! isset($conf->global->GEOIP_VERSION)) $conf->global->GEOIP_VERSION = '2';
/*
* View
@ -85,13 +90,6 @@ $geoip='';
if (! empty($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE))
{
$geoip=new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
//if ($geoip->error) print dol_htmloutput_errors($geoip->errorlabel,'',1);
if ($geoip->gi == 'NOGI') $geointernal=true;
else $geointernal=false;
}
else
{
if (function_exists('geoip_country_code_by_name')) $geointernal=true;
}
// Mode
@ -105,16 +103,30 @@ print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td
print '<td class="right"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
print "</tr>\n";
// Lib version
print '<tr class="oddeven"><td width=\"50%\">'.$langs->trans("GeoIPLibVersion").'</td>';
print '<td colspan="2">';
$arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
if ($conf->global->GEOIP_VERSION == 'php')
{
if ($geoip) $version=$geoip->getVersion();
if ($version)
{
print '<br>'.$langs->trans("Version").': '.$version;
}
}
print '</td></tr>';
// Path to database file
print '<tr class="oddeven"><td width=\"50%\">'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
print '<td colspan="2">';
if ($geointernal) print 'Using geoip PHP internal functions. Value must be '.geoip_db_filename(GEOIP_COUNTRY_EDITION).' or '.geoip_db_filename(GEOIP_CITY_EDITION_REV1).'<br>';
print '<input size="50" type="text" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE.'">';
if ($geoip) $version=$geoip->getVersion();
if ($version)
if ($conf->global->GEOIP_VERSION == 'php')
{
print '<br>'.$langs->trans("Version").': '.$version;
print 'Using geoip PHP internal functions. Value must be '.geoip_db_filename(GEOIP_COUNTRY_EDITION).' or '.geoip_db_filename(GEOIP_CITY_EDITION_REV1).' or /usr/share/GeoIP/GeoLite2-Country.mmdb<br>';
}
print '<input size="50" type="text" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE.'">';
print '</td></tr>';
print '</table>';

View File

@ -43,18 +43,30 @@ class DolGeoIP
*/
public function __construct($type, $datfile)
{
global $conf;
$geoipversion = '2'; // 'php', '1' or '2'
if (! empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
if ($type == 'country')
{
// geoip may have been already included with PEAR
if (! function_exists('geoip_country_code_by_name'))
if ($geoipversion == '2' || ($geoipversion != 'php' && ! function_exists('geoip_country_code_by_name')))
{
$res=include_once GEOIP_PATH.'geoip.inc';
if ($geoipversion == '1') $res=include_once GEOIP_PATH.'geoip.inc';
//else $res=include_once DOL_DOCUMENT_ROOT.'/includes/geoip2/vendor/autoload.php';
else require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
}
}
elseif ($type == 'city')
{
// geoip may have been already included with PEAR
if (! function_exists('geoip_country_code_by_name')) $res=include_once GEOIP_PATH.'geoipcity.inc';
if ($geoipversion == '2' || ($geoipversion != 'php' && ! function_exists('geoip_country_code_by_name')))
{
if ($geoipversion == '1') $res=include_once GEOIP_PATH.'geoipcity.inc';
//else $res=include_once DOL_DOCUMENT_ROOT.'/includes/geoip2/vendor/autoload.php';
else require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
}
}
else { print 'ErrorBadParameterInConstructor'; return 0; }
@ -73,7 +85,19 @@ class DolGeoIP
return 0;
}
if (function_exists('geoip_open'))
if ($geoipversion == '2')
{
try {
$this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
}
catch(Exception $e)
{
$this->error = $e->getMessage();
dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
return 0;
}
}
elseif (function_exists('geoip_open'))
{
$this->gi = geoip_open($datfile, GEOIP_STANDARD);
}
@ -93,6 +117,11 @@ class DolGeoIP
*/
public function getCountryCodeFromIP($ip)
{
global $conf;
$geoipversion = '2'; // 'php', '1' or '2'
if (! empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
if (empty($this->gi))
{
return '';
@ -106,13 +135,41 @@ class DolGeoIP
{
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));
if ($geoipversion == '2')
{
try {
$record = $this->gi->country($ip);
return strtolower($record->country->isoCode);
}
catch(Exception $e) {
//return $e->getMessage();
return '';
}
}
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));
}
}
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));
if ($geoipversion == '2')
{
try {
$record = $this->gi->country($ip);
return strtolower($record->country->isoCode);
}
catch(Exception $e) {
//return $e->getMessage();
return '';
}
}
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));
}
}
}
}
@ -125,11 +182,31 @@ class DolGeoIP
*/
public function getCountryCodeFromName($name)
{
global $conf;
$geoipversion = '2'; // 'php', '1' or '2'
if (! empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
if (empty($this->gi))
{
return '';
}
return geoip_country_code_by_name($this->gi, $name);
if ($geoipversion == '2')
{
try {
$record = $this->gi->country($name);
return $record->country->isoCode;
}
catch(Exception $e) {
//return $e->getMessage();
return '';
}
}
else
{
return geoip_country_code_by_name($this->gi, $name);
}
}
/**
@ -139,8 +216,18 @@ class DolGeoIP
*/
public function getVersion()
{
if ($this->gi == 'NOGI') return geoip_database_info();
return 'Not available (not using PHP internal geo functions)';
global $conf;
$geoipversion = '2'; // 'php', '1' or '2'
if (! empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
if ($geoipversion == 'php')
{
if ($this->gi == 'NOGI') return geoip_database_info();
else return 'geoip_database_info() function not available';
}
return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
}
/**

Binary file not shown.

View File

@ -1696,7 +1696,7 @@ SuppliersInvoiceNumberingModel=Vendor invoices numbering models
IfSetToYesDontForgetPermission=If set to yes, don't forget to provide permissions to groups or users allowed for the second approval
##### GeoIPMaxmind #####
GeoIPMaxmindSetup=GeoIP Maxmind module setup
PathToGeoIPMaxmindCountryDataFile=Path to file containing Maxmind ip to country translation.<br>Examples:<br>/usr/local/share/GeoIP/GeoIP.dat<br>/usr/share/GeoIP/GeoIP.dat
PathToGeoIPMaxmindCountryDataFile=Path to file containing Maxmind ip to country translation.<br>Examples:<br>/usr/local/share/GeoIP/GeoIP.dat<br>/usr/share/GeoIP/GeoIP.dat<br>/usr/share/GeoIP/GeoLite2-Country.mmdb
NoteOnPathLocation=Note that your ip to country data file must be inside a directory your PHP can read (Check your PHP open_basedir setup and filesystem permissions).
YouCanDownloadFreeDatFileTo=You can download a <b>free demo version</b> of the Maxmind GeoIP country file at %s.
YouCanDownloadAdvancedDatFileTo=You can also download a more <b>complete version, with updates,</b> of the Maxmind GeoIP country file at %s.