3 * Message and Spam Filter Plugin - Filtering Options
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
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.
19 * Also view plugins/README.plugins for more information.
22 * @copyright (c) 1999-2005 The SquirrelMail Project Team
23 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
29 * Path for SquirrelMail required files.
32 define('SM_PATH','../../');
34 /* SquirrelMail required files. */
35 require_once(SM_PATH
. 'include/validate.php');
36 require_once(SM_PATH
. 'functions/imap.php');
37 require_once(SM_PATH
. 'plugins/filters/filters.php');
39 if (!file_exists(SM_PATH
. 'plugins/filters/config.php')) return;
40 @include_once
(SM_PATH
. 'plugins/filters/config.php');
42 global $AllowSpamFilters;
44 displayPageHeader($color, 'None');
47 sqgetGlobalVar('username', $username, SQ_SESSION
);
48 sqgetGlobalVar('key', $key, SQ_COOKIE
);
49 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
);
50 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
52 sqgetGlobalVar('theid', $theid);
53 sqgetGlobalVar('action', $action, SQ_GET
);
55 if (sqgetGlobalVar('filter_submit',$filter_submit,SQ_POST
)) {
57 if(! isset($theid) ) $theid = 0;
61 // FIXME: write human readable error messages
62 sqgetGlobalVar('filter_what', $filter_what, SQ_POST
);
63 if (!sqgetGlobalVar('filter_what', $filter_what, SQ_POST
)) {
64 do_error("Post error");
68 sqgetGlobalVar('filter_where', $filter_where, SQ_POST
);
69 if (!sqgetGlobalVar('filter_where', $filter_where, SQ_POST
)) {
70 do_error("Post error");
74 sqgetGlobalVar('filter_folder', $filter_folder, SQ_POST
);
75 if (!sqgetGlobalVar('filter_folder', $filter_folder, SQ_POST
)) {
76 do_error("Post error");
81 $filter_what = str_replace(',', ' ', $filter_what);
82 $filter_what = str_replace("\\\\", "\\", $filter_what);
83 $filter_what = str_replace("\\\"", '"', $filter_what);
84 $filter_what = str_replace('"', '"', $filter_what);
86 if (empty($filter_what)) {
87 do_error(_("WARNING! You must enter something to search for."));
91 if (($filter_where == 'Header') && (strchr($filter_what,':') == '')) {
92 do_error(_("WARNING! Header filters should be of the format "Header: value""));
95 if ($action != 'edit') {
96 setPref($data_dir, $username, 'filter'.$theid, $filter_where.','.$filter_what.','.$filter_folder);
98 $filters[$theid]['where'] = $filter_where;
99 $filters[$theid]['what'] = $filter_what;
100 $filters[$theid]['folder'] = $filter_folder;
102 } elseif (isset($action) && $action == 'delete') {
103 remove_filter($theid);
104 } elseif (isset($action) && $action == 'move_up') {
105 filter_swap($theid, $theid - 1);
106 } elseif (isset($action) && $action == 'move_down') {
107 filter_swap($theid, $theid +
1);
108 } elseif (sqgetGlobalVar('user_submit',$user_submit,SQ_POST
)) {
109 sqgetGlobalVar('filters_user_scan_set',$filters_user_scan_set,SQ_POST
);
110 setPref($data_dir, $username, 'filters_user_scan', $filters_user_scan_set);
111 echo '<br /><center><b>'._("Saved Scan type")."</b></center>\n";
114 $filters = load_filters();
115 $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
117 echo html_tag( 'table',
120 '<center><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></center>' ,
124 'center', '', 'width="95%" border="0" cellpadding="2" cellspacing="0"'
126 '<br /><form method="post" action="options.php">'.
128 html_tag( 'table', '', '', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
130 html_tag( 'th', _("What to Scan:"), 'right', '', 'style="white-space: nowrap;"' ) .
131 html_tag( 'td', '', 'left' ) .
132 '<select name="filters_user_scan_set">'.
134 if ($filters_user_scan == '') {
135 echo ' selected="selected"';
137 echo '>' . _("All messages") . '</option>'.
138 '<option value="new"';
139 if ($filters_user_scan == 'new') {
140 echo ' selected="selected"';
142 echo '>' . _("Only unread messages") . '</option>' .
145 html_tag( 'td', '<input type="submit" name="user_submit" value="' . _("Save") . '" />', 'left' ) .
150 html_tag( 'div', '[<a href="options.php?action=add">' . _("New") .
151 '</a>] - [<a href="'.SM_PATH
.'src/options.php">' . _("Done") . '</a>]' ,
152 'center' ) . '<br />';
154 if (isset($action) && ($action == 'add' ||
$action == 'edit')) {
156 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
157 $boxes = sqimap_mailbox_list($imapConnection);
159 for ($a = 0, $cnt = count($boxes); $a < $cnt; $a++
) {
160 if (strtolower($boxes[$a]['formatted']) == 'inbox') {
165 sqimap_logout($imapConnection);
166 if ( !isset($theid) ) {
167 $theid = count($filters);
169 echo html_tag( 'div', '', 'center' ) .
170 '<form action="options.php" method="post">'.
171 html_tag( 'table', '', '', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
173 html_tag( 'td', _("Match:"), 'left' ) .
174 html_tag( 'td', '', 'left' ) .
175 '<select name="filter_where">';
177 $L = isset($filters[$theid]['where']);
179 $sel = (($L && $filters[$theid]['where'] == 'From')?
' selected="selected"':'');
180 echo "<option value=\"From\"$sel>" . _("From") . '</option>';
182 $sel = (($L && $filters[$theid]['where'] == 'To')?
' selected="selected"':'');
183 echo "<option value=\"To\"$sel>" . _("To") . '</option>';
185 $sel = (($L && $filters[$theid]['where'] == 'Cc')?
' selected="selected"':'');
186 echo "<option value=\"Cc\"$sel>" . _("Cc") . '</option>';
188 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?
' selected="selected"':'');
189 echo "<option value=\"To or Cc\"$sel>" . _("To or Cc") . '</option>';
191 $sel = (($L && $filters[$theid]['where'] == 'Subject')?
' selected="selected"':'');
192 echo "<option value=\"Subject\"$sel>" . _("Subject") . '</option>';
194 $sel = (($L && $filters[$theid]['where'] == 'Header')?
' selected="selected"':'');
195 echo "<option value=\"Header\"$sel>" . _("Header") . '</option>';
201 html_tag( 'td', _("Contains:"), 'right' ) .
202 html_tag( 'td', '', 'left' ) .
203 '<input type="text" size="32" name="filter_what" value="';
204 if (isset($filters[$theid]['what'])) {
205 echo $filters[$theid]['what'];
211 html_tag( 'td', _("Move to:"), 'left' ) .
212 html_tag( 'td', '', 'left' ) .
214 '<select name="filter_folder">';
216 if ( isset($filters[$theid]['folder']) )
217 $selected = array(strtolower($filters[$theid]['folder']));
218 echo sqimap_mailbox_option_list(0, $selected, 0, $boxes);
224 '<input type="submit" name="filter_submit" value="' . _("Submit") . "\" />\n".
225 addHidden('theid', $theid).
231 echo html_tag( 'table', '', 'center', '', 'border="0" cellpadding="3" cellspacing="0"' );
233 for ($i=0, $num = count($filters); $i < $num; $i++
) {
235 $clr = (($i %
2)?
$color[0]:$color[9]);
236 $fdr = ($folder_prefix)?
str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
237 echo html_tag( 'tr', '', '', $clr ) .
240 "[<a href=\"options.php?theid=$i&action=edit\">" . _("Edit") . '</a>]'.
245 "[<a href=\"options.php?theid=$i&action=delete\">" . _("Delete") . '</a>]'.
250 echo html_tag( 'td', '', 'center' ) . '<small>[';
251 if (isset($filters[$i +
1])) {
252 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
254 echo ' | ';
258 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
260 echo ']</small></td>';
262 echo html_tag( 'td', '-', 'left' ) .
263 html_tag( 'td', '', 'left' );
264 printf( _("If %s contains %s then move to %s"),
265 '<b>'.$filters[$i]['where'].'</b>',
266 '<b>'.$filters[$i]['what'].'</b>',
267 '<b>'.imap_utf7_decode_local($fdr).'</b>');
274 html_tag( 'td', ' ', 'left' )
276 'center', '', 'width="80%" border="0" cellpadding="2" cellspacing="0"' );
277 echo '</body></html>';