alecpl
2011-07-05 9d195d6e82c3be4e543a47ef8ff1e9fe54bd0939
commit | author | age
af3c04 1 <?php
A 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/settings/save_folder.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 to create/edit a folder                       |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Aleksander Machniak <alec@alec.pl>                            |
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
28 $name = trim(get_input_value('_name', RCUBE_INPUT_POST, true));
29 $old  = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true));
30 $path = trim(get_input_value('_parent', RCUBE_INPUT_POST, true));
31
32 $name_imap = rcube_charset_convert($name, RCMAIL_CHARSET, 'UTF7-IMAP');
33 $old_imap  = rcube_charset_convert($old, RCMAIL_CHARSET, 'UTF7-IMAP');
34 // $path is in UTF7-IMAP already
35
36 $delimiter = $IMAP->get_hierarchy_delimiter();
254d5e 37 $options = strlen($old_imap) ? rcmail_folder_options($old_imap) : array();
af3c04 38
A 39 // Folder name checks
bbce3e 40 if ($options['protected'] || $options['norename']) {
af3c04 41 }
A 42 else if (!strlen($name)) {
43     $error = rcube_label('cannotbeempty');
44 }
45 else if (mb_strlen($name) > 128) {
46     $error = rcube_label('nametoolong');
47 }
48 else {
49     // these characters are problematic e.g. when used in LIST/LSUB
50     foreach (array($delimiter, '%', '*') as $char) {
51         if (strpos($name, $delimiter) !== false) {
52             $error = rcube_label('forbiddencharacter') . " ($char)";
53             break;
54         }
55     }
56 }
57
58 if ($error) {
59     $OUTPUT->command('display_message', $error, 'error');
60 }
61 else {
bbce3e 62     if ($options['protected'] || $options['norename']) {
af3c04 63         $name_imap = $old_imap;
A 64     }
65     else if (strlen($path)) {
66         $name_imap = $path . $delimiter . $name_imap;
67     }
d08333 68     else {
f6eb1e 69         $name_imap = $RCMAIL->imap->mod_mailbox($name_imap, 'in');
d08333 70     }
af3c04 71
A 72     $folder['name']     = $name_imap;
73     $folder['oldname']  = $old_imap;
1a0343 74     $folder['class']    = '';
67975b 75     $folder['options']  = $options;
af3c04 76     $folder['settings'] = array(
A 77         // List view mode: 0-list, 1-threads
78         'view_mode'   => (int) get_input_value('_viewmode', RCUBE_INPUT_POST),
79         'sort_column' => get_input_value('_sortcol', RCUBE_INPUT_POST),
80         'sort_order'  => get_input_value('_sortord', RCUBE_INPUT_POST),
81     );
82 }
83
84 // create a new mailbox
85 if (!$error && !strlen($old)) {
86
1a0343 87     $folder['subscribe'] = true;
A 88
af3c04 89     $plugin = $RCMAIL->plugins->exec_hook('folder_create', array('record' => $folder));
A 90
91     $folder = $plugin['record'];
92
93     if (!$plugin['abort']) {
1a0343 94         $created = $IMAP->create_mailbox($folder['name'], $folder['subscribe']);
af3c04 95     }
A 96     else {
97         $created = $plugin['result'];
98     }
99
100     if ($created) {
101         // Save folder settings
102         if (isset($_POST['_viewmode'])) {
103             $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
104
105             if ($_POST['_viewmode'])
106                 $a_threaded[$folder['name']] = true;
107             else
108                 unset($a_threaded[$folder['name']]);
109
110             $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
111         }
1a0343 112
A 113         rcmail_update_folder_row($folder['name'], null, $folder['subscribe'], $folder['class']);
af3c04 114         $OUTPUT->show_message('foldercreated', 'confirmation');
1a0343 115         // reset folder preview frame
A 116         $OUTPUT->command('subscription_select');
af3c04 117         $OUTPUT->send('iframe');
A 118     }
119     else {
120         // show error message
121         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
122     }
123 }
124
125 // update a mailbox
126 else if (!$error) {
127     $plugin = $RCMAIL->plugins->exec_hook('folder_update', array('record' => $folder));
128
129     $folder = $plugin['record'];
130     $rename = ($folder['oldname'] != $folder['name']);
131
132     if (!$plugin['abort']) {
133         if ($rename) {
134             $updated = $RCMAIL->imap->rename_mailbox($folder['oldname'], $folder['name']);
135         }
136         else {
137             $updated = true;
138         }
139     }
140     else {
141         $updated = $plugin['result'];
142     }
143
144     if ($updated) {
145         // Update folder settings,
146         if (isset($_POST['_viewmode'])) {
147             $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
148
149             // In case of name change update names of childrens in settings
150             if ($rename) {
151                 $oldprefix  = '/^' . preg_quote($folder['oldname'] . $delimiter, '/') . '/';
152                 foreach ($a_threaded as $key => $val) {
153                     if ($key == $folder['oldname']) {
154                         unset($a_threaded[$key]);
155                     }
156                     else if (preg_match($oldprefix, $key)) {
157                         unset($a_threaded[$key]);
158                           $a_threaded[preg_replace($oldprefix, $folder['name'].$delimiter, $key)] = true;
159                     }
160                 }
161             }
162             if ($_POST['_viewmode'])
163                 $a_threaded[$folder['name']] = true;
164             else
165                 unset($a_threaded[$folder['name']]);
166
167             $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
168         }
169
170         $OUTPUT->show_message('folderupdated', 'confirmation');
171         if ($rename) {
1a0343 172             rcmail_update_folder_row($folder['name'], $folder['oldname'], $folder['subscribe'], $folder['class']);
af3c04 173             $OUTPUT->send('iframe');
A 174         }
175     }
176     else {
177         // show error message
178         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
179     }
180 }
181
182 rcmail_overwrite_action('edit-folder');