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