index.php 13 KB

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