dolibarr  19.0.0-dev
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 
38  public $error;
39  public $errorlabel;
40 
47  public function __construct($type, $datfile)
48  {
49  global $conf;
50 
51  $geoipversion = '2'; // 'php', or geoip version '2'
52  if (!empty($conf->global->GEOIP_VERSION)) {
53  $geoipversion = $conf->global->GEOIP_VERSION;
54  }
55 
56  if ($type == 'country') {
57  // geoip may have been already included with PEAR
58  if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
59  stream_wrapper_restore('phar');
60  require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
61  }
62  } elseif ($type == 'city') {
63  // geoip may have been already included with PEAR
64  if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
65  stream_wrapper_restore('phar');
66  require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
67  }
68  } else {
69  print 'ErrorBadParameterInConstructor';
70  return 0;
71  }
72 
73  // Here, function exists (embedded into PHP or exists because we made include)
74  if (empty($type) || empty($datfile)) {
75  $this->errorlabel = 'Constructor was called with no datafile parameter';
76  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
77  return 0;
78  }
79  if (!file_exists($datfile) || !is_readable($datfile)) {
80  $this->error = 'ErrorGeoIPClassNotInitialized';
81  $this->errorlabel = "Datafile ".$datfile." not found";
82  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
83  return 0;
84  }
85 
86  if ($geoipversion == '2') {
87  try {
88  $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
89  } catch (Exception $e) {
90  $this->error = $e->getMessage();
91  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
92  return 0;
93  }
94  } elseif (function_exists('geoip_open') && defined('GEOIP_STANDARD')) {
95  $this->gi = geoip_open($datfile, constant('GEOIP_STANDARD'));
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)) {
117  $geoipversion = $conf->global->GEOIP_VERSION;
118  }
119 
120  if (empty($this->gi)) {
121  return '';
122  }
123  if ($this->gi == 'NOGI') {
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  if ($geoipversion == '2') {
129  try {
130  $record = $this->gi->country($ip);
131  return strtolower($record->country->isoCode);
132  } catch (Exception $e) {
133  //return $e->getMessage();
134  return '';
135  }
136  } else {
137  if (!function_exists('geoip_country_code_by_addr')) {
138  return strtolower(geoip_country_code_by_name($ip));
139  }
140  return strtolower(geoip_country_code_by_addr($this->gi, $ip));
141  }
142  } else {
143  if ($geoipversion == '2') {
144  try {
145  $record = $this->gi->country($ip);
146  return strtolower($record->country->isoCode);
147  } catch (Exception $e) {
148  //return $e->getMessage();
149  return '';
150  }
151  } else {
152  if (function_exists('geoip_country_code_by_addr_v6')) {
153  return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
154  } elseif (function_exists('geoip_country_code_by_name_v6')) {
155  return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
156  }
157  return '';
158  }
159  }
160  }
161  }
162 
169  public function getCountryCodeFromName($name)
170  {
171  global $conf;
172 
173  $geoipversion = '2'; // 'php', or '2'
174  if (!empty($conf->global->GEOIP_VERSION)) {
175  $geoipversion = $conf->global->GEOIP_VERSION;
176  }
177 
178  if (empty($this->gi)) {
179  return '';
180  }
181 
182  if ($geoipversion == '2') {
183  try {
184  $record = $this->gi->country($name);
185  return $record->country->isoCode;
186  } catch (Exception $e) {
187  //return $e->getMessage();
188  return '';
189  }
190  } else {
191  return strtolower(geoip_country_code_by_name($name));
192  }
193  }
194 
200  public function getVersion()
201  {
202  global $conf;
203 
204  $geoipversion = '2'; // 'php', or '2'
205  if (!empty($conf->global->GEOIP_VERSION)) {
206  $geoipversion = $conf->global->GEOIP_VERSION;
207  }
208 
209  if ($geoipversion == 'php') {
210  if ($this->gi == 'NOGI') {
211  return geoip_database_info();
212  } else {
213  return 'geoip_database_info() function not available';
214  }
215  }
216 
217  return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
218  }
219 
225  public function close()
226  {
227  if (function_exists('geoip_close')) {
228  // With some geoip with PEAR, geoip_close function may not exists
229  geoip_close($this->gi);
230  }
231  }
232 }
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 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.