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