421975b3bdb8e8e50bbe87ee2b4e0ede3eb09df9
[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 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);
76 }
77 echo '<center>'.
78 '<form action="options.php" method=post>'.
79 '<br><table cellpadding=2 cellspacing=0 border=0>'.
80 '<tr>'.
81 '<td>&nbsp;</td>'.
82 '<td>'.
83 '<select name=filter_where>';
84
85 $L = isset($filters[$theid]['where']);
86
87 $sel = (($L && $filters[$theid]['where'] == 'From')?'selected':'');
88 echo "<option value=\"From\" $sel>" . _ ("From") . '</option>';
89
90 $sel = (($L && $filters[$theid]['where'] == 'To')?'selected':'');
91 echo "<option value=\"To\" $sel>" . _ ("To") . '</option>';
92
93 $sel = (($L && $filters[$theid]['where'] == 'Cc')?'selected':'');
94 echo "<option value=\"Cc\" $sel>" . _ ("Cc") . '</option>';
95
96 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?'selected':'');
97 echo "<option value=\"To or Cc\" $sel>" . _ ("To or Cc") . '</option>';
98
99 $sel = (($L && $filters[$theid]['where'] == 'Subject')?'selected':'');
100 echo "<option value=\"Subject\" $sel>" . _ ("Subject") . '</option>';
101
102 echo '</select>'.
103 '</td>'.
104 '</tr>'.
105 '<tr>'.
106 '<td align=right>'.
107 _("Contains:").
108 '</td>'.
109 '<td>'.
110 '<input type=text size=32 name=filter_what value="';
111 if (isset($filters[$theid]['what'])) {
112 echo $filters[$theid]["what"];
113 }
114 echo '">'.
115 '</td>'.
116 '</tr>'.
117 '<tr>'.
118 '<td>'.
119 _("Move to:").
120 '</td>'.
121 '<td>'.
122 '<tt>'.
123 '<select name=filter_folder>';
124
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(' ', '&nbsp;', $boxes[$i]['formatted']);
129 if (isset($filters[$theid]['folder']) &&
130 $filters[$theid]['folder'] == $box)
131 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</option>";
132 else
133 echo "<OPTION VALUE=\"$box\">$box2</option>";
134 }
135 }
136 echo '</tt>'.
137 '</select>'.
138 '</td>'.
139 '</tr>'.
140 '</table>'.
141 '<input type=submit name=filter_submit value=' . _("Submit") . '>'.
142 "<input type=hidden name=theid value=$theid>".
143 '</form>'.
144 '</center>';
145
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'];
155 }
156 }
157
158 echo '<form method=post action="options.php">'.
159 '<center>'.
160 '<table width=85% cellpadding=2 cellspacing=0 border=0>'.
161 '<tr>'.
162 '<th align=right nowrap>' . _("Move spam to:") . '</th>'.
163 '<td><select name="filters_spam_folder_set">';
164
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(' ', '&nbsp;', $boxes[$i]['formatted']);
169 if ($filters_spam_folder == $box)
170 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
171 else
172 echo "<OPTION VALUE=\"$box\">$box2</OPTION>\n";
173 }
174 }
175 echo '</select>'.
176 '</td>'.
177 '</tr>'.
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.") .
180 '</td></tr>'.
181 '<tr>'.
182 '<th align=right nowrap>' . _("What to Scan:") . '</th>'.
183 '<td><select name="filters_spam_scan_set">'.
184 '<option value=""';
185 if ($filters_spam_scan == '')
186 echo ' SELECTED';
187 echo '>' . _("All messages") . '</option>'.
188 '<option value="new"';
189 if ($filters_spam_scan == 'new')
190 echo ' SELECTED';
191 echo '>' . _("Only unread messages") . '</option>' .
192 '</select>'.
193 '</td>'.
194 '</tr>'.
195 '<tr>'.
196 '<td></td><td>'.
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.").
198 '</td></tr>';
199
200 $spam_filters = load_spam_filters();
201
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'] .
206 '_set"';
207 if ($spam_filters[$Key]['enabled'])
208 echo ' CHECKED';
209 echo '> - ';
210 if ($spam_filters[$Key]['link']) {
211 echo '<a href="' .
212 $spam_filters[$Key]['link'] .
213 '" target="_blank">';
214 }
215 echo $spam_filters[$Key]['name'];
216 if ($spam_filters[$Key]['link']) {
217 echo '</a>';
218 }
219 echo '</td></tr><tr><td></td><td>' .
220 $spam_filters[$Key]['comment'] .
221 "</td></tr>\n";
222 }
223 echo '<tr><td colspan=2 align=center><input type=submit name="spam_submit" value="' . _("Save") . '"></td></tr>'.
224 '</table>'.
225 '</center>'.
226 '</form>';
227
228 sqimap_logout($imapConnection);
229 }
230
231 echo '<br>' .
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>';
239
240 for ($i=0; $i < count($filters); $i++) {
241
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>[';
249
250 if (isset($filters[$i + 1])) {
251 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
252 if ($i > 0) {
253 echo ' | ';
254 }
255 }
256 if ($i > 0) {
257 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
258 }
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 );
261 echo '</td></tr>';
262
263 }
264 echo '</table>'.
265 '<table width=80% align=center border=0 cellpadding=2 cellspacing=0">'.
266 '<tr><td>&nbsp</td></tr>'.
267 '</table>';
268
269
270 if ($AllowSpamFilters) {
271
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>'.
274 '</table>';
275 if (! isset($action) || $action != 'spam') {
276
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>]") ) );
279 echo '<br>';
280 printf( _("Spam scan is limited to <b>%s</b>"), (($filters_spam_scan == 'new')?_("New Messages Only"):_("All Messages") ) );
281 echo '</p>'.
282 "<table border=0 cellpadding=3 cellspacing=0 align=center bgcolor=\"$color[0]\">";
283
284 $spam_filters = load_spam_filters();
285
286 foreach ($spam_filters as $Key => $Value) {
287 echo '<tr><th align=center>';
288
289 if ($spam_filters[$Key]['enabled']) {
290 echo _("ON");
291 } else {
292 echo _("OFF");
293 }
294
295 echo '</th><td>&nbsp;-&nbsp;</td><td>';
296
297 if ($spam_filters[$Key]['link']) {
298 echo '<a href="' .
299 $spam_filters[$Key]['link'] .
300 '" target="_blank">';
301 }
302
303 echo $spam_filters[$Key]['name'];
304 if ($spam_filters[$Key]['link']) {
305 echo '</a>';
306 }
307 echo "</td></tr>\n";
308 }
309 echo '</table>';
310 }
311 }
312
313 ?>