index.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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(
  66. HESTIA_CMD .
  67. "v-add-user " .
  68. $v_username .
  69. " " .
  70. $v_password .
  71. " " .
  72. $v_email .
  73. " " .
  74. $v_package .
  75. " " .
  76. $v_name,
  77. $output,
  78. $return_var,
  79. );
  80. check_return_code($return_var, $output);
  81. unset($output);
  82. unlink($v_password);
  83. $v_password = quoteshellarg($_POST["v_password"]);
  84. }
  85. // Set language
  86. if (empty($_SESSION["error_msg"])) {
  87. exec(
  88. HESTIA_CMD . "v-change-user-language " . $v_username . " " . $v_language,
  89. $output,
  90. $return_var,
  91. );
  92. check_return_code($return_var, $output);
  93. unset($output);
  94. }
  95. // Set Role
  96. if (empty($_SESSION["error_msg"])) {
  97. $v_role = quoteshellarg($_POST["v_role"]);
  98. exec(
  99. HESTIA_CMD . "v-change-user-role " . $v_username . " " . $v_role,
  100. $output,
  101. $return_var,
  102. );
  103. check_return_code($return_var, $output);
  104. unset($output);
  105. }
  106. // Set login restriction
  107. if (empty($_SESSION["error_msg"])) {
  108. if (!empty($_POST["v_login_disabled"])) {
  109. $_POST["v_login_disabled"] = "yes";
  110. exec(
  111. HESTIA_CMD .
  112. "v-change-user-config-value " .
  113. $v_username .
  114. " LOGIN_DISABLED " .
  115. quoteshellarg($_POST["v_login_disabled"]),
  116. $output,
  117. $return_var,
  118. );
  119. check_return_code($return_var, $output);
  120. unset($output);
  121. }
  122. }
  123. // Send email to the new user
  124. if (empty($_SESSION["error_msg"]) && !empty($v_notify)) {
  125. $to = $_POST["v_notify"];
  126. // send email in "users" language
  127. putenv("LANGUAGE=" . $_POST["v_language"]);
  128. $subject = _("Welcome to Hestia Control Panel");
  129. $hostname = get_hostname();
  130. unset($output);
  131. $from = "noreply@" . $hostname;
  132. $from_name = _("Hestia Control Panel");
  133. if (!empty($_POST["v_name"])) {
  134. $mailtext = sprintf(_("GREETINGS_GORDON"), $_POST["v_name"]) . "\r\n";
  135. } else {
  136. $mailtext = _("GREETINGS") . "\r\n";
  137. }
  138. $mailtext .= sprintf(
  139. _("ACCOUNT_READY"),
  140. $_SERVER["HTTP_HOST"],
  141. $_POST["v_username"],
  142. $_POST["v_password"],
  143. );
  144. send_email($to, $subject, $mailtext, $from, $from_name, $_POST["name"]);
  145. putenv("LANGUAGE=" . detect_user_language());
  146. }
  147. // Flush field values on success
  148. if (empty($_SESSION["error_msg"])) {
  149. $_SESSION["ok_msg"] = sprintf(
  150. _("USER_CREATED_OK"),
  151. htmlentities($_POST["v_username"]),
  152. htmlentities($_POST["v_username"]),
  153. );
  154. $_SESSION["ok_msg"] .=
  155. " / <a href=/login/?loginas=" .
  156. htmlentities($_POST["v_username"]) .
  157. "&token=" .
  158. htmlentities($_SESSION["token"]) .
  159. ">" .
  160. _("login as") .
  161. " " .
  162. htmlentities($_POST["v_username"]) .
  163. "</a>";
  164. unset($v_username);
  165. unset($v_password);
  166. unset($v_email);
  167. unset($v_name);
  168. unset($v_notify);
  169. }
  170. }
  171. // List hosting packages
  172. exec(HESTIA_CMD . "v-list-user-packages json", $output, $return_var);
  173. check_error($return_var);
  174. $data = json_decode(implode("", $output), true);
  175. unset($output);
  176. // List languages
  177. exec(HESTIA_CMD . "v-list-sys-languages json", $output, $return_var);
  178. $language = json_decode(implode("", $output), true);
  179. foreach ($language as $lang) {
  180. $languages[$lang] = translate_json($lang);
  181. }
  182. asort($languages);
  183. if (empty($v_username)) {
  184. $v_username = "";
  185. }
  186. if (empty($v_name)) {
  187. $v_name = "";
  188. }
  189. if (empty($v_email)) {
  190. $v_email = "";
  191. }
  192. if (empty($v_password)) {
  193. $v_password = "";
  194. }
  195. if (empty($v_login_disabled)) {
  196. $v_login_disabled = "";
  197. }
  198. if (empty($v_role)) {
  199. $v_role = "";
  200. }
  201. if (empty($v_notify)) {
  202. $v_notify = "";
  203. }
  204. // Render page
  205. render_page($user, $TAB, "add_user");
  206. // Flush session messages
  207. unset($_SESSION["error_msg"]);
  208. unset($_SESSION["ok_msg"]);