index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. ob_start();
  3. $TAB = 'MAIL';
  4. // Main include
  5. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  6. exec(HESTIA_CMD."v-list-sys-webmail json", $output, $return_var);
  7. $webmail_clients = json_decode(implode('', $output), true);
  8. unset($output);
  9. $v_domain = $_GET['domain'];
  10. if (!empty($v_domain)) {
  11. // Set webmail alias
  12. exec(HESTIA_CMD."v-list-mail-domain ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  13. if($return_var > 0){
  14. check_return_code_redirect($return_var, $output, '/list/mail/');
  15. }
  16. $data = json_decode(implode('', $output), true);
  17. unset($output);
  18. $v_webmail_alias = $data[$v_domain]['WEBMAIL_ALIAS'];
  19. }
  20. // Check POST request for mail domain
  21. if (!empty($_POST['ok'])) {
  22. // Check token
  23. verify_csrf($_POST);
  24. // Check empty fields
  25. if (empty($_POST['v_domain'])) {
  26. $errors[] = _('domain');
  27. }
  28. if (!empty($errors[0])) {
  29. foreach ($errors as $i => $error) {
  30. if ($i == 0) {
  31. $error_msg = $error;
  32. } else {
  33. $error_msg = $error_msg.", ".$error;
  34. }
  35. }
  36. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  37. }
  38. // Check antispam option
  39. if (!empty($_POST['v_antispam'])) {
  40. $v_antispam = 'yes';
  41. } else {
  42. $v_antispam = 'no';
  43. }
  44. // Check antivirus option
  45. if (!empty($_POST['v_antivirus'])) {
  46. $v_antivirus = 'yes';
  47. } else {
  48. $v_antivirus = 'no';
  49. }
  50. // Check dkim option
  51. if (!empty($_POST['v_dkim'])) {
  52. $v_dkim = 'yes';
  53. } else {
  54. $v_dkim = 'no';
  55. }
  56. // Set domain name to lowercase and remove www prefix
  57. $v_domain = preg_replace("/^www./i", "", $_POST['v_domain']);
  58. $v_domain = escapeshellarg($v_domain);
  59. $v_domain = strtolower($v_domain);
  60. // Add mail domain
  61. if (empty($_SESSION['error_msg'])) {
  62. exec(HESTIA_CMD."v-add-mail-domain ".$user." ".$v_domain." ".$v_antispam." ".$v_antivirus." ".$v_dkim, $output, $return_var);
  63. check_return_code($return_var, $output);
  64. unset($output);
  65. }
  66. if (!empty($_SESSION['IMAP_SYSTEM']) && !empty($_SESSION['WEBMAIL_SYSTEM'])) {
  67. if (empty($_SESSION['error_msg'])) {
  68. if (!empty($_POST['v_webmail'])) {
  69. $v_webmail = escapeshellarg($_POST['v_webmail']);
  70. exec(HESTIA_CMD."v-add-mail-domain-webmail ".$user." ".$v_domain." ".$v_webmail." yes", $output, $return_var);
  71. check_return_code($return_var, $output);
  72. unset($output);
  73. }
  74. }
  75. }
  76. if (!empty($_SESSION['IMAP_SYSTEM']) && !empty($_SESSION['WEBMAIL_SYSTEM'])) {
  77. if (empty($_POST['v_webmail'])) {
  78. if (empty($_SESSION['error_msg'])) {
  79. exec(HESTIA_CMD."v-delete-mail-domain-webmail ".$user." ".$v_domain." yes", $output, $return_var);
  80. check_return_code($return_var, $output);
  81. unset($output);
  82. }
  83. }
  84. }
  85. // Add SMTP Relay Support
  86. if (empty($_SESSION['error_msg'])) {
  87. if (isset($_POST['v_smtp_relay']) && (!empty($_POST['v_smtp_relay_host'])) && (!empty($_POST['v_smtp_relay_user']))) {
  88. if (($_POST['v_smtp_relay_host'] != $v_smtp_relay_host) ||
  89. ($_POST['v_smtp_relay_user'] != $v_smtp_relay_user) ||
  90. ($_POST['v_smtp_relay_port'] != $v_smtp_relay_port)) {
  91. if (!empty($_POST['v_smtp_relay_pass'])) {
  92. $v_smtp_relay = true;
  93. $v_smtp_relay_host = escapeshellarg($_POST['v_smtp_relay_host']);
  94. $v_smtp_relay_user = escapeshellarg($_POST['v_smtp_relay_user']);
  95. $v_smtp_relay_pass = escapeshellarg($_POST['v_smtp_relay_pass']);
  96. if (!empty($_POST['v_smtp_relay_port'])) {
  97. $v_smtp_relay_port = escapeshellarg($_POST['v_smtp_relay_port']);
  98. } else {
  99. $v_smtp_relay_port = '587';
  100. }
  101. exec(HESTIA_CMD."v-add-mail-domain-smtp-relay ".$user." ".$v_domain." ".$v_smtp_relay_host." ".$v_smtp_relay_user." ".$v_smtp_relay_pass." ".$v_smtp_relay_port, $output, $return_var);
  102. check_return_code($return_var, $output);
  103. unset($output);
  104. } else {
  105. $_SESSION['error_msg'] = _('SMTP Relay Password is required');
  106. }
  107. }
  108. }
  109. }
  110. // Flush field values on success
  111. if (empty($_SESSION['error_msg'])) {
  112. $_SESSION['ok_msg'] = sprintf(_('MAIL_DOMAIN_CREATED_OK'), htmlentities($_POST['v_domain']), htmlentities($_POST['v_domain']));
  113. unset($v_domain, $v_webmail);
  114. }
  115. }
  116. // Check POST request for mail account
  117. if (!empty($_POST['ok_acc'])) {
  118. // Check token
  119. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  120. header('location: /login/');
  121. exit();
  122. }
  123. // Check antispam option
  124. if (!empty($_POST['v_blackhole'])) {
  125. $v_blackhole = 'yes';
  126. } else {
  127. $v_blackhole = 'no';
  128. }
  129. // Check empty fields
  130. if (empty($_POST['v_domain'])) {
  131. $errors[] = _('domain');
  132. }
  133. if (empty($_POST['v_account'])) {
  134. $errors[] = _('account');
  135. }
  136. if ((empty($_POST['v_fwd_only']) && empty($_POST['v_password']))) {
  137. if (empty($_POST['v_password'])) {
  138. $errors[] = _('password');
  139. }
  140. }
  141. if (!empty($errors[0])) {
  142. foreach ($errors as $i => $error) {
  143. if ($i == 0) {
  144. $error_msg = $error;
  145. } else {
  146. $error_msg = $error_msg.", ".$error;
  147. }
  148. }
  149. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  150. }
  151. // Validate email
  152. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  153. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  154. $_SESSION['error_msg'] = _('Please enter valid email address.');
  155. }
  156. }
  157. // Check password length
  158. if (empty($_SESSION['error_msg']) && (empty($_POST['v_fwd_only']))) {
  159. if (!validate_password($_POST['v_password'])) {
  160. $_SESSION['error_msg'] = _('Password does not match the minimum requirements');
  161. }
  162. }
  163. // Protect input
  164. $v_domain = escapeshellarg($_POST['v_domain']);
  165. $v_domain = strtolower($v_domain);
  166. $v_account = escapeshellarg($_POST['v_account']);
  167. $v_quota = escapeshellarg($_POST['v_quota']);
  168. $v_send_email = $_POST['v_send_email'];
  169. $v_credentials = $_POST['v_credentials'];
  170. $v_aliases = $_POST['v_aliases'];
  171. $v_fwd = $_POST['v_fwd'];
  172. if (empty($_POST['v_quota'])) {
  173. $v_quota = 0;
  174. }
  175. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd']))) {
  176. $v_adv = 'yes';
  177. }
  178. // Add Mail Account
  179. if (empty($_SESSION['error_msg'])) {
  180. $v_password = tempnam("/tmp", "vst");
  181. $fp = fopen($v_password, "w");
  182. fwrite($fp, $_POST['v_password']."\n");
  183. fclose($fp);
  184. exec(HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  185. check_return_code($return_var, $output);
  186. unset($output);
  187. unlink($v_password);
  188. $v_password = escapeshellarg($_POST['v_password']);
  189. }
  190. // Add Aliases
  191. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  192. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  193. $valiases = preg_replace("/,/", " ", $valiases);
  194. $valiases = preg_replace('/\s+/', ' ', $valiases);
  195. $valiases = trim($valiases);
  196. $aliases = explode(" ", $valiases);
  197. foreach ($aliases as $alias) {
  198. $alias = escapeshellarg($alias);
  199. if (empty($_SESSION['error_msg'])) {
  200. exec(HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  201. check_return_code($return_var, $output);
  202. unset($output);
  203. }
  204. }
  205. }
  206. if ((!empty($_POST['v_blackhole'])) && (empty($_SESSION['error_msg']))) {
  207. exec(HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." :blackhole:", $output, $return_var);
  208. check_return_code($return_var, $output);
  209. unset($output);
  210. //disable any input in v_fwd
  211. $_POST['v_fwd'] = '';
  212. }
  213. // Add Forwarders
  214. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  215. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  216. $vfwd = preg_replace("/,/", " ", $vfwd);
  217. $vfwd = preg_replace('/\s+/', ' ', $vfwd);
  218. $vfwd = trim($vfwd);
  219. $fwd = explode(" ", $vfwd);
  220. foreach ($fwd as $forward) {
  221. $forward = escapeshellarg($forward);
  222. if (empty($_SESSION['error_msg'])) {
  223. exec(HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  224. check_return_code($return_var, $output);
  225. unset($output);
  226. }
  227. }
  228. }
  229. // Add fwd_only flag
  230. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  231. exec(HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  232. check_return_code($return_var, $output);
  233. unset($output);
  234. }
  235. // Get webmail url
  236. if (empty($_SESSION['error_msg'])) {
  237. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  238. $webmail = "http://".$hostname."/".$v_webmail_alias."/";
  239. if (!empty($_SESSION['WEBMAIL_ALIAS'])) {
  240. $webmail = $_SESSION['WEBMAIL_ALIAS'];
  241. }
  242. }
  243. // Email login credentials
  244. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  245. $to = $v_send_email;
  246. $subject = _("Email Credentials");
  247. $hostname = exec('hostname');
  248. $from = "noreply@".$hostname;
  249. $from_name = _('Hestia Control Panel');
  250. $mailtext = $v_credentials;
  251. send_email($to, $subject, $mailtext, $from, $from_name);
  252. }
  253. // Flush field values on success
  254. if (empty($_SESSION['error_msg'])) {
  255. $_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']));
  256. unset($v_account);
  257. unset($v_password);
  258. unset($v_aliases);
  259. unset($v_fwd);
  260. unset($v_quota);
  261. }
  262. }
  263. // Render page
  264. if (empty($_GET['domain'])) {
  265. // Display body for mail domain
  266. if (!empty($_POST['v_webmail'])) {
  267. $v_webmail = $_POST['v_webmail'];
  268. } else {
  269. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  270. $v_webmail = 'roundcube';
  271. }
  272. render_page($user, $TAB, 'add_mail');
  273. } else {
  274. // Display body for mail account
  275. $v_domain = $_GET['domain'];
  276. render_page($user, $TAB, 'add_mail_acc');
  277. }
  278. // Flush session messages
  279. unset($_SESSION['error_msg']);
  280. unset($_SESSION['ok_msg']);