index.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  57. header('location: /login/');
  58. exit();
  59. }
  60. // Check empty fields
  61. if (empty($_POST['v_action'])) $errors[] = _('action');
  62. if (empty($_POST['v_protocol'])) $errors[] = _('protocol');
  63. if (empty($_POST['v_port']) && strlen($_POST['v_port']) == 0) $errors[] = _('port');
  64. if (empty($_POST['v_ip'])) $errors[] = _('ip address');
  65. if (!empty($errors[0])) {
  66. foreach ($errors as $i => $error) {
  67. if ( $i == 0 ) {
  68. $error_msg = $error;
  69. } else {
  70. $error_msg = $error_msg.", ".$error;
  71. }
  72. }
  73. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  74. }
  75. if (!empty($_SESSION['error_msg'])) {
  76. $v_rule = escapeshellarg($_GET['rule']);
  77. $v_action = escapeshellarg($_POST['v_action']);
  78. $v_protocol = escapeshellarg($_POST['v_protocol']);
  79. $v_port = str_replace(" ",",", $_POST['v_port']);
  80. $v_port = preg_replace('/\,+/', ',', $v_port);
  81. $v_port = trim($v_port, ",");
  82. $v_port = escapeshellarg($v_port);
  83. $v_ip = escapeshellarg($_POST['v_ip']);
  84. $v_comment = escapeshellarg($_POST['v_comment']);
  85. // Change Status
  86. exec (HESTIA_CMD."v-change-firewall-rule ".$v_rule." ".$v_action." ".$v_ip." ".$v_port." ".$v_protocol." ".$v_comment, $output, $return_var);
  87. check_return_code($return_var,$output);
  88. unset($output);
  89. $v_rule = $_GET['v_rule'];
  90. $v_action = $_POST['v_action'];
  91. $v_protocol = $_POST['v_protocol'];
  92. $v_port = str_replace(" ",",", $_POST['v_port']);
  93. $v_port = preg_replace('/\,+/', ',', $v_port);
  94. $v_port = trim($v_port, ",");
  95. $v_ip = $_POST['v_ip'];
  96. $v_comment = $_POST['v_comment'];
  97. // Set success message
  98. if (empty($_SESSION['error_msg'])) {
  99. $_SESSION['ok_msg'] = _('Changes has been saved.');
  100. }
  101. }else{
  102. $v_rule = $_GET['v_rule'];
  103. $v_action = $_POST['v_action'];
  104. $v_protocol = $_POST['v_protocol'];
  105. $v_port = str_replace(" ",",", $_POST['v_port']);
  106. $v_port = preg_replace('/\,+/', ',', $v_port);
  107. $v_port = trim($v_port, ",");
  108. $v_ip = $_POST['v_ip'];
  109. $v_comment = $_POST['v_comment'];
  110. }
  111. }
  112. // Render page
  113. render_page($user, $TAB, 'edit_firewall');
  114. // Flush session messages
  115. unset($_SESSION['error_msg']);
  116. unset($_SESSION['ok_msg']);