settings.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. header('Content-Type: text/plain');
  8. require_once "vars.inc.php";
  9. ini_set('error_reporting', 0);
  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. try {
  18. $stmt = $pdo->query("SELECT * FROM `filterconf`");
  19. }
  20. catch (PDOException $e) {
  21. echo 'settings { }';
  22. exit;
  23. }
  24. ?>
  25. settings {
  26. <?php
  27. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  28. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  29. while ($row = array_shift($rows)) {
  30. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  31. ?>
  32. score_<?=$username_sane;?> {
  33. priority = low;
  34. <?php
  35. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  36. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  37. AND `object`= :object");
  38. $stmt->execute(array(':object' => $row['object']));
  39. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  40. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  41. WHERE (`object`= :object OR `object`= :object_domain)
  42. AND (`option` = 'blacklist_from' OR `option` = 'whitelist_from')");
  43. $stmt->execute(array(':object' => $row['object'], ':object_domain' => substr(strrchr($row['object'], "@"), 1)));
  44. $grouped_lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
  45. array_filter($grouped_lists);
  46. while ($grouped_list = array_shift($grouped_lists)) {
  47. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_list['value'])));
  48. if (!empty($value_sane)) {
  49. ?>
  50. from = "/^((?!<?=$value_sane;?>).)*$/";
  51. <?php
  52. }
  53. }
  54. ?>
  55. rcpt = "<?=$row['object'];?>";
  56. <?php
  57. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` = :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  58. $stmt->execute(array(':object_goto' => $row['object'], ':object_address' => $row['object']));
  59. $rows_aliases_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  60. while ($row_aliases_1 = array_shift($rows_aliases_1)) {
  61. ?>
  62. rcpt = "<?=$row_aliases_1['address'];?>";
  63. <?php
  64. }
  65. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  66. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  67. WHERE `mailbox`.`username` = :object");
  68. $stmt->execute(array(':object' => $row['object']));
  69. $rows_aliases_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  70. array_filter($rows_aliases_2);
  71. while ($row_aliases_2 = array_shift($rows_aliases_2)) {
  72. if (!empty($row_aliases_2['aliases'])) {
  73. ?>
  74. rcpt = "<?=$row_aliases_2['aliases'];?>";
  75. <?php
  76. }
  77. }
  78. ?>
  79. apply "default" {
  80. actions {
  81. reject = <?=$spamscore['highspamlevel'][0];?>;
  82. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  83. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  84. }
  85. }
  86. }
  87. <?php
  88. }
  89. /*
  90. // Start whitelist
  91. */
  92. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  93. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  94. while ($row = array_shift($rows)) {
  95. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  96. ?>
  97. whitelist_<?=$username_sane;?> {
  98. <?php
  99. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  100. WHERE `object`= :object
  101. AND `option` = 'whitelist_from'");
  102. $stmt->execute(array(':object' => $row['object']));
  103. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  104. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  105. ?>
  106. from = "/(<?=$value_sane;?>)/";
  107. <?php
  108. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  109. ?>
  110. priority = medium;
  111. rcpt = "/.*@<?=$row['object'];?>/";
  112. <?php
  113. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  114. WHERE `target_domain` = :object");
  115. $stmt->execute(array(':object' => $row['object']));
  116. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  117. array_filter($rows_domain_aliases);
  118. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  119. ?>
  120. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  121. <?php
  122. }
  123. }
  124. else {
  125. ?>
  126. priority = high;
  127. rcpt = "<?=$row['object'];?>";
  128. <?php
  129. }
  130. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` = :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  131. $stmt->execute(array(':object_goto' => $row['object'], ':object_address' => $row['object']));
  132. $rows_aliases_wl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  133. array_filter($rows_aliases_wl_1);
  134. while ($row_aliases_wl_1 = array_shift($rows_aliases_wl_1)) {
  135. ?>
  136. rcpt = "<?=$row_aliases_wl_1['address'];?>";
  137. <?php
  138. }
  139. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  140. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  141. WHERE `mailbox`.`username` = :object");
  142. $stmt->execute(array(':object' => $row['object']));
  143. $rows_aliases_wl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  144. array_filter($rows_aliases_wl_2);
  145. while ($row_aliases_wl_2 = array_shift($rows_aliases_wl_2)) {
  146. if (!empty($row_aliases_wl_2['aliases'])) {
  147. ?>
  148. rcpt = "<?=$row_aliases_wl_2['aliases'];?>";
  149. <?php
  150. }
  151. }
  152. ?>
  153. apply "default" {
  154. MAILCOW_MOO = -999.0;
  155. }
  156. }
  157. <?php
  158. }
  159. /*
  160. // Start blacklist
  161. */
  162. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  163. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  164. while ($row = array_shift($rows)) {
  165. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  166. ?>
  167. blacklist_<?=$username_sane;?> {
  168. <?php
  169. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(`value`, '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  170. WHERE `object`= :object
  171. AND `option` = 'blacklist_from'");
  172. $stmt->execute(array(':object' => $row['object']));
  173. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  174. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  175. ?>
  176. from = "/(<?=$value_sane;?>)/";
  177. <?php
  178. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  179. ?>
  180. priority = medium;
  181. rcpt = "/.*@<?=$row['object'];?>/";
  182. <?php
  183. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  184. WHERE `target_domain` = :object");
  185. $stmt->execute(array(':object' => $row['object']));
  186. $rows_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  187. array_filter($rows_domain_aliases);
  188. while ($row_domain_aliases = array_shift($rows_domain_aliases)) {
  189. ?>
  190. rcpt = "/.*@<?=$row_domain_aliases['alias_domain'];?>/";
  191. <?php
  192. }
  193. }
  194. else {
  195. ?>
  196. priority = high;
  197. rcpt = "<?=$row['object'];?>";
  198. <?php
  199. }
  200. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` = :object_goto AND `address` NOT LIKE '@%' AND `address` != :object_address");
  201. $stmt->execute(array(':object_goto' => $row['object'], ':object_address' => $row['object']));
  202. $rows_aliases_bl_1 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  203. array_filter($rows_aliases_bl_1);
  204. while ($row_aliases_bl_1 = array_shift($rows_aliases_bl_1)) {
  205. ?>
  206. rcpt = "<?=$row_aliases_bl_1['address'];?>";
  207. <?php
  208. }
  209. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `aliases` FROM `mailbox`
  210. LEFT OUTER JOIN `alias_domain` on `mailbox`.`domain` = `alias_domain`.`target_domain`
  211. WHERE `mailbox`.`username` = :object");
  212. $stmt->execute(array(':object' => $row['object']));
  213. $rows_aliases_bl_2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
  214. array_filter($rows_aliases_bl_2);
  215. while ($row_aliases_bl_2 = array_shift($rows_aliases_bl_2)) {
  216. if (!empty($row_aliases_bl_2['aliases'])) {
  217. ?>
  218. rcpt = "<?=$row_aliases_bl_2['aliases'];?>";
  219. <?php
  220. }
  221. }
  222. ?>
  223. apply "default" {
  224. MAILCOW_MOO = 999.0;
  225. }
  226. }
  227. <?php
  228. }
  229. ?>
  230. }