index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. ob_start();
  4. $TAB = "IP";
  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 POST request
  13. if (!empty($_POST["ok"])) {
  14. /// Check token
  15. verify_csrf($_POST);
  16. // Check empty fields
  17. if (empty($_POST["v_ip"])) {
  18. $errors[] = _("ip address");
  19. }
  20. if (empty($_POST["v_netmask"])) {
  21. $errors[] = _("netmask");
  22. }
  23. if (empty($_POST["v_interface"])) {
  24. $errors[] = _("interface");
  25. }
  26. if (empty($_POST["v_owner"])) {
  27. $errors[] = _("assigned user");
  28. }
  29. if (!empty($errors[0])) {
  30. foreach ($errors as $i => $error) {
  31. if ($i == 0) {
  32. $error_msg = $error;
  33. } else {
  34. $error_msg = $error_msg . ", " . $error;
  35. }
  36. }
  37. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  38. }
  39. // Protect input
  40. $v_ip = quoteshellarg($_POST["v_ip"]);
  41. $v_netmask = quoteshellarg($_POST["v_netmask"]);
  42. $v_name = quoteshellarg($_POST["v_name"]);
  43. $v_nat = quoteshellarg($_POST["v_nat"]);
  44. $v_interface = quoteshellarg($_POST["v_interface"]);
  45. $v_owner = quoteshellarg($_POST["v_owner"]);
  46. $v_shared = $_POST["v_shared"];
  47. if (filter_var($_POST["v_ip"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  48. $v_ipv6_suffix='v6';
  49. } else {
  50. $v_ipv6_suffix='';
  51. }
  52. // Check shared checkmark
  53. if ($v_shared == "on") {
  54. $ip_status = "shared";
  55. } else {
  56. $ip_status = "dedicated";
  57. $v_dedicated = "yes";
  58. }
  59. // Add IP
  60. if (empty($_SESSION["error_msg"])) {
  61. exec(
  62. HESTIA_CMD .
  63. "v-add-sys-ip".$v_ipv6_suffix." " .
  64. $v_ip .
  65. " " .
  66. $v_netmask .
  67. " " .
  68. $v_interface .
  69. " " .
  70. $v_owner .
  71. " " .
  72. quoteshellarg($ip_status) .
  73. " " .
  74. $v_name .
  75. " " .
  76. $v_nat,
  77. $output,
  78. $return_var,
  79. );
  80. check_return_code($return_var, $output);
  81. unset($output);
  82. $v_owner = $_POST["v_owner"];
  83. $v_interface = $_POST["v_interface"];
  84. }
  85. // Flush field values on success
  86. if (empty($_SESSION["error_msg"])) {
  87. $_SESSION["ok_msg"] = sprintf(
  88. _("IP_CREATED_OK"),
  89. htmlentities($_POST["v_ip"]),
  90. htmlentities($_POST["v_ip"]),
  91. );
  92. unset($v_ip);
  93. unset($v_netmask);
  94. unset($v_name);
  95. unset($v_nat);
  96. }
  97. }
  98. // List network interfaces
  99. exec(HESTIA_CMD . "v-list-sys-interfaces 'json'", $output, $return_var);
  100. $interfaces = json_decode(implode("", $output), true);
  101. unset($output);
  102. // List users
  103. exec(HESTIA_CMD . "v-list-sys-users 'json'", $output, $return_var);
  104. $users = json_decode(implode("", $output), true);
  105. unset($output);
  106. if (empty($v_ip)) {
  107. $v_ip = "";
  108. }
  109. if (empty($v_netmask)) {
  110. $v_netmask = "";
  111. }
  112. if (empty($v_name)) {
  113. $v_name = "";
  114. }
  115. if (empty($v_nat)) {
  116. $v_nat = "";
  117. }
  118. if (empty($v_interface)) {
  119. $v_interface = "";
  120. }
  121. if (empty($ip_status)) {
  122. $ip_status = "";
  123. }
  124. if (empty($v_owner)) {
  125. $v_owner = "";
  126. }
  127. // Render
  128. render_page($user, $TAB, "add_ip");
  129. // Flush session messages
  130. unset($_SESSION["error_msg"]);
  131. unset($_SESSION["ok_msg"]);