geoipmaxmind.php 7.0 KB

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