4 +-----------------------------------------------------------------------+
5 | program/steps/settings/folders.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2009, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Provide functionality of folders management |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 | Author: Aleksander Machniak <alec@alec.pl> |
17 +-----------------------------------------------------------------------+
19 $Id: folders.inc 5635 2011-12-21 10:07:42Z alec $
23 // WARNING: folder names in UI are encoded with RCMAIL_CHARSET
25 // init IMAP connection
26 $RCMAIL->imap_connect();
29 if ($RCMAIL->action == 'subscribe')
31 $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
33 $result = $IMAP->subscribe(array($mbox));
35 // Handle virtual (non-existing) folders
36 if (!$result && $IMAP->get_error_code() == -1 &&
37 $IMAP->get_response_code() == rcube_imap::TRYCREATE
39 $result = $IMAP->create_mailbox($mbox, true);
41 // @TODO: remove 'virtual' class of folder's row
46 // Handle subscription of protected folder (#1487656)
47 if ($CONFIG['protect_default_folders'] == true
48 && in_array($mbox, $CONFIG['default_imap_folders'])
50 $OUTPUT->command('disable_subscription', $mbox);
53 $OUTPUT->show_message('foldersubscribed', 'confirmation');
56 rcmail_display_server_error('errorsaving');
60 // unsubscribe mailbox
61 else if ($RCMAIL->action == 'unsubscribe')
63 $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
65 $result = $IMAP->unsubscribe(array($mbox));
67 $OUTPUT->show_message('folderunsubscribed', 'confirmation');
69 rcmail_display_server_error('errorsaving');
73 // delete an existing mailbox
74 else if ($RCMAIL->action == 'delete-folder')
76 $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
77 $mbox = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
80 $plugin = $RCMAIL->plugins->exec_hook('folder_delete', array('name' => $mbox));
82 if (!$plugin['abort']) {
83 $deleted = $IMAP->delete_mailbox($plugin['name']);
86 $deleted = $plugin['result'];
90 if ($OUTPUT->ajax_call && $deleted) {
91 // Remove folder and subfolders rows
92 $OUTPUT->command('remove_folder_row', $mbox_utf8, true);
93 $OUTPUT->show_message('folderdeleted', 'confirmation');
94 // Clear content frame
95 $OUTPUT->command('subscription_select');
96 $OUTPUT->command('set_quota', rcmail_quota_content());
99 rcmail_display_server_error('errorsaving');
103 // rename an existing mailbox
104 else if ($RCMAIL->action == 'rename-folder')
106 $name_utf8 = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST, true));
107 $oldname_utf8 = trim(get_input_value('_folder_oldname', RCUBE_INPUT_POST, true));
109 if (strlen($name_utf8) && strlen($oldname_utf8)) {
110 $name = rcube_charset_convert($name_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
111 $oldname = rcube_charset_convert($oldname_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
113 $rename = rcmail_rename_folder($oldname, $name);
116 if ($rename && $OUTPUT->ajax_call) {
117 rcmail_update_folder_row($name, $oldname);
120 rcmail_display_server_error('errorsaving');
125 else if ($RCMAIL->action == 'purge')
127 $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
128 $mbox = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
129 $delimiter = $IMAP->get_hierarchy_delimiter();
130 $trash_regexp = '/^' . preg_quote($CONFIG['trash_mbox'] . $delimiter, '/') . '/';
132 // we should only be purging trash (or their subfolders)
133 if (!strlen($CONFIG['trash_mbox']) || $mbox == $CONFIG['trash_mbox']
134 || preg_match($trash_regexp, $mbox)
136 $success = $IMAP->clear_mailbox($mbox);
141 $success = $IMAP->move_message('1:*', $CONFIG['trash_mbox'], $mbox);
146 $OUTPUT->set_env('messagecount', 0);
148 $OUTPUT->show_message('folderpurged', 'confirmation');
149 $OUTPUT->command('set_quota', rcmail_quota_content());
152 $OUTPUT->show_message('messagemoved', 'confirmation');
154 $_SESSION['unseen_count'][$mbox] = 0;
155 $OUTPUT->command('show_folder', $mbox_utf8, null, true);
158 rcmail_display_server_error('errorsaving');
163 else if ($RCMAIL->action == 'folder-size')
165 $name = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true));
167 $size = $IMAP->get_mailbox_size($name);
169 // @TODO: check quota and show percentage usage of specified mailbox?
171 if ($size !== false) {
172 $OUTPUT->command('folder_size_update', show_bytes($size));
175 rcmail_display_server_error();
179 if ($OUTPUT->ajax_call)
183 // build table with all folders listed by server
184 function rcube_subscription_form($attrib)
186 global $RCMAIL, $IMAP, $CONFIG, $OUTPUT;
188 list($form_start, $form_end) = get_form_tags($attrib, 'folders');
189 unset($attrib['form']);
192 $attrib['id'] = 'rcmSubscriptionlist';
194 $table = new html_table();
196 if ($attrib['noheader'] !== true && $attrib['noheader'] != "true") {
198 $table->add_header('name', rcube_label('foldername'));
199 $table->add_header('subscribed', '');
202 // get folders from server
203 $IMAP->clear_cache('mailboxes', true);
205 $a_unsubscribed = $IMAP->list_unsubscribed();
206 $a_subscribed = $IMAP->list_mailboxes('', '*', null, null, true); // unsorted
207 $delimiter = $IMAP->get_hierarchy_delimiter();
208 $namespace = $IMAP->get_namespace();
209 $a_js_folders = array();
211 $list_folders = array();
213 // pre-process folders list
214 foreach ($a_unsubscribed as $i => $folder) {
215 $folder_id = $folder;
216 $folder = $IMAP->mod_mailbox($folder);
217 $foldersplit = explode($delimiter, $folder);
218 $name = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP');
219 $parent_folder = join($delimiter, $foldersplit);
220 $level = count($foldersplit);
222 // add any necessary "virtual" parent folders
223 if ($parent_folder && !isset($seen[$parent_folder])) {
224 for ($i=1; $i<=$level; $i++) {
225 $ancestor_folder = join($delimiter, array_slice($foldersplit, 0, $i));
226 if ($ancestor_folder && !$seen[$ancestor_folder]++) {
227 $ancestor_name = rcube_charset_convert($foldersplit[$i-1], 'UTF7-IMAP');
228 $list_folders[] = array(
229 'id' => $ancestor_folder,
230 'name' => $ancestor_name,
238 // Handle properly INBOX.INBOX situation
239 if (isset($seen[$folder])) {
245 $list_folders[] = array(
254 // add drop-target representing 'root'
255 $table->add_row(array('id' => 'mailboxroot', 'class' => 'virtual root'));
256 $table->add('name', ' ');
257 $table->add(null, ' ');
259 $a_js_folders['mailboxroot'] = array('', '', true);
261 $checkbox_subscribe = new html_checkbox(array(
262 'name' => '_subscribed[]',
263 'title' => rcube_label('changesubscription'),
264 'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)",
267 // create list of available folders
268 foreach ($list_folders as $i => $folder) {
270 $sub_key = array_search($folder['id'], $a_subscribed);
271 $subscribed = $sub_key !== false;
272 $protected = ($CONFIG['protect_default_folders'] == true && in_array($folder['id'], $CONFIG['default_imap_folders']));
274 $classes = array($i%2 ? 'even' : 'odd');
276 $folder_js = Q($folder['id']);
277 $folder_utf8 = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
278 $display_folder = str_repeat(' ', $folder['level'])
279 . Q($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
281 if ($folder['virtual']) {
282 $classes[] = 'virtual';
286 $attrs = $IMAP->mailbox_attributes($folder['id']);
287 $noselect = in_array('\\Noselect', $attrs);
290 $disabled = (($protected && $subscribed) || $noselect);
292 // check if the folder is a namespace prefix, then disable subscription option on it
293 if (!$disabled && $folder['virtual'] && $folder['level'] == 0 && !empty($namespace)) {
294 $fname = $folder['id'] . $delimiter;
295 foreach ($namespace as $ns) {
297 foreach ($ns as $item) {
298 if ($item[0] === $fname) {
306 // check if the folder is an other users virtual-root folder, then disable subscription option on it
307 if (!$disabled && $folder['virtual'] && $folder['level'] == 1
308 && !empty($namespace) && !empty($namespace['other'])
310 $parts = explode($delimiter, $folder['id']);
311 $fname = $parts[0] . $delimiter;
312 foreach ($namespace['other'] as $item) {
313 if ($item[0] === $fname) {
319 // check if the folder is shared, then disable subscription option on it
320 if (!$disabled && $folder['virtual'] && !empty($namespace)) {
321 $tmp_ns = array_merge((array)$namespace['other'], (array)$namespace['shared']);
322 foreach ($tmp_ns as $item) {
323 if (strpos($folder['id'], $item[0]) === 0) {
330 $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes),
331 'foldername' => $folder['id']));
333 $table->add('name', $display_folder);
334 $table->add('subscribed', $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''),
335 array('value' => $folder_utf8, 'disabled' => $disabled ? 'disabled' : '')));
337 $a_js_folders['rcmrow'.$idx] = array($folder_utf8,
338 Q($display_folder), $protected || $folder['virtual']);
341 $RCMAIL->plugins->exec_hook('folders_list', array('table' => $table));
343 $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
344 $OUTPUT->set_env('subscriptionrows', $a_js_folders);
345 $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
346 $OUTPUT->set_env('delimiter', $delimiter);
348 return $form_start . $table->show($attrib) . $form_end;
351 function rcmail_folder_frame($attrib)
356 $attrib['id'] = 'rcmfolderframe';
358 $attrib['name'] = $attrib['id'];
360 $OUTPUT->set_env('contentframe', $attrib['name']);
361 $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
363 return html::iframe($attrib);
366 function rcmail_rename_folder($oldname, $newname)
370 $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
372 $plugin = $RCMAIL->plugins->exec_hook('folder_rename', array(
373 'oldname' => $oldname, 'newname' => $newname));
375 if (!$plugin['abort']) {
376 $renamed = $RCMAIL->imap->rename_mailbox($oldname, $newname);
379 $renamed = $plugin['result'];
382 // update per-folder options for modified folder and its subfolders
384 $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
385 $oldprefix = '/^' . preg_quote($oldname . $delimiter, '/') . '/';
387 foreach ($a_threaded as $key => $val) {
388 if ($key == $oldname) {
389 unset($a_threaded[$key]);
390 $a_threaded[$newname] = true;
392 else if (preg_match($oldprefix, $key)) {
393 unset($a_threaded[$key]);
394 $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = true;
397 $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
406 $OUTPUT->set_pagetitle(rcube_label('folders'));
407 $OUTPUT->include_script('list.js');
408 $OUTPUT->set_env('quota', $IMAP->get_capability('QUOTA'));
410 // add some labels to client
411 $OUTPUT->add_label('deletefolderconfirm', 'purgefolderconfirm', 'folderdeleting',
412 'foldermoving', 'foldersubscribing', 'folderunsubscribing', 'quota');
414 // register UI objects
415 $OUTPUT->add_handlers(array(
416 'foldersubscription' => 'rcube_subscription_form',
417 'folderframe' => 'rcmail_folder_frame',
418 'quotadisplay' => 'rcmail_quota_display',
421 $OUTPUT->send('folders');