]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/settings/save_folder.inc
Imported Upstream version 0.7
[roundcube.git] / program / steps / settings / save_folder.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/settings/save_folder.inc                                |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Provide functionality to create/edit a folder                       |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Aleksander Machniak <alec@alec.pl>                            |
16  +-----------------------------------------------------------------------+
17
18  $Id: save_folder.inc 5096 2011-08-19 08:07:05Z alec $
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();
37 $options = strlen($old_imap) ? rcmail_folder_options($old_imap) : array();
38
39 // Folder name checks
40 if ($options['protected'] || $options['norename']) {
41 }
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 {
62     if ($options['protected'] || $options['norename']) {
63         $name_imap = $old_imap;
64     }
65     else if (strlen($path)) {
66         $name_imap = $path . $delimiter . $name_imap;
67     }
68     else {
69         $name_imap = $RCMAIL->imap->mod_mailbox($name_imap, 'in');
70     }
71 }
72
73 // Check access rights to the parent folder
74 if (!$error && strlen($path) && (!strlen($old_imap) || $old_imap != $name_imap)) {
75     $parent_opts = $RCMAIL->imap->mailbox_info($path);
76     if ($parent_opts['namespace'] != 'personal'
77         && (empty($parent_opts['rights']) || !preg_match('/[ck]/', implode($parent_opts)))
78     ) {
79         $error = rcube_label('parentnotwritable');
80     }
81 }
82
83 if (!$error) {
84     $folder['name']     = $name_imap;
85     $folder['oldname']  = $old_imap;
86     $folder['class']    = '';
87     $folder['options']  = $options;
88     $folder['settings'] = array(
89         // List view mode: 0-list, 1-threads
90         'view_mode'   => (int) get_input_value('_viewmode', RCUBE_INPUT_POST),
91         'sort_column' => get_input_value('_sortcol', RCUBE_INPUT_POST),
92         'sort_order'  => get_input_value('_sortord', RCUBE_INPUT_POST),
93     );
94 }
95
96 // create a new mailbox
97 if (!$error && !strlen($old)) {
98
99     $folder['subscribe'] = true;
100
101     $plugin = $RCMAIL->plugins->exec_hook('folder_create', array('record' => $folder));
102
103     $folder = $plugin['record'];
104
105     if (!$plugin['abort']) {
106         $created = $IMAP->create_mailbox($folder['name'], $folder['subscribe']);
107     }
108     else {
109         $created = $plugin['result'];
110     }
111
112     if ($created) {
113         // Save folder settings
114         if (isset($_POST['_viewmode'])) {
115             $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
116
117             if ($_POST['_viewmode'])
118                 $a_threaded[$folder['name']] = true;
119             else
120                 unset($a_threaded[$folder['name']]);
121
122             $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
123         }
124
125         rcmail_update_folder_row($folder['name'], null, $folder['subscribe'], $folder['class']);
126         $OUTPUT->show_message('foldercreated', 'confirmation');
127         // reset folder preview frame
128         $OUTPUT->command('subscription_select');
129         $OUTPUT->send('iframe');
130     }
131     else {
132         // show error message
133         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
134     }
135 }
136
137 // update a mailbox
138 else if (!$error) {
139     $plugin = $RCMAIL->plugins->exec_hook('folder_update', array('record' => $folder));
140
141     $folder = $plugin['record'];
142     $rename = ($folder['oldname'] != $folder['name']);
143
144     if (!$plugin['abort']) {
145         if ($rename) {
146             $updated = $RCMAIL->imap->rename_mailbox($folder['oldname'], $folder['name']);
147         }
148         else {
149             $updated = true;
150         }
151     }
152     else {
153         $updated = $plugin['result'];
154     }
155
156     if ($updated) {
157         // Update folder settings,
158         if (isset($_POST['_viewmode'])) {
159             $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
160
161             // In case of name change update names of childrens in settings
162             if ($rename) {
163                 $oldprefix  = '/^' . preg_quote($folder['oldname'] . $delimiter, '/') . '/';
164                 foreach ($a_threaded as $key => $val) {
165                     if ($key == $folder['oldname']) {
166                         unset($a_threaded[$key]);
167                     }
168                     else if (preg_match($oldprefix, $key)) {
169                         unset($a_threaded[$key]);
170                             $a_threaded[preg_replace($oldprefix, $folder['name'].$delimiter, $key)] = true;
171                     }
172                 }
173             }
174             if ($_POST['_viewmode'])
175                 $a_threaded[$folder['name']] = true;
176             else
177                 unset($a_threaded[$folder['name']]);
178
179             $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
180         }
181
182         $OUTPUT->show_message('folderupdated', 'confirmation');
183         if ($rename) {
184             rcmail_update_folder_row($folder['name'], $folder['oldname'], $folder['subscribe'], $folder['class']);
185             $OUTPUT->send('iframe');
186         }
187     }
188     else {
189         // show error message
190         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
191     }
192 }
193
194 rcmail_overwrite_action('edit-folder');