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 (empty($this->gi)) {
145 if ($this->gi ==
'NOGI') {
147 return strtolower(geoip_country_code_by_name($ip));
149 if (preg_match(
'/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
150 if ($geoipversion ==
'2') {
152 $record = $this->gi->country($ip);
153 return strtolower($record->country->isoCode);
159 if (!function_exists(
'geoip_country_code_by_addr')) {
160 return strtolower(geoip_country_code_by_name($ip));
162 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
165 if ($geoipversion ==
'2') {
167 $record = $this->gi->country($ip);
168 return strtolower($record->country->isoCode);
174 if (function_exists(
'geoip_country_code_by_addr_v6')) {
175 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
176 } elseif (function_exists(
'geoip_country_code_by_name_v6')) {
177 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));