index.php 13 KB

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