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
19/**
20 *	\file       htdocs/admin/geoipmaxmind.php
21 *	\ingroup    geoipmaxmind
22 *	\brief      Setup page for geoipmaxmind module
23 */
24
25require '../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
29
30// Security check
31if (!$user->admin)
32accessforbidden();
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
44if ($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
78if (!isset($conf->global->GEOIP_VERSION)) $conf->global->GEOIP_VERSION = '2';
79
80
81/*
82 * View
83 */
84
85$form = new Form($db);
86
87llxHeader();
88
89$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
90print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup');
91print '<br>';
92
93$version = '';
94$geoip = '';
95if (!empty($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE))
96{
97	$geoip = new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
98}
99
100// Mode
101print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
102print '<input type="hidden" name="token" value="'.newToken().'">';
103print '<input type="hidden" name="action" value="set">';
104
105print '<table class="noborder centpercent">';
106print '<tr class="liste_titre">';
107print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
108print '<td class="right"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
109print "</tr>\n";
110
111// Lib version
112print '<tr class="oddeven"><td width="50%">'.$langs->trans("GeoIPLibVersion").'</td>';
113print '<td colspan="2">';
114$arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
115print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
116if ($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}
124print '</td></tr>';
125
126// Path to database file
127print '<tr class="oddeven"><td>'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
128print '<td colspan="2">';
129
130if ($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}
134print '<input size="50" type="text" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.dol_escape_htmltag($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE).'">';
135print '</td></tr>';
136
137print '</table>';
138
139print "</form>\n";
140
141print '<br>';
142
143print $langs->trans("NoteOnPathLocation").'<br>';
144
145$url1 = 'http://www.maxmind.com/en/city?rId=awstats';
146print $langs->trans("YouCanDownloadFreeDatFileTo", '<a href="'.$url1.'" target="_blank">'.$url1.'</a>');
147
148print '<br>';
149
150$url2 = 'http://www.maxmind.com/en/city?rId=awstats';
151print $langs->trans("YouCanDownloadAdvancedDatFileTo", '<a href="'.$url2.'" target="_blank">'.$url2.'</a>');
152
153if ($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
199llxFooter();
200$db->close();
201