index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // Check ip argument
  13. if (empty($_GET["rule"])) {
  14. header("Location: /list/firewall/");
  15. exit();
  16. }
  17. // List rule
  18. $v_rule = quoteshellarg($_GET["rule"]);
  19. exec(HESTIA_CMD . "v-list-firewall-rule " . $v_rule . " 'json'", $output, $return_var);
  20. check_return_code_redirect($return_var, $output, "/list/firewall");
  21. $data = json_decode(implode("", $output), true);
  22. unset($output);
  23. // Parse rule
  24. $v_rule = $_GET["rule"];
  25. $v_action = $data[$v_rule]["ACTION"];
  26. $v_protocol = $data[$v_rule]["PROTOCOL"];
  27. $v_port = $data[$v_rule]["PORT"];
  28. $v_ip = $data[$v_rule]["IP"];
  29. $v_comment = $data[$v_rule]["COMMENT"];
  30. $v_date = $data[$v_rule]["DATE"];
  31. $v_time = $data[$v_rule]["TIME"];
  32. $v_suspended = $data[$v_rule]["SUSPENDED"];
  33. if ($v_suspended == "yes") {
  34. $v_status = "suspended";
  35. } else {
  36. $v_status = "active";
  37. }
  38. // Get ipset lists
  39. exec(HESTIA_CMD . "v-list-firewall-ipset 'json'", $output, $return_var);
  40. check_return_code($return_var, $output);
  41. $data = json_decode(implode("", $output), true);
  42. unset($output);
  43. $ipset_lists = [];
  44. foreach ($data as $key => $value) {
  45. if (isset($value["SUSPENDED"]) && $value["SUSPENDED"] === "yes") {
  46. continue;
  47. }
  48. if (isset($value["IP_VERSION"]) && $value["IP_VERSION"] !== "v4") {
  49. continue;
  50. }
  51. array_push($ipset_lists, ["name" => $key]);
  52. }
  53. $ipset_lists_json = json_encode($ipset_lists);
  54. // Check POST request
  55. if (!empty($_POST["save"])) {
  56. // Check token
  57. verify_csrf($_POST);
  58. // Check empty fields
  59. if (empty($_POST["v_action"])) {
  60. $errors[] = _("Action");
  61. }
  62. if (empty($_POST["v_protocol"])) {
  63. $errors[] = _("Protocol");
  64. }
  65. if (empty($_POST["v_port"]) && strlen($_POST["v_port"]) == 0) {
  66. $errors[] = _("Port");
  67. }
  68. if (empty($_POST["v_ip"])) {
  69. $errors[] = _("IP Address");
  70. }
  71. if (!empty($errors[0])) {
  72. foreach ($errors as $i => $error) {
  73. if ($i == 0) {
  74. $error_msg = $error;
  75. } else {
  76. $error_msg = $error_msg . ", " . $error;
  77. }
  78. }
  79. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  80. }
  81. if (empty($_SESSION["error_msg"])) {
  82. $v_rule = quoteshellarg($_GET["rule"]);
  83. $v_action = quoteshellarg($_POST["v_action"]);
  84. $v_protocol = quoteshellarg($_POST["v_protocol"]);
  85. $v_port = str_replace(" ", ",", $_POST["v_port"]);
  86. $v_port = preg_replace("/\,+/", ",", $v_port);
  87. $v_port = trim($v_port, ",");
  88. $v_port = quoteshellarg($v_port);
  89. $v_ip = quoteshellarg($_POST["v_ip"]);
  90. $v_comment = quoteshellarg($_POST["v_comment"]);
  91. // Change Status
  92. exec(
  93. HESTIA_CMD .
  94. "v-change-firewall-rule " .
  95. $v_rule .
  96. " " .
  97. $v_action .
  98. " " .
  99. $v_ip .
  100. " " .
  101. $v_port .
  102. " " .
  103. $v_protocol .
  104. " " .
  105. $v_comment,
  106. $output,
  107. $return_var,
  108. );
  109. check_return_code($return_var, $output);
  110. unset($output);
  111. $v_rule = $_GET["v_rule"];
  112. $v_action = $_POST["v_action"];
  113. $v_protocol = $_POST["v_protocol"];
  114. $v_port = str_replace(" ", ",", $_POST["v_port"]);
  115. $v_port = preg_replace("/\,+/", ",", $v_port);
  116. $v_port = trim($v_port, ",");
  117. $v_ip = $_POST["v_ip"];
  118. $v_comment = $_POST["v_comment"];
  119. // Set success message
  120. if (empty($_SESSION["error_msg"])) {
  121. $_SESSION["ok_msg"] = _("Changes have been saved.");
  122. }
  123. } else {
  124. $v_rule = $_GET["v_rule"];
  125. $v_action = $_POST["v_action"];
  126. $v_protocol = $_POST["v_protocol"];
  127. $v_port = str_replace(" ", ",", $_POST["v_port"]);
  128. $v_port = preg_replace("/\,+/", ",", $v_port);
  129. $v_port = trim($v_port, ",");
  130. $v_ip = $_POST["v_ip"];
  131. $v_comment = $_POST["v_comment"];
  132. }
  133. }
  134. // Render page
  135. render_page($user, $TAB, "edit_firewall");
  136. // Flush session messages
  137. unset($_SESSION["error_msg"]);
  138. unset($_SESSION["ok_msg"]);