Happy New Year
[squirrelmail.git] / plugins / filters / filters.php
1 <?php
2
3 /**
4 * Message and Spam Filter Plugin - Filtering Functions
5 *
6 * @copyright 1999-2018 The SquirrelMail Project Team
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 * @version $Id$
9 * @package plugins
10 * @subpackage filters
11 */
12
13 // TODO: This plugin has an issue that seems to corrupt folder cache for subfolders of INBOX
14 /**
15 * do not allow to call this file directly
16 */
17 if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) {
18 header("Location: ../../src/login.php");
19 die();
20 }
21
22 /** load globals */
23 global $UseSeparateImapConnection,
24 $AllowSpamFilters, $SpamFilters_YourHop, $SpamFilters_ShowCommercial,
25 $SpamFilters_DNScache, $SpamFilters_BulkQuery, $SpamFilters_SharedCache,
26 $SpamFilters_CacheTTL;
27
28 /**
29 * load required functions. Plugin depends on IMAP functions and they are not
30 * loaded in src/webmail.php
31 */
32 include_once (SM_PATH . 'functions/imap.php');
33
34 /** load default config */
35 if (file_exists(SM_PATH . 'plugins/filters/config_default.php')) {
36 include_once (SM_PATH . 'plugins/filters/config_default.php');
37 } else {
38 // default config was removed.
39 $UseSeparateImapConnection = false;
40 $AllowSpamFilters = true;
41 $SpamFilters_YourHop = ' ';
42 $SpamFilters_ShowCommercial = false;
43 $SpamFilters_DNScache = array();
44 $SpamFilters_BulkQuery = '';
45 $SpamFilters_SharedCache = true;
46 $SpamFilters_CacheTTL = 7200;
47 }
48
49 if (file_exists(SM_PATH . 'config/filters_config.php')) {
50 include_once (SM_PATH . 'config/filters_config.php');
51 } elseif (file_exists(SM_PATH . 'plugins/filters/config.php')) {
52 include_once (SM_PATH . 'plugins/filters/config.php');
53 }
54
55 /**
56 * Register option blocks
57 * @access private
58 */
59 function filters_optpage_register_block() {
60 global $optpage_blocks, $AllowSpamFilters;
61
62 $optpage_blocks[] = array(
63 'name' => _("Message Filters"),
64 'url' => SM_PATH . 'plugins/filters/options.php',
65 'desc' => _("Filtering enables messages with different criteria to be automatically filtered into different folders for easier organization."),
66 'js' => false
67 );
68
69 if ($AllowSpamFilters) {
70 $optpage_blocks[] = array(
71 'name' => _("SPAM Filters"),
72 'url' => SM_PATH . 'plugins/filters/spamoptions.php',
73 'desc' => _("SPAM filters allow you to select from various DNS based blacklists to detect junk email in your INBOX and move it to another folder (like Trash)."),
74 'js' => false
75 );
76 }
77 }
78
79 /* Receive the status of the folder and do something with it */
80 function filters_folder_status($statusarr) {
81
82 global $filter_inbox_count;
83 if (empty($filter_inbox_count)) $filter_inbox_count=0;
84
85 if ($statusarr['MAILBOX'] == 'INBOX')
86 {
87 if (!empty($statusarr['MESSAGES'])) $filter_inbox_count=$statusarr['MESSAGES'];
88 }
89 }
90
91 /**
92 * Saves the DNS Cache to disk
93 * @access private
94 */
95 function filters_SaveCache () {
96 global $data_dir, $SpamFilters_DNScache;
97
98 if (file_exists($data_dir . '/dnscache')) {
99 $fp = fopen($data_dir . '/dnscache', 'r');
100 } else {
101 $fp = false;
102 }
103 if ($fp) {
104 flock($fp,LOCK_EX);
105 } else {
106 $fp = fopen($data_dir . '/dnscache', 'w+');
107 fclose($fp);
108 $fp = fopen($data_dir . '/dnscache', 'r');
109 flock($fp,LOCK_EX);
110 }
111 $fp1 = fopen($data_dir . '/dnscache', 'w+');
112
113 foreach ($SpamFilters_DNScache as $Key=> $Value) {
114 $tstr = $Key . ',' . $Value['L'] . ',' . $Value['T'] . "\n";
115 fputs ($fp1, $tstr);
116 }
117 fclose($fp1);
118 flock($fp,LOCK_UN);
119 fclose($fp);
120 }
121
122 /**
123 * Loads the DNS Cache from disk
124 * @access private
125 */
126 function filters_LoadCache () {
127 global $data_dir, $SpamFilters_DNScache;
128
129 if (file_exists($data_dir . '/dnscache')) {
130 $SpamFilters_DNScache = array();
131 if ($fp = fopen ($data_dir . '/dnscache', 'r')) {
132 flock($fp,LOCK_SH);
133 while ($data = fgetcsv($fp,1024)) {
134 if ($data[2] > time()) {
135 $SpamFilters_DNScache[$data[0]]['L'] = $data[1];
136 $SpamFilters_DNScache[$data[0]]['T'] = $data[2];
137 }
138 }
139 flock($fp,LOCK_UN);
140 }
141 }
142 }
143
144 /**
145 * Uses the BulkQuery executable to query all the RBLs at once
146 * @param array $filters Array of SPAM Filters
147 * @param array $IPs Array of IP Addresses
148 * @access private
149 */
150 function filters_bulkquery($filters, $IPs) {
151 global $attachment_dir, $username,
152 $SpamFilters_DNScache, $SpamFilters_BulkQuery,
153 $SpamFilters_CacheTTL;
154
155 if (count($IPs) > 0) {
156 $rbls = array();
157 foreach ($filters as $key => $value) {
158 if ($filters[$key]['enabled']) {
159 if ($filters[$key]['dns']) {
160 $rbls[$filters[$key]['dns']] = true;
161 }
162 }
163 }
164
165 $bqfil = $attachment_dir . $username . '-bq.in';
166 $fp = fopen($bqfil, 'w');
167 fputs ($fp, $SpamFilters_CacheTTL . "\n");
168 foreach ($rbls as $key => $value) {
169 fputs ($fp, '.' . $key . "\n");
170 }
171 fputs ($fp, "----------\n");
172 foreach ($IPs as $key => $value) {
173 fputs ($fp, $key . "\n");
174 }
175 fclose ($fp);
176 $bqout = array();
177 exec ($SpamFilters_BulkQuery . ' < ' . $bqfil, $bqout);
178 foreach ($bqout as $value) {
179 $Chunks = explode(',', $value);
180 $SpamFilters_DNScache[$Chunks[0]]['L'] = $Chunks[1];
181 $SpamFilters_DNScache[$Chunks[0]]['T'] = $Chunks[2] + time();
182 }
183 unlink($bqfil);
184 }
185 }
186
187 /**
188 * Starts the filtering process
189 * @param array $hook_args (since 1.5.2) do hook arguments. Is used to check
190 * hook name, array key = 0.
191 * @access private
192 */
193 function start_filters($hook_args) {
194 global $imapServerAddress, $imapPort, $imap_stream_options, $imap_stream,
195 $imapConnection, $UseSeparateImapConnection, $AllowSpamFilters,
196 $filter_inbox_count, $username;
197
198 // if there were filtering errors previously during
199 // this login session, we won't try again
200 //
201 // (errors that this plugin was able to catch or a "NO"
202 // response/failure from IMAP found in the current session,
203 // which could have resulted from an attempted filter copy
204 // (over quota), in which case execution halts before this
205 // plugin can catch the problem -- note, however, that any
206 // other IMAP "NO" failure (caused by unrelated actions) at
207 // any time during the current session will cause this plugin
208 // to effectively shut down)
209 //
210 sqgetGlobalVar('filters_error', $filters_error, SQ_SESSION, FALSE);
211 sqgetGlobalVar('IMAP_FATAL_ERROR_TYPE', $imap_fatal_error, SQ_SESSION, '');
212 if ($filters_error || $imap_fatal_error == 'NO')
213 return;
214
215 /**
216 * check hook that calls filtering. If filters are called by right_main_after_header,
217 * do filtering only when we are in INBOX folder.
218 */
219 if ($hook_args[0]=='right_main_after_header' &&
220 (sqgetGlobalVar('mailbox',$mailbox,SQ_FORM) && $mailbox!='INBOX')) {
221 return;
222 }
223
224 $filters = load_filters();
225
226 // No point running spam filters if there aren't any to run //
227 if ($AllowSpamFilters) {
228 $spamfilters = load_spam_filters();
229
230 $AllowSpamFilters = false;
231 foreach($spamfilters as $value) {
232 if ($value['enabled'] == SMPREF_ON) {
233 $AllowSpamFilters = true;
234 break;
235 }
236 }
237 }
238
239 // No user filters, and no spam filters, no need to continue //
240 if (!$AllowSpamFilters && empty($filters)) {
241 return;
242 }
243
244
245 // Detect if we have already connected to IMAP or not.
246 // Also check if we are forced to use a separate IMAP connection
247 if ((!isset($imap_stream) && !isset($imapConnection)) ||
248 $UseSeparateImapConnection ) {
249 $stream = sqimap_login($username, false, $imapServerAddress,
250 $imapPort, 10, $imap_stream_options);
251 $previously_connected = false;
252 } else if (isset($imapConnection)) {
253 $stream = $imapConnection;
254 $previously_connected = true;
255 } else {
256 $previously_connected = true;
257 $stream = $imap_stream;
258 }
259
260 if (!isset($filter_inbox_count)) {
261 $aStatus = sqimap_status_messages ($stream, 'INBOX', array('MESSAGES'));
262 if (!empty($aStatus['MESSAGES'])) {
263 $filter_inbox_count=$aStatus['MESSAGES'];
264 } else {
265 $filter_inbox_count=0;
266 }
267 }
268
269 if ($filter_inbox_count > 0) {
270 sqimap_mailbox_select($stream, 'INBOX');
271 // Filter spam from inbox before we sort them into folders
272 if ($AllowSpamFilters) {
273 spam_filters($stream);
274 }
275
276 // Sort into folders
277 user_filters($stream);
278 }
279
280 if (!$previously_connected) {
281 sqimap_logout($stream);
282 }
283 }
284
285 /**
286 * Does the loop through each filter
287 * @param stream imap_stream the stream to read from
288 * @access private
289 */
290 function user_filters($imap_stream) {
291 global $data_dir, $username;
292 $filters = load_filters();
293 if (! $filters) return;
294 $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
295
296 $expunge = false;
297 // For every rule
298 for ($i=0, $num = count($filters); $i < $num; $i++) {
299 // If it is the "combo" rule
300 if ($filters[$i]['where'] == 'To or Cc') {
301 /*
302 * If it's "TO OR CC", we have to do two searches, one for TO
303 * and the other for CC.
304 */
305 $expunge = filter_search_and_delete($imap_stream, 'TO',
306 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
307 $expunge = filter_search_and_delete($imap_stream, 'CC',
308 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
309 } else if ($filters[$i]['where'] == 'Header and Body') {
310 $expunge = filter_search_and_delete($imap_stream, 'TEXT',
311 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
312 } else if ($filters[$i]['where'] == 'Message Body') {
313 $expunge = filter_search_and_delete($imap_stream, 'BODY',
314 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
315 } else {
316 /*
317 * If it's a normal TO, CC, SUBJECT, or FROM, then handle it
318 * normally.
319 */
320 $expunge = filter_search_and_delete($imap_stream, $filters[$i]['where'],
321 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
322 }
323 }
324 // Clean out the mailbox whether or not auto_expunge is on
325 // That way it looks like it was redirected properly
326 if ($expunge) {
327 sqimap_mailbox_expunge($imap_stream, 'INBOX');
328 }
329 }
330
331 /**
332 * Creates and runs the IMAP command to filter messages
333 * @param string $imap_stream TODO: Document this parameter
334 * @param string $where Which part of the message to search (TO, CC, SUBJECT, etc...)
335 * @param string $what String to search for
336 * @param string $where_to Folder it will move to
337 * @param string $user_scan Whether to search all or just unseen
338 * @param string $should_expunge
339 * @access private
340 */
341 function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_scan,
342 $should_expunge) {
343 global $languages, $squirrelmail_language, $allow_charset_search, $imap_server_type;
344
345 //TODO: make use of new mailbox cache. See mailbox_display.phpinfo
346
347 if (strtolower($where_to) == 'inbox') {
348 return array();
349 }
350
351 if ($user_scan == 'new') {
352 $category = 'UNSEEN';
353 } else {
354 $category = 'ALL';
355 }
356 $category .= ' UNDELETED';
357
358 if ($allow_charset_search &&
359 isset($languages[$squirrelmail_language]['CHARSET']) &&
360 $languages[$squirrelmail_language]['CHARSET']) {
361 $search_str = 'SEARCH CHARSET '
362 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
363 . ' ' . $category;
364 } else {
365 $search_str = 'SEARCH CHARSET US-ASCII ' . $category;
366 }
367 if ($where == 'Header') {
368 $what = explode(':', $what);
369 $where = strtoupper($where);
370 $where = trim($where . ' ' . $what[0]);
371 $what = addslashes(trim($what[1]));
372 }
373
374 // see comments in squirrelmail sqimap_search function
375 if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') {
376 $search_str .= ' ' . $where . ' ' . $what;
377 /* read data back from IMAP */
378 $read = sqimap_run_command($imap_stream, $search_str, true, $response, $message, TRUE);
379 } else {
380 $search_str .= ' ' . $where . ' {' . strlen($what) . "}";
381 $sid = sqimap_session_id(true);
382 fputs ($imap_stream, $sid . ' ' . $search_str . "\r\n");
383 $read2 = sqimap_fgets($imap_stream);
384 # server should respond with Ready for argument, then we will send search text
385 #echo "RR2 $read2<br>";
386 fputs ($imap_stream, "$what\r\n");
387 #echo "SS $what<br>";
388 $read2 = sqimap_fgets($imap_stream);
389 #echo "RR2 $read2<br>";
390 $read[]=$read2;
391 $read3 = sqimap_fgets($imap_stream);
392 #echo "RR3 $read3<br>";
393 list($rtag,$response,$message)=explode(' ',$read3,3);
394 ## $read2 = sqimap_retrieve_imap_response($imap_stream, $sid, true,
395 ## $response, $message, $search_str, false, true, false);
396 #echo "RR2 $read2 / RESPONSE $response<br>";
397 }
398
399 if (isset($read[0])) {
400 $ids = array();
401 for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
402 if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
403 $ids += explode(' ', trim($regs[1]));
404 }
405 }
406 if ($response == 'OK' && count($ids)) {
407 if (sqimap_mailbox_exists($imap_stream, $where_to)) {
408 if (!sqimap_msgs_list_move ($imap_stream, $ids, $where_to, false)) {
409 // if errors occurred, don't try to filter again during this session
410 sqsession_register(TRUE, 'filters_error');
411 global $color;
412 error_box(_("A problem occurred filtering messages. Check filter settings and account quota if applicable. Filtering is disabled for the remainder of this login session."), $color);
413 }
414
415 // expunge even in the case of errors, in case some
416 // messages were filtered before the error happened
417 $should_expunge = true;
418 }
419 } elseif ($response != 'OK') {
420 $query = $search_str . "\r\n".$what ."\r\n";
421 if ($response == 'NO') {
422 if (strpos($message,'BADCHARSET') !== false ||
423 strpos($message,'character') !== false) {
424 sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message);
425 } else {
426 sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
427 }
428 } else {
429 sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
430 }
431 }
432 }
433 return $should_expunge;
434 }
435
436 /**
437 * Loops through all the Received Headers to find IP Addresses
438 * @param stream imap_stream the stream to read from
439 * @access private
440 */
441 function spam_filters($imap_stream) {
442 global $data_dir, $username;
443 global $SpamFilters_YourHop;
444 global $SpamFilters_DNScache;
445 global $SpamFilters_SharedCache;
446 global $SpamFilters_BulkQuery;
447 global $SpamFilters_CacheTTL;
448
449 $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan');
450 $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder');
451 $filters = load_spam_filters();
452
453 if ($SpamFilters_SharedCache) {
454 filters_LoadCache();
455 }
456
457 $run = false;
458
459 foreach ($filters as $Value) {
460 if ($Value['enabled']) {
461 $run = true;
462 break;
463 }
464 }
465
466 // short-circuit
467 if (!$run) {
468 return;
469 }
470
471 // Ask for a big list of all "Received" headers in the inbox with
472 // flags for each message. Kinda big.
473
474 if ($filters_spam_scan == 'new') {
475 $search_array = array();
476 $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, TRUE);
477 if (isset($read[0])) {
478 for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
479 if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
480 $search_array = explode(' ', trim($regs[1]));
481 break;
482 }
483 }
484 }
485 }
486 if ($filters_spam_scan == 'new' && count($search_array)) {
487 $headers = sqimap_get_small_header_list ($imap_stream, $search_array, array('Received'),array());
488 } else if ($filters_spam_scan != 'new') {
489 $headers = sqimap_get_small_header_list ($imap_stream, null , array('Received'),array());
490 } else {
491 return;
492 }
493 if (!count($headers)) {
494 return;
495 }
496 $bulkquery = (strlen($SpamFilters_BulkQuery) > 0 ? true : false);
497 $IPs = array();
498 $aSpamIds = array();
499 foreach ($headers as $id => $aValue) {
500 if (isset($aValue['UID'])) {
501 $MsgNum = $aValue['UID'];
502 } else {
503 $MsgNum = $id;
504 }
505 // Look through all of the Received headers for IP addresses
506 if (isset($aValue['RECEIVED'])) {
507 foreach ($aValue['RECEIVED'] as $received) {
508 // Check to see if this line is the right "Received from" line
509 // to check
510
511 // $aValue['Received'] is an array with all the received lines.
512 // We should check them from bottom to top and only check the first 2.
513 // Currently we check only the header where $SpamFilters_YourHop in occures
514
515 if (is_int(strpos($received, $SpamFilters_YourHop))) {
516 if (preg_match('/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/',$received,$aMatch)) {
517 $isspam = false;
518 if (filters_spam_check_site($aMatch[1],$aMatch[2],$aMatch[3],$aMatch[4],$filters)) {
519 $aSpamIds[] = $MsgNum;
520 $isspam = true;
521 }
522
523 if ($bulkquery) {
524 array_shift($aMatch);
525 $IP = explode('.', $aMatch);
526 foreach ($filters as $key => $value) {
527 if ($filters[$key]['enabled'] && $filters[$key]['dns']) {
528 if (strlen($SpamFilters_DNScache[$IP.'.'.$filters[$key]['dns']]) == 0) {
529 $IPs[$IP] = true;
530 break;
531 }
532 }
533 }
534 }
535 // If we've checked one IP and YourHop is
536 // just a space
537 if ($SpamFilters_YourHop == ' ' || $isspam) {
538 break; // don't check any more
539 }
540 }
541 }
542 }
543 }
544 }
545 // Lookie! It's spam! Yum!
546 if (count($aSpamIds) && sqimap_mailbox_exists($imap_stream, $filters_spam_folder)) {
547 if (!sqimap_msgs_list_move($imap_stream, $aSpamIds, $filters_spam_folder)) {
548 // if errors occurred, don't try to filter again during this session
549 sqsession_register(TRUE, 'filters_error');
550 global $color;
551 error_box(_("A problem occurred filtering messages. Check filter settings and account quota if applicable. Filtering is disabled for the remainder of this login session."), $color);
552 }
553
554 // expunge even in the case of errors, in case some
555 // messages were filtered before the error happened
556 sqimap_mailbox_expunge($imap_stream, 'INBOX');
557 }
558
559 if ($bulkquery && count($IPs)) {
560 filters_bulkquery($filters, $IPs);
561 }
562
563 if ($SpamFilters_SharedCache) {
564 filters_SaveCache();
565 } else {
566 sqsession_register($SpamFilters_DNScache, 'SpamFilters_DNScache');
567 }
568 }
569
570 /**
571 * Does the loop through each enabled filter for the specified IP address.
572 * IP format: $a.$b.$c.$d
573 * @param int $a First subset of IP
574 * @param int $b Second subset of IP
575 * @param int $c Third subset of IP
576 * @param int $d Forth subset of IP
577 * @param array $filters The Spam Filters
578 * @return boolean Whether the IP is Spam
579 * @access private
580 */
581 function filters_spam_check_site($a, $b, $c, $d, &$filters) {
582 global $SpamFilters_DNScache, $SpamFilters_CacheTTL;
583 foreach ($filters as $key => $value) {
584 if ($filters[$key]['enabled']) {
585 if ($filters[$key]['dns']) {
586
587 /**
588 * RFC allows . on end of hostname to force domain lookup to
589 * not use search domain from resolv.conf, i.e. to ensure
590 * search domain isn't used if no hostname is found
591 */
592 $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' .
593 $filters[$key]['dns'] . '.';
594
595 if(!isset($SpamFilters_DNScache[$filter_revip]['L']))
596 $SpamFilters_DNScache[$filter_revip]['L'] = '';
597
598 if(!isset($SpamFilters_DNScache[$filter_revip]['T']))
599 $SpamFilters_DNScache[$filter_revip]['T'] = '';
600
601 if (strlen($SpamFilters_DNScache[$filter_revip]['L']) == 0) {
602 $SpamFilters_DNScache[$filter_revip]['L'] =
603 gethostbyname($filter_revip);
604 $SpamFilters_DNScache[$filter_revip]['T'] =
605 time() + $SpamFilters_CacheTTL;
606 }
607
608 /**
609 * gethostbyname returns ip if resolved, or returns original
610 * host supplied to function if there is no resolution
611 */
612 if ($SpamFilters_DNScache[$filter_revip]['L'] != $filter_revip) {
613 return 1;
614 }
615 }
616 }
617 }
618 return 0;
619 }
620
621 /**
622 * Loads the filters from the user preferences
623 * @return array All the user filters
624 * @access private
625 */
626 function load_filters() {
627 global $data_dir, $username;
628
629 $filters = array();
630 for ($i = 0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
631 $ary = explode(',', $fltr);
632 $filters[$i]['where'] = $ary[0];
633 $filters[$i]['what'] = str_replace('###COMMA###', ',', $ary[1]);
634 $filters[$i]['folder'] = $ary[2];
635 }
636 return $filters;
637 }
638
639 /**
640 * Loads the Spam Filters and checks the preferences for the enabled status
641 * @return array All the spam filters
642 * @access private
643 */
644 function load_spam_filters() {
645 global $data_dir, $username, $SpamFilters_ShowCommercial;
646
647 if ($SpamFilters_ShowCommercial) {
648 $filters['MAPS RBL']['prefname'] = 'filters_spam_maps_rbl';
649 $filters['MAPS RBL']['name'] = 'MAPS Realtime Blackhole List';
650 $filters['MAPS RBL']['link'] = 'http://www.mail-abuse.org/rbl/';
651 $filters['MAPS RBL']['dns'] = 'blackholes.mail-abuse.org';
652 $filters['MAPS RBL']['result'] = '127.0.0.2';
653 $filters['MAPS RBL']['comment'] =
654 _("COMMERCIAL - This list contains servers that are verified spam senders. It is a pretty reliable list to scan spam from.");
655
656 $filters['MAPS RSS']['prefname'] = 'filters_spam_maps_rss';
657 $filters['MAPS RSS']['name'] = 'MAPS Relay Spam Stopper';
658 $filters['MAPS RSS']['link'] = 'http://www.mail-abuse.org/rss/';
659 $filters['MAPS RSS']['dns'] = 'relays.mail-abuse.org';
660 $filters['MAPS RSS']['result'] = '127.0.0.2';
661 $filters['MAPS RSS']['comment'] =
662 _("COMMERCIAL - Servers that are configured (or misconfigured) to allow spam to be relayed through their system will be banned with this. Another good one to use.");
663
664 $filters['MAPS DUL']['prefname'] = 'filters_spam_maps_dul';
665 $filters['MAPS DUL']['name'] = 'MAPS Dial-Up List';
666 $filters['MAPS DUL']['link'] = 'http://www.mail-abuse.org/dul/';
667 $filters['MAPS DUL']['dns'] = 'dialups.mail-abuse.org';
668 $filters['MAPS DUL']['result'] = '127.0.0.3';
669 $filters['MAPS DUL']['comment'] =
670 _("COMMERCIAL - Dial-up users are often filtered out since they should use their ISP's mail servers to send mail. Spammers typically get a dial-up account and send spam directly from there.");
671
672 $filters['MAPS RBLplus-RBL']['prefname'] = 'filters_spam_maps_rblplus_rbl';
673 $filters['MAPS RBLplus-RBL']['name'] = 'MAPS RBL+ RBL List';
674 $filters['MAPS RBLplus-RBL']['link'] = 'http://www.mail-abuse.org/';
675 $filters['MAPS RBLplus-RBL']['dns'] = 'rbl-plus.mail-abuse.org';
676 $filters['MAPS RBLplus-RBL']['result'] = '127.0.0.2';
677 $filters['MAPS RBLplus-RBL']['comment'] =
678 _("COMMERCIAL - RBL+ Blackhole entries.");
679
680 $filters['MAPS RBLplus-RSS']['prefname'] = 'filters_spam_maps_rblplus_rss';
681 $filters['MAPS RBLplus-RSS']['name'] = 'MAPS RBL+ List RSS entries';
682 $filters['MAPS RBLplus-RSS']['link'] = 'http://www.mail-abuse.org/';
683 $filters['MAPS RBLplus-RSS']['dns'] = 'rbl-plus.mail-abuse.org';
684 $filters['MAPS RBLplus-RSS']['result'] = '127.0.0.2';
685 $filters['MAPS RBLplus-RSS']['comment'] =
686 _("COMMERCIAL - RBL+ OpenRelay entries.");
687
688 $filters['MAPS RBLplus-DUL']['prefname'] = 'filters_spam_maps_rblplus_dul';
689 $filters['MAPS RBLplus-DUL']['name'] = 'MAPS RBL+ List DUL entries';
690 $filters['MAPS RBLplus-DUL']['link'] = 'http://www.mail-abuse.org/';
691 $filters['MAPS RBLplus-DUL']['dns'] = 'rbl-plus.mail-abuse.org';
692 $filters['MAPS RBLplus-DUL']['result'] = '127.0.0.3';
693 $filters['MAPS RBLplus-DUL']['comment'] =
694 _("COMMERCIAL - RBL+ Dial-up entries.");
695 }
696
697 $filters['FiveTen Direct']['prefname'] = 'filters_spam_fiveten_src';
698 $filters['FiveTen Direct']['name'] = 'Five-Ten-sg.com Direct SPAM Sources';
699 $filters['FiveTen Direct']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
700 $filters['FiveTen Direct']['dns'] = 'blackholes.five-ten-sg.com';
701 $filters['FiveTen Direct']['result'] = '127.0.0.2';
702 $filters['FiveTen Direct']['comment'] =
703 _("FREE - Five-Ten-sg.com - Direct SPAM sources.");
704
705 $filters['FiveTen DUL']['prefname'] = 'filters_spam_fiveten_dul';
706 $filters['FiveTen DUL']['name'] = 'Five-Ten-sg.com DUL Lists';
707 $filters['FiveTen DUL']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
708 $filters['FiveTen DUL']['dns'] = 'blackholes.five-ten-sg.com';
709 $filters['FiveTen DUL']['result'] = '127.0.0.3';
710 $filters['FiveTen DUL']['comment'] =
711 _("FREE - Five-Ten-sg.com - Dial-up lists - includes some DSL IPs.");
712
713 $filters['FiveTen Unc. OptIn']['prefname'] = 'filters_spam_fiveten_oi';
714 $filters['FiveTen Unc. OptIn']['name'] = 'Five-Ten-sg.com Unconfirmed OptIn Lists';
715 $filters['FiveTen Unc. OptIn']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
716 $filters['FiveTen Unc. OptIn']['dns'] = 'blackholes.five-ten-sg.com';
717 $filters['FiveTen Unc. OptIn']['result'] = '127.0.0.4';
718 $filters['FiveTen Unc. OptIn']['comment'] =
719 _("FREE - Five-Ten-sg.com - Bulk mailers that do not use confirmed opt-in.");
720
721 $filters['FiveTen Others']['prefname'] = 'filters_spam_fiveten_oth';
722 $filters['FiveTen Others']['name'] = 'Five-Ten-sg.com Other Misc. Servers';
723 $filters['FiveTen Others']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
724 $filters['FiveTen Others']['dns'] = 'blackholes.five-ten-sg.com';
725 $filters['FiveTen Others']['result'] = '127.0.0.5';
726 $filters['FiveTen Others']['comment'] =
727 _("FREE - Five-Ten-sg.com - Other misc. servers.");
728
729 $filters['FiveTen Single Stage']['prefname'] = 'filters_spam_fiveten_ss';
730 $filters['FiveTen Single Stage']['name'] = 'Five-Ten-sg.com Single Stage Servers';
731 $filters['FiveTen Single Stage']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
732 $filters['FiveTen Single Stage']['dns'] = 'blackholes.five-ten-sg.com';
733 $filters['FiveTen Single Stage']['result'] = '127.0.0.6';
734 $filters['FiveTen Single Stage']['comment'] =
735 _("FREE - Five-Ten-sg.com - Single Stage servers.");
736
737 $filters['FiveTen SPAM Support']['prefname'] = 'filters_spam_fiveten_supp';
738 $filters['FiveTen SPAM Support']['name'] = 'Five-Ten-sg.com SPAM Support Servers';
739 $filters['FiveTen SPAM Support']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
740 $filters['FiveTen SPAM Support']['dns'] = 'blackholes.five-ten-sg.com';
741 $filters['FiveTen SPAM Support']['result'] = '127.0.0.7';
742 $filters['FiveTen SPAM Support']['comment'] =
743 _("FREE - Five-Ten-sg.com - SPAM Support servers.");
744
745 $filters['FiveTen Web forms']['prefname'] = 'filters_spam_fiveten_wf';
746 $filters['FiveTen Web forms']['name'] = 'Five-Ten-sg.com Web Form IPs';
747 $filters['FiveTen Web forms']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
748 $filters['FiveTen Web forms']['dns'] = 'blackholes.five-ten-sg.com';
749 $filters['FiveTen Web forms']['result'] = '127.0.0.8';
750 $filters['FiveTen Web forms']['comment'] =
751 _("FREE - Five-Ten-sg.com - Web Form IPs.");
752
753 $filters['Dorkslayers']['prefname'] = 'filters_spam_dorks';
754 $filters['Dorkslayers']['name'] = 'Dorkslayers Lists';
755 $filters['Dorkslayers']['link'] = 'http://www.dorkslayers.com';
756 $filters['Dorkslayers']['dns'] = 'orbs.dorkslayers.com';
757 $filters['Dorkslayers']['result'] = '127.0.0.2';
758 $filters['Dorkslayers']['comment'] =
759 _("FREE - Dorkslayers appears to include only really bad open relays outside the US to avoid being sued. Interestingly enough, their website recommends you NOT use their service.");
760
761 $filters['SPAMhaus']['prefname'] = 'filters_spam_spamhaus';
762 $filters['SPAMhaus']['name'] = 'SPAMhaus Lists';
763 $filters['SPAMhaus']['link'] = 'http://www.spamhaus.org';
764 $filters['SPAMhaus']['dns'] = 'sbl.spamhaus.org';
765 $filters['SPAMhaus']['result'] = '127.0.0.2';
766 $filters['SPAMhaus']['comment'] =
767 _("FREE - SPAMhaus - A list of well-known SPAM sources.");
768
769 $filters['SPAMcop']['prefname'] = 'filters_spam_spamcop';
770 $filters['SPAMcop']['name'] = 'SPAM Cop Lists';
771 $filters['SPAMcop']['link'] = 'http://spamcop.net/bl.shtml';
772 $filters['SPAMcop']['dns'] = 'bl.spamcop.net';
773 $filters['SPAMcop']['result'] = '127.0.0.2';
774 $filters['SPAMcop']['comment'] =
775 _("FREE, for now - SpamCop - An interesting solution that lists servers that have a very high spam to legit email ratio (85 percent or more).");
776
777 $filters['dev.null.dk']['prefname'] = 'filters_spam_devnull';
778 $filters['dev.null.dk']['name'] = 'dev.null.dk Lists';
779 $filters['dev.null.dk']['link'] = 'http://dev.null.dk/';
780 $filters['dev.null.dk']['dns'] = 'dev.null.dk';
781 $filters['dev.null.dk']['result'] = '127.0.0.2';
782 $filters['dev.null.dk']['comment'] =
783 _("FREE - dev.null.dk - I don't have any detailed info on this list.");
784
785 $filters['visi.com']['prefname'] = 'filters_spam_visi';
786 $filters['visi.com']['name'] = 'visi.com Relay Stop List';
787 $filters['visi.com']['link'] = 'http://relays.visi.com';
788 $filters['visi.com']['dns'] = 'relays.visi.com';
789 $filters['visi.com']['result'] = '127.0.0.2';
790 $filters['visi.com']['comment'] =
791 _("FREE - visi.com - Relay Stop List. Very conservative OpenRelay List.");
792
793 $filters['ahbl.org Open Relays']['prefname'] = 'filters_spam_2mb_or';
794 $filters['ahbl.org Open Relays']['name'] = 'ahbl.org Open Relays List';
795 $filters['ahbl.org Open Relays']['link'] = 'http://www.ahbl.org/';
796 $filters['ahbl.org Open Relays']['dns'] = 'dnsbl.ahbl.org';
797 $filters['ahbl.org Open Relays']['result'] = '127.0.0.2';
798 $filters['ahbl.org Open Relays']['comment'] =
799 _("FREE - ahbl.org Open Relays - Another list of Open Relays.");
800
801 $filters['ahbl.org SPAM Source']['prefname'] = 'filters_spam_2mb_ss';
802 $filters['ahbl.org SPAM Source']['name'] = 'ahbl.org SPAM Source List';
803 $filters['ahbl.org SPAM Source']['link'] = 'http://www.ahbl.org/';
804 $filters['ahbl.org SPAM Source']['dns'] = 'dnsbl.ahbl.org';
805 $filters['ahbl.org SPAM Source']['result'] = '127.0.0.4';
806 $filters['ahbl.org SPAM Source']['comment'] =
807 _("FREE - ahbl.org SPAM Source - List of Direct SPAM Sources.");
808
809 $filters['ahbl.org SPAM ISPs']['prefname'] = 'filters_spam_2mb_isp';
810 $filters['ahbl.org SPAM ISPs']['name'] = 'ahbl.org SPAM-friendly ISP List';
811 $filters['ahbl.org SPAM ISPs']['link'] = 'http://www.ahbl.org/';
812 $filters['ahbl.org SPAM ISPs']['dns'] = 'dnsbl.ahbl.org';
813 $filters['ahbl.org SPAM ISPs']['result'] = '127.0.0.7';
814 $filters['ahbl.org SPAM ISPs']['comment'] =
815 _("FREE - ahbl.org SPAM ISPs - List of SPAM-friendly ISPs.");
816
817 $filters['Leadmon DUL']['prefname'] = 'filters_spam_lm_dul';
818 $filters['Leadmon DUL']['name'] = 'Leadmon.net DUL List';
819 $filters['Leadmon DUL']['link'] = 'http://www.leadmon.net/spamguard/';
820 $filters['Leadmon DUL']['dns'] = 'spamguard.leadmon.net';
821 $filters['Leadmon DUL']['result'] = '127.0.0.2';
822 $filters['Leadmon DUL']['comment'] =
823 _("FREE - Leadmon DUL - Another list of Dial-up or otherwise dynamically assigned IPs.");
824
825 $filters['Leadmon SPAM Source']['prefname'] = 'filters_spam_lm_ss';
826 $filters['Leadmon SPAM Source']['name'] = 'Leadmon.net SPAM Source List';
827 $filters['Leadmon SPAM Source']['link'] = 'http://www.leadmon.net/spamguard/';
828 $filters['Leadmon SPAM Source']['dns'] = 'spamguard.leadmon.net';
829 $filters['Leadmon SPAM Source']['result'] = '127.0.0.3';
830 $filters['Leadmon SPAM Source']['comment'] =
831 _("FREE - Leadmon SPAM Source - List of IPs Leadmon.net has received SPAM directly from.");
832
833 $filters['Leadmon Bulk Mailers']['prefname'] = 'filters_spam_lm_bm';
834 $filters['Leadmon Bulk Mailers']['name'] = 'Leadmon.net Bulk Mailers List';
835 $filters['Leadmon Bulk Mailers']['link'] = 'http://www.leadmon.net/spamguard/';
836 $filters['Leadmon Bulk Mailers']['dns'] = 'spamguard.leadmon.net';
837 $filters['Leadmon Bulk Mailers']['result'] = '127.0.0.4';
838 $filters['Leadmon Bulk Mailers']['comment'] =
839 _("FREE - Leadmon Bulk Mailers - Bulk mailers that do not require confirmed opt-in or that have allowed known spammers to become clients and abuse their services.");
840
841 $filters['Leadmon Open Relays']['prefname'] = 'filters_spam_lm_or';
842 $filters['Leadmon Open Relays']['name'] = 'Leadmon.net Open Relays List';
843 $filters['Leadmon Open Relays']['link'] = 'http://www.leadmon.net/spamguard/';
844 $filters['Leadmon Open Relays']['dns'] = 'spamguard.leadmon.net';
845 $filters['Leadmon Open Relays']['result'] = '127.0.0.5';
846 $filters['Leadmon Open Relays']['comment'] =
847 _("FREE - Leadmon Open Relays - Single Stage Open Relays that are not listed on other active RBLs.");
848
849 $filters['Leadmon Multi-stage']['prefname'] = 'filters_spam_lm_ms';
850 $filters['Leadmon Multi-stage']['name'] = 'Leadmon.net Multi-Stage Relay List';
851 $filters['Leadmon Multi-stage']['link'] = 'http://www.leadmon.net/spamguard/';
852 $filters['Leadmon Multi-stage']['dns'] = 'spamguard.leadmon.net';
853 $filters['Leadmon Multi-stage']['result'] = '127.0.0.6';
854 $filters['Leadmon Multi-stage']['comment'] =
855 _("FREE - Leadmon Multi-stage - Multi-Stage Open Relays that are not listed on other active RBLs and that have sent SPAM to Leadmon.net.");
856
857 $filters['Leadmon SpamBlock']['prefname'] = 'filters_spam_lm_sb';
858 $filters['Leadmon SpamBlock']['name'] = 'Leadmon.net SpamBlock Sites List';
859 $filters['Leadmon SpamBlock']['link'] = 'http://www.leadmon.net/spamguard/';
860 $filters['Leadmon SpamBlock']['dns'] = 'spamguard.leadmon.net';
861 $filters['Leadmon SpamBlock']['result'] = '127.0.0.7';
862 $filters['Leadmon SpamBlock']['comment'] =
863 _("FREE - Leadmon SpamBlock - Sites on this listing have sent Leadmon.net direct SPAM from IPs in netblocks where the entire block has no DNS mappings. It's a list of BLOCKS of IPs being used by people who have SPAMmed Leadmon.net.");
864
865 $filters['NJABL Open Relays']['prefname'] = 'filters_spam_njabl_or';
866 $filters['NJABL Open Relays']['name'] = 'NJABL Open Relay/Direct Spam Source List';
867 $filters['NJABL Open Relays']['link'] = 'http://www.njabl.org/';
868 $filters['NJABL Open Relays']['dns'] = 'dnsbl.njabl.org';
869 $filters['NJABL Open Relays']['result'] = '127.0.0.2';
870 $filters['NJABL Open Relays']['comment'] =
871 _("FREE, for now - Not Just Another Blacklist - Both Open Relays and Direct SPAM Sources.");
872
873 $filters['NJABL DUL']['prefname'] = 'filters_spam_njabl_dul';
874 $filters['NJABL DUL']['name'] = 'NJABL Dial-ups List';
875 $filters['NJABL DUL']['link'] = 'http://www.njabl.org/';
876 $filters['NJABL DUL']['dns'] = 'dnsbl.njabl.org';
877 $filters['NJABL DUL']['result'] = '127.0.0.3';
878 $filters['NJABL DUL']['comment'] =
879 _("FREE, for now - Not Just Another Blacklist - Dial-up IPs.");
880
881 foreach ($filters as $Key => $Value) {
882 $filters[$Key]['enabled'] = (bool)getPref($data_dir, $username, $filters[$Key]['prefname']);
883 }
884
885 return $filters;
886 }
887
888 /**
889 * Removes a User filter
890 * @param int $id ID of the filter to remove
891 * @access private
892 */
893 function remove_filter ($id) {
894 global $data_dir, $username;
895
896 while ($nextFilter = getPref($data_dir, $username, 'filter' . ($id + 1))) {
897 setPref($data_dir, $username, 'filter' . $id, $nextFilter);
898 $id ++;
899 }
900
901 removePref($data_dir, $username, 'filter' . $id);
902 }
903
904 /**
905 * Swaps two filters
906 * @param int $id1 ID of first filter to swap
907 * @param int $id2 ID of second filter to swap
908 * @access private
909 */
910 function filter_swap($id1, $id2) {
911 global $data_dir, $username;
912
913 $FirstFilter = getPref($data_dir, $username, 'filter' . $id1);
914 $SecondFilter = getPref($data_dir, $username, 'filter' . $id2);
915
916 if ($FirstFilter && $SecondFilter) {
917 setPref($data_dir, $username, 'filter' . $id2, $FirstFilter);
918 setPref($data_dir, $username, 'filter' . $id1, $SecondFilter);
919 }
920 }
921
922 /**
923 * This updates the filter rules when renaming or deleting folders
924 * @param array $args
925 * @access private
926 */
927 function update_for_folder ($args) {
928 $old_folder = $args[0];
929 $new_folder = $args[2];
930 $action = $args[1];
931 global $data_dir, $username;
932 $filters = array();
933 $filters = load_filters();
934 $filter_count = count($filters);
935 $p = 0;
936 for ($i = 0; $i < $filter_count; $i++) {
937 if (!empty($filters)) {
938 if ($old_folder == $filters[$i]['folder']) {
939 if ($action == 'rename') {
940 $filters[$i]['folder'] = $new_folder;
941 setPref($data_dir, $username, 'filter'.$i,
942 $filters[$i]['where'].','.$filters[$i]['what'].','.$new_folder);
943 }
944 elseif ($action == 'delete') {
945 remove_filter($p);
946 $p = $p-1;
947 }
948 }
949 $p++;
950 }
951 }
952 }
953
954 /**
955 * Display formated error message
956 * @param string $string text message
957 * @return string html formated text message
958 * @access private
959 */
960 function do_error($string) {
961 global $color;
962 echo "<p align=\"center\"><font color=\"$color[2]\">";
963 echo $string;
964 echo "</font></p>\n";
965 }