index.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. ob_start();
  3. $TAB = 'FIREWALL';
  4. // Main include
  5. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  6. // Check user
  7. if ($_SESSION['userContext'] != 'admin') {
  8. header("Location: /list/user");
  9. exit;
  10. }
  11. // Get ipset lists
  12. exec(HESTIA_CMD."v-list-firewall-ipset 'json'", $output, $return_var);
  13. check_return_code($return_var, $output);
  14. $data = json_decode(implode('', $output), true);
  15. unset($output);
  16. $ipset_lists=[];
  17. foreach ($data as $key => $value) {
  18. if (isset($value['SUSPENDED']) && $value['SUSPENDED'] === 'yes') {
  19. continue;
  20. }
  21. if (isset($value['IP_VERSION']) && $value['IP_VERSION'] !== 'v4') {
  22. continue;
  23. }
  24. array_push($ipset_lists, ['name'=>$key]);
  25. }
  26. $ipset_lists_json=json_encode($ipset_lists);
  27. // Check POST request
  28. if (!empty($_POST['ok'])) { // Check token
  29. // Check token
  30. verify_csrf($_POST);
  31. // Check empty fields
  32. if (empty($_POST['v_action'])) {
  33. $errors[] = _('action');
  34. }
  35. if (empty($_POST['v_protocol'])) {
  36. $errors[] = _('protocol');
  37. }
  38. if (empty($_POST['v_port']) && strlen($_POST['v_port']) == 0) {
  39. $errors[] = _('port');
  40. }
  41. if (empty($_POST['v_ip'])) {
  42. $errors[] = _('ip address');
  43. }
  44. if (!empty($errors[0])) {
  45. foreach ($errors as $i => $error) {
  46. if ($i == 0) {
  47. $error_msg = $error;
  48. } else {
  49. $error_msg = $error_msg.", ".$error;
  50. }
  51. }
  52. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  53. }
  54. // Protect input
  55. $v_action = escapeshellarg($_POST['v_action']);
  56. $v_protocol = escapeshellarg($_POST['v_protocol']);
  57. $v_port = str_replace(" ", ",", $_POST['v_port']);
  58. $v_port = preg_replace('/\,+/', ',', $v_port);
  59. $v_port = trim($v_port, ",");
  60. $v_port = escapeshellarg($v_port);
  61. $v_ip = escapeshellarg($_POST['v_ip']);
  62. $v_comment = escapeshellarg($_POST['v_comment']);
  63. // Add firewall rule
  64. if (empty($_SESSION['error_msg'])) {
  65. exec(HESTIA_CMD."v-add-firewall-rule ".$v_action." ".$v_ip." ".$v_port." ".$v_protocol." ".$v_comment, $output, $return_var);
  66. check_return_code($return_var, $output);
  67. unset($output);
  68. }
  69. // Flush field values on success
  70. if (empty($_SESSION['error_msg'])) {
  71. $_SESSION['ok_msg'] = _('RULE_CREATED_OK');
  72. unset($v_port);
  73. unset($v_ip);
  74. unset($v_comment);
  75. }
  76. }
  77. if (empty($v_action)) {
  78. $v_action = '';
  79. }
  80. if (empty($v_protocol)) {
  81. $v_protocol = '';
  82. }
  83. if (empty($v_port)) {
  84. $v_port = '';
  85. }
  86. if (empty($v_ip)) {
  87. $v_ip = '';
  88. }
  89. if (empty($v_comment)) {
  90. $v_comment = '';
  91. }
  92. // Render
  93. render_page($user, $TAB, 'add_firewall');
  94. // Flush session messages
  95. unset($_SESSION['error_msg']);
  96. unset($_SESSION['ok_msg']);