} } /** First create the top node in the tree **/ for ($i = 0; $i < $numboxes; $i++) { if ($boxes[$i]['unformatted-dm'] == $mailbox && strlen($boxes[$i]['unformatted-dm']) == strlen($mailbox)) { $foldersTree[0]['value'] = $mailbox; $foldersTree[0]['doIHaveChildren'] = false; continue; } } /* Now create the nodes for subfolders of the parent folder You can tell that it is a subfolder by tacking the mailbox delimiter on the end of the $mailbox string, and compare to that. */ for ($i = 0; $i < $numboxes; $i++) { if (substr($boxes[$i]['unformatted'], 0, strlen($mailbox_no_dm . $delimiter)) == $mailbox_no_dm . $delimiter) { addChildNodeToTree($boxes[$i]["unformatted"], $boxes[$i]['unformatted-dm'], $foldersTree); } } /** Lets start removing the folders and messages **/ if ($move_to_trash == true && $can_move_to_trash == true) { /** if they wish to move messages to the trash **/ walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imap_stream, $foldersTree, $mailbox); walkTreeInPreOrderDeleteFolders(0, $imap_stream, $foldersTree); } else { /** if they do NOT wish to move messages to the trash (or cannot)**/ walkTreeInPreOrderDeleteFolders(0, $imap_stream, $foldersTree); } /** Log out this session **/ sqimap_logout($imap_stream); $location = get_location(); header("Location: {$location}/folders.php?success=delete");
/** * Given a folder, moves it to trash (and all subfolders of it too). */ function folders_delete_do($imapConnection, $delimiter, $folder_name) { include SM_PATH . 'functions/tree.php'; $boxes = sqimap_mailbox_list($imapConnection); global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash; if (substr($folder_name, -1) == $delimiter) { $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1); } else { $folder_name_no_dm = $folder_name; } /** lets see if we CAN move folders to the trash.. otherwise, ** just delete them **/ if ($delete_folder || preg_match('/^' . preg_quote($trash_folder, '/') . '.+/i', $folder_name)) { $can_move_to_trash = FALSE; } else { /* Otherwise, check if trash folder exits and support sub-folders */ foreach ($boxes as $box) { if ($box['unformatted'] == $trash_folder) { $can_move_to_trash = !in_array('noinferiors', $box['flags']); } } } /** First create the top node in the tree **/ foreach ($boxes as $box) { if ($box['unformatted-dm'] == $folder_name && strlen($box['unformatted-dm']) == strlen($folder_name)) { $foldersTree[0]['value'] = $folder_name; $foldersTree[0]['doIHaveChildren'] = false; continue; } } /* Now create the nodes for subfolders of the parent folder You can tell that it is a subfolder by tacking the mailbox delimiter on the end of the $folder_name string, and compare to that. */ foreach ($boxes as $box) { if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == $folder_name_no_dm . $delimiter) { addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree); } } /** Lets start removing the folders and messages **/ if ($move_to_trash == true && $can_move_to_trash == true) { /** if they wish to move messages to the trash **/ walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name); walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree); } else { /** if they do NOT wish to move messages to the trash (or cannot)**/ walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree); } return; }
/** * Recursively walk a tree of folders to create them under the trash folder. */ function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) { global $trash_folder, $delimiter; $position = strrpos($topFolderName, $delimiter); if ($position !== FALSE) { $position++; } $subFolderName = substr($tree[$index]['value'], $position); if ($tree[$index]['doIHaveChildren']) { sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ""); $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); $messageCount = $mbx_response['EXISTS']; if ($messageCount > 0) { sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName); } // after copy close the mailbox to get in unselected state sqimap_run_command($imap_stream, 'CLOSE', false, $response, $message); for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName); } } else { sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ''); $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); $messageCount = $mbx_response['EXISTS']; if ($messageCount > 0) { sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName); } // after copy close the mailbox to get in unselected state sqimap_run_command($imap_stream, 'CLOSE', false, $response, $message); } }
function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) { global $trash_folder, $delimiter; $position = strrpos($topFolderName, $delimiter) + 1; $subFolderName = substr($tree[$index]['value'], $position); if ($tree[$index]['doIHaveChildren']) { sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ""); $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); $messageCount = $mbx_response['EXISTS']; if ($messageCount > 0) { sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName); } for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName); } } else { sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ''); $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); $messageCount = $mbx_response['EXISTS']; if ($messageCount > 0) { sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName); } } }