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