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