index.php 12 KB

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