index.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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[] = _("Username");
  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[] = _("Contact 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 a 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. $name = empty($_POST["v_name"]) ? $_POST["v_username"] : $_POST["v_name"];
  129. $template = get_email_template("account_ready", $v_language);
  130. if (!empty($template)) {
  131. preg_match("/<subject>(.*?)<\/subject>/si", $template, $matches);
  132. $subject = $matches[1];
  133. $subject = str_replace(
  134. ["{{hostname}}", "{{appname}}", "{{user}}", "{{name}}"],
  135. [get_hostname(), $_SESSION["APP_NAME"], $_POST["v_username"], $name],
  136. $subject,
  137. );
  138. $template = str_replace($matches[0], "", $template);
  139. } else {
  140. $template = _(
  141. "Hello {{name}},\n" .
  142. "\n" .
  143. "Your account has been created and ready to use.\n" .
  144. "\n" .
  145. "https://{{hostname}}/login/\n" .
  146. "Username: {{user}}\n" .
  147. "Password: {{password}}\n" .
  148. "\n" .
  149. "Best regards,\n" .
  150. "\n" .
  151. "--\n" .
  152. "{{appname}}",
  153. );
  154. }
  155. if (empty($subject)) {
  156. $subject = str_replace(
  157. ["{{subject}}", "{{hostname}}", "{{appname}}"],
  158. [
  159. sprintf(_("Welcome to %s"), $_SESSION["APP_NAME"]),
  160. get_hostname(),
  161. $_SESSION["APP_NAME"],
  162. ],
  163. $_SESSION["SUBJECT_EMAIL"],
  164. );
  165. }
  166. $hostname = get_hostname();
  167. $from = !empty($_SESSION["FROM_EMAIL"]) ? $_SESSION["FROM_EMAIL"] : "noreply@" . $hostname;
  168. $from_name = !empty($_SESSION["FROM_NAME"])
  169. ? $_SESSION["FROM_NAME"]
  170. : $_SESSION["APP_NAME"];
  171. if ($hostname) {
  172. $host = preg_replace("/(\[?[^]]*\]?):([0-9]{1,5})$/", "$1", $_SERVER["HTTP_HOST"]);
  173. if ($host == $hostname) {
  174. $port_is_defined = preg_match("/\[?[^]]*\]?:[0-9]{1,5}$/", $_SERVER["HTTP_HOST"]);
  175. if ($port_is_defined) {
  176. $port =
  177. ":" .
  178. preg_replace("/(\[?[^]]*\]?):([0-9]{1,5})$/", "$2", $_SERVER["HTTP_HOST"]);
  179. } else {
  180. $port = "";
  181. }
  182. } else {
  183. $port = ":" . $_SERVER["SERVER_PORT"];
  184. }
  185. $hostname = $hostname . $port;
  186. } else {
  187. $hostname = $_SERVER["HTTP_HOST"];
  188. }
  189. $mailtext = translate_email($template, [
  190. "name" => htmlentities($name),
  191. "user" => htmlentities($_POST["v_username"]),
  192. "password" => htmlentities($_POST["v_password"]),
  193. "hostname" => htmlentities($hostname),
  194. "appname" => $_SESSION["APP_NAME"],
  195. ]);
  196. send_email($to, $subject, $mailtext, $from, $from_name, $name);
  197. putenv("LANGUAGE=" . detect_user_language());
  198. }
  199. // Flush field values on success
  200. if (empty($_SESSION["error_msg"])) {
  201. $_SESSION["ok_msg"] = htmlify_trans(
  202. sprintf(
  203. _("User {%s} has been created successfully. / {Log in as %s}"),
  204. htmlentities($_POST["v_username"]),
  205. htmlentities($_POST["v_username"]),
  206. ),
  207. "</a>",
  208. '<a href="/edit/user/?user=' . htmlentities($_POST["v_username"]) . '">',
  209. '<a href="/login/?loginas=' .
  210. htmlentities($_POST["v_username"]) .
  211. "&token=" .
  212. htmlentities($_SESSION["token"]) .
  213. '">',
  214. );
  215. unset($v_username);
  216. unset($v_password);
  217. unset($v_email);
  218. unset($v_name);
  219. unset($v_notify);
  220. }
  221. }
  222. // List hosting packages
  223. exec(HESTIA_CMD . "v-list-user-packages json", $output, $return_var);
  224. check_error($return_var);
  225. $data = json_decode(implode("", $output), true);
  226. unset($output);
  227. // List languages
  228. exec(HESTIA_CMD . "v-list-sys-languages json", $output, $return_var);
  229. $language = json_decode(implode("", $output), true);
  230. foreach ($language as $lang) {
  231. $languages[$lang] = translate_json($lang);
  232. }
  233. asort($languages);
  234. if (empty($v_username)) {
  235. $v_username = "";
  236. }
  237. if (empty($v_name)) {
  238. $v_name = "";
  239. }
  240. if (empty($v_email)) {
  241. $v_email = "";
  242. }
  243. if (empty($v_password)) {
  244. $v_password = "";
  245. }
  246. if (empty($v_login_disabled)) {
  247. $v_login_disabled = "";
  248. }
  249. if (empty($v_role)) {
  250. $v_role = "";
  251. }
  252. if (empty($v_notify)) {
  253. $v_notify = "";
  254. }
  255. // Render page
  256. render_page($user, $TAB, "add_user");
  257. // Flush session messages
  258. unset($_SESSION["error_msg"]);
  259. unset($_SESSION["ok_msg"]);