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