thomascube
2011-08-18 fbe54043cf598b19a753dc2b21a7ed558d23fd15
commit | author | age
af3c04 1 <?php
A 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/settings/folders.inc                                    |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
af3c04 9  | Licensed under the GNU GPL                                            |
A 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$
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 &&
90f81a 37             $IMAP->get_response_code() == rcube_imap::TRYCREATE
af3c04 38         ) {
A 39             $result = $IMAP->create_mailbox($mbox, true);
40             if ($result) {
41                 // @TODO: remove 'virtual' class of folder's row
42             }
43         }
44
e81a30 45         if ($result) {
A 46             // Handle subscription of protected folder (#1487656)
47             if ($CONFIG['protect_default_folders'] == true
48                 && in_array($mbox, $CONFIG['default_imap_folders'])
49             ) {
50                 $OUTPUT->command('disable_subscription', $mbox);
51             }
52
af3c04 53             $OUTPUT->show_message('foldersubscribed', 'confirmation');
e81a30 54         }
af3c04 55         else
90f81a 56             rcmail_display_server_error('errorsaving');
af3c04 57     }
A 58 }
59
60 // unsubscribe mailbox
61 else if ($RCMAIL->action == 'unsubscribe')
62 {
63     $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
64     if (strlen($mbox)) {
65         $result = $IMAP->unsubscribe(array($mbox));
66         if ($result)
67             $OUTPUT->show_message('folderunsubscribed', 'confirmation');
68         else
90f81a 69             rcmail_display_server_error('errorsaving');
af3c04 70     }
A 71 }
72
73 // delete an existing mailbox
74 else if ($RCMAIL->action == 'delete-folder')
75 {
76     $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
77     $mbox      = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
78
198690 79     if (strlen($mbox)) {
A 80         $plugin = $RCMAIL->plugins->exec_hook('folder_delete', array('name' => $mbox));
81
82         if (!$plugin['abort']) {
83             $deleted = $IMAP->delete_mailbox($plugin['name']);
84         }
85         else {
86             $deleted = $plugin['result'];
87         }
88     }
af3c04 89
A 90     if ($OUTPUT->ajax_call && $deleted) {
0e1194 91         // Remove folder and subfolders rows
254d5e 92         $OUTPUT->command('remove_folder_row', $mbox_utf8, true);
af3c04 93         $OUTPUT->show_message('folderdeleted', 'confirmation');
A 94         // Clear content frame
95         $OUTPUT->command('subscription_select');
96         $OUTPUT->command('set_quota', rcmail_quota_content());
97     }
98     else if (!$deleted) {
90f81a 99         rcmail_display_server_error('errorsaving');
af3c04 100     }
A 101 }
102
103 // rename an existing mailbox
104 else if ($RCMAIL->action == 'rename-folder')
105 {
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));
108
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');
112
113         $rename = rcmail_rename_folder($oldname, $name);
114     }
115
116     if ($rename && $OUTPUT->ajax_call) {
254d5e 117         rcmail_update_folder_row($name, $oldname);
af3c04 118     }
A 119     else if (!$rename) {
90f81a 120         rcmail_display_server_error('errorsaving');
af3c04 121     }
A 122 }
123
124 // clear mailbox
125 else if ($RCMAIL->action == 'purge')
126 {
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, '/') . '/';
131
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)
135     ) {
136         $success = $IMAP->clear_mailbox($mbox);
137         $delete = true;
138     }
139     // copy to Trash
140     else {
141         $success = $IMAP->move_message('1:*', $CONFIG['trash_mbox'], $mbox);
142         $delete = false;
143     }
144
145     if ($success) {
146         $OUTPUT->set_env('messagecount', 0);
147         if ($delete) {
148             $OUTPUT->show_message('folderpurged', 'confirmation');
149             $OUTPUT->command('set_quota', rcmail_quota_content());
150         }
151         else {
152             $OUTPUT->show_message('messagemoved', 'confirmation');
153         }
154         $_SESSION['unseen_count'][$mbox] = 0;
155         $OUTPUT->command('show_folder', $mbox_utf8, null, true);
156     }
157     else {
90f81a 158         rcmail_display_server_error('errorsaving');
af3c04 159     }
A 160 }
161
162 // get mailbox size
163 else if ($RCMAIL->action == 'folder-size')
164 {
165     $name = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true));
166
167     $size = $IMAP->get_mailbox_size($name);
168
169     // @TODO: check quota and show percentage usage of specified mailbox?
170
171     if ($size !== false) {
172         $OUTPUT->command('folder_size_update', show_bytes($size));
173     }
90f81a 174     else {
A 175         rcmail_display_server_error();
176     }
af3c04 177 }
A 178
179 if ($OUTPUT->ajax_call)
180     $OUTPUT->send();
181
182
183 // build table with all folders listed by server
184 function rcube_subscription_form($attrib)
185 {
186     global $RCMAIL, $IMAP, $CONFIG, $OUTPUT;
187
188     list($form_start, $form_end) = get_form_tags($attrib, 'folders');
189     unset($attrib['form']);
67975b 190
af3c04 191     if (!$attrib['id'])
A 192         $attrib['id'] = 'rcmSubscriptionlist';
193
194     $table = new html_table();
195
196     if ($attrib['noheader'] !== true && $attrib['noheader'] != "true") {
197         // add table header
198         $table->add_header('name', rcube_label('foldername'));
199         $table->add_header('subscribed', '');
200     }
201
202     // get folders from server
ccc059 203     $IMAP->clear_cache('mailboxes', true);
af3c04 204
A 205     $a_unsubscribed = $IMAP->list_unsubscribed();
206     $a_subscribed   = $IMAP->list_mailboxes();
207     $delimiter      = $IMAP->get_hierarchy_delimiter();
67975b 208     $namespace      = $IMAP->get_namespace();
af3c04 209     $a_js_folders   = array();
A 210     $seen           = array();
211     $list_folders   = array();
212
213     // pre-process folders list
214     foreach ($a_unsubscribed as $i => $folder) {
d08333 215         $folder_id     = $folder;
A 216         $folder        = $IMAP->mod_mailbox($folder);
217         $foldersplit   = explode($delimiter, $folder);
218         $name          = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP');
af3c04 219         $parent_folder = join($delimiter, $foldersplit);
d08333 220         $level         = count($foldersplit);
af3c04 221
A 222         // add any necessary "virtual" parent folders
d08333 223         if ($parent_folder && !isset($seen[$parent_folder])) {
af3c04 224             for ($i=1; $i<=$level; $i++) {
A 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,
231                         'level'   => $i-1,
232                         'virtual' => true,
233                     );
234                 }
235             }
236         }
d08333 237
A 238         // Handle properly INBOX.INBOX situation
239         if (isset($seen[$folder])) {
240             continue;
241         }
242
af3c04 243         $seen[$folder]++;
A 244
245         $list_folders[] = array(
d08333 246             'id'    => $folder_id,
af3c04 247             'name'  => $name,
A 248             'level' => $level,
249         );
250     }
251
252     unset($seen);
253
71cc6b 254     // add drop-target representing 'root'
T 255     $table->add_row(array('id' => 'mailboxroot', 'class' => 'virtual root'));
256     $table->add('name', '&nbsp;');
257     $table->add(null, '&nbsp;');
258
85e7a3 259     $a_js_folders['mailboxroot'] = array('', '', true);
71cc6b 260
af3c04 261     $checkbox_subscribe = new html_checkbox(array(
A 262         'name'    => '_subscribed[]',
263         'title'   => rcube_label('changesubscription'),
264         'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)",
265     ));
266
267     // create list of available folders
268     foreach ($list_folders as $i => $folder) {
269         $idx        = $i + 1;
210438 270         $sub_key    = array_search($folder['id'], $a_subscribed);
A 271         $subscribed = $sub_key !== false;
af3c04 272         $protected  = ($CONFIG['protect_default_folders'] == true && in_array($folder['id'], $CONFIG['default_imap_folders']));
67975b 273         $noselect   = false;
af3c04 274         $classes    = array($i%2 ? 'even' : 'odd');
A 275
276         $folder_js      = Q($folder['id']);
277         $folder_utf8    = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
278         $display_folder = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $folder['level'])
279             . Q($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
210438 280
af3c04 281         if ($folder['virtual']) {
A 282             $classes[] = 'virtual';
283         }
284
285         if (!$protected) {
286             $opts = $IMAP->mailbox_options($folder['id']);
287             $noselect = in_array('\\Noselect', $opts);
288         }
289
e81a30 290         $disabled = (($protected && $subscribed) || $noselect);
A 291
67975b 292         // check if the folder is a namespace prefix, then disable subscription option on it
A 293         if (!$disabled && $folder['virtual'] && $folder['level'] == 0 && !empty($namespace)) {
294             $fname = $folder['id'] . $delimiter;
295             foreach ($namespace as $ns) {
296                 foreach ($ns as $item) {
297                     if ($item[0] === $fname) {
298                         $disabled = true;
922016 299                         break 2;
67975b 300                     }
A 301                 }
302             }
303         }
922016 304         // check if the folder is an other users virtual-root folder, then disable subscription option on it
A 305         if (!$disabled && $folder['virtual'] && $folder['level'] == 1
306             && !empty($namespace) && !empty($namespace['other'])
307         ) {
308             $parts = explode($delimiter, $folder['id']);
309             $fname = $parts[0] . $delimiter;
310             foreach ($namespace['other'] as $item) {
311                 if ($item[0] === $fname) {
312                     $disabled = true;
313                     break;
314                 }
315             }
316         }
72fa19 317         // check if the folder is shared, then disable subscription option on it
A 318         if (!$disabled && $folder['virtual'] && !empty($namespace)) {
319             $tmp_ns = array_merge((array)$namespace['other'], (array)$namespace['shared']);
320             foreach ($tmp_ns as $item) {
321                 if (strpos($folder['id'], $item[0]) === 0) {
322                     $disabled = true;
323                     break;
324                 }
325             }
326         }
67975b 327
5f2d15 328         $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes),
A 329             'foldername' => $folder['id']));
330
af3c04 331         $table->add('name', $display_folder);
A 332         $table->add('subscribed', $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''),
e81a30 333             array('value' => $folder_utf8, 'disabled' => $disabled ? 'disabled' : '')));
af3c04 334
e81a30 335         $a_js_folders['rcmrow'.$idx] = array($folder_utf8,
A 336             Q($display_folder), $protected || $folder['virtual']);
210438 337     }
A 338
af3c04 339     $RCMAIL->plugins->exec_hook('folders_list', array('table' => $table));
A 340
341     $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
342     $OUTPUT->set_env('subscriptionrows', $a_js_folders);
343     $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
344     $OUTPUT->set_env('delimiter', $delimiter);
345
346     return $form_start . $table->show($attrib) . $form_end;
347 }
348
349 function rcmail_folder_frame($attrib)
350 {
351     global $OUTPUT;
352
353     if (!$attrib['id'])
354         $attrib['id'] = 'rcmfolderframe';
5f2d15 355
af3c04 356     $attrib['name'] = $attrib['id'];
A 357
358     $OUTPUT->set_env('contentframe', $attrib['name']);
359     $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
360
361     return html::iframe($attrib);
362 }
363
364 function rcmail_rename_folder($oldname, $newname)
365 {
366     global $RCMAIL;
367
368     $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
198690 369
A 370     $plugin = $RCMAIL->plugins->exec_hook('folder_rename', array(
371         'oldname' => $oldname, 'newname' => $newname));
372
373     if (!$plugin['abort']) {
374         $renamed =  $RCMAIL->imap->rename_mailbox($oldname, $newname);
375     }
376     else {
377         $renamed = $plugin['result'];
378     }
af3c04 379
A 380     // update per-folder options for modified folder and its subfolders
198690 381     if ($renamed) {
af3c04 382         $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
A 383         $oldprefix  = '/^' . preg_quote($oldname . $delimiter, '/') . '/';
384
385         foreach ($a_threaded as $key => $val) {
386             if ($key == $oldname) {
387                 unset($a_threaded[$key]);
d08333 388                 $a_threaded[$newname] = true;
af3c04 389             }
A 390             else if (preg_match($oldprefix, $key)) {
391                 unset($a_threaded[$key]);
d08333 392                 $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = true;
af3c04 393             }
A 394         }
395         $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
396
397         return true;
398     }
399
400     return false;
401 }
402
254d5e 403
af3c04 404 $OUTPUT->set_pagetitle(rcube_label('folders'));
A 405 $OUTPUT->include_script('list.js');
406 $OUTPUT->set_env('quota', $IMAP->get_capability('QUOTA'));
407
408 // add some labels to client
409 $OUTPUT->add_label('deletefolderconfirm', 'purgefolderconfirm', 'folderdeleting',
410     'foldermoving', 'foldersubscribing', 'folderunsubscribing', 'quota');
411
412 // register UI objects
413 $OUTPUT->add_handlers(array(
414     'foldersubscription' => 'rcube_subscription_form',
415     'folderframe' => 'rcmail_folder_frame',
416     'quotadisplay' => 'rcmail_quota_display',
417 ));
418
419 $OUTPUT->send('folders');
420