index.php 11 KB

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