index.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // Check password length
  105. if (empty($_SESSION['error_msg']) && !empty($_POST['v_fwd_only']) ) {
  106. if (!validate_password($_POST['v_password'])) { $_SESSION['error_msg'] = __('Password does not match the minimum requirements');}
  107. }
  108. // Protect input
  109. $v_domain = escapeshellarg($_POST['v_domain']);
  110. $v_domain = strtolower($v_domain);
  111. $v_account = escapeshellarg($_POST['v_account']);
  112. $v_quota = escapeshellarg($_POST['v_quota']);
  113. $v_send_email = $_POST['v_send_email'];
  114. $v_credentials = $_POST['v_credentials'];
  115. $v_aliases = $_POST['v_aliases'];
  116. $v_fwd = $_POST['v_fwd'];
  117. if (empty($_POST['v_quota'])) $v_quota = 0;
  118. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  119. // Add Mail Account
  120. if (empty($_SESSION['error_msg'])) {
  121. $v_password = tempnam("/tmp","vst");
  122. $fp = fopen($v_password, "w");
  123. fwrite($fp, $_POST['v_password']."\n");
  124. fclose($fp);
  125. exec (HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  126. check_return_code($return_var,$output);
  127. unset($output);
  128. unlink($v_password);
  129. $v_password = escapeshellarg($_POST['v_password']);
  130. }
  131. // Add Aliases
  132. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  133. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  134. $valiases = preg_replace("/,/", " ", $valiases);
  135. $valiases = preg_replace('/\s+/', ' ',$valiases);
  136. $valiases = trim($valiases);
  137. $aliases = explode(" ", $valiases);
  138. foreach ($aliases as $alias) {
  139. $alias = escapeshellarg($alias);
  140. if (empty($_SESSION['error_msg'])) {
  141. exec (HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  142. check_return_code($return_var,$output);
  143. unset($output);
  144. }
  145. }
  146. }
  147. // Add Forwarders
  148. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  149. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  150. $vfwd = preg_replace("/,/", " ", $vfwd);
  151. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  152. $vfwd = trim($vfwd);
  153. $fwd = explode(" ", $vfwd);
  154. foreach ($fwd as $forward) {
  155. $forward = escapeshellarg($forward);
  156. if (empty($_SESSION['error_msg'])) {
  157. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  158. check_return_code($return_var,$output);
  159. unset($output);
  160. }
  161. }
  162. }
  163. // Add fwd_only flag
  164. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  165. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  166. check_return_code($return_var,$output);
  167. unset($output);
  168. }
  169. // Get webmail url
  170. if (empty($_SESSION['error_msg'])) {
  171. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  172. $webmail = "http://".$hostname."/".$v_webmail_alias."/";
  173. if (!empty($_SESSION['WEBMAIL_ALIAS'])) $webmail = $_SESSION['WEBMAIL_ALIAS'];
  174. }
  175. // Email login credentials
  176. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  177. $to = $v_send_email;
  178. $subject = __("Email Credentials");
  179. $hostname = exec('hostname');
  180. $from = __('MAIL_FROM', $hostname);
  181. $mailtext = $v_credentials;
  182. send_email($to, $subject, $mailtext, $from);
  183. }
  184. // Flush field values on success
  185. if (empty($_SESSION['error_msg'])) {
  186. $_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]));
  187. unset($v_account);
  188. unset($v_password);
  189. unset($v_password);
  190. unset($v_aliases);
  191. unset($v_fwd);
  192. unset($v_quota);
  193. }
  194. }
  195. // Render page
  196. if (empty($_GET['domain'])) {
  197. // Display body for mail domain
  198. render_page($user, $TAB, 'add_mail');
  199. } else {
  200. // Display body for mail account
  201. $v_domain = $_GET['domain'];
  202. render_page($user, $TAB, 'add_mail_acc');
  203. }
  204. // Flush session messages
  205. unset($_SESSION['error_msg']);
  206. unset($_SESSION['ok_msg']);