index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. exec(HESTIA_CMD . "v-list-sys-webmail json", $output, $return_var);
  8. $webmail_clients = json_decode(implode("", $output), true);
  9. unset($output);
  10. if (!empty($_GET["domain"])) {
  11. $v_domain = $_GET["domain"];
  12. }
  13. if (!empty($v_domain)) {
  14. // Set webmail alias
  15. exec(
  16. HESTIA_CMD . "v-list-mail-domain " . $user . " " . quoteshellarg($v_domain) . " json",
  17. $output,
  18. $return_var,
  19. );
  20. if ($return_var > 0) {
  21. check_return_code_redirect($return_var, $output, "/list/mail/");
  22. }
  23. $data = json_decode(implode("", $output), true);
  24. unset($output);
  25. $v_webmail_alias = $data[$v_domain]["WEBMAIL_ALIAS"];
  26. }
  27. // Check POST request for mail domain
  28. if (!empty($_POST["ok"])) {
  29. // Check token
  30. verify_csrf($_POST);
  31. // Check empty fields
  32. if (empty($_POST["v_domain"])) {
  33. $errors[] = _("Domain");
  34. }
  35. if (!empty($errors[0])) {
  36. foreach ($errors as $i => $error) {
  37. if ($i == 0) {
  38. $error_msg = $error;
  39. } else {
  40. $error_msg = $error_msg . ", " . $error;
  41. }
  42. }
  43. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  44. }
  45. // Check antispam option
  46. if (!empty($_POST["v_antispam"])) {
  47. $v_antispam = "yes";
  48. } else {
  49. $v_antispam = "no";
  50. }
  51. // Check antivirus option
  52. if (!empty($_POST["v_antivirus"])) {
  53. $v_antivirus = "yes";
  54. } else {
  55. $v_antivirus = "no";
  56. }
  57. // Check dkim option
  58. if (!empty($_POST["v_dkim"])) {
  59. $v_dkim = "yes";
  60. } else {
  61. $v_dkim = "no";
  62. }
  63. // Set domain name to lowercase and remove www prefix
  64. $v_domain = preg_replace("/^www./i", "", $_POST["v_domain"]);
  65. $v_domain = quoteshellarg($v_domain);
  66. $v_domain = strtolower($v_domain);
  67. // Add mail domain
  68. if (empty($_SESSION["error_msg"])) {
  69. exec(
  70. HESTIA_CMD .
  71. "v-add-mail-domain " .
  72. $user .
  73. " " .
  74. $v_domain .
  75. " " .
  76. $v_antispam .
  77. " " .
  78. $v_antivirus .
  79. " " .
  80. $v_dkim,
  81. $output,
  82. $return_var,
  83. );
  84. check_return_code($return_var, $output);
  85. unset($output);
  86. }
  87. if (!empty($_POST["v_reject"]) && $v_antispam == "yes") {
  88. exec(
  89. HESTIA_CMD . "v-add-mail-domain-reject " . $user . " " . $v_domain . " yes",
  90. $output,
  91. $return_var,
  92. );
  93. check_return_code($return_var, $output);
  94. unset($output);
  95. }
  96. if (!empty($_SESSION["IMAP_SYSTEM"]) && !empty($_SESSION["WEBMAIL_SYSTEM"])) {
  97. if (empty($_SESSION["error_msg"])) {
  98. if (!empty($_POST["v_webmail"])) {
  99. $v_webmail = quoteshellarg($_POST["v_webmail"]);
  100. exec(
  101. HESTIA_CMD .
  102. "v-add-mail-domain-webmail " .
  103. $user .
  104. " " .
  105. $v_domain .
  106. " " .
  107. $v_webmail .
  108. " yes",
  109. $output,
  110. $return_var,
  111. );
  112. check_return_code($return_var, $output);
  113. unset($output);
  114. }
  115. }
  116. }
  117. if (!empty($_SESSION["IMAP_SYSTEM"]) && !empty($_SESSION["WEBMAIL_SYSTEM"])) {
  118. if (empty($_POST["v_webmail"])) {
  119. if (empty($_SESSION["error_msg"])) {
  120. exec(
  121. HESTIA_CMD . "v-delete-mail-domain-webmail " . $user . " " . $v_domain . " yes",
  122. $output,
  123. $return_var,
  124. );
  125. check_return_code($return_var, $output);
  126. unset($output);
  127. }
  128. }
  129. }
  130. // Add SMTP Relay Support
  131. if (empty($_SESSION["error_msg"])) {
  132. if (isset($_POST["v_smtp_relay"]) && !empty($_POST["v_smtp_relay_host"])) {
  133. if (
  134. $_POST["v_smtp_relay_host"] != $v_smtp_relay_host ||
  135. $_POST["v_smtp_relay_user"] != $v_smtp_relay_user ||
  136. $_POST["v_smtp_relay_port"] != $v_smtp_relay_port
  137. ) {
  138. $v_smtp_relay = true;
  139. $v_smtp_relay_host = quoteshellarg($_POST["v_smtp_relay_host"]);
  140. $v_smtp_relay_user = quoteshellarg($_POST["v_smtp_relay_user"]);
  141. $v_smtp_relay_pass = quoteshellarg($_POST["v_smtp_relay_pass"]);
  142. if (!empty($_POST["v_smtp_relay_port"])) {
  143. $v_smtp_relay_port = quoteshellarg($_POST["v_smtp_relay_port"]);
  144. } else {
  145. $v_smtp_relay_port = "587";
  146. }
  147. exec(
  148. HESTIA_CMD .
  149. "v-add-mail-domain-smtp-relay " .
  150. $user .
  151. " " .
  152. $v_domain .
  153. " " .
  154. $v_smtp_relay_host .
  155. " '" .
  156. $v_smtp_relay_user .
  157. "' '" .
  158. $v_smtp_relay_pass .
  159. "' " .
  160. $v_smtp_relay_port,
  161. $output,
  162. $return_var,
  163. );
  164. check_return_code($return_var, $output);
  165. unset($output);
  166. }
  167. }
  168. }
  169. // Flush field values on success
  170. if (empty($_SESSION["error_msg"])) {
  171. $_SESSION["ok_msg"] = htmlify_trans(
  172. sprintf(
  173. _("Mail domain {%s} has been created successfully."),
  174. htmlentities($_POST["v_domain"]),
  175. ),
  176. "</b></a>",
  177. '<a href="/list/mail/?domain=' . htmlentities($_POST["v_domain"]) . '"><b>',
  178. );
  179. unset($v_domain, $v_webmail);
  180. }
  181. }
  182. // Check POST request for mail account
  183. if (!empty($_POST["ok_acc"])) {
  184. // Check token
  185. if (!isset($_POST["token"]) || $_SESSION["token"] != $_POST["token"]) {
  186. header("location: /login/");
  187. exit();
  188. }
  189. // Check antispam option
  190. if (!empty($_POST["v_blackhole"])) {
  191. $v_blackhole = "yes";
  192. } else {
  193. $v_blackhole = "no";
  194. }
  195. // Check empty fields
  196. if (empty($_POST["v_domain"])) {
  197. $errors[] = _("Domain");
  198. }
  199. if (empty($_POST["v_account"])) {
  200. $errors[] = _("Account");
  201. }
  202. if (empty($_POST["v_fwd_only"]) && empty($_POST["v_password"])) {
  203. if (empty($_POST["v_password"])) {
  204. $errors[] = _("Password");
  205. }
  206. }
  207. if (!empty($errors[0])) {
  208. foreach ($errors as $i => $error) {
  209. if ($i == 0) {
  210. $error_msg = $error;
  211. } else {
  212. $error_msg = $error_msg . ", " . $error;
  213. }
  214. }
  215. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  216. }
  217. // Validate email
  218. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  219. if (!filter_var($_POST["v_send_email"], FILTER_VALIDATE_EMAIL)) {
  220. $_SESSION["error_msg"] = _("Please enter a valid email address.");
  221. }
  222. }
  223. // Check password length
  224. if (empty($_SESSION["error_msg"]) && empty($_POST["v_fwd_only"])) {
  225. if (!validate_password($_POST["v_password"])) {
  226. $_SESSION["error_msg"] = _("Password does not match the minimum requirements.");
  227. }
  228. }
  229. // Protect input
  230. $v_domain = quoteshellarg($_POST["v_domain"]);
  231. $v_domain = strtolower($v_domain);
  232. $v_account = quoteshellarg($_POST["v_account"]);
  233. $v_quota = quoteshellarg($_POST["v_quota"]);
  234. $v_send_email = $_POST["v_send_email"];
  235. $v_aliases = $_POST["v_aliases"];
  236. $v_fwd = $_POST["v_fwd"];
  237. if (empty($_POST["v_quota"])) {
  238. $v_quota = 0;
  239. }
  240. if (!empty($_POST["v_quota"]) || !empty($_POST["v_aliases"]) || !empty($_POST["v_fwd"])) {
  241. $v_adv = "yes";
  242. }
  243. // Add Mail Account
  244. if (empty($_SESSION["error_msg"])) {
  245. $v_password = tempnam("/tmp", "vst");
  246. $fp = fopen($v_password, "w");
  247. fwrite($fp, $_POST["v_password"] . "\n");
  248. fclose($fp);
  249. exec(
  250. HESTIA_CMD .
  251. "v-add-mail-account " .
  252. $user .
  253. " " .
  254. $v_domain .
  255. " " .
  256. $v_account .
  257. " " .
  258. $v_password .
  259. " " .
  260. $v_quota,
  261. $output,
  262. $return_var,
  263. );
  264. check_return_code($return_var, $output);
  265. unset($output);
  266. unlink($v_password);
  267. $v_password = quoteshellarg($_POST["v_password"]);
  268. }
  269. // Add Aliases
  270. if (!empty($_POST["v_aliases"]) && empty($_SESSION["error_msg"])) {
  271. $valiases = preg_replace("/\n/", " ", $_POST["v_aliases"]);
  272. $valiases = preg_replace("/,/", " ", $valiases);
  273. $valiases = preg_replace("/\s+/", " ", $valiases);
  274. $valiases = trim($valiases);
  275. $aliases = explode(" ", $valiases);
  276. foreach ($aliases as $alias) {
  277. $alias = quoteshellarg($alias);
  278. if (empty($_SESSION["error_msg"])) {
  279. exec(
  280. HESTIA_CMD .
  281. "v-add-mail-account-alias " .
  282. $user .
  283. " " .
  284. $v_domain .
  285. " " .
  286. $v_account .
  287. " " .
  288. $alias,
  289. $output,
  290. $return_var,
  291. );
  292. check_return_code($return_var, $output);
  293. unset($output);
  294. }
  295. }
  296. }
  297. if (!empty($_POST["v_blackhole"]) && empty($_SESSION["error_msg"])) {
  298. exec(
  299. HESTIA_CMD .
  300. "v-add-mail-account-forward " .
  301. $user .
  302. " " .
  303. $v_domain .
  304. " " .
  305. $v_account .
  306. " :blackhole:",
  307. $output,
  308. $return_var,
  309. );
  310. check_return_code($return_var, $output);
  311. unset($output);
  312. //disable any input in v_fwd
  313. $_POST["v_fwd"] = "";
  314. }
  315. // Add Forwarders
  316. if (!empty($_POST["v_fwd"]) && empty($_SESSION["error_msg"])) {
  317. $vfwd = preg_replace("/\n/", " ", $_POST["v_fwd"]);
  318. $vfwd = preg_replace("/,/", " ", $vfwd);
  319. $vfwd = preg_replace("/\s+/", " ", $vfwd);
  320. $vfwd = trim($vfwd);
  321. $fwd = explode(" ", $vfwd);
  322. foreach ($fwd as $forward) {
  323. $forward = quoteshellarg($forward);
  324. if (empty($_SESSION["error_msg"])) {
  325. exec(
  326. HESTIA_CMD .
  327. "v-add-mail-account-forward " .
  328. $user .
  329. " " .
  330. $v_domain .
  331. " " .
  332. $v_account .
  333. " " .
  334. $forward,
  335. $output,
  336. $return_var,
  337. );
  338. check_return_code($return_var, $output);
  339. unset($output);
  340. }
  341. }
  342. }
  343. // Add fwd_only flag
  344. if (!empty($_POST["v_fwd_only"]) && empty($_SESSION["error_msg"])) {
  345. exec(
  346. HESTIA_CMD .
  347. "v-add-mail-account-fwd-only " .
  348. $user .
  349. " " .
  350. $v_domain .
  351. " " .
  352. $v_account,
  353. $output,
  354. $return_var,
  355. );
  356. check_return_code($return_var, $output);
  357. unset($output);
  358. }
  359. // Add fwd_only flag
  360. if (
  361. !empty($_POST["v_rate"]) &&
  362. empty($_SESSION["error_msg"]) &&
  363. $_SESSION["userContext"] == "admin"
  364. ) {
  365. $v_rate = quoteshellarg($_POST["v_rate"]);
  366. exec(
  367. HESTIA_CMD .
  368. "v-change-mail-account-rate-limit " .
  369. $user .
  370. " " .
  371. $v_domain .
  372. " " .
  373. $v_account .
  374. " " .
  375. $v_rate,
  376. $output,
  377. $return_var,
  378. );
  379. check_return_code($return_var, $output);
  380. unset($output);
  381. }
  382. // Get webmail url
  383. if (empty($_SESSION["error_msg"])) {
  384. [$hostname, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
  385. $webmail = "http://" . $hostname . "/" . $v_webmail_alias . "/";
  386. if (!empty($_SESSION["WEBMAIL_ALIAS"])) {
  387. $webmail = $_SESSION["WEBMAIL_ALIAS"];
  388. }
  389. }
  390. // Email login credentials
  391. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  392. $to = $_POST["v_send_email"];
  393. $template = get_email_template("email_credentials", $_SESSION["language"]);
  394. if (!empty($template)) {
  395. preg_match("/<subject>(.*?)<\/subject>/si", $template, $matches);
  396. $subject = $matches[1];
  397. $subject = str_replace(
  398. ["{{hostname}}", "{{appname}}", "{{account}}", "{{domain}}"],
  399. [
  400. get_hostname(),
  401. $_SESSION["APP_NAME"],
  402. htmlentities(strtolower($_POST["v_account"])),
  403. htmlentities($_POST["v_domain"]),
  404. ],
  405. $subject,
  406. );
  407. $template = str_replace($matches[0], "", $template);
  408. } else {
  409. $template = _(
  410. "Mail account has been created.\n" .
  411. "\n" .
  412. "Common Account Settings:\n" .
  413. "Username: {{account}}@{{domain}}\n" .
  414. "Password: {{password}}\n" .
  415. "Webmail: {{webmail}}\n" .
  416. "Hostname: {{hostname}}\n" .
  417. "\n" .
  418. "IMAP Settings\n" .
  419. "Authentication: Normal Password\n" .
  420. "SSL/TLS: Port 993\n" .
  421. "STARTTLS: Port 143\n" .
  422. "No encryption: Port 143\n" .
  423. "\n" .
  424. "POP3 Settings\n" .
  425. "Authentication: Normal Password\n" .
  426. "SSL/TLS: Port 995\n" .
  427. "STARTTLS: Port 110\n" .
  428. "No encryption: Port 110\n" .
  429. "\n" .
  430. "SMTP Settings\n" .
  431. "Authentication: Normal Password\n" .
  432. "SSL/TLS: Port 465\n" .
  433. "STARTTLS: Port 587\n" .
  434. "No encryption: Port 25\n" .
  435. "\n" .
  436. "Best regards,\n" .
  437. "\n" .
  438. "--\n" .
  439. "{{appname}}",
  440. );
  441. }
  442. if (empty($subject)) {
  443. $subject = str_replace(
  444. ["{{subject}}", "{{hostname}}", "{{appname}}"],
  445. [
  446. sprintf(
  447. _("Email Credentials: %s@%s"),
  448. htmlentities(strtolower($_POST["v_account"])),
  449. htmlentities($_POST["v_domain"]),
  450. ),
  451. get_hostname(),
  452. $_SESSION["APP_NAME"],
  453. ],
  454. $_SESSION["SUBJECT_EMAIL"],
  455. );
  456. }
  457. $hostname = get_hostname();
  458. $from = !empty($_SESSION["FROM_EMAIL"]) ? $_SESSION["FROM_EMAIL"] : "noreply@" . $hostname;
  459. $from_name = !empty($_SESSION["FROM_NAME"])
  460. ? $_SESSION["FROM_NAME"]
  461. : $_SESSION["APP_NAME"];
  462. $mailtext = translate_email($template, [
  463. "domain" => htmlentities($_POST["v_domain"]),
  464. "account" => htmlentities(strtolower($_POST["v_account"])),
  465. "password" => htmlentities($_POST["v_password"]),
  466. "webmail" => $webmail . "." . htmlentities($_POST["v_domain"]),
  467. "hostname" => "mail." . htmlentities($_POST["v_domain"]),
  468. "appname" => $_SESSION["APP_NAME"],
  469. ]);
  470. send_email($to, $subject, $mailtext, $from, $from_name);
  471. }
  472. // Flush field values on success
  473. if (empty($_SESSION["error_msg"])) {
  474. $_SESSION["ok_msg"] = htmlify_trans(
  475. sprintf(
  476. _("Mail account {%s@%s} has been created successfully."),
  477. htmlentities(strtolower($_POST["v_account"])),
  478. htmlentities($_POST["v_domain"]),
  479. ),
  480. "</b></a>",
  481. '<a href="/edit/mail/?account=' .
  482. htmlentities(strtolower($_POST["v_account"])) .
  483. "&domain=" .
  484. htmlentities($_POST["v_domain"]) .
  485. '"><b>',
  486. );
  487. unset($v_account);
  488. unset($v_password);
  489. unset($v_aliases);
  490. unset($v_fwd);
  491. unset($v_quota);
  492. }
  493. }
  494. // Render page
  495. if (empty($_GET["domain"])) {
  496. // Display body for mail domain
  497. if (!empty($_POST["v_webmail"])) {
  498. $v_webmail = $_POST["v_webmail"];
  499. } else {
  500. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  501. $v_webmail = "roundcube";
  502. }
  503. if (empty($_GET["accept"])) {
  504. $_GET["accept"] = false;
  505. }
  506. if (empty($v_domain)) {
  507. $v_domain = "";
  508. }
  509. if (empty($v_smtp_relay)) {
  510. $v_smtp_relay = "";
  511. }
  512. if (empty($v_smtp_relay_user)) {
  513. $v_smtp_relay_user = "";
  514. }
  515. if (empty($v_smtp_relay_password)) {
  516. $v_smtp_relay_password = "";
  517. }
  518. if (empty($v_smtp_relay_host)) {
  519. $v_smtp_relay_host = "";
  520. }
  521. if (empty($v_smtp_relay_port)) {
  522. $v_smtp_relay_port = "";
  523. }
  524. $accept = $_GET["accept"] ?? "";
  525. render_page($user, $TAB, "add_mail");
  526. } else {
  527. // Display body for mail account
  528. if (empty($v_account)) {
  529. $v_account = "";
  530. }
  531. if (empty($v_quota)) {
  532. $v_quota = "";
  533. }
  534. if (empty($v_rate)) {
  535. $v_rate = "";
  536. }
  537. if (empty($v_blackhole)) {
  538. $v_blackhole = "";
  539. }
  540. if (empty($v_fwd_only)) {
  541. $v_fwd_only = "";
  542. }
  543. if (empty($v_aliases)) {
  544. $v_aliases = "";
  545. }
  546. if (empty($v_send_email)) {
  547. $v_send_email = "";
  548. }
  549. if (empty($v_fwd)) {
  550. $v_fwd = "";
  551. }
  552. $v_domain = $_GET["domain"];
  553. render_page($user, $TAB, "add_mail_acc");
  554. }
  555. // Flush session messages
  556. unset($_SESSION["error_msg"]);
  557. unset($_SESSION["ok_msg"]);