index.php 6.7 KB

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