comparison plugins/managesieve/managesieve.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children 082a19037887
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /**
4 * Managesieve (Sieve Filters)
5 *
6 * Plugin that adds a possibility to manage Sieve filters in Thunderbird's style.
7 * It's clickable interface which operates on text scripts and communicates
8 * with server using managesieve protocol. Adds Filters tab in Settings.
9 *
10 * @author Aleksander Machniak <alec@alec.pl>
11 *
12 * Configuration (see config.inc.php.dist)
13 *
14 * Copyright (C) 2008-2013, The Roundcube Dev Team
15 * Copyright (C) 2011-2013, Kolab Systems AG
16 *
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see http://www.gnu.org/licenses/.
29 */
30
31 class managesieve extends rcube_plugin
32 {
33 public $task = 'mail|settings';
34 private $rc;
35 private $engine;
36
37 function init()
38 {
39 $this->rc = rcube::get_instance();
40
41 // register actions
42 $this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
43 $this->register_action('plugin.managesieve-action', array($this, 'managesieve_actions'));
44 $this->register_action('plugin.managesieve-vacation', array($this, 'managesieve_actions'));
45 $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
46 $this->register_action('plugin.managesieve-saveraw', array($this, 'managesieve_saveraw'));
47
48 if ($this->rc->task == 'settings') {
49 $this->add_hook('settings_actions', array($this, 'settings_actions'));
50 $this->init_ui();
51 }
52 else if ($this->rc->task == 'mail') {
53 // register message hook
54 if ($this->rc->action == 'show') {
55 $this->add_hook('message_headers_output', array($this, 'mail_headers'));
56 }
57
58 // inject Create Filter popup stuff
59 if (empty($this->rc->action) || $this->rc->action == 'show'
60 || strpos($this->rc->action, 'plugin.managesieve') === 0
61 ) {
62 $this->mail_task_handler();
63 }
64 }
65 }
66
67 /**
68 * Initializes plugin's UI (localization, js script)
69 */
70 function init_ui()
71 {
72 if ($this->ui_initialized) {
73 return;
74 }
75
76 // load localization
77 $this->add_texts('localization/');
78
79 $sieve_action = strpos($this->rc->action, 'plugin.managesieve') === 0;
80
81 if ($this->rc->task == 'mail' || $sieve_action) {
82 $this->include_script('managesieve.js');
83 }
84
85 // include styles
86 $skin_path = $this->local_skin_path();
87 if ($sieve_action || ($this->rc->task == 'settings' && empty($_REQUEST['_framed']))) {
88 $this->include_stylesheet("$skin_path/managesieve.css");
89 }
90 else if ($this->rc->task == 'mail') {
91 $this->include_stylesheet("$skin_path/managesieve_mail.css");
92 }
93
94 $this->ui_initialized = true;
95 }
96
97 /**
98 * Adds Filters section in Settings
99 */
100 function settings_actions($args)
101 {
102 $this->load_config();
103
104 $vacation_mode = (int) $this->rc->config->get('managesieve_vacation');
105
106 // register Filters action
107 if ($vacation_mode != 2) {
108 $args['actions'][] = array(
109 'action' => 'plugin.managesieve',
110 'class' => 'filter',
111 'label' => 'filters',
112 'domain' => 'managesieve',
113 'title' => 'filterstitle',
114 );
115 }
116
117 // register Vacation action
118 if ($vacation_mode > 0) {
119 $args['actions'][] = array(
120 'action' => 'plugin.managesieve-vacation',
121 'class' => 'vacation',
122 'label' => 'vacation',
123 'domain' => 'managesieve',
124 'title' => 'vacationtitle',
125 );
126 }
127
128 return $args;
129 }
130
131 /**
132 * Add UI elements to the 'mailbox view' and 'show message' UI.
133 */
134 function mail_task_handler()
135 {
136 // make sure we're not in ajax request
137 if ($this->rc->output->type != 'html') {
138 return;
139 }
140
141 // use jQuery for popup window
142 $this->require_plugin('jqueryui');
143
144 // include js script and localization
145 $this->init_ui();
146
147 // add 'Create filter' item to message menu
148 $this->api->add_content(html::tag('li', null,
149 $this->api->output->button(array(
150 'command' => 'managesieve-create',
151 'label' => 'managesieve.filtercreate',
152 'type' => 'link',
153 'classact' => 'icon filterlink active',
154 'class' => 'icon filterlink',
155 'innerclass' => 'icon filterlink',
156 ))), 'messagemenu');
157
158 // register some labels/messages
159 $this->rc->output->add_label('managesieve.newfilter', 'managesieve.usedata',
160 'managesieve.nodata', 'managesieve.nextstep', 'save');
161
162 $this->rc->session->remove('managesieve_current');
163 }
164
165 /**
166 * Get message headers for popup window
167 */
168 function mail_headers($args)
169 {
170 // this hook can be executed many times
171 if ($this->mail_headers_done) {
172 return $args;
173 }
174
175 $this->mail_headers_done = true;
176
177 $headers = $this->parse_headers($args['headers']);
178
179 if ($this->rc->action == 'preview')
180 $this->rc->output->command('parent.set_env', array('sieve_headers' => $headers));
181 else
182 $this->rc->output->set_env('sieve_headers', $headers);
183
184 return $args;
185 }
186
187 /**
188 * Plugin action handler
189 */
190 function managesieve_actions()
191 {
192 // handle fetching email headers for the new filter form
193 if ($uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST)) {
194 $uids = rcmail::get_uids();
195 $mailbox = key($uids);
196 $message = new rcube_message($uids[$mailbox][0], $mailbox);
197 $headers = $this->parse_headers($message->headers);
198
199 $this->rc->output->set_env('sieve_headers', $headers);
200 $this->rc->output->command('managesieve_create', true);
201 $this->rc->output->send();
202 }
203
204 // handle other actions
205 $engine_type = $this->rc->action == 'plugin.managesieve-vacation' ? 'vacation' : '';
206 $engine = $this->get_engine($engine_type);
207
208 $this->init_ui();
209 $engine->actions();
210 }
211
212 /**
213 * Forms save action handler
214 */
215 function managesieve_save()
216 {
217 // load localization
218 $this->add_texts('localization/', array('filters','managefilters'));
219
220 // include main js script
221 if ($this->api->output->type == 'html') {
222 $this->include_script('managesieve.js');
223 }
224
225 $engine = $this->get_engine();
226 $engine->save();
227 }
228
229 /**
230 * Raw form save action handler
231 */
232 function managesieve_saveraw()
233 {
234 $engine = $this->get_engine();
235
236 if (!$this->rc->config->get('managesieve_raw_editor', true)) {
237 return;
238 }
239
240 // load localization
241 $this->add_texts('localization/', array('filters','managefilters'));
242
243 $engine->saveraw();
244 }
245
246 /**
247 * Initializes engine object
248 */
249 public function get_engine($type = null)
250 {
251 if (!$this->engine) {
252 $this->load_config();
253
254 // Add include path for internal classes
255 $include_path = $this->home . '/lib' . PATH_SEPARATOR;
256 $include_path .= ini_get('include_path');
257 set_include_path($include_path);
258
259 $class_name = 'rcube_sieve_' . ($type ?: 'engine');
260 $this->engine = new $class_name($this);
261 }
262
263 return $this->engine;
264 }
265
266 /**
267 * Extract mail headers for new filter form
268 */
269 private function parse_headers($headers)
270 {
271 $result = array();
272
273 if ($headers->subject)
274 $result[] = array('Subject', rcube_mime::decode_header($headers->subject));
275
276 // @TODO: List-Id, others?
277 foreach (array('From', 'To') as $h) {
278 $hl = strtolower($h);
279 if ($headers->$hl) {
280 $list = rcube_mime::decode_address_list($headers->$hl);
281 foreach ($list as $item) {
282 if ($item['mailto']) {
283 $result[] = array($h, $item['mailto']);
284 }
285 }
286 }
287 }
288
289 return $result;
290 }
291 }