api_hosts.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. function exec_ogp_module()
  25. {
  26. global $db;
  27. require_once 'includes/api_functions.php';
  28. $api_hosts_file = 'api_authorized.hosts';
  29. $api_fwd_hosts_file = 'api_authorized.fwd_hosts';
  30. echo "<h2>".get_lang('autohorized_hosts')."</h2>";
  31. if(isset($_POST['remove_hosts']) or isset($_POST['remove_fwd_hosts']))
  32. {
  33. if(isset($_POST['remove_hosts']))
  34. {
  35. $hosts_file = $api_hosts_file;
  36. $to_remove = $_POST['hosts_to_remove'];
  37. }
  38. else
  39. {
  40. $hosts_file = $api_fwd_hosts_file;
  41. $to_remove = $_POST['fwd_hosts_to_remove'];
  42. }
  43. if(file_exists($hosts_file))
  44. {
  45. $hosts_list = file_get_contents($hosts_file);
  46. $hosts = preg_split("/[\r\n]+/", $hosts_list);
  47. $new_hosts = array();
  48. foreach($hosts as $host)
  49. {
  50. $host = trim($host);
  51. if($host == '')
  52. continue;
  53. if(in_array($host, $to_remove))
  54. continue;
  55. $new_hosts[] = $host;
  56. }
  57. file_put_contents($hosts_file, implode("\n", $new_hosts));
  58. }
  59. }
  60. if(isset($_POST['add_host']) or isset($_POST['add_fwd_host']))
  61. {
  62. if(isset($_POST['add_host']))
  63. {
  64. $hosts_file = $api_hosts_file;
  65. $host_to_add = trim($_POST['host_to_add']);
  66. }
  67. else
  68. {
  69. $hosts_file = $api_fwd_hosts_file;
  70. $host_to_add = trim($_POST['fwd_host_to_add']);
  71. }
  72. $new_hosts = array();
  73. if(file_exists($hosts_file))
  74. {
  75. $hosts_list = file_get_contents($hosts_file);
  76. $hosts = preg_split("/[\r\n]+/", $hosts_list);
  77. foreach($hosts as $host)
  78. {
  79. $host = trim($host);
  80. if($host == '' or in_array($host, $new_hosts))
  81. continue;
  82. $new_hosts[] = $host;
  83. }
  84. }
  85. if(strstr($host_to_add, '/'))
  86. {
  87. list($ip, $range) = explode('/', $host_to_add, 2);
  88. if(is_valid_ipv4($ip) and !in_array($host_to_add, $new_hosts))
  89. $new_host = $host_to_add;
  90. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip)."/".$range, $new_hosts))
  91. $new_host = ipv6_compress($ip)."/".$range;
  92. }
  93. else
  94. {
  95. $ip = getHostByName($host_to_add);
  96. if(is_valid_ipv4($ip) and !in_array($ip, $new_hosts))
  97. $new_host = $ip;
  98. elseif(is_valid_ipv6($ip) and !in_array(ipv6_compress($ip), $new_hosts))
  99. $new_host = ipv6_compress($ip);
  100. }
  101. if(file_exists($hosts_file))
  102. {
  103. if(isset($new_host))
  104. $new_hosts[] = $new_host;
  105. file_put_contents($hosts_file, implode("\n", $new_hosts));
  106. }
  107. else
  108. {
  109. if(isset($new_host))
  110. file_put_contents($hosts_file, $new_host);
  111. }
  112. }
  113. $authorized_hosts = array();
  114. $ip = getHostByName(getHostName());
  115. if(filter_var($ip, FILTER_VALIDATE_IP))
  116. $authorized_hosts[] = $ip;
  117. $remote_servers = $db->getRemoteServers();
  118. foreach($remote_servers as $remote_server)
  119. {
  120. $ip = getHostByName($remote_server['agent_ip']);
  121. if(filter_var($ip, FILTER_VALIDATE_IP))
  122. if(!in_array($ip, $authorized_hosts))
  123. $authorized_hosts[] = $ip;
  124. }
  125. echo "<h4>".get_lang('default_trusted_hosts')."</h4>\n<br>\n<div align='center'>\n";
  126. foreach($authorized_hosts as $authorized_host)
  127. {
  128. echo $authorized_host."<br>\n";
  129. }
  130. echo "</div>\n<br>\n<form method=POST action='?m=settings&p=api_hosts'>\n<div align='center'>\n".
  131. "<h4>".get_lang('trusted_host_or_proxy_addresses_or_cidr')."</h4>\n<br>\n";
  132. if(file_exists($api_hosts_file))
  133. {
  134. $hosts_list = file_get_contents($api_hosts_file);
  135. $hosts = preg_split("/[\r\n]+/", $hosts_list);
  136. $hosts = array_filter($hosts);
  137. if(!empty($hosts))
  138. {
  139. foreach($hosts as $host)
  140. {
  141. $host = trim($host);
  142. if($host == '')
  143. continue;
  144. echo "<input type=checkbox id='$host' name='hosts_to_remove[]' value='$host' ><label for='$host'>$host</label><br>\n";
  145. }
  146. echo "<br><input type=submit name=remove_hosts value='".get_lang('remove')."'>\n<br>\n<br>\n";
  147. }
  148. }
  149. echo "<input type=text name='host_to_add' >\n".
  150. "<input type=submit name=add_host value='".get_lang('add')."'>\n".
  151. "</div>\n".
  152. "</form>\n".
  153. "<br>\n".
  154. "<br>\n";
  155. echo "<form method=POST action='?m=settings&p=api_hosts'>\n<div align='center'>\n".
  156. "<h4>".get_lang('trusted_forwarded_ip_addresses_or_cidr')."</h4>\n<br>\n";
  157. if(file_exists($api_fwd_hosts_file))
  158. {
  159. $fwd_hosts_list = file_get_contents($api_fwd_hosts_file);
  160. $fwd_hosts = preg_split("/[\r\n]+/", $fwd_hosts_list);
  161. $fwd_hosts = array_filter($fwd_hosts);
  162. if(!empty($fwd_hosts))
  163. {
  164. foreach($fwd_hosts as $fwd_host)
  165. {
  166. $fwd_host = trim($fwd_host);
  167. if($fwd_host == '')
  168. continue;
  169. echo "<input type=checkbox id='$fwd_host' name='fwd_hosts_to_remove[]' value='$fwd_host' ><label for='$fwd_host'>$fwd_host</label><br>\n";
  170. }
  171. echo "<br><input type=submit name=remove_fwd_hosts value='".get_lang('remove')."'>\n<br>\n<br>\n";
  172. }
  173. }
  174. echo "<input type=text name='fwd_host_to_add' >\n".
  175. "<input type=submit name=add_fwd_host value='".get_lang('add')."'>\n".
  176. "</div>\n".
  177. "</form>\n".
  178. "<br>\n".
  179. "<br>\n".
  180. "<a href='?m=settings'>".get_lang('back')."</a>";
  181. }