dolibarr  16.0.5
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 require '../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
29 
30 // Security check
31 if (!$user->admin) {
33 }
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array("admin", "errors"));
37 
38 $action = GETPOST('action', 'aZ09');
39 
40 
41 /*
42  * Actions
43  */
44 
45 if ($action == 'set') {
46  $error = 0;
47 
48  $gimcdf = GETPOST("GEOIPMAXMIND_COUNTRY_DATAFILE");
49 
50  if (!$error && $gimcdf && !preg_match('/\.(dat|mmdb)$/', $gimcdf)) {
51  setEventMessages($langs->trans("ErrorFileMustHaveFormat", '.dat|.mmdb'), null, 'errors');
52  $error++;
53  }
54 
55  if (!$error && $gimcdf && !file_exists($gimcdf)) {
56  setEventMessages($langs->trans("ErrorFileNotFound", $gimcdf), null, 'errors');
57  $error++;
58  }
59 
60  if (!$error) {
61  $res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity);
62  if (!($res1 > 0)) {
63  $error++;
64  }
65 
66  $res2 = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
67  if (!($res2 > 0)) {
68  $error++;
69  }
70 
71  if (!$error) {
72  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
73  } else {
74  setEventMessages($langs->trans("Error"), null, 'errors');
75  }
76  }
77 }
78 
79 if (!isset($conf->global->GEOIP_VERSION)) {
80  $conf->global->GEOIP_VERSION = '2';
81 }
82 
83 
84 /*
85  * View
86  */
87 
88 $form = new Form($db);
89 
90 llxHeader();
91 
92 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
93 print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup');
94 print '<br>';
95 
96 $version = '';
97 $geoip = '';
98 if (!empty($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE)) {
99  $geoip = new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
100 }
101 
102 // Mode
103 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
104 print '<input type="hidden" name="token" value="'.newToken().'">';
105 print '<input type="hidden" name="action" value="set">';
106 
107 print '<table class="noborder centpercent">';
108 print '<tr class="liste_titre">';
109 print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
110 print '<td class="right"><input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'"></td>';
111 print "</tr>\n";
112 
113 // Lib version
114 print '<tr class="oddeven"><td width="50%">'.$langs->trans("GeoIPLibVersion").'</td>';
115 print '<td colspan="2">';
116 $arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
117 print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
118 if ($conf->global->GEOIP_VERSION == 'php') {
119  if ($geoip) {
120  $version = $geoip->getVersion();
121  }
122  if ($version) {
123  print '<br>'.$langs->trans("Version").': '.$version;
124  }
125 }
126 print '</td></tr>';
127 
128 // Path to database file
129 print '<tr class="oddeven"><td>'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
130 print '<td colspan="2">';
131 
132 if ($conf->global->GEOIP_VERSION == 'php') {
133  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>';
134 }
135 print '<input type="text" class="minwidth200" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.dol_escape_htmltag($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE).'">';
136 print '</td></tr>';
137 
138 print '</table>';
139 
140 print "</form>\n";
141 
142 print '<br>';
143 
144 print $langs->trans("NoteOnPathLocation").'<br>';
145 
146 $url1 = 'http://www.maxmind.com/en/city?rId=awstats';
147 print $langs->trans("YouCanDownloadFreeDatFileTo", '<a href="'.$url1.'" target="_blank" rel="noopener noreferrer external">'.$url1.'</a>');
148 
149 print '<br>';
150 
151 $url2 = 'http://www.maxmind.com/en/city?rId=awstats';
152 print $langs->trans("YouCanDownloadAdvancedDatFileTo", '<a href="'.$url2.'" target="_blank" rel="noopener noreferrer external">'.$url2.'</a>');
153 
154 if ($geoip) {
155  print '<br><br>';
156  print '<br><span class="opacitymedium">'.$langs->trans("TestGeoIPResult", $ip).':</span>';
157 
158  $ip = '24.24.24.24';
159  print '<br>'.$ip.' -> ';
160  $result = dol_print_ip($ip, 1);
161  if ($result) {
162  print $result;
163  } else {
164  print $langs->trans("Error");
165  }
166 
167  $ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
168  print '<br>'.$ip.' -> ';
169  $result = dol_print_ip($ip, 1);
170  if ($result) {
171  print $result;
172  } else {
173  print $langs->trans("Error");
174  }
175 
176 
177  /* We disable this test because dol_print_ip need an ip as input
178  $ip='www.google.com';
179  print '<br>'.$ip.' -> ';
180  $result=dol_print_ip($ip,1);
181  if ($result) print $result;
182  else print $langs->trans("Error");
183  */
184  //var_dump($_SERVER);
185  $ip = getUserRemoteIP();
186  //$ip='91.161.249.43';
187  $isip = is_ip($ip);
188  if ($isip == 1) {
189  print '<br>'.$ip.' -> ';
190  $result = dol_print_ip($ip, 1);
191  if ($result) {
192  print $result;
193  } else {
194  print $langs->trans("Error");
195  }
196  } else {
197  print '<br>'.$ip.' -> ';
198  $result = dol_print_ip($ip, 1);
199  if ($result) {
200  print $result;
201  } else {
202  print $langs->trans("NotAPublicIp");
203  }
204  }
205 
206  $geoip->close();
207 }
208 
209 // End of page
210 llxFooter();
211 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
DolGeoIP
Classe to manage GeoIP Usage: $geoip=new GeoIP('country',$datfile); $geoip->getCountryCodeFromIP($ip)...
Definition: dolgeoip.class.php:34
dol_print_ip
dol_print_ip($ip, $mode=0)
Return an IP formated to be shown on screen.
Definition: functions.lib.php:3481
dolibarr_set_const
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).
Definition: admin.lib.php:627
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
is_ip
is_ip($ip)
This function evaluates a string that should be a valid IPv4 Note: For ip 169.254....
Definition: functions2.lib.php:2012
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
getUserRemoteIP
getUserRemoteIP()
Return the IP of remote user.
Definition: functions.lib.php:3515