61 $datfile = str_replace(
'DOL_DATA_ROOT', DOL_DATA_ROOT, $datfile);
69 if ($type ==
'country') {
71 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name'))) {
72 if (function_exists(
'stream_wrapper_restore')) {
73 stream_wrapper_restore(
'phar');
75 include_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
77 } elseif ($type ==
'city') {
79 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name'))) {
80 if (function_exists(
'stream_wrapper_restore')) {
81 stream_wrapper_restore(
'phar');
83 include_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
86 print
'ErrorBadParameterInConstructor';
90 $this->error = $e->getMessage();
91 $this->errorlabel =
'Exception '.$this->error;
92 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
97 if (empty($type) || empty($datfile)) {
98 $this->errorlabel =
'Constructor was called with no datafile parameter';
99 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
102 if (!file_exists($datfile) || !is_readable($datfile)) {
103 $this->error =
'ErrorGeoIPClassNotInitialized';
104 $this->errorlabel =
"Datafile ".$datfile.
" not found";
105 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
109 if ($geoipversion ==
'2') {
112 $this->gi =
new GeoIp2\Database\Reader($datfile);
114 $this->error = $e->getMessage();
115 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
118 } elseif (function_exists(
'geoip_open') && defined(
'GEOIP_STANDARD')) {
119 $this->gi = geoip_open($datfile, constant(
'GEOIP_STANDARD'));
120 } elseif (function_exists(
'geoip_country_code_by_name')) {
142 if (is_object($this->gi) && method_exists($this->gi,
'metadata')) {
144 $databasetype = (string) $this->gi->metadata()->databaseType;
146 if (strpos($databasetype,
'Country') ===
false && strpos($databasetype,
'City') !==
false) {
147 $record = $this->gi->city($ipOrName);
149 $record = $this->gi->country($ipOrName);
151 return (
string) $record->country->isoCode;
170 if (is_object($this->gi) && method_exists($this->gi,
'metadata')) {
172 $databasetype = (string) $this->gi->metadata()->databaseType;
174 if (strpos($databasetype,
'City') ===
false) {
177 $record = $this->gi->city($ipOrName);
178 return (
string) $record->city->name;
197 if (empty($this->gi) || $this->gi ==
'NOGI') {
200 if ($geoipversion !=
'2') {
220 if (empty($this->gi)) {
223 if ($this->gi ==
'NOGI') {
225 return strtolower(geoip_country_code_by_name($ip));
227 if (preg_match(
'/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
228 if ($geoipversion ==
'2') {
231 if (!function_exists(
'geoip_country_code_by_addr')) {
232 return strtolower(geoip_country_code_by_name($ip));
234 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
237 if ($geoipversion ==
'2') {
240 if (function_exists(
'geoip_country_code_by_addr_v6')) {
241 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
242 } elseif (function_exists(
'geoip_country_code_by_name_v6')) {
243 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
264 if (empty($this->gi)) {
268 if ($geoipversion ==
'2') {
271 return strtolower(geoip_country_code_by_name($name));
287 if ($geoipversion ==
'php') {
288 if ($this->gi ==
'NOGI') {
289 return geoip_database_info();
291 return 'geoip_database_info() function not available';
295 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.
')';
305 if (function_exists(
'geoip_close')) {
307 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.
getCityNameFromIP($ip)
Return the city name from an ip.
getGeoIp2CityName($ipOrName)
Return the city name for an IP or a host name using the embedded GeoIP2 reader.
__construct($type, $datfile)
Constructor.
getVersion()
Return version of data file.
getGeoIp2IsoCode($ipOrName)
Return the ISO country code for an IP or a host name using the embedded GeoIP2 reader.
close()
Close geoip object.
getCountryCodeFromName($name)
Return in lower case the country code from a host name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.