4 +-----------------------------------------------------------------------+
5 | program/steps/settings/folders.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland |
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 4321 2010-12-08 12:52:04Z 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 $OUTPUT->show_message('foldersubscribed', 'confirmation');
48 rcmail_display_server_error('errorsaving');
52 // unsubscribe mailbox
53 else if ($RCMAIL->action == 'unsubscribe')
55 $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
57 $result = $IMAP->unsubscribe(array($mbox));
59 $OUTPUT->show_message('folderunsubscribed', 'confirmation');
61 rcmail_display_server_error('errorsaving');
65 // delete an existing mailbox
66 else if ($RCMAIL->action == 'delete-folder')
68 $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
69 $mbox = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
71 // get folder's children or all folders if the name contains special characters
72 $delimiter = $IMAP->get_hierarchy_delimiter();
73 if ((strpos($mbox, '%') === false) && (strpos($mbox, '*') === false))
74 $a_mboxes = $IMAP->list_unsubscribed('', $mbox.$delimiter.'*');
76 $a_mboxes = $IMAP->list_unsubscribed();
79 $deleted = $IMAP->delete_mailbox($mbox);
81 if ($OUTPUT->ajax_call && $deleted) {
82 // Remove folder and subfolders rows
83 $OUTPUT->command('remove_folder_row', $mbox_utf8);
84 foreach ($a_mboxes as $folder) {
85 if (preg_match('/^'. preg_quote($mbox.$delimiter, '/') .'/', $folder)) {
86 $OUTPUT->command('remove_folder_row', rcube_charset_convert($folder, 'UTF7-IMAP'));
89 $OUTPUT->show_message('folderdeleted', 'confirmation');
90 // Clear content frame
91 $OUTPUT->command('subscription_select');
92 $OUTPUT->command('set_quota', rcmail_quota_content());
95 rcmail_display_server_error('errorsaving');
99 // rename an existing mailbox
100 else if ($RCMAIL->action == 'rename-folder')
102 $name_utf8 = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST, true));
103 $oldname_utf8 = trim(get_input_value('_folder_oldname', RCUBE_INPUT_POST, true));
105 if (strlen($name_utf8) && strlen($oldname_utf8)) {
106 $name = rcube_charset_convert($name_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
107 $oldname = rcube_charset_convert($oldname_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
109 $rename = rcmail_rename_folder($oldname, $name);
112 if ($rename && $OUTPUT->ajax_call) {
113 $folderlist = $IMAP->list_unsubscribed();
114 $delimiter = $IMAP->get_hierarchy_delimiter();
116 $regexp = '/^' . preg_quote($name . $delimiter, '/') . '/';
119 for ($x=sizeof($folderlist)-1; $x>=0; $x--) {
120 if (preg_match($regexp, $folderlist[$x])) {
121 $oldfolder = $oldname . $delimiter . preg_replace($regexp, '', $folderlist[$x]);
122 $foldersplit = explode($delimiter, $folderlist[$x]);
123 $level = count($foldersplit) - 1;
124 $display_rename = str_repeat(' ', $level)
125 . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
127 $before = isset($folderlist[$x+1]) ? rcube_charset_convert($folderlist[$x+1], 'UTF7-IMAP') : false;
129 $OUTPUT->command('replace_folder_row', rcube_charset_convert($oldfolder, 'UTF7-IMAP'),
130 rcube_charset_convert($folderlist[$x], 'UTF7-IMAP'), $display_rename, $before);
134 $foldersplit = explode($delimiter, $name);
135 $level = count($foldersplit) - 1;
136 $display_rename = str_repeat(' ', $level) . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
137 $index = array_search($name, $folderlist);
138 $before = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF7-IMAP') : false;
140 $OUTPUT->command('replace_folder_row', $oldname_utf8,
141 rcube_charset_convert($name, 'UTF7-IMAP'), $display_rename, $before);
144 rcmail_display_server_error('errorsaving');
149 else if ($RCMAIL->action == 'purge')
151 $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
152 $mbox = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
153 $delimiter = $IMAP->get_hierarchy_delimiter();
154 $trash_regexp = '/^' . preg_quote($CONFIG['trash_mbox'] . $delimiter, '/') . '/';
156 // we should only be purging trash (or their subfolders)
157 if (!strlen($CONFIG['trash_mbox']) || $mbox == $CONFIG['trash_mbox']
158 || preg_match($trash_regexp, $mbox)
160 $success = $IMAP->clear_mailbox($mbox);
165 $success = $IMAP->move_message('1:*', $CONFIG['trash_mbox'], $mbox);
170 $OUTPUT->set_env('messagecount', 0);
172 $OUTPUT->show_message('folderpurged', 'confirmation');
173 $OUTPUT->command('set_quota', rcmail_quota_content());
176 $OUTPUT->show_message('messagemoved', 'confirmation');
178 $_SESSION['unseen_count'][$mbox] = 0;
179 $OUTPUT->command('show_folder', $mbox_utf8, null, true);
182 rcmail_display_server_error('errorsaving');
187 else if ($RCMAIL->action == 'folder-size')
189 $name = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true));
191 $size = $IMAP->get_mailbox_size($name);
193 // @TODO: check quota and show percentage usage of specified mailbox?
195 if ($size !== false) {
196 $OUTPUT->command('folder_size_update', show_bytes($size));
199 rcmail_display_server_error();
203 if ($OUTPUT->ajax_call)
207 // build table with all folders listed by server
208 function rcube_subscription_form($attrib)
210 global $RCMAIL, $IMAP, $CONFIG, $OUTPUT;
212 list($form_start, $form_end) = get_form_tags($attrib, 'folders');
213 unset($attrib['form']);
216 $attrib['id'] = 'rcmSubscriptionlist';
218 $table = new html_table();
220 if ($attrib['noheader'] !== true && $attrib['noheader'] != "true") {
222 $table->add_header('name', rcube_label('foldername'));
223 $table->add_header('subscribed', '');
226 // get folders from server
227 $IMAP->clear_cache('mailboxes');
229 $a_unsubscribed = $IMAP->list_unsubscribed();
230 $a_subscribed = $IMAP->list_mailboxes();
231 $delimiter = $IMAP->get_hierarchy_delimiter();
232 $a_js_folders = array();
234 $list_folders = array();
236 // pre-process folders list
237 foreach ($a_unsubscribed as $i => $folder) {
238 $foldersplit = explode($delimiter, $folder);
239 $name = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP');
240 $parent_folder = join($delimiter, $foldersplit);
241 $level = count($foldersplit);
243 // add any necessary "virtual" parent folders
244 if ($parent_folder && !$seen[$parent_folder]) {
245 for ($i=1; $i<=$level; $i++) {
246 $ancestor_folder = join($delimiter, array_slice($foldersplit, 0, $i));
247 if ($ancestor_folder && !$seen[$ancestor_folder]++) {
248 $ancestor_name = rcube_charset_convert($foldersplit[$i-1], 'UTF7-IMAP');
249 $list_folders[] = array(
250 'id' => $ancestor_folder,
251 'name' => $ancestor_name,
261 $list_folders[] = array(
270 $checkbox_subscribe = new html_checkbox(array(
271 'name' => '_subscribed[]',
272 'title' => rcube_label('changesubscription'),
273 'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)",
276 // create list of available folders
277 foreach ($list_folders as $i => $folder) {
279 $subscribed = in_array($folder['id'], $a_subscribed);
280 $protected = ($CONFIG['protect_default_folders'] == true && in_array($folder['id'], $CONFIG['default_imap_folders']));
281 $classes = array($i%2 ? 'even' : 'odd');
283 $folder_js = Q($folder['id']);
284 $folder_utf8 = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
285 $display_folder = str_repeat(' ', $folder['level'])
286 . Q($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
288 if ($folder['virtual']) {
289 $classes[] = 'virtual';
293 $opts = $IMAP->mailbox_options($folder['id']);
294 $noselect = in_array('\\Noselect', $opts);
297 $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes)));
299 $table->add('name', $display_folder);
300 $table->add('subscribed', $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''),
301 array('value' => $folder_utf8, 'disabled' => ($protected || $noselect) ? 'disabled' : '')));
303 $a_js_folders['rcmrow'.$idx] = array($folder_utf8, Q($display_folder), $protected || $folder['virtual']);
306 $RCMAIL->plugins->exec_hook('folders_list', array('table' => $table));
308 $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
309 $OUTPUT->set_env('subscriptionrows', $a_js_folders);
310 $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
311 $OUTPUT->set_env('delimiter', $delimiter);
313 return $form_start . $table->show($attrib) . $form_end;
316 function rcmail_folder_frame($attrib)
321 $attrib['id'] = 'rcmfolderframe';
323 $attrib['name'] = $attrib['id'];
325 $OUTPUT->set_env('contentframe', $attrib['name']);
326 $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
328 return html::iframe($attrib);
331 function rcmail_rename_folder($oldname, $newname)
335 $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
336 $rename = $RCMAIL->imap->rename_mailbox($oldname, $newname);
338 // update per-folder options for modified folder and its subfolders
339 if ($rename !== false) {
340 $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
341 $oldprefix = '/^' . preg_quote($oldname . $delimiter, '/') . '/';
343 foreach ($a_threaded as $key => $val) {
344 if ($key == $oldname) {
345 unset($a_threaded[$key]);
346 $a_threaded[$newname] = true;
348 else if (preg_match($oldprefix, $key)) {
349 unset($a_threaded[$key]);
350 $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = true;
353 $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
361 $OUTPUT->set_pagetitle(rcube_label('folders'));
362 $OUTPUT->include_script('list.js');
363 $OUTPUT->set_env('quota', $IMAP->get_capability('QUOTA'));
365 // add some labels to client
366 $OUTPUT->add_label('deletefolderconfirm', 'purgefolderconfirm', 'folderdeleting',
367 'foldermoving', 'foldersubscribing', 'folderunsubscribing', 'quota');
369 // register UI objects
370 $OUTPUT->add_handlers(array(
371 'foldersubscription' => 'rcube_subscription_form',
372 'folderframe' => 'rcmail_folder_frame',
373 'quotadisplay' => 'rcmail_quota_display',
376 $OUTPUT->send('folders');