phpDocumentor updates
[squirrelmail.git] / functions / tree.php
1 <?php
2
3 /**
4 * tree.php
5 *
6 * This file provides functions to walk trees of folders, for
7 * instance to delete a whole tree.
8 *
9 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /** Clearly, this needs the IMAP functions.. */
16 require_once(SM_PATH . 'functions/imap.php');
17
18 /**
19 * Recursive function to find the correct parent for a new node.
20 *
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
25 */
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);
34 if ($result > -1)
35 return $result;
36 }
37 // if we aren't a child of one of the subNodes, must be a child of current node
38 return $treeIndexToStart;
39 } else
40 return $treeIndexToStart;
41 } else {
42 // we aren't a child of this node at all
43 return -1;
44 }
45 }
46
47 /**
48 * Will insert a new value into the tree, based on a given comparison value.
49 *
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
53 */
54 function addChildNodeToTree($comparisonValue, $value, &$tree) {
55 $parentNode = findParentForChild($comparisonValue, 0, $tree);
56
57 // create a new subNode
58 $newNodeIndex = count($tree);
59 $tree[$newNodeIndex]['value'] = $value;
60 $tree[$newNodeIndex]['doIHaveChildren'] = false;
61
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;
66 } else {
67 $nextSubNode = count($tree[$parentNode]['subNodes']);
68 // make sure the parent knows it has children
69 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
70 }
71 }
72
73 /**
74 * Recursively walk the tree of trash mailboxes and delete all folders and messages
75 *
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
79 * @return void
80 */
81 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
82 global $trash_folder;
83 walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
84 }
85
86 /**
87 * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
88 *
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
93 * @return void
94 */
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);
99 }
100 if ($tree[$index]['value'] != $mailbox) {
101 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
102 } else {
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);
108 }
109 }
110 } else {
111 if ($tree[$index]['value'] != $mailbox) {
112 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
113 } else {
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);
119 }
120 }
121 }
122 }
123
124
125 /**
126 * Recursively delete a tree of mail folders.
127 *
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
131 * @return void
132 */
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);
137 }
138 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
139 } else {
140 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
141 }
142 }
143
144 /**
145 * Recursively walk a tree of folders to create them under the trash folder.
146 */
147 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
148 global $trash_folder, $delimiter;
149
150 $position = strrpos($topFolderName, $delimiter);
151 if ($position !== FALSE) {
152 $position++;
153 }
154 $subFolderName = substr($tree[$index]['value'], $position);
155
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);
163 }
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);
168 } else {
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);
175 }
176 // after copy close the mailbox to get in unselected state
177 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
178 }
179 }
180
181 /**
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
185 * @return void
186 */
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);
191 }
192 echo $tree[$index]['value'] . '<br />';
193 } else {
194 echo $tree[$index]['value'] . '<br />';
195 }
196 }
197
198 ?>