9d658a5c01d0577b6acc1e53201c1396aa306478
[squirrelmail.git] / plugins / filters / options.php
1 <?php
2 /**
3 ** Message and Spam Filter Plugin
4 **
5 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
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
14 ** running.
15 **
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.
21 **
22 ** Also view plugins/README.plugins for more information.
23 **
24 **/
25
26 chdir ('..');
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');
31
32 global $AllowSpamFilters;
33
34 displayPageHeader($color, 'None');
35
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("\"", "&quot;", $filter_what);
42
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'],
54 $$input);
55 }
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);
62 }
63
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');
67 }
68 $filters = load_filters();
69
70 echo '<br>' .
71 '<table width=95% align=center border=0 cellpadding=2 cellspacing=0>'.
72 "<tr><td bgcolor=\"$color[0]\">".
73 '<center><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></center>'.
74 '</td></tr></table>'.
75 '<br><center>[<a href="options.php?action=add">' . _("New") .
76 '</a>] - [<a href="../../src/options.php">' . _("Done") . '</a>]</center><br>' .
77 '<table border=0 cellpadding=3 cellspacing=0 align=center>';
78
79 for ($i=0; $i < count($filters); $i++) {
80
81 $clr = (($i % 2)?$color[0]:$color[9]);
82 $fdr = ($folder_prefix)?str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
83 echo "<tr bgcolor=\"$clr\"><td><small>".
84 "[<a href=\"options.php?theid=$i&action=edit\">" . _("Edit") . '</a>]'.
85 '</small></td><td><small>'.
86 "[<a href=\"options.php?theid=$i&action=delete\">" . _("Delete") . '</a>]'.
87 '</small></td><td align=center><small>[';
88
89 if (isset($filters[$i + 1])) {
90 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
91 if ($i > 0) {
92 echo ' | ';
93 }
94 }
95 if ($i > 0) {
96 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
97 }
98 echo ']</small></td><td> - ';
99 printf( _("If <b>%s</b> contains <b>%s</b> then move to <b>%s</b>"), _($filters[$i]['where']), $filters[$i]['what'], $fdr );
100 echo '</td></tr>';
101
102 }
103 echo '</table>'.
104 '<table width=80% align=center border=0 cellpadding=2 cellspacing=0">'.
105 '<tr><td>&nbsp</td></tr>'.
106 '</table>';
107
108
109 if ($AllowSpamFilters) {
110
111 echo "<table width=95% align=center border=0 cellpadding=2 cellspacing=0 bgcolor=\"$color[0]\">".
112 '<tr><th align=center>' . _("Spam Filtering") . '</th></tr>'.
113 '</table>';
114 if (! isset($action) || $action != 'spam') {
115
116 echo '<p align=center>[<a href="options.php?action=spam">' . _("Edit") . '</a>]<br>';
117 printf( _("Spam is sent to <b>%s</b>"), ($filters_spam_folder?$filters_spam_folder:_("[<i>not set yet</i>]") ) );
118 echo '<br>';
119 printf( _("Spam scan is limited to <b>%s</b>"), (($filters_spam_scan == 'new')?_("New Messages Only"):_("All Messages") ) );
120 echo '</p>'.
121 "<table border=0 cellpadding=3 cellspacing=0 align=center bgcolor=\"$color[0]\">";
122
123 $spam_filters = load_spam_filters();
124
125 foreach ($spam_filters as $Key => $Value) {
126 echo '<tr><th align=center>';
127
128 if ($spam_filters[$Key]['enabled']) {
129 echo _("ON");
130 } else {
131 echo _("OFF");
132 }
133
134 echo '</th><td>&nbsp;-&nbsp;</td><td>';
135
136 if ($spam_filters[$Key]['link']) {
137 echo '<a href="' .
138 $spam_filters[$Key]['link'] .
139 '" target="_blank">';
140 }
141
142 echo $spam_filters[$Key]['name'];
143 if ($spam_filters[$Key]['link']) {
144 echo '</a>';
145 }
146 echo "</td></tr>\n";
147 }
148 echo '</table>';
149 }
150 }
151
152 if (isset($action) && ($action == 'add' || $action == 'edit')) {
153 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
154 $boxes = sqimap_mailbox_list($imapConnection);
155 sqimap_logout($imapConnection);
156 if ( !isset($theid) ) {
157 $theid = count($filters);
158 }
159 echo '<center>'.
160 '<form action="options.php" method=post>'.
161 '<br><table cellpadding=2 cellspacing=0 border=0>'.
162 '<tr>'.
163 '<td>&nbsp;</td>'.
164 '<td>'.
165 '<select name=filter_where>';
166
167 $L = isset($filters[$theid]['where']);
168
169 $sel = (($L && $filters[$theid]['where'] == 'From')?'selected':'');
170 echo "<option value=\"From\" $sel>" . _ ("From") . '</option>';
171
172 $sel = (($L && $filters[$theid]['where'] == 'To')?'selected':'');
173 echo "<option value=\"To\" $sel>" . _ ("To") . '</option>';
174
175 $sel = (($L && $filters[$theid]['where'] == 'Cc')?'selected':'');
176 echo "<option value=\"Cc\" $sel>" . _ ("Cc") . '</option>';
177
178 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?'selected':'');
179 echo "<option value=\"To or Cc\" $sel>" . _ ("To or Cc") . '</option>';
180
181 $sel = (($L && $filters[$theid]['where'] == 'Subject')?'selected':'');
182 echo "<option value=\"Subject\" $sel>" . _ ("Subject") . '</option>';
183
184 echo '</select>'.
185 '</td>'.
186 '</tr>'.
187 '<tr>'.
188 '<td align=right>'.
189 _("Contains:").
190 '</td>'.
191 '<td>'.
192 '<input type=text size=32 name=filter_what value="';
193 if (isset($filters[$theid]['what'])) {
194 echo $filters[$theid]["what"];
195 }
196 echo '">'.
197 '</td>'.
198 '</tr>'.
199 '<tr>'.
200 '<td>'.
201 _("Move to:").
202 '</td>'.
203 '<td>'.
204 '<tt>'.
205 '<select name=filter_folder>';
206
207 for ($i = 0; $i < count($boxes); $i++) {
208 if (! in_array('noselect', $boxes[$i]['flags'])) {
209 $box = $boxes[$i]['unformatted'];
210 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['formatted']);
211 if (isset($filters[$theid]['folder']) &&
212 $filters[$theid]['folder'] == $box)
213 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</option>";
214 else
215 echo "<OPTION VALUE=\"$box\">$box2</option>";
216 }
217 }
218 echo '</tt>'.
219 '</select>'.
220 '</td>'.
221 '</tr>'.
222 '</table>'.
223 '<input type=submit name=filter_submit value=' . _("Submit") . '>'.
224 "<input type=hidden name=theid value=$theid>".
225 '</form>'.
226 '</center>';
227
228 } else if (isset($action) && $action == 'spam' && $AllowSpamFilters) {
229 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
230 $boxes = sqimap_mailbox_list($imapConnection);
231 sqimap_logout($imapConnection);
232 for ($i = 0; $i < count($boxes) && $filters_spam_folder == ''; $i++) {
233 if ($boxes[$i]['flags'][0] != 'noselect' &&
234 $boxes[$i]['flags'][1] != 'noselect' &&
235 $boxes[$i]['flags'][2] != 'noselect') {
236 $filters_spam_folder = $boxes[$i]['unformatted'];
237 }
238 }
239
240 echo '<form method=post action="options.php">'.
241 '<center>'.
242 '<table width=85% cellpadding=2 cellspacing=0 border=0>'.
243 '<tr>'.
244 '<th align=right nowrap>' . _("Move spam to:") . '</th>'.
245 '<td><select name="filters_spam_folder_set">';
246
247 for ($i = 0; $i < count($boxes); $i++) {
248 if (! in_array('noselect', $boxes[$i]['flags'])) {
249 $box = $boxes[$i]['unformatted'];
250 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['formatted']);
251 if ($filters_spam_folder == $box)
252 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
253 else
254 echo "<OPTION VALUE=\"$box\">$box2</OPTION>\n";
255 }
256 }
257 echo '</select>'.
258 '</td>'.
259 '</tr>'.
260 '<tr><td></td><td>' .
261 _("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.") .
262 '</td></tr>'.
263 '<tr>'.
264 '<th align=right nowrap>' . _("What to Scan:") . '</th>'.
265 '<td><select name="filters_spam_scan_set">'.
266 '<option value=""';
267 if ($filters_spam_scan == '')
268 echo ' SELECTED';
269 echo '>' . _("All messages") . '</option>'.
270 '<option value="new"';
271 if ($filters_spam_scan == 'new')
272 echo ' SELECTED';
273 echo '>' . _("Only unread messages") . '</option>' .
274 '</select>'.
275 '</td>'.
276 '</tr>'.
277 '<tr>'.
278 '<td></td><td>'.
279 _("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.").
280 '</td></tr>';
281
282 $spam_filters = load_spam_filters();
283
284 foreach ($spam_filters as $Key => $Value) {
285 echo "<tr><th align=right nowrap>$Key</th>\n" .
286 '<td><input type=checkbox name="' .
287 $spam_filters[$Key]['prefname'] .
288 '_set"';
289 if ($spam_filters[$Key]['enabled'])
290 echo ' CHECKED';
291 echo '> - ';
292 if ($spam_filters[$Key]['link']) {
293 echo '<a href="' .
294 $spam_filters[$Key]['link'] .
295 '" target="_blank">';
296 }
297 echo $spam_filters[$Key]['name'];
298 if ($spam_filters[$Key]['link']) {
299 echo '</a>';
300 }
301 echo '</td></tr><tr><td></td><td>' .
302 $spam_filters[$Key]['comment'] .
303 "</td></tr>\n";
304 }
305 echo '<tr><td colspan=2 align=center><input type=submit name="spam_submit" value="' . _("Save") . '"></td></tr>'.
306 '</table>'.
307 '</center>'.
308 '</form>';
309
310 sqimap_logout($imapConnection);
311 }
312 ?>