index.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Get ipset lists
  13. exec(HESTIA_CMD."v-list-firewall-ipset 'json'", $output, $return_var);
  14. check_return_code($return_var, $output);
  15. $data = json_decode(implode('', $output), true);
  16. unset($output);
  17. $ipset_lists=[];
  18. foreach ($data as $key => $value) {
  19. if (isset($value['SUSPENDED']) && $value['SUSPENDED'] === 'yes') {
  20. continue;
  21. }
  22. if (isset($value['IP_VERSION']) && $value['IP_VERSION'] !== 'v4') {
  23. continue;
  24. }
  25. array_push($ipset_lists, ['name'=>$key]);
  26. }
  27. $ipset_lists_json=json_encode($ipset_lists);
  28. // Check POST request
  29. if (!empty($_POST['ok'])) { // Check token
  30. // Check token
  31. verify_csrf($_POST);
  32. // Check empty fields
  33. if (empty($_POST['v_action'])) {
  34. $errors[] = _('action');
  35. }
  36. if (empty($_POST['v_protocol'])) {
  37. $errors[] = _('protocol');
  38. }
  39. if (empty($_POST['v_port']) && strlen($_POST['v_port']) == 0) {
  40. $errors[] = _('port');
  41. }
  42. if (empty($_POST['v_ip'])) {
  43. $errors[] = _('ip address');
  44. }
  45. if (!empty($errors[0])) {
  46. foreach ($errors as $i => $error) {
  47. if ($i == 0) {
  48. $error_msg = $error;
  49. } else {
  50. $error_msg = $error_msg.", ".$error;
  51. }
  52. }
  53. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  54. }
  55. // Protect input
  56. $v_action = escapeshellarg($_POST['v_action']);
  57. $v_protocol = escapeshellarg($_POST['v_protocol']);
  58. $v_port = str_replace(" ", ",", $_POST['v_port']);
  59. $v_port = preg_replace('/\,+/', ',', $v_port);
  60. $v_port = trim($v_port, ",");
  61. $v_port = escapeshellarg($v_port);
  62. $v_ip = escapeshellarg($_POST['v_ip']);
  63. $v_comment = escapeshellarg($_POST['v_comment']);
  64. // Add firewall rule
  65. if (empty($_SESSION['error_msg'])) {
  66. exec(HESTIA_CMD."v-add-firewall-rule ".$v_action." ".$v_ip." ".$v_port." ".$v_protocol." ".$v_comment, $output, $return_var);
  67. check_return_code($return_var, $output);
  68. unset($output);
  69. }
  70. // Flush field values on success
  71. if (empty($_SESSION['error_msg'])) {
  72. $_SESSION['ok_msg'] = _('RULE_CREATED_OK');
  73. unset($v_port);
  74. unset($v_ip);
  75. unset($v_comment);
  76. }
  77. }
  78. // Render
  79. render_page($user, $TAB, 'add_firewall');
  80. // Flush session messages
  81. unset($_SESSION['error_msg']);
  82. unset($_SESSION['ok_msg']);