dolibarr 21.0.0-alpha
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 *
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 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
31
32// Security check
33if (!$user->admin) {
35}
36
37// Load translation files required by the page
38$langs->loadLangs(array("admin", "errors"));
39
40$action = GETPOST('action', 'aZ09');
41
42
43/*
44 * Actions
45 */
46
47if ($action == 'set') {
48 $error = 0;
49
50 $gimcdf = GETPOST("GEOIPMAXMIND_COUNTRY_DATAFILE");
51
52 if (!$error && $gimcdf && !preg_match('/\.(dat|mmdb)$/', $gimcdf)) {
53 setEventMessages($langs->trans("ErrorFileMustHaveFormat", '.dat|.mmdb'), null, 'errors');
54 $error++;
55 }
56
57 $res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity);
58 if (!($res1 > 0)) {
59 $error++;
60 }
61
62 $res2 = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
63 if (!($res2 > 0)) {
64 $error++;
65 }
66
67 if (!$error) {
68 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
69 } else {
70 //setEventMessages($langs->trans("Error"), null, 'errors');
71 }
72}
73
74if (!isset($conf->global->GEOIP_VERSION)) {
75 $conf->global->GEOIP_VERSION = '2';
76}
77
78
79/*
80 * View
81 */
82
83$form = new Form($db);
84
85llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-geoipmaxmind');
86
87$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
88print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup');
89print '<br>';
90
91$version = '';
92$geoip = '';
93if (getDolGlobalString('GEOIPMAXMIND_COUNTRY_DATAFILE')) {
94 $geoip = new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
95}
96
97// Mode
98print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
99print '<input type="hidden" name="token" value="'.newToken().'">';
100print '<input type="hidden" name="action" value="set">';
101
102print '<table class="noborder centpercent">';
103print '<tr class="liste_titre">';
104print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
105print '<td class="right"><input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'"></td>';
106print "</tr>\n";
107
108// Lib version
109print '<tr class="oddeven"><td>'.$langs->trans("GeoIPLibVersion").'</td>';
110print '<td>';
111$arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
112print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
113if (getDolGlobalString('GEOIP_VERSION') == 'php') {
114 if ($geoip) {
115 $version = $geoip->getVersion();
116 }
117 if ($version) {
118 print '<br>'.$langs->trans("Version").': '.$version;
119 }
120}
121print '</td>';
122print '<td>';
123print '</td></tr>';
124
125$gimcdf = getDolGlobalString('GEOIPMAXMIND_COUNTRY_DATAFILE');
126
127// Path to database file
128print '<tr class="oddeven"><td>'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
129print '<td>';
130if (getDolGlobalString('GEOIP_VERSION') == 'php') {
131 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>';
132}
133print '<input type="text" class="minwidth200" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.dol_escape_htmltag(getDolGlobalString('GEOIPMAXMIND_COUNTRY_DATAFILE')).'">';
134if (!file_exists($gimcdf)) {
135 print '<div class="error">'.$langs->trans("ErrorFileNotFound", $gimcdf).'</div>';
136}
137print '</td><td>';
138print '<span class="opacitymedium">';
139print $langs->trans("Example").'<br>';
140print '/usr/local/share/GeoIP/GeoIP.dat<br>
141/usr/share/GeoIP/GeoIP.dat<br>
142/usr/share/GeoIP/GeoLite2-Country.mmdb';
143print '</span>';
144print '</td></tr>';
145
146print '</table>';
147
148print "</form>\n";
149
150print '<br>';
151
152print $langs->trans("NoteOnPathLocation").'<br>';
153
154$url1 = 'http://www.maxmind.com/en/city?rId=awstats';
155$textoshow = $langs->trans("YouCanDownloadFreeDatFileTo", '{s1}');
156$textoshow = str_replace('{s1}', '<a href="'.$url1.'" target="_blank" rel="noopener noreferrer external">'.$url1.'</a>', $textoshow);
157print $textoshow;
158
159print '<br>';
160
161$url2 = 'http://www.maxmind.com/en/city?rId=awstats';
162$textoshow = $langs->trans("YouCanDownloadAdvancedDatFileTo", '{s1}');
163$textoshow = str_replace('{s1}', '<a href="'.$url2.'" target="_blank" rel="noopener noreferrer external">'.$url2.'</a>', $textoshow);
164print $textoshow;
165
166if ($geoip) {
167 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
168 print '<input type="hidden" name="token" value="'.newToken().'">';
169
170 $ip = '24.24.24.24';
171
172 print '<br><br>';
173 print '<br><span class="opacitymedium">'.$langs->trans("TestGeoIPResult", $ip).':</span>';
174
175 print '<br>'.$ip.' -> ';
176 $result = dol_print_ip($ip, 1);
177 if ($result) {
178 print $result;
179 } else {
180 print $langs->trans("Error");
181 }
182
183 $ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
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
193 /* We disable this test because dol_print_ip need an ip as input
194 $ip='www.google.com';
195 print '<br>'.$ip.' -> ';
196 $result=dol_print_ip($ip,1);
197 if ($result) print $result;
198 else print $langs->trans("Error");
199 */
200 //var_dump($_SERVER);
201 $ip = getUserRemoteIP();
202 //$ip='91.161.249.43';
203 $isip = is_ip($ip);
204 if ($isip == 1) {
205 print '<br>'.$ip.' -> ';
206 $result = dol_print_ip($ip, 1);
207 if ($result) {
208 print $result;
209 } else {
210 print $langs->trans("Error");
211 }
212 } else {
213 print '<br>'.$ip.' -> ';
214 $result = dol_print_ip($ip, 1);
215 if ($result) {
216 print $result;
217 } else {
218 print $langs->trans("NotAPublicIp");
219 }
220 }
221
222 $ip = GETPOST("iptotest");
223 print '<br><input type="text class="width100" name="iptotest" id="iptotest" placeholder="'.dol_escape_htmltag($langs->trans("EnterAnIP")).'" value="'.$ip.'">';
224 print '<input type="submit" class="width40 button small smallpaddingimp" value=" -> ">';
225 if ($ip) {
226 $result = dol_print_ip($ip, 1);
227 if ($result) {
228 print $result;
229 } else {
230 print $langs->trans("Error");
231 }
232 }
233
234 print '</form>';
235
236 $geoip->close();
237}
238
239// End of page
240llxFooter();
241$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:70
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.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.