49 if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
51 if ($type ==
'country')
54 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name')))
56 require_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
58 } elseif ($type ==
'city')
61 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name')))
63 require_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
65 }
else {
print 'ErrorBadParameterInConstructor';
return 0; }
68 if (empty($type) || empty($datfile))
70 $this->errorlabel =
'Constructor was called with no datafile parameter';
71 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
74 if (!file_exists($datfile) || !is_readable($datfile))
76 $this->error =
'ErrorGeoIPClassNotInitialized';
77 $this->errorlabel =
"Datafile ".$datfile.
" not found";
78 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
82 if ($geoipversion ==
'2')
85 $this->gi =
new GeoIp2\Database\Reader($datfile);
88 $this->error = $e->getMessage();
89 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
92 } elseif (function_exists(
'geoip_open'))
94 $this->gi = geoip_open($datfile, GEOIP_STANDARD);
96 elseif (function_exists(
'geoip_country_code_by_name')) {
116 if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
118 if (empty($this->gi))
122 if ($this->gi ==
'NOGI')
125 return strtolower(geoip_country_code_by_name($ip));
127 if (preg_match(
'/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip))
129 if ($geoipversion ==
'2')
132 $record = $this->gi->country($ip);
133 return strtolower($record->country->isoCode);
139 if (!function_exists(
'geoip_country_code_by_addr'))
return strtolower(geoip_country_code_by_name($this->gi, $ip));
140 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
143 if ($geoipversion ==
'2')
146 $record = $this->gi->country($ip);
147 return strtolower($record->country->isoCode);
153 if (!function_exists(
'geoip_country_code_by_addr_v6'))
return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
154 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
171 if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
173 if (empty($this->gi))
178 if ($geoipversion ==
'2')
181 $record = $this->gi->country($name);
182 return $record->country->isoCode;
188 return geoip_country_code_by_name($this->gi, $name);
202 if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
204 if ($geoipversion ==
'php')
206 if ($this->gi ==
'NOGI')
return geoip_database_info();
207 else return 'geoip_database_info() function not available';
210 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.
')';
220 if (function_exists(
'geoip_close')) {
222 geoip_close($this->gi);
getVersion()
Return verion of data file.
getCountryCodeFromIP($ip)
Return in lower case the country code from an ip.
__construct($type, $datfile)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print
Draft customers invoices.
getCountryCodeFromName($name)
Return in lower case the country code from a host name.
close()
Close geoip object.
Classe to manage GeoIP Usage: $geoip=new GeoIP('country',$datfile); $geoip->getCountryCodeFromIP($ip)...