dolibarr  13.0.2
dolgeoip.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  * or see https://www.gnu.org/
17  */
18 
34 class DolGeoIP
35 {
36  public $gi;
37 
44  public function __construct($type, $datfile)
45  {
46  global $conf;
47 
48  $geoipversion = '2'; // 'php', or '2'
49  if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
50 
51  if ($type == 'country')
52  {
53  // geoip may have been already included with PEAR
54  if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name')))
55  {
56  require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
57  }
58  } elseif ($type == 'city')
59  {
60  // geoip may have been already included with PEAR
61  if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name')))
62  {
63  require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
64  }
65  } else { print 'ErrorBadParameterInConstructor'; return 0; }
66 
67  // Here, function exists (embedded into PHP or exists because we made include)
68  if (empty($type) || empty($datfile))
69  {
70  $this->errorlabel = 'Constructor was called with no datafile parameter';
71  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
72  return 0;
73  }
74  if (!file_exists($datfile) || !is_readable($datfile))
75  {
76  $this->error = 'ErrorGeoIPClassNotInitialized';
77  $this->errorlabel = "Datafile ".$datfile." not found";
78  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
79  return 0;
80  }
81 
82  if ($geoipversion == '2')
83  {
84  try {
85  $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
86  } catch (Exception $e)
87  {
88  $this->error = $e->getMessage();
89  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
90  return 0;
91  }
92  } elseif (function_exists('geoip_open'))
93  {
94  $this->gi = geoip_open($datfile, GEOIP_STANDARD);
95  }
96  elseif (function_exists('geoip_country_code_by_name')) {
97  $this->gi = 'NOGI'; // We are using embedded php geoip functions
98  //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
99  //print geoip_database_info();
100  } else {
101  $this->gi = ''; // For avoid error
102  }
103  }
104 
111  public function getCountryCodeFromIP($ip)
112  {
113  global $conf;
114 
115  $geoipversion = '2'; // 'php', or '2'
116  if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
117 
118  if (empty($this->gi))
119  {
120  return '';
121  }
122  if ($this->gi == 'NOGI')
123  {
124  // geoip_country_code_by_addr does not exists
125  return strtolower(geoip_country_code_by_name($ip));
126  } else {
127  if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip))
128  {
129  if ($geoipversion == '2')
130  {
131  try {
132  $record = $this->gi->country($ip);
133  return strtolower($record->country->isoCode);
134  } catch (Exception $e) {
135  //return $e->getMessage();
136  return '';
137  }
138  } else {
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));
141  }
142  } else {
143  if ($geoipversion == '2')
144  {
145  try {
146  $record = $this->gi->country($ip);
147  return strtolower($record->country->isoCode);
148  } catch (Exception $e) {
149  //return $e->getMessage();
150  return '';
151  }
152  } else {
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));
155  }
156  }
157  }
158  }
159 
166  public function getCountryCodeFromName($name)
167  {
168  global $conf;
169 
170  $geoipversion = '2'; // 'php', or '2'
171  if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
172 
173  if (empty($this->gi))
174  {
175  return '';
176  }
177 
178  if ($geoipversion == '2')
179  {
180  try {
181  $record = $this->gi->country($name);
182  return $record->country->isoCode;
183  } catch (Exception $e) {
184  //return $e->getMessage();
185  return '';
186  }
187  } else {
188  return geoip_country_code_by_name($this->gi, $name);
189  }
190  }
191 
197  public function getVersion()
198  {
199  global $conf;
200 
201  $geoipversion = '2'; // 'php', or '2'
202  if (!empty($conf->global->GEOIP_VERSION)) $geoipversion = $conf->global->GEOIP_VERSION;
203 
204  if ($geoipversion == 'php')
205  {
206  if ($this->gi == 'NOGI') return geoip_database_info();
207  else return 'geoip_database_info() function not available';
208  }
209 
210  return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
211  }
212 
218  public function close()
219  {
220  if (function_exists('geoip_close')) {
221  // With some geoip with PEAR, geoip_close function may not exists
222  geoip_close($this->gi);
223  }
224  }
225 }
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.
Definition: index.php:89
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(&#39;country&#39;,$datfile); $geoip-&gt;getCountryCodeFromIP($ip)...