settings.php 6.8 KB

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