index.php 9.3 KB

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