index.php 13 KB

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