diff --git a/htdocs/admin/geoipmaxmind.php b/htdocs/admin/geoipmaxmind.php index 725f43dfa0c..098d3b54199 100644 --- a/htdocs/admin/geoipmaxmind.php +++ b/htdocs/admin/geoipmaxmind.php @@ -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 ''.$langs->trans("Parameter").''.$langs->trans("Value").''; print "\n"; +// Lib version +print ''.$langs->trans("GeoIPLibVersion").''; +print ''; +$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 '
'.$langs->trans("Version").': '.$version; + } +} +print ''; + +// Path to database file print ''.$langs->trans("PathToGeoIPMaxmindCountryDataFile").''; print ''; -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).'
'; -print ''; -if ($geoip) $version=$geoip->getVersion(); -if ($version) +if ($conf->global->GEOIP_VERSION == 'php') { - print '
'.$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
'; } +print ''; print ''; print ''; diff --git a/htdocs/core/class/dolgeoip.class.php b/htdocs/core/class/dolgeoip.class.php index b25a5bc516e..a5200126737 100644 --- a/htdocs/core/class/dolgeoip.class.php +++ b/htdocs/core/class/dolgeoip.class.php @@ -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.')'; } /** diff --git a/htdocs/includes/geoip2/geoip2.phar b/htdocs/includes/geoip2/geoip2.phar new file mode 100644 index 00000000000..05104ff2737 Binary files /dev/null and b/htdocs/includes/geoip2/geoip2.phar differ diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 8bcbd1d889d..72721fcb364 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -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.
Examples:
/usr/local/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoIP.dat +PathToGeoIPMaxmindCountryDataFile=Path to file containing Maxmind ip to country translation.
Examples:
/usr/local/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoIP.dat
/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 free demo version of the Maxmind GeoIP country file at %s. YouCanDownloadAdvancedDatFileTo=You can also download a more complete version, with updates, of the Maxmind GeoIP country file at %s.