functions.fwdhost.inc.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. function fwdhost($_action, $_data = null) {
  3. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/spf.inc.php';
  4. global $redis;
  5. global $lang;
  6. switch ($_action) {
  7. case 'add':
  8. global $lang;
  9. if ($_SESSION['mailcow_cc_role'] != "admin") {
  10. $_SESSION['return'] = array(
  11. 'type' => 'danger',
  12. 'msg' => sprintf($lang['danger']['access_denied'])
  13. );
  14. return false;
  15. }
  16. $source = $_data['hostname'];
  17. $host = trim($_data['hostname']);
  18. $filter_spam = (isset($_data['filter_spam']) && $_data['filter_spam'] == 1) ? 1 : 0;
  19. if (preg_match('/^[0-9a-fA-F:\/]+$/', $host)) { // IPv6 address
  20. $hosts = array($host);
  21. }
  22. elseif (preg_match('/^[0-9\.\/]+$/', $host)) { // IPv4 address
  23. $hosts = array($host);
  24. }
  25. else {
  26. $hosts = get_outgoing_hosts_best_guess($host);
  27. }
  28. if (empty($hosts)) {
  29. $_SESSION['return'] = array(
  30. 'type' => 'danger',
  31. 'msg' => 'Invalid host specified: '. htmlspecialchars($host)
  32. );
  33. return false;
  34. }
  35. foreach ($hosts as $host) {
  36. try {
  37. $redis->hSet('WHITELISTED_FWD_HOST', $host, $source);
  38. if ($filter_spam == 0) {
  39. $redis->hSet('KEEP_SPAM', $host, 1);
  40. }
  41. elseif ($redis->hGet('KEEP_SPAM', $host)) {
  42. $redis->hDel('KEEP_SPAM', $host);
  43. }
  44. }
  45. catch (RedisException $e) {
  46. $_SESSION['return'] = array(
  47. 'type' => 'danger',
  48. 'msg' => 'Redis: '.$e
  49. );
  50. return false;
  51. }
  52. }
  53. $_SESSION['return'] = array(
  54. 'type' => 'success',
  55. 'msg' => sprintf($lang['success']['forwarding_host_added'], htmlspecialchars(implode(', ', $hosts)))
  56. );
  57. break;
  58. case 'edit':
  59. global $lang;
  60. if ($_SESSION['mailcow_cc_role'] != "admin") {
  61. $_SESSION['return'] = array(
  62. 'type' => 'danger',
  63. 'msg' => sprintf($lang['danger']['access_denied'])
  64. );
  65. return false;
  66. }
  67. $fwdhosts = (array)$_data['fwdhost'];
  68. foreach ($fwdhosts as $fwdhost) {
  69. $is_now = fwdhost('details', $fwdhost);
  70. if (!empty($is_now)) {
  71. $keep_spam = (isset($_data['keep_spam'])) ? $_data['keep_spam'] : $is_now['keep_spam'];
  72. }
  73. else {
  74. $_SESSION['return'] = array(
  75. 'type' => 'danger',
  76. 'msg' => sprintf($lang['danger']['access_denied'])
  77. );
  78. return false;
  79. }
  80. try {
  81. if ($keep_spam == 1) {
  82. $redis->hSet('KEEP_SPAM', $fwdhost, 1);
  83. }
  84. else {
  85. $redis->hDel('KEEP_SPAM', $fwdhost);
  86. }
  87. }
  88. catch (RedisException $e) {
  89. $_SESSION['return'] = array(
  90. 'type' => 'danger',
  91. 'msg' => 'Redis: '.$e
  92. );
  93. return false;
  94. }
  95. }
  96. $_SESSION['return'] = array(
  97. 'type' => 'success',
  98. 'msg' => sprintf($lang['success']['object_modified'], htmlspecialchars(implode(', ', $fwdhosts)))
  99. );
  100. break;
  101. case 'delete':
  102. $hosts = (array)$_data['forwardinghost'];
  103. foreach ($hosts as $host) {
  104. try {
  105. $redis->hDel('WHITELISTED_FWD_HOST', $host);
  106. $redis->hDel('KEEP_SPAM', $host);
  107. }
  108. catch (RedisException $e) {
  109. $_SESSION['return'] = array(
  110. 'type' => 'danger',
  111. 'msg' => 'Redis: '.$e
  112. );
  113. return false;
  114. }
  115. }
  116. $_SESSION['return'] = array(
  117. 'type' => 'success',
  118. 'msg' => sprintf($lang['success']['forwarding_host_removed'], htmlspecialchars(implode(', ', $hosts)))
  119. );
  120. break;
  121. case 'get':
  122. if ($_SESSION['mailcow_cc_role'] != "admin") {
  123. return false;
  124. }
  125. $fwdhostsdata = array();
  126. try {
  127. $fwd_hosts = $redis->hGetAll('WHITELISTED_FWD_HOST');
  128. if (!empty($fwd_hosts)) {
  129. foreach ($fwd_hosts as $fwd_host => $source) {
  130. $keep_spam = ($redis->hGet('KEEP_SPAM', $fwd_host)) ? "yes" : "no";
  131. $fwdhostsdata[] = array(
  132. 'host' => $fwd_host,
  133. 'source' => $source,
  134. 'keep_spam' => $keep_spam
  135. );
  136. }
  137. }
  138. }
  139. catch (RedisException $e) {
  140. $_SESSION['return'] = array(
  141. 'type' => 'danger',
  142. 'msg' => 'Redis: '.$e
  143. );
  144. return false;
  145. }
  146. return $fwdhostsdata;
  147. break;
  148. case 'details':
  149. $fwdhostdetails = array();
  150. if (!isset($_data) || empty($_data)) {
  151. return false;
  152. }
  153. try {
  154. if ($source = $redis->hGet('WHITELISTED_FWD_HOST', $_data)) {
  155. $fwdhostdetails['host'] = $_data;
  156. $fwdhostdetails['source'] = $source;
  157. $fwdhostdetails['keep_spam'] = ($redis->hGet('KEEP_SPAM', $_data)) ? "yes" : "no";
  158. }
  159. }
  160. catch (RedisException $e) {
  161. $_SESSION['return'] = array(
  162. 'type' => 'danger',
  163. 'msg' => 'Redis: '.$e
  164. );
  165. return false;
  166. }
  167. return $fwdhostdetails;
  168. break;
  169. }
  170. }