68 if ($type ==
'country') {
70 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name'))) {
71 if (function_exists(
'stream_wrapper_restore')) {
72 stream_wrapper_restore(
'phar');
74 require_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
76 } elseif ($type ==
'city') {
78 if ($geoipversion ==
'2' || ($geoipversion !=
'php' && !function_exists(
'geoip_country_code_by_name'))) {
79 if (function_exists(
'stream_wrapper_restore')) {
80 stream_wrapper_restore(
'phar');
82 require_once DOL_DOCUMENT_ROOT.
'/includes/geoip2/geoip2.phar';
85 print
'ErrorBadParameterInConstructor';
90 if (empty($type) || empty($datfile)) {
91 $this->errorlabel =
'Constructor was called with no datafile parameter';
92 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
95 if (!file_exists($datfile) || !is_readable($datfile)) {
96 $this->error =
'ErrorGeoIPClassNotInitialized';
97 $this->errorlabel =
"Datafile ".$datfile.
" not found";
98 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
102 if ($geoipversion ==
'2') {
105 $this->gi =
new GeoIp2\Database\Reader($datfile);
107 $this->error = $e->getMessage();
108 dol_syslog(
'DolGeoIP '.$this->errorlabel, LOG_ERR);
111 } elseif (function_exists(
'geoip_open') && defined(
'GEOIP_STANDARD')) {
112 $this->gi = geoip_open($datfile, constant(
'GEOIP_STANDARD'));
113 } elseif (function_exists(
'geoip_country_code_by_name')) {
137 if (empty($this->gi)) {
140 if ($this->gi ==
'NOGI') {
142 return strtolower(geoip_country_code_by_name($ip));
144 if (preg_match(
'/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
145 if ($geoipversion ==
'2') {
147 $record = $this->gi->country($ip);
148 return strtolower($record->country->isoCode);
154 if (!function_exists(
'geoip_country_code_by_addr')) {
155 return strtolower(geoip_country_code_by_name($ip));
157 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
160 if ($geoipversion ==
'2') {
162 $record = $this->gi->country($ip);
163 return strtolower($record->country->isoCode);
169 if (function_exists(
'geoip_country_code_by_addr_v6')) {
170 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
171 } elseif (function_exists(
'geoip_country_code_by_name_v6')) {
172 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
195 if (empty($this->gi)) {
199 if ($geoipversion ==
'2') {
201 $record = $this->gi->country($name);
202 return $record->country->isoCode;
208 return strtolower(geoip_country_code_by_name($name));
226 if ($geoipversion ==
'php') {
227 if ($this->gi ==
'NOGI') {
228 return geoip_database_info();
230 return 'geoip_database_info() function not available';
234 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.
')';
244 if (function_exists(
'geoip_close')) {
246 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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...