index.php 26 KB

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