index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. error_reporting(NULL);
  3. ob_start();
  4. $TAB = 'MAIL';
  5. // Main include
  6. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  7. // Check domain argument
  8. if (empty($_GET['domain'])) {
  9. header("Location: /list/mail/");
  10. exit;
  11. }
  12. // Edit as someone else?
  13. if (($_SESSION['user'] == 'admin') && (!empty($_GET['user']))) {
  14. $user=escapeshellarg($_GET['user']);
  15. }
  16. $v_username = $user;
  17. // Get all user domains
  18. exec (HESTIA_CMD."v-list-mail-domains ".escapeshellarg($user)." json", $output, $return_var);
  19. $user_domains = json_decode(implode('', $output), true);
  20. $user_domains = array_keys($user_domains);
  21. unset($output);
  22. // List mail domain
  23. if ((!empty($_GET['domain'])) && (empty($_GET['account']))) {
  24. $v_domain = $_GET['domain'];
  25. if(!in_array($v_domain, $user_domains)) {
  26. header("Location: /list/mail/");
  27. exit;
  28. }
  29. exec (HESTIA_CMD."v-list-mail-domain ".$user." ".escapeshellarg($v_domain)." json", $output, $return_var);
  30. $data = json_decode(implode('', $output), true);
  31. unset($output);
  32. // Parse domain
  33. $v_antispam = $data[$v_domain]['ANTISPAM'];
  34. $v_antivirus = $data[$v_domain]['ANTIVIRUS'];
  35. $v_dkim = $data[$v_domain]['DKIM'];
  36. $v_catchall = $data[$v_domain]['CATCHALL'];
  37. $v_date = $data[$v_domain]['DATE'];
  38. $v_time = $data[$v_domain]['TIME'];
  39. $v_suspended = $data[$v_domain]['SUSPENDED'];
  40. if ( $v_suspended == 'yes' ) {
  41. $v_status = 'suspended';
  42. } else {
  43. $v_status = 'active';
  44. }
  45. }
  46. // List mail account
  47. if ((!empty($_GET['domain'])) && (!empty($_GET['account']))) {
  48. $v_domain = $_GET['domain'];
  49. if(!in_array($v_domain, $user_domains)) {
  50. header("Location: /list/mail/");
  51. exit;
  52. }
  53. $v_account = $_GET['account'];
  54. exec (HESTIA_CMD."v-list-mail-account ".$user." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." 'json'", $output, $return_var);
  55. $data = json_decode(implode('', $output), true);
  56. unset($output);
  57. // Parse mail account
  58. $v_username = $user;
  59. $v_password = "";
  60. $v_aliases = str_replace(',', "\n", $data[$v_account]['ALIAS']);
  61. $valiases = explode(",", $data[$v_account]['ALIAS']);
  62. $v_fwd = str_replace(',', "\n", $data[$v_account]['FWD']);
  63. $vfwd = explode(",", $data[$v_account]['FWD']);
  64. $v_fwd_only = $data[$v_account]['FWD_ONLY'];
  65. $v_quota = $data[$v_account]['QUOTA'];
  66. $v_autoreply = $data[$v_account]['AUTOREPLY'];
  67. $v_suspended = $data[$v_account]['SUSPENDED'];
  68. if ( $v_suspended == 'yes' ) {
  69. $v_status = 'suspended';
  70. } else {
  71. $v_status = 'active';
  72. }
  73. $v_date = $data[$v_account]['DATE'];
  74. $v_time = $data[$v_account]['TIME'];
  75. // Parse autoreply
  76. if ( $v_autoreply == 'yes' ) {
  77. exec (HESTIA_CMD."v-list-mail-account-autoreply ".$user." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." json", $output, $return_var);
  78. $autoreply_str = json_decode(implode('', $output), true);
  79. unset($output);
  80. $v_autoreply_message = $autoreply_str[$v_account]['MSG'];
  81. $v_autoreply_message=str_replace("\\n", "\n", $v_autoreply_message);
  82. }
  83. }
  84. // Check POST request for mail domain
  85. if ((!empty($_POST['save'])) && (!empty($_GET['domain'])) && (empty($_GET['account']))) {
  86. $v_domain = $_POST['v_domain'];
  87. if(!in_array($v_domain, $user_domains)) {
  88. check_return_code(3, ["Unknown domain"]);
  89. }
  90. // Check token
  91. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  92. header('location: /login/');
  93. exit();
  94. }
  95. // Delete antispam
  96. if (($v_antispam == 'yes') && (empty($_POST['v_antispam'])) && (empty($_SESSION['error_msg']))) {
  97. exec (HESTIA_CMD."v-delete-mail-domain-antispam ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  98. check_return_code($return_var,$output);
  99. $v_antispam = 'no';
  100. unset($output);
  101. }
  102. // Add antispam
  103. if (($v_antispam == 'no') && (!empty($_POST['v_antispam'])) && (empty($_SESSION['error_msg']))) {
  104. exec (HESTIA_CMD."v-add-mail-domain-antispam ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  105. check_return_code($return_var,$output);
  106. $v_antispam = 'yes';
  107. unset($output);
  108. }
  109. // Delete antivirus
  110. if (($v_antivirus == 'yes') && (empty($_POST['v_antivirus'])) && (empty($_SESSION['error_msg']))) {
  111. exec (HESTIA_CMD."v-delete-mail-domain-antivirus ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  112. check_return_code($return_var,$output);
  113. $v_antivirus = 'no';
  114. unset($output);
  115. }
  116. // Add antivirs
  117. if (($v_antivirus == 'no') && (!empty($_POST['v_antivirus'])) && (empty($_SESSION['error_msg']))) {
  118. exec (HESTIA_CMD."v-add-mail-domain-antivirus ".$v_username." ".$escapeshellarg($v_domain), $output, $return_var);
  119. check_return_code($return_var,$output);
  120. $v_antivirus = 'yes';
  121. unset($output);
  122. }
  123. // Delete DKIM
  124. if (($v_dkim == 'yes') && (empty($_POST['v_dkim'])) && (empty($_SESSION['error_msg']))) {
  125. exec (HESTIA_CMD."v-delete-mail-domain-dkim ".$v_username." ".$escapeshellarg($v_domain), $output, $return_var);
  126. check_return_code($return_var,$output);
  127. $v_dkim = 'no';
  128. unset($output);
  129. }
  130. // Add DKIM
  131. if (($v_dkim == 'no') && (!empty($_POST['v_dkim'])) && (empty($_SESSION['error_msg']))) {
  132. exec (HESTIA_CMD."v-add-mail-domain-dkim ".$v_username." ".$escapeshellarg($v_domain), $output, $return_var);
  133. check_return_code($return_var,$output);
  134. $v_dkim = 'yes';
  135. unset($output);
  136. }
  137. // Delete catchall
  138. if ((!empty($v_catchall)) && (empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  139. exec (HESTIA_CMD."v-delete-mail-domain-catchall ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
  140. check_return_code($return_var,$output);
  141. $v_catchall = '';
  142. unset($output);
  143. }
  144. // Change catchall address
  145. if ((!empty($v_catchall)) && (!empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  146. if ($v_catchall != $_POST['v_catchall']) {
  147. $v_catchall = escapeshellarg($_POST['v_catchall']);
  148. exec (HESTIA_CMD."v-change-mail-domain-catchall ".$v_username." ".escapeshellarg($v_domain)." ".$v_catchall, $output, $return_var);
  149. check_return_code($return_var,$output);
  150. unset($output);
  151. }
  152. }
  153. // Add catchall
  154. if ((empty($v_catchall)) && (!empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  155. $v_catchall = escapeshellarg($_POST['v_catchall']);
  156. exec (HESTIA_CMD."v-add-mail-domain-catchall ".$v_username." ".escapeshellarg($v_domain)." ".$v_catchall, $output, $return_var);
  157. check_return_code($return_var,$output);
  158. unset($output);
  159. }
  160. // Set success message
  161. if (empty($_SESSION['error_msg'])) {
  162. $_SESSION['ok_msg'] = __('Changes has been saved.');
  163. }
  164. }
  165. // Check POST request for mail account
  166. if ((!empty($_POST['save'])) && (!empty($_GET['domain'])) && (!empty($_GET['account']))) {
  167. // Check token
  168. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  169. header('location: /login/');
  170. exit();
  171. }
  172. // Validate email
  173. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  174. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  175. $_SESSION['error_msg'] = __('Please enter valid email address.');
  176. }
  177. }
  178. $v_domain = $_POST['v_domain'];
  179. if(!in_array($v_domain, $user_domains)) {
  180. check_return_code(3, ["Unknown domain"]);
  181. }
  182. $v_account = $_POST['v_account'];
  183. $v_send_email = $_POST['v_send_email'];
  184. $v_credentials = $_POST['v_credentials'];
  185. // Change password
  186. if ((!empty($_POST['v_password'])) && (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-change-mail-account-password ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_password, $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. // Change quota
  198. if (($v_quota != $_POST['v_quota']) && (empty($_SESSION['error_msg']))) {
  199. if (empty($_POST['v_quota'])) {
  200. $v_quota = 0;
  201. } else {
  202. $v_quota = escapeshellarg($_POST['v_quota']);
  203. }
  204. exec (HESTIA_CMD."v-change-mail-account-quota ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_quota, $output, $return_var);
  205. check_return_code($return_var,$output);
  206. unset($output);
  207. }
  208. // Change account aliases
  209. if (empty($_SESSION['error_msg'])) {
  210. $waliases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  211. $waliases = preg_replace("/,/", " ", $waliases);
  212. $waliases = preg_replace('/\s+/', ' ',$waliases);
  213. $waliases = trim($waliases);
  214. $aliases = explode(" ", $waliases);
  215. $v_aliases = str_replace(' ', "\n", $waliases);
  216. $result = array_diff($valiases, $aliases);
  217. foreach ($result as $alias) {
  218. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  219. exec (HESTIA_CMD."v-delete-mail-account-alias ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($alias), $output, $return_var);
  220. check_return_code($return_var,$output);
  221. unset($output);
  222. }
  223. }
  224. $result = array_diff($aliases, $valiases);
  225. foreach ($result as $alias) {
  226. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  227. exec (HESTIA_CMD."v-add-mail-account-alias ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($alias), $output, $return_var);
  228. check_return_code($return_var,$output);
  229. unset($output);
  230. }
  231. }
  232. }
  233. // Change forwarders
  234. if (empty($_SESSION['error_msg'])) {
  235. $wfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  236. $wfwd = preg_replace("/,/", " ", $wfwd);
  237. $wfwd = preg_replace('/\s+/', ' ',$wfwd);
  238. $wfwd = trim($wfwd);
  239. $fwd = explode(" ", $wfwd);
  240. $v_fwd = str_replace(' ', "\n", $wfwd);
  241. $result = array_diff($vfwd, $fwd);
  242. foreach ($result as $forward) {
  243. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  244. exec (HESTIA_CMD."v-delete-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  245. check_return_code($return_var,$output);
  246. unset($output);
  247. }
  248. }
  249. $result = array_diff($fwd, $vfwd);
  250. foreach ($result as $forward) {
  251. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  252. exec (HESTIA_CMD."v-add-mail-account-forward ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".escapeshellarg($forward), $output, $return_var);
  253. check_return_code($return_var,$output);
  254. unset($output);
  255. }
  256. }
  257. }
  258. // Delete FWD_ONLY flag
  259. if (($v_fwd_only == 'yes') && (empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  260. exec (HESTIA_CMD."v-delete-mail-account-fwd-only ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  261. check_return_code($return_var,$output);
  262. unset($output);
  263. $v_fwd_only = '';
  264. }
  265. // Add FWD_ONLY flag
  266. if (($v_fwd_only != 'yes') && (!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  267. exec (HESTIA_CMD."v-add-mail-account-fwd-only ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  268. check_return_code($return_var,$output);
  269. unset($output);
  270. $v_fwd_only = 'yes';
  271. }
  272. // Delete autoreply
  273. if (($v_autoreply == 'yes') && (empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  274. exec (HESTIA_CMD."v-delete-mail-account-autoreply ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account), $output, $return_var);
  275. check_return_code($return_var,$output);
  276. unset($output);
  277. $v_autoreply = 'no';
  278. $v_autoreply_message = '';
  279. }
  280. // Add autoreply
  281. if ((!empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  282. if ( $v_autoreply_message != str_replace("\r\n", "\n", $_POST['v_autoreply_message'])) {
  283. $v_autoreply_message = str_replace("\r\n", "\n", $_POST['v_autoreply_message']);
  284. $v_autoreply_message = escapeshellarg($v_autoreply_message);
  285. exec (HESTIA_CMD."v-add-mail-account-autoreply ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($v_account)." ".$v_autoreply_message, $output, $return_var);
  286. check_return_code($return_var,$output);
  287. unset($output);
  288. $v_autoreply = 'yes';
  289. $v_autoreply_message = $_POST['v_autoreply_message'];
  290. }
  291. }
  292. // Email login credentials
  293. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  294. $to = $v_send_email;
  295. $subject = __("Email Credentials");
  296. $hostname = exec('hostname');
  297. $from = __('MAIL_FROM', $hostname);
  298. $mailtext = $v_credentials;
  299. send_email($to, $subject, $mailtext, $from);
  300. }
  301. // Set success message
  302. if (empty($_SESSION['error_msg'])) {
  303. $_SESSION['ok_msg'] = __('Changes has been saved.');
  304. }
  305. }
  306. // Render page
  307. if (empty($_GET['account'])) {
  308. // Display body for mail domain
  309. render_page($user, $TAB, 'edit_mail');
  310. } else {
  311. // Display body for mail account
  312. render_page($user, $TAB, 'edit_mail_acc');
  313. }
  314. // Flush session messages
  315. unset($_SESSION['error_msg']);
  316. unset($_SESSION['ok_msg']);