index.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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'] = sprintf(_('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'] = sprintf(_('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 antispam option
  85. if (!empty($_POST['v_blackhole'])) {
  86. $v_blackhole = 'yes';
  87. } else {
  88. $v_blackhole = 'no';
  89. }
  90. // Check empty fields
  91. if (empty($_POST['v_domain'])) $errors[] = _('domain');
  92. if (empty($_POST['v_account'])) $errors[] = _('account');
  93. if ((empty($_POST['v_fwd_only']) && empty($_POST['v_password']))) {
  94. if (empty($_POST['v_password'])) $errors[] = _('password');
  95. }
  96. if (!empty($errors[0])) {
  97. foreach ($errors as $i => $error) {
  98. if ( $i == 0 ) {
  99. $error_msg = $error;
  100. } else {
  101. $error_msg = $error_msg.", ".$error;
  102. }
  103. }
  104. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  105. }
  106. // Validate email
  107. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  108. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  109. $_SESSION['error_msg'] = _('Please enter valid email address.');
  110. }
  111. }
  112. // Check password length
  113. if (empty($_SESSION['error_msg']) && (empty($_POST['v_fwd_only']))) {
  114. if (!validate_password($_POST['v_password'])) { $_SESSION['error_msg'] = _('Password does not match the minimum requirements');}
  115. }
  116. // Protect input
  117. $v_domain = escapeshellarg($_POST['v_domain']);
  118. $v_domain = strtolower($v_domain);
  119. $v_account = escapeshellarg($_POST['v_account']);
  120. $v_quota = escapeshellarg($_POST['v_quota']);
  121. $v_send_email = $_POST['v_send_email'];
  122. $v_credentials = $_POST['v_credentials'];
  123. $v_aliases = $_POST['v_aliases'];
  124. $v_fwd = $_POST['v_fwd'];
  125. if (empty($_POST['v_quota'])) $v_quota = 0;
  126. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  127. // Add Mail Account
  128. if (empty($_SESSION['error_msg'])) {
  129. $v_password = tempnam("/tmp","vst");
  130. $fp = fopen($v_password, "w");
  131. fwrite($fp, $_POST['v_password']."\n");
  132. fclose($fp);
  133. exec (HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  134. check_return_code($return_var,$output);
  135. unset($output);
  136. unlink($v_password);
  137. $v_password = escapeshellarg($_POST['v_password']);
  138. }
  139. // Add Aliases
  140. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  141. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  142. $valiases = preg_replace("/,/", " ", $valiases);
  143. $valiases = preg_replace('/\s+/', ' ',$valiases);
  144. $valiases = trim($valiases);
  145. $aliases = explode(" ", $valiases);
  146. foreach ($aliases as $alias) {
  147. $alias = escapeshellarg($alias);
  148. if (empty($_SESSION['error_msg'])) {
  149. exec (HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  150. check_return_code($return_var,$output);
  151. unset($output);
  152. }
  153. }
  154. }
  155. if ((!empty($_POST['v_blackhole'])) && (empty($_SESSION['error_msg']))){
  156. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." :blackhole:", $output, $return_var);
  157. check_return_code($return_var,$output);
  158. unset($output);
  159. //disable any input in v_fwd
  160. $_POST['v_fwd'] = '';
  161. }
  162. // Add Forwarders
  163. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  164. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  165. $vfwd = preg_replace("/,/", " ", $vfwd);
  166. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  167. $vfwd = trim($vfwd);
  168. $fwd = explode(" ", $vfwd);
  169. foreach ($fwd as $forward) {
  170. $forward = escapeshellarg($forward);
  171. if (empty($_SESSION['error_msg'])) {
  172. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  173. check_return_code($return_var,$output);
  174. unset($output);
  175. }
  176. }
  177. }
  178. // Add fwd_only flag
  179. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  180. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  181. check_return_code($return_var,$output);
  182. unset($output);
  183. }
  184. // Get webmail url
  185. if (empty($_SESSION['error_msg'])) {
  186. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  187. $webmail = "http://".$hostname."/".$v_webmail_alias."/";
  188. if (!empty($_SESSION['WEBMAIL_ALIAS'])) $webmail = $_SESSION['WEBMAIL_ALIAS'];
  189. }
  190. // Email login credentials
  191. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  192. $to = $v_send_email;
  193. $subject = _("Email Credentials");
  194. $hostname = exec('hostname');
  195. $from = sprintf(_('MAIL_FROM'), $hostname);
  196. $mailtext = $v_credentials;
  197. send_email($to, $subject, $mailtext, $from);
  198. }
  199. // Flush field values on success
  200. if (empty($_SESSION['error_msg'])) {
  201. $_SESSION['ok_msg'] = sprintf(_('MAIL_ACCOUNT_CREATED_OK'),htmlentities(strtolower($_POST['v_account'])),htmlentities($_POST[v_domain]),htmlentities(strtolower($_POST['v_account'])),htmlentities($_POST[v_domain]));
  202. unset($v_account);
  203. unset($v_password);
  204. unset($v_password);
  205. unset($v_aliases);
  206. unset($v_fwd);
  207. unset($v_quota);
  208. }
  209. }
  210. // Render page
  211. if (empty($_GET['domain'])) {
  212. // Display body for mail domain
  213. render_page($user, $TAB, 'add_mail');
  214. } else {
  215. // Display body for mail account
  216. $v_domain = $_GET['domain'];
  217. render_page($user, $TAB, 'add_mail_acc');
  218. }
  219. // Flush session messages
  220. unset($_SESSION['error_msg']);
  221. unset($_SESSION['ok_msg']);