59177427 |
1 | <?php |
245a6892 |
2 | |
35586184 |
3 | /** |
4 | * tree.php |
5 | * |
1519cc53 |
6 | * This file provides functions to walk trees of folders, for |
7 | * instance to delete a whole tree. |
35586184 |
8 | * |
4b4abf93 |
9 | * @copyright © 1999-2005 The SquirrelMail Project Team |
10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
11 | * @version $Id$ |
d6c32258 |
12 | * @package squirrelmail |
35586184 |
13 | */ |
a4351446 |
14 | |
d6c32258 |
15 | /** Clearly, this needs the IMAP functions.. */ |
b68edc75 |
16 | require_once(SM_PATH . 'functions/imap.php'); |
35586184 |
17 | |
18 | /** |
8b096f0a |
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 |
35586184 |
25 | */ |
65c3ec94 |
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']) { |
41fd4ed4 |
31 | // loop through each subNode checking to see if we are a subNode of one of them |
8a549df2 |
32 | for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) { |
65c3ec94 |
33 | $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree); |
34 | if ($result > -1) |
35 | return $result; |
a4351446 |
36 | } |
41fd4ed4 |
37 | // if we aren't a child of one of the subNodes, must be a child of current node |
a4351446 |
38 | return $treeIndexToStart; |
65c3ec94 |
39 | } else |
a4351446 |
40 | return $treeIndexToStart; |
65c3ec94 |
41 | } else { |
42 | // we aren't a child of this node at all |
43 | return -1; |
44 | } |
45 | } |
46 | |
8b096f0a |
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 | */ |
65c3ec94 |
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 | |
8b096f0a |
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 | */ |
65c3ec94 |
81 | function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) { |
82 | global $trash_folder; |
1519cc53 |
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) { |
65c3ec94 |
96 | if ($tree[$index]['doIHaveChildren']) { |
97 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
8a549df2 |
98 | walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
65c3ec94 |
99 | } |
1519cc53 |
100 | if ($tree[$index]['value'] != $mailbox) { |
8a549df2 |
101 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
65c3ec94 |
102 | } else { |
1519cc53 |
103 | $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox); |
eaf1a312 |
104 | if ($mbx_response['EXISTS'] > 0) { |
a51932f2 |
105 | sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true); |
eaf1a312 |
106 | // CLOSE === EXPUNGE and UNSELECT |
107 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
294bf31a |
108 | } |
65c3ec94 |
109 | } |
110 | } else { |
1519cc53 |
111 | if ($tree[$index]['value'] != $mailbox) { |
8a549df2 |
112 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
65c3ec94 |
113 | } else { |
1519cc53 |
114 | $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox); |
eaf1a312 |
115 | if ($mbx_response['EXISTS'] > 0) { |
a51932f2 |
116 | sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true); |
eaf1a312 |
117 | // CLOSE === EXPUNGE and UNSELECT |
118 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
294bf31a |
119 | } |
65c3ec94 |
120 | } |
121 | } |
122 | } |
123 | |
b44ee3f0 |
124 | |
8b096f0a |
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 | */ |
65c3ec94 |
133 | function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) { |
134 | if ($tree[$index]['doIHaveChildren']) { |
135 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
8a549df2 |
136 | walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
65c3ec94 |
137 | } |
138 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
139 | } else { |
140 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
141 | } |
142 | } |
143 | |
8b096f0a |
144 | /** |
145 | * Recursively walk a tree of folders to create them under the trash folder. |
146 | */ |
65c3ec94 |
147 | function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) { |
148 | global $trash_folder, $delimiter; |
149 | |
4bce71a5 |
150 | $position = strrpos($topFolderName, $delimiter); |
e521a833 |
151 | if ($position !== FALSE) { |
4bce71a5 |
152 | $position++; |
e521a833 |
153 | } |
65c3ec94 |
154 | $subFolderName = substr($tree[$index]['value'], $position); |
155 | |
156 | if ($tree[$index]['doIHaveChildren']) { |
157 | sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ""); |
62f7daa5 |
158 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
eaf1a312 |
159 | $messageCount = $mbx_response['EXISTS']; |
160 | if ($messageCount > 0) { |
79577722 |
161 | // FIXME: broken call |
b44ee3f0 |
162 | sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName); |
eaf1a312 |
163 | } |
164 | // after copy close the mailbox to get in unselected state |
165 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
65c3ec94 |
166 | for ($j = 0;$j < count($tree[$index]['subNodes']); $j++) |
525b7ae6 |
167 | walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName); |
65c3ec94 |
168 | } else { |
169 | sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ''); |
b44ee3f0 |
170 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
171 | $messageCount = $mbx_response['EXISTS']; |
eaf1a312 |
172 | if ($messageCount > 0) { |
79577722 |
173 | // FIXME: broken call |
b44ee3f0 |
174 | sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName); |
eaf1a312 |
175 | } |
176 | // after copy close the mailbox to get in unselected state |
177 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
65c3ec94 |
178 | } |
179 | } |
abddc974 |
180 | |
8b096f0a |
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 | */ |
65c3ec94 |
187 | function simpleWalkTreePre($index, $tree) { |
188 | if ($tree[$index]['doIHaveChildren']) { |
189 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
8a549df2 |
190 | simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree); |
65c3ec94 |
191 | } |
6fd95361 |
192 | echo $tree[$index]['value'] . '<br />'; |
65c3ec94 |
193 | } else { |
6fd95361 |
194 | echo $tree[$index]['value'] . '<br />'; |
65c3ec94 |
195 | } |
196 | } |
a51932f2 |
197 | |
198 | ?> |