dolibarr  17.0.4
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 '<br><br>';
167  print '<br><span class="opacitymedium">'.$langs->trans("TestGeoIPResult", $ip).':</span>';
168 
169  $ip = '24.24.24.24';
170  print '<br>'.$ip.' -> ';
171  $result = dol_print_ip($ip, 1);
172  if ($result) {
173  print $result;
174  } else {
175  print $langs->trans("Error");
176  }
177 
178  $ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
179  print '<br>'.$ip.' -> ';
180  $result = dol_print_ip($ip, 1);
181  if ($result) {
182  print $result;
183  } else {
184  print $langs->trans("Error");
185  }
186 
187 
188  /* We disable this test because dol_print_ip need an ip as input
189  $ip='www.google.com';
190  print '<br>'.$ip.' -> ';
191  $result=dol_print_ip($ip,1);
192  if ($result) print $result;
193  else print $langs->trans("Error");
194  */
195  //var_dump($_SERVER);
196  $ip = getUserRemoteIP();
197  //$ip='91.161.249.43';
198  $isip = is_ip($ip);
199  if ($isip == 1) {
200  print '<br>'.$ip.' -> ';
201  $result = dol_print_ip($ip, 1);
202  if ($result) {
203  print $result;
204  } else {
205  print $langs->trans("Error");
206  }
207  } else {
208  print '<br>'.$ip.' -> ';
209  $result = dol_print_ip($ip, 1);
210  if ($result) {
211  print $result;
212  } else {
213  print $langs->trans("NotAPublicIp");
214  }
215  }
216 
217  $geoip->close();
218 }
219 
220 // End of page
221 llxFooter();
222 $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:632
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
Classe to manage GeoIP Usage: $geoip=new GeoIP('country',$datfile); $geoip->getCountryCodeFromIP($ip)...
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) 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.