dolibarr/htdocs/admin/geoipmaxmind.php

212 lines
6.0 KiB
PHP
Raw Normal View History

2009-10-14 13:04:09 +02:00
<?php
2019-08-22 01:25:25 +02:00
/* Copyright (C) 2009-2019 Laurent Destailleur <eldy@users.sourceforge.org>
* Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
2011-09-21 15:24:27 +02:00
*
2009-10-14 13:04:09 +02:00
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2009-10-14 13:04:09 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2009-10-14 13:04:09 +02:00
*/
/**
* \file htdocs/admin/geoipmaxmind.php
* \ingroup geoipmaxmind
* \brief Setup page for geoipmaxmind module
*/
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
2018-11-21 15:11:57 +01:00
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
2009-10-14 13:04:09 +02:00
// Security check
2021-02-26 22:04:03 +01:00
if (!$user->admin) {
accessforbidden();
}
2009-10-14 13:04:09 +02:00
2018-05-26 18:41:16 +02:00
// Load translation files required by the page
$langs->loadLangs(array("admin", "errors"));
2009-10-14 13:04:09 +02:00
$action = GETPOST('action', 'aZ09');
2011-09-06 12:52:42 +02:00
2019-08-22 01:25:25 +02:00
2009-10-14 13:04:09 +02:00
/*
* Actions
*/
2019-08-22 01:25:25 +02:00
2021-02-26 22:04:03 +01:00
if ($action == 'set') {
$error = 0;
2011-09-21 15:24:27 +02:00
$gimcdf = GETPOST("GEOIPMAXMIND_COUNTRY_DATAFILE");
2011-09-21 15:24:27 +02:00
if (!$error && $gimcdf && !preg_match('/\.(dat|mmdb)$/', $gimcdf)) {
2020-09-24 21:09:58 +02:00
setEventMessages($langs->trans("ErrorFileMustHaveFormat", '.dat|.mmdb'), null, 'errors');
$error++;
}
2021-02-26 22:04:03 +01:00
if (!$error && $gimcdf && !file_exists($gimcdf)) {
setEventMessages($langs->trans("ErrorFileNotFound", $gimcdf), null, 'errors');
2009-10-14 13:04:09 +02:00
$error++;
}
2011-09-21 15:24:27 +02:00
2021-02-26 22:04:03 +01:00
if (!$error) {
$res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity);
2022-08-12 15:54:50 +02:00
if (!($res1 > 0)) {
2021-02-26 22:04:03 +01:00
$error++;
}
$res2 = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
2022-08-12 15:54:50 +02:00
if (!($res2 > 0)) {
2021-02-26 22:04:03 +01:00
$error++;
}
2011-09-21 15:24:27 +02:00
2021-02-26 22:04:03 +01:00
if (!$error) {
2015-10-28 19:28:18 +01:00
setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
2020-05-21 09:35:30 +02:00
} else {
2015-10-28 19:28:18 +01:00
setEventMessages($langs->trans("Error"), null, 'errors');
}
2009-10-14 13:04:09 +02:00
}
}
2021-02-26 22:04:03 +01:00
if (!isset($conf->global->GEOIP_VERSION)) {
$conf->global->GEOIP_VERSION = '2';
}
2009-10-14 13:04:09 +02:00
/*
* View
*/
$form = new Form($db);
2009-10-14 13:04:09 +02:00
llxHeader();
$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup');
2009-10-14 13:04:09 +02:00
print '<br>';
$version = '';
$geoip = '';
2021-02-26 22:04:03 +01:00
if (!empty($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE)) {
$geoip = new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
2009-10-14 13:04:09 +02:00
}
// Mode
print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
print '<input type="hidden" name="token" value="'.newToken().'">';
2009-10-14 13:04:09 +02:00
print '<input type="hidden" name="action" value="set">';
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">';
2009-10-14 13:04:09 +02:00
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
print '<td class="right"><input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'"></td>';
2009-10-14 13:04:09 +02:00
print "</tr>\n";
// Lib version
2019-08-22 01:25:25 +02:00
print '<tr class="oddeven"><td width="50%">'.$langs->trans("GeoIPLibVersion").'</td>';
print '<td colspan="2">';
$arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
2021-02-26 22:04:03 +01:00
if ($conf->global->GEOIP_VERSION == 'php') {
if ($geoip) {
$version = $geoip->getVersion();
}
if ($version) {
print '<br>'.$langs->trans("Version").': '.$version;
}
}
print '</td></tr>';
// Path to database file
2019-08-22 01:25:25 +02:00
print '<tr class="oddeven"><td>'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
2009-10-14 13:04:09 +02:00
print '<td colspan="2">';
2013-07-09 00:19:58 +02:00
2021-02-26 22:04:03 +01:00
if ($conf->global->GEOIP_VERSION == 'php') {
2019-08-22 01:25:25 +02:00
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>';
2009-10-14 13:04:09 +02:00
}
2021-06-13 01:27:04 +02:00
print '<input type="text" class="minwidth200" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.dol_escape_htmltag($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE).'">';
2009-10-14 13:04:09 +02:00
print '</td></tr>';
print '</table>';
print "</form>\n";
print '<br>';
print $langs->trans("NoteOnPathLocation").'<br>';
$url1 = 'http://www.maxmind.com/en/city?rId=awstats';
2021-11-22 02:35:55 +01:00
print $langs->trans("YouCanDownloadFreeDatFileTo", '<a href="'.$url1.'" target="_blank" rel="noopener noreferrer external">'.$url1.'</a>');
2009-10-14 13:04:09 +02:00
print '<br>';
$url2 = 'http://www.maxmind.com/en/city?rId=awstats';
2021-11-22 02:35:55 +01:00
print $langs->trans("YouCanDownloadAdvancedDatFileTo", '<a href="'.$url2.'" target="_blank" rel="noopener noreferrer external">'.$url2.'</a>');
2009-10-14 13:04:09 +02:00
2021-02-26 22:04:03 +01:00
if ($geoip) {
2009-10-14 13:04:09 +02:00
print '<br><br>';
2020-09-24 21:09:58 +02:00
print '<br><span class="opacitymedium">'.$langs->trans("TestGeoIPResult", $ip).':</span>';
$ip = '24.24.24.24';
print '<br>'.$ip.' -> ';
$result = dol_print_ip($ip, 1);
2021-02-26 22:04:03 +01:00
if ($result) {
print $result;
} else {
print $langs->trans("Error");
}
2009-10-14 13:04:09 +02:00
$ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
2019-08-21 18:26:41 +02:00
print '<br>'.$ip.' -> ';
$result = dol_print_ip($ip, 1);
2021-02-26 22:04:03 +01:00
if ($result) {
print $result;
} else {
print $langs->trans("Error");
}
2019-08-21 18:26:41 +02:00
/* We disable this test because dol_print_ip need an ip as input
$ip='www.google.com';
print '<br>'.$ip.' -> ';
$result=dol_print_ip($ip,1);
if ($result) print $result;
else print $langs->trans("Error");
*/
2018-11-21 15:11:57 +01:00
//var_dump($_SERVER);
$ip = getUserRemoteIP();
2018-11-21 15:11:57 +01:00
//$ip='91.161.249.43';
$isip = is_ip($ip);
2021-02-26 22:04:03 +01:00
if ($isip == 1) {
2018-11-21 15:11:57 +01:00
print '<br>'.$ip.' -> ';
$result = dol_print_ip($ip, 1);
2021-02-26 22:04:03 +01:00
if ($result) {
print $result;
} else {
print $langs->trans("Error");
}
2020-05-21 09:35:30 +02:00
} else {
2018-11-21 15:11:57 +01:00
print '<br>'.$ip.' -> ';
$result = dol_print_ip($ip, 1);
2021-02-26 22:04:03 +01:00
if ($result) {
print $result;
} else {
print $langs->trans("NotAPublicIp");
}
2018-11-21 15:11:57 +01:00
}
2009-10-14 13:04:09 +02:00
$geoip->close();
}
2018-07-28 18:03:14 +02:00
// End of page
llxFooter();
$db->close();