settings.php 12 KB

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