3 ** Message and Spam Filter Plugin
5 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 ** This plugin filters your inbox into different folders based upon given
9 ** criteria. It is most useful for people who are subscibed to mailing lists
10 ** to help organize their messages. The argument stands that filtering is
11 ** not the place of the client, which is why this has been made a plugin for
12 ** SquirrelMail. You may be better off using products such as Sieve or
13 ** Procmail to do your filtering so it happens even when SquirrelMail isn't
16 ** If you need help with this, or see improvements that can be made, please
17 ** email me directly at the address above. I definately welcome suggestions
18 ** and comments. This plugin, as is the case with all SquirrelMail plugins,
19 ** is not directly supported by the developers. Please come to me off the
20 ** mailing list if you have trouble with it.
22 ** Also view plugins/README.plugins for more information.
27 require_once('../src/validate.php');
28 require_once('../functions/page_header.php');
29 require_once('../functions/imap.php');
30 require_once('../src/load_prefs.php');
32 global $AllowSpamFilters;
34 displayPageHeader($color, 'None');
36 if (isset($filter_submit)) {
37 if (!isset($theid)) $theid = 0;
38 $filter_what = ereg_replace(",", " ", $filter_what);
39 $filter_what = str_replace("\\\\", "\\", $filter_what);
40 $filter_what = str_replace("\\\"", "\"", $filter_what);
41 $filter_what = str_replace("\"", """, $filter_what);
43 setPref($data_dir, $username, "filter".$theid, $filter_where.",".$filter_what.",".$filter_folder);
44 $filters[$theid]["where"] = $filter_where;
45 $filters[$theid]["what"] = $filter_what;
46 $filters[$theid]["folder"] = $filter_folder;
47 } elseif (isset($spam_submit) && $AllowSpamFilters) {
48 $spam_filters = load_spam_filters();
49 setPref($data_dir, $username, 'filters_spam_folder', $filters_spam_folder_set);
50 setPref($data_dir, $username, 'filters_spam_scan', $filters_spam_scan_set);
51 foreach ($spam_filters as $Key => $Value) {
52 $input = $spam_filters[$Key]['prefname'] . '_set';
53 setPref($data_dir, $username, $spam_filters[$Key]['prefname'],
56 } elseif (isset($action) && $action == 'delete') {
57 remove_filter($theid);
58 } elseif (isset($action) && $action == 'move_up') {
59 filter_swap($theid, $theid - 1);
60 } elseif (isset($action) && $action == 'move_down') {
61 filter_swap($theid, $theid +
1);
64 if ($AllowSpamFilters) {
65 $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder');
66 $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan');
68 $filters = load_filters();
70 if (isset($action) && ($action == 'add' ||
$action == 'edit')) {
71 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
72 $boxes = sqimap_mailbox_list($imapConnection);
73 sqimap_logout($imapConnection);
74 if ( !isset($theid) ) {
75 $theid = count($filters);
78 '<form action="options.php" method=post>'.
79 '<br><table cellpadding=2 cellspacing=0 border=0>'.
83 '<select name=filter_where>';
85 $L = isset($filters[$theid]['where']);
87 $sel = (($L && $filters[$theid]['where'] == 'From')?
'selected':'');
88 echo "<option value=\"From\" $sel>" . _ ("From") . '</option>';
90 $sel = (($L && $filters[$theid]['where'] == 'To')?
'selected':'');
91 echo "<option value=\"To\" $sel>" . _ ("To") . '</option>';
93 $sel = (($L && $filters[$theid]['where'] == 'Cc')?
'selected':'');
94 echo "<option value=\"Cc\" $sel>" . _ ("Cc") . '</option>';
96 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?
'selected':'');
97 echo "<option value=\"To or Cc\" $sel>" . _ ("To or Cc") . '</option>';
99 $sel = (($L && $filters[$theid]['where'] == 'Subject')?
'selected':'');
100 echo "<option value=\"Subject\" $sel>" . _ ("Subject") . '</option>';
110 '<input type=text size=32 name=filter_what value="';
111 if (isset($filters[$theid]['what'])) {
112 echo $filters[$theid]["what"];
123 '<select name=filter_folder>';
125 for ($i = 0; $i < count($boxes); $i++
) {
126 if (! in_array('noselect', $boxes[$i]['flags'])) {
127 $box = $boxes[$i]['unformatted'];
128 $box2 = str_replace(' ', ' ', $boxes[$i]['formatted']);
129 if (isset($filters[$theid]['folder']) &&
130 $filters[$theid]['folder'] == $box)
131 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</option>";
133 echo "<OPTION VALUE=\"$box\">$box2</option>";
141 '<input type=submit name=filter_submit value=' . _("Submit") . '>'.
142 "<input type=hidden name=theid value=$theid>".
146 } else if (isset($action) && $action == 'spam' && $AllowSpamFilters) {
147 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
148 $boxes = sqimap_mailbox_list($imapConnection);
149 sqimap_logout($imapConnection);
150 for ($i = 0; $i < count($boxes) && $filters_spam_folder == ''; $i++
) {
151 if ($boxes[$i]['flags'][0] != 'noselect' &&
152 $boxes[$i]['flags'][1] != 'noselect' &&
153 $boxes[$i]['flags'][2] != 'noselect') {
154 $filters_spam_folder = $boxes[$i]['unformatted'];
158 echo '<form method=post action="options.php">'.
160 '<table width=85% cellpadding=2 cellspacing=0 border=0>'.
162 '<th align=right nowrap>' . _("Move spam to:") . '</th>'.
163 '<td><select name="filters_spam_folder_set">';
165 for ($i = 0; $i < count($boxes); $i++
) {
166 if (! in_array('noselect', $boxes[$i]['flags'])) {
167 $box = $boxes[$i]['unformatted'];
168 $box2 = str_replace(' ', ' ', $boxes[$i]['formatted']);
169 if ($filters_spam_folder == $box)
170 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
172 echo "<OPTION VALUE=\"$box\">$box2</OPTION>\n";
178 '<tr><td></td><td>' .
179 _("Moving spam directly to the trash may not be a good idea at first, since messages from friends and mailing lists might accidentally be marked as spam. Whatever folder you set this to, make sure that it gets cleaned out periodically, so that you don't have an excessively large mailbox hanging around.") .
182 '<th align=right nowrap>' . _("What to Scan:") . '</th>'.
183 '<td><select name="filters_spam_scan_set">'.
185 if ($filters_spam_scan == '')
187 echo '>' . _("All messages") . '</option>'.
188 '<option value="new"';
189 if ($filters_spam_scan == 'new')
191 echo '>' . _("Only unread messages") . '</option>' .
197 _("The more messages you scan, the longer it takes. I would suggest that you scan only new messages. If you make a change to your filters, I would set it to scan all messages, then go view my INBOX, then come back and set it to scan only new messages. That way, your new spam filters will be applied and you'll scan even the spam you read with the new filters.").
200 $spam_filters = load_spam_filters();
202 foreach ($spam_filters as $Key => $Value) {
203 echo "<tr><th align=right nowrap>$Key</th>\n" .
204 '<td><input type=checkbox name="' .
205 $spam_filters[$Key]['prefname'] .
207 if ($spam_filters[$Key]['enabled'])
210 if ($spam_filters[$Key]['link']) {
212 $spam_filters[$Key]['link'] .
213 '" target="_blank">';
215 echo $spam_filters[$Key]['name'];
216 if ($spam_filters[$Key]['link']) {
219 echo '</td></tr><tr><td></td><td>' .
220 $spam_filters[$Key]['comment'] .
223 echo '<tr><td colspan=2 align=center><input type=submit name="spam_submit" value="' . _("Save") . '"></td></tr>'.
228 sqimap_logout($imapConnection);
232 '<table width=95% align=center border=0 cellpadding=2 cellspacing=0>'.
233 "<tr><td bgcolor=\"$color[0]\">".
234 '<center><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></center>'.
235 '</td></tr></table>'.
236 '<br><center>[<a href="options.php?action=add">' . _("New") .
237 '</a>] - [<a href="../../src/options.php">' . _("Done") . '</a>]</center><br>' .
238 '<table border=0 cellpadding=3 cellspacing=0 align=center>';
240 for ($i=0; $i < count($filters); $i++
) {
242 $clr = (($i %
2)?
$color[0]:$color[9]);
243 $fdr = ($folder_prefix)?
str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
244 echo "<tr bgcolor=\"$clr\"><td><small>".
245 "[<a href=\"options.php?theid=$i&action=edit\">" . _("Edit") . '</a>]'.
246 '</small></td><td><small>'.
247 "[<a href=\"options.php?theid=$i&action=delete\">" . _("Delete") . '</a>]'.
248 '</small></td><td align=center><small>[';
250 if (isset($filters[$i +
1])) {
251 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
257 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
259 echo ']</small></td><td> - ';
260 printf( _("If <b>%s</b> contains <b>%s</b> then move to <b>%s</b>"), _($filters[$i]['where']), $filters[$i]['what'], $fdr );
265 '<table width=80% align=center border=0 cellpadding=2 cellspacing=0">'.
266 '<tr><td> </td></tr>'.
270 if ($AllowSpamFilters) {
272 echo "<table width=95% align=center border=0 cellpadding=2 cellspacing=0 bgcolor=\"$color[0]\">".
273 '<tr><th align=center>' . _("Spam Filtering") . '</th></tr>'.
275 if (! isset($action) ||
$action != 'spam') {
277 echo '<p align=center>[<a href="options.php?action=spam">' . _("Edit") . '</a>]<br>';
278 printf( _("Spam is sent to <b>%s</b>"), ($filters_spam_folder?
$filters_spam_folder:_("[<i>not set yet</i>]") ) );
280 printf( _("Spam scan is limited to <b>%s</b>"), (($filters_spam_scan == 'new')?
_("New Messages Only"):_("All Messages") ) );
282 "<table border=0 cellpadding=3 cellspacing=0 align=center bgcolor=\"$color[0]\">";
284 $spam_filters = load_spam_filters();
286 foreach ($spam_filters as $Key => $Value) {
287 echo '<tr><th align=center>';
289 if ($spam_filters[$Key]['enabled']) {
295 echo '</th><td> - </td><td>';
297 if ($spam_filters[$Key]['link']) {
299 $spam_filters[$Key]['link'] .
300 '" target="_blank">';
303 echo $spam_filters[$Key]['name'];
304 if ($spam_filters[$Key]['link']) {