]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/settings/manage_folders.inc
Imported Upstream version 0.2~alpha
[roundcube.git] / program / steps / settings / manage_folders.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/settings/manage_folders.inc                             |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Provide functionality to create/delete/rename folders               |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: manage_folders.inc 1455 2008-05-30 11:52:15Z alec $
19
20 */
21
22 // WARNING: folder names in UI are encoded with UTF-8
23
24 // init IMAP connection
25 $RCMAIL->imap_init(true);
26
27 // subscribe to one or more mailboxes
28 if ($RCMAIL->action=='subscribe')
29   {
30   if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF-7'))
31     $IMAP->subscribe(array($mbox));
32   }
33
34 // unsubscribe one or more mailboxes
35 else if ($RCMAIL->action=='unsubscribe')
36   {
37   if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF-7'))
38     $IMAP->unsubscribe(array($mbox));
39   }
40
41 // create a new mailbox
42 else if ($RCMAIL->action=='create-folder')
43   {
44   if (!empty($_POST['_name']))
45     {
46     $name = trim(get_input_value('_name', RCUBE_INPUT_POST, FALSE, 'UTF-7'));
47     // #1485036 (RFC3501, 5.1.3) TODO: it should be done on read not on write
48     $name = str_replace('&-', '&', $name);
49     $create = $IMAP->create_mailbox($name, TRUE);
50     }
51   
52   if ($create && $OUTPUT->ajax_call)
53     {
54     $delimiter = $IMAP->get_hierarchy_delimiter();
55     $folderlist = $IMAP->list_unsubscribed();
56     $index = array_search($create, $folderlist);
57     $before = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF-7') : false;
58     
59     $create = rcube_charset_convert($create, 'UTF-7');
60     $foldersplit = explode($delimiter, $create);
61     $display_create = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', substr_count($create, $delimiter)) . $foldersplit[count($foldersplit)-1];
62
63     $OUTPUT->command('add_folder_row', $create, $display_create, false, $before);
64     }
65   else if (!$create)
66     {
67     $OUTPUT->show_message('errorsaving', 'error');
68     }
69   }
70
71 // rename a mailbox
72 else if ($RCMAIL->action=='rename-folder')
73   {
74   if (!empty($_POST['_folder_oldname']) && !empty($_POST['_folder_newname']))
75     {
76     $name_utf8 = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST));
77     $oldname_utf8 = get_input_value('_folder_oldname', RCUBE_INPUT_POST);
78     $name = rcube_charset_convert($name_utf8, 'UTF-8', 'UTF-7');
79     $oldname = rcube_charset_convert($oldname_utf8, 'UTF-8', 'UTF-7');
80
81     // #1485036 (RFC3501, 5.1.3) TODO: it should be done on read not on write
82     $name = str_replace('&-', '&', $name);
83
84     $rename = $IMAP->rename_mailbox($oldname, $name);
85     }
86
87   if ($rename && $OUTPUT->ajax_call)
88     {
89     $folderlist = $IMAP->list_unsubscribed();
90     $delimiter = $IMAP->get_hierarchy_delimiter();
91
92     $regexp = '/^' . preg_quote($rename . $delimiter, '/') . '/';
93
94     // subfolders
95     for ($x=sizeof($folderlist)-1; $x>=0; $x--)
96       {
97       if (preg_match($regexp, $folderlist[$x]))
98         {
99         $oldfolder = $oldname . $delimiter . preg_replace($regexp, '', $folderlist[$x]);
100         $foldersplit = explode($delimiter, $folderlist[$x]);
101         $level = count($foldersplit) - 1;
102         $display_rename = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) 
103             . rcube_charset_convert($foldersplit[$level], 'UTF-7');
104
105         $before = isset($folderlist[$x+1]) ? rcube_charset_convert($folderlist[$x+1], 'UTF-7') : false;
106         
107         $OUTPUT->command('replace_folder_row', rcube_charset_convert($oldfolder, 'UTF-7'),
108             rcube_charset_convert($folderlist[$x], 'UTF-7'), $display_rename, $before);
109         }
110       }
111
112     $foldersplit = explode($delimiter, $rename);
113     $level = count($foldersplit) - 1;
114     $display_rename = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) . rcube_charset_convert($foldersplit[$level], 'UTF-7');
115     $index = array_search($rename, $folderlist);
116     $before = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF-7') : false;
117
118     $OUTPUT->command('replace_folder_row', $oldname_utf8, rcube_charset_convert($rename, 'UTF-7'),
119         $display_rename, $before);
120
121     $OUTPUT->command('reset_folder_rename');
122     }
123   else if (!$rename && $OUTPUT->ajax_call)
124     {
125     $OUTPUT->command('reset_folder_rename');
126     $OUTPUT->show_message('errorsaving', 'error');
127     }
128   else if (!$rename)
129     $OUTPUT->show_message('errorsaving', 'error');
130   }
131
132 // delete an existing IMAP mailbox
133 else if ($RCMAIL->action=='delete-folder')
134   {
135   $a_mboxes = $IMAP->list_unsubscribed();
136   $delimiter = $IMAP->get_hierarchy_delimiter();
137   
138   $mboxes_utf8 = get_input_value('_mboxes', RCUBE_INPUT_POST);
139   $mboxes = rcube_charset_convert($mboxes_utf8, 'UTF-8', 'UTF-7');
140
141   if ($mboxes)
142     $deleted = $IMAP->delete_mailbox(array($mboxes));
143
144   if ($OUTPUT->ajax_call && $deleted)
145     {
146     $OUTPUT->command('remove_folder_row', $mboxes_utf8);
147     foreach ($a_mboxes as $mbox)
148       {
149       if (preg_match('/^'. preg_quote($mboxes.$delimiter, '/') .'/', $mbox))
150         {
151         $OUTPUT->command('remove_folder_row', rcube_charset_convert($mbox, 'UTF-7'));
152         }
153       }
154     $OUTPUT->show_message('folderdeleted', 'confirmation');
155     }
156   else if (!$deleted)
157     {
158     $OUTPUT->show_message('errorsaving', 'error');
159     }
160   }
161
162 if ($OUTPUT->ajax_call)
163   $OUTPUT->send();
164
165
166 // build table with all folders listed by server
167 function rcube_subscription_form($attrib)
168   {
169   global $IMAP, $CONFIG, $OUTPUT;
170
171   list($form_start, $form_end) = get_form_tags($attrib, 'folders');
172   unset($attrib['form']);
173   
174   if (!$attrib['id'])
175     $attrib['id'] = 'rcmSubscriptionlist';
176
177   // allow the following attributes to be added to the <table> tag
178   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
179
180   $out = "$form_start\n<table" . $attrib_str . ">\n";
181
182
183   // add table header
184   $out .= "<thead><tr>\n";
185   $out .= sprintf('<td class="name">%s</td><td class="msgcount">%s</td><td class="subscribed">%s</td>'.
186                   '<td class="rename">&nbsp;</td><td class="delete">&nbsp;</td>',
187                   rcube_label('foldername'), rcube_label('messagecount'), rcube_label('subscribed'));
188                   
189   $out .= "\n</tr></thead>\n<tbody>\n";
190
191
192   // get folders from server
193   $IMAP->clear_cache('mailboxes');
194
195   $a_unsubscribed = $IMAP->list_unsubscribed();
196   $a_subscribed = $IMAP->list_mailboxes();
197   $delimiter = $IMAP->get_hierarchy_delimiter();
198   $a_js_folders = array();
199
200   $checkbox_subscribe = new html_checkbox(array('name' => '_subscribed[]', 'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)"));
201   
202   if (!empty($attrib['deleteicon']))
203     $del_button = sprintf('<img src="%s%s" alt="%s" border="0" />', $CONFIG['skin_path'], $attrib['deleteicon'], rcube_label('delete'));
204   else
205     $del_button = rcube_label('delete');
206
207   if (!empty($attrib['renameicon']))
208     $edit_button = sprintf('<img src="%s%s" alt="%s" border="0" />', $CONFIG['skin_path'], $attrib['renameicon'], rcube_label('rename'));
209   else
210     $del_button = rcube_label('rename');
211
212   // create list of available folders
213   foreach ($a_unsubscribed as $i => $folder)
214     {
215     $subscribed = in_array($folder, $a_subscribed);
216     $protected = ($CONFIG['protect_default_folders'] == TRUE && in_array($folder,$CONFIG['default_imap_folders']));
217     $zebra_class = $i%2 ? 'even' : 'odd';
218     $folder_js = JQ($folder);
219     $foldersplit = explode($delimiter, $folder);
220     $level = count($foldersplit) - 1;
221     $display_folder = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) . rcube_charset_convert($foldersplit[$level], 'UTF-7');
222     $folder_html = $CONFIG['protect_default_folders'] && in_array($folder, $CONFIG['default_imap_folders']) ? rcmail_localize_foldername($folder) : $display_folder;
223     $folder_utf8 = rcube_charset_convert($folder, 'UTF-7');
224
225     $a_js_folders['rcmrow'.($i+1)] = array($folder_utf8, $display_folder, $protected);
226
227     $out .= sprintf('<tr id="rcmrow%d" class="%s"><td class="name">%s</td><td class="msgcount">%d</td>',
228                     $i+1,
229                     $zebra_class,
230                     Q($folder_html),
231                     $IMAP->messagecount($folder));
232                     
233     if ($protected)
234       $out .= '<td class="subscribed">&nbsp;'.($subscribed ? '&#x2022;' : '-').'</td>';
235     else
236       $out .= '<td class="subscribed">'.$checkbox_subscribe->show($subscribed?$folder_utf8:'', array('value' => $folder_utf8)).'</td>';
237
238     // add rename and delete buttons
239     if (!$protected)
240       $out .= sprintf('<td class="rename"><a href="#rename" title="%s">%s</a>'.
241                       '<td class="delete"><a href="#delete" title="%s">%s</a></td>',
242                       rcube_label('renamefolder'),
243                       $edit_button,
244                       rcube_label('deletefolder'),
245                       $del_button);
246     else
247       $out .= '<td></td><td></td>';
248     
249     $out .= "</tr>\n";
250     }
251
252   $out .= "</tbody>\n</table>";
253   $out .= "\n$form_end";
254
255   $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
256   $OUTPUT->set_env('subscriptionrows', $a_js_folders);
257   $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
258   $OUTPUT->set_env('delimiter', $delimiter);
259
260   return $out;  
261   }
262
263
264 function rcube_create_folder_form($attrib)
265   {
266   global $OUTPUT;
267   
268   list($form_start, $form_end) = get_form_tags($attrib, 'create-folder');
269   unset($attrib['form']);
270
271   if ($attrib['hintbox'])
272     $OUTPUT->add_gui_object('createfolderhint', $attrib['hintbox']);
273
274   // return the complete edit form as table
275   $out = "$form_start\n";
276
277   $input = new html_inputfield(array('name' => '_folder_name'));
278   $out .= $input->show();
279   
280   if (get_boolean($attrib['button']))
281     {
282     $button = new html_inputfield(array('type' => 'button',
283                                     'value' => rcube_label('create'),
284                                     'onclick' => JS_OBJECT_NAME.".command('create-folder',this.form)"));
285     $out .= $button->show();
286     }
287
288   $out .= "\n$form_end";
289
290   return $out;
291   }
292
293 function rcube_rename_folder_form($attrib)
294   {
295   global $CONFIG, $IMAP;
296
297   list($form_start, $form_end) = get_form_tags($attrib, 'rename-folder');
298   unset($attrib['form']);
299
300   // return the complete edit form as table
301   $out = "$form_start\n";
302
303   $a_unsubscribed = $IMAP->list_unsubscribed();
304   $select_folder = new html_select(array('name' => '_folder_oldname', 'id' => 'rcmfd_oldfolder'));
305
306   foreach ($a_unsubscribed as $i => $folder)
307     {
308     if ($CONFIG['protect_default_folders'] == TRUE && in_array($folder,$CONFIG['default_imap_folders'])) 
309       continue;
310
311     $select_folder->add($folder);
312     }
313
314   $out .= $select_folder->show();
315
316   $out .= " to ";
317   $inputtwo = new html_inputfield(array('name' => '_folder_newname'));
318   $out .= $inputtwo->show();
319
320   if (get_boolean($attrib['button']))
321     {
322     $button = new html_inputfield(array('type' => 'button',
323                                     'value' => rcube_label('rename'),
324                                     'onclick' => JS_OBJECT_NAME.".command('rename-folder',this.form)"));
325     $out .= $button->show();
326     }
327
328   $out .= "\n$form_end";
329   
330   return $out;
331   }
332
333 $OUTPUT->set_pagetitle(rcube_label('folders'));
334 $OUTPUT->include_script('list.js');
335
336 // register UI objects
337 $OUTPUT->add_handlers(array(
338   'foldersubscription' => 'rcube_subscription_form',
339   'createfolder' => 'rcube_create_folder_form',
340   'renamefolder' => 'rcube_rename_folder_form'
341 ));
342
343 // add some labels to client
344 rcube_add_label('deletefolderconfirm','addsubfolderhint','forbiddencharacter');
345
346 $OUTPUT->send('managefolders');
347 ?>