index.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  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 = quoteshellarg($_GET["user"]);
  15. $user_plain = htmlentities($_GET["user"]);
  16. }
  17. $v_username = $user;
  18. // List mail domain
  19. if (!empty($_GET["domain"]) && empty($_GET["account"])) {
  20. $v_domain = $_GET["domain"];
  21. exec(HESTIA_CMD . "v-list-sys-webmail json", $output, $return_var);
  22. $webmail_clients = json_decode(implode("", $output), true);
  23. unset($output);
  24. exec(
  25. HESTIA_CMD . "v-list-mail-domain " . $user . " " . quoteshellarg($v_domain) . " json",
  26. $output,
  27. $return_var,
  28. );
  29. $data = json_decode(implode("", $output), true);
  30. check_return_code_redirect($return_var, $output, "/list/mail/");
  31. unset($output);
  32. // Parse domain
  33. $v_antispam = $data[$v_domain]["ANTISPAM"];
  34. $v_reject = $data[$v_domain]["REJECT"];
  35. $v_antivirus = $data[$v_domain]["ANTIVIRUS"];
  36. $v_dkim = $data[$v_domain]["DKIM"];
  37. $v_catchall = $data[$v_domain]["CATCHALL"];
  38. $v_rate = $data[$v_domain]["RATE_LIMIT"];
  39. $v_date = $data[$v_domain]["DATE"];
  40. $v_time = $data[$v_domain]["TIME"];
  41. $v_suspended = $data[$v_domain]["SUSPENDED"];
  42. $v_webmail_alias = $data[$v_domain]["WEBMAIL_ALIAS"];
  43. $v_webmail = $data[$v_domain]["WEBMAIL"];
  44. $v_smtp_relay = $data[$v_domain]["U_SMTP_RELAY"];
  45. $v_smtp_relay_host = $data[$v_domain]["U_SMTP_RELAY_HOST"];
  46. $v_smtp_relay_port = $data[$v_domain]["U_SMTP_RELAY_PORT"];
  47. $v_smtp_relay_user = $data[$v_domain]["U_SMTP_RELAY_USERNAME"];
  48. if ($v_suspended == "yes") {
  49. $v_status = "suspended";
  50. } else {
  51. $v_status = "active";
  52. }
  53. $v_ssl = $data[$v_domain]["SSL"];
  54. if (!empty($v_ssl)) {
  55. exec(
  56. HESTIA_CMD .
  57. "v-list-mail-domain-ssl " .
  58. $user .
  59. " " .
  60. quoteshellarg($v_domain) .
  61. " json",
  62. $output,
  63. $return_var,
  64. );
  65. $ssl_str = json_decode(implode("", $output), true);
  66. unset($output);
  67. $v_ssl_crt = $ssl_str[$v_domain]["CRT"];
  68. $v_ssl_key = $ssl_str[$v_domain]["KEY"];
  69. $v_ssl_ca = $ssl_str[$v_domain]["CA"];
  70. $v_ssl_subject = $ssl_str[$v_domain]["SUBJECT"];
  71. $v_ssl_aliases = $ssl_str[$v_domain]["ALIASES"];
  72. $v_ssl_not_before = $ssl_str[$v_domain]["NOT_BEFORE"];
  73. $v_ssl_not_after = $ssl_str[$v_domain]["NOT_AFTER"];
  74. $v_ssl_signature = $ssl_str[$v_domain]["SIGNATURE"];
  75. $v_ssl_pub_key = $ssl_str[$v_domain]["PUB_KEY"];
  76. $v_ssl_issuer = $ssl_str[$v_domain]["ISSUER"];
  77. }
  78. $v_letsencrypt = $data[$v_domain]["LETSENCRYPT"];
  79. if (empty($v_letsencrypt)) {
  80. $v_letsencrypt = "no";
  81. }
  82. }
  83. // List mail account
  84. if (!empty($_GET["domain"]) && !empty($_GET["account"])) {
  85. $v_domain = $_GET["domain"];
  86. $v_account = $_GET["account"];
  87. exec(
  88. HESTIA_CMD .
  89. "v-list-mail-account " .
  90. $user .
  91. " " .
  92. quoteshellarg($v_domain) .
  93. " " .
  94. quoteshellarg($v_account) .
  95. " 'json'",
  96. $output,
  97. $return_var,
  98. );
  99. $data = json_decode(implode("", $output), true);
  100. check_return_code_redirect($return_var, $output, "/list/mail/");
  101. unset($output);
  102. // Parse mail account
  103. $v_username = $user;
  104. $v_password = "";
  105. $v_aliases = str_replace(",", "\n", $data[$v_account]["ALIAS"]);
  106. $valiases = explode(",", $data[$v_account]["ALIAS"]);
  107. $v_fwd = str_replace(",", "\n", $data[$v_account]["FWD"]);
  108. if ($v_fwd == ":blackhole:") {
  109. $v_fwd = "";
  110. $v_blackhole = "yes";
  111. } else {
  112. $v_blackhole = "no";
  113. }
  114. $vfwd = explode(",", $data[$v_account]["FWD"]);
  115. $v_fwd_only = $data[$v_account]["FWD_ONLY"];
  116. $v_rate = $data[$v_account]["RATE_LIMIT"];
  117. $v_quota = $data[$v_account]["QUOTA"];
  118. $v_autoreply = $data[$v_account]["AUTOREPLY"];
  119. $v_suspended = $data[$v_account]["SUSPENDED"];
  120. $v_webmail_alias = $data[$v_account]["WEBMAIL_ALIAS"];
  121. if (empty($v_send_email)) {
  122. $v_send_email = "";
  123. }
  124. if ($v_suspended == "yes") {
  125. $v_status = "suspended";
  126. } else {
  127. $v_status = "active";
  128. }
  129. $v_date = $data[$v_account]["DATE"];
  130. $v_time = $data[$v_account]["TIME"];
  131. // Parse autoreply
  132. if ($v_autoreply == "yes") {
  133. exec(
  134. HESTIA_CMD .
  135. "v-list-mail-account-autoreply " .
  136. $user .
  137. " " .
  138. quoteshellarg($v_domain) .
  139. " " .
  140. quoteshellarg($v_account) .
  141. " json",
  142. $output,
  143. $return_var,
  144. );
  145. $autoreply_str = json_decode(implode("", $output), true);
  146. unset($output);
  147. $v_autoreply_message = $autoreply_str[$v_account]["MSG"];
  148. $v_autoreply_message = str_replace("\\n", "\n", $v_autoreply_message);
  149. } else {
  150. $v_autoreply_message = "";
  151. }
  152. }
  153. // Check POST request for mail domain
  154. if (!empty($_POST["save"]) && !empty($_GET["domain"]) && empty($_GET["account"])) {
  155. // Check token
  156. verify_csrf($_POST);
  157. exec(
  158. HESTIA_CMD . "v-list-mail-domain " . $user . " " . quoteshellarg($v_domain) . " json",
  159. $output,
  160. $return_var,
  161. );
  162. $data = json_decode(implode("", $output), true);
  163. check_return_code_redirect($return_var, $output, "/list/mail/");
  164. unset($output);
  165. // Delete antispam
  166. if ($v_antispam == "yes" && empty($_POST["v_antispam"]) && empty($_SESSION["error_msg"])) {
  167. exec(
  168. HESTIA_CMD .
  169. "v-delete-mail-domain-antispam " .
  170. $v_username .
  171. " " .
  172. quoteshellarg($v_domain),
  173. $output,
  174. $return_var,
  175. );
  176. check_return_code($return_var, $output);
  177. $v_antispam = "no";
  178. unset($output);
  179. }
  180. // Add antispam
  181. if ($v_antispam == "no" && !empty($_POST["v_antispam"]) && empty($_SESSION["error_msg"])) {
  182. exec(
  183. HESTIA_CMD .
  184. "v-add-mail-domain-antispam " .
  185. $v_username .
  186. " " .
  187. quoteshellarg($v_domain),
  188. $output,
  189. $return_var,
  190. );
  191. check_return_code($return_var, $output);
  192. $v_antispam = "yes";
  193. unset($output);
  194. }
  195. // Delete antivirus
  196. if ($v_antivirus == "yes" && empty($_POST["v_antivirus"]) && empty($_SESSION["error_msg"])) {
  197. exec(
  198. HESTIA_CMD .
  199. "v-delete-mail-domain-antivirus " .
  200. $v_username .
  201. " " .
  202. quoteshellarg($v_domain),
  203. $output,
  204. $return_var,
  205. );
  206. check_return_code($return_var, $output);
  207. $v_antivirus = "no";
  208. unset($output);
  209. }
  210. // Add antivirus
  211. if ($v_antivirus == "no" && !empty($_POST["v_antivirus"]) && empty($_SESSION["error_msg"])) {
  212. exec(
  213. HESTIA_CMD .
  214. "v-add-mail-domain-antivirus " .
  215. $v_username .
  216. " " .
  217. quoteshellarg($v_domain),
  218. $output,
  219. $return_var,
  220. );
  221. check_return_code($return_var, $output);
  222. $v_antivirus = "yes";
  223. unset($output);
  224. }
  225. // Delete DKIM
  226. if ($v_dkim == "yes" && empty($_POST["v_dkim"]) && empty($_SESSION["error_msg"])) {
  227. exec(
  228. HESTIA_CMD .
  229. "v-delete-mail-domain-dkim " .
  230. $v_username .
  231. " " .
  232. quoteshellarg($v_domain),
  233. $output,
  234. $return_var,
  235. );
  236. check_return_code($return_var, $output);
  237. $v_dkim = "no";
  238. unset($output);
  239. }
  240. // Add DKIM
  241. if ($v_dkim == "no" && !empty($_POST["v_dkim"]) && empty($_SESSION["error_msg"])) {
  242. exec(
  243. HESTIA_CMD . "v-add-mail-domain-dkim " . $v_username . " " . quoteshellarg($v_domain),
  244. $output,
  245. $return_var,
  246. );
  247. check_return_code($return_var, $output);
  248. $v_dkim = "yes";
  249. unset($output);
  250. }
  251. // Delete catchall
  252. if (!empty($v_catchall) && empty($_POST["v_catchall"]) && empty($_SESSION["error_msg"])) {
  253. exec(
  254. HESTIA_CMD .
  255. "v-delete-mail-domain-catchall " .
  256. $v_username .
  257. " " .
  258. quoteshellarg($v_domain),
  259. $output,
  260. $return_var,
  261. );
  262. check_return_code($return_var, $output);
  263. $v_catchall = "";
  264. unset($output);
  265. }
  266. // Change rate limit
  267. if (
  268. $v_rate != $_POST["v_rate"] &&
  269. empty($_SESSION["error_msg"]) &&
  270. $_SESSION["userContext"] == "admin"
  271. ) {
  272. if (empty($_POST["v_rate"])) {
  273. $v_rate = "system";
  274. } else {
  275. $v_rate = quoteshellarg($_POST["v_rate"]);
  276. }
  277. exec(
  278. HESTIA_CMD .
  279. "v-change-mail-domain-rate-limit " .
  280. $v_username .
  281. " " .
  282. quoteshellarg($v_domain) .
  283. " " .
  284. $v_rate,
  285. $output,
  286. $return_var,
  287. );
  288. check_return_code($return_var, $output);
  289. if ($v_rate == "system") {
  290. $v_rate = "";
  291. }
  292. unset($output);
  293. }
  294. if (!empty($_POST["v_reject"]) && $v_antispam == "yes" && $v_reject != "yes") {
  295. exec(
  296. HESTIA_CMD . "v-add-mail-domain-reject " . $user . " " . $v_domain . " yes",
  297. $output,
  298. $return_var,
  299. );
  300. check_return_code($return_var, $output);
  301. $v_reject = "yes";
  302. unset($output);
  303. }
  304. if (empty($_POST["v_reject"]) && $v_reject == "yes") {
  305. exec(
  306. HESTIA_CMD . "v-delete-mail-domain-reject " . $user . " " . $v_domain . " yes",
  307. $output,
  308. $return_var,
  309. );
  310. check_return_code($return_var, $output);
  311. $v_reject = "";
  312. unset($output);
  313. }
  314. // Change catchall address
  315. if (!empty($v_catchall) && !empty($_POST["v_catchall"]) && empty($_SESSION["error_msg"])) {
  316. if ($v_catchall != $_POST["v_catchall"]) {
  317. $v_catchall = quoteshellarg($_POST["v_catchall"]);
  318. exec(
  319. HESTIA_CMD .
  320. "v-change-mail-domain-catchall " .
  321. $v_username .
  322. " " .
  323. quoteshellarg($v_domain) .
  324. " " .
  325. $v_catchall,
  326. $output,
  327. $return_var,
  328. );
  329. check_return_code($return_var, $output);
  330. unset($output);
  331. }
  332. }
  333. // Add catchall
  334. if (empty($v_catchall) && !empty($_POST["v_catchall"]) && empty($_SESSION["error_msg"])) {
  335. $v_catchall = quoteshellarg($_POST["v_catchall"]);
  336. exec(
  337. HESTIA_CMD .
  338. "v-add-mail-domain-catchall " .
  339. $v_username .
  340. " " .
  341. quoteshellarg($v_domain) .
  342. " " .
  343. $v_catchall,
  344. $output,
  345. $return_var,
  346. );
  347. check_return_code($return_var, $output);
  348. unset($output);
  349. }
  350. if (!empty($_SESSION["IMAP_SYSTEM"]) && !empty($_SESSION["WEBMAIL_SYSTEM"])) {
  351. if (empty($_SESSION["error_msg"])) {
  352. if (!empty($_POST["v_webmail"])) {
  353. $v_webmail = quoteshellarg($_POST["v_webmail"]);
  354. exec(
  355. HESTIA_CMD .
  356. "v-add-mail-domain-webmail " .
  357. $user .
  358. " " .
  359. $v_domain .
  360. " " .
  361. $v_webmail .
  362. " yes",
  363. $output,
  364. $return_var,
  365. );
  366. check_return_code($return_var, $output);
  367. unset($output);
  368. }
  369. }
  370. }
  371. if (!empty($_SESSION["IMAP_SYSTEM"]) && !empty($_SESSION["WEBMAIL_SYSTEM"])) {
  372. if (empty($_POST["v_webmail"])) {
  373. if (empty($_SESSION["error_msg"])) {
  374. exec(
  375. HESTIA_CMD . "v-delete-mail-domain-webmail " . $user . " " . $v_domain . " yes",
  376. $output,
  377. $return_var,
  378. );
  379. check_return_code($return_var, $output);
  380. $v_webmail = "";
  381. unset($output);
  382. }
  383. }
  384. }
  385. // Change SSL certificate
  386. if (
  387. $v_letsencrypt == "no" &&
  388. empty($_POST["v_letsencrypt"]) &&
  389. $v_ssl == "yes" &&
  390. !empty($_POST["v_ssl"]) &&
  391. empty($_SESSION["error_msg"])
  392. ) {
  393. if (
  394. $v_ssl_crt != str_replace("\r\n", "\n", $_POST["v_ssl_crt"]) ||
  395. $v_ssl_key != str_replace("\r\n", "\n", $_POST["v_ssl_key"]) ||
  396. $v_ssl_ca != str_replace("\r\n", "\n", $_POST["v_ssl_ca"])
  397. ) {
  398. exec("mktemp -d", $mktemp_output, $return_var);
  399. $tmpdir = $mktemp_output[0];
  400. // Certificate
  401. if (!empty($_POST["v_ssl_crt"])) {
  402. $fp = fopen($tmpdir . "/" . $v_domain . ".crt", "w");
  403. fwrite($fp, str_replace("\r\n", "\n", $_POST["v_ssl_crt"]));
  404. fwrite($fp, "\n");
  405. fclose($fp);
  406. }
  407. // Key
  408. if (!empty($_POST["v_ssl_key"])) {
  409. $fp = fopen($tmpdir . "/" . $v_domain . ".key", "w");
  410. fwrite($fp, str_replace("\r\n", "\n", $_POST["v_ssl_key"]));
  411. fwrite($fp, "\n");
  412. fclose($fp);
  413. }
  414. // CA
  415. if (!empty($_POST["v_ssl_ca"])) {
  416. $fp = fopen($tmpdir . "/" . $v_domain . ".ca", "w");
  417. fwrite($fp, str_replace("\r\n", "\n", $_POST["v_ssl_ca"]));
  418. fwrite($fp, "\n");
  419. fclose($fp);
  420. }
  421. exec(
  422. HESTIA_CMD .
  423. "v-change-mail-domain-sslcert " .
  424. $user .
  425. " " .
  426. quoteshellarg($v_domain) .
  427. " " .
  428. $tmpdir .
  429. " 'no'",
  430. $output,
  431. $return_var,
  432. );
  433. check_return_code($return_var, $output);
  434. unset($output);
  435. $restart_web = "yes";
  436. $restart_proxy = "yes";
  437. exec(
  438. HESTIA_CMD .
  439. "v-list-mail-domain-ssl " .
  440. $user .
  441. " " .
  442. quoteshellarg($v_domain) .
  443. " json",
  444. $output,
  445. $return_var,
  446. );
  447. $ssl_str = json_decode(implode("", $output), true);
  448. unset($output);
  449. $v_ssl_crt = $ssl_str[$v_domain]["CRT"];
  450. $v_ssl_key = $ssl_str[$v_domain]["KEY"];
  451. $v_ssl_ca = $ssl_str[$v_domain]["CA"];
  452. $v_ssl_subject = $ssl_str[$v_domain]["SUBJECT"];
  453. $v_ssl_aliases = $ssl_str[$v_domain]["ALIASES"];
  454. $v_ssl_not_before = $ssl_str[$v_domain]["NOT_BEFORE"];
  455. $v_ssl_not_after = $ssl_str[$v_domain]["NOT_AFTER"];
  456. $v_ssl_signature = $ssl_str[$v_domain]["SIGNATURE"];
  457. $v_ssl_pub_key = $ssl_str[$v_domain]["PUB_KEY"];
  458. $v_ssl_issuer = $ssl_str[$v_domain]["ISSUER"];
  459. // Cleanup certificate tempfiles
  460. if (!empty($_POST["v_ssl_crt"])) {
  461. unlink($tmpdir . "/" . $v_domain . ".crt");
  462. }
  463. if (!empty($_POST["v_ssl_key"])) {
  464. unlink($tmpdir . "/" . $v_domain . ".key");
  465. }
  466. if (!empty($_POST["v_ssl_ca"])) {
  467. unlink($tmpdir . "/" . $v_domain . ".ca");
  468. }
  469. rmdir($tmpdir);
  470. }
  471. }
  472. // Delete Lets Encrypt support
  473. if (
  474. $v_letsencrypt == "yes" &&
  475. (empty($_POST["v_letsencrypt"]) || empty($_POST["v_ssl"])) &&
  476. empty($_SESSION["error_msg"])
  477. ) {
  478. exec(
  479. HESTIA_CMD .
  480. "v-delete-letsencrypt-domain " .
  481. $user .
  482. " " .
  483. quoteshellarg($v_domain) .
  484. " '' 'yes'",
  485. $output,
  486. $return_var,
  487. );
  488. check_return_code($return_var, $output);
  489. unset($output);
  490. $v_ssl_crt = "";
  491. $v_ssl_key = "";
  492. $v_ssl_ca = "";
  493. $v_letsencrypt = "no";
  494. $v_letsencrypt_deleted = "yes";
  495. $v_ssl = "no";
  496. $restart_mail = "yes";
  497. }
  498. // Delete SSL certificate
  499. if ($v_ssl == "yes" && empty($_POST["v_ssl"]) && empty($_SESSION["error_msg"])) {
  500. exec(
  501. HESTIA_CMD . "v-delete-mail-domain-ssl " . $v_username . " " . quoteshellarg($v_domain),
  502. $output,
  503. $return_var,
  504. );
  505. check_return_code($return_var, $output);
  506. unset($output);
  507. $v_ssl_crt = "";
  508. $v_ssl_key = "";
  509. $v_ssl_ca = "";
  510. $v_ssl = "no";
  511. $restart_mail = "yes";
  512. }
  513. // Add Lets Encrypt support
  514. if (
  515. !empty($_POST["v_ssl"]) &&
  516. $v_letsencrypt == "no" &&
  517. !empty($_POST["v_letsencrypt"]) &&
  518. empty($_SESSION["error_msg"])
  519. ) {
  520. exec(
  521. HESTIA_CMD .
  522. "v-add-letsencrypt-domain " .
  523. $user .
  524. " " .
  525. quoteshellarg($v_domain) .
  526. " ' ' 'yes'",
  527. $output,
  528. $return_var,
  529. );
  530. check_return_code($return_var, $output);
  531. unset($output);
  532. $v_letsencrypt = "yes";
  533. $v_ssl = "yes";
  534. $restart_mail = "yes";
  535. }
  536. // Add SSL certificate
  537. if (
  538. $v_ssl == "no" &&
  539. !empty($_POST["v_ssl"]) &&
  540. empty($v_letsencrypt_deleted) &&
  541. empty($_SESSION["error_msg"])
  542. ) {
  543. if (empty($_POST["v_ssl_crt"])) {
  544. $errors[] = "ssl certificate";
  545. }
  546. if (empty($_POST["v_ssl_key"])) {
  547. $errors[] = "ssl key";
  548. }
  549. if (!empty($errors[0])) {
  550. foreach ($errors as $i => $error) {
  551. if ($i == 0) {
  552. $error_msg = $error;
  553. } else {
  554. $error_msg = $error_msg . ", " . $error;
  555. }
  556. }
  557. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  558. } else {
  559. exec("mktemp -d", $mktemp_output, $return_var);
  560. $tmpdir = $mktemp_output[0];
  561. // Certificate
  562. if (!empty($_POST["v_ssl_crt"])) {
  563. $fp = fopen($tmpdir . "/" . $v_domain . ".crt", "w");
  564. fwrite($fp, str_replace("\r\n", "\n", $_POST["v_ssl_crt"]));
  565. fclose($fp);
  566. }
  567. // Key
  568. if (!empty($_POST["v_ssl_key"])) {
  569. $fp = fopen($tmpdir . "/" . $v_domain . ".key", "w");
  570. fwrite($fp, str_replace("\r\n", "\n", $_POST["v_ssl_key"]));
  571. fclose($fp);
  572. }
  573. // CA
  574. if (!empty($_POST["v_ssl_ca"])) {
  575. $fp = fopen($tmpdir . "/" . $v_domain . ".ca", "w");
  576. fwrite($fp, str_replace("\r\n", "\n", $_POST["v_ssl_ca"]));
  577. fclose($fp);
  578. }
  579. exec(
  580. HESTIA_CMD .
  581. "v-add-mail-domain-ssl " .
  582. $user .
  583. " " .
  584. quoteshellarg($v_domain) .
  585. " " .
  586. $tmpdir .
  587. " 'no'",
  588. $output,
  589. $return_var,
  590. );
  591. check_return_code($return_var, $output);
  592. unset($output);
  593. $v_ssl = "yes";
  594. $restart_web = "yes";
  595. $restart_proxy = "yes";
  596. exec(
  597. HESTIA_CMD .
  598. "v-list-mail-domain-ssl " .
  599. $user .
  600. " " .
  601. quoteshellarg($v_domain) .
  602. " json",
  603. $output,
  604. $return_var,
  605. );
  606. $ssl_str = json_decode(implode("", $output), true);
  607. unset($output);
  608. $v_ssl_crt = $ssl_str[$v_domain]["CRT"];
  609. $v_ssl_key = $ssl_str[$v_domain]["KEY"];
  610. $v_ssl_ca = $ssl_str[$v_domain]["CA"];
  611. $v_ssl_subject = $ssl_str[$v_domain]["SUBJECT"];
  612. $v_ssl_aliases = $ssl_str[$v_domain]["ALIASES"];
  613. $v_ssl_not_before = $ssl_str[$v_domain]["NOT_BEFORE"];
  614. $v_ssl_not_after = $ssl_str[$v_domain]["NOT_AFTER"];
  615. $v_ssl_signature = $ssl_str[$v_domain]["SIGNATURE"];
  616. $v_ssl_pub_key = $ssl_str[$v_domain]["PUB_KEY"];
  617. $v_ssl_issuer = $ssl_str[$v_domain]["ISSUER"];
  618. // Cleanup certificate tempfiles
  619. if (!empty($_POST["v_ssl_crt"])) {
  620. unlink($tmpdir . "/" . $v_domain . ".crt");
  621. }
  622. if (!empty($_POST["v_ssl_key"])) {
  623. unlink($tmpdir . "/" . $v_domain . ".key");
  624. }
  625. if (!empty($_POST["v_ssl_ca"])) {
  626. unlink($tmpdir . "/" . $v_domain . ".ca");
  627. }
  628. rmdir($tmpdir);
  629. }
  630. }
  631. // Add SMTP Relay Support
  632. if (empty($_SESSION["error_msg"])) {
  633. if (isset($_POST["v_smtp_relay"]) && !empty($_POST["v_smtp_relay_host"])) {
  634. if (
  635. $_POST["v_smtp_relay_host"] != $v_smtp_relay_host ||
  636. $_POST["v_smtp_relay_user"] != $v_smtp_relay_user ||
  637. $_POST["v_smtp_relay_port"] != $v_smtp_relay_port
  638. ) {
  639. $v_smtp_relay = true;
  640. $v_smtp_relay_host = quoteshellarg($_POST["v_smtp_relay_host"]);
  641. $v_smtp_relay_user = quoteshellarg($_POST["v_smtp_relay_user"]);
  642. $v_smtp_relay_pass = quoteshellarg($_POST["v_smtp_relay_pass"]);
  643. if (!empty($_POST["v_smtp_relay_port"])) {
  644. $v_smtp_relay_port = quoteshellarg($_POST["v_smtp_relay_port"]);
  645. } else {
  646. $v_smtp_relay_port = "587";
  647. }
  648. exec(
  649. HESTIA_CMD .
  650. "v-add-mail-domain-smtp-relay " .
  651. $v_username .
  652. " " .
  653. quoteshellarg($v_domain) .
  654. " " .
  655. $v_smtp_relay_host .
  656. " '" .
  657. $v_smtp_relay_user .
  658. "' '" .
  659. $v_smtp_relay_pass .
  660. "' " .
  661. $v_smtp_relay_port,
  662. $output,
  663. $return_var,
  664. );
  665. check_return_code($return_var, $output);
  666. unset($output);
  667. }
  668. }
  669. if (!isset($_POST["v_smtp_relay"]) && $v_smtp_relay == true) {
  670. $v_smtp_relay = false;
  671. $v_smtp_relay_host = $v_smtp_relay_user = $v_smtp_relay_pass = $v_smtp_relay_port = "";
  672. exec(
  673. HESTIA_CMD .
  674. "v-delete-mail-domain-smtp-relay " .
  675. $v_username .
  676. " " .
  677. quoteshellarg($v_domain),
  678. $output,
  679. $return_var,
  680. );
  681. check_return_code($return_var, $output);
  682. unset($output);
  683. }
  684. }
  685. // Set success message
  686. if (empty($_SESSION["error_msg"])) {
  687. $_SESSION["ok_msg"] = _("Changes have been saved.");
  688. }
  689. }
  690. // Check POST request for mail account
  691. if (!empty($_POST["save"]) && !empty($_GET["domain"]) && !empty($_GET["account"])) {
  692. // Check token
  693. verify_csrf($_POST);
  694. // Validate email
  695. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  696. if (!filter_var($_POST["v_send_email"], FILTER_VALIDATE_EMAIL)) {
  697. $_SESSION["error_msg"] = _("Please enter a valid email address.");
  698. }
  699. }
  700. $v_account = $_POST["v_account"];
  701. $v_send_email = $_POST["v_send_email"];
  702. $v_credentials = $_POST["v_credentials"];
  703. exec(
  704. HESTIA_CMD .
  705. "v-list-mail-account " .
  706. $user .
  707. " " .
  708. quoteshellarg($v_domain) .
  709. " " .
  710. quoteshellarg($v_account) .
  711. " json",
  712. $output,
  713. $return_var,
  714. );
  715. $data = json_decode(implode("", $output), true);
  716. check_return_code_redirect($return_var, $output, "/list/mail/");
  717. unset($output);
  718. // Change password
  719. if (!empty($_POST["v_password"]) && empty($_SESSION["error_msg"])) {
  720. if (!validate_password($_POST["v_password"])) {
  721. $_SESSION["error_msg"] = _("Password does not match the minimum requirements.");
  722. } else {
  723. $v_password = tempnam("/tmp", "vst");
  724. $fp = fopen($v_password, "w");
  725. fwrite($fp, $_POST["v_password"] . "\n");
  726. fclose($fp);
  727. exec(
  728. HESTIA_CMD .
  729. "v-change-mail-account-password " .
  730. $v_username .
  731. " " .
  732. quoteshellarg($v_domain) .
  733. " " .
  734. quoteshellarg($v_account) .
  735. " " .
  736. $v_password,
  737. $output,
  738. $return_var,
  739. );
  740. check_return_code($return_var, $output);
  741. unset($output);
  742. unlink($v_password);
  743. $v_password = quoteshellarg($_POST["v_password"]);
  744. }
  745. }
  746. // Change quota
  747. if ($v_quota != $_POST["v_quota"] && empty($_SESSION["error_msg"])) {
  748. if (empty($_POST["v_quota"])) {
  749. $v_quota = 0;
  750. } else {
  751. $v_quota = quoteshellarg($_POST["v_quota"]);
  752. }
  753. exec(
  754. HESTIA_CMD .
  755. "v-change-mail-account-quota " .
  756. $v_username .
  757. " " .
  758. quoteshellarg($v_domain) .
  759. " " .
  760. quoteshellarg($v_account) .
  761. " " .
  762. $v_quota,
  763. $output,
  764. $return_var,
  765. );
  766. check_return_code($return_var, $output);
  767. unset($output);
  768. }
  769. // Change rate limit
  770. if (
  771. $v_rate != $_POST["v_rate"] &&
  772. empty($_SESSION["error_msg"]) &&
  773. $_SESSION["userContext"] == "admin"
  774. ) {
  775. if (empty($_POST["v_rate"])) {
  776. $v_rate = "system";
  777. } else {
  778. $v_rate = quoteshellarg($_POST["v_rate"]);
  779. }
  780. exec(
  781. HESTIA_CMD .
  782. "v-change-mail-account-rate-limit " .
  783. $v_username .
  784. " " .
  785. quoteshellarg($v_domain) .
  786. " " .
  787. quoteshellarg($v_account) .
  788. " " .
  789. $v_rate,
  790. $output,
  791. $return_var,
  792. );
  793. check_return_code($return_var, $output);
  794. if ($v_rate == "system") {
  795. $v_rate = "";
  796. }
  797. unset($output);
  798. }
  799. // Change account aliases
  800. if (empty($_SESSION["error_msg"])) {
  801. $waliases = preg_replace("/\n/", " ", $_POST["v_aliases"]);
  802. $waliases = preg_replace("/,/", " ", $waliases);
  803. $waliases = preg_replace("/\s+/", " ", $waliases);
  804. $waliases = trim($waliases);
  805. $aliases = explode(" ", $waliases);
  806. $v_aliases = str_replace(" ", "\n", $waliases);
  807. $result = array_diff($valiases, $aliases);
  808. foreach ($result as $alias) {
  809. if (empty($_SESSION["error_msg"]) && !empty($alias)) {
  810. exec(
  811. HESTIA_CMD .
  812. "v-delete-mail-account-alias " .
  813. $v_username .
  814. " " .
  815. quoteshellarg($v_domain) .
  816. " " .
  817. quoteshellarg($v_account) .
  818. " " .
  819. quoteshellarg($alias),
  820. $output,
  821. $return_var,
  822. );
  823. check_return_code($return_var, $output);
  824. unset($output);
  825. }
  826. }
  827. $result = array_diff($aliases, $valiases);
  828. foreach ($result as $alias) {
  829. if (empty($_SESSION["error_msg"]) && !empty($alias)) {
  830. exec(
  831. HESTIA_CMD .
  832. "v-add-mail-account-alias " .
  833. $v_username .
  834. " " .
  835. quoteshellarg($v_domain) .
  836. " " .
  837. quoteshellarg($v_account) .
  838. " " .
  839. quoteshellarg($alias),
  840. $output,
  841. $return_var,
  842. );
  843. check_return_code($return_var, $output);
  844. unset($output);
  845. }
  846. }
  847. }
  848. // Change forwarders to :blackhole:
  849. if (empty($_SESSION["error_msg"]) && !empty($_POST["v_blackhole"])) {
  850. foreach ($vfwd as $forward) {
  851. if (empty($_SESSION["error_msg"]) && !empty($forward)) {
  852. exec(
  853. HESTIA_CMD .
  854. "v-delete-mail-account-forward " .
  855. $v_username .
  856. " " .
  857. quoteshellarg($v_domain) .
  858. " " .
  859. quoteshellarg($v_account) .
  860. " " .
  861. quoteshellarg($forward),
  862. $output,
  863. $return_var,
  864. );
  865. check_return_code($return_var, $output);
  866. unset($output);
  867. }
  868. exec(
  869. HESTIA_CMD .
  870. "v-add-mail-account-forward " .
  871. $v_username .
  872. " " .
  873. quoteshellarg($v_domain) .
  874. " " .
  875. quoteshellarg($v_account) .
  876. " :blackhole:",
  877. $output,
  878. $return_var,
  879. );
  880. check_return_code($return_var, $output);
  881. unset($output);
  882. $v_fwd = "";
  883. $v_blackhole = "yes";
  884. }
  885. }
  886. // Change forwarders
  887. if (empty($_SESSION["error_msg"]) && empty($_POST["v_blackhole"])) {
  888. $wfwd = preg_replace("/\n/", " ", $_POST["v_fwd"]);
  889. $wfwd = preg_replace("/,/", " ", $wfwd);
  890. $wfwd = preg_replace("/\s+/", " ", $wfwd);
  891. $wfwd = trim($wfwd);
  892. $fwd = explode(" ", $wfwd);
  893. $v_fwd = str_replace(" ", "\n", $wfwd);
  894. $result = array_diff($vfwd, $fwd);
  895. foreach ($result as $forward) {
  896. if (empty($_SESSION["error_msg"]) && !empty($forward)) {
  897. exec(
  898. HESTIA_CMD .
  899. "v-delete-mail-account-forward " .
  900. $v_username .
  901. " " .
  902. quoteshellarg($v_domain) .
  903. " " .
  904. quoteshellarg($v_account) .
  905. " " .
  906. quoteshellarg($forward),
  907. $output,
  908. $return_var,
  909. );
  910. check_return_code($return_var, $output);
  911. unset($output);
  912. }
  913. }
  914. $result = array_diff($fwd, $vfwd);
  915. foreach ($result as $forward) {
  916. if (empty($_SESSION["error_msg"]) && !empty($forward)) {
  917. exec(
  918. HESTIA_CMD .
  919. "v-add-mail-account-forward " .
  920. $v_username .
  921. " " .
  922. quoteshellarg($v_domain) .
  923. " " .
  924. quoteshellarg($v_account) .
  925. " " .
  926. quoteshellarg($forward),
  927. $output,
  928. $return_var,
  929. );
  930. check_return_code($return_var, $output);
  931. unset($output);
  932. }
  933. }
  934. $v_blackhole = "no";
  935. }
  936. // Delete FWD_ONLY flag
  937. if ($v_fwd_only == "yes" && empty($_POST["v_fwd_only"]) && empty($_SESSION["error_msg"])) {
  938. exec(
  939. HESTIA_CMD .
  940. "v-delete-mail-account-fwd-only " .
  941. $v_username .
  942. " " .
  943. quoteshellarg($v_domain) .
  944. " " .
  945. quoteshellarg($v_account),
  946. $output,
  947. $return_var,
  948. );
  949. check_return_code($return_var, $output);
  950. unset($output);
  951. $v_fwd_only = "";
  952. }
  953. // Add FWD_ONLY flag
  954. if ($v_fwd_only != "yes" && !empty($_POST["v_fwd_only"]) && empty($_SESSION["error_msg"])) {
  955. exec(
  956. HESTIA_CMD .
  957. "v-add-mail-account-fwd-only " .
  958. $v_username .
  959. " " .
  960. quoteshellarg($v_domain) .
  961. " " .
  962. quoteshellarg($v_account),
  963. $output,
  964. $return_var,
  965. );
  966. check_return_code($return_var, $output);
  967. unset($output);
  968. $v_fwd_only = "yes";
  969. }
  970. // Delete autoreply
  971. if ($v_autoreply == "yes" && empty($_POST["v_autoreply"]) && empty($_SESSION["error_msg"])) {
  972. exec(
  973. HESTIA_CMD .
  974. "v-delete-mail-account-autoreply " .
  975. $v_username .
  976. " " .
  977. quoteshellarg($v_domain) .
  978. " " .
  979. quoteshellarg($v_account),
  980. $output,
  981. $return_var,
  982. );
  983. check_return_code($return_var, $output);
  984. unset($output);
  985. $v_autoreply = "no";
  986. $v_autoreply_message = "";
  987. }
  988. // Add autoreply
  989. if (!empty($_POST["v_autoreply"]) && empty($_SESSION["error_msg"])) {
  990. if ($v_autoreply_message != str_replace("\r\n", "\n", $_POST["v_autoreply_message"])) {
  991. $v_autoreply_message = str_replace("\r\n", "\n", $_POST["v_autoreply_message"]);
  992. $v_autoreply_message = quoteshellarg($v_autoreply_message);
  993. exec(
  994. HESTIA_CMD .
  995. "v-add-mail-account-autoreply " .
  996. $v_username .
  997. " " .
  998. quoteshellarg($v_domain) .
  999. " " .
  1000. quoteshellarg($v_account) .
  1001. " " .
  1002. $v_autoreply_message,
  1003. $output,
  1004. $return_var,
  1005. );
  1006. check_return_code($return_var, $output);
  1007. unset($output);
  1008. $v_autoreply = "yes";
  1009. $v_autoreply_message = $_POST["v_autoreply_message"];
  1010. }
  1011. }
  1012. $webmail = "http://" . $hostname . "/" . $v_webmail_alias . "/";
  1013. if (!empty($_SESSION["WEBMAIL_ALIAS"])) {
  1014. $webmail = $_SESSION["WEBMAIL_ALIAS"];
  1015. }
  1016. // Email login credentials
  1017. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  1018. $to = $_POST["v_send_email"];
  1019. $template = get_email_template("email_credentials", $_SESSION["language"]);
  1020. if (!empty($template)) {
  1021. preg_match("/<subject>(.*?)<\/subject>/si", $template, $matches);
  1022. $subject = $matches[1];
  1023. $subject = str_replace(
  1024. ["{{hostname}}", "{{appname}}", "{{account}}", "{{domain}}"],
  1025. [
  1026. get_hostname(),
  1027. $_SESSION["APP_NAME"],
  1028. htmlentities(strtolower($_POST["v_account"])),
  1029. htmlentities($_POST["v_domain"]),
  1030. ],
  1031. $subject,
  1032. );
  1033. $template = str_replace($matches[0], "", $template);
  1034. } else {
  1035. $template = _(
  1036. "Mail account has been created.\n" .
  1037. "\n" .
  1038. "Common Account Settings:\n" .
  1039. "Username: {{account}}@{{domain}}\n" .
  1040. "Password: {{password}}\n" .
  1041. "Webmail: {{webmail}}\n" .
  1042. "Hostname: {{hostname}}\n" .
  1043. "\n" .
  1044. "IMAP Settings\n" .
  1045. "Authentication: Normal Password\n" .
  1046. "SSL/TLS: Port 993\n" .
  1047. "STARTTLS: Port 143\n" .
  1048. "No encryption: Port 143\n" .
  1049. "\n" .
  1050. "POP3 Settings\n" .
  1051. "Authentication: Normal Password\n" .
  1052. "SSL/TLS: Port 995\n" .
  1053. "STARTTLS: Port 110\n" .
  1054. "No encryption: Port 110\n" .
  1055. "\n" .
  1056. "SMTP Settings\n" .
  1057. "Authentication: Normal Password\n" .
  1058. "SSL/TLS: Port 465\n" .
  1059. "STARTTLS: Port 587\n" .
  1060. "No encryption: Port 25\n" .
  1061. "\n" .
  1062. "Best regards,\n" .
  1063. "\n" .
  1064. "--\n" .
  1065. "{{appname}}",
  1066. );
  1067. }
  1068. if (empty($subject)) {
  1069. $subject = str_replace(
  1070. ["{{subject}}", "{{hostname}}", "{{appname}}"],
  1071. [
  1072. sprintf(
  1073. _("Email Credentials: %s@%s"),
  1074. htmlentities(strtolower($_POST["v_account"])),
  1075. htmlentities($_POST["v_domain"]),
  1076. ),
  1077. get_hostname(),
  1078. $_SESSION["APP_NAME"],
  1079. ],
  1080. $_SESSION["SUBJECT_EMAIL"],
  1081. );
  1082. }
  1083. $hostname = get_hostname();
  1084. $from = !empty($_SESSION["FROM_EMAIL"]) ? $_SESSION["FROM_EMAIL"] : "noreply@" . $hostname;
  1085. $from_name = !empty($_SESSION["FROM_NAME"])
  1086. ? $_SESSION["FROM_NAME"]
  1087. : $_SESSION["APP_NAME"];
  1088. $mailtext = translate_email($template, [
  1089. "domain" => htmlentities($_POST["v_domain"]),
  1090. "account" => htmlentities(strtolower($_POST["v_account"])),
  1091. "password" => htmlentities($_POST["v_password"]),
  1092. "webmail" => $webmail . "." . htmlentities($_POST["v_domain"]),
  1093. "hostname" => "mail." . htmlentities($_POST["v_domain"]),
  1094. "appname" => $_SESSION["APP_NAME"],
  1095. ]);
  1096. send_email($to, $subject, $mailtext, $from, $from_name);
  1097. }
  1098. // Set success message
  1099. if (empty($_SESSION["error_msg"])) {
  1100. $_SESSION["ok_msg"] = _("Changes have been saved.");
  1101. }
  1102. }
  1103. // Render page
  1104. if (empty($_GET["account"])) {
  1105. // Display body for mail domain
  1106. render_page($user, $TAB, "edit_mail");
  1107. } else {
  1108. // Display body for mail account
  1109. render_page($user, $TAB, "edit_mail_acc");
  1110. }
  1111. // Flush session messages
  1112. unset($_SESSION["error_msg"]);
  1113. unset($_SESSION["ok_msg"]);