geoipmaxmind.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * \file htdocs/admin/geoipmaxmind.php
  20. * \ingroup geoipmaxmind
  21. * \brief Setup page for geoipmaxmind module
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeoip.class.php';
  27. // Security check
  28. if (!$user->admin) {
  29. accessforbidden();
  30. }
  31. // Load translation files required by the page
  32. $langs->loadLangs(array("admin", "errors"));
  33. $action = GETPOST('action', 'aZ09');
  34. /*
  35. * Actions
  36. */
  37. if ($action == 'set') {
  38. $error = 0;
  39. $gimcdf = GETPOST("GEOIPMAXMIND_COUNTRY_DATAFILE");
  40. if (!$error && $gimcdf && !preg_match('/\.(dat|mmdb)$/', $gimcdf)) {
  41. setEventMessages($langs->trans("ErrorFileMustHaveFormat", '.dat|.mmdb'), null, 'errors');
  42. $error++;
  43. }
  44. if (!$error && $gimcdf && !file_exists($gimcdf)) {
  45. setEventMessages($langs->trans("ErrorFileNotFound", $gimcdf), null, 'errors');
  46. $error++;
  47. }
  48. if (!$error) {
  49. $res1 = dolibarr_set_const($db, "GEOIP_VERSION", GETPOST('geoipversion', 'aZ09'), 'chaine', 0, '', $conf->entity);
  50. if (!($res1 > 0)) {
  51. $error++;
  52. }
  53. $res2 = dolibarr_set_const($db, "GEOIPMAXMIND_COUNTRY_DATAFILE", $gimcdf, 'chaine', 0, '', $conf->entity);
  54. if (!($res2 > 0)) {
  55. $error++;
  56. }
  57. if (!$error) {
  58. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  59. } else {
  60. setEventMessages($langs->trans("Error"), null, 'errors');
  61. }
  62. }
  63. }
  64. if (!isset($conf->global->GEOIP_VERSION)) {
  65. $conf->global->GEOIP_VERSION = '2';
  66. }
  67. /*
  68. * View
  69. */
  70. $form = new Form($db);
  71. llxHeader();
  72. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  73. print load_fiche_titre($langs->trans("GeoIPMaxmindSetup"), $linkback, 'title_setup');
  74. print '<br>';
  75. $version = '';
  76. $geoip = '';
  77. if (!empty($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE)) {
  78. $geoip = new DolGeoIP('country', $conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE);
  79. }
  80. // Mode
  81. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  82. print '<input type="hidden" name="token" value="'.newToken().'">';
  83. print '<input type="hidden" name="action" value="set">';
  84. print '<table class="noborder centpercent">';
  85. print '<tr class="liste_titre">';
  86. print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
  87. print '<td class="right"><input type="submit" class="button button-edit" value="'.$langs->trans("Modify").'"></td>';
  88. print "</tr>\n";
  89. // Lib version
  90. print '<tr class="oddeven"><td width="50%">'.$langs->trans("GeoIPLibVersion").'</td>';
  91. print '<td colspan="2">';
  92. $arrayofvalues = array('php' => 'Native PHP functions', '1' => 'Embedded GeoIP v1', '2' => 'Embedded GeoIP v2');
  93. print $form->selectarray('geoipversion', $arrayofvalues, (isset($conf->global->GEOIP_VERSION) ? $conf->global->GEOIP_VERSION : '2'));
  94. if ($conf->global->GEOIP_VERSION == 'php') {
  95. if ($geoip) {
  96. $version = $geoip->getVersion();
  97. }
  98. if ($version) {
  99. print '<br>'.$langs->trans("Version").': '.$version;
  100. }
  101. }
  102. print '</td></tr>';
  103. // Path to database file
  104. print '<tr class="oddeven"><td>'.$langs->trans("PathToGeoIPMaxmindCountryDataFile").'</td>';
  105. print '<td colspan="2">';
  106. if ($conf->global->GEOIP_VERSION == 'php') {
  107. 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>';
  108. }
  109. print '<input type="text" class="minwidth200" name="GEOIPMAXMIND_COUNTRY_DATAFILE" value="'.dol_escape_htmltag($conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE).'">';
  110. print '</td></tr>';
  111. print '</table>';
  112. print "</form>\n";
  113. print '<br>';
  114. print $langs->trans("NoteOnPathLocation").'<br>';
  115. $url1 = 'http://www.maxmind.com/en/city?rId=awstats';
  116. print $langs->trans("YouCanDownloadFreeDatFileTo", '<a href="'.$url1.'" target="_blank" rel="noopener noreferrer external">'.$url1.'</a>');
  117. print '<br>';
  118. $url2 = 'http://www.maxmind.com/en/city?rId=awstats';
  119. print $langs->trans("YouCanDownloadAdvancedDatFileTo", '<a href="'.$url2.'" target="_blank" rel="noopener noreferrer external">'.$url2.'</a>');
  120. if ($geoip) {
  121. print '<br><br>';
  122. print '<br><span class="opacitymedium">'.$langs->trans("TestGeoIPResult", $ip).':</span>';
  123. $ip = '24.24.24.24';
  124. print '<br>'.$ip.' -> ';
  125. $result = dol_print_ip($ip, 1);
  126. if ($result) {
  127. print $result;
  128. } else {
  129. print $langs->trans("Error");
  130. }
  131. $ip = '2a01:e0a:7e:4a60:429a:23ff:f7b8:dc8a'; // should be France
  132. print '<br>'.$ip.' -> ';
  133. $result = dol_print_ip($ip, 1);
  134. if ($result) {
  135. print $result;
  136. } else {
  137. print $langs->trans("Error");
  138. }
  139. /* We disable this test because dol_print_ip need an ip as input
  140. $ip='www.google.com';
  141. print '<br>'.$ip.' -> ';
  142. $result=dol_print_ip($ip,1);
  143. if ($result) print $result;
  144. else print $langs->trans("Error");
  145. */
  146. //var_dump($_SERVER);
  147. $ip = getUserRemoteIP();
  148. //$ip='91.161.249.43';
  149. $isip = is_ip($ip);
  150. if ($isip == 1) {
  151. print '<br>'.$ip.' -> ';
  152. $result = dol_print_ip($ip, 1);
  153. if ($result) {
  154. print $result;
  155. } else {
  156. print $langs->trans("Error");
  157. }
  158. } else {
  159. print '<br>'.$ip.' -> ';
  160. $result = dol_print_ip($ip, 1);
  161. if ($result) {
  162. print $result;
  163. } else {
  164. print $langs->trans("NotAPublicIp");
  165. }
  166. }
  167. $geoip->close();
  168. }
  169. // End of page
  170. llxFooter();
  171. $db->close();