index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. error_reporting(null);
  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 = escapeshellarg($_POST['v_username']);
  54. $v_email = escapeshellarg($_POST['v_email']);
  55. $v_package = escapeshellarg($_POST['v_package']);
  56. $v_language = escapeshellarg($_POST['v_language']);
  57. $v_name = escapeshellarg($_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 = escapeshellarg($_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 = escapeshellarg($_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 ($_POST['v_login_disabled']) {
  87. if ($_POST['v_login_disabled'] == 'on') {
  88. $_POST['v_login_disabled'] = 'yes';
  89. } else {
  90. $_POST['v_login_disabled'] = 'no';
  91. }
  92. exec(HESTIA_CMD."v-change-user-config-value ".$v_username." LOGIN_DISABLED ".escapeshellarg($_POST['v_login_disabled']), $output, $return_var);
  93. check_return_code($return_var, $output);
  94. unset($output);
  95. }
  96. }
  97. // Send email to the new user
  98. if ((empty($_SESSION['error_msg'])) && (!empty($v_notify))) {
  99. $to = $_POST['v_notify'];
  100. // send email in "users" language
  101. putenv("LANGUAGE=".$_POST['v_language']);
  102. $subject = _("Welcome to Hestia Control Panel");
  103. $hostname = exec('hostname');
  104. unset($output);
  105. $from = "noreply@".$hostname;
  106. $from_name = _('Hestia Control Panel');
  107. if (!empty($_POST['v_name'])) {
  108. $mailtext = sprintf(_('GREETINGS_GORDON'), $_POST['v_name'])."\r\n";
  109. } else {
  110. $mailtext = _('GREETINGS')."\r\n";
  111. }
  112. $mailtext .= sprintf(_('ACCOUNT_READY'), $_SERVER['HTTP_HOST'], $_POST['v_username'], $_POST['v_password']);
  113. send_email($to, $subject, $mailtext, $from, $from_name, $_POST['name']);
  114. putenv("LANGUAGE=".detect_user_language());
  115. }
  116. // Flush field values on success
  117. if (empty($_SESSION['error_msg'])) {
  118. $_SESSION['ok_msg'] = sprintf(_('USER_CREATED_OK'), htmlentities($_POST['v_username']), htmlentities($_POST['v_username']));
  119. $_SESSION['ok_msg'] .= " / <a href=/login/?loginas=".htmlentities($_POST['v_username'])."&token=".htmlentities($_SESSION['token']).">" . _('login as') ." ".htmlentities($_POST['v_username']). "</a>";
  120. unset($v_username);
  121. unset($v_password);
  122. unset($v_email);
  123. unset($v_name);
  124. unset($v_notify);
  125. }
  126. }
  127. // List hosting packages
  128. exec(HESTIA_CMD."v-list-user-packages json", $output, $return_var);
  129. check_error($return_var);
  130. $data = json_decode(implode('', $output), true);
  131. unset($output);
  132. // List languages
  133. exec(HESTIA_CMD."v-list-sys-languages json", $output, $return_var);
  134. $language = json_decode(implode('', $output), true);
  135. foreach ($language as $lang) {
  136. $languages[$lang] = translate_json($lang);
  137. }
  138. asort($languages);
  139. // Render page
  140. render_page($user, $TAB, 'add_user');
  141. // Flush session messages
  142. unset($_SESSION['error_msg']);
  143. unset($_SESSION['ok_msg']);