index.php 27 KB

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