index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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-sys-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($_POST['v_webmail'])) {
  85. if (empty($_SESSION['error_msg'])) {
  86. exec (HESTIA_CMD."v-delete-sys-webmail ".$user." ".$v_domain." yes", $output, $return_var);
  87. check_return_code($return_var,$output);
  88. unset($output);
  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'])) $errors[] = _('domain');
  137. if (empty($_POST['v_account'])) $errors[] = _('account');
  138. if ((empty($_POST['v_fwd_only']) && empty($_POST['v_password']))) {
  139. if (empty($_POST['v_password'])) $errors[] = _('password');
  140. }
  141. if (!empty($errors[0])) {
  142. foreach ($errors as $i => $error) {
  143. if ( $i == 0 ) {
  144. $error_msg = $error;
  145. } else {
  146. $error_msg = $error_msg.", ".$error;
  147. }
  148. }
  149. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  150. }
  151. // Validate email
  152. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  153. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  154. $_SESSION['error_msg'] = _('Please enter valid email address.');
  155. }
  156. }
  157. // Check password length
  158. if (empty($_SESSION['error_msg']) && (empty($_POST['v_fwd_only']))) {
  159. if (!validate_password($_POST['v_password'])) { $_SESSION['error_msg'] = _('Password does not match the minimum requirements');}
  160. }
  161. // Protect input
  162. $v_domain = escapeshellarg($_POST['v_domain']);
  163. $v_domain = strtolower($v_domain);
  164. $v_account = escapeshellarg($_POST['v_account']);
  165. $v_quota = escapeshellarg($_POST['v_quota']);
  166. $v_send_email = $_POST['v_send_email'];
  167. $v_credentials = $_POST['v_credentials'];
  168. $v_aliases = $_POST['v_aliases'];
  169. $v_fwd = $_POST['v_fwd'];
  170. if (empty($_POST['v_quota'])) $v_quota = 0;
  171. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  172. // Add Mail Account
  173. if (empty($_SESSION['error_msg'])) {
  174. $v_password = tempnam("/tmp","vst");
  175. $fp = fopen($v_password, "w");
  176. fwrite($fp, $_POST['v_password']."\n");
  177. fclose($fp);
  178. exec (HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  179. check_return_code($return_var,$output);
  180. unset($output);
  181. unlink($v_password);
  182. $v_password = escapeshellarg($_POST['v_password']);
  183. }
  184. // Add Aliases
  185. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  186. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  187. $valiases = preg_replace("/,/", " ", $valiases);
  188. $valiases = preg_replace('/\s+/', ' ',$valiases);
  189. $valiases = trim($valiases);
  190. $aliases = explode(" ", $valiases);
  191. foreach ($aliases as $alias) {
  192. $alias = escapeshellarg($alias);
  193. if (empty($_SESSION['error_msg'])) {
  194. exec (HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  195. check_return_code($return_var,$output);
  196. unset($output);
  197. }
  198. }
  199. }
  200. if ((!empty($_POST['v_blackhole'])) && (empty($_SESSION['error_msg']))){
  201. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." :blackhole:", $output, $return_var);
  202. check_return_code($return_var,$output);
  203. unset($output);
  204. //disable any input in v_fwd
  205. $_POST['v_fwd'] = '';
  206. }
  207. // Add Forwarders
  208. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  209. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  210. $vfwd = preg_replace("/,/", " ", $vfwd);
  211. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  212. $vfwd = trim($vfwd);
  213. $fwd = explode(" ", $vfwd);
  214. foreach ($fwd as $forward) {
  215. $forward = escapeshellarg($forward);
  216. if (empty($_SESSION['error_msg'])) {
  217. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  218. check_return_code($return_var,$output);
  219. unset($output);
  220. }
  221. }
  222. }
  223. // Add fwd_only flag
  224. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  225. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  226. check_return_code($return_var,$output);
  227. unset($output);
  228. }
  229. // Get webmail url
  230. if (empty($_SESSION['error_msg'])) {
  231. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  232. $webmail = "http://".$hostname."/".$v_webmail_alias."/";
  233. if (!empty($_SESSION['WEBMAIL_ALIAS'])) $webmail = $_SESSION['WEBMAIL_ALIAS'];
  234. }
  235. // Email login credentials
  236. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  237. $to = $v_send_email;
  238. $subject = _("Email Credentials");
  239. $hostname = exec('hostname');
  240. $from = sprintf(_('MAIL_FROM'), $hostname);
  241. $mailtext = $v_credentials;
  242. send_email($to, $subject, $mailtext, $from);
  243. }
  244. // Flush field values on success
  245. if (empty($_SESSION['error_msg'])) {
  246. $_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]));
  247. unset($v_account);
  248. unset($v_password);
  249. unset($v_password);
  250. unset($v_aliases);
  251. unset($v_fwd);
  252. unset($v_quota);
  253. }
  254. }
  255. // Render page
  256. if (empty($_GET['domain'])) {
  257. // Display body for mail domain
  258. if( !empty($_POST['v_webmail']) ){
  259. $v_webmail = $_POST['v_webmail'];
  260. }else{
  261. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  262. $v_webmail = 'roundcube';
  263. }
  264. render_page($user, $TAB, 'add_mail');
  265. } else {
  266. // Display body for mail account
  267. $v_domain = $_GET['domain'];
  268. render_page($user, $TAB, 'add_mail_acc');
  269. }
  270. // Flush session messages
  271. unset($_SESSION['error_msg']);
  272. unset($_SESSION['ok_msg']);