index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 = strtolower($v_domain);
  48. // Add mail domain
  49. if (empty($_SESSION['error_msg'])) {
  50. v_exec('v-add-mail-domain', [$user, $v_domain, $v_antispam, $v_antivirus, $v_dkim]);
  51. }
  52. // Flush field values on success
  53. if (empty($_SESSION['error_msg'])) {
  54. $_SESSION['ok_msg'] = __('MAIL_DOMAIN_CREATED_OK', htmlentities($_POST['v_domain']), htmlentities($_POST['v_domain']));
  55. unset($v_domain);
  56. }
  57. }
  58. // Check POST request for mail account
  59. if (!empty($_POST['ok_acc'])) {
  60. // Check token
  61. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  62. header('location: /login/');
  63. exit;
  64. }
  65. // Check empty fields
  66. if (empty($_POST['v_domain'])) $errors[] = __('domain');
  67. if (empty($_POST['v_account'])) $errors[] = __('account');
  68. if (empty($_POST['v_password'])) $errors[] = __('password');
  69. if (!empty($errors[0])) {
  70. foreach ($errors as $i => $error) {
  71. if ( $i == 0 ) {
  72. $error_msg = $error;
  73. } else {
  74. $error_msg = $error_msg.", ".$error;
  75. }
  76. }
  77. $_SESSION['error_msg'] = __('Field "%s" can not be blank.', $error_msg);
  78. }
  79. // Protect input
  80. $v_domain = strtolower($_POST['v_domain']);
  81. $v_account = $_POST['v_account'];
  82. $v_quota = $_POST['v_quota'];
  83. $v_aliases = $_POST['v_aliases'];
  84. $v_fwd = $_POST['v_fwd'];
  85. if (empty($_POST['v_quota'])) $v_quota = '0';
  86. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  87. // Add Mail Account
  88. if (empty($_SESSION['error_msg'])) {
  89. $v_password = tempnam("/tmp","vst");
  90. $fp = fopen($v_password, "w");
  91. fwrite($fp, $_POST['v_password']."\n");
  92. fclose($fp);
  93. v_exec('v-add-mail-account', [$user, $v_domain, $v_account, $v_password, $v_quota]);
  94. unlink($v_password);
  95. $v_password = $_POST['v_password'];
  96. }
  97. // Add Aliases
  98. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  99. $valiases = preg_replace('/\n/', ' ', $_POST['v_aliases']);
  100. $valiases = preg_replace('/,/', ' ', $valiases);
  101. $valiases = preg_replace('/\s+/', ' ',$valiases);
  102. $valiases = trim($valiases);
  103. $aliases = explode(' ', $valiases);
  104. foreach ($aliases as $alias) {
  105. if (empty($_SESSION['error_msg'])) {
  106. v_exec('v-add-mail-account-alias', [$user, $v_domain, $v_account, $alias]);
  107. }
  108. }
  109. }
  110. // Add Forwarders
  111. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  112. $vfwd = preg_replace('/\n/', ' ', $_POST['v_fwd']);
  113. $vfwd = preg_replace('/,/', ' ', $vfwd);
  114. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  115. $vfwd = trim($vfwd);
  116. $fwd = explode(' ', $vfwd);
  117. foreach ($fwd as $forward) {
  118. if (empty($_SESSION['error_msg'])) {
  119. v_exec('v-add-mail-account-forward', [$user, $v_domain, $v_account, $forward]);
  120. }
  121. }
  122. }
  123. // Add fwd_only flag
  124. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  125. v_exec('v-add-mail-account-fwd-only', [$user, $v_domain, $v_account]);
  126. }
  127. // Get webmail url
  128. if (empty($_SESSION['error_msg'])) {
  129. list($http_host, $port) = explode(':', $_SERVER['HTTP_HOST'].':');
  130. $webmail = "http://$http_host/webmail/";
  131. if (!empty($_SESSION['MAIL_URL'])) $webmail = $_SESSION['MAIL_URL'];
  132. }
  133. // Flush field values on success
  134. if (empty($_SESSION['error_msg'])) {
  135. $_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']));
  136. $_SESSION['ok_msg'] .= " / <a href=$webmail target='_blank'>" . __('open webmail') . '</a>';
  137. unset($v_account);
  138. unset($v_password);
  139. unset($v_password);
  140. unset($v_aliases);
  141. unset($v_fwd);
  142. unset($v_quota);
  143. }
  144. }
  145. // Header
  146. include($_SERVER['DOCUMENT_ROOT'].'/templates/header.html');
  147. // Panel
  148. top_panel($user,$TAB);
  149. // Display body for mail domain
  150. if (empty($_GET['domain'])) {
  151. include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_mail.html');
  152. }
  153. // Display body for mail account
  154. if (!empty($_GET['domain'])) {
  155. $v_domain = $_GET['domain'];
  156. include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_mail_acc.html');
  157. }
  158. // Flush session messages
  159. unset($_SESSION['error_msg']);
  160. unset($_SESSION['ok_msg']);
  161. // Footer
  162. include($_SERVER['DOCUMENT_ROOT'].'/templates/footer.html');