Encoded From headers now properly quoted (#2830141). A better fix might be to re...
[squirrelmail.git] / functions / folder_manip.php
CommitLineData
32aa0074 1<?php
2
3/**
aa00e648 4 * folder_manip.php
5 *
aa00e648 6 * Functions for IMAP folder manipulation:
7 * (un)subscribe, create, rename, delete.
8 *
4b4abf93 9 * @author Thijs Kinkhorst <kink at squirrelmail.org>
1977ab55 10 * @copyright 1999-2010 The SquirrelMail Project Team
4b4abf93 11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
aa00e648 12 * @version $Id$
13 * @package squirrelmail
14 * @see folders.php
aa00e648 15 */
32aa0074 16
17
18/**
19 * Helper function for the functions below; checks if the user entered
20 * folder name is valid according to the IMAP standard. If not, it
21 * bails out with an error and cleanly terminates the IMAP connection.
22 */
23function folders_checkname($imapConnection, $folder_name, $delimiter)
24{
25 if (substr_count($folder_name, '"') || substr_count($folder_name, "\\") ||
26 substr_count($folder_name, $delimiter) || ($folder_name == '')) {
f8a1ed5a 27
1b858d86 28 global $color, $oTemplate;
1dce0de7 29 error_box(_("Illegal folder name.") . "<br />\n" .
f8a1ed5a 30 sprintf(_("The name may not contain any of the following: %s"), '<tt>" \\ '.$delimiter.'</tt>')
31 . "<br />\n" .
32 _("Please select a different name.").
33 '<br /><a href="folders.php">'.
1b858d86 34 _("Click here to go back") . '</a>.');
aa00e648 35
32aa0074 36 sqimap_logout($imapConnection);
1b858d86 37 $oTemplate->display('footer.tpl');
aa00e648 38 exit;
32aa0074 39 }
40}
41
42/**
43 * Called from folders.php to create a new folder.
940ac569 44 * @param stream $imapConnection imap connection resource
45 * @param string $delimiter delimiter
51fa8319 46 * @param string $folder_name new folder name
940ac569 47 * @param string $subfolder folder that stores new folder
51fa8319 48 * @param string $contain_subs if not empty, creates folder that can store subfolders
940ac569 49 * @since 1.5.1
32aa0074 50 */
51function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs)
52{
53 folders_checkname($imapConnection, $folder_name, $delimiter);
54
55 global $folder_prefix;
56
57 $folder_name = imap_utf7_encode_local($folder_name);
f8a1ed5a 58
32aa0074 59 if ( ! empty($contain_subs) ) {
940ac569 60 $folder_type = 'noselect';
61 } else {
62 $folder_type = '';
32aa0074 63 }
64
65 if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) {
66 $folder_prefix = $folder_prefix . $delimiter;
67 }
68 if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) {
69 $subfolder_orig = $subfolder;
70 $subfolder = $folder_prefix . $subfolder;
71 } else {
72 $subfolder_orig = $subfolder;
73 }
74
75 if (trim($subfolder_orig) == '') {
940ac569 76 sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, $folder_type);
32aa0074 77 } else {
940ac569 78 sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, $folder_type);
32aa0074 79 }
80
81 return;
82}
83
84/**
85 * Called from folders.php, given a folder name, ask the user what this
86 * folder should be renamed to.
87 */
88function folders_rename_getname ($imapConnection, $delimiter, $old) {
9829e1d8 89 global $color, $default_folder_prefix, $oTemplate;
32aa0074 90
91 if ( $old == '' ) {
92 plain_error_message(_("You have not selected a folder to rename. Please do so.").
93 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
f8a1ed5a 94 sqimap_logout($imapConnection);
9829e1d8 95 $oTemplate->display('footer.tpl');
32aa0074 96 exit;
97 }
98
99 if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
100 $isfolder = TRUE;
101 $old = substr($old, 0, strlen($old) - 1);
102 } else {
103 $isfolder = FALSE;
104 }
105
106 $old = imap_utf7_decode_local($old);
107
108 if (strpos($old, $delimiter)) {
956f804a 109 $old_name = substr($old, strrpos($old, $delimiter)+1);
aa00e648 110 // hide default prefix (INBOX., mail/ or other)
111 $quoted_prefix=preg_quote($default_folder_prefix,'/');
112 $prefix_length=(preg_match("/^$quoted_prefix/",$old) ? strlen($default_folder_prefix) : 0);
113 if ($prefix_length>strrpos($old, $delimiter)) {
114 $old_parent = '';
115 } else {
116 $old_parent = substr($old, $prefix_length, (strrpos($old, $delimiter)-$prefix_length))
117 . ' ' . $delimiter;
118 }
32aa0074 119 } else {
120 $old_name = $old;
121 $old_parent = '';
122 }
9829e1d8 123
124 sqimap_logout($imapConnection);
32aa0074 125
9829e1d8 126 $oTemplate->assign('dialog_type', 'rename');
301d1912 127 $oTemplate->assign('parent_folder', htmlspecialchars($old_parent));
128 $oTemplate->assign('current_full_name', htmlspecialchars($old));
129 $oTemplate->assign('current_folder_name', htmlspecialchars($old_name));
130 $oTemplate->assign('is_folder', $isfolder);
9829e1d8 131
132 $oTemplate->display('folder_manip_dialog.tpl');
133 $oTemplate->display('footer.tpl');
f8a1ed5a 134
32aa0074 135 exit;
136}
137
138/**
139 * Given an old and new folder name, renames the folder.
140 */
141function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
142{
143 folders_checkname($imapConnection, $new_name, $delimiter);
144
145 $orig = imap_utf7_encode_local($orig);
146 $old_name = imap_utf7_encode_local($old_name);
147 $new_name = imap_utf7_encode_local($new_name);
f8a1ed5a 148
32aa0074 149 if ($old_name != $new_name) {
150
151 if (strpos($orig, $delimiter)) {
152 $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
153 } else {
154 $old_dir = '';
155 }
156
157 if ($old_dir != '') {
158 $newone = $old_dir . $delimiter . $new_name;
159 } else {
160 $newone = $new_name;
161 }
162
163 // Renaming a folder doesn't rename the folder but leaves you unsubscribed
164 // at least on Cyrus IMAP servers.
165 if (isset($isfolder)) {
166 $newone = $newone.$delimiter;
167 $orig = $orig.$delimiter;
168 }
169 sqimap_mailbox_rename( $imapConnection, $orig, $newone );
170
171 }
172
173 return;
174}
175
176/**
177 * Presents a confirmation dialog to the user asking whether they're
178 * sure they want to delete this folder.
179 */
180function folders_delete_ask ($imapConnection, $folder_name)
181{
9829e1d8 182 global $color, $default_folder_prefix, $oTemplate;
32aa0074 183
184 if ($folder_name == '') {
185 plain_error_message(_("You have not selected a folder to delete. Please do so.").
186 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
9829e1d8 187 sqimap_logout($imapConnection);
188 $oTemplate->display('footer.tpl');
32aa0074 189 exit;
190 }
191
aa00e648 192 // hide default folder prefix (INBOX., mail/ or other)
193 $visible_folder_name = imap_utf7_decode_local($folder_name);
194 $quoted_prefix = preg_quote($default_folder_prefix,'/');
195 $prefix_length = (preg_match("/^$quoted_prefix/",$visible_folder_name) ? strlen($default_folder_prefix) : 0);
196 $visible_folder_name = substr($visible_folder_name,$prefix_length);
197
f8a1ed5a 198 sqimap_logout($imapConnection);
9829e1d8 199
200 $oTemplate->assign('dialog_type', 'delete');
9829e1d8 201 $oTemplate->assign('folder_name', htmlspecialchars($folder_name));
202 $oTemplate->assign('visible_folder_name', htmlspecialchars($visible_folder_name));
203
204 $oTemplate->display('folder_manip_dialog.tpl');
205 $oTemplate->display('footer.tpl');
206
32aa0074 207 exit;
208}
209
210/**
211 * Given a folder, moves it to trash (and all subfolders of it too).
212 */
213function folders_delete_do ($imapConnection, $delimiter, $folder_name)
214{
202bcbcc 215 include(SM_PATH . 'functions/tree.php');
f8a1ed5a 216
32aa0074 217 $boxes = sqimap_mailbox_list ($imapConnection);
218
219 global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
220
221 if (substr($folder_name, -1) == $delimiter) {
222 $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
223 } else {
224 $folder_name_no_dm = $folder_name;
225 }
226
227 /** lets see if we CAN move folders to the trash.. otherwise,
228 ** just delete them **/
d8e94f0a 229 if ($delete_folder || preg_match('/^' . preg_quote($trash_folder, '/') . '.+/i', $folder_name) ) {
32aa0074 230 $can_move_to_trash = FALSE;
ceb7ad3c 231 } else {
32aa0074 232 /* Otherwise, check if trash folder exits and support sub-folders */
32aa0074 233 foreach($boxes as $box) {
234 if ($box['unformatted'] == $trash_folder) {
235 $can_move_to_trash = !in_array('noinferiors', $box['flags']);
236 }
237 }
238 }
239
240 /** First create the top node in the tree **/
241 foreach($boxes as $box) {
242 if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
243 $foldersTree[0]['value'] = $folder_name;
244 $foldersTree[0]['doIHaveChildren'] = false;
245 continue;
246 }
247 }
248
249 /* Now create the nodes for subfolders of the parent folder
250 You can tell that it is a subfolder by tacking the mailbox delimiter
251 on the end of the $folder_name string, and compare to that. */
252 foreach($boxes as $box) {
253 if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
254 addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
255 }
256 }
257
258 /** Lets start removing the folders and messages **/
259 if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
260 walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
261 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
262 } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
263 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
264 }
265
266 return;
267}
268
269/**
270 * Given an array of folder_names, subscribes to each of them.
271 */
272function folders_subscribe($imapConnection, $folder_names)
273{
9829e1d8 274 global $color, $oTemplate;
32aa0074 275
276 if (count($folder_names) == 0 || $folder_names[0] == '') {
277 plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
278 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
279 sqimap_logout($imapConnection);
9829e1d8 280 $oTemplate->display('footer.tpl');
32aa0074 281 exit;
282 }
283
284 global $no_list_for_subscribe, $imap_server_type;
285
286 if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
287 /* Cyrus, atleast, does not typically allow subscription to
288 * nonexistent folders (this is an optional part of IMAP),
289 * lets catch it here and report back cleanly. */
290 if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
291 plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
292 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
293 sqimap_logout($imapConnection);
294 exit;
f8a1ed5a 295
32aa0074 296 }
297 }
298 foreach ( $folder_names as $folder_name ) {
299 sqimap_subscribe ($imapConnection, $folder_name);
300 }
301
302 return;
303}
304
305/**
306 * Given a list of folder names, unsubscribes from each of them.
307 */
308function folders_unsubscribe($imapConnection, $folder_names)
309{
9829e1d8 310 global $color, $oTemplate;
32aa0074 311
312 if (count($folder_names) == 0 || $folder_names[0] == '') {
313 plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
314 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
315 sqimap_logout($imapConnection);
9829e1d8 316 $oTemplate->display('footer.tpl');
32aa0074 317 exit;
318 }
319
320 foreach ( $folder_names as $folder_name ) {
321 sqimap_unsubscribe ($imapConnection, $folder_name);
322 }
323
324 return;
325}