dolibarr 21.0.0-beta
geoipmaxmind.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2009-2019 Laurent Destailleur <eldy@users.sourceforge.org>
3 * Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
32
41// Security check
42if (!$user->admin) {
44}
45
46// Load translation files required by the page
47$langs->loadLangs(array("admin", "errors"));
48
49$action = GETPOST('action', 'aZ09');
50
51
52/*
53 * Actions
54 */
55
56if ($action == 'set') {
57 $error = 0;
58
59 $gimcdf = GETPOST("GEOIPMAXMIND_COUNTRY_DATAFILE");
60
61 if (!$error && $gimcdf && !preg_match('/\.(dat|mmdb)$/', $gimcdf)) {
62 setEventMessages($langs->trans("ErrorFileMustHaveFormat", '.dat|.mmdb'), null, 'errors');
63 $error++;
64 }
65
66 $res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity);
67 if (!($res1 > 0)) {
68 $error++;
69 }
70
71 $res2 = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
72 if (!($res2 > 0)) {
73 $error++;
74 }
75
76 if (!$error) {
77 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
78 } else {
79 //setEventMessages($langs->trans("Error"), null, 'errors');
80 }
81}
82
83if (!isset($conf->global->GEOIP_VERSION)) {
84 $conf->global->GEOIP_VERSION = '2';
85}
86
87
88/*
89 * View
90 */
91
92$form = new Form($db);
93
94llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-geoipmaxmind');
95
96$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
97print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup');
98print '<br>';
99
100$version = '';
101$geoip = '';
102if (getDolGlobalString('GEOIPMAXMIND_COUNTRY_DATAFILE')) {
103 $geoip = new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
104}
105
106// Mode
107print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
108print '<input type="hidden" name="token" value="'.newToken().'">';
109print '<input type="hidden" name="action" value="set">';
110
111print '<table class="noborder centpercent">';
112print '<tr class="liste_titre">';
113print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
114print '<td class="right"><input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'"></td>';
115print "</tr>\n";
116
117// Lib version
118print '<tr class="oddeven"><td>'.$langs->trans("GeoIPLibVersion").'</td>';
119print '<td>';
120$arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
121print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
122if (getDolGlobalString('GEOIP_VERSION') == 'php') {
123 if ($geoip) {
124 $version = $geoip->getVersion();
125 }
126 if ($version) {
127 print '<br>'.$langs->trans("Version").': '.$version;
128 }
129}
130print '</td>';
131print '<td>';
132print '</td></tr>';
133
134$gimcdf = getDolGlobalString('GEOIPMAXMIND_COUNTRY_DATAFILE');
135
136// Path to database file
137print '<tr class="oddeven"><td>'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
138print '<td>';
139if (getDolGlobalString('GEOIP_VERSION') == 'php') {
140 print 'Using geoip PHP internal functions. Value must be '.geoip_db_filename(GEOIP_COUNTRY_EDITION).' or '.geoip_db_filename(GEOIP_CITY_EDITION_REV1).' or /pathtodatafile/GeoLite2-Country.mmdb<br>';
141}
142print '<input type="text" class="minwidth200" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.dol_escape_htmltag(getDolGlobalString('GEOIPMAXMIND_COUNTRY_DATAFILE')).'">';
143if (!file_exists($gimcdf)) {
144 print '<div class="error">'.$langs->trans("ErrorFileNotFound", $gimcdf).'</div>';
145}
146print '</td><td>';
147print '<span class="opacitymedium">';
148print $langs->trans("Example").'<br>';
149print '/usr/local/share/GeoIP/GeoIP.dat<br>
150/usr/share/GeoIP/GeoIP.dat<br>
151/usr/share/GeoIP/GeoLite2-Country.mmdb';
152print '</span>';
153print '</td></tr>';
154
155print '</table>';
156
157print "</form>\n";
158
159print '<br>';
160
161print $langs->trans("NoteOnPathLocation").'<br>';
162
163$url1 = 'http://www.maxmind.com/en/city?rId=awstats';
164$textoshow = $langs->trans("YouCanDownloadFreeDatFileTo", '{s1}');
165$textoshow = str_replace('{s1}', '<a href="'.$url1.'" target="_blank" rel="noopener noreferrer external">'.$url1.'</a>', $textoshow);
166print $textoshow;
167
168print '<br>';
169
170$url2 = 'http://www.maxmind.com/en/city?rId=awstats';
171$textoshow = $langs->trans("YouCanDownloadAdvancedDatFileTo", '{s1}');
172$textoshow = str_replace('{s1}', '<a href="'.$url2.'" target="_blank" rel="noopener noreferrer external">'.$url2.'</a>', $textoshow);
173print $textoshow;
174
175if ($geoip) {
176 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
177 print '<input type="hidden" name="token" value="'.newToken().'">';
178
179 $ip = '24.24.24.24';
180
181 print '<br><br>';
182 print '<br><span class="opacitymedium">'.$langs->trans("TestGeoIPResult", $ip).':</span>';
183
184 print '<br>'.$ip.' -> ';
185 $result = dol_print_ip($ip, 1);
186 if ($result) {
187 print $result;
188 } else {
189 print $langs->trans("Error");
190 }
191
192 $ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
193 print '<br>'.$ip.' -> ';
194 $result = dol_print_ip($ip, 1);
195 if ($result) {
196 print $result;
197 } else {
198 print $langs->trans("Error");
199 }
200
201
202 /* We disable this test because dol_print_ip need an ip as input
203 $ip='www.google.com';
204 print '<br>'.$ip.' -> ';
205 $result=dol_print_ip($ip,1);
206 if ($result) print $result;
207 else print $langs->trans("Error");
208 */
209 //var_dump($_SERVER);
210 $ip = getUserRemoteIP();
211 //$ip='91.161.249.43';
212 $isip = is_ip($ip);
213 if ($isip == 1) {
214 print '<br>'.$ip.' -> ';
215 $result = dol_print_ip($ip, 1);
216 if ($result) {
217 print $result;
218 } else {
219 print $langs->trans("Error");
220 }
221 } else {
222 print '<br>'.$ip.' -> ';
223 $result = dol_print_ip($ip, 1);
224 if ($result) {
225 print $result;
226 } else {
227 print $langs->trans("NotAPublicIp");
228 }
229 }
230
231 $ip = GETPOST("iptotest");
232 print '<br><input type="text class="width100" name="iptotest" id="iptotest" placeholder="'.dol_escape_htmltag($langs->trans("EnterAnIP")).'" value="'.$ip.'">';
233 print '<input type="submit" class="width40 button small smallpaddingimp" value=" -> ">';
234 if ($ip) {
235 $result = dol_print_ip($ip, 1);
236 if ($result) {
237 print $result;
238 } else {
239 print $langs->trans("Error");
240 }
241 }
242
243 print '</form>';
244
245 $geoip->close();
246}
247
248// End of page
249llxFooter();
250$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage GeoIP conversion Usage: $geoip=new GeoIP('country',$datfile); $geoip->getCountryCodeF...
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
is_ip($ip)
This function evaluates a string that should be a valid IPv4 Note: For ip 169.254....
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dol_print_ip($ip, $mode=0)
Return an IP formatted to be shown on screen.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
getUserRemoteIP()
Return the IP of remote user.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.