index.php 5.5 KB

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