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
16 if (! defined('SM_PATH')) define('SM_PATH','../');
18 /** Clearly, this needs the IMAP functions.. */
19 require_once(SM_PATH
. 'functions/imap.php');
22 * Recursive function to find the correct parent for a new node.
24 * @param mixed value the value to find a parent for
25 * @param int treeIndexToStart where to start the search, usually the root node (0)
26 * @param array tree the tree to search
27 * @return int the index of the parent
29 function findParentForChild($value, $treeIndexToStart, $tree) {
30 // is $value in $tree[$treeIndexToStart]['value']
31 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
32 // do I have children, if not then must be a childnode of the current node
33 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
34 // loop through each subNode checking to see if we are a subNode of one of them
35 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++
) {
36 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
40 // if we aren't a child of one of the subNodes, must be a child of current node
41 return $treeIndexToStart;
43 return $treeIndexToStart;
45 // we aren't a child of this node at all
51 * Will insert a new value into the tree, based on a given comparison value.
53 * @param mixed comparisonValue the value to determine where the new element should be placed.
54 * @param mixed value the new node to insert
55 * @param array tree the tree to insert the node in, by ref
57 function addChildNodeToTree($comparisonValue, $value, &$tree) {
58 $parentNode = findParentForChild($comparisonValue, 0, $tree);
60 // create a new subNode
61 $newNodeIndex = count($tree);
62 $tree[$newNodeIndex]['value'] = $value;
63 $tree[$newNodeIndex]['doIHaveChildren'] = false;
65 if ($tree[$parentNode]['doIHaveChildren'] == false) {
66 // make sure the parent knows it has children
67 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
68 $tree[$parentNode]['doIHaveChildren'] = true;
70 $nextSubNode = count($tree[$parentNode]['subNodes']);
71 // make sure the parent knows it has children
72 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
77 * Recursively walk the tree of trash mailboxes and delete all folders and messages
79 * @param int index the place in the tree to start, usually 0
80 * @param stream imap_stream the IMAP connection to send commands to
81 * @param array tree the tree to walk
84 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
86 walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
90 * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
92 * @param int index the place in the tree to start, usually 0
93 * @param stream imap_stream the IMAP connection to send commands to
94 * @param array tree the tree to walk
95 * @param mailbox the name of the root folder to empty
98 function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) {
99 if ($tree[$index]['doIHaveChildren']) {
100 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
101 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
103 if ($tree[$index]['value'] != $mailbox) {
104 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
106 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
107 if ($mbx_response['EXISTS'] > 0) {
108 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
109 // CLOSE === EXPUNGE and UNSELECT
110 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
114 if ($tree[$index]['value'] != $mailbox) {
115 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
117 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
118 if ($mbx_response['EXISTS'] > 0) {
119 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
120 // CLOSE === EXPUNGE and UNSELECT
121 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
129 * Recursively delete a tree of mail folders.
131 * @param int index the place in the tree to start, usually 0
132 * @param stream imap_stream the IMAP connection to send commands to
133 * @param array tree the tree to walk
136 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
137 if ($tree[$index]['doIHaveChildren']) {
138 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
139 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
141 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
143 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
148 * Recursively walk a tree of folders to create them under the trash folder.
150 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
151 global $trash_folder, $delimiter;
153 $position = strrpos($topFolderName, $delimiter);
154 if ($position !== FALSE) {
157 $subFolderName = substr($tree[$index]['value'], $position);
159 if ($tree[$index]['doIHaveChildren']) {
160 // create new trash subfolder only if it does not exist.
161 if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
162 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
164 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
165 $messageCount = $mbx_response['EXISTS'];
166 if ($messageCount > 0) {
167 sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
169 // after copy close the mailbox to get in unselected state
170 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
171 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++
)
172 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
174 if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
175 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
176 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
177 $messageCount = $mbx_response['EXISTS'];
178 if ($messageCount > 0) {
179 sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
181 // after copy close the mailbox to get in unselected state
182 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
187 * Recursive function that outputs a tree In-Pre-Order.
188 * @param int index the node to start (usually 0)
189 * @param array tree the tree to walk
192 function simpleWalkTreePre($index, $tree) {
193 if ($tree[$index]['doIHaveChildren']) {
194 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
195 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
197 echo $tree[$index]['value'] . '<br />';
199 echo $tree[$index]['value'] . '<br />';