6 * Functions for IMAP folder manipulation:
7 * (un)subscribe, create, rename, delete.
9 * @author Thijs Kinkhorst <kink at squirrelmail.org>
10 * @copyright © 1999-2006 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
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.
23 function folders_checkname($imapConnection, $folder_name, $delimiter)
25 if (substr_count($folder_name, '"') ||
substr_count($folder_name, "\\") ||
26 substr_count($folder_name, $delimiter) ||
($folder_name == '')) {
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>')
32 _("Please select a different name.").
33 '<br /><a href="folders.php">'.
34 _("Click here to go back") . '</a>.');
36 sqimap_logout($imapConnection);
37 $oTemplate->display('footer.tpl');
43 * Called from folders.php to create a new folder.
44 * @param stream $imapConnection imap connection resource
45 * @param string $delimiter delimiter
46 * @param string $folder_name new folder name
47 * @param string $subfolder folder that stores new folder
48 * @param string $contain_subs if not empty, creates folder that can store subfolders
51 function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs)
53 folders_checkname($imapConnection, $folder_name, $delimiter);
55 global $folder_prefix;
57 $folder_name = imap_utf7_encode_local($folder_name);
59 if ( ! empty($contain_subs) ) {
60 $folder_type = 'noselect';
65 if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) {
66 $folder_prefix = $folder_prefix . $delimiter;
68 if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) {
69 $subfolder_orig = $subfolder;
70 $subfolder = $folder_prefix . $subfolder;
72 $subfolder_orig = $subfolder;
75 if (trim($subfolder_orig) == '') {
76 sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, $folder_type);
78 sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, $folder_type);
85 * Called from folders.php, given a folder name, ask the user what this
86 * folder should be renamed to.
88 function folders_rename_getname ($imapConnection, $delimiter, $old) {
89 global $color, $default_folder_prefix, $oTemplate;
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);
94 sqimap_logout($imapConnection);
95 $oTemplate->display('footer.tpl');
99 if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
101 $old = substr($old, 0, strlen($old) - 1);
106 $old = imap_utf7_decode_local($old);
108 if (strpos($old, $delimiter)) {
109 $old_name = substr($old, strrpos($old, $delimiter)+
1, strlen($old));
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)) {
116 $old_parent = substr($old, $prefix_length, (strrpos($old, $delimiter)-$prefix_length))
124 sqimap_logout($imapConnection);
126 $oTemplate->assign('dialog_type', 'rename');
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);
132 $oTemplate->display('folder_manip_dialog.tpl');
133 $oTemplate->display('footer.tpl');
139 * Given an old and new folder name, renames the folder.
141 function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
143 folders_checkname($imapConnection, $new_name, $delimiter);
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);
149 if ($old_name != $new_name) {
151 if (strpos($orig, $delimiter)) {
152 $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
157 if ($old_dir != '') {
158 $newone = $old_dir . $delimiter . $new_name;
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;
169 sqimap_mailbox_rename( $imapConnection, $orig, $newone );
177 * Presents a confirmation dialog to the user asking whether they're
178 * sure they want to delete this folder.
180 function folders_delete_ask ($imapConnection, $folder_name)
182 global $color, $default_folder_prefix, $oTemplate;
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);
187 sqimap_logout($imapConnection);
188 $oTemplate->display('footer.tpl');
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);
198 sqimap_logout($imapConnection);
200 $oTemplate->assign('dialog_type', 'delete');
201 $oTemplate->assign('folder_name', htmlspecialchars($folder_name));
202 $oTemplate->assign('visible_folder_name', htmlspecialchars($visible_folder_name));
204 $oTemplate->display('folder_manip_dialog.tpl');
205 $oTemplate->display('footer.tpl');
211 * Given a folder, moves it to trash (and all subfolders of it too).
213 function folders_delete_do ($imapConnection, $delimiter, $folder_name)
215 include(SM_PATH
. 'functions/tree.php');
217 $boxes = sqimap_mailbox_list ($imapConnection);
219 global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
221 if (substr($folder_name, -1) == $delimiter) {
222 $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
224 $folder_name_no_dm = $folder_name;
227 /** lets see if we CAN move folders to the trash.. otherwise,
228 ** just delete them **/
229 if ($delete_folder ||
eregi('^'.$trash_folder.'.+', $folder_name) ) {
230 $can_move_to_trash = FALSE;
232 /* Otherwise, check if trash folder exits and support sub-folders */
233 foreach($boxes as $box) {
234 if ($box['unformatted'] == $trash_folder) {
235 $can_move_to_trash = !in_array('noinferiors', $box['flags']);
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;
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);
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);
270 * Given an array of folder_names, subscribes to each of them.
272 function folders_subscribe($imapConnection, $folder_names)
274 global $color, $oTemplate;
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);
280 $oTemplate->display('footer.tpl');
284 global $no_list_for_subscribe, $imap_server_type;
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);
298 foreach ( $folder_names as $folder_name ) {
299 sqimap_subscribe ($imapConnection, $folder_name);
306 * Given a list of folder names, unsubscribes from each of them.
308 function folders_unsubscribe($imapConnection, $folder_names)
310 global $color, $oTemplate;
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);
316 $oTemplate->display('footer.tpl');
320 foreach ( $folder_names as $folder_name ) {
321 sqimap_unsubscribe ($imapConnection, $folder_name);