dolibarr  7.0.0-beta
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 <http://www.gnu.org/licenses/>.
16  * or see http://www.gnu.org/
17  */
18 
34 class DolGeoIP
35 {
36  var $gi;
37 
44  function __construct($type,$datfile)
45  {
46  if ($type == 'country')
47  {
48  // geoip may have been already included with PEAR
49  if (! function_exists('geoip_country_code_by_name')) $res=include_once GEOIP_PATH.'geoip.inc';
50  }
51  else if ($type == 'city')
52  {
53  // geoip may have been already included with PEAR
54  if (! function_exists('geoip_country_code_by_name')) $res=include_once GEOIP_PATH.'geoipcity.inc';
55  }
56  else { print 'ErrorBadParameterInConstructor'; return 0; }
57 
58  // Here, function exists (embedded into PHP or exists because we made include)
59  if (empty($type) || empty($datfile))
60  {
61  $this->errorlabel='Constructor was called with no datafile parameter';
62  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
63  return 0;
64  }
65  if (! file_exists($datfile) || ! is_readable($datfile))
66  {
67  $this->error='ErrorGeoIPClassNotInitialized';
68  $this->errorlabel="Datafile ".$datfile." not found";
69  dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
70  return 0;
71  }
72 
73  if (function_exists('geoip_open'))
74  {
75  $this->gi = geoip_open($datfile,GEOIP_STANDARD);
76  }
77  else
78  {
79  $this->gi = 'NOGI'; // We are using embedded php geoip functions
80  //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
81  //print geoip_database_info();
82  }
83  }
84 
91  function getCountryCodeFromIP($ip)
92  {
93  if (empty($this->gi))
94  {
95  return '';
96  }
97  if ($this->gi == 'NOGI')
98  {
99  // geoip_country_code_by_addr does not exists
100  return strtolower(geoip_country_code_by_name($ip));
101  }
102  else
103  {
104  if (! function_exists('geoip_country_code_by_addr')) return strtolower(geoip_country_code_by_name($this->gi, $ip));
105  return strtolower(geoip_country_code_by_addr($this->gi, $ip));
106  }
107  }
108 
115  function getCountryCodeFromName($name)
116  {
117  if (empty($this->gi))
118  {
119  return '';
120  }
121  return geoip_country_code_by_name($this->gi, $name);
122  }
123 
129  function getVersion()
130  {
131  if ($this->gi == 'NOGI') return geoip_database_info();
132  return 'Not available (not using PHP internal geo functions)';
133  }
134 
140  function close()
141  {
142  if (function_exists('geoip_close')) // With some geoip with PEAR, geoip_close function may not exists
143  {
144  geoip_close($this->gi);
145  }
146  }
147 }
getVersion()
Return verion of data file.
getCountryCodeFromIP($ip)
Return in lower case the country code from an ip.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
__construct($type, $datfile)
Constructor.
print
Draft customers invoices.
Definition: index.php:91
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('country',$datfile); $geoip->getCountryCodeFromIP($ip)...