-
Notifications
You must be signed in to change notification settings - Fork 35
/
filters.php
440 lines (375 loc) · 15.6 KB
/
filters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* Filters
*
* Plugin that adds a new tab to the settings section to create client-side e-mail filtering.
*
* @version 2.1.4
* @author Roberto Zarrelli <[email protected]>
* @developer Artur Petrov <[email protected]>
*/
class filters extends rcube_plugin{
public $task = 'login|mail|settings';
private $autoAddSpamFilterRule;
private $spam_subject;
private $caseInsensitiveSearch;
private $decodeBase64Msg;
private $searchstring = array();
private $destfolder = array();
private $msg_uids = array();
private $open_mbox;
function init(){
/* Filters parameters initialization. See readme.txt */
$this->autoAddSpamFilterRule = TRUE;
$this->spam_subject = '[SPAM]';
$this->caseInsensitiveSearch = TRUE;
$this->decodeBase64Msg = FALSE;
/* ***************************************************** */
$rcmail = rcmail::get_instance();
if($rcmail->task == 'mail')
$this->add_hook('messages_list', array($this, 'filters_checkmsg'));
else if ($rcmail->task == 'settings'){
$this->register_action('plugin.filters', array($this, 'filters_init'));
$this->register_action('plugin.filters-save', array($this, 'filters_save'));
$this->register_action('plugin.filters-delete', array($this, 'filters_delete'));
$this->add_texts('localization/', array('filters','nosearchstring'));
$rcmail->output->add_label('filters');
$this->include_script('filters.js');
}
else if ($rcmail->task == 'login'){
if ($this->autoAddSpamFilterRule)
$this->add_hook('login_after', array($this, 'filters_addMoveSpamRule'));
}
}
function filters_checkmsg($mlist){
$rcmail = rcmail::get_instance();
$user = $rcmail->user;
$imap = $rcmail->imap;
$open_mbox = $imap->get_mailbox_name();
$this->open_mbox=$open_mbox;
// does not consider the messages already in the trash
if ($open_mbox == $rcmail->config->get('trash_mbox'))
return;
//load filters
$arr_prefs = $rcmail->config->get('filters', array());
foreach ($arr_prefs as $key => $saved_filter){
// if saved destination folder exists and current folder is INBOX
if ($imap->mailbox_exists($saved_filter['destfolder']) && 'INBOX'==$open_mbox){
$saved_filter['searchstring'] = html_entity_decode($saved_filter['searchstring']);
// if (!isset($saved_filter['filterpriority'])) $saved_filter['filterpriority'] = '';
// destfolder#messages#filterpriority#markread
$this->searchstring[ $saved_filter['whatfilter'] ][ $saved_filter['searchstring'] ] =
$saved_filter['destfolder']."#".$saved_filter['messages']."#".$saved_filter['filterpriority']."#".$saved_filter['markread'];
}
}
// if there aren't filters return
if(!count($arr_prefs) || !count($this->searchstring) || !isset($mlist['messages']) || !is_array($mlist['messages']))
return;
// scan the messages
foreach($mlist["messages"] as $message){
$this->filters_search($message);
}
// move the filtered messages
if (count($this->destfolder) > 0){
foreach ($this->destfolder as $dfolder){
$uids = array();
foreach ($this->msg_uids[$dfolder] as $muids){
$uids[] = $muids;
}
if (count($uids)){
$imap->move_message($uids, $dfolder, $open_mbox);
// refresh
$unseen = $rcmail->storage->count($dfolder, 'UNSEEN');
$this->api->output->command('set_unread_count',$dfolder, $unseen);
$this->api->output->command('list_mailbox');
$this->api->output->send();
}
}
}
}
function filters_init(){
$this->add_texts('localization/');
$this->register_handler('plugin.body', array($this, 'filters_form'));
$rcmail = rcmail::get_instance();
$rcmail->output->set_pagetitle($this->gettext('filters'));
$rcmail->output->send('plugin');
}
function filters_save(){
$rcmail = rcmail::get_instance();
$user = $rcmail->user;
$this->add_texts('localization/');
$this->register_handler('plugin.body', array($this, 'filters_form'));
$rcmail->output->set_pagetitle($this->gettext('filters'));
$searchstring = trim(get_input_value('_searchstring', RCUBE_INPUT_POST, true));
$destfolder = trim(get_input_value('_folders', RCUBE_INPUT_POST, true));
$whatfilter = trim(get_input_value('_whatfilter', RCUBE_INPUT_POST, true));
$messages = trim(get_input_value('_messages', RCUBE_INPUT_POST, true));
$filterpriority = trim(get_input_value('_checkbox', RCUBE_INPUT_POST, true));
$markread = trim(get_input_value('_markread', RCUBE_INPUT_POST, true));
if ($searchstring == "")
$rcmail->output->command('display_message', $this->gettext('nosearchstring'), 'error');
else{
$new_arr['whatfilter'] = $whatfilter;
$new_arr['searchstring'] = htmlspecialchars(addslashes($searchstring));
$new_arr['destfolder'] = addslashes($destfolder);
$new_arr['messages'] = $messages;
$new_arr['filterpriority'] = $filterpriority;
$new_arr['markread'] = $markread;
$arr_prefs = $user->get_prefs();
$arr_prefs['filters'][] = $new_arr;
if ($user->save_prefs($arr_prefs))
$rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
else
$rcmail->output->command('display_message', $this->gettext('unsuccessfullysaved'), 'error');
}
rcmail_overwrite_action('plugin.filters');
$rcmail->output->send('plugin');
}
function filters_delete(){
$rcmail = rcmail::get_instance();
$user = $rcmail->user;
$this->add_texts('localization/');
$this->register_handler('plugin.body', array($this, 'filters_form'));
$rcmail->output->set_pagetitle($this->gettext('filters'));
if (isset($_GET[filterid])){
$filter_id = $_GET[filterid];
$arr_prefs = $user->get_prefs();
$arr_prefs['filters'][$filter_id] = '';
$arr_prefs['filters'] = array_diff($arr_prefs['filters'], array(''));
if ($user->save_prefs($arr_prefs))
$rcmail->output->command('display_message', $this->gettext('successfullydeleted'), 'confirmation');
else
$rcmail->output->command('display_message', $this->gettext('unsuccessfullydeleted'), 'error');
}
rcmail_overwrite_action('plugin.filters');
$rcmail->output->send('plugin');
}
// fix for compare strings
// исправление для сравнения строк
function imap_mime_header_decode_fix($value) {
$text='';
$elements = imap_mime_header_decode($value);
for ($i=0; $i<count($elements); $i++) {
if ($elements[$i]->charset != 'default')
$elements[$i]->text = iconv ($elements[$i]->charset, "UTF-8", $elements[$i]->text);
$text .= $elements[$i]->text;
}
return $text;
}
function filters_form(){
$rcmail = rcmail::get_instance();
$rcmail->imap_connect();
$table = new html_table(array('cols' => 2));
$table->add('title', Q($this->gettext('whatfilter').":"));
$select = new html_select(array('name' => '_whatfilter', 'id' => 'whatfilter'));
$select->add($this->gettext('from'), 'from');
$select->add($this->gettext('to'), 'to');
$select->add($this->gettext('cc'), 'cc');
$select->add($this->gettext('subject'), 'subject');
$table->add('', $select->show($this->gettext('from')));
$table->add('title', Q($this->gettext('searchstring').":"));
$inputfield = new html_inputfield(array('name' => '_searchstring', 'id' => 'searchstring'));
$table->add('', $inputfield->show(""));
$table->add('title', Q($this->gettext('moveto').":"));
$select = rcmail_mailbox_select(array('name' => '_folders', 'id' => 'folders'));
$table->add('title', $select->show());
# new option: all, read and unread messages
$table->add('title', Q($this->gettext('messagecount').":"));
$select = new html_select(array('name' => '_messages', 'id' => 'messages'));
$select->add($this->gettext('all'), 'all');
$select->add($this->gettext('unread'), 'unread');
$select->add($this->gettext('isread'), 'isread');
$table->add('', $select->show($this->gettext('all')));
# new option: markread or markunread messages
$table->add('title', Q($this->gettext('markmessages').":"));
$select = new html_select(array('name' => '_markread', 'id' => 'markread'));
$select->add($this->gettext('none'), 'none');
$select->add($this->gettext('markunread'), 'markunread');
$select->add($this->gettext('markread'), 'markread');
$table->add('', $select->show($this->gettext('none')));
# new option: filter priority, "on" as enable and "" as disable
$table->add('title', Q($this->gettext('filterpriority').":"));
$checkbox = new html_checkbox(array('name' => '_checkbox', 'id' => 'checkbox'));
$table->add('', $checkbox->show("0"));
// load saved filters
$user = $rcmail->user;
$arr_prefs = $user->get_prefs();
$i = 1;
$flag=false;
$table2 = new html_table(array('cols' => 2));
foreach ($arr_prefs['filters'] as $key => $saved_filter){
$flag=true;
if (empty($saved_filter['markread'])) $saved_filter['markread'] = 'none';
$folder_id = $saved_filter['destfolder'];
$folder_name = rcmail_localize_folderpath($folder_id);
$messages = $saved_filter['messages'];
$msg = $i." - ".$this->gettext('msg_if_field')." <b>".$this->gettext($saved_filter['whatfilter'])."</b> ".$this->gettext('msg_contains').
" <b>".stripslashes($saved_filter['searchstring'])."</b> ".
$this->gettext('msg_move_msg_in')." <b>".$folder_name."</b> ".
"(".$this->gettext('messagecount').": ".$this->gettext($saved_filter['messages']).
", ".$this->gettext('mark').": ".$this->gettext($saved_filter['markread']).")";
if ( !empty($saved_filter['filterpriority']))
$msg = "<font color='green'>".$msg."</font>";
$table2->add('title',$msg);
$dlink = "<a href='./?_task=settings&_action=plugin.filters-delete&filterid=".$key."'>".$this->gettext('delete')."</a>";
$table2->add('title',$dlink);
$i++;
}
if (!$flag){
$table2->add('title',Q($this->gettext('msg_no_stored_filters')));
}
$out = html::div(array('class' => 'box'),
html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('filters')) .
html::div(array('class' => 'boxcontent'), $table->show() .
html::p(null,
$rcmail->output->button(array(
'command' => 'plugin.filters-save',
'type' => 'input',
'class' => 'button mainaction',
'label' => 'save'
)))));
$out.= html::div(array('id' => 'prefs-title','class' => 'boxtitle'), $this->gettext('storedfilters')). html::div(array('class' => 'uibox listbox scroller','style'=>'margin-top:205px;'),
html::div(array('class' => 'boxcontent'), $table2->show() ));
$rcmail->output->add_gui_object('filtersform', 'filters-form');
return $rcmail->output->form_tag(array(
'id' => 'filters-form',
'name' => 'filters-form',
'method' => 'post',
'class' => 'propform',
'action' => './?_task=settings&_action=plugin.filters-save',
), $out);
}
function filters_search($message){
// check if a message has been read
if (isset($message->flags['SEEN']) && $message->flags['SEEN'])
$msg_read = 1;
$headers = array('from','to','cc','subject');
$destination_folder = '';
$filter_flag = '';
$mark_flag = '';
foreach($headers as $whatfilter){
if (isset($this->searchstring[$whatfilter])){
foreach ($this->searchstring[$whatfilter] as $from => $dest){
$arr = explode("#",$dest);
$destination = $arr[0];
$msg_filter = $arr[1];
$filterpriority = $arr[2];
$markread = $arr[3];
switch ($whatfilter){
case 'from':
$field = $message->from;
break;
case 'to':
$field = $message->to;
break;
case 'cc':
$field = $message->cc;
break;
case 'subject':
$field = $message->subject;
break;
default:
$field = "";
}
if ($this->filters_searchString($field, $from) != false && $destination!=$this->open_mbox){
if (!empty($filterpriority)){
$destination_folder = $destination;
$filter_flag = $msg_filter;
$mark_flag = $markread;
break 2;
}
if (empty($destination_folder)){
$destination_folder = $destination;
$filter_flag = $msg_filter;
$mark_flag = $markread;
}
}
}
}
}
if (!empty($destination_folder)){
// if message as read and need unread message, then exit from function
// Если сообщение как прочитанное и нужно непрочитанное сообщение, то выход из функции
if (!empty($msg_read) && $filter_flag == "unread") return;
// if message as unread and need read message, then exit from function
// Если сообщение как непрочитанное и нужно прочитанное сообщение, то выход из функции
if (empty($msg_read) && $filter_flag == "isread") return;
$this->msg_uids[$destination_folder][] = $message->uid;
if (!in_array($destination_folder, $this->destfolder)) $this->destfolder[] = $destination_folder;
// Mark message as read if need mark message as read
// Отметить сообщение как прочитанное
if ($mark_flag == "markread") $this->filters_markread($message);
// Mark message as unread if need mark message as unread
// Отметить сообщение как непрочитанное
if ($mark_flag == "markunread") $this->filters_markread($message,'UNSEEN');
}
}
// Mark message as read (SEEN) or as unread (UNSEEN)
function filters_markread($message,$markread='SEEN'){
$rcmail = rcmail::get_instance();
$storage = $rcmail->storage;
$storage->set_flag($message->uid, $markread, NULL);
}
function filters_searchString($msg,$stringToSearch){
$ret = FALSE;
$ciSearch = $this->caseInsensitiveSearch;
$decode_msg = $this->imap_mime_header_decode_fix($msg);
$stringToSearch=stripslashes($stringToSearch);
if ($ciSearch){
$tmp = mb_stripos($decode_msg, $stringToSearch);
}
else{
$tmp = mb_strpos($decode_msg, $stringToSearch);
}
if ($tmp !== FALSE){
$ret = TRUE;
}
/*
else{
if ($this->decodeBase64Msg === TRUE){
// decode and search BASE64 msg
$decoded_str = $this->imap_mime_header_decode_fix(base64_decode($msg));
if ($decoded_str !== FALSE){
if ($ciSearch){
$tmp = mb_stripos($decoded_str, $stringToSearch, 0, 'utf8');
}
else{
$tmp = mb_strpos($decoded_str, $stringToSearch, 0, 'utf8');
}
if ($tmp !== FALSE){
$ret = TRUE;
}
}
}
}
*/
return $ret;
}
function filters_addMoveSpamRule(){
$rcmail = rcmail::get_instance();
$user = $rcmail->user;
$searchstring = $this->spam_subject;
$destfolder = $rcmail->config->get('junk_mbox', null);
$whatfilter = "subject";
$messages = "all";
//load filters
$arr_prefs = $rcmail->config->get('filters', array());
// check if the rule is already enabled
$found = false;
foreach ($arr_prefs as $key => $saved_filter){
if ($saved_filter['searchstring'] == $searchstring && $saved_filter['whatfilter'] == $whatfilter){
$found = true;
}
}
if (!$found && $destfolder !== null && $destfolder !== ""){
$new_arr['whatfilter'] = $whatfilter;
$new_arr['searchstring'] = $searchstring;
$new_arr['destfolder'] = $destfolder;
$new_arr['messages'] = $messages;
$arr_prefs = $user->get_prefs();
$arr_prefs['filters'][] = $new_arr;
$user->save_prefs($arr_prefs);
}
}
}
?>