222ca7493d071f7c1e66fb35c080b0a56f03d095
[squirrelmail.git] / plugins / filters / options.php
1 <?php
2
3 /**
4 * Message and Spam Filter Plugin - Filtering Options
5 *
6 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 * @version $Id$
9 * @package plugins
10 * @subpackage filters
11 */
12
13 /**
14 * Include the SquirrelMail initialization file.
15 */
16 require('../../include/init.php');
17 include_once(SM_PATH . 'functions/imap_general.php');
18 include_once(SM_PATH . 'plugins/filters/filters.php');
19
20 displayPageHeader($color, 'None');
21
22 /* get globals */
23 sqgetGlobalVar('username', $username, SQ_SESSION);
24 sqgetGlobalVar('key', $key, SQ_COOKIE);
25 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
26 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
27
28 sqgetGlobalVar('theid', $theid);
29 sqgetGlobalVar('action', $action, SQ_GET);
30
31 if (sqgetGlobalVar('filter_submit',$filter_submit,SQ_POST)) {
32
33 if(! isset($theid) ) $theid = 0;
34
35 $complete_post=true;
36
37 // FIXME: write human readable error messages
38 sqgetGlobalVar('filter_what', $filter_what, SQ_POST);
39 if (!sqgetGlobalVar('filter_what', $filter_what, SQ_POST)) {
40 do_error("Post error");
41 $complete_post=false;
42 }
43
44 sqgetGlobalVar('filter_where', $filter_where, SQ_POST);
45 if (!sqgetGlobalVar('filter_where', $filter_where, SQ_POST)) {
46 do_error("Post error");
47 $complete_post=false;
48 }
49
50 sqgetGlobalVar('filter_folder', $filter_folder, SQ_POST);
51 if (!sqgetGlobalVar('filter_folder', $filter_folder, SQ_POST)) {
52 do_error("Post error");
53 $complete_post=false;
54 }
55
56 if ($complete_post) {
57 $filter_what = str_replace(',', ' ', $filter_what);
58 $filter_what = str_replace("\\\\", "\\", $filter_what);
59 $filter_what = str_replace("\\\"", '"', $filter_what);
60 $filter_what = str_replace('"', '&quot;', $filter_what);
61
62 if (empty($filter_what)) {
63 do_error(_("WARNING! You must enter something to search for."));
64 $action = 'edit';
65 }
66
67 if (($filter_where == 'Header') && (strchr($filter_what,':') == '')) {
68 do_error(_("WARNING! Header filters should be of the format &quot;Header: value&quot;"));
69 $action = 'edit';
70 }
71 if ($action != 'edit') {
72 setPref($data_dir, $username, 'filter'.$theid, $filter_where.','.$filter_what.','.$filter_folder);
73 }
74 $filters[$theid]['where'] = $filter_where;
75 $filters[$theid]['what'] = $filter_what;
76 $filters[$theid]['folder'] = $filter_folder;
77 }
78 } elseif (isset($action) && $action == 'delete') {
79 remove_filter($theid);
80 } elseif (isset($action) && $action == 'move_up') {
81 filter_swap($theid, $theid - 1);
82 } elseif (isset($action) && $action == 'move_down') {
83 filter_swap($theid, $theid + 1);
84 } elseif (sqgetGlobalVar('user_submit',$user_submit,SQ_POST)) {
85 sqgetGlobalVar('filters_user_scan_set',$filters_user_scan_set,SQ_POST);
86 setPref($data_dir, $username, 'filters_user_scan', $filters_user_scan_set);
87 echo '<br /><div style="text-align: center;"><b>'._("Saved Scan type")."</b></div>\n";
88 }
89
90 $filters = load_filters();
91 $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
92
93 echo html_tag( 'table',
94 html_tag( 'tr',
95 html_tag( 'td',
96 '<div style="text-align: center;"><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></div>' ,
97 'left', $color[0]
98 )
99 ),
100 'center', '', 'width="95%" border="0" cellpadding="2" cellspacing="0"'
101 ) .
102 '<br /><form method="post" action="options.php">'.
103 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
104 html_tag( 'tr' ) .
105 html_tag( 'th', _("What to Scan:"), 'right', '', 'style="white-space: nowrap;"' ) .
106 html_tag( 'td', '', 'left' ) .
107 '<select name="filters_user_scan_set">'.
108 '<option value=""';
109 if ($filters_user_scan == '') {
110 echo ' selected="selected"';
111 }
112 echo '>' . _("All messages") . '</option>'.
113 '<option value="new"';
114 if ($filters_user_scan == 'new') {
115 echo ' selected="selected"';
116 }
117 echo '>' . _("Only unread messages") . '</option>' .
118 '</select>'.
119 '</td>'.
120 html_tag( 'td', '<input type="submit" name="user_submit" value="' . _("Save") . '" />', 'left' ) .
121 '</table>'.
122 '</form>'.
123
124 html_tag( 'div', '[<a href="options.php?action=add">' . _("New") .
125 '</a>] - [<a href="'.SM_PATH.'src/options.php">' . _("Done") . '</a>]' ,
126 'center' ) . '<br />';
127
128 if (isset($action) && ($action == 'add' || $action == 'edit')) {
129
130 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
131 $boxes = sqimap_mailbox_list($imapConnection);
132
133 for ($a = 0, $cnt = count($boxes); $a < $cnt; $a++) {
134 if (strtolower($boxes[$a]['formatted']) == 'inbox') {
135 unset($boxes[$a]);
136 }
137 }
138
139 sqimap_logout($imapConnection);
140 if ( !isset($theid) ) {
141 $theid = count($filters);
142 }
143 echo html_tag( 'div', '', 'center' ) .
144 '<form action="options.php" method="post">'.
145 html_tag( 'table', '', '', '', 'border="0" cellpadding="2" cellspacing="0"' ) .
146 html_tag( 'tr' ) .
147 html_tag( 'td', _("Match:"), 'left' ) .
148 html_tag( 'td', '', 'left' ) .
149 '<select name="filter_where">';
150
151 $L = isset($filters[$theid]['where']);
152
153 $sel = (($L && $filters[$theid]['where'] == 'From')?' selected="selected"':'');
154 echo "<option value=\"From\"$sel>" . _("From") . '</option>';
155
156 $sel = (($L && $filters[$theid]['where'] == 'To')?' selected="selected"':'');
157 echo "<option value=\"To\"$sel>" . _("To") . '</option>';
158
159 $sel = (($L && $filters[$theid]['where'] == 'Cc')?' selected="selected"':'');
160 echo "<option value=\"Cc\"$sel>" . _("Cc") . '</option>';
161
162 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?' selected="selected"':'');
163 echo "<option value=\"To or Cc\"$sel>" . _("To or Cc") . '</option>';
164
165 $sel = (($L && $filters[$theid]['where'] == 'Subject')?' selected="selected"':'');
166 echo "<option value=\"Subject\"$sel>" . _("Subject") . '</option>';
167
168 $sel = (($L && $filters[$theid]['where'] == 'Message Body')?' selected="selected"':'');
169 echo "<option value=\"Message Body\"$sel>" . _("Message Body") . '</option>';
170
171 $sel = (($L && $filters[$theid]['where'] == 'Header and Body')?' selected="selected"':'');
172 echo "<option value=\"Header and Body\"$sel>" . _("Header and Body") . '</option>';
173
174 $sel = (($L && $filters[$theid]['where'] == 'Header')?' selected="selected"':'');
175 echo "<option value=\"Header\"$sel>" . _("Header") . '</option>';
176
177 echo '</select>'.
178 '</td>'.
179 '</tr>'.
180 html_tag( 'tr' ) .
181 html_tag( 'td', _("Contains:"), 'right' ) .
182 html_tag( 'td', '', 'left' ) .
183 '<input type="text" size="32" name="filter_what" value="';
184 if (isset($filters[$theid]['what'])) {
185 echo htmlspecialchars($filters[$theid]['what']);
186 }
187 echo '" />'.
188 '</td>'.
189 '</tr>'.
190 html_tag( 'tr' ) .
191 html_tag( 'td', _("Move to:"), 'left' ) .
192 html_tag( 'td', '', 'left' ) .
193 '<tt>'.
194 '<select name="filter_folder">';
195 $selected = 0;
196 if ( isset($filters[$theid]['folder']) )
197 $selected = array(strtolower($filters[$theid]['folder']));
198 echo sqimap_mailbox_option_list(0, $selected, 0, $boxes);
199 echo '</select>'.
200 '</tt>'.
201 '</td>'.
202 '</tr>'.
203 '</table>'.
204 '<input type="submit" name="filter_submit" value="' . _("Submit") . "\" />\n".
205 addHidden('theid', $theid).
206 '</form>'.
207 '</div>';
208
209 }
210
211 if (count($filters)) {
212 echo html_tag( 'table', '', 'center', '', 'border="0" cellpadding="3" cellspacing="0"' );
213
214 for ($i=0, $num = count($filters); $i < $num; $i++) {
215
216 $clr = (($i % 2)?$color[0]:$color[9]);
217 $fdr = ($folder_prefix)?str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
218 echo html_tag( 'tr', '', '', $clr ) .
219 html_tag( 'td',
220 '<small>' .
221 "[<a href=\"options.php?theid=$i&amp;action=edit\">" . _("Edit") . '</a>]'.
222 '</small>' ,
223 'left' ) .
224 html_tag( 'td',
225 '<small>' .
226 "[<a href=\"options.php?theid=$i&amp;action=delete\">" . _("Delete") . '</a>]'.
227 '</small>' ,
228 'left' );
229
230 if ($num > 1) {
231 echo html_tag( 'td', '', 'center' ) . '<small>[';
232 if (isset($filters[$i + 1])) {
233 echo "<a href=\"options.php?theid=$i&amp;action=move_down\">" . _("Down") . '</a>';
234 if ($i > 0) {
235 echo '&nbsp;|&nbsp;';
236 }
237 }
238 if ($i > 0) {
239 echo "<a href=\"options.php?theid=$i&amp;action=move_up\">" . _("Up") . '</a>';
240 }
241 echo ']</small></td>';
242 }
243 echo html_tag( 'td', '-', 'left' ) .
244 html_tag( 'td', '', 'left' );
245 printf( _("If %s contains %s then move to %s"),
246 '<b>'.$filters[$i]['where'].'</b>',
247 '<b>'.$filters[$i]['what'].'</b>',
248 '<b>'.imap_utf7_decode_local($fdr).'</b>');
249 echo '</td></tr>';
250
251 }
252 echo '</table>';
253 }
254 echo html_tag( 'table',
255 html_tag( 'tr',
256 html_tag( 'td', '&nbsp;', 'left' )
257 ) ,
258 'center', '', 'width="80%" border="0" cellpadding="2" cellspacing="0"' );
259 echo '</body></html>';
260 ?>