]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/settings/folders.inc
f118171fa3e7a111c9b37c3f08d2833f214dcdf4
[roundcube.git] / program / steps / settings / folders.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/settings/folders.inc                                    |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Provide functionality of folders management                         |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  | Author: Aleksander Machniak <alec@alec.pl>                            |
17  +-----------------------------------------------------------------------+
18
19  $Id: folders.inc 4321 2010-12-08 12:52:04Z alec $
20
21 */
22
23 // WARNING: folder names in UI are encoded with RCMAIL_CHARSET
24
25 // init IMAP connection
26 $RCMAIL->imap_connect();
27
28 // subscribe mailbox
29 if ($RCMAIL->action == 'subscribe')
30 {
31     $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
32     if (strlen($mbox)) {
33         $result = $IMAP->subscribe(array($mbox));
34
35         // Handle virtual (non-existing) folders
36         if (!$result && $IMAP->get_error_code() == -1 &&
37             $IMAP->get_response_code() == rcube_imap::TRYCREATE
38         ) {
39             $result = $IMAP->create_mailbox($mbox, true);
40             if ($result) {
41                 // @TODO: remove 'virtual' class of folder's row
42             }
43         }
44
45         if ($result)
46             $OUTPUT->show_message('foldersubscribed', 'confirmation');
47         else
48             rcmail_display_server_error('errorsaving');
49     }
50 }
51
52 // unsubscribe mailbox
53 else if ($RCMAIL->action == 'unsubscribe')
54 {
55     $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
56     if (strlen($mbox)) {
57         $result = $IMAP->unsubscribe(array($mbox));
58         if ($result)
59             $OUTPUT->show_message('folderunsubscribed', 'confirmation');
60         else
61             rcmail_display_server_error('errorsaving');
62     }
63 }
64
65 // delete an existing mailbox
66 else if ($RCMAIL->action == 'delete-folder')
67 {
68     $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
69     $mbox      = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
70
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.'*');
75     else
76         $a_mboxes  = $IMAP->list_unsubscribed();
77
78     if (strlen($mbox))
79         $deleted = $IMAP->delete_mailbox($mbox);
80
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'));
87             }
88         }
89         $OUTPUT->show_message('folderdeleted', 'confirmation');
90         // Clear content frame
91         $OUTPUT->command('subscription_select');
92         $OUTPUT->command('set_quota', rcmail_quota_content());
93     }
94     else if (!$deleted) {
95         rcmail_display_server_error('errorsaving');
96     }
97 }
98
99 // rename an existing mailbox
100 else if ($RCMAIL->action == 'rename-folder')
101 {
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));
104
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');
108
109         $rename = rcmail_rename_folder($oldname, $name);
110     }
111
112     if ($rename && $OUTPUT->ajax_call) {
113         $folderlist = $IMAP->list_unsubscribed();
114         $delimiter  = $IMAP->get_hierarchy_delimiter();
115
116         $regexp = '/^' . preg_quote($name . $delimiter, '/') . '/';
117
118         // subfolders
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('&nbsp;&nbsp;&nbsp;&nbsp;', $level) 
125                     . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
126
127                 $before = isset($folderlist[$x+1]) ? rcube_charset_convert($folderlist[$x+1], 'UTF7-IMAP') : false;
128
129                 $OUTPUT->command('replace_folder_row', rcube_charset_convert($oldfolder, 'UTF7-IMAP'),
130                     rcube_charset_convert($folderlist[$x], 'UTF7-IMAP'), $display_rename, $before);
131             }
132         }
133
134         $foldersplit = explode($delimiter, $name);
135         $level = count($foldersplit) - 1;
136         $display_rename = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $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;
139
140         $OUTPUT->command('replace_folder_row', $oldname_utf8,
141             rcube_charset_convert($name, 'UTF7-IMAP'), $display_rename, $before);
142     }
143     else if (!$rename) {
144         rcmail_display_server_error('errorsaving');
145     }
146 }
147
148 // clear mailbox
149 else if ($RCMAIL->action == 'purge')
150 {
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, '/') . '/';
155
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)
159     ) {
160         $success = $IMAP->clear_mailbox($mbox);
161         $delete = true;
162     }
163     // copy to Trash
164     else {
165         $success = $IMAP->move_message('1:*', $CONFIG['trash_mbox'], $mbox);
166         $delete = false;
167     }
168
169     if ($success) {
170         $OUTPUT->set_env('messagecount', 0);
171         if ($delete) {
172             $OUTPUT->show_message('folderpurged', 'confirmation');
173             $OUTPUT->command('set_quota', rcmail_quota_content());
174         }
175         else {
176             $OUTPUT->show_message('messagemoved', 'confirmation');
177         }
178         $_SESSION['unseen_count'][$mbox] = 0;
179         $OUTPUT->command('show_folder', $mbox_utf8, null, true);
180     }
181     else {
182         rcmail_display_server_error('errorsaving');
183     }
184 }
185
186 // get mailbox size
187 else if ($RCMAIL->action == 'folder-size')
188 {
189     $name = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true));
190
191     $size = $IMAP->get_mailbox_size($name);
192
193     // @TODO: check quota and show percentage usage of specified mailbox?
194
195     if ($size !== false) {
196         $OUTPUT->command('folder_size_update', show_bytes($size));
197     }
198     else {
199         rcmail_display_server_error();
200     }
201 }
202
203 if ($OUTPUT->ajax_call)
204     $OUTPUT->send();
205
206
207 // build table with all folders listed by server
208 function rcube_subscription_form($attrib)
209 {
210     global $RCMAIL, $IMAP, $CONFIG, $OUTPUT;
211
212     list($form_start, $form_end) = get_form_tags($attrib, 'folders');
213     unset($attrib['form']);
214   
215     if (!$attrib['id'])
216         $attrib['id'] = 'rcmSubscriptionlist';
217
218     $table = new html_table();
219
220     if ($attrib['noheader'] !== true && $attrib['noheader'] != "true") {
221         // add table header
222         $table->add_header('name', rcube_label('foldername'));
223         $table->add_header('subscribed', '');
224     }
225
226     // get folders from server
227     $IMAP->clear_cache('mailboxes');
228
229     $a_unsubscribed = $IMAP->list_unsubscribed();
230     $a_subscribed   = $IMAP->list_mailboxes();
231     $delimiter      = $IMAP->get_hierarchy_delimiter();
232     $a_js_folders   = array();
233     $seen           = array();
234     $list_folders   = array();
235
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);
242
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,
252                         'level'   => $i-1,
253                         'virtual' => true,
254                     );
255                     }
256             }
257         }
258     
259         $seen[$folder]++;
260
261         $list_folders[] = array(
262             'id'    => $folder,
263             'name'  => $name,
264             'level' => $level,
265         );
266     }
267
268     unset($seen);
269
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)",
274     ));
275
276     // create list of available folders
277     foreach ($list_folders as $i => $folder) {
278         $idx        = $i + 1;
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');
282
283         $folder_js      = Q($folder['id']);
284         $folder_utf8    = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
285         $display_folder = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $folder['level'])
286             . Q($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
287     
288         if ($folder['virtual']) {
289             $classes[] = 'virtual';
290         }
291
292         if (!$protected) {
293             $opts = $IMAP->mailbox_options($folder['id']);
294             $noselect = in_array('\\Noselect', $opts);
295         }
296
297         $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes)));
298     
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' : '')));
302
303         $a_js_folders['rcmrow'.$idx] = array($folder_utf8, Q($display_folder), $protected || $folder['virtual']);
304     }
305
306     $RCMAIL->plugins->exec_hook('folders_list', array('table' => $table));
307
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);
312
313     return $form_start . $table->show($attrib) . $form_end;
314 }
315
316 function rcmail_folder_frame($attrib)
317 {
318     global $OUTPUT;
319
320     if (!$attrib['id'])
321         $attrib['id'] = 'rcmfolderframe';
322     
323     $attrib['name'] = $attrib['id'];
324
325     $OUTPUT->set_env('contentframe', $attrib['name']);
326     $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
327
328     return html::iframe($attrib);
329 }
330
331 function rcmail_rename_folder($oldname, $newname)
332 {
333     global $RCMAIL;
334
335     $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
336     $rename    = $RCMAIL->imap->rename_mailbox($oldname, $newname);
337
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, '/') . '/';
342
343         foreach ($a_threaded as $key => $val) {
344             if ($key == $oldname) {
345                 unset($a_threaded[$key]);
346                 $a_threaded[$newname] = true;
347             }
348             else if (preg_match($oldprefix, $key)) {
349                 unset($a_threaded[$key]);
350                     $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = true;
351             }
352         }
353         $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
354
355         return true;
356     }
357
358     return false;
359 }
360
361 $OUTPUT->set_pagetitle(rcube_label('folders'));
362 $OUTPUT->include_script('list.js');
363 $OUTPUT->set_env('quota', $IMAP->get_capability('QUOTA'));
364
365 // add some labels to client
366 $OUTPUT->add_label('deletefolderconfirm', 'purgefolderconfirm', 'folderdeleting',
367     'foldermoving', 'foldersubscribing', 'folderunsubscribing', 'quota');
368
369 // register UI objects
370 $OUTPUT->add_handlers(array(
371     'foldersubscription' => 'rcube_subscription_form',
372     'folderframe' => 'rcmail_folder_frame',
373     'quotadisplay' => 'rcmail_quota_display',
374 ));
375
376 $OUTPUT->send('folders');
377