alecpl
2011-05-05 d08333ea578e3b6c6ab42bed05f808a2b7b93cf1
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
0e1194 79     // get folder's children or all folders if the name contains special characters
A 80     $delimiter = $IMAP->get_hierarchy_delimiter();
81     if ((strpos($mbox, '%') === false) && (strpos($mbox, '*') === false))
82         $a_mboxes  = $IMAP->list_unsubscribed('', $mbox.$delimiter.'*');
83     else
84         $a_mboxes  = $IMAP->list_unsubscribed();
85
af3c04 86     if (strlen($mbox))
0e1194 87         $deleted = $IMAP->delete_mailbox($mbox);
af3c04 88
A 89     if ($OUTPUT->ajax_call && $deleted) {
0e1194 90         // Remove folder and subfolders rows
af3c04 91         $OUTPUT->command('remove_folder_row', $mbox_utf8);
A 92         foreach ($a_mboxes as $folder) {
93             if (preg_match('/^'. preg_quote($mbox.$delimiter, '/') .'/', $folder)) {
94                 $OUTPUT->command('remove_folder_row', rcube_charset_convert($folder, 'UTF7-IMAP'));
95             }
96         }
97         $OUTPUT->show_message('folderdeleted', 'confirmation');
98         // Clear content frame
99         $OUTPUT->command('subscription_select');
100         $OUTPUT->command('set_quota', rcmail_quota_content());
101     }
102     else if (!$deleted) {
90f81a 103         rcmail_display_server_error('errorsaving');
af3c04 104     }
A 105 }
106
107 // rename an existing mailbox
108 else if ($RCMAIL->action == 'rename-folder')
109 {
110     $name_utf8    = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST, true));
111     $oldname_utf8 = trim(get_input_value('_folder_oldname', RCUBE_INPUT_POST, true));
112
113     if (strlen($name_utf8) && strlen($oldname_utf8)) {
114         $name    = rcube_charset_convert($name_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
115         $oldname = rcube_charset_convert($oldname_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
116
117         $rename = rcmail_rename_folder($oldname, $name);
118     }
119
120     if ($rename && $OUTPUT->ajax_call) {
121         $folderlist = $IMAP->list_unsubscribed();
122         $delimiter  = $IMAP->get_hierarchy_delimiter();
123
124         $regexp = '/^' . preg_quote($name . $delimiter, '/') . '/';
125
126         // subfolders
127         for ($x=sizeof($folderlist)-1; $x>=0; $x--) {
128             if (preg_match($regexp, $folderlist[$x])) {
d08333 129                 $oldfolder   = $oldname . $delimiter . preg_replace($regexp, '', $folderlist[$x]);
A 130                 $foldersplit = explode($delimiter, $IMAP->mod_mailbox($folderlist[$x]));
131                 $level       = count($foldersplit) - 1;
af3c04 132                 $display_rename = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) 
A 133                     . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
134                 $before = isset($folderlist[$x+1]) ? rcube_charset_convert($folderlist[$x+1], 'UTF7-IMAP') : false;
0e1194 135
af3c04 136                 $OUTPUT->command('replace_folder_row', rcube_charset_convert($oldfolder, 'UTF7-IMAP'),
A 137                     rcube_charset_convert($folderlist[$x], 'UTF7-IMAP'), $display_rename, $before);
138             }
139         }
140
d08333 141         $index       = array_search($name, $folderlist);
A 142         $name        = $IMAP->mod_mailbox($name);
af3c04 143         $foldersplit = explode($delimiter, $name);
d08333 144         $level       = count($foldersplit) - 1;
af3c04 145         $display_rename = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
d08333 146         $before         = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF7-IMAP') : false;
af3c04 147
d08333 148         $OUTPUT->command('replace_folder_row', $oldname_utf8, $name_utf8, $display_rename, $before);
af3c04 149     }
A 150     else if (!$rename) {
90f81a 151         rcmail_display_server_error('errorsaving');
af3c04 152     }
A 153 }
154
155 // clear mailbox
156 else if ($RCMAIL->action == 'purge')
157 {
158     $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
159     $mbox      = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
160     $delimiter = $IMAP->get_hierarchy_delimiter();
161     $trash_regexp = '/^' . preg_quote($CONFIG['trash_mbox'] . $delimiter, '/') . '/';
162
163     // we should only be purging trash (or their subfolders)
164     if (!strlen($CONFIG['trash_mbox']) || $mbox == $CONFIG['trash_mbox']
165         || preg_match($trash_regexp, $mbox)
166     ) {
167         $success = $IMAP->clear_mailbox($mbox);
168         $delete = true;
169     }
170     // copy to Trash
171     else {
172         $success = $IMAP->move_message('1:*', $CONFIG['trash_mbox'], $mbox);
173         $delete = false;
174     }
175
176     if ($success) {
177         $OUTPUT->set_env('messagecount', 0);
178         if ($delete) {
179             $OUTPUT->show_message('folderpurged', 'confirmation');
180             $OUTPUT->command('set_quota', rcmail_quota_content());
181         }
182         else {
183             $OUTPUT->show_message('messagemoved', 'confirmation');
184         }
185         $_SESSION['unseen_count'][$mbox] = 0;
186         $OUTPUT->command('show_folder', $mbox_utf8, null, true);
187     }
188     else {
90f81a 189         rcmail_display_server_error('errorsaving');
af3c04 190     }
A 191 }
192
193 // get mailbox size
194 else if ($RCMAIL->action == 'folder-size')
195 {
196     $name = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true));
197
198     $size = $IMAP->get_mailbox_size($name);
199
200     // @TODO: check quota and show percentage usage of specified mailbox?
201
202     if ($size !== false) {
203         $OUTPUT->command('folder_size_update', show_bytes($size));
204     }
90f81a 205     else {
A 206         rcmail_display_server_error();
207     }
af3c04 208 }
A 209
210 if ($OUTPUT->ajax_call)
211     $OUTPUT->send();
212
213
214 // build table with all folders listed by server
215 function rcube_subscription_form($attrib)
216 {
217     global $RCMAIL, $IMAP, $CONFIG, $OUTPUT;
218
219     list($form_start, $form_end) = get_form_tags($attrib, 'folders');
220     unset($attrib['form']);
221   
222     if (!$attrib['id'])
223         $attrib['id'] = 'rcmSubscriptionlist';
224
225     $table = new html_table();
226
227     if ($attrib['noheader'] !== true && $attrib['noheader'] != "true") {
228         // add table header
229         $table->add_header('name', rcube_label('foldername'));
230         $table->add_header('subscribed', '');
231     }
232
233     // get folders from server
234     $IMAP->clear_cache('mailboxes');
235
236     $a_unsubscribed = $IMAP->list_unsubscribed();
237     $a_subscribed   = $IMAP->list_mailboxes();
238     $delimiter      = $IMAP->get_hierarchy_delimiter();
239     $a_js_folders   = array();
240     $seen           = array();
241     $list_folders   = array();
242
243     // pre-process folders list
244     foreach ($a_unsubscribed as $i => $folder) {
d08333 245         $folder_id     = $folder;
A 246         $folder        = $IMAP->mod_mailbox($folder);
247         $foldersplit   = explode($delimiter, $folder);
248         $name          = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP');
af3c04 249         $parent_folder = join($delimiter, $foldersplit);
d08333 250         $level         = count($foldersplit);
af3c04 251
A 252         // add any necessary "virtual" parent folders
d08333 253         if ($parent_folder && !isset($seen[$parent_folder])) {
af3c04 254             for ($i=1; $i<=$level; $i++) {
A 255                 $ancestor_folder = join($delimiter, array_slice($foldersplit, 0, $i));
256                 if ($ancestor_folder && !$seen[$ancestor_folder]++) {
257                     $ancestor_name = rcube_charset_convert($foldersplit[$i-1], 'UTF7-IMAP');
258                     $list_folders[] = array(
259                         'id'      => $ancestor_folder,
260                         'name'    => $ancestor_name,
261                         'level'   => $i-1,
262                         'virtual' => true,
263                     );
264                 }
265             }
266         }
d08333 267
A 268         // Handle properly INBOX.INBOX situation
269         if (isset($seen[$folder])) {
270             continue;
271         }
272
af3c04 273         $seen[$folder]++;
A 274
275         $list_folders[] = array(
d08333 276             'id'    => $folder_id,
af3c04 277             'name'  => $name,
A 278             'level' => $level,
279         );
280     }
281
282     unset($seen);
283
284     $checkbox_subscribe = new html_checkbox(array(
285         'name'    => '_subscribed[]',
286         'title'   => rcube_label('changesubscription'),
287         'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)",
288     ));
289
290     // create list of available folders
291     foreach ($list_folders as $i => $folder) {
292         $idx        = $i + 1;
293         $subscribed = in_array($folder['id'], $a_subscribed);
294         $protected  = ($CONFIG['protect_default_folders'] == true && in_array($folder['id'], $CONFIG['default_imap_folders']));
295         $classes    = array($i%2 ? 'even' : 'odd');
296
297         $folder_js      = Q($folder['id']);
298         $folder_utf8    = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
299         $display_folder = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $folder['level'])
300             . Q($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
301     
302         if ($folder['virtual']) {
303             $classes[] = 'virtual';
304         }
305
306         if (!$protected) {
307             $opts = $IMAP->mailbox_options($folder['id']);
308             $noselect = in_array('\\Noselect', $opts);
309         }
310
e81a30 311         $disabled = (($protected && $subscribed) || $noselect);
A 312
af3c04 313         $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes)));
A 314     
315         $table->add('name', $display_folder);
316         $table->add('subscribed', $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''),
e81a30 317             array('value' => $folder_utf8, 'disabled' => $disabled ? 'disabled' : '')));
af3c04 318
e81a30 319         $a_js_folders['rcmrow'.$idx] = array($folder_utf8,
A 320             Q($display_folder), $protected || $folder['virtual']);
af3c04 321     }
A 322
323     $RCMAIL->plugins->exec_hook('folders_list', array('table' => $table));
324
325     $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
326     $OUTPUT->set_env('subscriptionrows', $a_js_folders);
327     $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
328     $OUTPUT->set_env('delimiter', $delimiter);
329
330     return $form_start . $table->show($attrib) . $form_end;
331 }
332
333 function rcmail_folder_frame($attrib)
334 {
335     global $OUTPUT;
336
337     if (!$attrib['id'])
338         $attrib['id'] = 'rcmfolderframe';
339     
340     $attrib['name'] = $attrib['id'];
341
342     $OUTPUT->set_env('contentframe', $attrib['name']);
343     $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
344
345     return html::iframe($attrib);
346 }
347
348 function rcmail_rename_folder($oldname, $newname)
349 {
350     global $RCMAIL;
351
352     $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
353     $rename    = $RCMAIL->imap->rename_mailbox($oldname, $newname);
354
355     // update per-folder options for modified folder and its subfolders
356     if ($rename !== false) {
357         $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
358         $oldprefix  = '/^' . preg_quote($oldname . $delimiter, '/') . '/';
359
360         foreach ($a_threaded as $key => $val) {
361             if ($key == $oldname) {
362                 unset($a_threaded[$key]);
d08333 363                 $a_threaded[$newname] = true;
af3c04 364             }
A 365             else if (preg_match($oldprefix, $key)) {
366                 unset($a_threaded[$key]);
d08333 367                 $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = true;
af3c04 368             }
A 369         }
370         $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
371
372         return true;
373     }
374
375     return false;
376 }
377
378 $OUTPUT->set_pagetitle(rcube_label('folders'));
379 $OUTPUT->include_script('list.js');
380 $OUTPUT->set_env('quota', $IMAP->get_capability('QUOTA'));
381
382 // add some labels to client
383 $OUTPUT->add_label('deletefolderconfirm', 'purgefolderconfirm', 'folderdeleting',
384     'foldermoving', 'foldersubscribing', 'folderunsubscribing', 'quota');
385
386 // register UI objects
387 $OUTPUT->add_handlers(array(
388     'foldersubscription' => 'rcube_subscription_form',
389     'folderframe' => 'rcmail_folder_frame',
390     'quotadisplay' => 'rcmail_quota_display',
391 ));
392
393 $OUTPUT->send('folders');
394