index.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // Protect input
  82. $v_domain = escapeshellarg($_POST['v_domain']);
  83. $v_domain = strtolower($v_domain);
  84. $v_account = escapeshellarg($_POST['v_account']);
  85. $v_quota = escapeshellarg($_POST['v_quota']);
  86. $v_aliases = $_POST['v_aliases'];
  87. $v_fwd = $_POST['v_fwd'];
  88. if (empty($_POST['v_quota'])) $v_quota = 0;
  89. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  90. // Add Mail Account
  91. if (empty($_SESSION['error_msg'])) {
  92. $v_password = tempnam("/tmp","vst");
  93. $fp = fopen($v_password, "w");
  94. fwrite($fp, $_POST['v_password']."\n");
  95. fclose($fp);
  96. exec (VESTA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  97. check_return_code($return_var,$output);
  98. unset($output);
  99. unlink($v_password);
  100. $v_password = escapeshellarg($_POST['v_password']);
  101. }
  102. // Add Aliases
  103. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  104. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  105. $valiases = preg_replace("/,/", " ", $valiases);
  106. $valiases = preg_replace('/\s+/', ' ',$valiases);
  107. $valiases = trim($valiases);
  108. $aliases = explode(" ", $valiases);
  109. foreach ($aliases as $alias) {
  110. $alias = escapeshellarg($alias);
  111. if (empty($_SESSION['error_msg'])) {
  112. exec (VESTA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  113. check_return_code($return_var,$output);
  114. unset($output);
  115. }
  116. }
  117. }
  118. // Add Forwarders
  119. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  120. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  121. $vfwd = preg_replace("/,/", " ", $vfwd);
  122. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  123. $vfwd = trim($vfwd);
  124. $fwd = explode(" ", $vfwd);
  125. foreach ($fwd as $forward) {
  126. $forward = escapeshellarg($forward);
  127. if (empty($_SESSION['error_msg'])) {
  128. exec (VESTA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  129. check_return_code($return_var,$output);
  130. unset($output);
  131. }
  132. }
  133. }
  134. // Add fwd_only flag
  135. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  136. exec (VESTA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  137. check_return_code($return_var,$output);
  138. unset($output);
  139. }
  140. // Get webmail url
  141. if (empty($_SESSION['error_msg'])) {
  142. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  143. $webmail = "http://".$http_host."/webmail/";
  144. if (!empty($_SESSION['MAIL_URL'])) $webmail = $_SESSION['MAIL_URL'];
  145. }
  146. // Flush field values on success
  147. if (empty($_SESSION['error_msg'])) {
  148. $_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]));
  149. $_SESSION['ok_msg'] .= " / <a href=".$webmail." target='_blank'>" . __('open webmail') . "</a>";
  150. unset($v_account);
  151. unset($v_password);
  152. unset($v_password);
  153. unset($v_aliases);
  154. unset($v_fwd);
  155. unset($v_quota);
  156. }
  157. }
  158. // Render page
  159. if (empty($_GET['domain'])) {
  160. // Display body for mail domain
  161. render_page($user, $TAB, 'add_mail');
  162. } else {
  163. // Display body for mail account
  164. $v_domain = $_GET['domain'];
  165. render_page($user, $TAB, 'add_mail_acc');
  166. }
  167. // Flush session messages
  168. unset($_SESSION['error_msg']);
  169. unset($_SESSION['ok_msg']);