index.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 = escapeshellarg($v_domain);
  47. $v_domain = strtolower($v_domain);
  48. // Add mail domain
  49. if (empty($_SESSION['error_msg'])) {
  50. exec (VESTA_CMD."v-add-mail-domain ".$user." ".$v_domain." ".$v_antispam." ".$v_antivirus." ".$v_dkim, $output, $return_var);
  51. check_return_code($return_var,$output);
  52. unset($output);
  53. }
  54. // Flush field values on success
  55. if (empty($_SESSION['error_msg'])) {
  56. $_SESSION['ok_msg'] = __('MAIL_DOMAIN_CREATED_OK',htmlentities($_POST['v_domain']),htmlentities($_POST['v_domain']));
  57. unset($v_domain);
  58. }
  59. }
  60. // Check POST request for mail account
  61. if (!empty($_POST['ok_acc'])) {
  62. // Check token
  63. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  64. header('location: /login/');
  65. exit();
  66. }
  67. // Check empty fields
  68. if (empty($_POST['v_domain'])) $errors[] = __('domain');
  69. if (empty($_POST['v_account'])) $errors[] = __('account');
  70. if (empty($_POST['v_password'])) $errors[] = __('password');
  71. if (!empty($errors[0])) {
  72. foreach ($errors as $i => $error) {
  73. if ( $i == 0 ) {
  74. $error_msg = $error;
  75. } else {
  76. $error_msg = $error_msg.", ".$error;
  77. }
  78. }
  79. $_SESSION['error_msg'] = __('Field "%s" can not be blank.',$error_msg);
  80. }
  81. // Validate email
  82. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  83. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  84. $_SESSION['error_msg'] = __('Please enter valid email address.');
  85. }
  86. }
  87. // Protect input
  88. $v_domain = escapeshellarg($_POST['v_domain']);
  89. $v_domain = strtolower($v_domain);
  90. $v_account = escapeshellarg($_POST['v_account']);
  91. $v_quota = escapeshellarg($_POST['v_quota']);
  92. $v_send_email = $_POST['v_send_email'];
  93. $v_credentials = $_POST['v_credentials'];
  94. $v_aliases = $_POST['v_aliases'];
  95. $v_fwd = $_POST['v_fwd'];
  96. if (empty($_POST['v_quota'])) $v_quota = 0;
  97. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  98. // Add Mail Account
  99. if (empty($_SESSION['error_msg'])) {
  100. $v_password = tempnam("/tmp","vst");
  101. $fp = fopen($v_password, "w");
  102. fwrite($fp, $_POST['v_password']."\n");
  103. fclose($fp);
  104. exec (VESTA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  105. check_return_code($return_var,$output);
  106. unset($output);
  107. unlink($v_password);
  108. $v_password = escapeshellarg($_POST['v_password']);
  109. }
  110. // Add Aliases
  111. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  112. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  113. $valiases = preg_replace("/,/", " ", $valiases);
  114. $valiases = preg_replace('/\s+/', ' ',$valiases);
  115. $valiases = trim($valiases);
  116. $aliases = explode(" ", $valiases);
  117. foreach ($aliases as $alias) {
  118. $alias = escapeshellarg($alias);
  119. if (empty($_SESSION['error_msg'])) {
  120. exec (VESTA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  121. check_return_code($return_var,$output);
  122. unset($output);
  123. }
  124. }
  125. }
  126. // Add Forwarders
  127. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  128. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  129. $vfwd = preg_replace("/,/", " ", $vfwd);
  130. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  131. $vfwd = trim($vfwd);
  132. $fwd = explode(" ", $vfwd);
  133. foreach ($fwd as $forward) {
  134. $forward = escapeshellarg($forward);
  135. if (empty($_SESSION['error_msg'])) {
  136. exec (VESTA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  137. check_return_code($return_var,$output);
  138. unset($output);
  139. }
  140. }
  141. }
  142. // Add fwd_only flag
  143. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  144. exec (VESTA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  145. check_return_code($return_var,$output);
  146. unset($output);
  147. }
  148. // Get webmail url
  149. if (empty($_SESSION['error_msg'])) {
  150. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  151. $webmail = "http://".$http_host."/webmail/";
  152. if (!empty($_SESSION['MAIL_URL'])) $webmail = $_SESSION['MAIL_URL'];
  153. }
  154. // Email login credentials
  155. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  156. $to = $v_send_email;
  157. $subject = __("Email Credentials");
  158. $hostname = exec('hostname');
  159. $from = __('MAIL_FROM', $hostname);
  160. $mailtext = $v_credentials;
  161. send_email($to, $subject, $mailtext, $from);
  162. }
  163. // Flush field values on success
  164. if (empty($_SESSION['error_msg'])) {
  165. $_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]));
  166. $_SESSION['ok_msg'] .= " / <a href=".$webmail." target='_blank'>" . __('open webmail') . "</a>";
  167. unset($v_account);
  168. unset($v_password);
  169. unset($v_password);
  170. unset($v_aliases);
  171. unset($v_fwd);
  172. unset($v_quota);
  173. }
  174. }
  175. // Render page
  176. if (empty($_GET['domain'])) {
  177. // Display body for mail domain
  178. render_page($user, $TAB, 'add_mail');
  179. } else {
  180. // Display body for mail account
  181. $v_domain = $_GET['domain'];
  182. render_page($user, $TAB, 'add_mail_acc');
  183. }
  184. // Flush session messages
  185. unset($_SESSION['error_msg']);
  186. unset($_SESSION['ok_msg']);