6 * This file provides functions to walk trees of folders, for
7 * instance to delete a whole tree.
9 * @copyright © 1999-2005 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
15 /** Clearly, this needs the IMAP functions.. */
16 require_once(SM_PATH
. 'functions/imap.php');
19 * Recursive function to find the correct parent for a new node.
21 * @param mixed value the value to find a parent for
22 * @param int treeIndexToStart where to start the search, usually the root node (0)
23 * @param array tree the tree to search
24 * @return int the index of the parent
26 function findParentForChild($value, $treeIndexToStart, $tree) {
27 // is $value in $tree[$treeIndexToStart]['value']
28 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
29 // do I have children, if not then must be a childnode of the current node
30 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
31 // loop through each subNode checking to see if we are a subNode of one of them
32 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++
) {
33 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
37 // if we aren't a child of one of the subNodes, must be a child of current node
38 return $treeIndexToStart;
40 return $treeIndexToStart;
42 // we aren't a child of this node at all
48 * Will insert a new value into the tree, based on a given comparison value.
50 * @param mixed comparisonValue the value to determine where the new element should be placed.
51 * @param mixed value the new node to insert
52 * @param array tree the tree to insert the node in, by ref
54 function addChildNodeToTree($comparisonValue, $value, &$tree) {
55 $parentNode = findParentForChild($comparisonValue, 0, $tree);
57 // create a new subNode
58 $newNodeIndex = count($tree);
59 $tree[$newNodeIndex]['value'] = $value;
60 $tree[$newNodeIndex]['doIHaveChildren'] = false;
62 if ($tree[$parentNode]['doIHaveChildren'] == false) {
63 // make sure the parent knows it has children
64 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
65 $tree[$parentNode]['doIHaveChildren'] = true;
67 $nextSubNode = count($tree[$parentNode]['subNodes']);
68 // make sure the parent knows it has children
69 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
74 * Recursively walk the tree of trash mailboxes and delete all folders and messages
76 * @param int index the place in the tree to start, usually 0
77 * @param stream imap_stream the IMAP connection to send commands to
78 * @param array tree the tree to walk
81 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
83 walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
87 * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
89 * @param int index the place in the tree to start, usually 0
90 * @param stream imap_stream the IMAP connection to send commands to
91 * @param array tree the tree to walk
92 * @param mailbox the name of the root folder to empty
95 function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) {
96 if ($tree[$index]['doIHaveChildren']) {
97 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
98 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
100 if ($tree[$index]['value'] != $mailbox) {
101 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
103 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
104 if ($mbx_response['EXISTS'] > 0) {
105 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
106 // CLOSE === EXPUNGE and UNSELECT
107 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
111 if ($tree[$index]['value'] != $mailbox) {
112 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
114 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
115 if ($mbx_response['EXISTS'] > 0) {
116 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
117 // CLOSE === EXPUNGE and UNSELECT
118 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
126 * Recursively delete a tree of mail folders.
128 * @param int index the place in the tree to start, usually 0
129 * @param stream imap_stream the IMAP connection to send commands to
130 * @param array tree the tree to walk
133 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
134 if ($tree[$index]['doIHaveChildren']) {
135 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
136 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
138 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
140 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
145 * Recursively walk a tree of folders to create them under the trash folder.
147 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
148 global $trash_folder, $delimiter;
150 $position = strrpos($topFolderName, $delimiter);
151 if ($position !== FALSE) {
154 $subFolderName = substr($tree[$index]['value'], $position);
156 if ($tree[$index]['doIHaveChildren']) {
157 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
158 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
159 $messageCount = $mbx_response['EXISTS'];
160 if ($messageCount > 0) {
161 // FIXME: broken call
162 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
164 // after copy close the mailbox to get in unselected state
165 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
166 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++
)
167 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
169 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
170 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
171 $messageCount = $mbx_response['EXISTS'];
172 if ($messageCount > 0) {
173 // FIXME: broken call
174 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
176 // after copy close the mailbox to get in unselected state
177 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
182 * Recursive function that outputs a tree In-Pre-Order.
183 * @param int index the node to start (usually 0)
184 * @param array tree the tree to walk
187 function simpleWalkTreePre($index, $tree) {
188 if ($tree[$index]['doIHaveChildren']) {
189 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
190 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
192 echo $tree[$index]['value'] . '<br />';
194 echo $tree[$index]['value'] . '<br />';