settings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  12. $opt = [
  13. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  14. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  15. PDO::ATTR_EMULATE_PREPARES => false,
  16. ];
  17. try {
  18. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  19. $stmt = $pdo->query("SELECT '1' FROM `filterconf`");
  20. }
  21. catch (PDOException $e) {
  22. echo 'settings { }';
  23. exit;
  24. }
  25. function parse_email($email) {
  26. if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
  27. $a = strrpos($email, '@');
  28. return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
  29. }
  30. function ucl_rcpts($object, $type) {
  31. global $pdo;
  32. if ($type == 'mailbox') {
  33. // Standard aliases
  34. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  35. WHERE `goto` = :object_goto
  36. AND `address` NOT LIKE '@%'");
  37. $stmt->execute(array(
  38. ':object_goto' => $object
  39. ));
  40. $standard_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  41. while ($row = array_shift($standard_aliases)) {
  42. $local = parse_email($row['address'])['local'];
  43. $domain = parse_email($row['address'])['domain'];
  44. if (!empty($local) && !empty($domain)) {
  45. $rcpt[] = '/^' . str_replace('/', '\/', $local) . '[+].*' . str_replace('/', '\/', $domain) . '$/i';
  46. }
  47. $rcpt[] = '/^' . str_replace('/', '\/', $row['address']) . '$/i';
  48. }
  49. // Aliases by alias domains
  50. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias` FROM `mailbox`
  51. LEFT OUTER JOIN `alias_domain` ON `mailbox`.`domain` = `alias_domain`.`target_domain`
  52. WHERE `mailbox`.`username` = :object");
  53. $stmt->execute(array(
  54. ':object' => $object
  55. ));
  56. $by_domain_aliases = $stmt->fetchAll(PDO::FETCH_ASSOC);
  57. array_filter($by_domain_aliases);
  58. while ($row = array_shift($by_domain_aliases)) {
  59. if (!empty($row['alias'])) {
  60. $local = parse_email($row['alias'])['local'];
  61. $domain = parse_email($row['alias'])['domain'];
  62. if (!empty($local) && !empty($domain)) {
  63. $rcpt[] = '/^' . str_replace('/', '\/', $local) . '[+].*' . str_replace('/', '\/', $domain) . '$/i';
  64. }
  65. $rcpt[] = '/^' . str_replace('/', '\/', $row['alias']) . '$/i';
  66. }
  67. }
  68. }
  69. elseif ($type == 'domain') {
  70. // Domain self
  71. $rcpt[] = '/.*@' . $object . '/i';
  72. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  73. WHERE `target_domain` = :object");
  74. $stmt->execute(array(':object' => $object));
  75. $alias_domains = $stmt->fetchAll(PDO::FETCH_ASSOC);
  76. array_filter($alias_domains);
  77. while ($row = array_shift($alias_domains)) {
  78. $rcpt[] = '/.*@' . $row['alias_domain'] . '/i';
  79. }
  80. }
  81. if (!empty($rcpt)) {
  82. return $rcpt;
  83. }
  84. return false;
  85. }
  86. ?>
  87. settings {
  88. watchdog {
  89. priority = 10;
  90. rcpt = "/null@localhost/i";
  91. from = "/watchdog@localhost/i";
  92. apply "default" {
  93. actions {
  94. reject = 9999.0;
  95. greylist = 9998.0;
  96. "add header" = 9997.0;
  97. }
  98. }
  99. }
  100. <?php
  101. /*
  102. // Start custom scores for users
  103. */
  104. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'highspamlevel' OR `option` = 'lowspamlevel'");
  105. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  106. while ($row = array_shift($rows)) {
  107. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  108. ?>
  109. score_<?=$username_sane;?> {
  110. priority = 4;
  111. <?php
  112. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  113. ?>
  114. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  115. <?php
  116. }
  117. $stmt = $pdo->prepare("SELECT `option`, `value` FROM `filterconf`
  118. WHERE (`option` = 'highspamlevel' OR `option` = 'lowspamlevel')
  119. AND `object`= :object");
  120. $stmt->execute(array(':object' => $row['object']));
  121. $spamscore = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
  122. ?>
  123. apply "default" {
  124. actions {
  125. reject = <?=$spamscore['highspamlevel'][0];?>;
  126. greylist = <?=$spamscore['lowspamlevel'][0] - 1;?>;
  127. "add header" = <?=$spamscore['lowspamlevel'][0];?>;
  128. }
  129. }
  130. }
  131. <?php
  132. }
  133. /*
  134. // Start whitelist
  135. */
  136. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'whitelist_from'");
  137. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  138. while ($row = array_shift($rows)) {
  139. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  140. ?>
  141. whitelist_<?=$username_sane;?> {
  142. <?php
  143. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('^', `value`, '$'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  144. WHERE `object`= :object
  145. AND `option` = 'whitelist_from'");
  146. $stmt->execute(array(':object' => $row['object']));
  147. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  148. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  149. ?>
  150. from = "/(<?=$value_sane;?>)/i";
  151. <?php
  152. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  153. ?>
  154. priority = 5;
  155. <?php
  156. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  157. ?>
  158. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  159. <?php
  160. }
  161. }
  162. else {
  163. ?>
  164. priority = 6;
  165. <?php
  166. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  167. ?>
  168. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  169. <?php
  170. }
  171. }
  172. ?>
  173. apply "default" {
  174. MAILCOW_WHITE = -999.0;
  175. }
  176. symbols [
  177. "MAILCOW_WHITE"
  178. ]
  179. }
  180. whitelist_header_<?=$username_sane;?> {
  181. <?php
  182. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('\<', `value`, '\>'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  183. WHERE `object`= :object
  184. AND `option` = 'whitelist_from'");
  185. $stmt->execute(array(':object' => $row['object']));
  186. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  187. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  188. ?>
  189. header = {
  190. "From" = "/(<?=$value_sane;?>)/i";
  191. }
  192. <?php
  193. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  194. ?>
  195. priority = 5;
  196. <?php
  197. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  198. ?>
  199. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  200. <?php
  201. }
  202. }
  203. else {
  204. ?>
  205. priority = 6;
  206. <?php
  207. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  208. ?>
  209. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  210. <?php
  211. }
  212. }
  213. ?>
  214. apply "default" {
  215. MAILCOW_WHITE = -999.0;
  216. }
  217. symbols [
  218. "MAILCOW_WHITE"
  219. ]
  220. }
  221. <?php
  222. }
  223. /*
  224. // Start blacklist
  225. */
  226. $stmt = $pdo->query("SELECT DISTINCT `object` FROM `filterconf` WHERE `option` = 'blacklist_from'");
  227. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  228. while ($row = array_shift($rows)) {
  229. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['object']);
  230. ?>
  231. blacklist_<?=$username_sane;?> {
  232. <?php
  233. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('^', `value`, '$'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  234. WHERE `object`= :object
  235. AND `option` = 'blacklist_from'");
  236. $stmt->execute(array(':object' => $row['object']));
  237. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  238. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  239. ?>
  240. from = "/(<?=$value_sane;?>)/i";
  241. <?php
  242. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  243. ?>
  244. priority = 5;
  245. <?php
  246. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  247. ?>
  248. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  249. <?php
  250. }
  251. }
  252. else {
  253. ?>
  254. priority = 6;
  255. <?php
  256. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  257. ?>
  258. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  259. <?php
  260. }
  261. }
  262. ?>
  263. apply "default" {
  264. MAILCOW_BLACK = 999.0;
  265. }
  266. symbols [
  267. "MAILCOW_BLACK"
  268. ]
  269. }
  270. blacklist_header_<?=$username_sane;?> {
  271. <?php
  272. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(REPLACE(CONCAT('\<', `value`, '\>'), '*', '.*') SEPARATOR '|') AS `value` FROM `filterconf`
  273. WHERE `object`= :object
  274. AND `option` = 'blacklist_from'");
  275. $stmt->execute(array(':object' => $row['object']));
  276. $grouped_lists = $stmt->fetchAll(PDO::FETCH_COLUMN);
  277. $value_sane = preg_replace("/\.\./", ".", (preg_replace("/\*/", ".*", $grouped_lists[0])));
  278. ?>
  279. header = {
  280. "From" = "/(<?=$value_sane;?>)/i";
  281. }
  282. <?php
  283. if (!filter_var(trim($row['object']), FILTER_VALIDATE_EMAIL)) {
  284. ?>
  285. priority = 5;
  286. <?php
  287. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  288. ?>
  289. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  290. <?php
  291. }
  292. }
  293. else {
  294. ?>
  295. priority = 6;
  296. <?php
  297. foreach (ucl_rcpts($row['object'], strpos($row['object'], '@') === FALSE ? 'domain' : 'mailbox') as $rcpt) {
  298. ?>
  299. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  300. <?php
  301. }
  302. }
  303. ?>
  304. apply "default" {
  305. MAILCOW_BLACK = 999.0;
  306. }
  307. symbols [
  308. "MAILCOW_BLACK"
  309. ]
  310. }
  311. <?php
  312. }
  313. /*
  314. // Start traps
  315. */
  316. ?>
  317. traps {
  318. <?php
  319. foreach (ucl_rcpts('spam@localhost', 'mailbox') as $rcpt) {
  320. ?>
  321. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  322. <?php
  323. }
  324. foreach (ucl_rcpts('ham@localhost', 'mailbox') as $rcpt) {
  325. ?>
  326. rcpt = <?=json_encode($rcpt, JSON_UNESCAPED_SLASHES);?>;
  327. <?php
  328. }
  329. ?>
  330. priority = 9;
  331. want_spam = yes;
  332. }
  333. <?php
  334. // Start additional content
  335. $stmt = $pdo->query("SELECT `id`, `content` FROM `settingsmap` WHERE `active` = '1'");
  336. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  337. while ($row = array_shift($rows)) {
  338. $username_sane = preg_replace("/[^a-zA-Z0-9]+/", "", $row['id']);
  339. ?>
  340. additional_settings_<?=intval($row['id']);?> {
  341. <?php
  342. $content = preg_split('/\r\n|\r|\n/', $row['content']);
  343. foreach ($content as $line) {
  344. echo ' ' . $line . PHP_EOL;
  345. }
  346. }
  347. ?>
  348. }