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