index.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(HESTIA_CMD."v-change-firewall-rule ".$v_rule." ".$v_action." ".$v_ip." ".$v_port." ".$v_protocol." ".$v_comment, $output, $return_var);
  93. check_return_code($return_var, $output);
  94. unset($output);
  95. $v_rule = $_GET['v_rule'];
  96. $v_action = $_POST['v_action'];
  97. $v_protocol = $_POST['v_protocol'];
  98. $v_port = str_replace(" ", ",", $_POST['v_port']);
  99. $v_port = preg_replace('/\,+/', ',', $v_port);
  100. $v_port = trim($v_port, ",");
  101. $v_ip = $_POST['v_ip'];
  102. $v_comment = $_POST['v_comment'];
  103. // Set success message
  104. if (empty($_SESSION['error_msg'])) {
  105. $_SESSION['ok_msg'] = _('Changes has been saved.');
  106. }
  107. } else {
  108. $v_rule = $_GET['v_rule'];
  109. $v_action = $_POST['v_action'];
  110. $v_protocol = $_POST['v_protocol'];
  111. $v_port = str_replace(" ", ",", $_POST['v_port']);
  112. $v_port = preg_replace('/\,+/', ',', $v_port);
  113. $v_port = trim($v_port, ",");
  114. $v_ip = $_POST['v_ip'];
  115. $v_comment = $_POST['v_comment'];
  116. }
  117. }
  118. // Render page
  119. render_page($user, $TAB, 'edit_firewall');
  120. // Flush session messages
  121. unset($_SESSION['error_msg']);
  122. unset($_SESSION['ok_msg']);