index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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"] = sprintf(
  172. _("MAIL_DOMAIN_CREATED_OK"),
  173. htmlentities($_POST["v_domain"]),
  174. htmlentities($_POST["v_domain"]),
  175. );
  176. unset($v_domain, $v_webmail);
  177. }
  178. }
  179. // Check POST request for mail account
  180. if (!empty($_POST["ok_acc"])) {
  181. // Check token
  182. if (!isset($_POST["token"]) || $_SESSION["token"] != $_POST["token"]) {
  183. header("location: /login/");
  184. exit();
  185. }
  186. // Check antispam option
  187. if (!empty($_POST["v_blackhole"])) {
  188. $v_blackhole = "yes";
  189. } else {
  190. $v_blackhole = "no";
  191. }
  192. // Check empty fields
  193. if (empty($_POST["v_domain"])) {
  194. $errors[] = _("domain");
  195. }
  196. if (empty($_POST["v_account"])) {
  197. $errors[] = _("account");
  198. }
  199. if (empty($_POST["v_fwd_only"]) && empty($_POST["v_password"])) {
  200. if (empty($_POST["v_password"])) {
  201. $errors[] = _("password");
  202. }
  203. }
  204. if (!empty($errors[0])) {
  205. foreach ($errors as $i => $error) {
  206. if ($i == 0) {
  207. $error_msg = $error;
  208. } else {
  209. $error_msg = $error_msg . ", " . $error;
  210. }
  211. }
  212. $_SESSION["error_msg"] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  213. }
  214. // Validate email
  215. if (!empty($_POST["v_send_email"]) && empty($_SESSION["error_msg"])) {
  216. if (!filter_var($_POST["v_send_email"], FILTER_VALIDATE_EMAIL)) {
  217. $_SESSION["error_msg"] = _("Please enter valid email address.");
  218. }
  219. }
  220. // Check password length
  221. if (empty($_SESSION["error_msg"]) && empty($_POST["v_fwd_only"])) {
  222. if (!validate_password($_POST["v_password"])) {
  223. $_SESSION["error_msg"] = _("Password does not match the minimum requirements");
  224. }
  225. }
  226. // Protect input
  227. $v_domain = quoteshellarg($_POST["v_domain"]);
  228. $v_domain = strtolower($v_domain);
  229. $v_account = quoteshellarg($_POST["v_account"]);
  230. $v_quota = quoteshellarg($_POST["v_quota"]);
  231. $v_send_email = $_POST["v_send_email"];
  232. $v_credentials = $_POST["v_credentials"];
  233. $v_aliases = $_POST["v_aliases"];
  234. $v_fwd = $_POST["v_fwd"];
  235. if (empty($_POST["v_quota"])) {
  236. $v_quota = 0;
  237. }
  238. if (!empty($_POST["v_quota"]) || !empty($_POST["v_aliases"]) || !empty($_POST["v_fwd"])) {
  239. $v_adv = "yes";
  240. }
  241. // Add Mail Account
  242. if (empty($_SESSION["error_msg"])) {
  243. $v_password = tempnam("/tmp", "vst");
  244. $fp = fopen($v_password, "w");
  245. fwrite($fp, $_POST["v_password"] . "\n");
  246. fclose($fp);
  247. exec(
  248. HESTIA_CMD .
  249. "v-add-mail-account " .
  250. $user .
  251. " " .
  252. $v_domain .
  253. " " .
  254. $v_account .
  255. " " .
  256. $v_password .
  257. " " .
  258. $v_quota,
  259. $output,
  260. $return_var,
  261. );
  262. check_return_code($return_var, $output);
  263. unset($output);
  264. unlink($v_password);
  265. $v_password = quoteshellarg($_POST["v_password"]);
  266. }
  267. // Add Aliases
  268. if (!empty($_POST["v_aliases"]) && empty($_SESSION["error_msg"])) {
  269. $valiases = preg_replace("/\n/", " ", $_POST["v_aliases"]);
  270. $valiases = preg_replace("/,/", " ", $valiases);
  271. $valiases = preg_replace("/\s+/", " ", $valiases);
  272. $valiases = trim($valiases);
  273. $aliases = explode(" ", $valiases);
  274. foreach ($aliases as $alias) {
  275. $alias = quoteshellarg($alias);
  276. if (empty($_SESSION["error_msg"])) {
  277. exec(
  278. HESTIA_CMD .
  279. "v-add-mail-account-alias " .
  280. $user .
  281. " " .
  282. $v_domain .
  283. " " .
  284. $v_account .
  285. " " .
  286. $alias,
  287. $output,
  288. $return_var,
  289. );
  290. check_return_code($return_var, $output);
  291. unset($output);
  292. }
  293. }
  294. }
  295. if (!empty($_POST["v_blackhole"]) && empty($_SESSION["error_msg"])) {
  296. exec(
  297. HESTIA_CMD .
  298. "v-add-mail-account-forward " .
  299. $user .
  300. " " .
  301. $v_domain .
  302. " " .
  303. $v_account .
  304. " :blackhole:",
  305. $output,
  306. $return_var,
  307. );
  308. check_return_code($return_var, $output);
  309. unset($output);
  310. //disable any input in v_fwd
  311. $_POST["v_fwd"] = "";
  312. }
  313. // Add Forwarders
  314. if (!empty($_POST["v_fwd"]) && empty($_SESSION["error_msg"])) {
  315. $vfwd = preg_replace("/\n/", " ", $_POST["v_fwd"]);
  316. $vfwd = preg_replace("/,/", " ", $vfwd);
  317. $vfwd = preg_replace("/\s+/", " ", $vfwd);
  318. $vfwd = trim($vfwd);
  319. $fwd = explode(" ", $vfwd);
  320. foreach ($fwd as $forward) {
  321. $forward = quoteshellarg($forward);
  322. if (empty($_SESSION["error_msg"])) {
  323. exec(
  324. HESTIA_CMD .
  325. "v-add-mail-account-forward " .
  326. $user .
  327. " " .
  328. $v_domain .
  329. " " .
  330. $v_account .
  331. " " .
  332. $forward,
  333. $output,
  334. $return_var,
  335. );
  336. check_return_code($return_var, $output);
  337. unset($output);
  338. }
  339. }
  340. }
  341. // Add fwd_only flag
  342. if (!empty($_POST["v_fwd_only"]) && empty($_SESSION["error_msg"])) {
  343. exec(
  344. HESTIA_CMD .
  345. "v-add-mail-account-fwd-only " .
  346. $user .
  347. " " .
  348. $v_domain .
  349. " " .
  350. $v_account,
  351. $output,
  352. $return_var,
  353. );
  354. check_return_code($return_var, $output);
  355. unset($output);
  356. }
  357. // Add fwd_only flag
  358. if (
  359. !empty($_POST["v_rate"]) &&
  360. empty($_SESSION["error_msg"]) &&
  361. $_SESSION["userContext"] == "admin"
  362. ) {
  363. $v_rate = quoteshellarg($_POST["v_rate"]);
  364. exec(
  365. HESTIA_CMD .
  366. "v-change-mail-account-rate-limit " .
  367. $user .
  368. " " .
  369. $v_domain .
  370. " " .
  371. $v_account .
  372. " " .
  373. $v_rate,
  374. $output,
  375. $return_var,
  376. );
  377. check_return_code($return_var, $output);
  378. unset($output);
  379. }
  380. // Get webmail url
  381. if (empty($_SESSION["error_msg"])) {
  382. [$hostname, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
  383. $webmail = "http://" . $hostname . "/" . $v_webmail_alias . "/";
  384. if (!empty($_SESSION["WEBMAIL_ALIAS"])) {
  385. $webmail = $_SESSION["WEBMAIL_ALIAS"];
  386. }
  387. }
  388. // Email login credentials
  389. if (!empty($v_send_email) && empty($_SESSION["error_msg"])) {
  390. $to = $v_send_email;
  391. $subject = _("Email Credentials");
  392. $hostname = get_hostname();
  393. $from = "noreply@" . $hostname;
  394. $from_name = _("Hestia Control Panel");
  395. $mailtext = $v_credentials;
  396. send_email($to, $subject, $mailtext, $from, $from_name);
  397. }
  398. // Flush field values on success
  399. if (empty($_SESSION["error_msg"])) {
  400. $_SESSION["ok_msg"] = sprintf(
  401. _("MAIL_ACCOUNT_CREATED_OK"),
  402. htmlentities(strtolower($_POST["v_account"])),
  403. htmlentities($_POST["v_domain"]),
  404. htmlentities(strtolower($_POST["v_account"])),
  405. htmlentities($_POST["v_domain"]),
  406. );
  407. unset($v_account);
  408. unset($v_password);
  409. unset($v_aliases);
  410. unset($v_fwd);
  411. unset($v_quota);
  412. }
  413. }
  414. // Render page
  415. if (empty($_GET["domain"])) {
  416. // Display body for mail domain
  417. if (!empty($_POST["v_webmail"])) {
  418. $v_webmail = $_POST["v_webmail"];
  419. } else {
  420. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  421. $v_webmail = "roundcube";
  422. }
  423. if (empty($_GET["accept"])) {
  424. $_GET["accept"] = false;
  425. }
  426. if (empty($v_domain)) {
  427. $v_domain = "";
  428. }
  429. if (empty($v_smtp_relay)) {
  430. $v_smtp_relay = "";
  431. }
  432. if (empty($v_smtp_relay_user)) {
  433. $v_smtp_relay_user = "";
  434. }
  435. if (empty($v_smtp_relay_password)) {
  436. $v_smtp_relay_password = "";
  437. }
  438. if (empty($v_smtp_relay_host)) {
  439. $v_smtp_relay_host = "";
  440. }
  441. if (empty($v_smtp_relay_port)) {
  442. $v_smtp_relay_port = "";
  443. }
  444. render_page($user, $TAB, "add_mail");
  445. } else {
  446. // Display body for mail account
  447. if (empty($v_account)) {
  448. $v_account = "";
  449. }
  450. if (empty($v_quota)) {
  451. $v_quota = "";
  452. }
  453. if (empty($v_rate)) {
  454. $v_rate = "";
  455. }
  456. if (empty($v_blackhole)) {
  457. $v_blackhole = "";
  458. }
  459. if (empty($v_fwd_only)) {
  460. $v_fwd_only = "";
  461. }
  462. if (empty($v_aliases)) {
  463. $v_aliases = "";
  464. }
  465. if (empty($v_send_email)) {
  466. $v_send_email = "";
  467. }
  468. if (empty($v_fwd)) {
  469. $v_fwd = "";
  470. }
  471. $v_domain = $_GET["domain"];
  472. render_page($user, $TAB, "add_mail_acc");
  473. }
  474. // Flush session messages
  475. unset($_SESSION["error_msg"]);
  476. unset($_SESSION["ok_msg"]);