source: github/program/steps/settings/folders.inc @ 18a3dca

HEADcourier-fixdev-browser-capabilitiespdorelease-0.8
Last change on this file since 18a3dca was 18a3dca, checked in by alecpl <alec@…>, 15 months ago
  • Fix issue with folder creation under INBOX. namespace (#1488349)
  • Property mode set to 100644
File size: 14.7 KB
Line 
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, The Roundcube Dev Team                       |
9 |                                                                       |
10 | Licensed under the GNU General Public License version 3 or            |
11 | any later version with exceptions for skins & plugins.                |
12 | See the README file for a full license statement.                     |
13 |                                                                       |
14 | PURPOSE:                                                              |
15 |   Provide functionality of folders management                         |
16 |                                                                       |
17 +-----------------------------------------------------------------------+
18 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19 | Author: Aleksander Machniak <alec@alec.pl>                            |
20 +-----------------------------------------------------------------------+
21
22 $Id$
23
24*/
25
26// WARNING: folder names in UI are encoded with RCMAIL_CHARSET
27
28// init IMAP connection
29$STORAGE = $RCMAIL->get_storage();
30
31// subscribe mailbox
32if ($RCMAIL->action == 'subscribe')
33{
34    $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
35    if (strlen($mbox)) {
36        $result = $STORAGE->subscribe(array($mbox));
37
38        // Handle virtual (non-existing) folders
39        if (!$result && $STORAGE->get_error_code() == -1 &&
40            $STORAGE->get_response_code() == rcube_storage::TRYCREATE
41        ) {
42            $result = $STORAGE->create_folder($mbox, true);
43            if ($result) {
44                // @TODO: remove 'virtual' class of folder's row
45            }
46        }
47
48        if ($result) {
49            // Handle subscription of protected folder (#1487656)
50            if ($CONFIG['protect_default_folders'] == true
51                && in_array($mbox, $CONFIG['default_folders'])
52            ) {
53                $OUTPUT->command('disable_subscription', $mbox);
54            }
55
56            $OUTPUT->show_message('foldersubscribed', 'confirmation');
57        }
58        else
59            rcmail_display_server_error('errorsaving');
60    }
61}
62
63// unsubscribe mailbox
64else if ($RCMAIL->action == 'unsubscribe')
65{
66    $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP');
67    if (strlen($mbox)) {
68        $result = $STORAGE->unsubscribe(array($mbox));
69        if ($result)
70            $OUTPUT->show_message('folderunsubscribed', 'confirmation');
71        else
72            rcmail_display_server_error('errorsaving');
73    }
74}
75
76// delete an existing mailbox
77else if ($RCMAIL->action == 'delete-folder')
78{
79    $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
80    $mbox      = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
81
82    if (strlen($mbox)) {
83        $plugin = $RCMAIL->plugins->exec_hook('folder_delete', array('name' => $mbox));
84
85        if (!$plugin['abort']) {
86            $deleted = $STORAGE->delete_folder($plugin['name']);
87        }
88        else {
89            $deleted = $plugin['result'];
90        }
91    }
92
93    if ($OUTPUT->ajax_call && $deleted) {
94        // Remove folder and subfolders rows
95        $OUTPUT->command('remove_folder_row', $mbox_utf8, true);
96        $OUTPUT->show_message('folderdeleted', 'confirmation');
97        // Clear content frame
98        $OUTPUT->command('subscription_select');
99        $OUTPUT->command('set_quota', rcmail_quota_content());
100    }
101    else if (!$deleted) {
102        rcmail_display_server_error('errorsaving');
103    }
104}
105
106// rename an existing mailbox
107else if ($RCMAIL->action == 'rename-folder')
108{
109    $name_utf8    = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST, true));
110    $oldname_utf8 = trim(get_input_value('_folder_oldname', RCUBE_INPUT_POST, true));
111
112    if (strlen($name_utf8) && strlen($oldname_utf8)) {
113        $name    = rcube_charset_convert($name_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
114        $oldname = rcube_charset_convert($oldname_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
115
116        $rename = rcmail_rename_folder($oldname, $name);
117    }
118
119    if ($rename && $OUTPUT->ajax_call) {
120        rcmail_update_folder_row($name, $oldname);
121    }
122    else if (!$rename) {
123        rcmail_display_server_error('errorsaving');
124    }
125}
126
127// clear mailbox
128else if ($RCMAIL->action == 'purge')
129{
130    $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true);
131    $mbox      = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
132    $delimiter = $STORAGE->get_hierarchy_delimiter();
133    $trash_regexp = '/^' . preg_quote($CONFIG['trash_mbox'] . $delimiter, '/') . '/';
134
135    // we should only be purging trash (or their subfolders)
136    if (!strlen($CONFIG['trash_mbox']) || $mbox == $CONFIG['trash_mbox']
137        || preg_match($trash_regexp, $mbox)
138    ) {
139        $success = $STORAGE->delete_message('*', $mbox);
140        $delete = true;
141    }
142    // copy to Trash
143    else {
144        $success = $STORAGE->move_message('1:*', $CONFIG['trash_mbox'], $mbox);
145        $delete = false;
146    }
147
148    if ($success) {
149        $OUTPUT->set_env('messagecount', 0);
150        if ($delete) {
151            $OUTPUT->show_message('folderpurged', 'confirmation');
152            $OUTPUT->command('set_quota', rcmail_quota_content());
153        }
154        else {
155            $OUTPUT->show_message('messagemoved', 'confirmation');
156        }
157        $_SESSION['unseen_count'][$mbox] = 0;
158        $OUTPUT->command('show_folder', $mbox_utf8, null, true);
159    }
160    else {
161        rcmail_display_server_error('errorsaving');
162    }
163}
164
165// get mailbox size
166else if ($RCMAIL->action == 'folder-size')
167{
168    $name = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true));
169
170    $size = $STORAGE->folder_size($name);
171
172    // @TODO: check quota and show percentage usage of specified mailbox?
173
174    if ($size !== false) {
175        $OUTPUT->command('folder_size_update', show_bytes($size));
176    }
177    else {
178        rcmail_display_server_error();
179    }
180}
181
182if ($OUTPUT->ajax_call)
183    $OUTPUT->send();
184
185
186// build table with all folders listed by server
187function rcube_subscription_form($attrib)
188{
189    global $RCMAIL, $OUTPUT;
190
191    list($form_start, $form_end) = get_form_tags($attrib, 'folders');
192    unset($attrib['form']);
193
194    if (!$attrib['id'])
195        $attrib['id'] = 'rcmSubscriptionlist';
196
197    $table = new html_table();
198
199    if ($attrib['noheader'] !== true && $attrib['noheader'] != "true") {
200        // add table header
201        $table->add_header('name', rcube_label('foldername'));
202        $table->add_header('subscribed', '');
203    }
204
205    $STORAGE = $RCMAIL->get_storage();
206
207    // get folders from server
208    $STORAGE->clear_cache('mailboxes', true);
209
210    $a_unsubscribed = $STORAGE->list_folders();
211    $a_subscribed   = $STORAGE->list_folders_subscribed('', '*', null, null, true); // unsorted
212    $delimiter      = $STORAGE->get_hierarchy_delimiter();
213    $namespace      = $STORAGE->get_namespace();
214    $a_js_folders   = array();
215    $seen           = array();
216    $list_folders   = array();
217
218    $default_folders = (array) $RCMAIL->config->get('default_folders');
219    $protect_default = $RCMAIL->config->get('protect_default_folders');
220
221    // pre-process folders list
222    foreach ($a_unsubscribed as $i => $folder) {
223        $folder_id     = $folder;
224        $folder        = $STORAGE->mod_folder($folder);
225        $foldersplit   = explode($delimiter, $folder);
226        $name          = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP');
227        $parent_folder = join($delimiter, $foldersplit);
228        $level         = count($foldersplit);
229
230        // add any necessary "virtual" parent folders
231        if ($parent_folder && !isset($seen[$parent_folder])) {
232            for ($i=1; $i<=$level; $i++) {
233                    $ancestor_folder = join($delimiter, array_slice($foldersplit, 0, $i));
234                    if ($ancestor_folder && !$seen[$ancestor_folder]++) {
235                        $ancestor_name = rcube_charset_convert($foldersplit[$i-1], 'UTF7-IMAP');
236                        $list_folders[] = array(
237                        'id'      => $ancestor_folder,
238                        'name'    => $ancestor_name,
239                        'level'   => $i-1,
240                        'virtual' => true,
241                    );
242                    }
243            }
244        }
245
246        // Handle properly INBOX.INBOX situation
247        if (isset($seen[$folder])) {
248            continue;
249        }
250
251        $seen[$folder]++;
252
253        $list_folders[] = array(
254            'id'    => $folder_id,
255            'name'  => $name,
256            'level' => $level,
257        );
258    }
259
260    unset($seen);
261
262    // add drop-target representing 'root'
263    $table->add_row(array('id' => 'mailboxroot', 'class' => 'virtual root'));
264    $table->add('name', '&nbsp;');
265    $table->add(null, '&nbsp;');
266
267    $a_js_folders['mailboxroot'] = array('', '', true);
268
269    $checkbox_subscribe = new html_checkbox(array(
270        'name'    => '_subscribed[]',
271        'title'   => rcube_label('changesubscription'),
272        'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)",
273    ));
274
275    // create list of available folders
276    foreach ($list_folders as $i => $folder) {
277        $idx        = $i + 1;
278        $sub_key    = array_search($folder['id'], $a_subscribed);
279        $subscribed = $sub_key !== false;
280        $protected  = $protect_default && in_array($folder['id'], $default_folders);
281        $noselect   = false;
282        $classes    = array($i%2 ? 'even' : 'odd');
283
284        $folder_js      = Q($folder['id']);
285        $folder_utf8    = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
286        $display_folder = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $folder['level'])
287            . Q($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
288
289        if ($folder['virtual']) {
290            $classes[] = 'virtual';
291        }
292
293        if (!$protected) {
294            $attrs = $STORAGE->folder_attributes($folder['id']);
295            $noselect = in_array('\\Noselect', $attrs);
296        }
297
298        $disabled = (($protected && $subscribed) || $noselect);
299
300        // check if the folder is a namespace prefix, then disable subscription option on it
301        if (!$disabled && $folder['virtual'] && $folder['level'] == 0 && !empty($namespace)) {
302            $fname = $folder['id'] . $delimiter;
303            foreach ($namespace as $ns) {
304                if (is_array($ns)) {
305                    foreach ($ns as $item) {
306                        if ($item[0] === $fname) {
307                            $disabled = true;
308                            break 2;
309                        }
310                    }
311                }
312            }
313        }
314        // check if the folder is an other users virtual-root folder, then disable subscription option on it
315        if (!$disabled && $folder['virtual'] && $folder['level'] == 1
316            && !empty($namespace) && !empty($namespace['other'])
317        ) {
318            $parts = explode($delimiter, $folder['id']);
319            $fname = $parts[0] . $delimiter;
320            foreach ($namespace['other'] as $item) {
321                if ($item[0] === $fname) {
322                    $disabled = true;
323                    break;
324                }
325            }
326        }
327        // check if the folder is shared, then disable subscription option on it
328        if (!$disabled && $folder['virtual'] && !empty($namespace)) {
329            $tmp_ns = array_merge((array)$namespace['other'], (array)$namespace['shared']);
330            foreach ($tmp_ns as $item) {
331                if (strpos($folder['id'], $item[0]) === 0) {
332                    $disabled = true;
333                    break;
334                }
335            }
336        }
337
338        $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes),
339            'foldername' => $folder['id']));
340
341        $table->add('name', $display_folder);
342        $table->add('subscribed', $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''),
343            array('value' => $folder_utf8, 'disabled' => $disabled ? 'disabled' : '')));
344
345        $a_js_folders['rcmrow'.$idx] = array($folder_utf8,
346            Q($display_folder), $protected || $folder['virtual']);
347    }
348
349    $RCMAIL->plugins->exec_hook('folders_list', array('table' => $table));
350
351    $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
352    $OUTPUT->set_env('subscriptionrows', $a_js_folders);
353    $OUTPUT->set_env('defaultfolders', $default_folders);
354    $OUTPUT->set_env('delimiter', $delimiter);
355
356    return $form_start . $table->show($attrib) . $form_end;
357}
358
359function rcmail_folder_frame($attrib)
360{
361    global $OUTPUT;
362
363    if (!$attrib['id'])
364        $attrib['id'] = 'rcmfolderframe';
365
366    $attrib['name'] = $attrib['id'];
367
368    $OUTPUT->set_env('contentframe', $attrib['name']);
369    $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
370
371    return html::iframe($attrib);
372}
373
374function rcmail_rename_folder($oldname, $newname)
375{
376    global $RCMAIL;
377
378    $storage   = $RCMAIL->get_storage();
379    $delimiter = $storage->get_hierarchy_delimiter();
380
381    $plugin = $RCMAIL->plugins->exec_hook('folder_rename', array(
382        'oldname' => $oldname, 'newname' => $newname));
383
384    if (!$plugin['abort']) {
385        $renamed =  $storage->rename_folder($oldname, $newname);
386    }
387    else {
388        $renamed = $plugin['result'];
389    }
390
391    // update per-folder options for modified folder and its subfolders
392    if ($renamed) {
393        $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
394        $oldprefix  = '/^' . preg_quote($oldname . $delimiter, '/') . '/';
395
396        foreach ($a_threaded as $key => $val) {
397            if ($key == $oldname) {
398                unset($a_threaded[$key]);
399                $a_threaded[$newname] = true;
400            }
401            else if (preg_match($oldprefix, $key)) {
402                unset($a_threaded[$key]);
403                    $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = true;
404            }
405        }
406        $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
407
408        return true;
409    }
410
411    return false;
412}
413
414
415$OUTPUT->set_pagetitle(rcube_label('folders'));
416$OUTPUT->include_script('list.js');
417$OUTPUT->set_env('quota', $STORAGE->get_capability('QUOTA'));
418$OUTPUT->set_env('prefix_ns', $STORAGE->get_namespace('prefix'));
419
420// add some labels to client
421$OUTPUT->add_label('deletefolderconfirm', 'purgefolderconfirm', 'folderdeleting',
422    'foldermoving', 'foldersubscribing', 'folderunsubscribing', 'quota');
423
424// register UI objects
425$OUTPUT->add_handlers(array(
426    'foldersubscription' => 'rcube_subscription_form',
427    'folderframe' => 'rcmail_folder_frame',
428    'quotadisplay' => 'rcmail_quota_display',
429));
430
431$OUTPUT->send('folders');
432
Note: See TracBrowser for help on using the repository browser.