46ba4b9d8469beddabd0e1ca6eb4759830d69fcb
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Functions for IMAP folder manipulation:
10 * (un)subscribe, create, rename, delete.
13 * @package squirrelmail
15 * @author Thijs Kinkhorst - kink@squirrelmail.org
20 * Helper function for the functions below; checks if the user entered
21 * folder name is valid according to the IMAP standard. If not, it
22 * bails out with an error and cleanly terminates the IMAP connection.
24 function folders_checkname($imapConnection, $folder_name, $delimiter)
26 if (substr_count($folder_name, '"') ||
substr_count($folder_name, "\\") ||
27 substr_count($folder_name, $delimiter) ||
($folder_name == '')) {
30 plain_error_message(_("Illegal folder name. Please select a different name.").
31 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
33 sqimap_logout($imapConnection);
39 * Called from folders.php to create a new folder.
41 function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs)
43 folders_checkname($imapConnection, $folder_name, $delimiter);
45 global $folder_prefix;
47 $folder_name = imap_utf7_encode_local($folder_name);
49 if ( ! empty($contain_subs) ) {
50 $folder_name = $folder_name . $delimiter;
53 if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) {
54 $folder_prefix = $folder_prefix . $delimiter;
56 if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) {
57 $subfolder_orig = $subfolder;
58 $subfolder = $folder_prefix . $subfolder;
60 $subfolder_orig = $subfolder;
63 if (trim($subfolder_orig) == '') {
64 sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, '');
66 sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, '');
73 * Called from folders.php, given a folder name, ask the user what this
74 * folder should be renamed to.
76 function folders_rename_getname ($imapConnection, $delimiter, $old) {
80 plain_error_message(_("You have not selected a folder to rename. Please do so.").
81 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
82 sqimap_logout($imapConnection);
86 if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
88 $old = substr($old, 0, strlen($old) - 1);
93 $old = imap_utf7_decode_local($old);
95 if (strpos($old, $delimiter)) {
96 $old_name = substr($old, strrpos($old, $delimiter)+
1, strlen($old));
97 $old_parent = substr($old, 0, strrpos($old, $delimiter));
104 html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
106 html_tag( 'td', '<b>' . _("Rename a folder") . '</b>', 'center', $color[0] )
109 html_tag( 'td', '', 'center', $color[4] ) .
110 addForm('folders.php').
111 addHidden('smaction', 'rename').
113 '<br /><b>' . htmlspecialchars($old_parent) . ' ' . htmlspecialchars($delimiter) . '</b>' .
114 addInput('new_name', $old_name, 25) . '<br /><br />' . "\n";
116 echo addHidden('isfolder', 'true');
118 echo addHidden('orig', $old).
119 addHidden('old_name', $old_name).
120 '<input type="submit" value="'._("Rename")."\" />\n".
121 '<input type="submit" name="cancelbutton" value="'._("Cancel")."\" />\n".
122 '</form><br /></td></tr></table>';
123 echo "\n\n</body></html>";
125 sqimap_logout($imapConnection);
130 * Given an old and new folder name, renames the folder.
132 function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
134 folders_checkname($imapConnection, $new_name, $delimiter);
136 $orig = imap_utf7_encode_local($orig);
137 $old_name = imap_utf7_encode_local($old_name);
138 $new_name = imap_utf7_encode_local($new_name);
140 if ($old_name != $new_name) {
142 if (strpos($orig, $delimiter)) {
143 $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
148 if ($old_dir != '') {
149 $newone = $old_dir . $delimiter . $new_name;
154 // Renaming a folder doesn't rename the folder but leaves you unsubscribed
155 // at least on Cyrus IMAP servers.
156 if (isset($isfolder)) {
157 $newone = $newone.$delimiter;
158 $orig = $orig.$delimiter;
160 sqimap_mailbox_rename( $imapConnection, $orig, $newone );
168 * Presents a confirmation dialog to the user asking whether they're
169 * sure they want to delete this folder.
171 function folders_delete_ask ($imapConnection, $folder_name)
175 if ($folder_name == '') {
176 plain_error_message(_("You have not selected a folder to delete. Please do so.").
177 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
182 html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
184 html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[0] )
187 html_tag( 'td', '', 'center', $color[4] ) .
188 sprintf(_("Are you sure you want to delete %s?"),
189 str_replace(array(' ','<','>'),array(' ','<','>'),
190 imap_utf7_decode_local($folder_name))).
191 addForm('folders.php', 'post')."<p>\n".
192 addHidden('smaction', 'delete').
193 addHidden('folder_name', $folder_name).
194 addSubmit(_("Yes"), 'confirmed').
195 addSubmit(_("No"), 'cancelbutton').
196 '</p></form><br /></td></tr></table>';
198 echo "\n\n</body></html>";
200 sqimap_logout($imapConnection);
205 * Given a folder, moves it to trash (and all subfolders of it too).
207 function folders_delete_do ($imapConnection, $delimiter, $folder_name)
209 require_once(SM_PATH
. 'functions/tree.php');
211 $boxes = sqimap_mailbox_list ($imapConnection);
213 global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
215 if (substr($folder_name, -1) == $delimiter) {
216 $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
218 $folder_name_no_dm = $folder_name;
221 /** lets see if we CAN move folders to the trash.. otherwise,
222 ** just delete them **/
224 /* Courier IMAP doesn't like subfolders of Trash
225 * If global options say we can't move it into Trash
226 * If it's already a subfolder of trash, we'll have to delete it */
227 if (strtolower($imap_server_type) == 'courier' ||
228 (isset($delete_folder) && $delete_folder) ||
229 eregi('^'.$trash_folder.'.+', $folder_name) )
231 $can_move_to_trash = FALSE;
233 /* Otherwise, check if trash folder exits and support sub-folders */
235 foreach($boxes as $box) {
236 if ($box['unformatted'] == $trash_folder) {
237 $can_move_to_trash = !in_array('noinferiors', $box['flags']);
242 /** First create the top node in the tree **/
243 foreach($boxes as $box) {
244 if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
245 $foldersTree[0]['value'] = $folder_name;
246 $foldersTree[0]['doIHaveChildren'] = false;
251 /* Now create the nodes for subfolders of the parent folder
252 You can tell that it is a subfolder by tacking the mailbox delimiter
253 on the end of the $folder_name string, and compare to that. */
254 foreach($boxes as $box) {
255 if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
256 addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
260 /** Lets start removing the folders and messages **/
261 if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
262 walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
263 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
264 } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
265 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
272 * Given an array of folder_names, subscribes to each of them.
274 function folders_subscribe($imapConnection, $folder_names)
278 if (count($folder_names) == 0 ||
$folder_names[0] == '') {
279 plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
280 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
281 sqimap_logout($imapConnection);
285 global $no_list_for_subscribe, $imap_server_type;
287 if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
288 /* Cyrus, atleast, does not typically allow subscription to
289 * nonexistent folders (this is an optional part of IMAP),
290 * lets catch it here and report back cleanly. */
291 if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
292 plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
293 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
294 sqimap_logout($imapConnection);
299 foreach ( $folder_names as $folder_name ) {
300 sqimap_subscribe ($imapConnection, $folder_name);
307 * Given a list of folder names, unsubscribes from each of them.
309 function folders_unsubscribe($imapConnection, $folder_names)
313 if (count($folder_names) == 0 ||
$folder_names[0] == '') {
314 plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
315 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
316 sqimap_logout($imapConnection);
320 foreach ( $folder_names as $folder_name ) {
321 sqimap_unsubscribe ($imapConnection, $folder_name);