index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. error_reporting(NULL);
  3. ob_start();
  4. $TAB = 'MAIL';
  5. header('Content-Type: application/json');
  6. // Main include
  7. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  8. // Check domain argument
  9. if (empty($_GET['domain'])) {
  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. exit();
  82. }
  83. // Delete antispam
  84. if (($v_antispam == 'yes') && (empty($_POST['v_antispam'])) && (empty($_SESSION['error_msg']))) {
  85. exec (VESTA_CMD."v-delete-mail-domain-antispam ".$v_username." ".$v_domain, $output, $return_var);
  86. check_return_code($return_var,$output);
  87. $v_antispam = 'no';
  88. unset($output);
  89. }
  90. // Add antispam
  91. if (($v_antispam == 'no') && (!empty($_POST['v_antispam'])) && (empty($_SESSION['error_msg']))) {
  92. exec (VESTA_CMD."v-add-mail-domain-antispam ".$v_username." ".$v_domain, $output, $return_var);
  93. check_return_code($return_var,$output);
  94. $v_antispam = 'yes';
  95. unset($output);
  96. }
  97. // Delete antivirus
  98. if (($v_antivirus == 'yes') && (empty($_POST['v_antivirus'])) && (empty($_SESSION['error_msg']))) {
  99. exec (VESTA_CMD."v-delete-mail-domain-antivirus ".$v_username." ".$v_domain, $output, $return_var);
  100. check_return_code($return_var,$output);
  101. $v_antivirus = 'no';
  102. unset($output);
  103. }
  104. // Add antivirs
  105. if (($v_antivirus == 'no') && (!empty($_POST['v_antivirus'])) && (empty($_SESSION['error_msg']))) {
  106. exec (VESTA_CMD."v-add-mail-domain-antivirus ".$v_username." ".$v_domain, $output, $return_var);
  107. check_return_code($return_var,$output);
  108. $v_antivirus = 'yes';
  109. unset($output);
  110. }
  111. // Delete DKIM
  112. if (($v_dkim == 'yes') && (empty($_POST['v_dkim'])) && (empty($_SESSION['error_msg']))) {
  113. exec (VESTA_CMD."v-delete-mail-domain-dkim ".$v_username." ".$v_domain, $output, $return_var);
  114. check_return_code($return_var,$output);
  115. $v_dkim = 'no';
  116. unset($output);
  117. }
  118. // Add DKIM
  119. if (($v_dkim == 'no') && (!empty($_POST['v_dkim'])) && (empty($_SESSION['error_msg']))) {
  120. exec (VESTA_CMD."v-add-mail-domain-dkim ".$v_username." ".$v_domain, $output, $return_var);
  121. check_return_code($return_var,$output);
  122. $v_dkim = 'yes';
  123. unset($output);
  124. }
  125. // Delete catchall
  126. if ((!empty($v_catchall)) && (empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  127. exec (VESTA_CMD."v-delete-mail-domain-catchall ".$v_username." ".$v_domain, $output, $return_var);
  128. check_return_code($return_var,$output);
  129. $v_catchall = '';
  130. unset($output);
  131. }
  132. // Change catchall address
  133. if ((!empty($v_catchall)) && (!empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  134. if ($v_catchall != $_POST['v_catchall']) {
  135. $v_catchall = escapeshellarg($_POST['v_catchall']);
  136. exec (VESTA_CMD."v-change-mail-domain-catchall ".$v_username." ".$v_domain." ".$v_catchall, $output, $return_var);
  137. check_return_code($return_var,$output);
  138. unset($output);
  139. }
  140. }
  141. // Add catchall
  142. if ((empty($v_catchall)) && (!empty($_POST['v_catchall'])) && (empty($_SESSION['error_msg']))) {
  143. $v_catchall = escapeshellarg($_POST['v_catchall']);
  144. exec (VESTA_CMD."v-add-mail-domain-catchall ".$v_username." ".$v_domain." ".$v_catchall, $output, $return_var);
  145. check_return_code($return_var,$output);
  146. unset($output);
  147. }
  148. // Set success message
  149. if (empty($_SESSION['error_msg'])) {
  150. $_SESSION['ok_msg'] = __('Changes has been saved.');
  151. }
  152. }
  153. // Check POST request for mail account
  154. if ((!empty($_POST['save'])) && (!empty($_GET['domain'])) && (!empty($_GET['account']))) {
  155. // Check token
  156. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  157. header('location: /login/');
  158. exit();
  159. }
  160. // Validate email
  161. if ((!empty($_POST['v_send_email'])) && (empty($_SESSION['error_msg']))) {
  162. if (!filter_var($_POST['v_send_email'], FILTER_VALIDATE_EMAIL)) {
  163. $_SESSION['error_msg'] = __('Please enter valid email address.');
  164. }
  165. }
  166. $v_domain = escapeshellarg($_POST['v_domain']);
  167. $v_account = escapeshellarg($_POST['v_account']);
  168. $v_send_email = $_POST['v_send_email'];
  169. $v_credentials = $_POST['v_credentials'];
  170. // Change password
  171. if ((!empty($_POST['v_password'])) && (empty($_SESSION['error_msg']))) {
  172. $v_password = tempnam("/tmp","vst");
  173. $fp = fopen($v_password, "w");
  174. fwrite($fp, $_POST['v_password']."\n");
  175. fclose($fp);
  176. exec (VESTA_CMD."v-change-mail-account-password ".$v_username." ".$v_domain." ".$v_account." ".$v_password, $output, $return_var);
  177. check_return_code($return_var,$output);
  178. unset($output);
  179. unlink($v_password);
  180. $v_password = escapeshellarg($_POST['v_password']);;
  181. }
  182. // Change quota
  183. if (($v_quota != $_POST['v_quota']) && (empty($_SESSION['error_msg']))) {
  184. if (empty($_POST['v_quota'])) {
  185. $v_quota = 0;
  186. } else {
  187. $v_quota = escapeshellarg($_POST['v_quota']);
  188. }
  189. exec (VESTA_CMD."v-change-mail-account-quota ".$v_username." ".$v_domain." ".$v_account." ".$v_quota, $output, $return_var);
  190. check_return_code($return_var,$output);
  191. unset($output);
  192. }
  193. // Change account aliases
  194. if (empty($_SESSION['error_msg'])) {
  195. $waliases = preg_replace("/\n/", " ", $_POST['v_aliases']);
  196. $waliases = preg_replace("/,/", " ", $waliases);
  197. $waliases = preg_replace('/\s+/', ' ',$waliases);
  198. $waliases = trim($waliases);
  199. $aliases = explode(" ", $waliases);
  200. $v_aliases = str_replace(' ', "\n", $waliases);
  201. $result = array_diff($valiases, $aliases);
  202. foreach ($result as $alias) {
  203. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  204. exec (VESTA_CMD."v-delete-mail-account-alias ".$v_username." ".$v_domain." ".$v_account." ".escapeshellarg($alias), $output, $return_var);
  205. check_return_code($return_var,$output);
  206. unset($output);
  207. }
  208. }
  209. $result = array_diff($aliases, $valiases);
  210. foreach ($result as $alias) {
  211. if ((empty($_SESSION['error_msg'])) && (!empty($alias))) {
  212. exec (VESTA_CMD."v-add-mail-account-alias ".$v_username." ".$v_domain." ".$v_account." ".escapeshellarg($alias), $output, $return_var);
  213. check_return_code($return_var,$output);
  214. unset($output);
  215. }
  216. }
  217. }
  218. // Change forwarders
  219. if (empty($_SESSION['error_msg'])) {
  220. $wfwd = preg_replace("/\n/", " ", $_POST['v_fwd']);
  221. $wfwd = preg_replace("/,/", " ", $wfwd);
  222. $wfwd = preg_replace('/\s+/', ' ',$wfwd);
  223. $wfwd = trim($wfwd);
  224. $fwd = explode(" ", $wfwd);
  225. $v_fwd = str_replace(' ', "\n", $wfwd);
  226. $result = array_diff($vfwd, $fwd);
  227. foreach ($result as $forward) {
  228. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  229. exec (VESTA_CMD."v-delete-mail-account-forward ".$v_username." ".$v_domain." ".$v_account." ".escapeshellarg($forward), $output, $return_var);
  230. check_return_code($return_var,$output);
  231. unset($output);
  232. }
  233. }
  234. $result = array_diff($fwd, $vfwd);
  235. foreach ($result as $forward) {
  236. if ((empty($_SESSION['error_msg'])) && (!empty($forward))) {
  237. exec (VESTA_CMD."v-add-mail-account-forward ".$v_username." ".$v_domain." ".$v_account." ".escapeshellarg($forward), $output, $return_var);
  238. check_return_code($return_var,$output);
  239. unset($output);
  240. }
  241. }
  242. }
  243. // Delete FWD_ONLY flag
  244. if (($v_fwd_only == 'yes') && (empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  245. exec (VESTA_CMD."v-delete-mail-account-fwd-only ".$v_username." ".$v_domain." ".$v_account, $output, $return_var);
  246. check_return_code($return_var,$output);
  247. unset($output);
  248. $v_fwd_only = '';
  249. }
  250. // Add FWD_ONLY flag
  251. if (($v_fwd_only != 'yes') && (!empty($_POST['v_fwd_only'])) && (empty($_SESSION['error_msg']))) {
  252. exec (VESTA_CMD."v-add-mail-account-fwd-only ".$v_username." ".$v_domain." ".$v_account, $output, $return_var);
  253. check_return_code($return_var,$output);
  254. unset($output);
  255. $v_fwd_only = 'yes';
  256. }
  257. // Delete autoreply
  258. if (($v_autoreply == 'yes') && (empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  259. exec (VESTA_CMD."v-delete-mail-account-autoreply ".$v_username." ".$v_domain." ".$v_account, $output, $return_var);
  260. check_return_code($return_var,$output);
  261. unset($output);
  262. $v_autoreply = 'no';
  263. $v_autoreply_message = '';
  264. }
  265. // Add autoreply
  266. if ((!empty($_POST['v_autoreply'])) && (empty($_SESSION['error_msg']))) {
  267. if ( $v_autoreply_message != str_replace("\r\n", "\n", $_POST['v_autoreply_message'])) {
  268. $v_autoreply_message = str_replace("\r\n", "\n", $_POST['v_autoreply_message']);
  269. $v_autoreply_message = escapeshellarg($v_autoreply_message);
  270. exec (VESTA_CMD."v-add-mail-account-autoreply ".$v_username." ".$v_domain." ".$v_account." ".$v_autoreply_message, $output, $return_var);
  271. check_return_code($return_var,$output);
  272. unset($output);
  273. $v_autoreply = 'yes';
  274. $v_autoreply_message = $_POST['v_autoreply_message'];
  275. }
  276. }
  277. // Email login credentials
  278. if ((!empty($v_send_email)) && (empty($_SESSION['error_msg']))) {
  279. $to = $v_send_email;
  280. $subject = __("Email Credentials");
  281. $hostname = exec('hostname');
  282. $from = __('MAIL_FROM', $hostname);
  283. $mailtext = $v_credentials;
  284. send_email($to, $subject, $mailtext, $from);
  285. }
  286. // Set success message
  287. if (empty($_SESSION['error_msg'])) {
  288. $_SESSION['ok_msg'] = __('Changes has been saved.');
  289. }
  290. }
  291. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"].":");
  292. $webmail = "/webmail/";
  293. if (!empty($_SESSION['MAIL_URL'])) $webmail = $_SESSION['MAIL_URL'];
  294. if (empty($hostname)) {
  295. $uname_arr=posix_uname();
  296. $hostname=$uname_arr['nodename'];
  297. }
  298. $result = array(
  299. 'antispam' => $data[$v_domain]['ANTISPAM'],
  300. 'antivirus' => $data[$v_domain]['ANTIVIRUS'],
  301. 'dkim' => $data[$v_domain]['DKIM'],
  302. 'catchall' => $data[$v_domain]['CATCHALL'],
  303. 'status' => $v_status,
  304. 'username' => $user,
  305. 'domain' => $v_domain,
  306. 'account' => $v_account,
  307. 'password' => $v_password,
  308. 'aliases' => $v_aliases,
  309. 'valiases' => $valiases,
  310. 'fwd' => $v_fwd,
  311. 'vfwd' => $vfwd,
  312. 'fwd_only' => $v_fwd_only,
  313. 'quota' => $v_quota,
  314. 'autoreply' => $v_autoreply,
  315. 'quota' => $v_quota,
  316. 'date' => $v_date,
  317. 'time' => $v_time,
  318. 'send_email' => $v_send_email,
  319. 'credentials' => $v_credentials,
  320. 'webmail' => $webmail,
  321. 'hostname' => $hostname,
  322. 'autoreply_message' => $v_autoreply_message,
  323. 'v_suspended' => $v_suspended,
  324. 'error_msg' => $_SESSION['error_msg'],
  325. 'ok_msg' => $_SESSION['ok_msg']
  326. );
  327. echo json_encode($result);
  328. // Flush session messages
  329. unset($_SESSION['error_msg']);
  330. unset($_SESSION['ok_msg']);