index.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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['user'] != 'admin') {
  9. header("Location: /list/user");
  10. exit;
  11. }
  12. // Check POST request
  13. if (!empty($_POST['ok'])) {
  14. // Check token
  15. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  16. header('location: /login/');
  17. exit();
  18. }
  19. // Check empty fields
  20. if (empty($_POST['v_username'])) $errors[] = _('user');
  21. if (empty($_POST['v_password'])) $errors[] = _('password');
  22. if (empty($_POST['v_package'])) $errrors[] = _('package');
  23. if (empty($_POST['v_email'])) $errors[] = _('email');
  24. if (empty($_POST['v_name'])) $errors[] = _('name');
  25. if (!empty($errors)) {
  26. foreach ($errors as $i => $error) {
  27. if ( $i == 0 ) {
  28. $error_msg = $error;
  29. } else {
  30. $error_msg = $error_msg.", ".$error;
  31. }
  32. }
  33. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  34. }
  35. // Validate email
  36. if ((empty($_SESSION['error_msg'])) && (!filter_var($_POST['v_email'], FILTER_VALIDATE_EMAIL))) {
  37. $_SESSION['error_msg'] = _('Please enter valid email address.');
  38. }
  39. // Check password length
  40. if (empty($_SESSION['error_msg'])) {
  41. if (!validate_password($_POST['v_password'])) { $_SESSION['error_msg'] = _('Password does not match the minimum requirements'); }
  42. }
  43. // Protect input
  44. $v_username = escapeshellarg($_POST['v_username']);
  45. $v_email = escapeshellarg($_POST['v_email']);
  46. $v_package = escapeshellarg($_POST['v_package']);
  47. $v_language = escapeshellarg($_POST['v_language']);
  48. $v_name = escapeshellarg($_POST['v_name']);
  49. $v_notify = $_POST['v_notify'];
  50. // Add user
  51. if (empty($_SESSION['error_msg'])) {
  52. $v_password = tempnam("/tmp","vst");
  53. $fp = fopen($v_password, "w");
  54. fwrite($fp, $_POST['v_password']."\n");
  55. fclose($fp);
  56. exec (HESTIA_CMD."v-add-user ".$v_username." ".$v_password." ".$v_email." ".$v_package." ".$v_name, $output, $return_var);
  57. check_return_code($return_var,$output);
  58. unset($output);
  59. unlink($v_password);
  60. $v_password = escapeshellarg($_POST['v_password']);
  61. }
  62. // Set language
  63. if (empty($_SESSION['error_msg'])) {
  64. exec (HESTIA_CMD."v-change-user-language ".$v_username." ".$v_language, $output, $return_var);
  65. check_return_code($return_var,$output);
  66. unset($output);
  67. }
  68. // Set Role
  69. if (empty($_SESSION['error_msg'])) {
  70. $v_role = escapeshellarg($_POST['v_role']);
  71. exec (HESTIA_CMD."v-change-user-role ".$v_username." ".$v_role, $output, $return_var);
  72. check_return_code($return_var,$output);
  73. unset($output);
  74. }
  75. // Send email to the new user
  76. if ((empty($_SESSION['error_msg'])) && (!empty($v_notify))) {
  77. $to = $_POST['v_notify'];
  78. $subject = _("Welcome to Hestia Control Panel"); //currently not supported to use the account language
  79. $hostname = exec('hostname');
  80. unset($output);
  81. $from = sprintf(_('MAIL_FROM'),$hostname); //currently not supported to use the account language
  82. if (!empty($_POST['v_name'])) {
  83. $mailtext = sprintf(_('GREETINGS_GORDON'),$_POST['v_name'])."\r\n";
  84. } else {
  85. $mailtext = _('GREETINGS')."\r\n";
  86. }
  87. $mailtext .= sprintf(_('ACCOUNT_READY'),$_SERVER['HTTP_HOST'],$_POST['v_username'],$_POST['v_password']);
  88. send_email($to, $subject, $mailtext, $from);
  89. }
  90. // Flush field values on success
  91. if (empty($_SESSION['error_msg'])) {
  92. $_SESSION['ok_msg'] = sprintf(_('USER_CREATED_OK'),htmlentities($_POST['v_username']),htmlentities($_POST['v_username']));
  93. $_SESSION['ok_msg'] .= " / <a href=/login/?loginas=".htmlentities($_POST['v_username']).">" . _('login as') ." ".htmlentities($_POST['v_username']). "</a>";
  94. unset($v_username);
  95. unset($v_password);
  96. unset($v_email);
  97. unset($v_name);
  98. unset($v_notify);
  99. }
  100. }
  101. // List hosting packages
  102. exec (HESTIA_CMD."v-list-user-packages json", $output, $return_var);
  103. check_error($return_var);
  104. $data = json_decode(implode('', $output), true);
  105. unset($output);
  106. // List languages
  107. exec (HESTIA_CMD."v-list-sys-languages json", $output, $return_var);
  108. $languages = json_decode(implode('', $output), true);
  109. unset($output);
  110. // Render page
  111. render_page($user, $TAB, 'add_user');
  112. // Flush session messages
  113. unset($_SESSION['error_msg']);
  114. unset($_SESSION['ok_msg']);