index.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. error_reporting(NULL);
  3. ob_start();
  4. $TAB = 'IP';
  5. // Main include
  6. header('Content-Type: application/json');
  7. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  8. // Check user
  9. if ($_SESSION['user'] != 'admin') {
  10. exit;
  11. }
  12. // Check POST request
  13. if (!empty($_POST['ok'])) {
  14. // Check token
  15. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  16. exit();
  17. }
  18. // Check empty fields
  19. if (empty($_POST['v_ip'])) $errors[] = __('ip address');
  20. if (empty($_POST['v_netmask'])) $errors[] = __('netmask');
  21. if (empty($_POST['v_interface'])) $errors[] = __('interface');
  22. if (empty($_POST['v_owner'])) $errors[] = __('assigned user');
  23. if (!empty($errors[0])) {
  24. foreach ($errors as $i => $error) {
  25. if ( $i == 0 ) {
  26. $error_msg = $error;
  27. } else {
  28. $error_msg = $error_msg.", ".$error;
  29. }
  30. }
  31. $_SESSION['error_msg'] = __('Field "%s" can not be blank.',$error_msg);
  32. }
  33. // Protect input
  34. $v_ip = escapeshellarg($_POST['v_ip']);
  35. $v_netmask = escapeshellarg($_POST['v_netmask']);
  36. $v_name = escapeshellarg($_POST['v_name']);
  37. $v_nat = escapeshellarg($_POST['v_nat']);
  38. $v_interface = escapeshellarg($_POST['v_interface']);
  39. $v_owner = escapeshellarg($_POST['v_owner']);
  40. $v_shared = $_POST['v_shared'];
  41. // Check shared checkmark
  42. if ($v_shared == 'on') {
  43. $ip_status = 'shared';
  44. } else {
  45. $ip_status = 'dedicated';
  46. $v_dedicated = 'yes';
  47. }
  48. // Add IP
  49. if (empty($_SESSION['error_msg'])) {
  50. exec (VESTA_CMD."v-add-sys-ip ".$v_ip." ".$v_netmask." ".$v_interface." ".$v_owner." ".$ip_status." ".$v_name." ".$v_nat, $output, $return_var);
  51. check_return_code($return_var,$output);
  52. unset($output);
  53. $v_owner = $_POST['v_owner'];
  54. $v_interface = $_POST['v_interface'];
  55. }
  56. // Flush field values on success
  57. if (empty($_SESSION['error_msg'])) {
  58. $_SESSION['ok_msg'] = __('IP_CREATED_OK',htmlentities($_POST['v_ip']),htmlentities($_POST['v_ip']));
  59. unset($v_ip);
  60. unset($v_netmask);
  61. unset($v_name);
  62. unset($v_nat);
  63. }
  64. }
  65. // List network interfaces
  66. exec (VESTA_CMD."v-list-sys-interfaces json", $output, $return_var);
  67. $interfaces = json_decode(implode('', $output), true);
  68. unset($output);
  69. // List users
  70. exec (VESTA_CMD."v-list-sys-users json", $output, $return_var);
  71. $users = json_decode(implode('', $output), true);
  72. unset($output);
  73. $result = array(
  74. 'interfaces' => $interfaces,
  75. 'users' => $users,
  76. 'error_msg' => $_SESSION['error_msg'],
  77. 'ok_msg' => $_SESSION['ok_msg']
  78. );
  79. echo json_encode($result);
  80. // Flush session messages
  81. unset($_SESSION['error_msg']);
  82. unset($_SESSION['ok_msg']);