index.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. error_reporting(NULL);
  3. ob_start();
  4. $TAB = 'MAIL';
  5. // Main include
  6. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  7. // Check POST request for mail domain
  8. if (!empty($_POST['ok'])) {
  9. // Check token
  10. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  11. header('location: /login/');
  12. exit();
  13. }
  14. // Check empty fields
  15. if (empty($_POST['v_domain'])) $errors[] = __('domain');
  16. if (!empty($errors[0])) {
  17. foreach ($errors as $i => $error) {
  18. if ( $i == 0 ) {
  19. $error_msg = $error;
  20. } else {
  21. $error_msg = $error_msg.", ".$error;
  22. }
  23. }
  24. $_SESSION['error_msg'] = __('Field "%s" can not be blank.',$error_msg);
  25. }
  26. // Check antispam option
  27. if (!empty($_POST['v_antispam'])) {
  28. $v_antispam = 'yes';
  29. } else {
  30. $v_antispam = 'no';
  31. }
  32. // Check antivirus option
  33. if (!empty($_POST['v_antivirus'])) {
  34. $v_antivirus = 'yes';
  35. } else {
  36. $v_antivirus = 'no';
  37. }
  38. // Check dkim option
  39. if (!empty($_POST['v_dkim'])) {
  40. $v_dkim = 'yes';
  41. } else {
  42. $v_dkim = 'no';
  43. }
  44. // Set domain name to lowercase and remove www prefix
  45. $v_domain = preg_replace("/^www./i", "", $_POST['v_domain']);
  46. $v_domain = strtolower($v_domain);
  47. exec (HESTIA_CMD."v-list-mail-domain ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  48. $data = json_decode(implode('', $output), true);
  49. unset($output);
  50. $v_webmail_alias = $data[$v_domain]['WEBMAIL_ALIAS'];
  51. // Add mail domain
  52. if (empty($_SESSION['error_msg'])) {
  53. exec (HESTIA_CMD."v-add-mail-domain ".$user." ".$v_domain." ".$v_antispam." ".$v_antivirus." ".$v_dkim, $output, $return_var);
  54. check_return_code($return_var,$output);
  55. unset($output);
  56. }
  57. // Flush field values on success
  58. if (empty($_SESSION['error_msg'])) {
  59. $_SESSION['ok_msg'] = __('MAIL_DOMAIN_CREATED_OK',htmlentities($_POST['v_domain']),htmlentities($_POST['v_domain']));
  60. unset($v_domain);
  61. }
  62. }
  63. // Check POST request for mail account
  64. if (!empty($_POST['ok_acc'])) {
  65. // Check token
  66. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  67. header('location: /login/');
  68. exit();
  69. }
  70. // Check empty fields
  71. if (empty($_POST['v_domain'])) $errors[] = __('domain');
  72. if (empty($_POST['v_account'])) $errors[] = __('account');
  73. if (empty($_POST['v_password'])) $errors[] = __('password');
  74. if (!empty($errors[0])) {
  75. foreach ($errors as $i => $error) {
  76. if ( $i == 0 ) {
  77. $error_msg = $error;
  78. } else {
  79. $error_msg = $error_msg.", ".$error;
  80. }
  81. }
  82. $_SESSION['error_msg'] = __('Field "%s" can not be blank.',$error_msg);
  83. }
  84. // Validate email
  85. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  86. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  87. $_SESSION['error_msg'] = __('Please enter valid email address.');
  88. }
  89. }
  90. // Protect input
  91. $v_domain = escapeshellarg($_POST['v_domain']);
  92. $v_domain = strtolower($v_domain);
  93. $v_account = escapeshellarg($_POST['v_account']);
  94. $v_quota = escapeshellarg($_POST['v_quota']);
  95. $v_send_email = $_POST['v_send_email'];
  96. $v_credentials = $_POST['v_credentials'];
  97. $v_aliases = $_POST['v_aliases'];
  98. $v_fwd = $_POST['v_fwd'];
  99. if (empty($_POST['v_quota'])) $v_quota = 0;
  100. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  101. // Add Mail Account
  102. if (empty($_SESSION['error_msg'])) {
  103. $v_password = tempnam("/tmp","vst");
  104. $fp = fopen($v_password, "w");
  105. fwrite($fp, $_POST['v_password']."\n");
  106. fclose($fp);
  107. exec (HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  108. check_return_code($return_var,$output);
  109. unset($output);
  110. unlink($v_password);
  111. $v_password = escapeshellarg($_POST['v_password']);
  112. }
  113. // Add Aliases
  114. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  115. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  116. $valiases = preg_replace("/,/", " ", $valiases);
  117. $valiases = preg_replace('/\s+/', ' ',$valiases);
  118. $valiases = trim($valiases);
  119. $aliases = explode(" ", $valiases);
  120. foreach ($aliases as $alias) {
  121. $alias = escapeshellarg($alias);
  122. if (empty($_SESSION['error_msg'])) {
  123. exec (HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  124. check_return_code($return_var,$output);
  125. unset($output);
  126. }
  127. }
  128. }
  129. // Add Forwarders
  130. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  131. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  132. $vfwd = preg_replace("/,/", " ", $vfwd);
  133. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  134. $vfwd = trim($vfwd);
  135. $fwd = explode(" ", $vfwd);
  136. foreach ($fwd as $forward) {
  137. $forward = escapeshellarg($forward);
  138. if (empty($_SESSION['error_msg'])) {
  139. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  140. check_return_code($return_var,$output);
  141. unset($output);
  142. }
  143. }
  144. }
  145. // Add fwd_only flag
  146. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  147. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  148. check_return_code($return_var,$output);
  149. unset($output);
  150. }
  151. // Email login credentials
  152. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  153. $to = $v_send_email;
  154. $subject = __("Email Credentials");
  155. $hostname = exec('hostname');
  156. $from = __('MAIL_FROM', $hostname);
  157. $mailtext = $v_credentials;
  158. send_email($to, $subject, $mailtext, $from);
  159. }
  160. // Flush field values on success
  161. if (empty($_SESSION['error_msg'])) {
  162. $_SESSION['ok_msg'] = __('MAIL_ACCOUNT_CREATED_OK',htmlentities(strtolower($_POST['v_account'])),htmlentities($_POST[v_domain]),htmlentities(strtolower($_POST['v_account'])),htmlentities($_POST[v_domain]));
  163. unset($v_account);
  164. unset($v_password);
  165. unset($v_password);
  166. unset($v_aliases);
  167. unset($v_fwd);
  168. unset($v_quota);
  169. }
  170. }
  171. // Render page
  172. if (empty($_GET['domain'])) {
  173. // Display body for mail domain
  174. render_page($user, $TAB, 'add_mail');
  175. } else {
  176. // Display body for mail account
  177. $v_domain = $_GET['domain'];
  178. render_page($user, $TAB, 'add_mail_acc');
  179. }
  180. // Flush session messages
  181. unset($_SESSION['error_msg']);
  182. unset($_SESSION['ok_msg']);