2009-10-14 11:04:09 +00:00
< ? php
2025-08-27 01:10:23 +00:00
/* Copyright ( C ) 2009 - 2019 Laurent Destailleur < eldy @ users . sourceforge . org >
* Copyright ( C ) 2011 - 2013 Juanjo Menent < jmenent @ 2 byte . es >
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
* Copyright ( C ) 2024 - 2025 Frédéric France < frederic . france @ free . fr >
2011-09-21 13:24:27 +00:00
*
2009-10-14 11:04:09 +00: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 14:36:08 +00:00
* the Free Software Foundation ; either version 3 of the License , or
2009-10-14 11:04:09 +00: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 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2009-10-14 11:04:09 +00:00
*/
/**
* \file htdocs / admin / geoipmaxmind . php
* \ingroup geoipmaxmind
* \brief Setup page for geoipmaxmind module
*/
2022-09-07 18:08:59 +00:00
// Load Dolibarr environment
2012-08-22 21:24:21 +00:00
require '../main.inc.php' ;
2024-11-04 22:53:20 +00:00
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var HookManager $hookmanager
* @ var Translate $langs
* @ var User $user
*/
2025-12-28 01:19:01 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/class/dolgeoip.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php' ;
2024-11-04 22:53:20 +00:00
2009-10-14 11:04:09 +00:00
// Security check
2021-02-26 21:04:03 +00:00
if ( ! $user -> admin ) {
accessforbidden ();
}
2009-10-14 11:04:09 +00:00
2018-05-26 16:41:16 +00:00
// Load translation files required by the page
2020-04-10 08:59:32 +00:00
$langs -> loadLangs ( array ( " admin " , " errors " ));
2009-10-14 11:04:09 +00:00
2019-01-27 10:55:16 +00:00
$action = GETPOST ( 'action' , 'aZ09' );
2011-09-06 10:52:42 +00:00
2026-01-16 01:55:21 +00:00
if ( ! getDolGlobalString ( " GEOIP_VERSION " )) {
2025-12-28 01:19:01 +00:00
$conf -> global -> GEOIP_VERSION = '2' ;
}
2019-08-21 23:25:25 +00:00
2009-10-14 11:04:09 +00:00
/*
* Actions
*/
2019-08-21 23:25:25 +00:00
2021-02-26 21:04:03 +00:00
if ( $action == 'set' ) {
2020-04-10 08:59:32 +00:00
$error = 0 ;
2011-09-21 13:24:27 +00:00
2022-11-19 12:36:52 +00:00
$res1 = dolibarr_set_const ( $db , " GEOIP_VERSION " , GETPOST ( 'geoipversion' , 'aZ09' ), 'chaine' , 0 , '' , $conf -> entity );
if ( ! ( $res1 > 0 )) {
$error ++ ;
}
2025-12-28 01:19:01 +00:00
if ( getDolGlobalString ( 'GEOIP_VERSION' ) == 'php' ) {
$gimcdf = GETPOST ( " GEOIPMAXMIND_COUNTRY_DATAFILE " );
if ( $gimcdf ) {
if ( ! preg_match ( '/\.(dat|mmdb)$/' , $gimcdf )) {
setEventMessages ( $langs -> trans ( " ErrorFileMustHaveFormat " , '.dat|.mmdb' ), null , 'errors' );
$error ++ ;
}
if ( ! $error ) {
$res2 = dolibarr_set_const ( $db , " GEOIPMAXMIND_COUNTRY_DATAFILE " , $gimcdf , 'chaine' , 0 , '' , $conf -> entity );
if ( ! ( $res2 > 0 )) {
$error ++ ;
}
}
}
} else {
$gimcdf = GETPOST ( " GEOIPMAXMIND_COUNTRY_DATAFILE_EMBEDDED " );
if ( $gimcdf && ! preg_match ( '/\.(dat|mmdb)$/' , $gimcdf )) {
setEventMessages ( $langs -> trans ( " ErrorFileMustHaveFormat " , '.dat|.mmdb' ), null , 'errors' );
$error ++ ;
}
if ( ! $error ) {
$varname = 'GEOIPMAXMIND_COUNTRY_DATAFILE_EMBEDDED' ;
if ( isset ( $_FILES [ $varname ]) && $_FILES [ $varname ][ " name " ]) {
$diroffile = getMultidirOutput ( null , 'geoipmaxmind' );
if ( $diroffile ) {
$dirforterms = $diroffile . '/' ;
$original_file = $_FILES [ $varname ][ " name " ];
2026-01-19 13:55:37 +00:00
// @phan-suppress-next-line PhanTypeInvalidDimOffset
2025-12-28 01:19:01 +00:00
$result = dol_move_uploaded_file ( $_FILES [ $varname ][ " tmp_name " ], $dirforterms . $original_file , 1 , 0 , $_FILES [ $varname ][ 'error' ]);
if (( int ) $result > 0 ) {
dolibarr_set_const ( $db , $varname , $original_file , 'chaine' , 0 , '' , $conf -> entity );
} else {
$error ++ ;
setEventMessages ( $langs -> trans ( " Error " ) . ' ' . $langs -> transnoentitiesnoconv (( string ) $result ), null , 'errors' );
}
}
}
}
2009-10-14 11:04:09 +00:00
}
2011-09-21 13:24:27 +00:00
2021-02-26 21:04:03 +00:00
if ( ! $error ) {
2022-11-19 12:36:52 +00:00
setEventMessages ( $langs -> trans ( " SetupSaved " ), null , 'mesgs' );
} else {
//setEventMessages($langs->trans("Error"), null, 'errors');
2009-10-14 11:04:09 +00:00
}
}
2025-12-28 01:19:01 +00:00
$documenturl = getDolGlobalString ( 'DOL_URL_ROOT_DOCUMENT_PHP' , DOL_URL_ROOT . '/document.php' );
2019-08-21 17:38:45 +00:00
2009-10-14 11:04:09 +00:00
/*
* View
*/
2020-04-10 08:59:32 +00:00
$form = new Form ( $db );
2025-12-28 01:19:01 +00:00
$formfile = new FormFile ( $db );
2009-10-14 11:04:09 +00:00
2024-06-08 12:53:14 +00:00
llxHeader ( '' , '' , '' , '' , 0 , 0 , '' , '' , '' , 'mod-admin page-geoipmaxmind' );
2009-10-14 11:04:09 +00:00
2025-10-17 00:31:07 +00:00
$linkback = '<a href="' . dolBuildUrl ( DOL_URL_ROOT . '/admin/modules.php' , [ 'restore_lastsearch_values' => 1 ]) . '">' . img_picto ( $langs -> trans ( " BackToModuleList " ), 'back' , 'class="pictofixedwidth"' ) . '<span class="hideonsmartphone">' . $langs -> trans ( " BackToModuleList " ) . '</span></a>' ;
2025-08-09 18:44:30 +00:00
2019-01-27 10:55:16 +00:00
print load_fiche_titre ( $langs -> trans ( " GeoIPMaxmindSetup " ), $linkback , 'title_setup' );
2009-10-14 11:04:09 +00:00
print '<br>' ;
2020-04-10 08:59:32 +00:00
$version = '' ;
$geoip = '' ;
2025-12-28 01:19:01 +00:00
if ( getDolGlobalString ( 'GEOIP_VERSION' ) == 'php' ) {
$datafile = getDolGlobalString ( 'GEOIPMAXMIND_COUNTRY_DATAFILE' );
} else {
$diroffile = getMultidirOutput ( null , 'geoipmaxmind' );
$datafile = $diroffile . '/' . getDolGlobalString ( 'GEOIPMAXMIND_COUNTRY_DATAFILE_EMBEDDED' );
}
if ( $datafile ) {
$geoip = new DolGeoIP ( 'country' , $datafile );
2009-10-14 11:04:09 +00:00
}
// Mode
2025-12-28 01:19:01 +00:00
print '<form action="' . $_SERVER [ " PHP_SELF " ] . '" enctype="multipart/form-data" method="post">' ;
2019-12-18 22:12:31 +00:00
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
2009-10-14 11:04:09 +00:00
print '<input type="hidden" name="action" value="set">' ;
2025-12-27 23:07:18 +00:00
print '<div class="div-table-responsive-no-min">' ;
2019-11-05 20:24:41 +00:00
print '<table class="noborder centpercent">' ;
2009-10-14 11:04:09 +00:00
print '<tr class="liste_titre">' ;
2025-01-24 12:12:48 +00:00
print '<td>' . $langs -> trans ( " Parameter " ) . '</td><td></td>' ;
2025-12-28 01:19:01 +00:00
print '<td class="right"></td>' ;
2009-10-14 11:04:09 +00:00
print " </tr> \n " ;
2019-08-21 17:38:45 +00:00
// Lib version
2022-11-19 12:36:52 +00:00
print '<tr class="oddeven"><td>' . $langs -> trans ( " GeoIPLibVersion " ) . '</td>' ;
print '<td>' ;
2019-08-21 17:38:45 +00:00
$arrayofvalues = array ( 'php' => 'Native PHP functions' , '1' => 'Embedded GeoIP v1' , '2' => 'Embedded GeoIP v2' );
2025-08-27 01:10:23 +00:00
print $form -> selectarray ( 'geoipversion' , $arrayofvalues , getDolGlobalString ( 'GEOIP_VERSION' , '2' ));
2023-10-08 21:19:49 +00:00
if ( getDolGlobalString ( 'GEOIP_VERSION' ) == 'php' ) {
2021-02-26 21:04:03 +00:00
if ( $geoip ) {
$version = $geoip -> getVersion ();
}
if ( $version ) {
2019-08-21 17:38:45 +00:00
print '<br>' . $langs -> trans ( " Version " ) . ': ' . $version ;
}
}
2022-11-19 12:36:52 +00:00
print '</td>' ;
print '<td>' ;
2019-08-21 17:38:45 +00:00
print '</td></tr>' ;
// Path to database file
2019-08-21 23:25:25 +00:00
print '<tr class="oddeven"><td>' . $langs -> trans ( " PathToGeoIPMaxmindCountryDataFile " ) . '</td>' ;
2022-11-19 12:36:52 +00:00
print '<td>' ;
2023-10-08 21:19:49 +00:00
if ( getDolGlobalString ( 'GEOIP_VERSION' ) == 'php' ) {
2025-12-28 01:19:01 +00:00
$gimcdf = getDolGlobalString ( 'GEOIPMAXMIND_COUNTRY_DATAFILE' );
if ( function_exists ( 'geoip_db_filename' )) {
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>' ;
}
print '<input type="text" class="minwidth200" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="' . dol_escape_htmltag ( getDolGlobalString ( 'GEOIPMAXMIND_COUNTRY_DATAFILE' )) . '">' ;
if ( ! file_exists ( str_replace ( 'DOL_DATA_ROOT' , DOL_DATA_ROOT , $gimcdf ))) {
print '<div class="error">' . $langs -> trans ( " ErrorFileNotFound " , $gimcdf ) . '</div>' ;
}
} else {
$modulepart = 'geoipmaxmind' ;
print '<div class="inline-block nobordernopadding valignmiddle "><div class="inline-block marginrightonly">' ;
$maxfilesizearray = getMaxFileSizeArray ();
$maxmin = $maxfilesizearray [ 'maxmin' ];
if ( $maxmin > 0 ) {
print '<input type="hidden" name="MAX_FILE_SIZE" value="' . ( $maxmin * 1024 ) . '">' ; // MAX_FILE_SIZE must precede the field type=file
}
print '<input type="file" class="flat minwidth100 maxwidthinputfileonsmartphone" name="GEOIPMAXMIND_COUNTRY_DATAFILE_EMBEDDED" id="GEOIPMAXMIND_COUNTRY_DATAFILE_EMBEDDED">' ;
// TODO Move this into a function $out = getHelpOnUploadMax();
$out = '' ;
if ( getDolGlobalString ( 'MAIN_UPLOAD_DOC' )) {
$max = getDolGlobalString ( 'MAIN_UPLOAD_DOC' ); // In Kb
$maxphp = @ ini_get ( 'upload_max_filesize' ); // In unknown
if ( preg_match ( '/k$/i' , $maxphp )) {
$maxphp = preg_replace ( '/k$/i' , '' , $maxphp );
$maxphp = ( int ) $maxphp * 1 ;
}
if ( preg_match ( '/m$/i' , $maxphp )) {
$maxphp = preg_replace ( '/m$/i' , '' , $maxphp );
$maxphp = ( int ) $maxphp * 1024 ;
}
if ( preg_match ( '/g$/i' , $maxphp )) {
$maxphp = preg_replace ( '/g$/i' , '' , $maxphp );
$maxphp = ( int ) $maxphp * 1024 * 1024 ;
}
if ( preg_match ( '/t$/i' , $maxphp )) {
$maxphp = preg_replace ( '/t$/i' , '' , $maxphp );
$maxphp = ( int ) $maxphp * 1024 * 1024 * 1024 ;
}
$maxphp2 = @ ini_get ( 'post_max_size' ); // In unknown
if ( preg_match ( '/k$/i' , $maxphp2 )) {
$maxphp2 = preg_replace ( '/k$/i' , '' , $maxphp2 );
$maxphp2 = ( int ) $maxphp2 * 1 ;
}
if ( preg_match ( '/m$/i' , $maxphp2 )) {
$maxphp2 = preg_replace ( '/m$/i' , '' , $maxphp2 );
$maxphp2 = ( int ) $maxphp2 * 1024 ;
}
if ( preg_match ( '/g$/i' , $maxphp2 )) {
$maxphp2 = preg_replace ( '/g$/i' , '' , $maxphp2 );
$maxphp2 = ( int ) $maxphp2 * 1024 * 1024 ;
}
if ( preg_match ( '/t$/i' , $maxphp2 )) {
$maxphp2 = preg_replace ( '/t$/i' , '' , $maxphp2 );
$maxphp2 = ( int ) $maxphp2 * 1024 * 1024 * 1024 ;
}
// Now $max and $maxphp and $maxphp2 are in Kb
$maxmin = $max ;
$maxphptoshow = $maxphptoshowparam = '' ;
if ( $maxphp > 0 ) {
$maxmin = min ( $max , $maxphp );
$maxphptoshow = $maxphp ;
$maxphptoshowparam = 'upload_max_filesize' ;
}
if ( $maxphp2 > 0 ) {
$maxmin = min ( $max , $maxphp2 );
if ( $maxphp2 < $maxphp ) {
$maxphptoshow = $maxphp2 ;
$maxphptoshowparam = 'post_max_size' ;
}
}
$langs -> load ( 'other' );
$out .= ' ' ;
$out .= info_admin ( $langs -> trans ( " ThisLimitIsDefinedInSetup " , $max , $maxphptoshow ), 1 );
} else {
$out .= ' (' . $langs -> trans ( " UploadDisabled " ) . ')' ;
}
print $out ;
print '</div>' ;
if ( getDolGlobalString ( " GEOIPMAXMIND_COUNTRY_DATAFILE_EMBEDDED " )) {
$geoipfile = getDolGlobalString ( " GEOIPMAXMIND_COUNTRY_DATAFILE_EMBEDDED " );
$diroffile = getMultidirOutput ( null , 'geoipmaxmind' );
if ( file_exists ( $diroffile . '/' . $geoipfile )) {
$file = dol_dir_list ( $diroffile , 'files' , 0 , $geoipfile );
print ' ' ;
print '<div class="inline-block valignmiddle marginrightonly"><a href="' . $documenturl . '?modulepart=' . $modulepart . '&file=' . urlencode ( $geoipfile ) . '">' . $geoipfile . '</a>' . $formfile -> showPreview ( $file [ 0 ], $modulepart , $geoipfile , 0 , '' );
print '<div class="inline-block valignmiddle marginrightonly"><a class="reposition" href="' . $_SERVER [ " PHP_SELF " ] . '?action=removetermsofsale&modulepart=' . $modulepart . '&token=' . newToken () . '">' . img_delete ( $langs -> trans ( " Delete " ), '' , 'marginleftonly' ) . '</a></div>' ;
}
}
print '</div>' ;
2022-11-19 12:36:52 +00:00
}
2025-12-28 01:19:01 +00:00
2022-11-19 12:36:52 +00:00
print '</td><td>' ;
2025-12-28 01:19:01 +00:00
if ( getDolGlobalString ( 'GEOIP_VERSION' ) == 'php' ) {
print '<span class="opacitymedium">' ;
print $langs -> trans ( " Example " ) . '<br>' ;
print ' / usr / local / share / GeoIP / GeoIP . dat < br >
2022-11-19 12:36:52 +00:00
/ usr / share / GeoIP / GeoIP . dat < br >
/ usr / share / GeoIP / GeoLite2 - Country . mmdb ' ;
2025-12-28 01:19:01 +00:00
print '</span>' ;
}
2009-10-14 11:04:09 +00:00
print '</td></tr>' ;
print '</table>' ;
2025-12-27 23:07:18 +00:00
print '</div>' ;
2009-10-14 11:04:09 +00:00
2025-12-28 01:19:01 +00:00
print '<center>' ;
print '<input type="submit" class="button button-edit" value="' . $langs -> trans ( " Save " ) . '">' ;
print '</center>' ;
2009-10-14 11:04:09 +00:00
print " </form> \n " ;
print '<br>' ;
2025-12-28 01:19:01 +00:00
print '<div class="hideonsmartphone info">' ;
if ( getDolGlobalString ( 'GEOIP_VERSION' ) == 'php' ) {
print $langs -> trans ( " NoteOnPathLocation " ) . '<br>' ;
}
2009-10-14 11:04:09 +00:00
2020-04-10 08:59:32 +00:00
$url1 = 'http://www.maxmind.com/en/city?rId=awstats' ;
2022-11-19 12:36:52 +00: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 11:04:09 +00:00
print '<br>' ;
2020-04-10 08:59:32 +00:00
$url2 = 'http://www.maxmind.com/en/city?rId=awstats' ;
2022-11-19 12:36:52 +00: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 11:04:09 +00:00
2025-12-27 23:07:18 +00:00
print '</div>' ;
2021-02-26 21:04:03 +00:00
if ( $geoip ) {
2025-12-28 01:19:01 +00:00
print '<br>' ;
2025-12-27 23:07:18 +00:00
2023-03-06 09:49:38 +00:00
print '<form action="' . $_SERVER [ " PHP_SELF " ] . '" method="post">' ;
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
2024-09-18 01:27:25 +00:00
$ip = '24.24.24.24' ;
2025-12-27 23:07:18 +00:00
print load_fiche_titre ( $langs -> trans ( " TestGeoIPResult " , $ip ));
2012-01-15 01:39:43 +00:00
2025-12-27 23:07:18 +00:00
print $ip . ' -> ' ;
2020-04-10 08:59:32 +00:00
$result = dol_print_ip ( $ip , 1 );
2021-02-26 21:04:03 +00:00
if ( $result ) {
print $result ;
} else {
print $langs -> trans ( " Error " );
}
2009-10-14 11:04:09 +00:00
2020-04-10 08:59:32 +00:00
$ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a' ; // should be France
2019-08-21 16:26:41 +00:00
print '<br>' . $ip . ' -> ' ;
2020-04-10 08:59:32 +00:00
$result = dol_print_ip ( $ip , 1 );
2021-02-26 21:04:03 +00:00
if ( $result ) {
print $result ;
} else {
print $langs -> trans ( " Error " );
}
2019-08-21 16:26:41 +00:00
2012-01-15 01:39:43 +00: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 14:11:57 +00:00
//var_dump($_SERVER);
2018-11-21 14:40:15 +00:00
$ip = getUserRemoteIP ();
2018-11-21 14:11:57 +00:00
//$ip='91.161.249.43';
2020-04-10 08:59:32 +00:00
$isip = is_ip ( $ip );
2021-02-26 21:04:03 +00:00
if ( $isip == 1 ) {
2025-12-27 23:07:18 +00:00
print '<br>' . $langs -> trans ( " CurrentIP " ) . ': ' . $ip . ' -> ' ;
2020-04-10 08:59:32 +00:00
$result = dol_print_ip ( $ip , 1 );
2021-02-26 21:04:03 +00:00
if ( $result ) {
print $result ;
} else {
print $langs -> trans ( " Error " );
}
2020-05-21 07:35:30 +00:00
} else {
2025-12-28 01:19:01 +00:00
print '<br>' . ( $isip == 2 ? $langs -> trans ( " CurrentIP " ) . ': ' : '' ) . $ip . ' -> ' ;
2020-04-10 08:59:32 +00:00
$result = dol_print_ip ( $ip , 1 );
2021-02-26 21:04:03 +00:00
if ( $result ) {
print $result ;
} else {
print $langs -> trans ( " NotAPublicIp " );
}
2018-11-21 14:11:57 +00:00
}
2023-03-06 09:49:38 +00: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 11:04:09 +00:00
$geoip -> close ();
}
2018-07-28 16:03:14 +00:00
// End of page
2011-08-27 14:24:16 +00:00
llxFooter ();
2012-01-15 01:39:43 +00:00
$db -> close ();