index.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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 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. // Email login credentials
  1013. if (!empty($v_send_email) && empty($_SESSION["error_msg"])) {
  1014. $to = $v_send_email;
  1015. $subject = _("Email Credentials");
  1016. $hostname = get_hostname();
  1017. $from = "noreply@" . $hostname;
  1018. $from_name = _("Hestia Control Panel");
  1019. $mailtext = $v_credentials;
  1020. send_email($to, $subject, $mailtext, $from, $from_name);
  1021. }
  1022. // Set success message
  1023. if (empty($_SESSION["error_msg"])) {
  1024. $_SESSION["ok_msg"] = _("Changes have been saved.");
  1025. }
  1026. }
  1027. // Render page
  1028. if (empty($_GET["account"])) {
  1029. // Display body for mail domain
  1030. render_page($user, $TAB, "edit_mail");
  1031. } else {
  1032. // Display body for mail account
  1033. render_page($user, $TAB, "edit_mail_acc");
  1034. }
  1035. // Flush session messages
  1036. unset($_SESSION["error_msg"]);
  1037. unset($_SESSION["ok_msg"]);