index.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. // Flush field values on success
  92. if (empty($_SESSION['error_msg'])) {
  93. $_SESSION['ok_msg'] = sprintf(_('MAIL_DOMAIN_CREATED_OK'),htmlentities($_POST['v_domain']),htmlentities($_POST['v_domain']));
  94. unset($v_domain, $v_webmail);
  95. }
  96. }
  97. // Check POST request for mail account
  98. if (!empty($_POST['ok_acc'])) {
  99. // Check token
  100. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  101. header('location: /login/');
  102. exit();
  103. }
  104. // Check antispam option
  105. if (!empty($_POST['v_blackhole'])) {
  106. $v_blackhole = 'yes';
  107. } else {
  108. $v_blackhole = 'no';
  109. }
  110. // Check empty fields
  111. if (empty($_POST['v_domain'])) $errors[] = _('domain');
  112. if (empty($_POST['v_account'])) $errors[] = _('account');
  113. if ((empty($_POST['v_fwd_only']) && empty($_POST['v_password']))) {
  114. if (empty($_POST['v_password'])) $errors[] = _('password');
  115. }
  116. if (!empty($errors[0])) {
  117. foreach ($errors as $i => $error) {
  118. if ( $i == 0 ) {
  119. $error_msg = $error;
  120. } else {
  121. $error_msg = $error_msg.", ".$error;
  122. }
  123. }
  124. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'),$error_msg);
  125. }
  126. // Validate email
  127. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  128. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  129. $_SESSION['error_msg'] = _('Please enter valid email address.');
  130. }
  131. }
  132. // Check password length
  133. if (empty($_SESSION['error_msg']) && (empty($_POST['v_fwd_only']))) {
  134. if (!validate_password($_POST['v_password'])) { $_SESSION['error_msg'] = _('Password does not match the minimum requirements');}
  135. }
  136. // Protect input
  137. $v_domain = escapeshellarg($_POST['v_domain']);
  138. $v_domain = strtolower($v_domain);
  139. $v_account = escapeshellarg($_POST['v_account']);
  140. $v_quota = escapeshellarg($_POST['v_quota']);
  141. $v_send_email = $_POST['v_send_email'];
  142. $v_credentials = $_POST['v_credentials'];
  143. $v_aliases = $_POST['v_aliases'];
  144. $v_fwd = $_POST['v_fwd'];
  145. if (empty($_POST['v_quota'])) $v_quota = 0;
  146. if ((!empty($_POST['v_quota'])) || (!empty($_POST['v_aliases'])) || (!empty($_POST['v_fwd'])) ) $v_adv = 'yes';
  147. // Add Mail Account
  148. if (empty($_SESSION['error_msg'])) {
  149. $v_password = tempnam("/tmp","vst");
  150. $fp = fopen($v_password, "w");
  151. fwrite($fp, $_POST['v_password']."\n");
  152. fclose($fp);
  153. exec (HESTIA_CMD."v-add-mail-account ".$user." ".$v_domain." ".$v_account." ".$v_password." ".$v_quota, $output, $return_var);
  154. check_return_code($return_var,$output);
  155. unset($output);
  156. unlink($v_password);
  157. $v_password = escapeshellarg($_POST['v_password']);
  158. }
  159. // Add Aliases
  160. if ((!empty($_POST['v_aliases'])) && (empty($_SESSION['error_msg']))) {
  161. $valiases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  162. $valiases = preg_replace("/,/", " ", $valiases);
  163. $valiases = preg_replace('/\s+/', ' ',$valiases);
  164. $valiases = trim($valiases);
  165. $aliases = explode(" ", $valiases);
  166. foreach ($aliases as $alias) {
  167. $alias = escapeshellarg($alias);
  168. if (empty($_SESSION['error_msg'])) {
  169. exec (HESTIA_CMD."v-add-mail-account-alias ".$user." ".$v_domain." ".$v_account." ".$alias, $output, $return_var);
  170. check_return_code($return_var,$output);
  171. unset($output);
  172. }
  173. }
  174. }
  175. if ((!empty($_POST['v_blackhole'])) && (empty($_SESSION['error_msg']))){
  176. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." :blackhole:", $output, $return_var);
  177. check_return_code($return_var,$output);
  178. unset($output);
  179. //disable any input in v_fwd
  180. $_POST['v_fwd'] = '';
  181. }
  182. // Add Forwarders
  183. if ((!empty($_POST['v_fwd'])) && (empty($_SESSION['error_msg']))) {
  184. $vfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  185. $vfwd = preg_replace("/,/", " ", $vfwd);
  186. $vfwd = preg_replace('/\s+/', ' ',$vfwd);
  187. $vfwd = trim($vfwd);
  188. $fwd = explode(" ", $vfwd);
  189. foreach ($fwd as $forward) {
  190. $forward = escapeshellarg($forward);
  191. if (empty($_SESSION['error_msg'])) {
  192. exec (HESTIA_CMD."v-add-mail-account-forward ".$user." ".$v_domain." ".$v_account." ".$forward, $output, $return_var);
  193. check_return_code($return_var,$output);
  194. unset($output);
  195. }
  196. }
  197. }
  198. // Add fwd_only flag
  199. if ((!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  200. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$user." ".$v_domain." ".$v_account, $output, $return_var);
  201. check_return_code($return_var,$output);
  202. unset($output);
  203. }
  204. // Get webmail url
  205. if (empty($_SESSION['error_msg'])) {
  206. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  207. $webmail = "http://".$hostname."/".$v_webmail_alias."/";
  208. if (!empty($_SESSION['WEBMAIL_ALIAS'])) $webmail = $_SESSION['WEBMAIL_ALIAS'];
  209. }
  210. // Email login credentials
  211. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  212. $to = $v_send_email;
  213. $subject = _("Email Credentials");
  214. $hostname = exec('hostname');
  215. $from = sprintf(_('MAIL_FROM'), $hostname);
  216. $mailtext = $v_credentials;
  217. send_email($to, $subject, $mailtext, $from);
  218. }
  219. // Flush field values on success
  220. if (empty($_SESSION['error_msg'])) {
  221. $_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]));
  222. unset($v_account);
  223. unset($v_password);
  224. unset($v_password);
  225. unset($v_aliases);
  226. unset($v_fwd);
  227. unset($v_quota);
  228. }
  229. }
  230. // Render page
  231. if (empty($_GET['domain'])) {
  232. // Display body for mail domain
  233. if( !empty($_POST['v_webmail']) ){
  234. $v_webmail = $_POST['v_webmail'];
  235. }else{
  236. //default is always roundcube unless it hasn't been installed. Then picks the first one in order
  237. $v_webmail = 'roundcube';
  238. }
  239. render_page($user, $TAB, 'add_mail');
  240. } else {
  241. // Display body for mail account
  242. $v_domain = $_GET['domain'];
  243. render_page($user, $TAB, 'add_mail_acc');
  244. }
  245. // Flush session messages
  246. unset($_SESSION['error_msg']);
  247. unset($_SESSION['ok_msg']);