index.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. error_reporting(null);
  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 = escapeshellarg($_GET['rule']);
  19. exec(HESTIA_CMD."v-list-firewall-rule ".$v_rule." 'json'", $output, $return_var);
  20. check_return_code($return_var, $output);
  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. $ipset_lists=[];
  43. foreach ($data as $key => $value) {
  44. if (isset($value['SUSPENDED']) && $value['SUSPENDED'] === 'yes') {
  45. continue;
  46. }
  47. if (isset($value['IP_VERSION']) && $value['IP_VERSION'] !== 'v4') {
  48. continue;
  49. }
  50. array_push($ipset_lists, ['name'=>$key]);
  51. }
  52. $ipset_lists_json=json_encode($ipset_lists);
  53. // Check POST request
  54. if (!empty($_POST['save'])) {
  55. // Check token
  56. verify_csrf($_POST);
  57. // Check empty fields
  58. if (empty($_POST['v_action'])) {
  59. $errors[] = _('action');
  60. }
  61. if (empty($_POST['v_protocol'])) {
  62. $errors[] = _('protocol');
  63. }
  64. if (empty($_POST['v_port']) && strlen($_POST['v_port']) == 0) {
  65. $errors[] = _('port');
  66. }
  67. if (empty($_POST['v_ip'])) {
  68. $errors[] = _('ip address');
  69. }
  70. if (!empty($errors[0])) {
  71. foreach ($errors as $i => $error) {
  72. if ($i == 0) {
  73. $error_msg = $error;
  74. } else {
  75. $error_msg = $error_msg.", ".$error;
  76. }
  77. }
  78. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  79. }
  80. if (empty($_SESSION['error_msg'])) {
  81. $v_rule = escapeshellarg($_GET['rule']);
  82. $v_action = escapeshellarg($_POST['v_action']);
  83. $v_protocol = escapeshellarg($_POST['v_protocol']);
  84. $v_port = str_replace(" ", ",", $_POST['v_port']);
  85. $v_port = preg_replace('/\,+/', ',', $v_port);
  86. $v_port = trim($v_port, ",");
  87. $v_port = escapeshellarg($v_port);
  88. $v_ip = escapeshellarg($_POST['v_ip']);
  89. $v_comment = escapeshellarg($_POST['v_comment']);
  90. // Change Status
  91. exec(HESTIA_CMD."v-change-firewall-rule ".$v_rule." ".$v_action." ".$v_ip." ".$v_port." ".$v_protocol." ".$v_comment, $output, $return_var);
  92. check_return_code($return_var, $output);
  93. unset($output);
  94. $v_rule = $_GET['v_rule'];
  95. $v_action = $_POST['v_action'];
  96. $v_protocol = $_POST['v_protocol'];
  97. $v_port = str_replace(" ", ",", $_POST['v_port']);
  98. $v_port = preg_replace('/\,+/', ',', $v_port);
  99. $v_port = trim($v_port, ",");
  100. $v_ip = $_POST['v_ip'];
  101. $v_comment = $_POST['v_comment'];
  102. // Set success message
  103. if (empty($_SESSION['error_msg'])) {
  104. $_SESSION['ok_msg'] = _('Changes has been saved.');
  105. }
  106. } else {
  107. $v_rule = $_GET['v_rule'];
  108. $v_action = $_POST['v_action'];
  109. $v_protocol = $_POST['v_protocol'];
  110. $v_port = str_replace(" ", ",", $_POST['v_port']);
  111. $v_port = preg_replace('/\,+/', ',', $v_port);
  112. $v_port = trim($v_port, ",");
  113. $v_ip = $_POST['v_ip'];
  114. $v_comment = $_POST['v_comment'];
  115. }
  116. }
  117. // Render page
  118. render_page($user, $TAB, 'edit_firewall');
  119. // Flush session messages
  120. unset($_SESSION['error_msg']);
  121. unset($_SESSION['ok_msg']);