dolibarr 19.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 *
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{
39 public $gi;
40
41 public $error;
42 public $errorlabel;
43
50 public function __construct($type, $datfile)
51 {
52 global $conf;
53
54 $geoipversion = '2'; // 'php', or geoip version '2'
55 if (getDolGlobalString('GEOIP_VERSION')) {
56 $geoipversion = $conf->global->GEOIP_VERSION;
57 }
58
59 if ($type == 'country') {
60 // geoip may have been already included with PEAR
61 if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
62 if (function_exists('stream_wrapper_restore')) {
63 stream_wrapper_restore('phar');
64 }
65 require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
66 }
67 } elseif ($type == 'city') {
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 require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
74 }
75 } else {
76 print 'ErrorBadParameterInConstructor';
77 return;
78 }
79
80 // Here, function exists (embedded into PHP or exists because we made include)
81 if (empty($type) || empty($datfile)) {
82 $this->errorlabel = 'Constructor was called with no datafile parameter';
83 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
84 return;
85 }
86 if (!file_exists($datfile) || !is_readable($datfile)) {
87 $this->error = 'ErrorGeoIPClassNotInitialized';
88 $this->errorlabel = "Datafile ".$datfile." not found";
89 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
90 return;
91 }
92
93 if ($geoipversion == '2') {
94 try {
95 // @phpstan-ignore-next-line
96 $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
97 } catch (Exception $e) {
98 $this->error = $e->getMessage();
99 dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
100 return;
101 }
102 } elseif (function_exists('geoip_open') && defined('GEOIP_STANDARD')) {
103 $this->gi = geoip_open($datfile, constant('GEOIP_STANDARD'));
104 } elseif (function_exists('geoip_country_code_by_name')) {
105 $this->gi = 'NOGI'; // We are using embedded php geoip functions
106 //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
107 //print geoip_database_info();
108 } else {
109 $this->gi = ''; // For avoid error
110 }
111 }
112
119 public function getCountryCodeFromIP($ip)
120 {
121 global $conf;
122
123 $geoipversion = '2'; // 'php', or '2'
124 if (getDolGlobalString('GEOIP_VERSION')) {
125 $geoipversion = $conf->global->GEOIP_VERSION;
126 }
127
128 if (empty($this->gi)) {
129 return '';
130 }
131 if ($this->gi == 'NOGI') {
132 // geoip_country_code_by_addr does not exists
133 return strtolower(geoip_country_code_by_name($ip));
134 } else {
135 if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
136 if ($geoipversion == '2') {
137 try {
138 $record = $this->gi->country($ip);
139 return strtolower($record->country->isoCode);
140 } catch (Exception $e) {
141 //return $e->getMessage();
142 return '';
143 }
144 } else {
145 if (!function_exists('geoip_country_code_by_addr')) {
146 return strtolower(geoip_country_code_by_name($ip));
147 }
148 return strtolower(geoip_country_code_by_addr($this->gi, $ip));
149 }
150 } else {
151 if ($geoipversion == '2') {
152 try {
153 $record = $this->gi->country($ip);
154 return strtolower($record->country->isoCode);
155 } catch (Exception $e) {
156 //return $e->getMessage();
157 return '';
158 }
159 } else {
160 if (function_exists('geoip_country_code_by_addr_v6')) {
161 return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
162 } elseif (function_exists('geoip_country_code_by_name_v6')) {
163 return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
164 }
165 return '';
166 }
167 }
168 }
169 }
170
177 public function getCountryCodeFromName($name)
178 {
179 global $conf;
180
181 $geoipversion = '2'; // 'php', or '2'
182 if (getDolGlobalString('GEOIP_VERSION')) {
183 $geoipversion = $conf->global->GEOIP_VERSION;
184 }
185
186 if (empty($this->gi)) {
187 return '';
188 }
189
190 if ($geoipversion == '2') {
191 try {
192 $record = $this->gi->country($name);
193 return $record->country->isoCode;
194 } catch (Exception $e) {
195 //return $e->getMessage();
196 return '';
197 }
198 } else {
199 return strtolower(geoip_country_code_by_name($name));
200 }
201 }
202
208 public function getVersion()
209 {
210 global $conf;
211
212 $geoipversion = '2'; // 'php', or '2'
213 if (getDolGlobalString('GEOIP_VERSION')) {
214 $geoipversion = $conf->global->GEOIP_VERSION;
215 }
216
217 if ($geoipversion == 'php') {
218 if ($this->gi == 'NOGI') {
219 return geoip_database_info();
220 } else {
221 return 'geoip_database_info() function not available';
222 }
223 }
224
225 return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
226 }
227
233 public function close()
234 {
235 if (function_exists('geoip_close')) {
236 // With some geoip with PEAR, geoip_close function may not exists
237 geoip_close($this->gi);
238 }
239 }
240}
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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.