dolibarr 18.0.6
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
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 require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
60 }
61 } elseif ($type == 'city') {
62 // geoip may have been already included with PEAR
63 if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
64 require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
65 }
66 } else {
67 print 'ErrorBadParameterInConstructor';
68 return 0;
69 }
70
71 // Here, function exists (embedded into PHP or exists because we made include)
72 if (empty($type) || empty($datfile)) {
73 $this->errorlabel = 'Constructor was called with no datafile parameter';
74 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
75 return 0;
76 }
77 if (!file_exists($datfile) || !is_readable($datfile)) {
78 $this->error = 'ErrorGeoIPClassNotInitialized';
79 $this->errorlabel = "Datafile ".$datfile." not found";
80 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
81 return 0;
82 }
83
84 if ($geoipversion == '2') {
85 try {
86 $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
87 } catch (Exception $e) {
88 $this->error = $e->getMessage();
89 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
90 return 0;
91 }
92 } elseif (function_exists('geoip_open') && defined('GEOIP_STANDARD')) {
93 $this->gi = geoip_open($datfile, constant('GEOIP_STANDARD'));
94 } elseif (function_exists('geoip_country_code_by_name')) {
95 $this->gi = 'NOGI'; // We are using embedded php geoip functions
96 //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
97 //print geoip_database_info();
98 } else {
99 $this->gi = ''; // For avoid error
100 }
101 }
102
109 public function getCountryCodeFromIP($ip)
110 {
111 global $conf;
112
113 $geoipversion = '2'; // 'php', or '2'
114 if (!empty($conf->global->GEOIP_VERSION)) {
115 $geoipversion = $conf->global->GEOIP_VERSION;
116 }
117
118 if (empty($this->gi)) {
119 return '';
120 }
121 if ($this->gi == 'NOGI') {
122 // geoip_country_code_by_addr does not exists
123 return strtolower(geoip_country_code_by_name($ip));
124 } else {
125 if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
126 if ($geoipversion == '2') {
127 try {
128 $record = $this->gi->country($ip);
129 return strtolower($record->country->isoCode);
130 } catch (Exception $e) {
131 //return $e->getMessage();
132 return '';
133 }
134 } else {
135 if (!function_exists('geoip_country_code_by_addr')) {
136 return strtolower(geoip_country_code_by_name($ip));
137 }
138 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
139 }
140 } else {
141 if ($geoipversion == '2') {
142 try {
143 $record = $this->gi->country($ip);
144 return strtolower($record->country->isoCode);
145 } catch (Exception $e) {
146 //return $e->getMessage();
147 return '';
148 }
149 } else {
150 if (function_exists('geoip_country_code_by_addr_v6')) {
151 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
152 } elseif (function_exists('geoip_country_code_by_name_v6')) {
153 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
154 }
155 return '';
156 }
157 }
158 }
159 }
160
167 public function getCountryCodeFromName($name)
168 {
169 global $conf;
170
171 $geoipversion = '2'; // 'php', or '2'
172 if (!empty($conf->global->GEOIP_VERSION)) {
173 $geoipversion = $conf->global->GEOIP_VERSION;
174 }
175
176 if (empty($this->gi)) {
177 return '';
178 }
179
180 if ($geoipversion == '2') {
181 try {
182 $record = $this->gi->country($name);
183 return $record->country->isoCode;
184 } catch (Exception $e) {
185 //return $e->getMessage();
186 return '';
187 }
188 } else {
189 return strtolower(geoip_country_code_by_name($name));
190 }
191 }
192
198 public function getVersion()
199 {
200 global $conf;
201
202 $geoipversion = '2'; // 'php', or '2'
203 if (!empty($conf->global->GEOIP_VERSION)) {
204 $geoipversion = $conf->global->GEOIP_VERSION;
205 }
206
207 if ($geoipversion == 'php') {
208 if ($this->gi == 'NOGI') {
209 return geoip_database_info();
210 } else {
211 return 'geoip_database_info() function not available';
212 }
213 }
214
215 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
216 }
217
223 public function close()
224 {
225 if (function_exists('geoip_close')) {
226 // With some geoip with PEAR, geoip_close function may not exists
227 geoip_close($this->gi);
228 }
229 }
230}
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.