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