]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/settings/save_folder.inc
Imported Upstream version 0.5
[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, Roundcube Dev. - Switzerland                 |
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 4304 2010-12-03 10:58:40Z 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 $special   = (strlen($old_imap) && in_array($old_imap, (array) $RCMAIL->config->get('default_imap_folders')));
38 $protected = ($special && $RCMAIL->config->get('protect_default_folders'));
39
40
41 // Folder name checks
42 if ($protected) {
43 }
44 else if (!strlen($name)) {
45     $error = rcube_label('cannotbeempty');
46 }
47 else if (mb_strlen($name) > 128) {
48     $error = rcube_label('nametoolong');
49 }
50 else {
51     // these characters are problematic e.g. when used in LIST/LSUB
52     foreach (array($delimiter, '%', '*') as $char) {
53         if (strpos($name, $delimiter) !== false) {
54             $error = rcube_label('forbiddencharacter') . " ($char)";
55             break;
56         }
57     }
58 }
59
60 if ($error) {
61     $OUTPUT->command('display_message', $error, 'error');
62 }
63 else {
64     if ($protected) {
65         $name_imap = $old_imap;
66     }
67     else if (strlen($path)) {
68         $name_imap = $path . $delimiter . $name_imap;
69     }
70
71     $folder['name']     = $name_imap;
72     $folder['oldname']  = $old_imap;
73     $folder['settings'] = array(
74         // List view mode: 0-list, 1-threads
75         'view_mode'   => (int) get_input_value('_viewmode', RCUBE_INPUT_POST),
76         'sort_column' => get_input_value('_sortcol', RCUBE_INPUT_POST),
77         'sort_order'  => get_input_value('_sortord', RCUBE_INPUT_POST),
78     );
79 }
80
81 // create a new mailbox
82 if (!$error && !strlen($old)) {
83
84     $plugin = $RCMAIL->plugins->exec_hook('folder_create', array('record' => $folder));
85
86     $folder = $plugin['record'];
87
88     if (!$plugin['abort']) {
89         $created = $IMAP->create_mailbox($folder['name'], TRUE);
90     }
91     else {
92         $created = $plugin['result'];
93     }
94
95     if ($created) {
96         // Save folder settings
97         if (isset($_POST['_viewmode'])) {
98             $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
99
100             if ($_POST['_viewmode'])
101                 $a_threaded[$folder['name']] = true;
102             else
103                 unset($a_threaded[$folder['name']]);
104
105             $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
106         }
107
108         $OUTPUT->show_message('foldercreated', 'confirmation');
109         $OUTPUT->command('reload', 250);
110         $OUTPUT->send('iframe');
111     }
112     else {
113         // show error message
114         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
115     }
116 }
117
118 // update a mailbox
119 else if (!$error) {
120     $plugin = $RCMAIL->plugins->exec_hook('folder_update', array('record' => $folder));
121
122     $folder = $plugin['record'];
123     $rename = ($folder['oldname'] != $folder['name']);
124
125     if (!$plugin['abort']) {
126         if ($rename) {
127             $updated = $RCMAIL->imap->rename_mailbox($folder['oldname'], $folder['name']);
128         }
129         else {
130             $updated = true;
131         }
132     }
133     else {
134         $updated = $plugin['result'];
135     }
136
137     if ($updated) {
138         // Update folder settings,
139         if (isset($_POST['_viewmode'])) {
140             $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
141
142             // In case of name change update names of childrens in settings
143             if ($rename) {
144                 $delimiter  = $RCMAIL->imap->get_hierarchy_delimiter();
145                 $oldprefix  = '/^' . preg_quote($folder['oldname'] . $delimiter, '/') . '/';
146                 foreach ($a_threaded as $key => $val) {
147                     if ($key == $folder['oldname']) {
148                         unset($a_threaded[$key]);
149                     }
150                     else if (preg_match($oldprefix, $key)) {
151                         unset($a_threaded[$key]);
152                             $a_threaded[preg_replace($oldprefix, $folder['name'].$delimiter, $key)] = true;
153                     }
154                 }
155             }
156             if ($_POST['_viewmode'])
157                 $a_threaded[$folder['name']] = true;
158             else
159                 unset($a_threaded[$folder['name']]);
160
161             $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
162         }
163
164         $OUTPUT->show_message('folderupdated', 'confirmation');
165         if ($rename) {
166             $OUTPUT->command('reload', 250);
167             $OUTPUT->send('iframe');
168         }
169     }
170     else {
171         // show error message
172         $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
173     }
174 }
175
176 rcmail_overwrite_action('edit-folder');