6 * This file provides functions to walk trees of folders, for
7 * instance to delete a whole tree.
9 * @copyright 1999-2011 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
15 require_once(SM_PATH
. 'functions/imap.php');
18 * Recursive function to find the correct parent for a new node.
20 * @param mixed value the value to find a parent for
21 * @param int treeIndexToStart where to start the search, usually the root node (0)
22 * @param array tree the tree to search
23 * @return int the index of the parent
25 function findParentForChild($value, $treeIndexToStart, $tree) {
26 // is $value in $tree[$treeIndexToStart]['value']
27 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
28 // do I have children, if not then must be a childnode of the current node
29 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
30 // loop through each subNode checking to see if we are a subNode of one of them
31 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++
) {
32 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
36 // if we aren't a child of one of the subNodes, must be a child of current node
37 return $treeIndexToStart;
39 return $treeIndexToStart;
41 // we aren't a child of this node at all
47 * Will insert a new value into the tree, based on a given comparison value.
49 * @param mixed comparisonValue the value to determine where the new element should be placed.
50 * @param mixed value the new node to insert
51 * @param array tree the tree to insert the node in, by ref
53 function addChildNodeToTree($comparisonValue, $value, &$tree) {
54 $parentNode = findParentForChild($comparisonValue, 0, $tree);
56 // create a new subNode
57 $newNodeIndex = count($tree);
58 $tree[$newNodeIndex]['value'] = $value;
59 $tree[$newNodeIndex]['doIHaveChildren'] = false;
61 if ($tree[$parentNode]['doIHaveChildren'] == false) {
62 // make sure the parent knows it has children
63 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
64 $tree[$parentNode]['doIHaveChildren'] = true;
66 $nextSubNode = count($tree[$parentNode]['subNodes']);
67 // make sure the parent knows it has children
68 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
73 * Recursively walk the tree of trash mailboxes and delete all folders and messages
75 * @param int index the place in the tree to start, usually 0
76 * @param stream imap_stream the IMAP connection to send commands to
77 * @param array tree the tree to walk
80 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
82 walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
86 * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
88 * @param int index the place in the tree to start, usually 0
89 * @param stream imap_stream the IMAP connection to send commands to
90 * @param array tree the tree to walk
91 * @param mailbox the name of the root folder to empty
94 function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) {
95 if ($tree[$index]['doIHaveChildren']) {
96 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
97 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
99 if ($tree[$index]['value'] != $mailbox) {
100 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
102 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
103 if ($mbx_response['EXISTS'] > 0) {
104 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
105 // CLOSE === EXPUNGE and UNSELECT
106 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
110 if ($tree[$index]['value'] != $mailbox) {
111 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
113 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
114 if ($mbx_response['EXISTS'] > 0) {
115 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
116 // CLOSE === EXPUNGE and UNSELECT
117 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
125 * Recursively delete a tree of mail folders.
127 * @param int index the place in the tree to start, usually 0
128 * @param stream imap_stream the IMAP connection to send commands to
129 * @param array tree the tree to walk
132 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
133 if ($tree[$index]['doIHaveChildren']) {
134 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
135 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
137 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
139 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
144 * Recursively walk a tree of folders to create them under the trash folder.
146 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
147 global $trash_folder, $delimiter;
149 $position = strrpos($topFolderName, $delimiter);
150 if ($position !== FALSE) {
153 $subFolderName = substr($tree[$index]['value'], $position);
155 if ($tree[$index]['doIHaveChildren']) {
156 // create new trash subfolder only if it does not exist.
157 if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
158 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
160 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
161 $messageCount = $mbx_response['EXISTS'];
162 if ($messageCount > 0) {
163 sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
165 // after copy close the mailbox to get in unselected state
166 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
167 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++
)
168 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
170 if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
171 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
172 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
173 $messageCount = $mbx_response['EXISTS'];
174 if ($messageCount > 0) {
175 sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
177 // after copy close the mailbox to get in unselected state
178 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
183 * Recursive function that outputs a tree In-Pre-Order.
184 * @param int index the node to start (usually 0)
185 * @param array tree the tree to walk
188 function simpleWalkTreePre($index, $tree) {
189 if ($tree[$index]['doIHaveChildren']) {
190 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
191 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
193 echo $tree[$index]['value'] . '<br />';
195 echo $tree[$index]['value'] . '<br />';