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