67 if ($type ==
'country') {
69 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name'))) {
70 if (function_exists(
'stream_wrapper_restore')) {
71 stream_wrapper_restore(
'phar');
73 require_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
75 } elseif ($type ==
'city') {
77 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name'))) {
78 if (function_exists(
'stream_wrapper_restore')) {
79 stream_wrapper_restore(
'phar');
81 require_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
84 print
'ErrorBadParameterInConstructor';
89 if (empty($type) || empty($datfile)) {
90 $this->errorlabel =
'Constructor was called with no datafile parameter';
91 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
94 if (!file_exists($datfile) || !is_readable($datfile)) {
95 $this->error =
'ErrorGeoIPClassNotInitialized';
96 $this->errorlabel =
"Datafile ".$datfile.
" not found";
97 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
101 if ($geoipversion ==
'2') {
104 $this->gi =
new GeoIp2\Database\Reader($datfile);
106 $this->error = $e->getMessage();
107 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
110 } elseif (function_exists(
'geoip_open') && defined(
'GEOIP_STANDARD')) {
111 $this->gi = geoip_open($datfile, constant(
'GEOIP_STANDARD'));
112 } elseif (function_exists(
'geoip_country_code_by_name')) {
136 if (empty($this->gi)) {
139 if ($this->gi ==
'NOGI') {
141 return strtolower(geoip_country_code_by_name($ip));
143 if (preg_match(
'/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
144 if ($geoipversion ==
'2') {
146 $record = $this->gi->country($ip);
147 return strtolower($record->country->isoCode);
153 if (!function_exists(
'geoip_country_code_by_addr')) {
154 return strtolower(geoip_country_code_by_name($ip));
156 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
159 if ($geoipversion ==
'2') {
161 $record = $this->gi->country($ip);
162 return strtolower($record->country->isoCode);
168 if (function_exists(
'geoip_country_code_by_addr_v6')) {
169 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
170 } elseif (function_exists(
'geoip_country_code_by_name_v6')) {
171 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
194 if (empty($this->gi)) {
198 if ($geoipversion ==
'2') {
200 $record = $this->gi->country($name);
201 return $record->country->isoCode;
207 return strtolower(geoip_country_code_by_name($name));
225 if ($geoipversion ==
'php') {
226 if ($this->gi ==
'NOGI') {
227 return geoip_database_info();
229 return 'geoip_database_info() function not available';
233 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.
')';
243 if (function_exists(
'geoip_close')) {
245 geoip_close($this->gi);
Class to manage GeoIP conversion Usage: $geoip=new GeoIP('country',$datfile); $geoip->getCountryCodeF...
getCountryCodeFromIP($ip)
Return in lower case the country code from an ip.
__construct($type, $datfile)
Constructor.
getVersion()
Return version of data file.
close()
Close geoip object.
getCountryCodeFromName($name)
Return in lower case the country code from a host name.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.