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