index.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 domain argument
  8. if (empty($_GET['domain'])) {
  9. header("Location: /list/mail/");
  10. exit;
  11. }
  12. // Edit as someone else?
  13. if (($_SESSION['userContext'] === 'admin') && (!empty($_GET['user']))) {
  14. $user=escapeshellarg($_GET['user']);
  15. }
  16. $v_username = $user;
  17. // Get all user domains
  18. exec(HESTIA_CMD."v-list-mail-domains ".escapeshellarg($user)." json", $output, $return_var);
  19. $user_domains = json_decode(implode('', $output), true);
  20. $user_domains = array_keys($user_domains);
  21. unset($output);
  22. exec(HESTIA_CMD."v-list-sys-webmail json", $output, $return_var);
  23. $webmail_clients = json_decode(implode('', $output), true);
  24. unset($output);
  25. // List mail domain
  26. if ((!empty($_GET['domain'])) && (empty($_GET['account']))) {
  27. $v_domain = $_GET['domain'];
  28. if ($_SESSION['userContext'] !== 'admin') {
  29. if (!in_array($v_domain, $user_domains)) {
  30. header("Location: /list/mail/");
  31. exit;
  32. }
  33. }
  34. exec(HESTIA_CMD."v-list-mail-domain ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  35. $data = json_decode(implode('', $output), true);
  36. unset($output);
  37. // Parse domain
  38. $v_antispam = $data[$v_domain]['ANTISPAM'];
  39. $v_antivirus = $data[$v_domain]['ANTIVIRUS'];
  40. $v_dkim = $data[$v_domain]['DKIM'];
  41. $v_catchall = $data[$v_domain]['CATCHALL'];
  42. $v_date = $data[$v_domain]['DATE'];
  43. $v_time = $data[$v_domain]['TIME'];
  44. $v_suspended = $data[$v_domain]['SUSPENDED'];
  45. $v_webmail_alias = $data[$v_domain]['WEBMAIL_ALIAS'];
  46. $v_webmail = $data[$v_domain]['WEBMAIL'];
  47. $v_smtp_relay = $data[$v_domain]['U_SMTP_RELAY'];
  48. $v_smtp_relay_host = $data[$v_domain]['U_SMTP_RELAY_HOST'];
  49. $v_smtp_relay_port = $data[$v_domain]['U_SMTP_RELAY_PORT'];
  50. $v_smtp_relay_user = $data[$v_domain]['U_SMTP_RELAY_USERNAME'];
  51. if ($v_suspended == 'yes') {
  52. $v_status = 'suspended';
  53. } else {
  54. $v_status = 'active';
  55. }
  56. $v_ssl = $data[$v_domain]['SSL'];
  57. if (!empty($v_ssl)) {
  58. exec(HESTIA_CMD."v-list-mail-domain-ssl ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  59. $ssl_str = json_decode(implode('', $output), true);
  60. unset($output);
  61. $v_ssl_crt = $ssl_str[$v_domain]['CRT'];
  62. $v_ssl_key = $ssl_str[$v_domain]['KEY'];
  63. $v_ssl_ca = $ssl_str[$v_domain]['CA'];
  64. $v_ssl_subject = $ssl_str[$v_domain]['SUBJECT'];
  65. $v_ssl_aliases = $ssl_str[$v_domain]['ALIASES'];
  66. $v_ssl_not_before = $ssl_str[$v_domain]['NOT_BEFORE'];
  67. $v_ssl_not_after = $ssl_str[$v_domain]['NOT_AFTER'];
  68. $v_ssl_signature = $ssl_str[$v_domain]['SIGNATURE'];
  69. $v_ssl_pub_key = $ssl_str[$v_domain]['PUB_KEY'];
  70. $v_ssl_issuer = $ssl_str[$v_domain]['ISSUER'];
  71. }
  72. $v_letsencrypt = $data[$v_domain]['LETSENCRYPT'];
  73. if (empty($v_letsencrypt)) {
  74. $v_letsencrypt = 'no';
  75. }
  76. }
  77. // List mail account
  78. if ((!empty($_GET['domain'])) && (!empty($_GET['account']))) {
  79. $v_domain = $_GET['domain'];
  80. if ($_SESSION['userContext'] !== 'admin') {
  81. if (!in_array($v_domain, $user_domains)) {
  82. header("Location: /list/mail/");
  83. exit;
  84. }
  85. }
  86. $v_account = $_GET['account'];
  87. exec(HESTIA_CMD."v-list-mail-account ".$user." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." 'json'", $output, $return_var);
  88. $data = json_decode(implode('', $output), true);
  89. unset($output);
  90. // Parse mail account
  91. $v_username = $user;
  92. $v_password = "";
  93. $v_aliases = str_replace(',', "\n", $data[$v_account]['ALIAS']);
  94. $valiases = explode(",", $data[$v_account]['ALIAS']);
  95. $v_fwd = str_replace(',', "\n", $data[$v_account]['FWD']);
  96. if ($v_fwd == ":blackhole:") {
  97. $v_fwd = '';
  98. $v_blackhole = "yes";
  99. } else {
  100. $v_blackhole = "no";
  101. }
  102. $vfwd = explode(",", $data[$v_account]['FWD']);
  103. $v_fwd_only = $data[$v_account]['FWD_ONLY'];
  104. $v_quota = $data[$v_account]['QUOTA'];
  105. $v_autoreply = $data[$v_account]['AUTOREPLY'];
  106. $v_suspended = $data[$v_account]['SUSPENDED'];
  107. $v_webmail_alias = $data[$v_account]['WEBMAIL_ALIAS'];
  108. if ($v_suspended == 'yes') {
  109. $v_status = 'suspended';
  110. } else {
  111. $v_status = 'active';
  112. }
  113. $v_date = $data[$v_account]['DATE'];
  114. $v_time = $data[$v_account]['TIME'];
  115. // Parse autoreply
  116. if ($v_autoreply == 'yes') {
  117. exec(HESTIA_CMD."v-list-mail-account-autoreply ".$user." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." json", $output, $return_var);
  118. $autoreply_str = json_decode(implode('', $output), true);
  119. unset($output);
  120. $v_autoreply_message = $autoreply_str[$v_account]['MSG'];
  121. $v_autoreply_message=str_replace("\\n", "\n", $v_autoreply_message);
  122. }
  123. }
  124. // Check POST request for mail domain
  125. if ((!empty($_POST['save'])) && (!empty($_GET['domain'])) && (empty($_GET['account']))) {
  126. $v_domain = $_POST['v_domain'];
  127. if (!in_array($v_domain, $user_domains)) {
  128. check_return_code(3, ["Unknown domain"]);
  129. }
  130. // Check token
  131. verify_csrf($_POST);
  132. // Delete antispam
  133. if (($v_antispam == 'yes') && (empty($_POST['v_antispam'])) && (empty($_SESSION['error_msg']))) {
  134. exec(HESTIA_CMD."v-delete-mail-domain-antispam ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  135. check_return_code($return_var, $output);
  136. $v_antispam = 'no';
  137. unset($output);
  138. }
  139. // Add antispam
  140. if (($v_antispam == 'no') && (!empty($_POST['v_antispam'])) && (empty($_SESSION['error_msg']))) {
  141. exec(HESTIA_CMD."v-add-mail-domain-antispam ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  142. check_return_code($return_var, $output);
  143. $v_antispam = 'yes';
  144. unset($output);
  145. }
  146. // Delete antivirus
  147. if (($v_antivirus == 'yes') && (empty($_POST['v_antivirus'])) && (empty($_SESSION['error_msg']))) {
  148. exec(HESTIA_CMD."v-delete-mail-domain-antivirus ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  149. check_return_code($return_var, $output);
  150. $v_antivirus = 'no';
  151. unset($output);
  152. }
  153. // Add antivirus
  154. if (($v_antivirus == 'no') && (!empty($_POST['v_antivirus'])) && (empty($_SESSION['error_msg']))) {
  155. exec(HESTIA_CMD."v-add-mail-domain-antivirus ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  156. check_return_code($return_var, $output);
  157. $v_antivirus = 'yes';
  158. unset($output);
  159. }
  160. // Delete DKIM
  161. if (($v_dkim == 'yes') && (empty($_POST['v_dkim'])) && (empty($_SESSION['error_msg']))) {
  162. exec(HESTIA_CMD."v-delete-mail-domain-dkim ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  163. check_return_code($return_var, $output);
  164. $v_dkim = 'no';
  165. unset($output);
  166. }
  167. // Add DKIM
  168. if (($v_dkim == 'no') && (!empty($_POST['v_dkim'])) && (empty($_SESSION['error_msg']))) {
  169. exec(HESTIA_CMD."v-add-mail-domain-dkim ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  170. check_return_code($return_var, $output);
  171. $v_dkim = 'yes';
  172. unset($output);
  173. }
  174. // Delete catchall
  175. if ((!empty($v_catchall)) && (empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  176. exec(HESTIA_CMD."v-delete-mail-domain-catchall ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  177. check_return_code($return_var, $output);
  178. $v_catchall = '';
  179. unset($output);
  180. }
  181. // Change catchall address
  182. if ((!empty($v_catchall)) && (!empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  183. if ($v_catchall != $_POST['v_catchall']) {
  184. $v_catchall = escapeshellarg($_POST['v_catchall']);
  185. exec(HESTIA_CMD."v-change-mail-domain-catchall ".$v_username." ".escapeshellarg($v_domain)." ".$v_catchall, $output, $return_var);
  186. check_return_code($return_var, $output);
  187. unset($output);
  188. }
  189. }
  190. // Add catchall
  191. if ((empty($v_catchall)) && (!empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  192. $v_catchall = escapeshellarg($_POST['v_catchall']);
  193. exec(HESTIA_CMD."v-add-mail-domain-catchall ".$v_username." ".escapeshellarg($v_domain)." ".$v_catchall, $output, $return_var);
  194. check_return_code($return_var, $output);
  195. unset($output);
  196. }
  197. if (!empty($_SESSION['IMAP_SYSTEM']) && !empty($_SESSION['WEBMAIL_SYSTEM'])) {
  198. if (empty($_SESSION['error_msg'])) {
  199. if (!empty($_POST['v_webmail'])) {
  200. $v_webmail = escapeshellarg($_POST['v_webmail']);
  201. exec(HESTIA_CMD."v-add-mail-domain-webmail ".$user." ".$v_domain." ".$v_webmail." yes", $output, $return_var);
  202. check_return_code($return_var, $output);
  203. unset($output);
  204. }
  205. }
  206. }
  207. if (!empty($_SESSION['IMAP_SYSTEM']) && !empty($_SESSION['WEBMAIL_SYSTEM'])) {
  208. if (empty($_POST['v_webmail'])) {
  209. if (empty($_SESSION['error_msg'])) {
  210. exec(HESTIA_CMD."v-delete-mail-domain-webmail ".$user." ".$v_domain." yes", $output, $return_var);
  211. check_return_code($return_var, $output);
  212. $v_webmail = "";
  213. unset($output);
  214. }
  215. }
  216. }
  217. // Change SSL certificate
  218. if (($v_letsencrypt == 'no') && (empty($_POST['v_letsencrypt'])) && ($v_ssl == 'yes') && (!empty($_POST['v_ssl'])) && (empty($_SESSION['error_msg']))) {
  219. if (($v_ssl_crt != str_replace("\r\n", "\n", $_POST['v_ssl_crt'])) || ($v_ssl_key != str_replace("\r\n", "\n", $_POST['v_ssl_key'])) || ($v_ssl_ca != str_replace("\r\n", "\n", $_POST['v_ssl_ca']))) {
  220. exec('mktemp -d', $mktemp_output, $return_var);
  221. $tmpdir = $mktemp_output[0];
  222. // Certificate
  223. if (!empty($_POST['v_ssl_crt'])) {
  224. $fp = fopen($tmpdir."/".$v_domain.".crt", 'w');
  225. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_crt']));
  226. fwrite($fp, "\n");
  227. fclose($fp);
  228. }
  229. // Key
  230. if (!empty($_POST['v_ssl_key'])) {
  231. $fp = fopen($tmpdir."/".$v_domain.".key", 'w');
  232. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_key']));
  233. fwrite($fp, "\n");
  234. fclose($fp);
  235. }
  236. // CA
  237. if (!empty($_POST['v_ssl_ca'])) {
  238. $fp = fopen($tmpdir."/".$v_domain.".ca", 'w');
  239. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_ca']));
  240. fwrite($fp, "\n");
  241. fclose($fp);
  242. }
  243. exec(HESTIA_CMD."v-change-mail-domain-sslcert ".$user." ".escapeshellarg($v_domain)." ".$tmpdir." 'no'", $output, $return_var);
  244. check_return_code($return_var, $output);
  245. unset($output);
  246. $restart_web = 'yes';
  247. $restart_proxy = 'yes';
  248. exec(HESTIA_CMD."v-list-mail-domain-ssl ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  249. $ssl_str = json_decode(implode('', $output), true);
  250. unset($output);
  251. $v_ssl_crt = $ssl_str[$v_domain]['CRT'];
  252. $v_ssl_key = $ssl_str[$v_domain]['KEY'];
  253. $v_ssl_ca = $ssl_str[$v_domain]['CA'];
  254. $v_ssl_subject = $ssl_str[$v_domain]['SUBJECT'];
  255. $v_ssl_aliases = $ssl_str[$v_domain]['ALIASES'];
  256. $v_ssl_not_before = $ssl_str[$v_domain]['NOT_BEFORE'];
  257. $v_ssl_not_after = $ssl_str[$v_domain]['NOT_AFTER'];
  258. $v_ssl_signature = $ssl_str[$v_domain]['SIGNATURE'];
  259. $v_ssl_pub_key = $ssl_str[$v_domain]['PUB_KEY'];
  260. $v_ssl_issuer = $ssl_str[$v_domain]['ISSUER'];
  261. // Cleanup certificate tempfiles
  262. if (!empty($_POST['v_ssl_crt'])) {
  263. unlink($tmpdir."/".$v_domain.".crt");
  264. }
  265. if (!empty($_POST['v_ssl_key'])) {
  266. unlink($tmpdir."/".$v_domain.".key");
  267. }
  268. if (!empty($_POST['v_ssl_ca'])) {
  269. unlink($tmpdir."/".$v_domain.".ca");
  270. }
  271. rmdir($tmpdir);
  272. }
  273. }
  274. // Delete Lets Encrypt support
  275. if (($v_letsencrypt == 'yes') && (empty($_POST['v_letsencrypt']) || empty($_POST['v_ssl'])) && (empty($_SESSION['error_msg']))) {
  276. exec(HESTIA_CMD."v-delete-letsencrypt-domain ".$user." ".escapeshellarg($v_domain)." ' ' 'yes'", $output, $return_var);
  277. check_return_code($return_var, $output);
  278. unset($output);
  279. $v_ssl_crt = '';
  280. $v_ssl_key = '';
  281. $v_ssl_ca = '';
  282. $v_letsencrypt = 'no';
  283. $v_letsencrypt_deleted = 'yes';
  284. $v_ssl = 'no';
  285. $restart_mail = 'yes';
  286. }
  287. // Delete SSL certificate
  288. if (($v_ssl == 'yes') && (empty($_POST['v_ssl'])) && (empty($_SESSION['error_msg']))) {
  289. exec(HESTIA_CMD."v-delete-mail-domain-ssl ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  290. check_return_code($return_var, $output);
  291. unset($output);
  292. $v_ssl_crt = '';
  293. $v_ssl_key = '';
  294. $v_ssl_ca = '';
  295. $v_ssl = 'no';
  296. $restart_mail = 'yes';
  297. }
  298. // Add Lets Encrypt support
  299. if ((!empty($_POST['v_ssl'])) && ($v_letsencrypt == 'no') && (!empty($_POST['v_letsencrypt'])) && empty($_SESSION['error_msg'])) {
  300. exec(HESTIA_CMD."v-add-letsencrypt-domain ".$user." ".escapeshellarg($v_domain)." ' ' 'yes'", $output, $return_var);
  301. check_return_code($return_var, $output);
  302. unset($output);
  303. $v_letsencrypt = 'yes';
  304. $v_ssl = 'yes';
  305. $restart_mail = 'yes';
  306. }
  307. // Add SSL certificate
  308. if (($v_ssl == 'no') && (!empty($_POST['v_ssl'])) && (empty($v_letsencrypt_deleted)) && (empty($_SESSION['error_msg']))) {
  309. if (empty($_POST['v_ssl_crt'])) {
  310. $errors[] = 'ssl certificate';
  311. }
  312. if (empty($_POST['v_ssl_key'])) {
  313. $errors[] = 'ssl key';
  314. }
  315. if (!empty($errors[0])) {
  316. foreach ($errors as $i => $error) {
  317. if ($i == 0) {
  318. $error_msg = $error;
  319. } else {
  320. $error_msg = $error_msg.", ".$error;
  321. }
  322. }
  323. $_SESSION['error_msg'] = _('Field "%s" can not be blank.', $error_msg);
  324. } else {
  325. exec('mktemp -d', $mktemp_output, $return_var);
  326. $tmpdir = $mktemp_output[0];
  327. // Certificate
  328. if (!empty($_POST['v_ssl_crt'])) {
  329. $fp = fopen($tmpdir."/".$v_domain.".crt", 'w');
  330. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_crt']));
  331. fclose($fp);
  332. }
  333. // Key
  334. if (!empty($_POST['v_ssl_key'])) {
  335. $fp = fopen($tmpdir."/".$v_domain.".key", 'w');
  336. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_key']));
  337. fclose($fp);
  338. }
  339. // CA
  340. if (!empty($_POST['v_ssl_ca'])) {
  341. $fp = fopen($tmpdir."/".$v_domain.".ca", 'w');
  342. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_ca']));
  343. fclose($fp);
  344. }
  345. exec(HESTIA_CMD."v-add-mail-domain-ssl ".$user." ".escapeshellarg($v_domain)." ".$tmpdir." 'no'", $output, $return_var);
  346. check_return_code($return_var, $output);
  347. unset($output);
  348. $v_ssl = 'yes';
  349. $restart_web = 'yes';
  350. $restart_proxy = 'yes';
  351. exec(HESTIA_CMD."v-list-mail-domain-ssl ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  352. $ssl_str = json_decode(implode('', $output), true);
  353. unset($output);
  354. $v_ssl_crt = $ssl_str[$v_domain]['CRT'];
  355. $v_ssl_key = $ssl_str[$v_domain]['KEY'];
  356. $v_ssl_ca = $ssl_str[$v_domain]['CA'];
  357. $v_ssl_subject = $ssl_str[$v_domain]['SUBJECT'];
  358. $v_ssl_aliases = $ssl_str[$v_domain]['ALIASES'];
  359. $v_ssl_not_before = $ssl_str[$v_domain]['NOT_BEFORE'];
  360. $v_ssl_not_after = $ssl_str[$v_domain]['NOT_AFTER'];
  361. $v_ssl_signature = $ssl_str[$v_domain]['SIGNATURE'];
  362. $v_ssl_pub_key = $ssl_str[$v_domain]['PUB_KEY'];
  363. $v_ssl_issuer = $ssl_str[$v_domain]['ISSUER'];
  364. // Cleanup certificate tempfiles
  365. if (!empty($_POST['v_ssl_crt'])) {
  366. unlink($tmpdir."/".$v_domain.".crt");
  367. }
  368. if (!empty($_POST['v_ssl_key'])) {
  369. unlink($tmpdir."/".$v_domain.".key");
  370. }
  371. if (!empty($_POST['v_ssl_ca'])) {
  372. unlink($tmpdir."/".$v_domain.".ca");
  373. }
  374. rmdir($tmpdir);
  375. }
  376. }
  377. // Add SMTP Relay Support
  378. if (empty($_SESSION['error_msg'])) {
  379. if (isset($_POST['v_smtp_relay']) && (!empty($_POST['v_smtp_relay_host'])) && (!empty($_POST['v_smtp_relay_user']))) {
  380. if (($_POST['v_smtp_relay_host'] != $v_smtp_relay_host) ||
  381. ($_POST['v_smtp_relay_user'] != $v_smtp_relay_user) ||
  382. ($_POST['v_smtp_relay_port'] != $v_smtp_relay_port) ||
  383. (!empty($_POST['v_smtp_relay_pass']))) {
  384. if (!empty($_POST['v_smtp_relay_pass'])) {
  385. $v_smtp_relay = true;
  386. $v_smtp_relay_host = escapeshellarg($_POST['v_smtp_relay_host']);
  387. $v_smtp_relay_user = escapeshellarg($_POST['v_smtp_relay_user']);
  388. $v_smtp_relay_pass = escapeshellarg($_POST['v_smtp_relay_pass']);
  389. if (!empty($_POST['v_smtp_relay_port'])) {
  390. $v_smtp_relay_port = escapeshellarg($_POST['v_smtp_relay_port']);
  391. } else {
  392. $v_smtp_relay_port = '587';
  393. }
  394. exec(HESTIA_CMD."v-add-mail-domain-smtp-relay ".$v_username." ".escapeshellarg($v_domain)." ".$v_smtp_relay_host." ".$v_smtp_relay_user." ".$v_smtp_relay_pass." ".$v_smtp_relay_port, $output, $return_var);
  395. check_return_code($return_var, $output);
  396. unset($output);
  397. } else {
  398. $_SESSION['error_msg'] = _('SMTP Relay Password is required');
  399. }
  400. }
  401. }
  402. if ((!isset($_POST['v_smtp_relay'])) && ($v_smtp_relay == true)) {
  403. $v_smtp_relay = false;
  404. $v_smtp_relay_host = $v_smtp_relay_user = $v_smtp_relay_pass = $v_smtp_relay_port = '';
  405. exec(HESTIA_CMD."v-delete-mail-domain-smtp-relay ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  406. check_return_code($return_var, $output);
  407. unset($output);
  408. }
  409. }
  410. // Set success message
  411. if (empty($_SESSION['error_msg'])) {
  412. $_SESSION['ok_msg'] = _('Changes has been saved.');
  413. }
  414. }
  415. // Check POST request for mail account
  416. if ((!empty($_POST['save'])) && (!empty($_GET['domain'])) && (!empty($_GET['account']))) {
  417. // Check token
  418. verify_csrf($_POST);
  419. // Validate email
  420. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  421. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  422. $_SESSION['error_msg'] = _('Please enter valid email address.');
  423. }
  424. }
  425. $v_domain = $_POST['v_domain'];
  426. if (!in_array($v_domain, $user_domains)) {
  427. check_return_code(3, ["Unknown domain"]);
  428. }
  429. $v_account = $_POST['v_account'];
  430. $v_send_email = $_POST['v_send_email'];
  431. $v_credentials = $_POST['v_credentials'];
  432. // Change password
  433. if ((!empty($_POST['v_password'])) && (empty($_SESSION['error_msg']))) {
  434. if (!validate_password($_POST['v_password'])) {
  435. $_SESSION['error_msg'] = _('Password does not match the minimum requirements');
  436. } else {
  437. $v_password = tempnam("/tmp", "vst");
  438. $fp = fopen($v_password, "w");
  439. fwrite($fp, $_POST['v_password']."\n");
  440. fclose($fp);
  441. exec(HESTIA_CMD."v-change-mail-account-password ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_password, $output, $return_var);
  442. check_return_code($return_var, $output);
  443. unset($output);
  444. unlink($v_password);
  445. $v_password = escapeshellarg($_POST['v_password']);
  446. }
  447. }
  448. // Change quota
  449. if (($v_quota != $_POST['v_quota']) && (empty($_SESSION['error_msg']))) {
  450. if (empty($_POST['v_quota'])) {
  451. $v_quota = 0;
  452. } else {
  453. $v_quota = escapeshellarg($_POST['v_quota']);
  454. }
  455. exec(HESTIA_CMD."v-change-mail-account-quota ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_quota, $output, $return_var);
  456. check_return_code($return_var, $output);
  457. unset($output);
  458. }
  459. // Change account aliases
  460. if (empty($_SESSION['error_msg'])) {
  461. $waliases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  462. $waliases = preg_replace("/,/", " ", $waliases);
  463. $waliases = preg_replace('/\s+/', ' ', $waliases);
  464. $waliases = trim($waliases);
  465. $aliases = explode(" ", $waliases);
  466. $v_aliases = str_replace(' ', "\n", $waliases);
  467. $result = array_diff($valiases, $aliases);
  468. foreach ($result as $alias) {
  469. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  470. exec(HESTIA_CMD."v-delete-mail-account-alias ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($alias), $output, $return_var);
  471. check_return_code($return_var, $output);
  472. unset($output);
  473. }
  474. }
  475. $result = array_diff($aliases, $valiases);
  476. foreach ($result as $alias) {
  477. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  478. exec(HESTIA_CMD."v-add-mail-account-alias ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($alias), $output, $return_var);
  479. check_return_code($return_var, $output);
  480. unset($output);
  481. }
  482. }
  483. }
  484. // Change forwarders to :blackhole:
  485. if (empty($_SESSION['error_msg']) && !empty($_POST['v_blackhole'])) {
  486. foreach ($vfwd as $forward) {
  487. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  488. exec(HESTIA_CMD."v-delete-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  489. check_return_code($return_var, $output);
  490. unset($output);
  491. }
  492. exec(HESTIA_CMD."v-add-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." :blackhole:", $output, $return_var);
  493. check_return_code($return_var, $output);
  494. unset($output);
  495. $v_fwd = '';
  496. $v_blackhole = "yes";
  497. }
  498. }
  499. // Change forwarders
  500. if (empty($_SESSION['error_msg']) && empty($_POST['v_blackhole'])) {
  501. $wfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  502. $wfwd = preg_replace("/,/", " ", $wfwd);
  503. $wfwd = preg_replace('/\s+/', ' ', $wfwd);
  504. $wfwd = trim($wfwd);
  505. $fwd = explode(" ", $wfwd);
  506. $v_fwd = str_replace(' ', "\n", $wfwd);
  507. $result = array_diff($vfwd, $fwd);
  508. foreach ($result as $forward) {
  509. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  510. exec(HESTIA_CMD."v-delete-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  511. check_return_code($return_var, $output);
  512. unset($output);
  513. }
  514. }
  515. $result = array_diff($fwd, $vfwd);
  516. foreach ($result as $forward) {
  517. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  518. exec(HESTIA_CMD."v-add-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  519. check_return_code($return_var, $output);
  520. unset($output);
  521. }
  522. }
  523. $v_blackhole = "no";
  524. }
  525. // Delete FWD_ONLY flag
  526. if (($v_fwd_only == 'yes') && (empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  527. exec(HESTIA_CMD."v-delete-mail-account-fwd-only ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  528. check_return_code($return_var, $output);
  529. unset($output);
  530. $v_fwd_only = '';
  531. }
  532. // Add FWD_ONLY flag
  533. if (($v_fwd_only != 'yes') && (!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  534. exec(HESTIA_CMD."v-add-mail-account-fwd-only ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  535. check_return_code($return_var, $output);
  536. unset($output);
  537. $v_fwd_only = 'yes';
  538. }
  539. // Delete autoreply
  540. if (($v_autoreply == 'yes') && (empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  541. exec(HESTIA_CMD."v-delete-mail-account-autoreply ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  542. check_return_code($return_var, $output);
  543. unset($output);
  544. $v_autoreply = 'no';
  545. $v_autoreply_message = '';
  546. }
  547. // Add autoreply
  548. if ((!empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  549. if ($v_autoreply_message != str_replace("\r\n", "\n", $_POST['v_autoreply_message'])) {
  550. $v_autoreply_message = str_replace("\r\n", "\n", $_POST['v_autoreply_message']);
  551. $v_autoreply_message = escapeshellarg($v_autoreply_message);
  552. exec(HESTIA_CMD."v-add-mail-account-autoreply ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_autoreply_message, $output, $return_var);
  553. check_return_code($return_var, $output);
  554. unset($output);
  555. $v_autoreply = 'yes';
  556. $v_autoreply_message = $_POST['v_autoreply_message'];
  557. }
  558. }
  559. // Email login credentials
  560. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  561. $to = $v_send_email;
  562. $subject = _("Email Credentials");
  563. $hostname = exec('hostname');
  564. $from = "noreply@".$hostname;
  565. $from_name = _('Hestia Control Panel');
  566. $mailtext = $v_credentials;
  567. send_email($to, $subject, $mailtext, $from, $from_name);
  568. }
  569. // Set success message
  570. if (empty($_SESSION['error_msg'])) {
  571. $_SESSION['ok_msg'] = _('Changes has been saved.');
  572. }
  573. }
  574. // Render page
  575. if (empty($_GET['account'])) {
  576. // Display body for mail domain
  577. render_page($user, $TAB, 'edit_mail');
  578. } else {
  579. // Display body for mail account
  580. render_page($user, $TAB, 'edit_mail_acc');
  581. }
  582. // Flush session messages
  583. unset($_SESSION['error_msg']);
  584. unset($_SESSION['ok_msg']);