index.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // Init
  3. error_reporting(NULL);
  4. ob_start();
  5. session_start();
  6. $TAB = 'USER';
  7. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  8. // Check user
  9. if ($_SESSION['user'] != 'admin') {
  10. header("Location: /list/user");
  11. exit;
  12. }
  13. // Check POST request
  14. if (!empty($_POST['ok'])) {
  15. // Check token
  16. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  17. header('location: /login/');
  18. exit();
  19. }
  20. // Check empty fields
  21. if (empty($_POST['v_username'])) $errors[] = __('user');
  22. if (empty($_POST['v_password'])) $errors[] = __('password');
  23. if (empty($_POST['v_package'])) $errrors[] = __('package');
  24. if (empty($_POST['v_email'])) $errors[] = __('email');
  25. if (empty($_POST['v_fname'])) $errors[] = __('first name');
  26. if (empty($_POST['v_lname'])) $errors[] = __('last name');
  27. if (!empty($errors[0])) {
  28. foreach ($errors as $i => $error) {
  29. if ( $i == 0 ) {
  30. $error_msg = $error;
  31. } else {
  32. $error_msg = $error_msg.", ".$error;
  33. }
  34. }
  35. $_SESSION['error_msg'] = __('Field "%s" can not be blank.',$error_msg);
  36. }
  37. // Validate email
  38. if ((empty($_SESSION['error_msg'])) && (!filter_var($_POST['v_email'], FILTER_VALIDATE_EMAIL))) {
  39. $_SESSION['error_msg'] = __('Please enter valid email address.');
  40. }
  41. // Check password length
  42. if (empty($_SESSION['error_msg'])) {
  43. $pw_len = strlen($_POST['v_password']);
  44. if ($pw_len < 6 ) $_SESSION['error_msg'] = __('Password is too short.',$error_msg);
  45. }
  46. // Protect input
  47. $v_username = escapeshellarg($_POST['v_username']);
  48. $v_email = escapeshellarg($_POST['v_email']);
  49. $v_package = escapeshellarg($_POST['v_package']);
  50. $v_language = escapeshellarg($_POST['v_language']);
  51. $v_fname = escapeshellarg($_POST['v_fname']);
  52. $v_lname = escapeshellarg($_POST['v_lname']);
  53. $v_notify = $_POST['v_notify'];
  54. // Add user
  55. if (empty($_SESSION['error_msg'])) {
  56. $v_password = tempnam("/tmp","vst");
  57. $fp = fopen($v_password, "w");
  58. fwrite($fp, $_POST['v_password']."\n");
  59. fclose($fp);
  60. exec (VESTA_CMD."v-add-user ".$v_username." ".$v_password." ".$v_email." ".$v_package." ".$v_fname." ".$v_lname, $output, $return_var);
  61. check_return_code($return_var,$output);
  62. unset($output);
  63. unlink($v_password);
  64. $v_password = escapeshellarg($_POST['v_password']);
  65. }
  66. // Set language
  67. if (empty($_SESSION['error_msg'])) {
  68. exec (VESTA_CMD."v-change-user-language ".$v_username." ".$v_language, $output, $return_var);
  69. check_return_code($return_var,$output);
  70. unset($output);
  71. }
  72. // Send email to the new user
  73. if ((empty($_SESSION['error_msg'])) && (!empty($v_notify))) {
  74. $to = $_POST['v_notify'];
  75. $subject = _translate($_POST['v_language'],"Welcome to Vesta Control Panel");
  76. $hostname = exec('hostname');
  77. unset($output);
  78. $from = _translate($_POST['v_language'],'MAIL_FROM',$hostname);
  79. if (!empty($_POST['v_fname'])) {
  80. $mailtext = _translate($_POST['v_language'],'GREETINGS_GORDON_FREEMAN',$_POST['v_fname'],$_POST['v_lname']);
  81. } else {
  82. $mailtext = _translate($_POST['v_language'],'GREETINGS');
  83. }
  84. $mailtext .= _translate($_POST['v_language'],'ACCOUNT_READY',$_SERVER['HTTP_HOST'],$_POST['v_username'],$_POST['v_password']);
  85. send_email($to, $subject, $mailtext, $from);
  86. }
  87. // Flush field values on success
  88. if (empty($_SESSION['error_msg'])) {
  89. $_SESSION['ok_msg'] = __('USER_CREATED_OK',htmlentities($_POST['v_username']),htmlentities($_POST['v_username']));
  90. $_SESSION['ok_msg'] .= " / <a href=/login/?loginas=".htmlentities($_POST['v_username']).">" . __('login as') ." ".htmlentities($_POST['v_username']). "</a>";
  91. unset($v_username);
  92. unset($v_password);
  93. unset($v_email);
  94. unset($v_fname);
  95. unset($v_lname);
  96. unset($v_notify);
  97. }
  98. }
  99. // Header
  100. include($_SERVER['DOCUMENT_ROOT'].'/templates/header.html');
  101. // Panel
  102. top_panel($user,$TAB);
  103. // List hosting packages
  104. exec (VESTA_CMD."v-list-user-packages json", $output, $return_var);
  105. check_error($return_var);
  106. $data = json_decode(implode('', $output), true);
  107. unset($output);
  108. // List languages
  109. exec (VESTA_CMD."v-list-sys-languages json", $output, $return_var);
  110. $languages = json_decode(implode('', $output), true);
  111. unset($output);
  112. // Display body
  113. include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_user.html');
  114. // Flush session messages
  115. unset($_SESSION['error_msg']);
  116. unset($_SESSION['ok_msg']);
  117. // Footer
  118. include($_SERVER['DOCUMENT_ROOT'].'/templates/footer.html');