index.php 11 KB

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