4 * Message and Spam Filter Plugin
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
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
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.
23 * Also view plugins/README.plugins for more information.
28 /* Path for SquirrelMail required files. */
29 define('SM_PATH','../../');
31 /* SquirrelMail required files. */
32 require_once(SM_PATH
. 'include/validate.php');
33 require_once(SM_PATH
. 'functions/page_header.php');
34 require_once(SM_PATH
. 'functions/imap.php');
35 require_once(SM_PATH
. 'functions/imap_mailbox.php');
36 require_once(SM_PATH
. 'include/load_prefs.php');
37 require_once(SM_PATH
. 'plugins/filters/filters.php');
39 global $AllowSpamFilters;
41 displayPageHeader($color, 'None');
43 $username = $_SESSION['username'];
44 $key = $_COOKIE['key'];
45 $onetimepad = $_SESSION['onetimepad'];
46 $delimiter = $_SESSION['delimiter'];
47 if(isset($_GET['theid'])) {
48 $theid = $_GET['theid'];
50 if(isset($_POST['theid'])) {
51 $theid = $_POST['theid'];
53 if(isset($_GET['action'])) {
54 $action = $_GET['action'];
57 if (isset($_POST['filter_submit'])) {
58 if(isset($_GET['theid'])) {
59 $theid = $_GET['theid'];
60 } elseif (isset($_POST['theid'])) {
61 $theid = $_POST['theid'];
65 $filter_what = $_POST['filter_what'];
66 $filter_where = $_POST['filter_where'];
67 $filter_folder = $_POST['filter_folder'];
69 $filter_what = str_replace(',', ' ', $filter_what);
70 $filter_what = str_replace("\\\\", "\\", $filter_what);
71 $filter_what = str_replace("\\\"", '"', $filter_what);
72 $filter_what = str_replace('"', '"', $filter_what);
74 if (($filter_where == 'Header') && (strchr($filter_what,':') == '')) {
75 print ('WARNING! Header filters should be of the format "Header: value"<BR>');
78 setPref($data_dir, $username, 'filter'.$theid, $filter_where.','.$filter_what.','.$filter_folder);
79 $filters[$theid]['where'] = $filter_where;
80 $filters[$theid]['what'] = $filter_what;
81 $filters[$theid]['folder'] = $filter_folder;
82 } elseif (isset($action) && $action == 'delete') {
83 remove_filter($theid);
84 } elseif (isset($action) && $action == 'move_up') {
85 filter_swap($theid, $theid - 1);
86 } elseif (isset($action) && $action == 'move_down') {
87 filter_swap($theid, $theid +
1);
88 } elseif (isset($_POST['user_submit'])) {
89 setPref($data_dir, $username, 'filters_user_scan', $_POST['filters_user_scan_set']);
90 echo '<br><center><b>'._("Saved Scan type")."</b></center>\n";
93 $filters = load_filters();
94 $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
96 echo html_tag( 'table',
99 '<center><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></center>' ,
102 'center', '', 'width="95%" border="0" cellpadding="2" cellspacing="0"' ) .
104 '<br><form method=post action="options.php">'.
106 html_tag( 'table', '', '', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
108 html_tag( 'th', _("What to Scan:"), 'right', '', 'nowrap' ) .
109 html_tag( 'td', '', 'left' ) .
110 '<select name="filters_user_scan_set">'.
112 if ($filters_user_scan == '') {
115 echo '>' . _("All messages") . '</option>'.
116 '<option value="new"';
117 if ($filters_user_scan == 'new') {
120 echo '>' . _("Only unread messages") . '</option>' .
123 html_tag( 'td', '<input type=submit name="user_submit" value="' . _("Save") . '">', 'left' ) .
128 html_tag( 'div', '[<a href="options.php?action=add">' . _("New") .
129 '</a>] - [<a href="'.SM_PATH
.'src/options.php">' . _("Done") . '</a>]' ,
132 if (isset($action) && ($action == 'add' ||
$action == 'edit')) {
133 $username = $_SESSION['username'];
134 $key = $_COOKIE['key'];
136 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
137 $boxes = sqimap_mailbox_list($imapConnection);
138 sqimap_logout($imapConnection);
139 if ( !isset($theid) ) {
140 $theid = count($filters);
142 echo html_tag( 'div', '', 'center' ) .
143 '<form action="options.php" method=post>'.
144 html_tag( 'table', '', '', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
146 html_tag( 'td', _("Match:"), 'left' ) .
147 html_tag( 'td', '', 'left' ) .
148 '<select name=filter_where>';
150 $L = isset($filters[$theid]['where']);
152 $sel = (($L && $filters[$theid]['where'] == 'From')?
'selected':'');
153 echo "<option value=\"From\" $sel>" . _ ("From") . '</option>';
155 $sel = (($L && $filters[$theid]['where'] == 'To')?
'selected':'');
156 echo "<option value=\"To\" $sel>" . _ ("To") . '</option>';
158 $sel = (($L && $filters[$theid]['where'] == 'Cc')?
'selected':'');
159 echo "<option value=\"Cc\" $sel>" . _ ("Cc") . '</option>';
161 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?
'selected':'');
162 echo "<option value=\"To or Cc\" $sel>" . _ ("To or Cc") . '</option>';
164 $sel = (($L && $filters[$theid]['where'] == 'Subject')?
'selected':'');
165 echo "<option value=\"Subject\" $sel>" . _ ("Subject") . '</option>';
167 $sel = (($L && $filters[$theid]['where'] == 'Header')?
'selected':'');
168 echo "<option value=\"Header\" $sel>" . _ ("Header") . '</option>';
174 html_tag( 'td', _("Contains:"), 'right' ) .
175 html_tag( 'td', '', 'left' ) .
176 '<input type=text size=32 name=filter_what value="';
177 if (isset($filters[$theid]['what'])) {
178 echo $filters[$theid]["what"];
184 html_tag( 'td', _("Move to:"), 'left' ) .
185 html_tag( 'td', '', 'left' ) .
187 '<select name=filter_folder>';
189 if ( isset($filters[$theid]['folder']) )
190 $selected = array(strtolower($filters[$theid]['folder']));
191 echo sqimap_mailbox_option_list(0, $selected, 0, $boxes);
197 '<input type=submit name=filter_submit value=' . _("Submit") . '>'.
198 "<input type=hidden name=theid value=$theid>".
204 echo html_tag( 'table', '', 'center', '', 'border="0" cellpadding="3" cellspacing="0"' );
206 for ($i=0, $num = count($filters); $i < $num; $i++
) {
208 $clr = (($i %
2)?
$color[0]:$color[9]);
209 $fdr = ($folder_prefix)?
str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
210 echo html_tag( 'tr', '', '', $clr ) .
213 "[<a href=\"options.php?theid=$i&action=edit\">" . _("Edit") . '</a>]'.
218 "[<a href=\"options.php?theid=$i&action=delete\">" . _("Delete") . '</a>]'.
221 html_tag( 'td', '', 'center' ) . '<small>[';
223 if (isset($filters[$i +
1])) {
224 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
226 echo ' | ';
230 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
232 echo ']</small></td>'.
233 html_tag( 'td', '-', 'left' ) .
234 html_tag( 'td', '', 'left' );
235 printf( _("If <b>%s</b> contains <b>%s</b> then move to <b>%s</b>"), _($filters[$i]['where']), $filters[$i]['what'], $fdr );
242 html_tag( 'td', ' ', 'left' )
244 'center', '', 'width="80%" border="0" cellpadding="2" cellspacing="0"' );