initial commit, for roundcube v0.7.2-9
[roundcube/plugins/junk_keyword.git] / junk_keyword.php
1 <?php
2
3 class junk_keyword extends rcube_plugin {
4 public $task = 'mail';
5
6 function init() {
7 #write_log($this->name, "init: task=" . print_r($rcmail->task, true) . " action=" . print_r($rcmail->action, true));
8 $rcmail = rcmail::get_instance();
9 $this->name = get_class($this);
10 if (($rcmail->task == 'mail') && ($rcmail->action == '')) {
11 $this->add_hook('render_page', array($this, 'render_page'));
12 $this->include_script('junk_keyword.js');
13 }
14 $this->add_junk_flags = array
15 ( 'JUNK' => 'Junk'
16 , 'NONJUNK' => 'NonJunk'
17 );
18 $this->register_action('plugin.junk_keyword.set_keywords', array($this, 'set_keywords'));
19 $this->add_hook('messages_list', array($this, 'message_list'));
20 $this->add_texts('localization');
21 }
22 public function render_page($args) {
23 #write_log($this->name, "render_page: args=" . print_r($args, true));
24 $rcmail = rcmail::get_instance();
25 $icon = 'plugins/junk_keyword/' .$this->local_skin_path(). '/junk.png';
26 $junk_keywordicon = html::img(array
27 ( 'alt' => $this->gettext('junk_keyword')
28 , 'id' => 'junk_keyword_menulink'
29 , 'src' => $icon
30 , 'title' => $this->gettext('junk_keyword')));
31 $rcmail->output->add_label('junk_keyword.junk_keyword');
32 $rcmail->output->set_env('junk_keywordicon', $junk_keywordicon);
33 $this->include_stylesheet($this->local_skin_path(). '/junk.css');
34
35 return $args;
36 }
37 public function message_list($args) {
38 #write_log($this->name, "message_list: args=" . print_r($args, true));
39 if (!isset($args['messages']) or !is_array($args['messages']))
40 return $args;
41 foreach($args['messages'] as $message) {
42 #write_log($this->name, "message_list: foreach: begin: message=" . print_r($message, true));
43 $message->list_flags['extra_flags']['junk_keywords'] = array(); # always set extra_flags, needed for javascript later!
44 if (is_array($message->flags)) {
45 $uid = $message->uid;
46 $class = '';
47 foreach ($message->flags as $flagname => $flagvalue) {
48 $flag = is_numeric("$flagvalue") ? $flagname : $flagvalue; // for compatibility with < 0.5.4
49 $flag = strtolower($flag);
50 #write_log($this->name, "message_list: foreach: flag=" . print_r($flag, true));
51 $message->list_flags['extra_flags']['junk'] = false;
52 if (preg_match('/^junk$/', $flag)) {
53 $class = "junk";
54 $message->list_flags['extra_flags']['junk'] = true;
55 }
56 if (preg_match('/^nonjunk$/', $flag)) {
57 $class = "nonjunk";
58 $message->list_flags['extra_flags']['junk'] = false;
59 }
60 }
61 if (!empty($uid))
62 $message->list_cols['junk_keyword'] = '<div id="rcmjunk_keyword'.$uid.'" name="rcmjunk_keyword'.$uid.'" class="'.$class.'">&nbsp;</div>';
63 }
64 }
65 return $args;
66 }
67 public function set_keywords() {
68 #write_log($this->name, "set_keywords: _GET=" . print_r($_GET, true));
69
70 $rcmail = rcmail::get_instance();
71 $imap = $rcmail->imap;
72 //$cbox = get_input_value('_cur', RCUBE_INPUT_GET);
73 $mbox = get_input_value('_mbox', RCUBE_INPUT_GET);
74 $junk_uids = get_input_value('_junk_uids', RCUBE_INPUT_GET);
75 $junk_uids = array_filter(explode(',', $junk_uids));
76 $nonjunk_uids = get_input_value('_nonjunk_uids', RCUBE_INPUT_GET);
77 $nonjunk_uids = array_filter(explode(',', $nonjunk_uids));
78
79 $imap->conn->flags = array_merge($imap->conn->flags, $this->add_junk_flags);
80
81 if (!is_array($junk_uids) || !is_array($nonjunk_uids))
82 return false;
83 //write_log($this->name, "set_keywords: junk_uids=" . print_r($junk_uids, true));
84 //write_log($this->name, "set_keywords: nonjunk_uids=" . print_r($nonjunk_uids, true));
85
86 $error = "";
87 $imap->conn->error = "";
88 if (!empty($junk_uids)) {
89 if (!$imap->set_flag($junk_uids, 'SEEN', $mbox))
90 $error = "set SEEN: " . $imap->conn->error;
91 if (!$imap->unset_flag($junk_uids, 'NONJUNK', $mbox))
92 $error = "unset NONJUNK: " . $imap->conn->error;
93 if (!$imap->set_flag($junk_uids, 'JUNK', $mbox))
94 $error = "set JUNK: " . $imap->conn->error;
95 }
96 if (!empty($nonjunk_uids)) {
97 if (!$imap->unset_flag($nonjunk_uids, 'JUNK', $mbox))
98 $error = "unset JUNK: " . $imap->conn->error;
99 if (!$imap->set_flag($nonjunk_uids, 'NONJUNK', $mbox))
100 $error = "set NONJUNK: " . $imap->conn->error;
101 }
102 if (empty($error))
103 $rcmail->output->command('display_message', $this->gettext('classification_done'), 'confirmation');
104 else
105 $rcmail->output->command('display_message', $this->gettext('classification_failed'). ': ' . $error, 'error');
106 $this->api->output->send();
107 return empty($error);
108 }
109 }
110 ?>