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