index.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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"])) {
  30. // Check token
  31. // Check token
  32. verify_csrf($_POST);
  33. // Check empty fields
  34. if (empty($_POST["v_action"])) {
  35. $errors[] = _("action");
  36. }
  37. if (empty($_POST["v_protocol"])) {
  38. $errors[] = _("protocol");
  39. }
  40. if (empty($_POST["v_port"]) && strlen($_POST["v_port"]) == 0) {
  41. $errors[] = _("port");
  42. }
  43. if (empty($_POST["v_ip"])) {
  44. $errors[] = _("ip address");
  45. }
  46. if (!empty($errors[0])) {
  47. foreach ($errors as $i => $error) {
  48. if ($i == 0) {
  49. $error_msg = $error;
  50. } else {
  51. $error_msg = $error_msg . ", " . $error;
  52. }
  53. }
  54. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  55. }
  56. // Protect input
  57. $v_action = quoteshellarg($_POST["v_action"]);
  58. $v_protocol = quoteshellarg($_POST["v_protocol"]);
  59. $v_port = str_replace(" ", ",", $_POST["v_port"]);
  60. $v_port = preg_replace("/\,+/", ",", $v_port);
  61. $v_port = trim($v_port, ",");
  62. $v_port = quoteshellarg($v_port);
  63. $v_ip = quoteshellarg($_POST["v_ip"]);
  64. $v_comment = quoteshellarg($_POST["v_comment"]);
  65. // Add firewall rule
  66. if (empty($_SESSION["error_msg"])) {
  67. exec(
  68. HESTIA_CMD .
  69. "v-add-firewall-rule " .
  70. $v_action .
  71. " " .
  72. $v_ip .
  73. " " .
  74. $v_port .
  75. " " .
  76. $v_protocol .
  77. " " .
  78. $v_comment,
  79. $output,
  80. $return_var,
  81. );
  82. check_return_code($return_var, $output);
  83. unset($output);
  84. }
  85. // Flush field values on success
  86. if (empty($_SESSION["error_msg"])) {
  87. $_SESSION["ok_msg"] = _("RULE_CREATED_OK");
  88. unset($v_port);
  89. unset($v_ip);
  90. unset($v_comment);
  91. }
  92. }
  93. if (empty($v_action)) {
  94. $v_action = "";
  95. }
  96. if (empty($v_protocol)) {
  97. $v_protocol = "";
  98. }
  99. if (empty($v_port)) {
  100. $v_port = "";
  101. }
  102. if (empty($v_ip)) {
  103. $v_ip = "";
  104. }
  105. if (empty($v_comment)) {
  106. $v_comment = "";
  107. }
  108. // Render
  109. render_page($user, $TAB, "add_firewall");
  110. // Flush session messages
  111. unset($_SESSION["error_msg"]);
  112. unset($_SESSION["ok_msg"]);