index.php 7.7 KB

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