index.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. ob_start();
  3. $TAB = 'USER';
  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. // Check POST request
  12. if (!empty($_POST['ok'])) {
  13. // Check token
  14. verify_csrf($_POST);
  15. // Check empty fields
  16. if (empty($_POST['v_username'])) {
  17. $errors[] = _('user');
  18. }
  19. if (empty($_POST['v_password'])) {
  20. $errors[] = _('password');
  21. }
  22. if (empty($_POST['v_package'])) {
  23. $errrors[] = _('package');
  24. }
  25. if (empty($_POST['v_email'])) {
  26. $errors[] = _('email');
  27. }
  28. if (empty($_POST['v_name'])) {
  29. $errors[] = _('name');
  30. }
  31. if (!empty($errors)) {
  32. foreach ($errors as $i => $error) {
  33. if ($i == 0) {
  34. $error_msg = $error;
  35. } else {
  36. $error_msg = $error_msg.", ".$error;
  37. }
  38. }
  39. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  40. }
  41. // Validate email
  42. if ((empty($_SESSION['error_msg'])) && (!filter_var($_POST['v_email'], FILTER_VALIDATE_EMAIL))) {
  43. $_SESSION['error_msg'] = _('Please enter valid email address.');
  44. }
  45. // Check password length
  46. if (empty($_SESSION['error_msg'])) {
  47. if (!validate_password($_POST['v_password'])) {
  48. $_SESSION['error_msg'] = _('Password does not match the minimum requirements');
  49. }
  50. }
  51. // Protect input
  52. $v_username = escapeshellarg($_POST['v_username']);
  53. $v_email = escapeshellarg($_POST['v_email']);
  54. $v_package = escapeshellarg($_POST['v_package']);
  55. $v_language = escapeshellarg($_POST['v_language']);
  56. $v_name = escapeshellarg($_POST['v_name']);
  57. $v_notify = $_POST['v_notify'];
  58. // Add user
  59. if (empty($_SESSION['error_msg'])) {
  60. $v_password = tempnam("/tmp", "vst");
  61. $fp = fopen($v_password, "w");
  62. fwrite($fp, $_POST['v_password']."\n");
  63. fclose($fp);
  64. exec(HESTIA_CMD."v-add-user ".$v_username." ".$v_password." ".$v_email." ".$v_package." ".$v_name, $output, $return_var);
  65. check_return_code($return_var, $output);
  66. unset($output);
  67. unlink($v_password);
  68. $v_password = escapeshellarg($_POST['v_password']);
  69. }
  70. // Set language
  71. if (empty($_SESSION['error_msg'])) {
  72. exec(HESTIA_CMD."v-change-user-language ".$v_username." ".$v_language, $output, $return_var);
  73. check_return_code($return_var, $output);
  74. unset($output);
  75. }
  76. // Set Role
  77. if (empty($_SESSION['error_msg'])) {
  78. $v_role = escapeshellarg($_POST['v_role']);
  79. exec(HESTIA_CMD."v-change-user-role ".$v_username." ".$v_role, $output, $return_var);
  80. check_return_code($return_var, $output);
  81. unset($output);
  82. }
  83. // Set login restriction
  84. if (empty($_SESSION['error_msg'])) {
  85. if (!empty($_POST['v_login_disabled'])) {
  86. $_POST['v_login_disabled'] = 'yes';
  87. exec(HESTIA_CMD."v-change-user-config-value ".$v_username." LOGIN_DISABLED ".escapeshellarg($_POST['v_login_disabled']), $output, $return_var);
  88. check_return_code($return_var, $output);
  89. unset($output);
  90. }
  91. }
  92. // Send email to the new user
  93. if ((empty($_SESSION['error_msg'])) && (!empty($v_notify))) {
  94. $to = $_POST['v_notify'];
  95. // send email in "users" language
  96. putenv("LANGUAGE=".$_POST['v_language']);
  97. $subject = _("Welcome to Hestia Control Panel");
  98. $hostname = get_hostname();
  99. unset($output);
  100. $from = "noreply@".$hostname;
  101. $from_name = _('Hestia Control Panel');
  102. if (!empty($_POST['v_name'])) {
  103. $mailtext = sprintf(_('GREETINGS_GORDON'), $_POST['v_name'])."\r\n";
  104. } else {
  105. $mailtext = _('GREETINGS')."\r\n";
  106. }
  107. $mailtext .= sprintf(_('ACCOUNT_READY'), $_SERVER['HTTP_HOST'], $_POST['v_username'], $_POST['v_password']);
  108. send_email($to, $subject, $mailtext, $from, $from_name, $_POST['name']);
  109. putenv("LANGUAGE=".detect_user_language());
  110. }
  111. // Flush field values on success
  112. if (empty($_SESSION['error_msg'])) {
  113. $_SESSION['ok_msg'] = sprintf(_('USER_CREATED_OK'), htmlentities($_POST['v_username']), htmlentities($_POST['v_username']));
  114. $_SESSION['ok_msg'] .= " / <a href=/login/?loginas=".htmlentities($_POST['v_username'])."&token=".htmlentities($_SESSION['token']).">" . _('login as') ." ".htmlentities($_POST['v_username']). "</a>";
  115. unset($v_username);
  116. unset($v_password);
  117. unset($v_email);
  118. unset($v_name);
  119. unset($v_notify);
  120. }
  121. }
  122. // List hosting packages
  123. exec(HESTIA_CMD."v-list-user-packages json", $output, $return_var);
  124. check_error($return_var);
  125. $data = json_decode(implode('', $output), true);
  126. unset($output);
  127. // List languages
  128. exec(HESTIA_CMD."v-list-sys-languages json", $output, $return_var);
  129. $language = json_decode(implode('', $output), true);
  130. foreach ($language as $lang) {
  131. $languages[$lang] = translate_json($lang);
  132. }
  133. asort($languages);
  134. if (empty($v_username)) {
  135. $v_username = '';
  136. }
  137. if (empty($v_name)) {
  138. $v_name = '';
  139. }
  140. if (empty($v_email)) {
  141. $v_email = '';
  142. }
  143. if (empty($v_password)) {
  144. $v_password = '';
  145. }
  146. if (empty($v_login_disabled)) {
  147. $v_login_disabled = '';
  148. }
  149. if (empty($v_role)) {
  150. $v_role = '';
  151. }
  152. if (empty($v_notify)) {
  153. $v_notify = '';
  154. }
  155. // Render page
  156. render_page($user, $TAB, 'add_user');
  157. // Flush session messages
  158. unset($_SESSION['error_msg']);
  159. unset($_SESSION['ok_msg']);