(c) stuff
[squirrelmail.git] / plugins / filters / options.php
1 <?php
2 /*
3 * Message and Spam Filter Plugin
4 * Copyright (c) 1999-2001 The Squirrelmail Development Team
5 * Licensed under the GNU GPL. For full terms see the file COPYING.
6 *
7 * This plugin filters your inbox into different folders based upon given
8 * criteria. It is most useful for people who are subscibed to mailing lists
9 * to help organize their messages. The argument stands that filtering is
10 * not the place of the client, which is why this has been made a plugin for
11 * SquirrelMail. You may be better off using products such as Sieve or
12 * Procmail to do your filtering so it happens even when SquirrelMail isn't
13 * running.
14 *
15 * If you need help with this, or see improvements that can be made, please
16 * email me directly at the address above. I definately welcome suggestions
17 * and comments. This plugin, as is the case with all SquirrelMail plugins,
18 * is not directly supported by the developers. Please come to me off the
19 * mailing list if you have trouble with it.
20 *
21 * Also view plugins/README.plugins for more information.
22 *
23 * $Id$
24 *
25 */
26
27 chdir('..');
28 require_once('../src/validate.php');
29 require_once('../functions/page_header.php');
30 require_once('../functions/imap.php');
31 require_once('../src/load_prefs.php');
32
33 global $AllowSpamFilters;
34
35 displayPageHeader($color, 'None');
36
37 if (isset($filter_submit)) {
38 if (!isset($theid)) $theid = 0;
39 $filter_what = str_replace(',', ' ', $filter_what);
40 $filter_what = str_replace("\\\\", "\\", $filter_what);
41 $filter_what = str_replace("\\\"", "\"", $filter_what);
42 $filter_what = str_replace("\"", "&quot;", $filter_what);
43
44 setPref($data_dir, $username, 'filter'.$theid, $filter_where.','.$filter_what.','.$filter_folder);
45 $filters[$theid]['where'] = $filter_where;
46 $filters[$theid]['what'] = $filter_what;
47 $filters[$theid]['folder'] = $filter_folder;
48 } elseif (isset($spam_submit) && $AllowSpamFilters) {
49 $spam_filters = load_spam_filters();
50 setPref($data_dir, $username, 'filters_spam_folder', $filters_spam_folder_set);
51 setPref($data_dir, $username, 'filters_spam_scan', $filters_spam_scan_set);
52 foreach ($spam_filters as $Key => $Value)
53 {
54 $input = $spam_filters[$Key]['prefname'] . '_set';
55 setPref($data_dir, $username, $spam_filters[$Key]['prefname'],
56 $$input);
57 }
58 } elseif (isset($action) && $action == 'delete') {
59 remove_filter($theid);
60 } elseif (isset($action) && $action == 'move_up') {
61 filter_swap($theid, $theid - 1);
62 } elseif (isset($action) && $action == 'move_down') {
63 filter_swap($theid, $theid + 1);
64 }
65
66 if ($AllowSpamFilters) {
67 $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder');
68 $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan');
69 }
70 $filters = load_filters();
71
72 echo '<br>' .
73 '<table width=95% align=center border=0 cellpadding=2 cellspacing=0>'.
74 "<tr><td bgcolor=\"$color[0]\">".
75 '<center><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></center>'.
76 '</td></tr></table>'.
77 '<br><center>[<a href="options.php?action=add">' . _("New") .
78 '</a>] - [<a href="../../src/options.php">' . _("Done") . '</a>]</center><br>' .
79 '<table border=0 cellpadding=3 cellspacing=0 align=center>';
80
81 for ($i=0; $i < count($filters); $i++) {
82 if ($i % 2 == 0) {
83 $clr = $color[0];
84 } else {
85 $clr = $color[9];
86 }
87
88 $fdr = ($folder_prefix)?str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
89
90 echo "<tr bgcolor=\"$clr\"><td><small>".
91 "[<a href=\"options.php?theid=$i&action=edit\">" . _("Edit") . '</a>]'.
92 '</small></td><td><small>'.
93 "[<a href=\"options.php?theid=$i&action=delete\">" . _("Delete") . '</a>]'.
94 '</small></td><td align=center><small>[';
95
96 if (isset($filters[$i + 1])) {
97 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
98 if ($i > 0) {
99 echo ' | ';
100 }
101 }
102 if ($i > 0) {
103 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
104 }
105 echo ']</small></td><td> - ';
106 printf( _("If <b>%s</b> contains <b>%s</b> then move to <b>%s</b>"), _($filters[$i]['where']), $filters[$i]['what'], $fdr );
107 echo '</td></tr>';
108
109 }
110
111 echo '</table>'.
112 '<table width=80% align=center border=0 cellpadding=2 cellspacing=0">'.
113 '<tr><td>&nbsp</td></tr>'.
114 '</table>';
115
116 if ($AllowSpamFilters) {
117
118 echo "<table width=95% align=center border=0 cellpadding=2 cellspacing=0 bgcolor=\"$color[0]\">".
119 '<tr><th align=center>' . _("Spam Filtering") . '</th></tr>'.
120 '</table>';
121 if (! isset($action) || $action != 'spam') {
122
123 echo '<p align=center>[<a href="options.php?action=spam">' . _("Edit") . '</a>]<br>';
124 printf( _("Spam is sent to <b>%s</b>"), ($filters_spam_folder?$filters_spam_folder:_("[<i>not set yet</i>]") ) );
125 echo '<br>';
126 printf( _("Spam scan is limited to <b>%s</b>"), (($filters_spam_scan == 'new')?_("New Messages Only"):_("All Messages") ) );
127 echo '</p>'.
128 "<table border=0 cellpadding=3 cellspacing=0 align=center bgcolor=\"$color[0]\">";
129
130 $spam_filters = load_spam_filters();
131
132 foreach ($spam_filters as $Key => $Value) {
133 echo '<tr><th align=center>';
134
135 if ($spam_filters[$Key]['enabled']) {
136 echo _("ON");
137 } else {
138 echo _("OFF");
139 }
140
141 echo '</th><td>&nbsp;-&nbsp;</td><td>';
142
143 if ($spam_filters[$Key]['link']) {
144 echo '<a href="' .
145 $spam_filters[$Key]['link'] .
146 '" target="_blank">';
147 }
148
149 echo $spam_filters[$Key]['name'];
150 if ($spam_filters[$Key]['link']) {
151 echo '</a>';
152 }
153 echo "</td></tr>\n";
154 }
155 echo '</table>';
156 }
157 }
158
159 if (isset($action) && ($action == 'add' || $action == 'edit')) {
160 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
161 $boxes = sqimap_mailbox_list($imapConnection);
162 sqimap_logout($imapConnection);
163 if ( !isset($theid) ) {
164 $theid = count($filters);
165 }
166 echo '<center>'.
167 '<form action="options.php" method=post>'.
168 '<br><table cellpadding=2 cellspacing=0 border=0>'.
169 '<tr>'.
170 '<td>&nbsp;</td>'.
171 '<td>'.
172 '<select name=filter_where>';
173
174 $L = isset($filters[$theid]['where']);
175
176 $sel = (($L && $filters[$theid]['where'] == 'From')?'selected':'');
177 echo "<option value=\"From\" $sel>" . _ ("From") . '</option>';
178
179 $sel = (($L && $filters[$theid]['where'] == 'To')?'selected':'');
180 echo "<option value=\"To\" $sel>" . _ ("To") . '</option>';
181
182 $sel = (($L && $filters[$theid]['where'] == 'Cc')?'selected':'');
183 echo "<option value=\"Cc\" $sel>" . _ ("Cc") . '</option>';
184
185 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?'selected':'');
186 echo "<option value=\"To or Cc\" $sel>" . _ ("To or Cc") . '</option>';
187
188 $sel = (($L && $filters[$theid]['where'] == 'Subject')?'selected':'');
189 echo "<option value=\"Subject\" $sel>" . _ ("Subject") . '</option>';
190
191 echo '</select>'.
192 '</td>'.
193 '</tr>'.
194 '<tr>'.
195 '<td align=right>'.
196 _("Contains:").
197 '</td>'.
198 '<td>'.
199 '<input type=text size=32 name=filter_what value="';
200 if (isset($filters[$theid]['what'])) {
201 echo $filters[$theid]["what"];
202 }
203 echo '">'.
204 '</td>'.
205 '</tr>'.
206 '<tr>'.
207 '<td>'.
208 _("Move to:").
209 '</td>'.
210 '<td>'.
211 '<tt>'.
212 '<select name=filter_folder>';
213
214 for ($i = 0; $i < count($boxes); $i++) {
215 if (! in_array('noselect', $boxes[$i]['flags'])) {
216 $box = $boxes[$i]['unformatted'];
217 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['formatted']);
218 if (isset($filters[$theid]['folder']) &&
219 $filters[$theid]['folder'] == $box)
220 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</option>";
221 else
222 echo "<OPTION VALUE=\"$box\">$box2</option>";
223 }
224 }
225 echo '</tt>'.
226 '</select>'.
227 '</td>'.
228 '</tr>'.
229 '</table>'.
230 '<input type=submit name=filter_submit value=' . _("Submit") . '>'.
231 "<input type=hidden name=theid value=$theid>".
232 '</form>'.
233 '</center>';
234
235 } else if (isset($action) && $action == 'spam' && $AllowSpamFilters) {
236 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
237 $boxes = sqimap_mailbox_list($imapConnection);
238 sqimap_logout($imapConnection);
239 for ($i = 0; $i < count($boxes) && $filters_spam_folder == ''; $i++) {
240 if ($boxes[$i]['flags'][0] != 'noselect' &&
241 $boxes[$i]['flags'][1] != 'noselect' &&
242 $boxes[$i]['flags'][2] != 'noselect') {
243 $filters_spam_folder = $boxes[$i]['unformatted'];
244 }
245 }
246
247 echo '<form method=post action="options.php">'.
248 '<center>'.
249 '<table width=85% cellpadding=2 cellspacing=0 border=0>'.
250 '<tr>'.
251 '<th align=right nowrap>' . _("Move spam to:") . '</th>'.
252 '<td><select name="filters_spam_folder_set">';
253
254 for ($i = 0; $i < count($boxes); $i++) {
255 if (! in_array('noselect', $boxes[$i]['flags'])) {
256 $box = $boxes[$i]['unformatted'];
257 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['formatted']);
258 if ($filters_spam_folder == $box)
259 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
260 else
261 echo "<OPTION VALUE=\"$box\">$box2</OPTION>\n";
262 }
263 }
264 echo '</select>'.
265 '</td>'.
266 '</tr>'.
267 '<tr><td></td><td>' .
268 _("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.") .
269 '</td></tr>'.
270 '<tr>'.
271 '<th align=right nowrap>' . _("What to Scan:") . '</th>'.
272 '<td><select name="filters_spam_scan_set">'.
273 '<option value=""';
274 if ($filters_spam_scan == '')
275 echo ' SELECTED';
276 echo '>' . _("All messages") . '</option>'.
277 '<option value="new"';
278 if ($filters_spam_scan == 'new')
279 echo ' SELECTED';
280 echo '>' . _("Only unread messages") . '</option>' .
281 '</select>'.
282 '</td>'.
283 '</tr>'.
284 '<tr>'.
285 '<td></td><td>'.
286 _("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.").
287 '</td></tr>';
288
289 $spam_filters = load_spam_filters();
290
291 foreach ($spam_filters as $Key => $Value) {
292 echo "<tr><th align=right nowrap>$Key</th>\n" .
293 '<td><input type=checkbox name="' .
294 $spam_filters[$Key]['prefname'] .
295 '_set"';
296 if ($spam_filters[$Key]['enabled'])
297 echo ' CHECKED';
298 echo '> - ';
299 if ($spam_filters[$Key]['link']) {
300 echo '<a href="' .
301 $spam_filters[$Key]['link'] .
302 '" target="_blank">';
303 }
304 echo $spam_filters[$Key]['name'];
305 if ($spam_filters[$Key]['link']) {
306 echo '</a>';
307 }
308 echo '</td></tr><tr><td></td><td>' .
309 $spam_filters[$Key]['comment'] .
310 "</td></tr>\n";
311 }
312 echo '<tr><td colspan=2 align=center><input type=submit name="spam_submit" value="' . _("Save") . '"></td></tr>'.
313 '</table>'.
314 '</center>'.
315 '</form>';
316
317 sqimap_logout($imapConnection);
318 }
319 ?>