Happy 2014
[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 *
701e7bee 6 * @copyright 1999-2014 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
202bcbcc 13/**
14 * do not allow to call this file directly
15 */
f627180c 16if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) {
202bcbcc 17 header("Location: ../../src/login.php");
18 die();
19}
e5c8ec61 20
21/** load globals */
48879ef0 22global $UseSeparateImapConnection,
23 $AllowSpamFilters, $SpamFilters_YourHop, $SpamFilters_ShowCommercial,
24 $SpamFilters_DNScache, $SpamFilters_BulkQuery, $SpamFilters_SharedCache,
e5c8ec61 25 $SpamFilters_CacheTTL;
26
3bde2539 27/**
ee951d3a 28 * load required functions. Plugin depends on IMAP functions and they are not
3bde2539 29 * loaded in src/webmail.php
30 */
31include_once (SM_PATH . 'functions/imap.php');
32
e5c8ec61 33/** load default config */
34if (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
48if (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')) {
8510adf2 51 include_once (SM_PATH . 'plugins/filters/config.php');
e5c8ec61 52}
9c655416 53
ea5f4b8e 54/**
9c655416 55 * Register option blocks
56 * @access private
57 */
58function filters_optpage_register_block() {
59 global $optpage_blocks, $AllowSpamFilters;
f8a1ed5a 60
9c655416 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
48879ef0 78/* Receive the status of the folder and do something with it */
79function filters_folder_status($statusarr) {
80
b2f4cdad 81 global $filter_inbox_count;
82 if (empty($filter_inbox_count)) $filter_inbox_count=0;
202bcbcc 83
b2f4cdad 84 if ($statusarr['MAILBOX'] == 'INBOX')
48879ef0 85 {
86 if (!empty($statusarr['MESSAGES'])) $filter_inbox_count=$statusarr['MESSAGES'];
87 }
88}
89
9c655416 90/**
91 * Saves the DNS Cache to disk
0a1dc88e 92 * @access private
5b4ba967 93 */
a2a566eb 94function filters_SaveCache () {
95 global $data_dir, $SpamFilters_DNScache;
96
ae48f757 97 if (file_exists($data_dir . '/dnscache')) {
98 $fp = fopen($data_dir . '/dnscache', 'r');
a2a566eb 99 } else {
100 $fp = false;
101 }
102 if ($fp) {
103 flock($fp,LOCK_EX);
104 } else {
ae48f757 105 $fp = fopen($data_dir . '/dnscache', 'w+');
a2a566eb 106 fclose($fp);
ae48f757 107 $fp = fopen($data_dir . '/dnscache', 'r');
a2a566eb 108 flock($fp,LOCK_EX);
109 }
9c655416 110 $fp1 = fopen($data_dir . '/dnscache', 'w+');
a2a566eb 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
0a1dc88e 121/**
9c655416 122 * Loads the DNS Cache from disk
0a1dc88e 123 * @access private
124 */
a2a566eb 125function filters_LoadCache () {
126 global $data_dir, $SpamFilters_DNScache;
127
ae48f757 128 if (file_exists($data_dir . '/dnscache')) {
6ade76e0 129 $SpamFilters_DNScache = array();
ae48f757 130 if ($fp = fopen ($data_dir . '/dnscache', 'r')) {
a2a566eb 131 flock($fp,LOCK_SH);
9c655416 132 while ($data = fgetcsv($fp,1024)) {
a2a566eb 133 if ($data[2] > time()) {
134 $SpamFilters_DNScache[$data[0]]['L'] = $data[1];
135 $SpamFilters_DNScache[$data[0]]['T'] = $data[2];
136 }
137 }
a2a566eb 138 flock($fp,LOCK_UN);
139 }
140 }
141}
142
0a1dc88e 143/**
9c655416 144 * Uses the BulkQuery executable to query all the RBLs at once
41ffc892 145 * @param array $filters Array of SPAM Filters
9c655416 146 * @param array $IPs Array of IP Addresses
0a1dc88e 147 * @access private
148 */
fb577a4d 149function filters_bulkquery($filters, $IPs) {
ce68b76b 150 global $attachment_dir, $username,
6ade76e0 151 $SpamFilters_DNScache, $SpamFilters_BulkQuery,
152 $SpamFilters_CacheTTL;
a2a566eb 153
a2a566eb 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
ae48f757 164 $bqfil = $attachment_dir . $username . '-bq.in';
165 $fp = fopen($bqfil, 'w');
6ade76e0 166 fputs ($fp, $SpamFilters_CacheTTL . "\n");
a2a566eb 167 foreach ($rbls as $key => $value) {
ae48f757 168 fputs ($fp, '.' . $key . "\n");
a2a566eb 169 }
170 fputs ($fp, "----------\n");
171 foreach ($IPs as $key => $value) {
172 fputs ($fp, $key . "\n");
173 }
174 fclose ($fp);
175 $bqout = array();
ae48f757 176 exec ($SpamFilters_BulkQuery . ' < ' . $bqfil, $bqout);
a2a566eb 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
0a1dc88e 186/**
9c655416 187 * Starts the filtering process
b2f4cdad 188 * @param array $hook_args (since 1.5.2) do hook arguments. Is used to check
189 * hook name, array key = 0.
0a1dc88e 190 * @access private
191 */
b2f4cdad 192function start_filters($hook_args) {
ce68b76b 193 global $imapServerAddress, $imapPort, $imap_stream, $imapConnection,
2128bbc6 194 $UseSeparateImapConnection, $AllowSpamFilters, $filter_inbox_count,
195 $username;
10a26cea 196
b2f4cdad 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
9a43a06b 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;
fb0bcfdb 213 foreach($spamfilters as $value) {
ee951d3a 214 if ($value['enabled'] == SMPREF_ON) {
9a43a06b 215 $AllowSpamFilters = true;
216 break;
217 }
218 }
219 }
220
41ffc892 221 // No user filters, and no spam filters, no need to continue //
9a43a06b 222 if (!$AllowSpamFilters && empty($filters)) {
223 return;
224 }
225
226
2714d4ff 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 ) {
2128bbc6 231 $stream = sqimap_login($username, false, $imapServerAddress,
1320cab5 232 $imapPort, 10);
233 $previously_connected = false;
2714d4ff 234 } else if (isset($imapConnection)) {
235 $stream = $imapConnection;
236 $previously_connected = true;
237 } else {
238 $previously_connected = true;
239 $stream = $imap_stream;
240 }
4eee5968 241
48879ef0 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) {
2714d4ff 252 sqimap_mailbox_select($stream, 'INBOX');
253 // Filter spam from inbox before we sort them into folders
254 if ($AllowSpamFilters) {
255 spam_filters($stream);
10a26cea 256 }
4eee5968 257
2714d4ff 258 // Sort into folders
259 user_filters($stream);
260 }
261
262 if (!$previously_connected) {
263 sqimap_logout($stream);
264 }
10a26cea 265}
266
0a1dc88e 267/**
9c655416 268 * Does the loop through each filter
269 * @param stream imap_stream the stream to read from
0a1dc88e 270 * @access private
271 */
10a26cea 272function user_filters($imap_stream) {
c8a2c24d 273 global $data_dir, $username;
10a26cea 274 $filters = load_filters();
275 if (! $filters) return;
c8a2c24d 276 $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
10a26cea 277
fb577a4d 278 $expunge = false;
53fcc494 279 // For every rule
ae48f757 280 for ($i=0, $num = count($filters); $i < $num; $i++) {
10a26cea 281 // If it is the "combo" rule
282 if ($filters[$i]['where'] == 'To or Cc') {
53fcc494 283 /*
284 * If it's "TO OR CC", we have to do two searches, one for TO
285 * and the other for CC.
286 */
fb577a4d 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);
8a7ccd82 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',
f8a1ed5a 296 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
10a26cea 297 } else {
53fcc494 298 /*
299 * If it's a normal TO, CC, SUBJECT, or FROM, then handle it
300 * normally.
301 */
fb577a4d 302 $expunge = filter_search_and_delete($imap_stream, $filters[$i]['where'],
303 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
4eee5968 304 }
4eee5968 305 }
53fcc494 306 // Clean out the mailbox whether or not auto_expunge is on
307 // That way it looks like it was redirected properly
fb577a4d 308 if ($expunge) {
4f6915b0 309 sqimap_mailbox_expunge($imap_stream, 'INBOX');
53fcc494 310 }
10a26cea 311}
312
0a1dc88e 313/**
9c655416 314 * Creates and runs the IMAP command to filter messages
41ffc892 315 * @param string $imap_stream TODO: Document this parameter
9c655416 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
f8a1ed5a 320 * @param string $should_expunge
0a1dc88e 321 * @access private
322 */
2714d4ff 323function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_scan,
fb577a4d 324 $should_expunge) {
6201339c 325 global $languages, $squirrelmail_language, $allow_charset_search, $imap_server_type;
326
1320cab5 327 //TODO: make use of new mailbox cache. See mailbox_display.phpinfo
328
174523e3 329 if (strtolower($where_to) == 'inbox') {
330 return array();
331 }
332
c8a2c24d 333 if ($user_scan == 'new') {
334 $category = 'UNSEEN';
335 } else {
336 $category = 'ALL';
337 }
86e6d79f 338 $category .= ' UNDELETED';
c8a2c24d 339
3eea00ca 340 if ($allow_charset_search &&
341 isset($languages[$squirrelmail_language]['CHARSET']) &&
53fcc494 342 $languages[$squirrelmail_language]['CHARSET']) {
3eea00ca 343 $search_str = 'SEARCH CHARSET '
344 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
345 . ' ' . $category;
f9ca4be0 346 } else {
3eea00ca 347 $search_str = 'SEARCH CHARSET US-ASCII ' . $category;
f9ca4be0 348 }
ae48f757 349 if ($where == 'Header') {
3eea00ca 350 $what = explode(':', $what);
1320cab5 351 $where = strtoupper($where);
3eea00ca 352 $where = trim($where . ' ' . $what[0]);
353 $what = addslashes(trim($what[1]));
9da6bdde 354 }
f18ea37e 355
cf2aa192 356 // see comments in squirrelmail sqimap_search function
357 if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') {
41ffc892 358 $search_str .= ' ' . $where . ' ' . $what;
1320cab5 359 /* read data back from IMAP */
360 $read = sqimap_run_command($imap_stream, $search_str, true, $response, $message, TRUE);
f18ea37e 361 } else {
1320cab5 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>";
f18ea37e 379 }
10a26cea 380
2714d4ff 381 if (isset($read[0])) {
382 $ids = array();
9c655416 383 for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
2714d4ff 384 if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
a895042a 385 $ids += explode(' ', trim($regs[1]));
fb577a4d 386 }
2714d4ff 387 }
388 if ($response == 'OK' && count($ids)) {
389 if (sqimap_mailbox_exists($imap_stream, $where_to)) {
390 $should_expunge = true;
4e6e5d2d 391 sqimap_msgs_list_move ($imap_stream, $ids, $where_to, false);
4eee5968 392 }
1320cab5 393 } elseif ($response != 'OK') {
9cda618e 394 $query = $search_str . "\r\n".$what ."\r\n";
1320cab5 395 if ($response == 'NO') {
1320cab5 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 }
4eee5968 405 }
406 }
fb577a4d 407 return $should_expunge;
10a26cea 408}
4eee5968 409
0a1dc88e 410/**
9c655416 411 * Loops through all the Received Headers to find IP Addresses
412 * @param stream imap_stream the stream to read from
0a1dc88e 413 * @access private
414 */
10a26cea 415function spam_filters($imap_stream) {
6201339c 416 global $data_dir, $username;
10a26cea 417 global $SpamFilters_YourHop;
418 global $SpamFilters_DNScache;
a2a566eb 419 global $SpamFilters_SharedCache;
420 global $SpamFilters_BulkQuery;
fb577a4d 421 global $SpamFilters_CacheTTL;
4eee5968 422
10a26cea 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();
4eee5968 426
a2a566eb 427 if ($SpamFilters_SharedCache) {
428 filters_LoadCache();
429 }
430
fb577a4d 431 $run = false;
4eee5968 432
fb0bcfdb 433 foreach ($filters as $Value) {
10a26cea 434 if ($Value['enabled']) {
fb577a4d 435 $run = true;
436 break;
4eee5968 437 }
10a26cea 438 }
4eee5968 439
10a26cea 440 // short-circuit
fb577a4d 441 if (!$run) {
10a26cea 442 return;
443 }
4eee5968 444
10a26cea 445 // Ask for a big list of all "Received" headers in the inbox with
446 // flags for each message. Kinda big.
2714d4ff 447
448 if ($filters_spam_scan == 'new') {
449 $search_array = array();
6201339c 450 $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, TRUE);
2714d4ff 451 if (isset($read[0])) {
9c655416 452 for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
2714d4ff 453 if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
a895042a 454 $search_array = explode(' ', trim($regs[1]));
41ffc892 455 break;
6174dcc7 456 }
c8a2c24d 457 }
2714d4ff 458 }
c8a2c24d 459 }
2714d4ff 460 if ($filters_spam_scan == 'new' && count($search_array)) {
d3fae94f 461 $headers = sqimap_get_small_header_list ($imap_stream, $search_array, array('Received'),array());
2714d4ff 462 } else if ($filters_spam_scan != 'new') {
d3fae94f 463 $headers = sqimap_get_small_header_list ($imap_stream, null , array('Received'),array());
2714d4ff 464 } else {
465 return;
466 }
467 if (!count($headers)) {
10a26cea 468 return;
469 }
fb577a4d 470 $bulkquery = (strlen($SpamFilters_BulkQuery) > 0 ? true : false);
471 $IPs = array();
472 $aSpamIds = array();
473 foreach ($headers as $id => $aValue) {
2714d4ff 474 if (isset($aValue['UID'])) {
fb577a4d 475 $MsgNum = $aValue['UID'];
476 } else {
477 $MsgNum = $id;
10a26cea 478 }
10a26cea 479 // Look through all of the Received headers for IP addresses
2714d4ff 480 if (isset($aValue['RECEIVED'])) {
481 foreach ($aValue['RECEIVED'] as $received) {
fb577a4d 482 // Check to see if this line is the right "Received from" line
483 // to check
2714d4ff 484
485 // $aValue['Received'] is an array with all the received lines.
fb577a4d 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
2714d4ff 488
fb577a4d 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 }
41ffc892 496
fb577a4d 497 if ($bulkquery) {
498 array_shift($aMatch);
41ffc892 499 $IP = explode('.', $aMatch);
fb577a4d 500 foreach ($filters as $key => $value) {
501 if ($filters[$key]['enabled'] && $filters[$key]['dns']) {
502 if (strlen($SpamFilters_DNScache[$IP.'.'.$filters[$key]['dns']]) == 0) {
41ffc892 503 $IPs[$IP] = true;
504 break;
fb577a4d 505 }
506 }
507 }
c8a2c24d 508 }
509 // If we've checked one IP and YourHop is
510 // just a space
fb577a4d 511 if ($SpamFilters_YourHop == ' ' || $isspam) {
c8a2c24d 512 break; // don't check any more
a9aa7ab7 513 }
a9aa7ab7 514 }
4eee5968 515 }
4eee5968 516 }
4eee5968 517 }
10a26cea 518 }
fb577a4d 519 // Lookie! It's spam! Yum!
520 if (count($aSpamIds) && sqimap_mailbox_exists($imap_stream, $filters_spam_folder)) {
41ffc892 521 sqimap_msgs_list_move($imap_stream, $aSpamIds, $filters_spam_folder);
fb577a4d 522 sqimap_mailbox_expunge($imap_stream, 'INBOX');
523 }
4eee5968 524
fb577a4d 525 if ($bulkquery && count($IPs)) {
526 filters_bulkquery($filters, $IPs);
527 }
4eee5968 528
a2a566eb 529 if ($SpamFilters_SharedCache) {
530 filters_SaveCache();
531 } else {
41100fce 532 sqsession_register($SpamFilters_DNScache, 'SpamFilters_DNScache');
a2a566eb 533 }
10a26cea 534}
4eee5968 535
0a1dc88e 536/**
0a1dc88e 537 * Does the loop through each enabled filter for the specified IP address.
538 * IP format: $a.$b.$c.$d
9c655416 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
0a1dc88e 545 * @access private
546 */
10a26cea 547function filters_spam_check_site($a, $b, $c, $d, &$filters) {
a2a566eb 548 global $SpamFilters_DNScache, $SpamFilters_CacheTTL;
10a26cea 549 foreach ($filters as $key => $value) {
550 if ($filters[$key]['enabled']) {
551 if ($filters[$key]['dns']) {
41ffc892 552
ee951d3a 553 /**
41ffc892 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
ee951d3a 557 */
10a26cea 558 $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' .
ee951d3a 559 $filters[$key]['dns'] . '.';
ae48f757 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
a7cd90dd 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 }
ee951d3a 573
574 /**
575 * gethostbyname returns ip if resolved, or returns original
41ffc892 576 * host supplied to function if there is no resolution
ee951d3a 577 */
578 if ($SpamFilters_DNScache[$filter_revip]['L'] != $filter_revip) {
a7cd90dd 579 return 1;
4eee5968 580 }
581 }
582 }
4eee5968 583 }
10a26cea 584 return 0;
585}
586
0a1dc88e 587/**
9c655416 588 * Loads the filters from the user preferences
589 * @return array All the user filters
0a1dc88e 590 * @access private
591 */
10a26cea 592function load_filters() {
593 global $data_dir, $username;
c8a2c24d 594
10a26cea 595 $filters = array();
9c655416 596 for ($i = 0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
10a26cea 597 $ary = explode(',', $fltr);
598 $filters[$i]['where'] = $ary[0];
eceb3fe5 599 $filters[$i]['what'] = str_replace('###COMMA###', ',', $ary[1]);
10a26cea 600 $filters[$i]['folder'] = $ary[2];
4eee5968 601 }
10a26cea 602 return $filters;
603}
604
0a1dc88e 605/**
9c655416 606 * Loads the Spam Filters and checks the preferences for the enabled status
607 * @return array All the spam filters
0a1dc88e 608 * @access private
609 */
10a26cea 610function load_spam_filters() {
51199e7a 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'] =
99aaff8b 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.");
51199e7a 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'] =
99aaff8b 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.");
51199e7a 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 }
10a26cea 662
10a26cea 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';
ee951d3a 731 $filters['SPAMhaus']['result'] = '127.0.0.2';
10a26cea 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'] =
ace4e0d3 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).");
10a26cea 742
46a184ff 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
bd35ab2b 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.");
46a184ff 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
10a26cea 847 foreach ($filters as $Key => $Value) {
ee951d3a 848 $filters[$Key]['enabled'] = (bool)getPref($data_dir, $username, $filters[$Key]['prefname']);
4eee5968 849 }
850
10a26cea 851 return $filters;
852}
4eee5968 853
0a1dc88e 854/**
9c655416 855 * Removes a User filter
856 * @param int $id ID of the filter to remove
0a1dc88e 857 * @access private
858 */
10a26cea 859function remove_filter ($id) {
860 global $data_dir, $username;
4eee5968 861
9c655416 862 while ($nextFilter = getPref($data_dir, $username, 'filter' . ($id + 1))) {
10a26cea 863 setPref($data_dir, $username, 'filter' . $id, $nextFilter);
864 $id ++;
4eee5968 865 }
866
10a26cea 867 removePref($data_dir, $username, 'filter' . $id);
868}
4eee5968 869
0a1dc88e 870/**
9c655416 871 * Swaps two filters
872 * @param int $id1 ID of first filter to swap
f8a1ed5a 873 * @param int $id2 ID of second filter to swap
0a1dc88e 874 * @access private
875 */
10a26cea 876function filter_swap($id1, $id2) {
877 global $data_dir, $username;
4eee5968 878
10a26cea 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);
4eee5968 885 }
10a26cea 886}
4eec80a7 887
0a1dc88e 888/**
9c655416 889 * This updates the filter rules when renaming or deleting folders
0a1dc88e 890 * @param array $args
b2d8bc6c 891 * @access private
0a1dc88e 892 */
f5ab1fb9 893function update_for_folder ($args) {
894 $old_folder = $args[0];
9c655416 895 $new_folder = $args[2];
896 $action = $args[1];
ce68b76b 897 global $data_dir, $username;
4eec80a7 898 $filters = array();
899 $filters = load_filters();
900 $filter_count = count($filters);
901 $p = 0;
9c655416 902 for ($i = 0; $i < $filter_count; $i++) {
4eec80a7 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}
0a1dc88e 919
920/**
0a1dc88e 921 * Display formated error message
922 * @param string $string text message
923 * @return string html formated text message
924 * @access private
925 */
926function do_error($string) {
927 global $color;
928 echo "<p align=\"center\"><font color=\"$color[2]\">";
929 echo $string;
930 echo "</font></p>\n";
931}