index.php 27 KB

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