dolibarr 22.0.5
dolgeoip.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
37{
41 public $gi;
42
46 public $error;
47
51 public $errorlabel;
52
59 public function __construct($type, $datfile)
60 {
61 $geoipversion = '2'; // 'php', or geoip version '2'
62 if (getDolGlobalString('GEOIP_VERSION')) {
63 $geoipversion = getDolGlobalString('GEOIP_VERSION');
64 }
65
66 try {
67 if ($type == 'country') {
68 // geoip may have been already included with PEAR
69 if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
70 if (function_exists('stream_wrapper_restore')) {
71 stream_wrapper_restore('phar');
72 }
73 include_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
74 }
75 } elseif ($type == 'city') {
76 // geoip may have been already included with PEAR
77 if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
78 if (function_exists('stream_wrapper_restore')) {
79 stream_wrapper_restore('phar');
80 }
81 include_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
82 }
83 } else {
84 print 'ErrorBadParameterInConstructor';
85 return;
86 }
87 } catch (Exception $e) {
88 $this->error = $e->getMessage();
89 $this->errorlabel = 'Exception '.$this->error;
90 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
91 return;
92 }
93
94 // Here, function exists (embedded into PHP or exists because we made include)
95 if (empty($type) || empty($datfile)) {
96 $this->errorlabel = 'Constructor was called with no datafile parameter';
97 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
98 return;
99 }
100 if (!file_exists($datfile) || !is_readable($datfile)) {
101 $this->error = 'ErrorGeoIPClassNotInitialized';
102 $this->errorlabel = "Datafile ".$datfile." not found";
103 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
104 return;
105 }
106
107 if ($geoipversion == '2') {
108 try {
109 // @phpstan-ignore-next-line
110 $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
111 } catch (Exception $e) {
112 $this->error = $e->getMessage();
113 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
114 return;
115 }
116 } elseif (function_exists('geoip_open') && defined('GEOIP_STANDARD')) {
117 $this->gi = geoip_open($datfile, constant('GEOIP_STANDARD'));
118 } elseif (function_exists('geoip_country_code_by_name')) {
119 $this->gi = 'NOGI'; // We are using embedded php geoip functions
120 //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
121 //print geoip_database_info();
122 } else {
123 $this->gi = ''; // For avoid error
124 }
125 }
126
133 public function getCountryCodeFromIP($ip)
134 {
135 global $conf;
136
137 $geoipversion = '2'; // 'php', or '2'
138 if (getDolGlobalString('GEOIP_VERSION')) {
139 $geoipversion = getDolGlobalString('GEOIP_VERSION');
140 }
141
142 if (empty($this->gi)) {
143 return '';
144 }
145 if ($this->gi == 'NOGI') {
146 // geoip_country_code_by_addr does not exists
147 return strtolower(geoip_country_code_by_name($ip));
148 } else {
149 if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
150 if ($geoipversion == '2') {
151 try {
152 $record = $this->gi->country($ip);
153 return strtolower($record->country->isoCode);
154 } catch (Exception $e) {
155 //return $e->getMessage();
156 return '';
157 }
158 } else {
159 if (!function_exists('geoip_country_code_by_addr')) {
160 return strtolower(geoip_country_code_by_name($ip));
161 }
162 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
163 }
164 } else {
165 if ($geoipversion == '2') {
166 try {
167 $record = $this->gi->country($ip);
168 return strtolower($record->country->isoCode);
169 } catch (Exception $e) {
170 //return $e->getMessage();
171 return '';
172 }
173 } else {
174 if (function_exists('geoip_country_code_by_addr_v6')) {
175 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
176 } elseif (function_exists('geoip_country_code_by_name_v6')) {
177 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
178 }
179 return '';
180 }
181 }
182 }
183 }
184
191 public function getCountryCodeFromName($name)
192 {
193 global $conf;
194
195 $geoipversion = '2'; // 'php', or '2'
196 if (getDolGlobalString('GEOIP_VERSION')) {
197 $geoipversion = getDolGlobalString('GEOIP_VERSION');
198 }
199
200 if (empty($this->gi)) {
201 return '';
202 }
203
204 if ($geoipversion == '2') {
205 try {
206 $record = $this->gi->country($name);
207 return $record->country->isoCode;
208 } catch (Exception $e) {
209 //return $e->getMessage();
210 return '';
211 }
212 } else {
213 return strtolower(geoip_country_code_by_name($name));
214 }
215 }
216
222 public function getVersion()
223 {
224 global $conf;
225
226 $geoipversion = '2'; // 'php', or '2'
227 if (getDolGlobalString('GEOIP_VERSION')) {
228 $geoipversion = getDolGlobalString('GEOIP_VERSION');
229 }
230
231 if ($geoipversion == 'php') {
232 if ($this->gi == 'NOGI') {
233 return geoip_database_info();
234 } else {
235 return 'geoip_database_info() function not available';
236 }
237 }
238
239 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
240 }
241
247 public function close()
248 {
249 if (function_exists('geoip_close')) {
250 // With some geoip with PEAR, geoip_close function may not exists
251 geoip_close($this->gi);
252 }
253 }
254}
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 version of data file.
close()
Close geoip object.
getCountryCodeFromName($name)
Return in lower case the country code from a host name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79