index.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. $_POST["v_smtp_relay_pass"] != ""
  639. ) {
  640. $v_smtp_relay = true;
  641. $v_smtp_relay_host = quoteshellarg($_POST["v_smtp_relay_host"]);
  642. $v_smtp_relay_user = quoteshellarg($_POST["v_smtp_relay_user"]);
  643. $v_smtp_relay_pass = quoteshellarg($_POST["v_smtp_relay_pass"]);
  644. if (!empty($_POST["v_smtp_relay_port"])) {
  645. $v_smtp_relay_port = quoteshellarg($_POST["v_smtp_relay_port"]);
  646. } else {
  647. $v_smtp_relay_port = "587";
  648. }
  649. exec(
  650. HESTIA_CMD .
  651. "v-add-mail-domain-smtp-relay " .
  652. $v_username .
  653. " " .
  654. quoteshellarg($v_domain) .
  655. " " .
  656. $v_smtp_relay_host .
  657. " " .
  658. $v_smtp_relay_user .
  659. " " .
  660. $v_smtp_relay_pass .
  661. " " .
  662. $v_smtp_relay_port,
  663. $output,
  664. $return_var,
  665. );
  666. check_return_code($return_var, $output);
  667. unset($output);
  668. }
  669. }
  670. if (!isset($_POST["v_smtp_relay"]) && $v_smtp_relay == true) {
  671. $v_smtp_relay = false;
  672. $v_smtp_relay_host = $v_smtp_relay_user = $v_smtp_relay_pass = $v_smtp_relay_port = "";
  673. exec(
  674. HESTIA_CMD .
  675. "v-delete-mail-domain-smtp-relay " .
  676. $v_username .
  677. " " .
  678. quoteshellarg($v_domain),
  679. $output,
  680. $return_var,
  681. );
  682. check_return_code($return_var, $output);
  683. unset($output);
  684. }
  685. }
  686. // Set success message
  687. if (empty($_SESSION["error_msg"])) {
  688. $_SESSION["ok_msg"] = _("Changes have been saved.");
  689. }
  690. }
  691. // Check POST request for mail account
  692. if (!empty($_POST["save"]) && !empty($_GET["domain"]) && !empty($_GET["account"])) {
  693. // Check token
  694. verify_csrf($_POST);
  695. // Validate email
  696. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  697. if (!filter_var($_POST["v_send_email"], FILTER_VALIDATE_EMAIL)) {
  698. $_SESSION["error_msg"] = _("Please enter a valid email address.");
  699. }
  700. }
  701. $v_account = $_POST["v_account"];
  702. $v_send_email = $_POST["v_send_email"];
  703. $v_credentials = $_POST["v_credentials"];
  704. exec(
  705. HESTIA_CMD .
  706. "v-list-mail-account " .
  707. $user .
  708. " " .
  709. quoteshellarg($v_domain) .
  710. " " .
  711. quoteshellarg($v_account) .
  712. " json",
  713. $output,
  714. $return_var,
  715. );
  716. $data = json_decode(implode("", $output), true);
  717. check_return_code_redirect($return_var, $output, "/list/mail/");
  718. unset($output);
  719. // Change password
  720. if (!empty($_POST["v_password"]) && empty($_SESSION["error_msg"])) {
  721. if (!validate_password($_POST["v_password"])) {
  722. $_SESSION["error_msg"] = _("Password does not match the minimum requirements.");
  723. } else {
  724. $v_password = tempnam("/tmp", "vst");
  725. $fp = fopen($v_password, "w");
  726. fwrite($fp, $_POST["v_password"] . "\n");
  727. fclose($fp);
  728. exec(
  729. HESTIA_CMD .
  730. "v-change-mail-account-password " .
  731. $v_username .
  732. " " .
  733. quoteshellarg($v_domain) .
  734. " " .
  735. quoteshellarg($v_account) .
  736. " " .
  737. $v_password,
  738. $output,
  739. $return_var,
  740. );
  741. check_return_code($return_var, $output);
  742. unset($output);
  743. unlink($v_password);
  744. $v_password = quoteshellarg($_POST["v_password"]);
  745. }
  746. }
  747. // Change quota
  748. if ($v_quota != $_POST["v_quota"] && empty($_SESSION["error_msg"])) {
  749. if (empty($_POST["v_quota"])) {
  750. $v_quota = 0;
  751. } else {
  752. $v_quota = quoteshellarg($_POST["v_quota"]);
  753. }
  754. exec(
  755. HESTIA_CMD .
  756. "v-change-mail-account-quota " .
  757. $v_username .
  758. " " .
  759. quoteshellarg($v_domain) .
  760. " " .
  761. quoteshellarg($v_account) .
  762. " " .
  763. $v_quota,
  764. $output,
  765. $return_var,
  766. );
  767. check_return_code($return_var, $output);
  768. unset($output);
  769. }
  770. // Change rate limit
  771. if (
  772. $v_rate != $_POST["v_rate"] &&
  773. empty($_SESSION["error_msg"]) &&
  774. $_SESSION["userContext"] == "admin"
  775. ) {
  776. if (empty($_POST["v_rate"])) {
  777. $v_rate = "system";
  778. } else {
  779. $v_rate = quoteshellarg($_POST["v_rate"]);
  780. }
  781. exec(
  782. HESTIA_CMD .
  783. "v-change-mail-account-rate-limit " .
  784. $v_username .
  785. " " .
  786. quoteshellarg($v_domain) .
  787. " " .
  788. quoteshellarg($v_account) .
  789. " " .
  790. $v_rate,
  791. $output,
  792. $return_var,
  793. );
  794. check_return_code($return_var, $output);
  795. if ($v_rate == "system") {
  796. $v_rate = "";
  797. }
  798. unset($output);
  799. }
  800. // Change account aliases
  801. if (empty($_SESSION["error_msg"])) {
  802. $waliases = preg_replace("/\n/", " ", $_POST["v_aliases"]);
  803. $waliases = preg_replace("/,/", " ", $waliases);
  804. $waliases = preg_replace("/\s+/", " ", $waliases);
  805. $waliases = trim($waliases);
  806. $aliases = explode(" ", $waliases);
  807. $v_aliases = str_replace(" ", "\n", $waliases);
  808. $result = array_diff($valiases, $aliases);
  809. foreach ($result as $alias) {
  810. if (empty($_SESSION["error_msg"]) && !empty($alias)) {
  811. exec(
  812. HESTIA_CMD .
  813. "v-delete-mail-account-alias " .
  814. $v_username .
  815. " " .
  816. quoteshellarg($v_domain) .
  817. " " .
  818. quoteshellarg($v_account) .
  819. " " .
  820. quoteshellarg($alias),
  821. $output,
  822. $return_var,
  823. );
  824. check_return_code($return_var, $output);
  825. unset($output);
  826. }
  827. }
  828. $result = array_diff($aliases, $valiases);
  829. foreach ($result as $alias) {
  830. if (empty($_SESSION["error_msg"]) && !empty($alias)) {
  831. exec(
  832. HESTIA_CMD .
  833. "v-add-mail-account-alias " .
  834. $v_username .
  835. " " .
  836. quoteshellarg($v_domain) .
  837. " " .
  838. quoteshellarg($v_account) .
  839. " " .
  840. quoteshellarg($alias),
  841. $output,
  842. $return_var,
  843. );
  844. check_return_code($return_var, $output);
  845. unset($output);
  846. }
  847. }
  848. }
  849. // Change forwarders to :blackhole:
  850. if (empty($_SESSION["error_msg"]) && !empty($_POST["v_blackhole"])) {
  851. foreach ($vfwd as $forward) {
  852. if (empty($_SESSION["error_msg"]) && !empty($forward)) {
  853. exec(
  854. HESTIA_CMD .
  855. "v-delete-mail-account-forward " .
  856. $v_username .
  857. " " .
  858. quoteshellarg($v_domain) .
  859. " " .
  860. quoteshellarg($v_account) .
  861. " " .
  862. quoteshellarg($forward),
  863. $output,
  864. $return_var,
  865. );
  866. check_return_code($return_var, $output);
  867. unset($output);
  868. }
  869. exec(
  870. HESTIA_CMD .
  871. "v-add-mail-account-forward " .
  872. $v_username .
  873. " " .
  874. quoteshellarg($v_domain) .
  875. " " .
  876. quoteshellarg($v_account) .
  877. " :blackhole:",
  878. $output,
  879. $return_var,
  880. );
  881. check_return_code($return_var, $output);
  882. unset($output);
  883. $v_fwd = "";
  884. $v_blackhole = "yes";
  885. }
  886. }
  887. // Change forwarders
  888. if (empty($_SESSION["error_msg"]) && empty($_POST["v_blackhole"])) {
  889. $wfwd = preg_replace("/\n/", " ", $_POST["v_fwd"]);
  890. $wfwd = preg_replace("/,/", " ", $wfwd);
  891. $wfwd = preg_replace("/\s+/", " ", $wfwd);
  892. $wfwd = trim($wfwd);
  893. $fwd = explode(" ", $wfwd);
  894. $v_fwd = str_replace(" ", "\n", $wfwd);
  895. $result = array_diff($vfwd, $fwd);
  896. foreach ($result as $forward) {
  897. if (empty($_SESSION["error_msg"]) && !empty($forward)) {
  898. exec(
  899. HESTIA_CMD .
  900. "v-delete-mail-account-forward " .
  901. $v_username .
  902. " " .
  903. quoteshellarg($v_domain) .
  904. " " .
  905. quoteshellarg($v_account) .
  906. " " .
  907. quoteshellarg($forward),
  908. $output,
  909. $return_var,
  910. );
  911. check_return_code($return_var, $output);
  912. unset($output);
  913. }
  914. }
  915. $result = array_diff($fwd, $vfwd);
  916. foreach ($result as $forward) {
  917. if (empty($_SESSION["error_msg"]) && !empty($forward)) {
  918. exec(
  919. HESTIA_CMD .
  920. "v-add-mail-account-forward " .
  921. $v_username .
  922. " " .
  923. quoteshellarg($v_domain) .
  924. " " .
  925. quoteshellarg($v_account) .
  926. " " .
  927. quoteshellarg($forward),
  928. $output,
  929. $return_var,
  930. );
  931. check_return_code($return_var, $output);
  932. unset($output);
  933. }
  934. }
  935. $v_blackhole = "no";
  936. }
  937. // Delete FWD_ONLY flag
  938. if ($v_fwd_only == "yes" && empty($_POST["v_fwd_only"]) && empty($_SESSION["error_msg"])) {
  939. exec(
  940. HESTIA_CMD .
  941. "v-delete-mail-account-fwd-only " .
  942. $v_username .
  943. " " .
  944. quoteshellarg($v_domain) .
  945. " " .
  946. quoteshellarg($v_account),
  947. $output,
  948. $return_var,
  949. );
  950. check_return_code($return_var, $output);
  951. unset($output);
  952. $v_fwd_only = "";
  953. }
  954. // Add FWD_ONLY flag
  955. if ($v_fwd_only != "yes" && !empty($_POST["v_fwd_only"]) && empty($_SESSION["error_msg"])) {
  956. exec(
  957. HESTIA_CMD .
  958. "v-add-mail-account-fwd-only " .
  959. $v_username .
  960. " " .
  961. quoteshellarg($v_domain) .
  962. " " .
  963. quoteshellarg($v_account),
  964. $output,
  965. $return_var,
  966. );
  967. check_return_code($return_var, $output);
  968. unset($output);
  969. $v_fwd_only = "yes";
  970. }
  971. // Delete autoreply
  972. if ($v_autoreply == "yes" && empty($_POST["v_autoreply"]) && empty($_SESSION["error_msg"])) {
  973. exec(
  974. HESTIA_CMD .
  975. "v-delete-mail-account-autoreply " .
  976. $v_username .
  977. " " .
  978. quoteshellarg($v_domain) .
  979. " " .
  980. quoteshellarg($v_account),
  981. $output,
  982. $return_var,
  983. );
  984. check_return_code($return_var, $output);
  985. unset($output);
  986. $v_autoreply = "no";
  987. $v_autoreply_message = "";
  988. }
  989. // Add autoreply
  990. if (!empty($_POST["v_autoreply"]) && empty($_SESSION["error_msg"])) {
  991. if ($v_autoreply_message != str_replace("\r\n", "\n", $_POST["v_autoreply_message"])) {
  992. $v_autoreply_message = str_replace("\r\n", "\n", $_POST["v_autoreply_message"]);
  993. $v_autoreply_message = quoteshellarg($v_autoreply_message);
  994. exec(
  995. HESTIA_CMD .
  996. "v-add-mail-account-autoreply " .
  997. $v_username .
  998. " " .
  999. quoteshellarg($v_domain) .
  1000. " " .
  1001. quoteshellarg($v_account) .
  1002. " " .
  1003. $v_autoreply_message,
  1004. $output,
  1005. $return_var,
  1006. );
  1007. check_return_code($return_var, $output);
  1008. unset($output);
  1009. $v_autoreply = "yes";
  1010. $v_autoreply_message = $_POST["v_autoreply_message"];
  1011. }
  1012. }
  1013. $webmail = "http://" . $hostname . "/" . $v_webmail_alias . "/";
  1014. if (!empty($_SESSION["WEBMAIL_ALIAS"])) {
  1015. $webmail = $_SESSION["WEBMAIL_ALIAS"];
  1016. }
  1017. // Email login credentials
  1018. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  1019. $to = $_POST["v_send_email"];
  1020. $template = get_email_template("email_credentials", $_SESSION["language"]);
  1021. if (!empty($template)) {
  1022. preg_match("/<subject>(.*?)<\/subject>/si", $template, $matches);
  1023. $subject = $matches[1];
  1024. $subject = str_replace(
  1025. ["{{hostname}}", "{{appname}}", "{{account}}", "{{domain}}"],
  1026. [
  1027. get_hostname(),
  1028. $_SESSION["APP_NAME"],
  1029. htmlentities(strtolower($_POST["v_account"])),
  1030. htmlentities($_POST["v_domain"]),
  1031. ],
  1032. $subject,
  1033. );
  1034. $template = str_replace($matches[0], "", $template);
  1035. } else {
  1036. $template = _(
  1037. "Mail account has been created.\n" .
  1038. "\n" .
  1039. "Common Account Settings:\n" .
  1040. "Username: {{account}}@{{domain}}\n" .
  1041. "Password: {{password}}\n" .
  1042. "Webmail: {{webmail}}\n" .
  1043. "Hostname: {{hostname}}\n" .
  1044. "\n" .
  1045. "IMAP Settings\n" .
  1046. "Authentication: Normal Password\n" .
  1047. "SSL/TLS: Port 993\n" .
  1048. "STARTTLS: Port 143\n" .
  1049. "No encryption: Port 143\n" .
  1050. "\n" .
  1051. "POP3 Settings\n" .
  1052. "Authentication: Normal Password\n" .
  1053. "SSL/TLS: Port 995\n" .
  1054. "STARTTLS: Port 110\n" .
  1055. "No encryption: Port 110\n" .
  1056. "\n" .
  1057. "SMTP Settings\n" .
  1058. "Authentication: Normal Password\n" .
  1059. "SSL/TLS: Port 465\n" .
  1060. "STARTTLS: Port 587\n" .
  1061. "No encryption: Port 25\n" .
  1062. "\n" .
  1063. "Best regards,\n" .
  1064. "\n" .
  1065. "--\n" .
  1066. "{{appname}}",
  1067. );
  1068. }
  1069. if (empty($subject)) {
  1070. $subject = str_replace(
  1071. ["{{subject}}", "{{hostname}}", "{{appname}}"],
  1072. [
  1073. sprintf(
  1074. _("Email Credentials: %s@%s"),
  1075. htmlentities(strtolower($_POST["v_account"])),
  1076. htmlentities($_POST["v_domain"]),
  1077. ),
  1078. get_hostname(),
  1079. $_SESSION["APP_NAME"],
  1080. ],
  1081. $_SESSION["SUBJECT_EMAIL"],
  1082. );
  1083. }
  1084. $hostname = get_hostname();
  1085. $from = !empty($_SESSION["FROM_EMAIL"]) ? $_SESSION["FROM_EMAIL"] : "noreply@" . $hostname;
  1086. $from_name = !empty($_SESSION["FROM_NAME"])
  1087. ? $_SESSION["FROM_NAME"]
  1088. : $_SESSION["APP_NAME"];
  1089. $mailtext = translate_email($template, [
  1090. "domain" => htmlentities($_POST["v_domain"]),
  1091. "account" => htmlentities(strtolower($_POST["v_account"])),
  1092. "password" => htmlentities($_POST["v_password"]),
  1093. "webmail" => $webmail . "." . htmlentities($_POST["v_domain"]),
  1094. "hostname" => "mail." . htmlentities($_POST["v_domain"]),
  1095. "appname" => $_SESSION["APP_NAME"],
  1096. ]);
  1097. send_email($to, $subject, $mailtext, $from, $from_name);
  1098. }
  1099. // Set success message
  1100. if (empty($_SESSION["error_msg"])) {
  1101. $_SESSION["ok_msg"] = _("Changes have been saved.");
  1102. }
  1103. }
  1104. // Render page
  1105. if (empty($_GET["account"])) {
  1106. // Display body for mail domain
  1107. render_page($user, $TAB, "edit_mail");
  1108. } else {
  1109. // Display body for mail account
  1110. render_page($user, $TAB, "edit_mail_acc");
  1111. }
  1112. // Flush session messages
  1113. unset($_SESSION["error_msg"]);
  1114. unset($_SESSION["ok_msg"]);