settings.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. The match section performs AND operation on different matches: for example, if you have from and rcpt in the same rule,
  4. then the rule matches only when from AND rcpt match. For similar matches, the OR rule applies: if you have multiple rcpt matches,
  5. then any of these will trigger the rule. If a rule is triggered then no more rules are matched.
  6. */
  7. ini_set('error_reporting', '0');
  8. header('Content-Type: text/plain');
  9. require_once "vars.inc.php";
  10. $dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
  11. $opt = [
  12. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  13. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  14. PDO::ATTR_EMULATE_PREPARES => false,
  15. ];
  16. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  17. ?>
  18. settings {
  19. <?php
  20. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  21. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  22. while ($row = array_shift($rows)) {
  23. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  24. ?>
  25. score_<?=$username_sane;?> {
  26. priority = low;
  27. <?php
  28. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  29. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  30. AND `object`= :object");
  31. $stmt->execute(array(':object' => $row['object']));
  32. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  33. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  34. WHERE (`object`= :object OR `object`= :object_domain)
  35. AND (`option` = 'blacklist_from' OR `option` = 'whitelist_from')");
  36. $stmt->execute(array(':object' => $row['object'], ':object_domain' => substr(strrchr($row['object'], "@"), 1)));
  37. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  38. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  39. ?>
  40. from = "/^((?!<?=$value_sane;?>).)*$/";
  41. rcpt = "<?=$row['object'];?>";
  42. <?php
  43. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` = :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  44. $stmt->execute(array(':object_goto' => $row['object'], ':object_address' => $row['object']));
  45. $rows_aliases_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  46. while ($row_aliases_1 = array_shift($rows_aliases_1)) {
  47. ?>
  48. rcpt = "<?=$row_aliases_1['address'];?>";
  49. <?php
  50. }
  51. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  52. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  53. WHERE `mailbox`.`username` = :object");
  54. $stmt->execute(array(':object' => $row['object']));
  55. $rows_aliases_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  56. array_filter($rows_aliases_2);
  57. while ($row_aliases_2 = array_shift($rows_aliases_2)) {
  58. if (!empty($row_aliases_2['aliases'])) {
  59. ?>
  60. rcpt = "<?=$row_aliases_2['aliases'];?>";
  61. <?php
  62. }
  63. }
  64. ?>
  65. apply "default" {
  66. actions {
  67. reject = <?=$spamscore['highspamlevel'][0];?>;
  68. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  69. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  70. }
  71. }
  72. }
  73. <?php
  74. }
  75. /*
  76. // Start whitelist
  77. */
  78. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  79. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  80. while ($row = array_shift($rows)) {
  81. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  82. ?>
  83. whitelist_<?=$username_sane;?> {
  84. <?php
  85. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  86. WHERE `object`= :object
  87. AND `option` = 'whitelist_from'");
  88. $stmt->execute(array(':object' => $row['object']));
  89. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  90. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  91. ?>
  92. from = "/(<?=$value_sane;?>)/";
  93. <?php
  94. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  95. ?>
  96. priority = medium;
  97. rcpt = "/.*@<?=$row['object'];?>/";
  98. <?php
  99. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  100. WHERE `target_domain` = :object");
  101. $stmt->execute(array(':object' => $row['object']));
  102. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  103. array_filter($rows_domain_aliases);
  104. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  105. ?>
  106. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  107. <?php
  108. }
  109. }
  110. else {
  111. ?>
  112. priority = high;
  113. rcpt = "<?=$row['object'];?>";
  114. <?php
  115. }
  116. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` = :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  117. $stmt->execute(array(':object_goto' => $row['object'], ':object_address' => $row['object']));
  118. $rows_aliases_wl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  119. array_filter($rows_aliases_wl_1);
  120. while ($row_aliases_wl_1 = array_shift($rows_aliases_wl_1)) {
  121. ?>
  122. rcpt = "<?=$row_aliases_wl_1['address'];?>";
  123. <?php
  124. }
  125. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  126. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  127. WHERE `mailbox`.`username` = :object");
  128. $stmt->execute(array(':object' => $row['object']));
  129. $rows_aliases_wl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  130. array_filter($rows_aliases_wl_2);
  131. while ($row_aliases_wl_2 = array_shift($rows_aliases_wl_2)) {
  132. if (!empty($row_aliases_wl_2['aliases'])) {
  133. ?>
  134. rcpt = "<?=$row_aliases_wl_2['aliases'];?>";
  135. <?php
  136. }
  137. }
  138. ?>
  139. apply "default" {
  140. MAILCOW_MOO = -999.0;
  141. }
  142. }
  143. <?php
  144. }
  145. /*
  146. // Start blacklist
  147. */
  148. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  149. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  150. while ($row = array_shift($rows)) {
  151. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  152. ?>
  153. blacklist_<?=$username_sane;?> {
  154. <?php
  155. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  156. WHERE `object`= :object
  157. AND `option` = 'blacklist_from'");
  158. $stmt->execute(array(':object' => $row['object']));
  159. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  160. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  161. ?>
  162. from = "/(<?=$value_sane;?>)/";
  163. <?php
  164. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  165. ?>
  166. priority = medium;
  167. rcpt = "/.*@<?=$row['object'];?>/";
  168. <?php
  169. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  170. WHERE `target_domain` = :object");
  171. $stmt->execute(array(':object' => $row['object']));
  172. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  173. array_filter($rows_domain_aliases);
  174. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  175. ?>
  176. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  177. <?php
  178. }
  179. }
  180. else {
  181. ?>
  182. priority = high;
  183. rcpt = "<?=$row['object'];?>";
  184. <?php
  185. }
  186. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` = :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  187. $stmt->execute(array(':object_goto' => $row['object'], ':object_address' => $row['object']));
  188. $rows_aliases_bl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  189. array_filter($rows_aliases_bl_1);
  190. while ($row_aliases_bl_1 = array_shift($rows_aliases_bl_1)) {
  191. ?>
  192. rcpt = "<?=$row_aliases_bl_1['address'];?>";
  193. <?php
  194. }
  195. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  196. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  197. WHERE `mailbox`.`username` = :object");
  198. $stmt->execute(array(':object' => $row['object']));
  199. $rows_aliases_bl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  200. array_filter($rows_aliases_bl_2);
  201. while ($row_aliases_bl_2 = array_shift($rows_aliases_bl_2)) {
  202. if (!empty($row_aliases_bl_2['aliases'])) {
  203. ?>
  204. rcpt = "<?=$row_aliases_bl_2['aliases'];?>";
  205. <?php
  206. }
  207. }
  208. ?>
  209. apply "default" {
  210. MAILCOW_MOO = 999.0;
  211. }
  212. }
  213. <?php
  214. }
  215. ?>
  216. }