dolibarr 23.0.4
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
138 private function getGeoIp2IsoCode($ipOrName)
139 {
140 try {
141 $databasetype = '';
142 if (is_object($this->gi) && method_exists($this->gi, 'metadata')) {
143 $databasetype = (string) $this->gi->metadata()->databaseType;
144 }
145 if (strpos($databasetype, 'Country') === false && strpos($databasetype, 'City') !== false) {
146 $record = $this->gi->city($ipOrName);
147 } else {
148 $record = $this->gi->country($ipOrName);
149 }
150 return (string) $record->country->isoCode;
151 } catch (Exception $e) {
152 //return $e->getMessage();
153 return '';
154 }
155 }
156
163 public function getCountryCodeFromIP($ip)
164 {
165 $geoipversion = '2'; // 'php', or '2'
166 if (getDolGlobalString('GEOIP_VERSION')) {
167 $geoipversion = getDolGlobalString('GEOIP_VERSION');
168 }
169
170 if (empty($this->gi)) {
171 return '';
172 }
173 if ($this->gi == 'NOGI') {
174 // geoip_country_code_by_addr does not exists
175 return strtolower(geoip_country_code_by_name($ip));
176 } else {
177 if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
178 if ($geoipversion == '2') {
179 return strtolower($this->getGeoIp2IsoCode($ip));
180 } else {
181 if (!function_exists('geoip_country_code_by_addr')) {
182 return strtolower(geoip_country_code_by_name($ip));
183 }
184 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
185 }
186 } else {
187 if ($geoipversion == '2') {
188 return strtolower($this->getGeoIp2IsoCode($ip));
189 } else {
190 if (function_exists('geoip_country_code_by_addr_v6')) {
191 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
192 } elseif (function_exists('geoip_country_code_by_name_v6')) {
193 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
194 }
195 return '';
196 }
197 }
198 }
199 }
200
207 public function getCountryCodeFromName($name)
208 {
209 $geoipversion = '2'; // 'php', or '2'
210 if (getDolGlobalString('GEOIP_VERSION')) {
211 $geoipversion = getDolGlobalString('GEOIP_VERSION');
212 }
213
214 if (empty($this->gi)) {
215 return '';
216 }
217
218 if ($geoipversion == '2') {
219 return $this->getGeoIp2IsoCode($name);
220 } else {
221 return strtolower(geoip_country_code_by_name($name));
222 }
223 }
224
230 public function getVersion()
231 {
232 $geoipversion = '2'; // 'php', or '2'
233 if (getDolGlobalString('GEOIP_VERSION')) {
234 $geoipversion = getDolGlobalString('GEOIP_VERSION');
235 }
236
237 if ($geoipversion == 'php') {
238 if ($this->gi == 'NOGI') {
239 return geoip_database_info();
240 } else {
241 return 'geoip_database_info() function not available';
242 }
243 }
244
245 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
246 }
247
253 public function close()
254 {
255 if (function_exists('geoip_close')) {
256 // With some geoip with PEAR, geoip_close function may not exists
257 geoip_close($this->gi);
258 }
259 }
260}
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.
getGeoIp2IsoCode($ipOrName)
Return the ISO country code for an IP or a host name using the embedded GeoIP2 reader.
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.