* Copyright (C) 2011-2013 Juanjo Menent * Copyright (C) 2024 MDW * Copyright (C) 2024-2025 Frédéric France * * 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 * (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 * along with this program. If not, see . */ /** * \file htdocs/admin/geoipmaxmind.php * \ingroup geoipmaxmind * \brief Setup page for geoipmaxmind module */ // Load Dolibarr environment require '../main.inc.php'; /** * @var Conf $conf * @var DoliDB $db * @var HookManager $hookmanager * @var Translate $langs * @var User $user */ 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'; // Security check if (!$user->admin) { accessforbidden(); } // Load translation files required by the page $langs->loadLangs(array("admin", "errors")); $action = GETPOST('action', 'aZ09'); if (!isset($conf->global->GEOIP_VERSION)) { $conf->global->GEOIP_VERSION = '2'; } /* * Actions */ if ($action == 'set') { $error = 0; $res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity); if (!($res1 > 0)) { $error++; } 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"]; $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'); } } } } } if (!$error) { setEventMessages($langs->trans("SetupSaved"), null, 'mesgs'); } else { //setEventMessages($langs->trans("Error"), null, 'errors'); } } $documenturl = getDolGlobalString('DOL_URL_ROOT_DOCUMENT_PHP', DOL_URL_ROOT.'/document.php'); /* * View */ $form = new Form($db); $formfile = new FormFile($db); llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-geoipmaxmind'); $linkback = ''.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').''.$langs->trans("BackToModuleList").''; print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup'); print '
'; $version = ''; $geoip = ''; 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); } // Mode print '
'; print ''; print ''; print '
'; print ''; print ''; print ''; print ''; print "\n"; // Lib version print ''; print ''; print ''; // Path to database file print ''; print ''; print '
'.$langs->trans("Parameter").'
'.$langs->trans("GeoIPLibVersion").''; $arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2'); print $form->selectarray('geoipversion', $arrayofvalues, getDolGlobalString('GEOIP_VERSION', '2')); if (getDolGlobalString('GEOIP_VERSION') == 'php') { if ($geoip) { $version = $geoip->getVersion(); } if ($version) { print '
'.$langs->trans("Version").': '.$version; } } print '
'; print '
'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").''; if (getDolGlobalString('GEOIP_VERSION') == 'php') { $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
'; } print ''; if (!file_exists(str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $gimcdf))) { print '
'.$langs->trans("ErrorFileNotFound", $gimcdf).'
'; } } else { $modulepart = 'geoipmaxmind'; print '
'; $maxfilesizearray = getMaxFileSizeArray(); $maxmin = $maxfilesizearray['maxmin']; if ($maxmin > 0) { print ''; // MAX_FILE_SIZE must precede the field type=file } print ''; // 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 '
'; 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 '
'.$geoipfile.''.$formfile->showPreview($file[0], $modulepart, $geoipfile, 0, ''); print ''; } } print '
'; } print '
'; if (getDolGlobalString('GEOIP_VERSION') == 'php') { print ''; print $langs->trans("Example").'
'; print '/usr/local/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoLite2-Country.mmdb'; print '
'; } print '
'; print '
'; print '
'; print ''; print '
'; print "
\n"; print '
'; print '
'; if (getDolGlobalString('GEOIP_VERSION') == 'php') { print $langs->trans("NoteOnPathLocation").'
'; } $url1 = 'http://www.maxmind.com/en/city?rId=awstats'; $textoshow = $langs->trans("YouCanDownloadFreeDatFileTo", '{s1}'); $textoshow = str_replace('{s1}', ''.$url1.'', $textoshow); print $textoshow; print '
'; $url2 = 'http://www.maxmind.com/en/city?rId=awstats'; $textoshow = $langs->trans("YouCanDownloadAdvancedDatFileTo", '{s1}'); $textoshow = str_replace('{s1}', ''.$url2.'', $textoshow); print $textoshow; print '
'; if ($geoip) { print '
'; print '
'; print ''; $ip = '24.24.24.24'; print load_fiche_titre($langs->trans("TestGeoIPResult", $ip)); print $ip.' -> '; $result = dol_print_ip($ip, 1); if ($result) { print $result; } else { print $langs->trans("Error"); } $ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France print '
'.$ip.' -> '; $result = dol_print_ip($ip, 1); if ($result) { print $result; } else { print $langs->trans("Error"); } /* We disable this test because dol_print_ip need an ip as input $ip='www.google.com'; print '
'.$ip.' -> '; $result=dol_print_ip($ip,1); if ($result) print $result; else print $langs->trans("Error"); */ //var_dump($_SERVER); $ip = getUserRemoteIP(); //$ip='91.161.249.43'; $isip = is_ip($ip); if ($isip == 1) { print '
'.$langs->trans("CurrentIP").': '.$ip.' -> '; $result = dol_print_ip($ip, 1); if ($result) { print $result; } else { print $langs->trans("Error"); } } else { print '
'.($isip == 2 ? $langs->trans("CurrentIP").': ' : '').$ip.' -> '; $result = dol_print_ip($ip, 1); if ($result) { print $result; } else { print $langs->trans("NotAPublicIp"); } } $ip = GETPOST("iptotest"); print '
'; print ''; if ($ip) { $result = dol_print_ip($ip, 1); if ($result) { print $result; } else { print $langs->trans("Error"); } } print '
'; $geoip->close(); } // End of page llxFooter(); $db->close();