index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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(HESTIA_CMD."v-list-mail-domain ".$user." ".quoteshellarg($v_domain)." json", $output, $return_var);
  16. if ($return_var > 0) {
  17. check_return_code_redirect($return_var, $output, '/list/mail/');
  18. }
  19. $data = json_decode(implode('', $output), true);
  20. unset($output);
  21. $v_webmail_alias = $data[$v_domain]['WEBMAIL_ALIAS'];
  22. }
  23. // Check POST request for mail domain
  24. if (!empty($_POST['ok'])) {
  25. // Check token
  26. verify_csrf($_POST);
  27. // Check empty fields
  28. if (empty($_POST['v_domain'])) {
  29. $errors[] = _('domain');
  30. }
  31. if (!empty($errors[0])) {
  32. foreach ($errors as $i => $error) {
  33. if ($i == 0) {
  34. $error_msg = $error;
  35. } else {
  36. $error_msg = $error_msg.", ".$error;
  37. }
  38. }
  39. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  40. }
  41. // Check antispam option
  42. if (!empty($_POST['v_antispam'])) {
  43. $v_antispam = 'yes';
  44. } else {
  45. $v_antispam = 'no';
  46. }
  47. // Check antivirus option
  48. if (!empty($_POST['v_antivirus'])) {
  49. $v_antivirus = 'yes';
  50. } else {
  51. $v_antivirus = 'no';
  52. }
  53. // Check dkim option
  54. if (!empty($_POST['v_dkim'])) {
  55. $v_dkim = 'yes';
  56. } else {
  57. $v_dkim = 'no';
  58. }
  59. // Set domain name to lowercase and remove www prefix
  60. $v_domain = preg_replace("/^www./i", "", $_POST['v_domain']);
  61. $v_domain = quoteshellarg($v_domain);
  62. $v_domain = strtolower($v_domain);
  63. // Add mail domain
  64. if (empty($_SESSION['error_msg'])) {
  65. exec(HESTIA_CMD."v-add-mail-domain ".$user." ".$v_domain." ".$v_antispam." ".$v_antivirus." ".$v_dkim, $output, $return_var);
  66. check_return_code($return_var, $output);
  67. unset($output);
  68. }
  69. if (!empty($_POST['v_reject']) && $v_antispam == "yes") {
  70. exec(HESTIA_CMD."v-add-mail-domain-reject ".$user." ".$v_domain." yes", $output, $return_var);
  71. check_return_code($return_var, $output);
  72. unset($output);
  73. }
  74. if (!empty($_SESSION['IMAP_SYSTEM']) && !empty($_SESSION['WEBMAIL_SYSTEM'])) {
  75. if (empty($_SESSION['error_msg'])) {
  76. if (!empty($_POST['v_webmail'])) {
  77. $v_webmail = quoteshellarg($_POST['v_webmail']);
  78. exec(HESTIA_CMD."v-add-mail-domain-webmail ".$user." ".$v_domain." ".$v_webmail." yes", $output, $return_var);
  79. check_return_code($return_var, $output);
  80. unset($output);
  81. }
  82. }
  83. }
  84. if (!empty($_SESSION['IMAP_SYSTEM']) && !empty($_SESSION['WEBMAIL_SYSTEM'])) {
  85. if (empty($_POST['v_webmail'])) {
  86. if (empty($_SESSION['error_msg'])) {
  87. exec(HESTIA_CMD."v-delete-mail-domain-webmail ".$user." ".$v_domain." yes", $output, $return_var);
  88. check_return_code($return_var, $output);
  89. unset($output);
  90. }
  91. }
  92. }
  93. // Add SMTP Relay Support
  94. if (empty($_SESSION['error_msg'])) {
  95. if (isset($_POST['v_smtp_relay']) && !empty($_POST['v_smtp_relay_host'])) {
  96. if (($_POST['v_smtp_relay_host'] != $v_smtp_relay_host) ||
  97. ($_POST['v_smtp_relay_user'] != $v_smtp_relay_user) ||
  98. ($_POST['v_smtp_relay_port'] != $v_smtp_relay_port)) {
  99. $v_smtp_relay = true;
  100. $v_smtp_relay_host = quoteshellarg($_POST['v_smtp_relay_host']);
  101. $v_smtp_relay_user = quoteshellarg($_POST['v_smtp_relay_user']);
  102. $v_smtp_relay_pass = quoteshellarg($_POST['v_smtp_relay_pass']);
  103. if (!empty($_POST['v_smtp_relay_port'])) {
  104. $v_smtp_relay_port = quoteshellarg($_POST['v_smtp_relay_port']);
  105. } else {
  106. $v_smtp_relay_port = '587';
  107. }
  108. exec(HESTIA_CMD."v-add-mail-domain-smtp-relay ".$user." ".$v_domain." ".$v_smtp_relay_host." '".$v_smtp_relay_user."' '".$v_smtp_relay_pass."' ".$v_smtp_relay_port, $output, $return_var);
  109. check_return_code($return_var, $output);
  110. unset($output);
  111. }
  112. }
  113. }
  114. // Flush field values on success
  115. if (empty($_SESSION['error_msg'])) {
  116. $_SESSION['ok_msg'] = sprintf(_('MAIL_DOMAIN_CREATED_OK'), htmlentities($_POST['v_domain']), htmlentities($_POST['v_domain']));
  117. unset($v_domain, $v_webmail);
  118. }
  119. }
  120. // Check POST request for mail account
  121. if (!empty($_POST['ok_acc'])) {
  122. // Check token
  123. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  124. header('location: /login/');
  125. exit();
  126. }
  127. // Check antispam option
  128. if (!empty($_POST['v_blackhole'])) {
  129. $v_blackhole = 'yes';
  130. } else {
  131. $v_blackhole = 'no';
  132. }
  133. // Check empty fields
  134. if (empty($_POST['v_domain'])) {
  135. $errors[] = _('domain');
  136. }
  137. if (empty($_POST['v_account'])) {
  138. $errors[] = _('account');
  139. }
  140. if ((empty($_POST['v_fwd_only']) && empty($_POST['v_password']))) {
  141. if (empty($_POST['v_password'])) {
  142. $errors[] = _('password');
  143. }
  144. }
  145. if (!empty($errors[0])) {
  146. foreach ($errors as $i => $error) {
  147. if ($i == 0) {
  148. $error_msg = $error;
  149. } else {
  150. $error_msg = $error_msg.", ".$error;
  151. }
  152. }
  153. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  154. }
  155. // Validate email
  156. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  157. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  158. $_SESSION['error_msg'] = _('Please enter valid email address.');
  159. }
  160. }
  161. // Check password length
  162. if (empty($_SESSION['error_msg']) && (empty($_POST['v_fwd_only']))) {
  163. if (!validate_password($_POST['v_password'])) {
  164. $_SESSION['error_msg'] = _('Password does not match the minimum requirements');
  165. }
  166. }
  167. // Protect input
  168. $v_domain = quoteshellarg($_POST['v_domain']);
  169. $v_domain = strtolower($v_domain);
  170. $v_account = quoteshellarg($_POST['v_account']);
  171. $v_quota = quoteshellarg($_POST['v_quota']);
  172. $v_send_email = $_POST['v_send_email'];
  173. $v_credentials = $_POST['v_credentials'];
  174. $v_aliases = $_POST['v_aliases'];
  175. $v_fwd = $_POST['v_fwd'];
  176. if (empty($_POST['v_quota'])) {
  177. $v_quota = 0;
  178. }
  179. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd']))) {
  180. $v_adv = 'yes';
  181. }
  182. // Add Mail Account
  183. if (empty($_SESSION['error_msg'])) {
  184. $v_password = tempnam("/tmp", "vst");
  185. $fp = fopen($v_password, "w");
  186. fwrite($fp, $_POST['v_password']."\n");
  187. fclose($fp);
  188. exec(HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  189. check_return_code($return_var, $output);
  190. unset($output);
  191. unlink($v_password);
  192. $v_password = quoteshellarg($_POST['v_password']);
  193. }
  194. // Add Aliases
  195. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  196. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  197. $valiases = preg_replace("/,/", " ", $valiases);
  198. $valiases = preg_replace('/\s+/', ' ', $valiases);
  199. $valiases = trim($valiases);
  200. $aliases = explode(" ", $valiases);
  201. foreach ($aliases as $alias) {
  202. $alias = quoteshellarg($alias);
  203. if (empty($_SESSION['error_msg'])) {
  204. exec(HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  205. check_return_code($return_var, $output);
  206. unset($output);
  207. }
  208. }
  209. }
  210. if ((!empty($_POST['v_blackhole'])) && (empty($_SESSION['error_msg']))) {
  211. exec(HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." :blackhole:", $output, $return_var);
  212. check_return_code($return_var, $output);
  213. unset($output);
  214. //disable any input in v_fwd
  215. $_POST['v_fwd'] = '';
  216. }
  217. // Add Forwarders
  218. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  219. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  220. $vfwd = preg_replace("/,/", " ", $vfwd);
  221. $vfwd = preg_replace('/\s+/', ' ', $vfwd);
  222. $vfwd = trim($vfwd);
  223. $fwd = explode(" ", $vfwd);
  224. foreach ($fwd as $forward) {
  225. $forward = quoteshellarg($forward);
  226. if (empty($_SESSION['error_msg'])) {
  227. exec(HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  228. check_return_code($return_var, $output);
  229. unset($output);
  230. }
  231. }
  232. }
  233. // Add fwd_only flag
  234. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  235. exec(HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  236. check_return_code($return_var, $output);
  237. unset($output);
  238. }
  239. // Add fwd_only flag
  240. if ((!empty($_POST['v_rate'])) && (empty($_SESSION['error_msg'])) && $_SESSION['userContext'] == 'admin') {
  241. $v_rate = quoteshellarg($_POST['v_rate']);
  242. exec(HESTIA_CMD."v-change-mail-account-rate-limit ".$user." ".$v_domain." ".$v_account." ".$v_rate, $output, $return_var);
  243. check_return_code($return_var, $output);
  244. unset($output);
  245. }
  246. // Get webmail url
  247. if (empty($_SESSION['error_msg'])) {
  248. list($hostname, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  249. $webmail = "http://".$hostname."/".$v_webmail_alias."/";
  250. if (!empty($_SESSION['WEBMAIL_ALIAS'])) {
  251. $webmail = $_SESSION['WEBMAIL_ALIAS'];
  252. }
  253. }
  254. // Email login credentials
  255. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  256. $to = $v_send_email;
  257. $subject = _("Email Credentials");
  258. $hostname = get_hostname();
  259. $from = "noreply@".$hostname;
  260. $from_name = _('Hestia Control Panel');
  261. $mailtext = $v_credentials;
  262. send_email($to, $subject, $mailtext, $from, $from_name);
  263. }
  264. // Flush field values on success
  265. if (empty($_SESSION['error_msg'])) {
  266. $_SESSION['ok_msg'] = sprintf(_('MAIL_ACCOUNT_CREATED_OK'), htmlentities(strtolower($_POST['v_account'])), htmlentities($_POST['v_domain']), htmlentities(strtolower($_POST['v_account'])), htmlentities($_POST['v_domain']));
  267. unset($v_account);
  268. unset($v_password);
  269. unset($v_aliases);
  270. unset($v_fwd);
  271. unset($v_quota);
  272. }
  273. }
  274. // Render page
  275. if (empty($_GET['domain'])) {
  276. // Display body for mail domain
  277. if (!empty($_POST['v_webmail'])) {
  278. $v_webmail = $_POST['v_webmail'];
  279. } else {
  280. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  281. $v_webmail = 'roundcube';
  282. }
  283. if (empty($_GET['accept'])) {
  284. $_GET['accept'] = false;
  285. }
  286. if (empty($v_domain)) {
  287. $v_domain = '';
  288. }
  289. if (empty($v_smtp_relay)) {
  290. $v_smtp_relay = '';
  291. }
  292. if (empty($v_smtp_relay_user)) {
  293. $v_smtp_relay_user = '';
  294. }
  295. if (empty($v_smtp_relay_password)) {
  296. $v_smtp_relay_password = '';
  297. }
  298. if (empty($v_smtp_relay_host)) {
  299. $v_smtp_relay_host = '';
  300. }
  301. if (empty($v_smtp_relay_port)) {
  302. $v_smtp_relay_port = '';
  303. }
  304. render_page($user, $TAB, 'add_mail');
  305. } else {
  306. // Display body for mail account
  307. if (empty($v_account)) {
  308. $v_account = '';
  309. }
  310. if (empty($v_quota)) {
  311. $v_quota = '';
  312. }
  313. if (empty($v_rate)) {
  314. $v_rate = '';
  315. }
  316. if (empty($v_blackhole)) {
  317. $v_blackhole = '';
  318. }
  319. if (empty($v_fwd_only)) {
  320. $v_fwd_only = '';
  321. }
  322. if (empty($v_aliases)) {
  323. $v_aliases = '';
  324. }
  325. if (empty($v_send_email)) {
  326. $v_send_email = '';
  327. }
  328. if (empty($v_fwd)) {
  329. $v_fwd = '';
  330. }
  331. $v_domain = $_GET['domain'];
  332. render_page($user, $TAB, 'add_mail_acc');
  333. }
  334. // Flush session messages
  335. unset($_SESSION['error_msg']);
  336. unset($_SESSION['ok_msg']);