settings.php 12 KB

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