index.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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($_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. // 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'])) unlink($tmpdir."/".$v_domain.".crt");
  262. if (!empty($_POST['v_ssl_key'])) unlink($tmpdir."/".$v_domain.".key");
  263. if (!empty($_POST['v_ssl_ca'])) unlink($tmpdir."/".$v_domain.".ca");
  264. rmdir($tmpdir);
  265. }
  266. }
  267. // Delete Lets Encrypt support
  268. if (( $v_letsencrypt == 'yes' ) && (empty($_POST['v_letsencrypt']) || empty($_POST['v_ssl'])) && (empty($_SESSION['error_msg']))) {
  269. exec (HESTIA_CMD."v-delete-letsencrypt-domain ".$user." ".escapeshellarg($v_domain)." ' ' 'yes'", $output, $return_var);
  270. check_return_code($return_var,$output);
  271. unset($output);
  272. $v_ssl_crt = '';
  273. $v_ssl_key = '';
  274. $v_ssl_ca = '';
  275. $v_letsencrypt = 'no';
  276. $v_letsencrypt_deleted = 'yes';
  277. $v_ssl = 'no';
  278. $restart_mail = 'yes';
  279. }
  280. // Delete SSL certificate
  281. if (( $v_ssl == 'yes' ) && (empty($_POST['v_ssl'])) && (empty($_SESSION['error_msg']))) {
  282. exec (HESTIA_CMD."v-delete-mail-domain-ssl ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  283. check_return_code($return_var,$output);
  284. unset($output);
  285. $v_ssl_crt = '';
  286. $v_ssl_key = '';
  287. $v_ssl_ca = '';
  288. $v_ssl = 'no';
  289. $restart_mail = 'yes';
  290. }
  291. // Add Lets Encrypt support
  292. if ((!empty($_POST['v_ssl'])) && ( $v_letsencrypt == 'no' ) && (!empty($_POST['v_letsencrypt'])) && empty($_SESSION['error_msg'])) {
  293. exec (HESTIA_CMD."v-add-letsencrypt-domain ".$user." ".escapeshellarg($v_domain)." ' ' 'yes'", $output, $return_var);
  294. check_return_code($return_var,$output);
  295. unset($output);
  296. $v_letsencrypt = 'yes';
  297. $v_ssl = 'yes';
  298. $restart_mail = 'yes';
  299. }
  300. // Add SSL certificate
  301. if (( $v_ssl == 'no' ) && (!empty($_POST['v_ssl'])) && (empty($v_letsencrypt_deleted)) && (empty($_SESSION['error_msg']))) {
  302. if (empty($_POST['v_ssl_crt'])) $errors[] = 'ssl certificate';
  303. if (empty($_POST['v_ssl_key'])) $errors[] = 'ssl key';
  304. if (!empty($errors[0])) {
  305. foreach ($errors as $i => $error) {
  306. if ( $i == 0 ) {
  307. $error_msg = $error;
  308. } else {
  309. $error_msg = $error_msg.", ".$error;
  310. }
  311. }
  312. $_SESSION['error_msg'] = _('Field "%s" can not be blank.',$error_msg);
  313. } else {
  314. exec ('mktemp -d', $mktemp_output, $return_var);
  315. $tmpdir = $mktemp_output[0];
  316. // Certificate
  317. if (!empty($_POST['v_ssl_crt'])) {
  318. $fp = fopen($tmpdir."/".$v_domain.".crt", 'w');
  319. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_crt']));
  320. fclose($fp);
  321. }
  322. // Key
  323. if (!empty($_POST['v_ssl_key'])) {
  324. $fp = fopen($tmpdir."/".$v_domain.".key", 'w');
  325. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_key']));
  326. fclose($fp);
  327. }
  328. // CA
  329. if (!empty($_POST['v_ssl_ca'])) {
  330. $fp = fopen($tmpdir."/".$v_domain.".ca", 'w');
  331. fwrite($fp, str_replace("\r\n", "\n", $_POST['v_ssl_ca']));
  332. fclose($fp);
  333. }
  334. exec (HESTIA_CMD."v-add-mail-domain-ssl ".$user." ".escapeshellarg($v_domain)." ".$tmpdir." 'no'", $output, $return_var);
  335. check_return_code($return_var,$output);
  336. unset($output);
  337. $v_ssl = 'yes';
  338. $restart_web = 'yes';
  339. $restart_proxy = 'yes';
  340. exec (HESTIA_CMD."v-list-mail-domain-ssl ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  341. $ssl_str = json_decode(implode('', $output), true);
  342. unset($output);
  343. $v_ssl_crt = $ssl_str[$v_domain]['CRT'];
  344. $v_ssl_key = $ssl_str[$v_domain]['KEY'];
  345. $v_ssl_ca = $ssl_str[$v_domain]['CA'];
  346. $v_ssl_subject = $ssl_str[$v_domain]['SUBJECT'];
  347. $v_ssl_aliases = $ssl_str[$v_domain]['ALIASES'];
  348. $v_ssl_not_before = $ssl_str[$v_domain]['NOT_BEFORE'];
  349. $v_ssl_not_after = $ssl_str[$v_domain]['NOT_AFTER'];
  350. $v_ssl_signature = $ssl_str[$v_domain]['SIGNATURE'];
  351. $v_ssl_pub_key = $ssl_str[$v_domain]['PUB_KEY'];
  352. $v_ssl_issuer = $ssl_str[$v_domain]['ISSUER'];
  353. // Cleanup certificate tempfiles
  354. if (!empty($_POST['v_ssl_crt'])) unlink($tmpdir."/".$v_domain.".crt");
  355. if (!empty($_POST['v_ssl_key'])) unlink($tmpdir."/".$v_domain.".key");
  356. if (!empty($_POST['v_ssl_ca'])) unlink($tmpdir."/".$v_domain.".ca");
  357. rmdir($tmpdir);
  358. }
  359. }
  360. // Add SMTP Relay Support
  361. if (empty($_SESSION['error_msg'])) {
  362. if (isset($_POST['v_smtp_relay']) && (!empty($_POST['v_smtp_relay_host'])) && (!empty($_POST['v_smtp_relay_user']))) {
  363. if (($_POST['v_smtp_relay_host'] != $v_smtp_relay_host) ||
  364. ($_POST['v_smtp_relay_user'] != $v_smtp_relay_user) ||
  365. ($_POST['v_smtp_relay_port'] != $v_smtp_relay_port) ||
  366. (!empty($_POST['v_smtp_relay_pass']))) {
  367. if (!empty($_POST['v_smtp_relay_pass'])) {
  368. $v_smtp_relay = true;
  369. $v_smtp_relay_host = escapeshellarg($_POST['v_smtp_relay_host']);
  370. $v_smtp_relay_user = escapeshellarg($_POST['v_smtp_relay_user']);
  371. $v_smtp_relay_pass = escapeshellarg($_POST['v_smtp_relay_pass']);
  372. if (!empty($_POST['v_smtp_relay_port'])) {
  373. $v_smtp_relay_port = escapeshellarg($_POST['v_smtp_relay_port']);
  374. } else {
  375. $v_smtp_relay_port = '587';
  376. }
  377. 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);
  378. check_return_code($return_var,$output);
  379. unset($output);
  380. } else {
  381. $_SESSION['error_msg'] = _('SMTP Relay Password is required');
  382. }
  383. }
  384. }
  385. if ((!isset($_POST['v_smtp_relay'])) && ($v_smtp_relay == true)) {
  386. $v_smtp_relay = false;
  387. $v_smtp_relay_host = $v_smtp_relay_user = $v_smtp_relay_pass = $v_smtp_relay_port = '';
  388. exec (HESTIA_CMD."v-delete-mail-domain-smtp-relay ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  389. check_return_code($return_var,$output);
  390. unset($output);
  391. }
  392. }
  393. // Set success message
  394. if (empty($_SESSION['error_msg'])) {
  395. $_SESSION['ok_msg'] = _('Changes has been saved.');
  396. }
  397. }
  398. // Check POST request for mail account
  399. if ((!empty($_POST['save'])) && (!empty($_GET['domain'])) && (!empty($_GET['account']))) {
  400. // Check token
  401. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  402. header('location: /login/');
  403. exit();
  404. }
  405. // Validate email
  406. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  407. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  408. $_SESSION['error_msg'] = _('Please enter valid email address.');
  409. }
  410. }
  411. $v_domain = $_POST['v_domain'];
  412. if(!in_array($v_domain, $user_domains)) {
  413. check_return_code(3, ["Unknown domain"]);
  414. }
  415. $v_account = $_POST['v_account'];
  416. $v_send_email = $_POST['v_send_email'];
  417. $v_credentials = $_POST['v_credentials'];
  418. // Change password
  419. if ((!empty($_POST['v_password'])) && (empty($_SESSION['error_msg']))) {
  420. if (!validate_password($_POST['v_password'])) {
  421. $_SESSION['error_msg'] = _('Password does not match the minimum requirements');
  422. }else{
  423. $v_password = tempnam("/tmp","vst");
  424. $fp = fopen($v_password, "w");
  425. fwrite($fp, $_POST['v_password']."\n");
  426. fclose($fp);
  427. exec (HESTIA_CMD."v-change-mail-account-password ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_password, $output, $return_var);
  428. check_return_code($return_var,$output);
  429. unset($output);
  430. unlink($v_password);
  431. $v_password = escapeshellarg($_POST['v_password']);
  432. }
  433. }
  434. // Change quota
  435. if (($v_quota != $_POST['v_quota']) && (empty($_SESSION['error_msg']))) {
  436. if (empty($_POST['v_quota'])) {
  437. $v_quota = 0;
  438. } else {
  439. $v_quota = escapeshellarg($_POST['v_quota']);
  440. }
  441. exec (HESTIA_CMD."v-change-mail-account-quota ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_quota, $output, $return_var);
  442. check_return_code($return_var,$output);
  443. unset($output);
  444. }
  445. // Change account aliases
  446. if (empty($_SESSION['error_msg'])) {
  447. $waliases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  448. $waliases = preg_replace("/,/", " ", $waliases);
  449. $waliases = preg_replace('/\s+/', ' ',$waliases);
  450. $waliases = trim($waliases);
  451. $aliases = explode(" ", $waliases);
  452. $v_aliases = str_replace(' ', "\n", $waliases);
  453. $result = array_diff($valiases, $aliases);
  454. foreach ($result as $alias) {
  455. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  456. exec (HESTIA_CMD."v-delete-mail-account-alias ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($alias), $output, $return_var);
  457. check_return_code($return_var,$output);
  458. unset($output);
  459. }
  460. }
  461. $result = array_diff($aliases, $valiases);
  462. foreach ($result as $alias) {
  463. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  464. exec (HESTIA_CMD."v-add-mail-account-alias ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($alias), $output, $return_var);
  465. check_return_code($return_var,$output);
  466. unset($output);
  467. }
  468. }
  469. }
  470. // Change forwarders to :blackhole:
  471. if (empty($_SESSION['error_msg']) && !empty($_POST['v_blackhole'])) {
  472. foreach ($vfwd as $forward) {
  473. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  474. exec (HESTIA_CMD."v-delete-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  475. check_return_code($return_var,$output);
  476. unset($output);
  477. }
  478. exec (HESTIA_CMD."v-add-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." :blackhole:", $output, $return_var);
  479. check_return_code($return_var,$output);
  480. unset($output);
  481. $v_fwd = '';
  482. $v_blackhole = "yes";
  483. }
  484. }
  485. // Change forwarders
  486. if (empty($_SESSION['error_msg']) && empty($_POST['v_blackhole'])) {
  487. $wfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  488. $wfwd = preg_replace("/,/", " ", $wfwd);
  489. $wfwd = preg_replace('/\s+/', ' ',$wfwd);
  490. $wfwd = trim($wfwd);
  491. $fwd = explode(" ", $wfwd);
  492. $v_fwd = str_replace(' ', "\n", $wfwd);
  493. $result = array_diff($vfwd, $fwd);
  494. foreach ($result as $forward) {
  495. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  496. exec (HESTIA_CMD."v-delete-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  497. check_return_code($return_var,$output);
  498. unset($output);
  499. }
  500. }
  501. $result = array_diff($fwd, $vfwd);
  502. foreach ($result as $forward) {
  503. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  504. exec (HESTIA_CMD."v-add-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  505. check_return_code($return_var,$output);
  506. unset($output);
  507. }
  508. }
  509. $v_blackhole = "no";
  510. }
  511. // Delete FWD_ONLY flag
  512. if (($v_fwd_only == 'yes') && (empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  513. exec (HESTIA_CMD."v-delete-mail-account-fwd-only ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  514. check_return_code($return_var,$output);
  515. unset($output);
  516. $v_fwd_only = '';
  517. }
  518. // Add FWD_ONLY flag
  519. if (($v_fwd_only != 'yes') && (!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  520. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  521. check_return_code($return_var,$output);
  522. unset($output);
  523. $v_fwd_only = 'yes';
  524. }
  525. // Delete autoreply
  526. if (($v_autoreply == 'yes') && (empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  527. exec (HESTIA_CMD."v-delete-mail-account-autoreply ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  528. check_return_code($return_var,$output);
  529. unset($output);
  530. $v_autoreply = 'no';
  531. $v_autoreply_message = '';
  532. }
  533. // Add autoreply
  534. if ((!empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  535. if ( $v_autoreply_message != str_replace("\r\n", "\n", $_POST['v_autoreply_message'])) {
  536. $v_autoreply_message = str_replace("\r\n", "\n", $_POST['v_autoreply_message']);
  537. $v_autoreply_message = escapeshellarg($v_autoreply_message);
  538. exec (HESTIA_CMD."v-add-mail-account-autoreply ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_autoreply_message, $output, $return_var);
  539. check_return_code($return_var,$output);
  540. unset($output);
  541. $v_autoreply = 'yes';
  542. $v_autoreply_message = $_POST['v_autoreply_message'];
  543. }
  544. }
  545. // Email login credentials
  546. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  547. $to = $v_send_email;
  548. $subject = _("Email Credentials");
  549. $hostname = exec('hostname');
  550. $from = sprintf(_('MAIL_FROM'), $hostname);
  551. $mailtext = $v_credentials;
  552. send_email($to, $subject, $mailtext, $from);
  553. }
  554. // Set success message
  555. if (empty($_SESSION['error_msg'])) {
  556. $_SESSION['ok_msg'] = _('Changes has been saved.');
  557. }
  558. }
  559. // Render page
  560. if (empty($_GET['account'])) {
  561. // Display body for mail domain
  562. render_page($user, $TAB, 'edit_mail');
  563. } else {
  564. // Display body for mail account
  565. render_page($user, $TAB, 'edit_mail_acc');
  566. }
  567. // Flush session messages
  568. unset($_SESSION['error_msg']);
  569. unset($_SESSION['ok_msg']);