index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. error_reporting(NULL);
  3. ob_start();
  4. $TAB = 'FIREWALL';
  5. header('Content-Type: application/json');
  6. // Main include
  7. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  8. // Check user
  9. if ($_SESSION['user'] != 'admin') {
  10. exit;
  11. }
  12. // Check ip argument
  13. if (empty($_GET['rule'])) {
  14. exit;
  15. }
  16. // List rule
  17. $v_rule = escapeshellarg($_GET['rule']);
  18. exec (VESTA_CMD."v-list-firewall-rule ".$v_rule." json", $output, $return_var);
  19. check_return_code($return_var,$output);
  20. $data = json_decode(implode('', $output), true);
  21. unset($output);
  22. // Parse rule
  23. $v_rule = $_GET['rule'];
  24. $v_action = $data[$v_rule]['ACTION'];
  25. $v_protocol = $data[$v_rule]['PROTOCOL'];
  26. $v_port = $data[$v_rule]['PORT'];
  27. $v_ip = $data[$v_rule]['IP'];
  28. $v_comment = $data[$v_rule]['COMMENT'];
  29. $v_date = $data[$v_rule]['DATE'];
  30. $v_time = $data[$v_rule]['TIME'];
  31. $v_suspended = $data[$v_rule]['SUSPENDED'];
  32. if ( $v_suspended == 'yes' ) {
  33. $v_status = 'suspended';
  34. } else {
  35. $v_status = 'active';
  36. }
  37. // Check POST request
  38. if (!empty($_POST['save'])) {
  39. // Check token
  40. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  41. exit();
  42. }
  43. $v_rule = escapeshellarg($_GET['rule']);
  44. $v_action = escapeshellarg($_POST['v_action']);
  45. $v_protocol = escapeshellarg($_POST['v_protocol']);
  46. $v_port = str_replace(" ",",", $_POST['v_port']);
  47. $v_port = preg_replace('/\,+/', ',', $v_port);
  48. $v_port = trim($v_port, ",");
  49. $v_port = escapeshellarg($v_port);
  50. $v_ip = escapeshellarg($_POST['v_ip']);
  51. $v_comment = escapeshellarg($_POST['v_comment']);
  52. // Change Status
  53. exec (VESTA_CMD."v-change-firewall-rule ".$v_rule." ".$v_action." ".$v_ip." ".$v_port." ".$v_protocol." ".$v_comment, $output, $return_var);
  54. check_return_code($return_var,$output);
  55. unset($output);
  56. $v_rule = $_GET['v_rule'];
  57. $v_action = $_POST['v_action'];
  58. $v_protocol = $_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_ip = $_POST['v_ip'];
  63. $v_comment = $_POST['v_comment'];
  64. // Set success message
  65. if (empty($_SESSION['error_msg'])) {
  66. $_SESSION['ok_msg'] = __('Changes has been saved.');
  67. }
  68. }
  69. $result = array(
  70. 'rule' => $_GET['rule'],
  71. 'action' => $data[$v_rule]['ACTION'],
  72. 'protocol' => $data[$v_rule]['PROTOCOL'],
  73. 'port' => $data[$v_rule]['PORT'],
  74. 'ip' => $data[$v_rule]['IP'],
  75. 'comment' => $data[$v_rule]['COMMENT'],
  76. 'date' => $data[$v_rule]['DATE'],
  77. 'time' => $data[$v_rule]['TIME'],
  78. 'suspended' => $data[$v_rule]['SUSPENDED'],
  79. 'status' => $v_status,
  80. 'actions' => [ __('DROP'), __('ACCEPT') ],
  81. 'protocols' => [ __('TCP'), __('UDP'), __('ICMP') ],
  82. 'error_msg' => $_SESSION['error_msg'],
  83. 'ok_msg' => $_SESSION['ok_msg']
  84. );
  85. echo json_encode($result);
  86. // Flush session messages
  87. unset($_SESSION['error_msg']);
  88. unset($_SESSION['ok_msg']);