source: github/program/steps/settings/manage_folders.inc @ 01538da

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 01538da was 01538da, checked in by alecpl <alec@…>, 3 years ago
  • Show disabled checkboxes for protected folders instead of dots (#1485498)
  • Property mode set to 100644
File size: 14.8 KB
Line 
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-2009, 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$
19
20*/
21
22// WARNING: folder names in UI are encoded with RCMAIL_CHARSET
23
24// init IMAP connection
25$RCMAIL->imap_connect();
26
27// subscribe to one or more mailboxes
28if ($RCMAIL->action=='subscribe')
29  {
30  if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF7-IMAP'))
31    $IMAP->subscribe(array($mbox));
32  }
33
34// unsubscribe one or more mailboxes
35else if ($RCMAIL->action=='unsubscribe')
36  {
37  if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF7-IMAP'))
38    $IMAP->unsubscribe(array($mbox));
39  }
40
41// enable threading for one or more mailboxes
42else if ($RCMAIL->action=='enable-threading')
43  {
44  if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF7-IMAP'))
45    rcube_set_threading($mbox, true);
46  }
47
48// enable threading for one or more mailboxes
49else if ($RCMAIL->action=='disable-threading')
50  {
51  if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF7-IMAP'))
52    rcube_set_threading($mbox, false);
53  }
54
55// create a new mailbox
56else if ($RCMAIL->action=='create-folder')
57  {
58  if (!empty($_POST['_name']))
59    {
60    $name = trim(get_input_value('_name', RCUBE_INPUT_POST, FALSE, 'UTF7-IMAP'));
61    $create = $IMAP->create_mailbox($name, TRUE);
62    }
63 
64  if ($create && $OUTPUT->ajax_call)
65    {
66    $delimiter = $IMAP->get_hierarchy_delimiter();
67    $folderlist = $IMAP->list_unsubscribed();
68    $index = array_search($create, $folderlist);
69    $before = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF7-IMAP') : false;
70   
71    $create = rcube_charset_convert($create, 'UTF7-IMAP');
72    $foldersplit = explode($delimiter, $create);
73    $display_create = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', substr_count($create, $delimiter)) . $foldersplit[count($foldersplit)-1];
74
75    $OUTPUT->command('add_folder_row', $create, $display_create, false, $before);
76    }
77  else if (!$create)
78    {
79    $OUTPUT->show_message('errorsaving', 'error');
80    }
81  }
82
83// rename a mailbox
84else if ($RCMAIL->action=='rename-folder')
85  {
86  if (!empty($_POST['_folder_oldname']) && !empty($_POST['_folder_newname']))
87    {
88    $name_utf8 = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST));
89    $oldname_utf8 = get_input_value('_folder_oldname', RCUBE_INPUT_POST);
90    $name = rcube_charset_convert($name_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
91    $oldname = rcube_charset_convert($oldname_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
92
93    $rename = $IMAP->rename_mailbox($oldname, $name);
94    }
95
96  // update per-folder options for modified folder and its subfolders
97  if ($rename) {
98    $a_threaded = $RCMAIL->config->get('message_threading', array());
99    $delimiter = $IMAP->get_hierarchy_delimiter();
100    $oldprefix = '/^' . preg_quote($oldname . $delimiter, '/') . '/';
101    foreach ($a_threaded as $key => $val)
102      if ($key == $oldname) {
103        unset($a_threaded[$key]);
104        $a_threaded[$name] = true;
105        }
106      else if (preg_match($oldprefix, $key)) {
107        unset($a_threaded[$key]);
108        $a_threaded[preg_replace($oldprefix, $name.$delimiter, $key)] = true;     
109      }
110     
111    $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
112    }
113
114  if ($rename && $OUTPUT->ajax_call)
115    {
116    $folderlist = $IMAP->list_unsubscribed();
117    $delimiter = $IMAP->get_hierarchy_delimiter();
118
119    $regexp = '/^' . preg_quote($rename . $delimiter, '/') . '/';
120
121    // subfolders
122    for ($x=sizeof($folderlist)-1; $x>=0; $x--)
123      {
124      if (preg_match($regexp, $folderlist[$x]))
125        {
126        $oldfolder = $oldname . $delimiter . preg_replace($regexp, '', $folderlist[$x]);
127        $foldersplit = explode($delimiter, $folderlist[$x]);
128        $level = count($foldersplit) - 1;
129        $display_rename = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level)
130          . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
131
132        $before = isset($folderlist[$x+1]) ? rcube_charset_convert($folderlist[$x+1], 'UTF7-IMAP') : false;
133       
134        $OUTPUT->command('replace_folder_row', rcube_charset_convert($oldfolder, 'UTF7-IMAP'),
135          rcube_charset_convert($folderlist[$x], 'UTF7-IMAP'), $display_rename, $before);
136        }
137      }
138
139    $foldersplit = explode($delimiter, $rename);
140    $level = count($foldersplit) - 1;
141    $display_rename = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
142    $index = array_search($rename, $folderlist);
143    $before = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF7-IMAP') : false;
144
145    $OUTPUT->command('replace_folder_row', $oldname_utf8, rcube_charset_convert($rename, 'UTF7-IMAP'), $display_rename, $before);
146    $OUTPUT->command('reset_folder_rename');
147    }
148  else if (!$rename && $OUTPUT->ajax_call)
149    {
150    $OUTPUT->command('reset_folder_rename');
151    $OUTPUT->show_message('errorsaving', 'error');
152    }
153  else if (!$rename)
154    $OUTPUT->show_message('errorsaving', 'error');
155  }
156
157// delete an existing IMAP mailbox
158else if ($RCMAIL->action=='delete-folder')
159  {
160  $a_mboxes = $IMAP->list_unsubscribed();
161  $delimiter = $IMAP->get_hierarchy_delimiter();
162 
163  $mboxes_utf8 = get_input_value('_mboxes', RCUBE_INPUT_POST);
164  $mboxes = rcube_charset_convert($mboxes_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
165
166  if ($mboxes)
167    $deleted = $IMAP->delete_mailbox(array($mboxes));
168
169  if ($OUTPUT->ajax_call && $deleted)
170    {
171    $OUTPUT->command('remove_folder_row', $mboxes_utf8);
172    foreach ($a_mboxes as $mbox)
173      {
174      if (preg_match('/^'. preg_quote($mboxes.$delimiter, '/') .'/', $mbox))
175        {
176        $OUTPUT->command('remove_folder_row', rcube_charset_convert($mbox, 'UTF7-IMAP'));
177        }
178      }
179    $OUTPUT->show_message('folderdeleted', 'confirmation');
180    }
181  else if (!$deleted)
182    {
183    $OUTPUT->show_message('errorsaving', 'error');
184    }
185  }
186
187if ($OUTPUT->ajax_call)
188  $OUTPUT->send();
189
190
191// build table with all folders listed by server
192function rcube_subscription_form($attrib)
193  {
194  global $RCMAIL, $IMAP, $CONFIG, $OUTPUT;
195
196  $threading_supported = $IMAP->get_capability('thread=references')
197    || $IMAP->get_capability('thread=orderedsubject')
198    || $IMAP->get_capability('thread=refs');
199
200  list($form_start, $form_end) = get_form_tags($attrib, 'folders');
201  unset($attrib['form']);
202 
203  if (!$attrib['id'])
204    $attrib['id'] = 'rcmSubscriptionlist';
205
206  $table = new html_table();
207
208  // add table header
209  $table->add_header('name', rcube_label('foldername'));
210  $table->add_header('msgcount', rcube_label('messagecount'));
211  $table->add_header('subscribed', rcube_label('subscribed'));
212  if ($threading_supported)
213    $table->add_header('threaded', rcube_label('threaded'));
214  $table->add_header('rename', '&nbsp;');
215  $table->add_header('delete', '&nbsp;');
216
217  // get folders from server
218  $IMAP->clear_cache('mailboxes');
219
220  $a_unsubscribed = $IMAP->list_unsubscribed();
221  $a_subscribed = $IMAP->list_mailboxes();
222  $a_threaded = $a_threaded_copy = $RCMAIL->config->get('message_threading', array());
223  $delimiter = $IMAP->get_hierarchy_delimiter();
224  $a_js_folders = $seen_folders = $list_folders = array();
225
226  // pre-process folders list
227  foreach ($a_unsubscribed as $i => $folder) {
228    $foldersplit = explode($delimiter, $folder);
229    $name = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP');
230    $parent_folder = join($delimiter, $foldersplit);
231    $level = count($foldersplit);
232
233    // add any necessary "virtual" parent folders
234    if ($parent_folder && !$seen[$parent_folder]) {
235      for ($i=1; $i<=$level; $i++) {
236        $ancestor_folder = join($delimiter, array_slice($foldersplit, 0, $i));
237        if ($ancestor_folder && !$seen[$ancestor_folder]++) {
238          $ancestor_name = rcube_charset_convert($foldersplit[$i-1], 'UTF7-IMAP');
239          $list_folders[] = array('id' => $ancestor_folder, 'name' => $ancestor_name, 'level' => $i-1, 'virtual' => true);
240        }
241      }
242    }
243   
244    unset($a_threaded_copy[$folder]);
245   
246    $list_folders[] = array('id' => $folder, 'name' => $name, 'level' => $level);
247    $seen[$folder]++;
248  }
249
250  // remove 'message_threading' option for not existing folders
251  if ($a_threaded_copy) {
252    foreach ($a_threaded_copy as $key => $val)
253      unset($a_threaded[$key]);
254    unset($a_threaded_copy);
255    $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
256  }
257
258  $checkbox_subscribe = new html_checkbox(array(
259    'name' => '_subscribed[]',
260    'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)",
261  ));
262  $checkbox_threaded = new html_checkbox(array(
263    'name' => '_threaded[]',
264    'onclick' => JS_OBJECT_NAME.".command(this.checked?'enable-threading':'disable-threading',this.value)",
265  ));
266 
267  if (!empty($attrib['deleteicon']))
268    $del_button = html::img(array('src' => $CONFIG['skin_path'] . $attrib['deleteicon'], 'alt' => rcube_label('delete')));
269  else
270    $del_button = rcube_label('delete');
271
272  if (!empty($attrib['renameicon']))
273    $edit_button = html::img(array('src' => $CONFIG['skin_path'] . $attrib['renameicon'], 'alt' => rcube_label('rename')));
274  else
275    $edit_button = rcube_label('rename');
276   
277  // create list of available folders
278  foreach ($list_folders as $i => $folder) {
279    $idx = $i + 1;
280    $subscribed = in_array($folder['id'], $a_subscribed);
281    $threaded = $a_threaded[$folder['id']];
282    $protected = ($CONFIG['protect_default_folders'] == true && in_array($folder['id'], $CONFIG['default_imap_folders']));
283    $classes = array($i%2 ? 'even' : 'odd');
284    $folder_js = JQ($folder['id']);
285    $display_folder = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $folder['level']) . ($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
286    $folder_utf8 = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
287   
288    if ($folder['virtual'])
289      $classes[] = 'virtual';
290   
291    $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes)));
292   
293    $table->add('name', Q($display_folder));
294    $table->add('msgcount', ($folder['virtual'] ? '' : $IMAP->messagecount($folder['id'], 'ALL', false, false)));
295    $table->add('subscribed', $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''),
296      array('value' => $folder_utf8, 'disabled' => $protected ? 'disabled' : '')));
297    if ($threading_supported) {
298      $table->add('threaded', $folder['virtual'] ? '' :
299            $checkbox_threaded->show(($threaded ? $folder_utf8 : ''), array('value' => $folder_utf8)));
300    }
301   
302    // add rename and delete buttons
303    if (!$protected && !$folder['virtual']) {
304      $table->add('rename', html::a(array('href' => "#rename", 'title' => rcube_label('renamefolder')), $edit_button));
305      $table->add('delete', html::a(array('href' => "#delete", 'title' => rcube_label('deletefolder')), $del_button));
306    }
307    else {
308      $table->add('rename', '&nbsp;');
309      $table->add('delete', '&nbsp;');
310    }
311   
312    $a_js_folders['rcmrow'.$idx] = array($folder_utf8, $display_folder, $protected || $folder['virtual']);
313  }
314
315  rcmail::get_instance()->plugins->exec_hook('folders_list', array('table'=>$table));
316
317  $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
318  $OUTPUT->set_env('subscriptionrows', $a_js_folders);
319  $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
320  $OUTPUT->set_env('delimiter', $delimiter);
321
322  return $form_start . $table->show($attrib) . $form_end;
323  }
324
325
326function rcube_create_folder_form($attrib)
327  {
328  global $OUTPUT;
329 
330  list($form_start, $form_end) = get_form_tags($attrib, 'create-folder');
331  unset($attrib['form']);
332
333  if ($attrib['hintbox'])
334    $OUTPUT->add_gui_object('createfolderhint', $attrib['hintbox']);
335
336  // return the complete edit form as table
337  $out = "$form_start\n";
338
339  $input = new html_inputfield(array('name' => '_folder_name'));
340  $out .= $input->show();
341 
342  if (get_boolean($attrib['button']))
343    {
344    $button = new html_inputfield(array('type' => 'button',
345                                    'value' => rcube_label('create'),
346                                    'onclick' => JS_OBJECT_NAME.".command('create-folder',this.form)"));
347    $out .= $button->show();
348    }
349
350  $out .= "\n$form_end";
351
352  return $out;
353  }
354
355function rcube_rename_folder_form($attrib)
356  {
357  global $CONFIG, $IMAP;
358
359  list($form_start, $form_end) = get_form_tags($attrib, 'rename-folder');
360  unset($attrib['form']);
361
362  // return the complete edit form as table
363  $out = "$form_start\n";
364
365  $a_unsubscribed = $IMAP->list_unsubscribed();
366  $select_folder = new html_select(array('name' => '_folder_oldname', 'id' => 'rcmfd_oldfolder'));
367
368  foreach ($a_unsubscribed as $i => $folder)
369    {
370    if ($CONFIG['protect_default_folders'] == TRUE && in_array($folder,$CONFIG['default_imap_folders']))
371      continue;
372
373    $select_folder->add($folder);
374    }
375
376  $out .= $select_folder->show();
377
378  $out .= " to ";
379  $inputtwo = new html_inputfield(array('name' => '_folder_newname'));
380  $out .= $inputtwo->show();
381
382  if (get_boolean($attrib['button']))
383    {
384    $button = new html_inputfield(array('type' => 'button',
385                                    'value' => rcube_label('rename'),
386                                    'onclick' => JS_OBJECT_NAME.".command('rename-folder',this.form)"));
387    $out .= $button->show();
388    }
389
390  $out .= "\n$form_end";
391 
392  return $out;
393  }
394
395
396// (un)set 'threading' for selected folder
397function rcube_set_threading($mbox, $state=true)
398  {
399  global $RCMAIL;
400  $mbox = (array)$mbox;
401  $a_prefs = (array)$RCMAIL->config->get('message_threading');
402
403  if ($state) {
404    foreach ($mbox as $box)
405      $a_prefs[$box] = true;
406    }
407  else {
408    foreach ($mbox as $box)
409      unset($a_prefs[$box]);
410    }
411
412  $RCMAIL->user->save_prefs(array('message_threading' => $a_prefs));
413  }
414
415
416$OUTPUT->set_pagetitle(rcube_label('folders'));
417$OUTPUT->include_script('list.js');
418
419// register UI objects
420$OUTPUT->add_handlers(array(
421  'foldersubscription' => 'rcube_subscription_form',
422  'createfolder' => 'rcube_create_folder_form',
423  'renamefolder' => 'rcube_rename_folder_form'
424));
425
426// add some labels to client
427$OUTPUT->add_label('deletefolderconfirm','addsubfolderhint','forbiddencharacter','folderdeleting','folderrenaming','foldercreating','foldermoving');
428
429$OUTPUT->send('managefolders');
430
Note: See TracBrowser for help on using the repository browser.