dolibarr 24.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 * Copyright (C) 2024-2025 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 $datfile = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $datfile);
62
63 $geoipversion = '2'; // 'php', or geoip version '2'
64 if (getDolGlobalString('GEOIP_VERSION')) {
65 $geoipversion = getDolGlobalString('GEOIP_VERSION');
66 }
67
68 try {
69 if ($type == 'country') {
70 // geoip may have been already included with PEAR
71 if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
72 if (function_exists('stream_wrapper_restore')) {
73 stream_wrapper_restore('phar');
74 }
75 include_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
76 }
77 } elseif ($type == 'city') {
78 // geoip may have been already included with PEAR
79 if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
80 if (function_exists('stream_wrapper_restore')) {
81 stream_wrapper_restore('phar');
82 }
83 include_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
84 }
85 } else {
86 print 'ErrorBadParameterInConstructor';
87 return;
88 }
89 } catch (Exception $e) {
90 $this->error = $e->getMessage();
91 $this->errorlabel = 'Exception '.$this->error;
92 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
93 return;
94 }
95
96 // Here, function exists (embedded into PHP or exists because we made include)
97 if (empty($type) || empty($datfile)) {
98 $this->errorlabel = 'Constructor was called with no datafile parameter';
99 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
100 return;
101 }
102 if (!file_exists($datfile) || !is_readable($datfile)) {
103 $this->error = 'ErrorGeoIPClassNotInitialized';
104 $this->errorlabel = "Datafile ".$datfile." not found";
105 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
106 return;
107 }
108
109 if ($geoipversion == '2') {
110 try {
111 // @phpstan-ignore-next-line
112 $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
113 } catch (Exception $e) {
114 $this->error = $e->getMessage();
115 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
116 return;
117 }
118 } elseif (function_exists('geoip_open') && defined('GEOIP_STANDARD')) {
119 $this->gi = geoip_open($datfile, constant('GEOIP_STANDARD'));
120 } elseif (function_exists('geoip_country_code_by_name')) {
121 $this->gi = 'NOGI'; // We are using embedded php geoip functions
122 //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
123 //print geoip_database_info();
124 } else {
125 $this->gi = ''; // For avoid error
126 }
127 }
128
135 public function getCountryCodeFromIP($ip)
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 $geoipversion = '2'; // 'php', or '2'
194 if (getDolGlobalString('GEOIP_VERSION')) {
195 $geoipversion = getDolGlobalString('GEOIP_VERSION');
196 }
197
198 if (empty($this->gi)) {
199 return '';
200 }
201
202 if ($geoipversion == '2') {
203 try {
204 $record = $this->gi->country($name);
205 return $record->country->isoCode;
206 } catch (Exception $e) {
207 //return $e->getMessage();
208 return '';
209 }
210 } else {
211 return strtolower(geoip_country_code_by_name($name));
212 }
213 }
214
220 public function getVersion()
221 {
222 $geoipversion = '2'; // 'php', or '2'
223 if (getDolGlobalString('GEOIP_VERSION')) {
224 $geoipversion = getDolGlobalString('GEOIP_VERSION');
225 }
226
227 if ($geoipversion == 'php') {
228 if ($this->gi == 'NOGI') {
229 return geoip_database_info();
230 } else {
231 return 'geoip_database_info() function not available';
232 }
233 }
234
235 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
236 }
237
243 public function close()
244 {
245 if (function_exists('geoip_close')) {
246 // With some geoip with PEAR, geoip_close function may not exists
247 geoip_close($this->gi);
248 }
249 }
250}
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.