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 >
2013-08-14 12:48:00 +02:00
* Copyright ( C ) 2011 - 2013 Juanjo Menent < jmenent @ 2 byte . es >
2024-09-18 03:27:25 +02:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2024-11-04 23:53:20 +01:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
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
2013-01-16 15:36:08 +01:00
* 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
*/
2022-09-07 20:08:59 +02:00
// Load Dolibarr environment
2012-08-22 23:24:21 +02:00
require '../main.inc.php' ;
2012-08-22 23:11:24 +02:00
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' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/dolgeoip.class.php' ;
2009-10-14 13:04:09 +02:00
2024-11-04 23:53:20 +01:00
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var HookManager $hookmanager
* @ var Translate $langs
* @ var User $user
*/
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
2020-04-10 10:59:32 +02:00
$langs -> loadLangs ( array ( " admin " , " errors " ));
2009-10-14 13:04:09 +02:00
2019-01-27 11:55:16 +01: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' ) {
2020-04-10 10:59:32 +02:00
$error = 0 ;
2011-09-21 15:24:27 +02:00
2020-04-10 10:59:32 +02:00
$gimcdf = GETPOST ( " GEOIPMAXMIND_COUNTRY_DATAFILE " );
2011-09-21 15:24:27 +02:00
2020-10-31 14:32:18 +01: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 ++ ;
}
2022-11-19 13:36:52 +01:00
$res1 = dolibarr_set_const ( $db , " GEOIP_VERSION " , GETPOST ( 'geoipversion' , 'aZ09' ), 'chaine' , 0 , '' , $conf -> entity );
if ( ! ( $res1 > 0 )) {
$error ++ ;
}
$res2 = dolibarr_set_const ( $db , " GEOIPMAXMIND_COUNTRY_DATAFILE " , $gimcdf , 'chaine' , 0 , '' , $conf -> entity );
if ( ! ( $res2 > 0 )) {
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 ) {
2022-11-19 13:36:52 +01:00
setEventMessages ( $langs -> trans ( " SetupSaved " ), null , 'mesgs' );
} else {
//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' ;
}
2019-08-21 19:38:45 +02:00
2009-10-14 13:04:09 +02:00
/*
* View
*/
2020-04-10 10:59:32 +02:00
$form = new Form ( $db );
2009-10-14 13:04:09 +02:00
2024-06-08 14:53:14 +02:00
llxHeader ( '' , '' , '' , '' , 0 , 0 , '' , '' , '' , 'mod-admin page-geoipmaxmind' );
2009-10-14 13:04:09 +02:00
2020-04-10 10:59:32 +02:00
$linkback = '<a href="' . DOL_URL_ROOT . '/admin/modules.php?restore_lastsearch_values=1">' . $langs -> trans ( " BackToModuleList " ) . '</a>' ;
2019-01-27 11:55:16 +01:00
print load_fiche_titre ( $langs -> trans ( " GeoIPMaxmindSetup " ), $linkback , 'title_setup' );
2009-10-14 13:04:09 +02:00
print '<br>' ;
2020-04-10 10:59:32 +02:00
$version = '' ;
$geoip = '' ;
2023-10-24 17:00:13 +02:00
if ( getDolGlobalString ( 'GEOIPMAXMIND_COUNTRY_DATAFILE' )) {
2020-04-10 10:59:32 +02:00
$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">' ;
2019-12-18 23:12:31 +01:00
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">' ;
2025-01-24 13:12:48 +01:00
print '<td>' . $langs -> trans ( " Parameter " ) . '</td><td></td>' ;
2021-08-24 17:04:17 +02:00
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 " ;
2019-08-21 19:38:45 +02:00
// Lib version
2022-11-19 13:36:52 +01:00
print '<tr class="oddeven"><td>' . $langs -> trans ( " GeoIPLibVersion " ) . '</td>' ;
print '<td>' ;
2019-08-21 19:38:45 +02:00
$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' ));
2023-10-08 23:19:49 +02:00
if ( getDolGlobalString ( 'GEOIP_VERSION' ) == 'php' ) {
2021-02-26 22:04:03 +01:00
if ( $geoip ) {
$version = $geoip -> getVersion ();
}
if ( $version ) {
2019-08-21 19:38:45 +02:00
print '<br>' . $langs -> trans ( " Version " ) . ': ' . $version ;
}
}
2022-11-19 13:36:52 +01:00
print '</td>' ;
print '<td>' ;
2019-08-21 19:38:45 +02:00
print '</td></tr>' ;
2022-11-19 13:36:52 +01:00
$gimcdf = getDolGlobalString ( 'GEOIPMAXMIND_COUNTRY_DATAFILE' );
2019-08-21 19:38:45 +02:00
// Path to database file
2019-08-22 01:25:25 +02:00
print '<tr class="oddeven"><td>' . $langs -> trans ( " PathToGeoIPMaxmindCountryDataFile " ) . '</td>' ;
2022-11-19 13:36:52 +01:00
print '<td>' ;
2023-10-08 23:19:49 +02:00
if ( getDolGlobalString ( '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
}
2022-11-19 13:36:52 +01:00
print '<input type="text" class="minwidth200" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="' . dol_escape_htmltag ( getDolGlobalString ( 'GEOIPMAXMIND_COUNTRY_DATAFILE' )) . '">' ;
if ( ! file_exists ( $gimcdf )) {
print '<div class="error">' . $langs -> trans ( " ErrorFileNotFound " , $gimcdf ) . '</div>' ;
}
print '</td><td>' ;
print '<span class="opacitymedium">' ;
print $langs -> trans ( " Example " ) . '<br>' ;
print ' / usr / local / share / GeoIP / GeoIP . dat < br >
/ usr / share / GeoIP / GeoIP . dat < br >
/ usr / share / GeoIP / GeoLite2 - Country . mmdb ' ;
print '</span>' ;
2009-10-14 13:04:09 +02:00
print '</td></tr>' ;
print '</table>' ;
print " </form> \n " ;
print '<br>' ;
print $langs -> trans ( " NoteOnPathLocation " ) . '<br>' ;
2020-04-10 10:59:32 +02:00
$url1 = 'http://www.maxmind.com/en/city?rId=awstats' ;
2022-11-19 13:36:52 +01:00
$textoshow = $langs -> trans ( " YouCanDownloadFreeDatFileTo " , '{s1}' );
$textoshow = str_replace ( '{s1}' , '<a href="' . $url1 . '" target="_blank" rel="noopener noreferrer external">' . $url1 . '</a>' , $textoshow );
print $textoshow ;
2009-10-14 13:04:09 +02:00
print '<br>' ;
2020-04-10 10:59:32 +02:00
$url2 = 'http://www.maxmind.com/en/city?rId=awstats' ;
2022-11-19 13:36:52 +01:00
$textoshow = $langs -> trans ( " YouCanDownloadAdvancedDatFileTo " , '{s1}' );
$textoshow = str_replace ( '{s1}' , '<a href="' . $url2 . '" target="_blank" rel="noopener noreferrer external">' . $url2 . '</a>' , $textoshow );
print $textoshow ;
2009-10-14 13:04:09 +02:00
2021-02-26 22:04:03 +01:00
if ( $geoip ) {
2023-03-06 10:49:38 +01:00
print '<form action="' . $_SERVER [ " PHP_SELF " ] . '" method="post">' ;
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
2024-09-18 03:27:25 +02:00
$ip = '24.24.24.24' ;
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>' ;
2012-01-15 02:39:43 +01:00
print '<br>' . $ip . ' -> ' ;
2020-04-10 10:59:32 +02:00
$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
2020-04-10 10:59:32 +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 . ' -> ' ;
2020-04-10 10:59:32 +02:00
$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
2012-01-15 02:39:43 +01: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);
2018-11-21 15:40:15 +01:00
$ip = getUserRemoteIP ();
2018-11-21 15:11:57 +01:00
//$ip='91.161.249.43';
2020-04-10 10:59:32 +02:00
$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 . ' -> ' ;
2020-04-10 10:59:32 +02:00
$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 . ' -> ' ;
2020-04-10 10:59:32 +02:00
$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
}
2023-03-06 10:49:38 +01:00
$ip = GETPOST ( " iptotest " );
print '<br><input type="text class="width100" name="iptotest" id="iptotest" placeholder="' . dol_escape_htmltag ( $langs -> trans ( " EnterAnIP " )) . '" value="' . $ip . '">' ;
print '<input type="submit" class="width40 button small smallpaddingimp" value=" -> ">' ;
if ( $ip ) {
$result = dol_print_ip ( $ip , 1 );
if ( $result ) {
print $result ;
} else {
print $langs -> trans ( " Error " );
}
}
print '</form>' ;
2009-10-14 13:04:09 +02:00
$geoip -> close ();
}
2018-07-28 18:03:14 +02:00
// End of page
2011-08-27 16:24:16 +02:00
llxFooter ();
2012-01-15 02:39:43 +01:00
$db -> close ();