dolibarr  17.0.4
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 geoip version '2'
49  if (!empty($conf->global->GEOIP_VERSION)) {
50  $geoipversion = $conf->global->GEOIP_VERSION;
51  }
52 
53  if ($type == 'country') {
54  // geoip may have been already included with PEAR
55  if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
56  require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
57  }
58  } elseif ($type == 'city') {
59  // geoip may have been already included with PEAR
60  if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
61  require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
62  }
63  } else {
64  print 'ErrorBadParameterInConstructor';
65  return 0;
66  }
67 
68  // Here, function exists (embedded into PHP or exists because we made include)
69  if (empty($type) || empty($datfile)) {
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  $this->error = 'ErrorGeoIPClassNotInitialized';
76  $this->errorlabel = "Datafile ".$datfile." not found";
77  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
78  return 0;
79  }
80 
81  if ($geoipversion == '2') {
82  try {
83  $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
84  } catch (Exception $e) {
85  $this->error = $e->getMessage();
86  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
87  return 0;
88  }
89  } elseif (function_exists('geoip_open') && defined('GEOIP_STANDARD')) {
90  $this->gi = geoip_open($datfile, constant('GEOIP_STANDARD'));
91  } elseif (function_exists('geoip_country_code_by_name')) {
92  $this->gi = 'NOGI'; // We are using embedded php geoip functions
93  //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
94  //print geoip_database_info();
95  } else {
96  $this->gi = ''; // For avoid error
97  }
98  }
99 
106  public function getCountryCodeFromIP($ip)
107  {
108  global $conf;
109 
110  $geoipversion = '2'; // 'php', or '2'
111  if (!empty($conf->global->GEOIP_VERSION)) {
112  $geoipversion = $conf->global->GEOIP_VERSION;
113  }
114 
115  if (empty($this->gi)) {
116  return '';
117  }
118  if ($this->gi == 'NOGI') {
119  // geoip_country_code_by_addr does not exists
120  return strtolower(geoip_country_code_by_name($ip));
121  } else {
122  if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
123  if ($geoipversion == '2') {
124  try {
125  $record = $this->gi->country($ip);
126  return strtolower($record->country->isoCode);
127  } catch (Exception $e) {
128  //return $e->getMessage();
129  return '';
130  }
131  } else {
132  if (!function_exists('geoip_country_code_by_addr')) {
133  return strtolower(geoip_country_code_by_name($ip));
134  }
135  return strtolower(geoip_country_code_by_addr($this->gi, $ip));
136  }
137  } else {
138  if ($geoipversion == '2') {
139  try {
140  $record = $this->gi->country($ip);
141  return strtolower($record->country->isoCode);
142  } catch (Exception $e) {
143  //return $e->getMessage();
144  return '';
145  }
146  } else {
147  if (!function_exists('geoip_country_code_by_addr_v6')) {
148  return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
149  }
150  return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
151  }
152  }
153  }
154  }
155 
162  public function getCountryCodeFromName($name)
163  {
164  global $conf;
165 
166  $geoipversion = '2'; // 'php', or '2'
167  if (!empty($conf->global->GEOIP_VERSION)) {
168  $geoipversion = $conf->global->GEOIP_VERSION;
169  }
170 
171  if (empty($this->gi)) {
172  return '';
173  }
174 
175  if ($geoipversion == '2') {
176  try {
177  $record = $this->gi->country($name);
178  return $record->country->isoCode;
179  } catch (Exception $e) {
180  //return $e->getMessage();
181  return '';
182  }
183  } else {
184  return strtolower(geoip_country_code_by_name($name));
185  }
186  }
187 
193  public function getVersion()
194  {
195  global $conf;
196 
197  $geoipversion = '2'; // 'php', or '2'
198  if (!empty($conf->global->GEOIP_VERSION)) {
199  $geoipversion = $conf->global->GEOIP_VERSION;
200  }
201 
202  if ($geoipversion == 'php') {
203  if ($this->gi == 'NOGI') {
204  return geoip_database_info();
205  } else {
206  return 'geoip_database_info() function not available';
207  }
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 }
Classe to manage GeoIP Usage: $geoip=new GeoIP('country',$datfile); $geoip->getCountryCodeFromIP($ip)...
getCountryCodeFromIP($ip)
Return in lower case the country code from an ip.
__construct($type, $datfile)
Constructor.
getVersion()
Return verion of data file.
close()
Close geoip object.
getCountryCodeFromName($name)
Return in lower case the country code from a host name.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.