dolibarr  13.0.2
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 // Load translation files required by the page
35 $langs->loadLangs(array("admin", "errors"));
36 
37 $action = GETPOST('action', 'aZ09');
38 
39 
40 /*
41  * Actions
42  */
43 
44 if ($action == 'set')
45 {
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  {
57  setEventMessages($langs->trans("ErrorFileNotFound", $gimcdf), null, 'errors');
58  $error++;
59  }
60 
61  if (!$error)
62  {
63  $res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity);
64  if (!$res1 > 0) $error++;
65 
66  $res2 = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
67  if (!$res2 > 0) $error++;
68 
69  if (!$error)
70  {
71  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
72  } else {
73  setEventMessages($langs->trans("Error"), null, 'errors');
74  }
75  }
76 }
77 
78 if (!isset($conf->global->GEOIP_VERSION)) $conf->global->GEOIP_VERSION = '2';
79 
80 
81 /*
82  * View
83  */
84 
85 $form = new Form($db);
86 
87 llxHeader();
88 
89 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
90 print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup');
91 print '<br>';
92 
93 $version = '';
94 $geoip = '';
95 if (!empty($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE))
96 {
97  $geoip = new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
98 }
99 
100 // Mode
101 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
102 print '<input type="hidden" name="token" value="'.newToken().'">';
103 print '<input type="hidden" name="action" value="set">';
104 
105 print '<table class="noborder centpercent">';
106 print '<tr class="liste_titre">';
107 print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
108 print '<td class="right"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
109 print "</tr>\n";
110 
111 // Lib version
112 print '<tr class="oddeven"><td width="50%">'.$langs->trans("GeoIPLibVersion").'</td>';
113 print '<td colspan="2">';
114 $arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
115 print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
116 if ($conf->global->GEOIP_VERSION == 'php')
117 {
118  if ($geoip) $version = $geoip->getVersion();
119  if ($version)
120  {
121  print '<br>'.$langs->trans("Version").': '.$version;
122  }
123 }
124 print '</td></tr>';
125 
126 // Path to database file
127 print '<tr class="oddeven"><td>'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
128 print '<td colspan="2">';
129 
130 if ($conf->global->GEOIP_VERSION == 'php')
131 {
132  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>';
133 }
134 print '<input size="50" type="text" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.dol_escape_htmltag($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE).'">';
135 print '</td></tr>';
136 
137 print '</table>';
138 
139 print "</form>\n";
140 
141 print '<br>';
142 
143 print $langs->trans("NoteOnPathLocation").'<br>';
144 
145 $url1 = 'http://www.maxmind.com/en/city?rId=awstats';
146 print $langs->trans("YouCanDownloadFreeDatFileTo", '<a href="'.$url1.'" target="_blank">'.$url1.'</a>');
147 
148 print '<br>';
149 
150 $url2 = 'http://www.maxmind.com/en/city?rId=awstats';
151 print $langs->trans("YouCanDownloadAdvancedDatFileTo", '<a href="'.$url2.'" target="_blank">'.$url2.'</a>');
152 
153 if ($geoip)
154 {
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) print $result;
162  else print $langs->trans("Error");
163 
164  $ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
165  print '<br>'.$ip.' -> ';
166  $result = dol_print_ip($ip, 1);
167  if ($result) print $result;
168  else print $langs->trans("Error");
169 
170 
171  /* We disable this test because dol_print_ip need an ip as input
172  $ip='www.google.com';
173  print '<br>'.$ip.' -> ';
174  $result=dol_print_ip($ip,1);
175  if ($result) print $result;
176  else print $langs->trans("Error");
177  */
178  //var_dump($_SERVER);
179  $ip = getUserRemoteIP();
180  //$ip='91.161.249.43';
181  $isip = is_ip($ip);
182  if ($isip == 1)
183  {
184  print '<br>'.$ip.' -> ';
185  $result = dol_print_ip($ip, 1);
186  if ($result) print $result;
187  else print $langs->trans("Error");
188  } else {
189  print '<br>'.$ip.' -> ';
190  $result = dol_print_ip($ip, 1);
191  if ($result) print $result;
192  else print $langs->trans("NotAPublicIp");
193  }
194 
195  $geoip->close();
196 }
197 
198 // End of page
199 llxFooter();
200 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getUserRemoteIP()
Return the IP of remote user.
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:575
llxHeader()
Empty header.
Definition: wrapper.php:45
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.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
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 ...
is_ip($ip)
This function evaluates a string that should be a valid IPv4 Note: For ip 169.254.0.0, it returns 0 with some PHP (5.6.24) and 2 with some minor patchs of PHP (5.6.25).
print
Draft customers invoices.
Definition: index.php:89
llxFooter()
Empty footer.
Definition: wrapper.php:59
Classe to manage GeoIP Usage: $geoip=new GeoIP(&#39;country&#39;,$datfile); $geoip-&gt;getCountryCodeFromIP($ip)...