dolibarr  18.0.0-alpha
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_addr_v6($this->gi, $ip));
149  } elseif (function_exists('geoip_country_code_by_name_v6')) {
150  return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
151  }
152  return '';
153  }
154  }
155  }
156  }
157 
164  public function getCountryCodeFromName($name)
165  {
166  global $conf;
167 
168  $geoipversion = '2'; // 'php', or '2'
169  if (!empty($conf->global->GEOIP_VERSION)) {
170  $geoipversion = $conf->global->GEOIP_VERSION;
171  }
172 
173  if (empty($this->gi)) {
174  return '';
175  }
176 
177  if ($geoipversion == '2') {
178  try {
179  $record = $this->gi->country($name);
180  return $record->country->isoCode;
181  } catch (Exception $e) {
182  //return $e->getMessage();
183  return '';
184  }
185  } else {
186  return strtolower(geoip_country_code_by_name($name));
187  }
188  }
189 
195  public function getVersion()
196  {
197  global $conf;
198 
199  $geoipversion = '2'; // 'php', or '2'
200  if (!empty($conf->global->GEOIP_VERSION)) {
201  $geoipversion = $conf->global->GEOIP_VERSION;
202  }
203 
204  if ($geoipversion == 'php') {
205  if ($this->gi == 'NOGI') {
206  return geoip_database_info();
207  } else {
208  return 'geoip_database_info() function not available';
209  }
210  }
211 
212  return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
213  }
214 
220  public function close()
221  {
222  if (function_exists('geoip_close')) {
223  // With some geoip with PEAR, geoip_close function may not exists
224  geoip_close($this->gi);
225  }
226  }
227 }
DolGeoIP
Classe to manage GeoIP Usage: $geoip=new GeoIP('country',$datfile); $geoip->getCountryCodeFromIP($ip)...
Definition: dolgeoip.class.php:34
DolGeoIP\close
close()
Close geoip object.
Definition: dolgeoip.class.php:220
Exception
DolGeoIP\__construct
__construct($type, $datfile)
Constructor.
Definition: dolgeoip.class.php:44
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1639
DolGeoIP\getVersion
getVersion()
Return verion of data file.
Definition: dolgeoip.class.php:195
DolGeoIP\getCountryCodeFromName
getCountryCodeFromName($name)
Return in lower case the country code from a host name.
Definition: dolgeoip.class.php:164
DolGeoIP\getCountryCodeFromIP
getCountryCodeFromIP($ip)
Return in lower case the country code from an ip.
Definition: dolgeoip.class.php:106