index.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. ob_start();
  4. $TAB = 'FIREWALL';
  5. // Main include
  6. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  7. // Check user
  8. if ($_SESSION['userContext'] != 'admin') {
  9. header("Location: /list/user");
  10. exit;
  11. }
  12. // Get ipset lists
  13. exec(HESTIA_CMD."v-list-firewall-ipset 'json'", $output, $return_var);
  14. check_return_code($return_var, $output);
  15. $data = json_decode(implode('', $output), true);
  16. unset($output);
  17. $ipset_lists=[];
  18. foreach ($data as $key => $value) {
  19. if (isset($value['SUSPENDED']) && $value['SUSPENDED'] === 'yes') {
  20. continue;
  21. }
  22. if (isset($value['IP_VERSION']) && $value['IP_VERSION'] !== 'v4') {
  23. continue;
  24. }
  25. array_push($ipset_lists, ['name'=>$key]);
  26. }
  27. $ipset_lists_json=json_encode($ipset_lists);
  28. // Check POST request
  29. if (!empty($_POST['ok'])) { // Check token
  30. // Check token
  31. verify_csrf($_POST);
  32. // Check empty fields
  33. if (empty($_POST['v_action'])) {
  34. $errors[] = _('action');
  35. }
  36. if (empty($_POST['v_protocol'])) {
  37. $errors[] = _('protocol');
  38. }
  39. if (empty($_POST['v_port']) && strlen($_POST['v_port']) == 0) {
  40. $errors[] = _('port');
  41. }
  42. if (empty($_POST['v_ip'])) {
  43. $errors[] = _('ip address');
  44. }
  45. if (!empty($errors[0])) {
  46. foreach ($errors as $i => $error) {
  47. if ($i == 0) {
  48. $error_msg = $error;
  49. } else {
  50. $error_msg = $error_msg.", ".$error;
  51. }
  52. }
  53. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  54. }
  55. // Protect input
  56. $v_action = quoteshellarg($_POST['v_action']);
  57. $v_protocol = quoteshellarg($_POST['v_protocol']);
  58. $v_port = str_replace(" ", ",", $_POST['v_port']);
  59. $v_port = preg_replace('/\,+/', ',', $v_port);
  60. $v_port = trim($v_port, ",");
  61. $v_port = quoteshellarg($v_port);
  62. $v_ip = quoteshellarg($_POST['v_ip']);
  63. $v_comment = quoteshellarg($_POST['v_comment']);
  64. // Add firewall rule
  65. if (empty($_SESSION['error_msg'])) {
  66. exec(HESTIA_CMD."v-add-firewall-rule ".$v_action." ".$v_ip." ".$v_port." ".$v_protocol." ".$v_comment, $output, $return_var);
  67. check_return_code($return_var, $output);
  68. unset($output);
  69. }
  70. // Flush field values on success
  71. if (empty($_SESSION['error_msg'])) {
  72. $_SESSION['ok_msg'] = _('RULE_CREATED_OK');
  73. unset($v_port);
  74. unset($v_ip);
  75. unset($v_comment);
  76. }
  77. }
  78. if (empty($v_action)) {
  79. $v_action = '';
  80. }
  81. if (empty($v_protocol)) {
  82. $v_protocol = '';
  83. }
  84. if (empty($v_port)) {
  85. $v_port = '';
  86. }
  87. if (empty($v_ip)) {
  88. $v_ip = '';
  89. }
  90. if (empty($v_comment)) {
  91. $v_comment = '';
  92. }
  93. // Render
  94. render_page($user, $TAB, 'add_firewall');
  95. // Flush session messages
  96. unset($_SESSION['error_msg']);
  97. unset($_SESSION['ok_msg']);