Make the filters plugin more efficient and remove two E_ALL warnings
[squirrelmail.git] / plugins / filters / options.php
CommitLineData
849bdf42 1<?php
15e6162e 2
3/**
4 * Message and Spam Filter Plugin
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This plugin filters your inbox into different folders based upon given
10 * criteria. It is most useful for people who are subscibed to mailing lists
11 * to help organize their messages. The argument stands that filtering is
12 * not the place of the client, which is why this has been made a plugin for
13 * SquirrelMail. You may be better off using products such as Sieve or
14 * Procmail to do your filtering so it happens even when SquirrelMail isn't
15 * running.
16 *
17 * If you need help with this, or see improvements that can be made, please
18 * email me directly at the address above. I definately welcome suggestions
19 * and comments. This plugin, as is the case with all SquirrelMail plugins,
20 * is not directly supported by the developers. Please come to me off the
21 * mailing list if you have trouble with it.
22 *
23 * Also view plugins/README.plugins for more information.
24 *
25 * $Id$
26 */
4eee5968 27
08185f2a 28/* Path for SquirrelMail required files. */
29define('SM_PATH','../../');
30
31/* SquirrelMail required files. */
fca6d99e 32require_once(SM_PATH . 'include/validate.php');
08185f2a 33require_once(SM_PATH . 'functions/page_header.php');
34require_once(SM_PATH . 'functions/imap.php');
fca6d99e 35require_once(SM_PATH . 'include/load_prefs.php');
36require_once(SM_PATH . 'plugins/filters/filters.php');
849bdf42 37
38 global $AllowSpamFilters;
39
3fd1252d 40 displayPageHeader($color, 'None');
849bdf42 41
41100fce 42 $username = $_SESSION['username'];
43 $key = $_COOKIE['key'];
44 $onetimepad = $_SESSION['onetimepad'];
45 $delimiter = $_SESSION['delimiter'];
46 if(isset($_GET['theid'])) {
47 $theid = $_GET['theid'];
48 }
49 if(isset($_POST['theid'])) {
50 $theid = $_POST['theid'];
51 }
52 if(isset($_GET['action'])) {
53 $action = $_GET['action'];
54 }
55
56 if (isset($_POST['filter_submit'])) {
57 if(isset($_GET['theid'])) {
58 $theid = $_GET['theid'];
59 } elseif (isset($_POST['theid'])) {
60 $theid = $_POST['theid'];
61 } else {
62 $theid = 0;
63 }
64 $filter_what = $_POST['filter_what'];
65 $filter_where = $_POST['filter_where'];
66 $filter_folder = $_POST['filter_folder'];
67
ae48f757 68 $filter_what = str_replace(',', ' ', $filter_what);
849bdf42 69 $filter_what = str_replace("\\\\", "\\", $filter_what);
ae48f757 70 $filter_what = str_replace("\\\"", '"', $filter_what);
71 $filter_what = str_replace('"', '&quot;', $filter_what);
849bdf42 72
8e485f9b 73 if (($filter_where == 'Header') && (strchr($filter_what,':') == '')) {
74 print ('WARNING! Header filters should be of the format "Header: value"<BR>');
75 $action = 'edit';
76 }
ae48f757 77 setPref($data_dir, $username, 'filter'.$theid, $filter_where.','.$filter_what.','.$filter_folder);
78 $filters[$theid]['where'] = $filter_where;
79 $filters[$theid]['what'] = $filter_what;
80 $filters[$theid]['folder'] = $filter_folder;
3fd1252d 81 } elseif (isset($action) && $action == 'delete') {
849bdf42 82 remove_filter($theid);
3fd1252d 83 } elseif (isset($action) && $action == 'move_up') {
849bdf42 84 filter_swap($theid, $theid - 1);
3fd1252d 85 } elseif (isset($action) && $action == 'move_down') {
849bdf42 86 filter_swap($theid, $theid + 1);
41100fce 87 } elseif (isset($_POST['user_submit'])) {
88 setPref($data_dir, $username, 'filters_user_scan', $_POST['filters_user_scan_set']);
89 echo '<br><center><b>'._("Saved Scan type")."</b></center>\n";
849bdf42 90 }
91
849bdf42 92 $filters = load_filters();
c8a2c24d 93 $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
849bdf42 94
6206f6c4 95 echo html_tag( 'table',
b01b21d0 96 html_tag( 'tr',
97 html_tag( 'td',
98 '<center><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></center>' ,
99 'left', $color[0] )
100 ) ,
101 'center', '', 'width="95%" border="0" cellpadding="2" cellspacing="0"' ) .
c8a2c24d 102
103 '<br><form method=post action="options.php">'.
104 '<center>'.
831a25d5 105 html_tag( 'table', '', '', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
b01b21d0 106 html_tag( 'tr' ) .
107 html_tag( 'th', _("What to Scan:"), 'right', '', 'nowrap' ) .
108 html_tag( 'td', '', 'left' ) .
109 '<select name="filters_user_scan_set">'.
c8a2c24d 110 '<option value=""';
111 if ($filters_user_scan == '') {
b01b21d0 112 echo ' selected';
c8a2c24d 113 }
114 echo '>' . _("All messages") . '</option>'.
115 '<option value="new"';
116 if ($filters_user_scan == 'new') {
b01b21d0 117 echo ' selected';
c8a2c24d 118 }
119 echo '>' . _("Only unread messages") . '</option>' .
120 '</select>'.
121 '</td>'.
b01b21d0 122 html_tag( 'td', '<input type=submit name="user_submit" value="' . _("Save") . '">', 'left' ) .
c8a2c24d 123 '</table>'.
124 '</center>'.
125 '</form>'.
126
b01b21d0 127 html_tag( 'div', '[<a href="options.php?action=add">' . _("New") .
128 '</a>] - [<a href="../../src/options.php">' . _("Done") . '</a>]' ,
129 'center' ) . '<br>';
0e838332 130
3fd1252d 131 if (isset($action) && ($action == 'add' || $action == 'edit')) {
41100fce 132 $username = $_SESSION['username'];
133 $key = $_COOKIE['key'];
134
3fd1252d 135 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
136 $boxes = sqimap_mailbox_list($imapConnection);
137 sqimap_logout($imapConnection);
138 if ( !isset($theid) ) {
849bdf42 139 $theid = count($filters);
3fd1252d 140 }
b01b21d0 141 echo html_tag( 'div', '', 'center' ) .
72570d58 142 '<form action="options.php" method=post>'.
831a25d5 143 html_tag( 'table', '', '', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
b01b21d0 144 html_tag( 'tr' ) .
145 html_tag( 'td', _("Match:"), 'left' ) .
146 html_tag( 'td', '', 'left' ) .
4eee5968 147 '<select name=filter_where>';
849bdf42 148
3fd1252d 149 $L = isset($filters[$theid]['where']);
150
151 $sel = (($L && $filters[$theid]['where'] == 'From')?'selected':'');
152 echo "<option value=\"From\" $sel>" . _ ("From") . '</option>';
4eee5968 153
3fd1252d 154 $sel = (($L && $filters[$theid]['where'] == 'To')?'selected':'');
155 echo "<option value=\"To\" $sel>" . _ ("To") . '</option>';
156
157 $sel = (($L && $filters[$theid]['where'] == 'Cc')?'selected':'');
158 echo "<option value=\"Cc\" $sel>" . _ ("Cc") . '</option>';
159
160 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?'selected':'');
161 echo "<option value=\"To or Cc\" $sel>" . _ ("To or Cc") . '</option>';
162
163 $sel = (($L && $filters[$theid]['where'] == 'Subject')?'selected':'');
164 echo "<option value=\"Subject\" $sel>" . _ ("Subject") . '</option>';
165
9da6bdde 166 $sel = (($L && $filters[$theid]['where'] == 'Header')?'selected':'');
167 echo "<option value=\"Header\" $sel>" . _ ("Header") . '</option>';
168
3fd1252d 169 echo '</select>'.
4eee5968 170 '</td>'.
171 '</tr>'.
b01b21d0 172 html_tag( 'tr' ) .
173 html_tag( 'td', _("Contains:"), 'right' ) .
174 html_tag( 'td', '', 'left' ) .
4eee5968 175 '<input type=text size=32 name=filter_what value="';
3fd1252d 176 if (isset($filters[$theid]['what'])) {
4eee5968 177 echo $filters[$theid]["what"];
3fd1252d 178 }
179 echo '">'.
4eee5968 180 '</td>'.
181 '</tr>'.
b01b21d0 182 html_tag( 'tr' ) .
183 html_tag( 'td', _("Move to:"), 'left' ) .
184 html_tag( 'td', '', 'left' ) .
4eee5968 185 '<tt>'.
186 '<select name=filter_folder>';
3fd1252d 187
ae48f757 188 for ($i = 0, $num = count($boxes); $i < $num; $i++) {
3fd1252d 189 if (! in_array('noselect', $boxes[$i]['flags'])) {
190 $box = $boxes[$i]['unformatted'];
191 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['formatted']);
192 if (isset($filters[$theid]['folder']) &&
193 $filters[$theid]['folder'] == $box)
4eee5968 194 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</option>";
3fd1252d 195 else
4eee5968 196 echo "<OPTION VALUE=\"$box\">$box2</option>";
849bdf42 197 }
3fd1252d 198 }
199 echo '</tt>'.
4eee5968 200 '</select>'.
201 '</td>'.
202 '</tr>'.
3fd1252d 203 '</table>'.
204 '<input type=submit name=filter_submit value=' . _("Submit") . '>'.
205 "<input type=hidden name=theid value=$theid>".
206 '</form>'.
b01b21d0 207 '</div>';
3fd1252d 208
3fd1252d 209 }
cb2ccf4b 210
831a25d5 211 echo html_tag( 'table', '', 'center', '', 'border="0" cellpadding="3" cellspacing="0"' );
cb2ccf4b 212
ae48f757 213 for ($i=0, $num = count($filters); $i < $num; $i++) {
cb2ccf4b 214
215 $clr = (($i % 2)?$color[0]:$color[9]);
216 $fdr = ($folder_prefix)?str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
831a25d5 217 echo html_tag( 'tr', '', '', $clr ) .
b01b21d0 218 html_tag( 'td',
219 '<small>' .
220 "[<a href=\"options.php?theid=$i&action=edit\">" . _("Edit") . '</a>]'.
221 '</small>' ,
222 'left' ) .
223 html_tag( 'td',
224 '<small>' .
225 "[<a href=\"options.php?theid=$i&action=delete\">" . _("Delete") . '</a>]'.
226 '</small>' ,
227 'left' ) .
228 html_tag( 'td', '', 'center' ) . '<small>[';
cb2ccf4b 229
230 if (isset($filters[$i + 1])) {
231 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
232 if ($i > 0) {
2a61e066 233 echo '&nbsp;|&nbsp;';
cb2ccf4b 234 }
235 }
236 if ($i > 0) {
237 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
238 }
b01b21d0 239 echo ']</small></td>'.
240 html_tag( 'td', '-', 'left' ) .
241 html_tag( 'td', '', 'left' );
cb2ccf4b 242 printf( _("If <b>%s</b> contains <b>%s</b> then move to <b>%s</b>"), _($filters[$i]['where']), $filters[$i]['what'], $fdr );
243 echo '</td></tr>';
244
245 }
246 echo '</table>'.
b01b21d0 247 html_tag( 'table',
248 html_tag( 'tr',
249 html_tag( 'td', '&nbsp', 'left' )
250 ) ,
251 'center', '', 'width="80%" border="0" cellpadding="2" cellspacing="0"' );
4eee5968 252?>