index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  31. header('location: /login/');
  32. exit();
  33. }
  34. // Check empty fields
  35. if (empty($_POST['v_domain'])) $errors[] = _('domain');
  36. if (!empty($errors[0])) {
  37. foreach ($errors as $i => $error) {
  38. if ( $i == 0 ) {
  39. $error_msg = $error;
  40. } else {
  41. $error_msg = $error_msg.", ".$error;
  42. }
  43. }
  44. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  45. }
  46. // Check antispam option
  47. if (!empty($_POST['v_antispam'])) {
  48. $v_antispam = 'yes';
  49. } else {
  50. $v_antispam = 'no';
  51. }
  52. // Check antivirus option
  53. if (!empty($_POST['v_antivirus'])) {
  54. $v_antivirus = 'yes';
  55. } else {
  56. $v_antivirus = 'no';
  57. }
  58. // Check dkim option
  59. if (!empty($_POST['v_dkim'])) {
  60. $v_dkim = 'yes';
  61. } else {
  62. $v_dkim = 'no';
  63. }
  64. // Set domain name to lowercase and remove www prefix
  65. $v_domain = preg_replace("/^www./i", "", $_POST['v_domain']);
  66. $v_domain = escapeshellarg($v_domain);
  67. $v_domain = strtolower($v_domain);
  68. // Add mail domain
  69. if (empty($_SESSION['error_msg'])) {
  70. exec (HESTIA_CMD."v-add-mail-domain ".$user." ".$v_domain." ".$v_antispam." ".$v_antivirus." ".$v_dkim, $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 = escapeshellarg($_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'])) && (!empty($_POST['v_smtp_relay_user']))) {
  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. if (!empty($_POST['v_smtp_relay_pass'])) {
  100. $v_smtp_relay = true;
  101. $v_smtp_relay_host = escapeshellarg($_POST['v_smtp_relay_host']);
  102. $v_smtp_relay_user = escapeshellarg($_POST['v_smtp_relay_user']);
  103. $v_smtp_relay_pass = escapeshellarg($_POST['v_smtp_relay_pass']);
  104. if (!empty($_POST['v_smtp_relay_port'])) {
  105. $v_smtp_relay_port = escapeshellarg($_POST['v_smtp_relay_port']);
  106. } else {
  107. $v_smtp_relay_port = '587';
  108. }
  109. 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);
  110. check_return_code($return_var,$output);
  111. unset($output);
  112. } else {
  113. $_SESSION['error_msg'] = _('SMTP Relay Password is required');
  114. }
  115. }
  116. }
  117. }
  118. // Flush field values on success
  119. if (empty($_SESSION['error_msg'])) {
  120. $_SESSION['ok_msg'] = sprintf(_('MAIL_DOMAIN_CREATED_OK'),htmlentities($_POST['v_domain']),htmlentities($_POST['v_domain']));
  121. unset($v_domain, $v_webmail);
  122. }
  123. }
  124. // Check POST request for mail account
  125. if (!empty($_POST['ok_acc'])) {
  126. // Check token
  127. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  128. header('location: /login/');
  129. exit();
  130. }
  131. // Check antispam option
  132. if (!empty($_POST['v_blackhole'])) {
  133. $v_blackhole = 'yes';
  134. } else {
  135. $v_blackhole = 'no';
  136. }
  137. // Check empty fields
  138. if (empty($_POST['v_domain'])) $errors[] = _('domain');
  139. if (empty($_POST['v_account'])) $errors[] = _('account');
  140. if ((empty($_POST['v_fwd_only']) && empty($_POST['v_password']))) {
  141. if (empty($_POST['v_password'])) $errors[] = _('password');
  142. }
  143. if (!empty($errors[0])) {
  144. foreach ($errors as $i => $error) {
  145. if ( $i == 0 ) {
  146. $error_msg = $error;
  147. } else {
  148. $error_msg = $error_msg.", ".$error;
  149. }
  150. }
  151. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  152. }
  153. // Validate email
  154. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  155. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  156. $_SESSION['error_msg'] = _('Please enter valid email address.');
  157. }
  158. }
  159. // Check password length
  160. if (empty($_SESSION['error_msg']) && (empty($_POST['v_fwd_only']))) {
  161. if (!validate_password($_POST['v_password'])) { $_SESSION['error_msg'] = _('Password does not match the minimum requirements');}
  162. }
  163. // Protect input
  164. $v_domain = escapeshellarg($_POST['v_domain']);
  165. $v_domain = strtolower($v_domain);
  166. $v_account = escapeshellarg($_POST['v_account']);
  167. $v_quota = escapeshellarg($_POST['v_quota']);
  168. $v_send_email = $_POST['v_send_email'];
  169. $v_credentials = $_POST['v_credentials'];
  170. $v_aliases = $_POST['v_aliases'];
  171. $v_fwd = $_POST['v_fwd'];
  172. if (empty($_POST['v_quota'])) $v_quota = 0;
  173. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  174. // Add Mail Account
  175. if (empty($_SESSION['error_msg'])) {
  176. $v_password = tempnam("/tmp","vst");
  177. $fp = fopen($v_password, "w");
  178. fwrite($fp, $_POST['v_password']."\n");
  179. fclose($fp);
  180. exec (HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  181. check_return_code($return_var,$output);
  182. unset($output);
  183. unlink($v_password);
  184. $v_password = escapeshellarg($_POST['v_password']);
  185. }
  186. // Add Aliases
  187. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  188. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  189. $valiases = preg_replace("/,/", " ", $valiases);
  190. $valiases = preg_replace('/\s+/', ' ',$valiases);
  191. $valiases = trim($valiases);
  192. $aliases = explode(" ", $valiases);
  193. foreach ($aliases as $alias) {
  194. $alias = escapeshellarg($alias);
  195. if (empty($_SESSION['error_msg'])) {
  196. exec (HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  197. check_return_code($return_var,$output);
  198. unset($output);
  199. }
  200. }
  201. }
  202. if ((!empty($_POST['v_blackhole'])) && (empty($_SESSION['error_msg']))){
  203. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." :blackhole:", $output, $return_var);
  204. check_return_code($return_var,$output);
  205. unset($output);
  206. //disable any input in v_fwd
  207. $_POST['v_fwd'] = '';
  208. }
  209. // Add Forwarders
  210. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  211. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  212. $vfwd = preg_replace("/,/", " ", $vfwd);
  213. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  214. $vfwd = trim($vfwd);
  215. $fwd = explode(" ", $vfwd);
  216. foreach ($fwd as $forward) {
  217. $forward = escapeshellarg($forward);
  218. if (empty($_SESSION['error_msg'])) {
  219. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  220. check_return_code($return_var,$output);
  221. unset($output);
  222. }
  223. }
  224. }
  225. // Add fwd_only flag
  226. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  227. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  228. check_return_code($return_var,$output);
  229. unset($output);
  230. }
  231. // Get webmail url
  232. if (empty($_SESSION['error_msg'])) {
  233. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  234. $webmail = "http://".$hostname."/".$v_webmail_alias."/";
  235. if (!empty($_SESSION['WEBMAIL_ALIAS'])) $webmail = $_SESSION['WEBMAIL_ALIAS'];
  236. }
  237. // Email login credentials
  238. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  239. $to = $v_send_email;
  240. $subject = _("Email Credentials");
  241. $hostname = exec('hostname');
  242. $from = "noreply@".$hostname;
  243. $from_name = _('Hestia Control Panel');
  244. $mailtext = $v_credentials;
  245. send_email($to, $subject, $mailtext, $from, $from_name);
  246. }
  247. // Flush field values on success
  248. if (empty($_SESSION['error_msg'])) {
  249. $_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']));
  250. unset($v_account);
  251. unset($v_password);
  252. unset($v_aliases);
  253. unset($v_fwd);
  254. unset($v_quota);
  255. }
  256. }
  257. // Render page
  258. if (empty($_GET['domain'])) {
  259. // Display body for mail domain
  260. if( !empty($_POST['v_webmail']) ){
  261. $v_webmail = $_POST['v_webmail'];
  262. }else{
  263. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  264. $v_webmail = 'roundcube';
  265. }
  266. render_page($user, $TAB, 'add_mail');
  267. } else {
  268. // Display body for mail account
  269. $v_domain = $_GET['domain'];
  270. render_page($user, $TAB, 'add_mail_acc');
  271. }
  272. // Flush session messages
  273. unset($_SESSION['error_msg']);
  274. unset($_SESSION['ok_msg']);