6 * This file provides functions to walk trees of folders, for
7 * instance to delete a whole tree.
9 * @copyright © 1999-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
17 * Recursive function to find the correct parent for a new node.
19 * @param mixed value the value to find a parent for
20 * @param int treeIndexToStart where to start the search, usually the root node (0)
21 * @param array tree the tree to search
22 * @return int the index of the parent
24 function findParentForChild($value, $treeIndexToStart, $tree) {
25 // is $value in $tree[$treeIndexToStart]['value']
26 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
27 // do I have children, if not then must be a childnode of the current node
28 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
29 // loop through each subNode checking to see if we are a subNode of one of them
30 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++
) {
31 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
35 // if we aren't a child of one of the subNodes, must be a child of current node
36 return $treeIndexToStart;
38 return $treeIndexToStart;
40 // we aren't a child of this node at all
46 * Will insert a new value into the tree, based on a given comparison value.
48 * @param mixed comparisonValue the value to determine where the new element should be placed.
49 * @param mixed value the new node to insert
50 * @param array tree the tree to insert the node in, by ref
52 function addChildNodeToTree($comparisonValue, $value, &$tree) {
53 $parentNode = findParentForChild($comparisonValue, 0, $tree);
55 // create a new subNode
56 $newNodeIndex = count($tree);
57 $tree[$newNodeIndex]['value'] = $value;
58 $tree[$newNodeIndex]['doIHaveChildren'] = false;
60 if ($tree[$parentNode]['doIHaveChildren'] == false) {
61 // make sure the parent knows it has children
62 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
63 $tree[$parentNode]['doIHaveChildren'] = true;
65 $nextSubNode = count($tree[$parentNode]['subNodes']);
66 // make sure the parent knows it has children
67 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
72 * Recursively walk the tree of trash mailboxes and delete all folders and messages
74 * @param int index the place in the tree to start, usually 0
75 * @param stream imap_stream the IMAP connection to send commands to
76 * @param array tree the tree to walk
79 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
81 walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
85 * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
87 * @param int index the place in the tree to start, usually 0
88 * @param stream imap_stream the IMAP connection to send commands to
89 * @param array tree the tree to walk
90 * @param mailbox the name of the root folder to empty
93 function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) {
94 if ($tree[$index]['doIHaveChildren']) {
95 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
96 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
98 if ($tree[$index]['value'] != $mailbox) {
99 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
101 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
102 if ($mbx_response['EXISTS'] > 0) {
103 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
104 // CLOSE === EXPUNGE and UNSELECT
105 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
109 if ($tree[$index]['value'] != $mailbox) {
110 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
112 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
113 if ($mbx_response['EXISTS'] > 0) {
114 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
115 // CLOSE === EXPUNGE and UNSELECT
116 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
124 * Recursively delete a tree of mail folders.
126 * @param int index the place in the tree to start, usually 0
127 * @param stream imap_stream the IMAP connection to send commands to
128 * @param array tree the tree to walk
131 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
132 if ($tree[$index]['doIHaveChildren']) {
133 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
134 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
136 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
138 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
143 * Recursively walk a tree of folders to create them under the trash folder.
145 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
146 global $trash_folder, $delimiter;
148 $position = strrpos($topFolderName, $delimiter);
149 if ($position !== FALSE) {
152 $subFolderName = substr($tree[$index]['value'], $position);
154 if ($tree[$index]['doIHaveChildren']) {
155 // create new trash subfolder only if it does not exist.
156 if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
157 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
159 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
160 $messageCount = $mbx_response['EXISTS'];
161 if ($messageCount > 0) {
162 sqimap_msgs_list_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 if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
170 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
171 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
172 $messageCount = $mbx_response['EXISTS'];
173 if ($messageCount > 0) {
174 sqimap_msgs_list_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 />';