index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. "</a>",
  177. '<a class="u-text-bold" href="/list/mail/?domain=' .
  178. htmlentities($_POST["v_domain"]) .
  179. '">',
  180. );
  181. unset($v_domain, $v_webmail);
  182. }
  183. }
  184. // Check POST request for mail account
  185. if (!empty($_POST["ok_acc"])) {
  186. // Check token
  187. if (!isset($_POST["token"]) || $_SESSION["token"] != $_POST["token"]) {
  188. header("location: /login/");
  189. exit();
  190. }
  191. // Check antispam option
  192. if (!empty($_POST["v_blackhole"])) {
  193. $v_blackhole = "yes";
  194. } else {
  195. $v_blackhole = "no";
  196. }
  197. // Check empty fields
  198. if (empty($_POST["v_domain"])) {
  199. $errors[] = _("Domain");
  200. }
  201. if (empty($_POST["v_account"])) {
  202. $errors[] = _("Account");
  203. }
  204. if (empty($_POST["v_fwd_only"]) && empty($_POST["v_password"])) {
  205. if (empty($_POST["v_password"])) {
  206. $errors[] = _("Password");
  207. }
  208. }
  209. if (!empty($errors[0])) {
  210. foreach ($errors as $i => $error) {
  211. if ($i == 0) {
  212. $error_msg = $error;
  213. } else {
  214. $error_msg = $error_msg . ", " . $error;
  215. }
  216. }
  217. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  218. }
  219. // Validate email
  220. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  221. if (!filter_var($_POST["v_send_email"], FILTER_VALIDATE_EMAIL)) {
  222. $_SESSION["error_msg"] = _("Please enter a valid email address.");
  223. }
  224. }
  225. // Check password length
  226. if (empty($_SESSION["error_msg"]) && empty($_POST["v_fwd_only"])) {
  227. if (!validate_password($_POST["v_password"])) {
  228. $_SESSION["error_msg"] = _("Password does not match the minimum requirements.");
  229. }
  230. }
  231. // Protect input
  232. $v_domain = quoteshellarg($_POST["v_domain"]);
  233. $v_domain = strtolower($v_domain);
  234. $v_account = quoteshellarg($_POST["v_account"]);
  235. $v_quota = quoteshellarg($_POST["v_quota"]);
  236. $v_send_email = $_POST["v_send_email"];
  237. $v_aliases = $_POST["v_aliases"];
  238. $v_fwd = $_POST["v_fwd"];
  239. if (empty($_POST["v_quota"])) {
  240. $v_quota = 0;
  241. }
  242. if (!empty($_POST["v_quota"]) || !empty($_POST["v_aliases"]) || !empty($_POST["v_fwd"])) {
  243. $v_adv = "yes";
  244. }
  245. // Add Mail Account
  246. if (empty($_SESSION["error_msg"])) {
  247. $v_password = tempnam("/tmp", "vst");
  248. $fp = fopen($v_password, "w");
  249. fwrite($fp, $_POST["v_password"] . "\n");
  250. fclose($fp);
  251. exec(
  252. HESTIA_CMD .
  253. "v-add-mail-account " .
  254. $user .
  255. " " .
  256. $v_domain .
  257. " " .
  258. $v_account .
  259. " " .
  260. $v_password .
  261. " " .
  262. $v_quota,
  263. $output,
  264. $return_var,
  265. );
  266. check_return_code($return_var, $output);
  267. unset($output);
  268. unlink($v_password);
  269. $v_password = quoteshellarg($_POST["v_password"]);
  270. }
  271. // Add Aliases
  272. if (!empty($_POST["v_aliases"]) && empty($_SESSION["error_msg"])) {
  273. $valiases = preg_replace("/\n/", " ", $_POST["v_aliases"]);
  274. $valiases = preg_replace("/,/", " ", $valiases);
  275. $valiases = preg_replace("/\s+/", " ", $valiases);
  276. $valiases = trim($valiases);
  277. $aliases = explode(" ", $valiases);
  278. foreach ($aliases as $alias) {
  279. $alias = quoteshellarg($alias);
  280. if (empty($_SESSION["error_msg"])) {
  281. exec(
  282. HESTIA_CMD .
  283. "v-add-mail-account-alias " .
  284. $user .
  285. " " .
  286. $v_domain .
  287. " " .
  288. $v_account .
  289. " " .
  290. $alias,
  291. $output,
  292. $return_var,
  293. );
  294. check_return_code($return_var, $output);
  295. unset($output);
  296. }
  297. }
  298. }
  299. if (!empty($_POST["v_blackhole"]) && empty($_SESSION["error_msg"])) {
  300. exec(
  301. HESTIA_CMD .
  302. "v-add-mail-account-forward " .
  303. $user .
  304. " " .
  305. $v_domain .
  306. " " .
  307. $v_account .
  308. " :blackhole:",
  309. $output,
  310. $return_var,
  311. );
  312. check_return_code($return_var, $output);
  313. unset($output);
  314. //disable any input in v_fwd
  315. $_POST["v_fwd"] = "";
  316. }
  317. // Add Forwarders
  318. if (!empty($_POST["v_fwd"]) && empty($_SESSION["error_msg"])) {
  319. $vfwd = preg_replace("/\n/", " ", $_POST["v_fwd"]);
  320. $vfwd = preg_replace("/,/", " ", $vfwd);
  321. $vfwd = preg_replace("/\s+/", " ", $vfwd);
  322. $vfwd = trim($vfwd);
  323. $fwd = explode(" ", $vfwd);
  324. foreach ($fwd as $forward) {
  325. $forward = quoteshellarg($forward);
  326. if (empty($_SESSION["error_msg"])) {
  327. exec(
  328. HESTIA_CMD .
  329. "v-add-mail-account-forward " .
  330. $user .
  331. " " .
  332. $v_domain .
  333. " " .
  334. $v_account .
  335. " " .
  336. $forward,
  337. $output,
  338. $return_var,
  339. );
  340. check_return_code($return_var, $output);
  341. unset($output);
  342. }
  343. }
  344. }
  345. // Add fwd_only flag
  346. if (!empty($_POST["v_fwd_only"]) && empty($_SESSION["error_msg"])) {
  347. exec(
  348. HESTIA_CMD .
  349. "v-add-mail-account-fwd-only " .
  350. $user .
  351. " " .
  352. $v_domain .
  353. " " .
  354. $v_account,
  355. $output,
  356. $return_var,
  357. );
  358. check_return_code($return_var, $output);
  359. unset($output);
  360. }
  361. // Add fwd_only flag
  362. if (
  363. !empty($_POST["v_rate"]) &&
  364. empty($_SESSION["error_msg"]) &&
  365. $_SESSION["userContext"] == "admin"
  366. ) {
  367. $v_rate = quoteshellarg($_POST["v_rate"]);
  368. exec(
  369. HESTIA_CMD .
  370. "v-change-mail-account-rate-limit " .
  371. $user .
  372. " " .
  373. $v_domain .
  374. " " .
  375. $v_account .
  376. " " .
  377. $v_rate,
  378. $output,
  379. $return_var,
  380. );
  381. check_return_code($return_var, $output);
  382. unset($output);
  383. }
  384. // Get webmail url
  385. if (empty($_SESSION["error_msg"])) {
  386. [$hostname, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
  387. $webmail = "http://" . $hostname . "/" . $v_webmail_alias . "/";
  388. if (!empty($_SESSION["WEBMAIL_ALIAS"])) {
  389. $webmail = $_SESSION["WEBMAIL_ALIAS"];
  390. }
  391. }
  392. // Email login credentials
  393. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  394. $to = $_POST["v_send_email"];
  395. $template = get_email_template("email_credentials", $_SESSION["language"]);
  396. if (!empty($template)) {
  397. preg_match("/<subject>(.*?)<\/subject>/si", $template, $matches);
  398. $subject = $matches[1];
  399. $subject = str_replace(
  400. ["{{hostname}}", "{{appname}}", "{{account}}", "{{domain}}"],
  401. [
  402. get_hostname(),
  403. $_SESSION["APP_NAME"],
  404. htmlentities(strtolower($_POST["v_account"])),
  405. htmlentities($_POST["v_domain"]),
  406. ],
  407. $subject,
  408. );
  409. $template = str_replace($matches[0], "", $template);
  410. } else {
  411. $template = _(
  412. "Mail account has been created.\n" .
  413. "\n" .
  414. "Common Account Settings:\n" .
  415. "Username: {{account}}@{{domain}}\n" .
  416. "Password: {{password}}\n" .
  417. "Webmail: {{webmail}}\n" .
  418. "Hostname: {{hostname}}\n" .
  419. "\n" .
  420. "IMAP Settings\n" .
  421. "Authentication: Normal Password\n" .
  422. "SSL/TLS: Port 993\n" .
  423. "STARTTLS: Port 143\n" .
  424. "No encryption: Port 143\n" .
  425. "\n" .
  426. "POP3 Settings\n" .
  427. "Authentication: Normal Password\n" .
  428. "SSL/TLS: Port 995\n" .
  429. "STARTTLS: Port 110\n" .
  430. "No encryption: Port 110\n" .
  431. "\n" .
  432. "SMTP Settings\n" .
  433. "Authentication: Normal Password\n" .
  434. "SSL/TLS: Port 465\n" .
  435. "STARTTLS: Port 587\n" .
  436. "No encryption: Port 25\n" .
  437. "\n" .
  438. "Best regards,\n" .
  439. "\n" .
  440. "--\n" .
  441. "{{appname}}",
  442. );
  443. }
  444. if (empty($subject)) {
  445. $subject = str_replace(
  446. ["{{subject}}", "{{hostname}}", "{{appname}}"],
  447. [
  448. sprintf(
  449. _("Email Credentials: %s@%s"),
  450. htmlentities(strtolower($_POST["v_account"])),
  451. htmlentities($_POST["v_domain"]),
  452. ),
  453. get_hostname(),
  454. $_SESSION["APP_NAME"],
  455. ],
  456. $_SESSION["SUBJECT_EMAIL"],
  457. );
  458. }
  459. $hostname = get_hostname();
  460. $from = !empty($_SESSION["FROM_EMAIL"]) ? $_SESSION["FROM_EMAIL"] : "noreply@" . $hostname;
  461. $from_name = !empty($_SESSION["FROM_NAME"])
  462. ? $_SESSION["FROM_NAME"]
  463. : $_SESSION["APP_NAME"];
  464. $mailtext = translate_email($template, [
  465. "domain" => htmlentities($_POST["v_domain"]),
  466. "account" => htmlentities(strtolower($_POST["v_account"])),
  467. "password" => htmlentities($_POST["v_password"]),
  468. "webmail" => $webmail . "." . htmlentities($_POST["v_domain"]),
  469. "hostname" => "mail." . htmlentities($_POST["v_domain"]),
  470. "appname" => $_SESSION["APP_NAME"],
  471. ]);
  472. send_email($to, $subject, $mailtext, $from, $from_name);
  473. }
  474. // Flush field values on success
  475. if (empty($_SESSION["error_msg"])) {
  476. $_SESSION["ok_msg"] = htmlify_trans(
  477. sprintf(
  478. _("Mail account {%s@%s} has been created successfully."),
  479. htmlentities(strtolower($_POST["v_account"])),
  480. htmlentities($_POST["v_domain"]),
  481. ),
  482. "</a>",
  483. '<a class="u-text-bold" href="/edit/mail/?account=' .
  484. htmlentities(strtolower($_POST["v_account"])) .
  485. "&domain=" .
  486. htmlentities($_POST["v_domain"]) .
  487. '">',
  488. );
  489. unset($v_account);
  490. unset($v_password);
  491. unset($v_aliases);
  492. unset($v_fwd);
  493. unset($v_quota);
  494. }
  495. }
  496. // Render page
  497. if (empty($_GET["domain"])) {
  498. // Display body for mail domain
  499. if (!empty($_POST["v_webmail"])) {
  500. $v_webmail = $_POST["v_webmail"];
  501. } else {
  502. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  503. $v_webmail = "roundcube";
  504. }
  505. if (empty($_GET["accept"])) {
  506. $_GET["accept"] = false;
  507. }
  508. if (empty($v_domain)) {
  509. $v_domain = "";
  510. }
  511. if (empty($v_smtp_relay)) {
  512. $v_smtp_relay = "";
  513. }
  514. if (empty($v_smtp_relay_user)) {
  515. $v_smtp_relay_user = "";
  516. }
  517. if (empty($v_smtp_relay_password)) {
  518. $v_smtp_relay_password = "";
  519. }
  520. if (empty($v_smtp_relay_host)) {
  521. $v_smtp_relay_host = "";
  522. }
  523. if (empty($v_smtp_relay_port)) {
  524. $v_smtp_relay_port = "";
  525. }
  526. $accept = $_GET["accept"] ?? "";
  527. render_page($user, $TAB, "add_mail");
  528. } else {
  529. // Display body for mail account
  530. if (empty($v_account)) {
  531. $v_account = "";
  532. }
  533. if (empty($v_quota)) {
  534. $v_quota = "";
  535. }
  536. if (empty($v_rate)) {
  537. $v_rate = "";
  538. }
  539. if (empty($v_blackhole)) {
  540. $v_blackhole = "";
  541. }
  542. if (empty($v_fwd_only)) {
  543. $v_fwd_only = "";
  544. }
  545. if (empty($v_aliases)) {
  546. $v_aliases = "";
  547. }
  548. if (empty($v_send_email)) {
  549. $v_send_email = "";
  550. }
  551. if (empty($v_fwd)) {
  552. $v_fwd = "";
  553. }
  554. $v_domain = $_GET["domain"];
  555. render_page($user, $TAB, "add_mail_acc");
  556. }
  557. // Flush session messages
  558. unset($_SESSION["error_msg"]);
  559. unset($_SESSION["ok_msg"]);