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