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 | * |
47ccfad4 |
9 | * @copyright © 1999-2006 The SquirrelMail Project Team |
4b4abf93 |
10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
11 | * @version $Id$ |
d6c32258 |
12 | * @package squirrelmail |
35586184 |
13 | */ |
a4351446 |
14 | |
0d18f614 |
15 | /** @ignore */ |
16 | if (! defined('SM_PATH')) define('SM_PATH','../'); |
17 | |
d6c32258 |
18 | /** Clearly, this needs the IMAP functions.. */ |
b68edc75 |
19 | require_once(SM_PATH . 'functions/imap.php'); |
35586184 |
20 | |
21 | /** |
8b096f0a |
22 | * Recursive function to find the correct parent for a new node. |
23 | * |
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 |
35586184 |
28 | */ |
65c3ec94 |
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']) { |
41fd4ed4 |
34 | // loop through each subNode checking to see if we are a subNode of one of them |
8a549df2 |
35 | for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) { |
65c3ec94 |
36 | $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree); |
37 | if ($result > -1) |
38 | return $result; |
a4351446 |
39 | } |
41fd4ed4 |
40 | // if we aren't a child of one of the subNodes, must be a child of current node |
a4351446 |
41 | return $treeIndexToStart; |
65c3ec94 |
42 | } else |
a4351446 |
43 | return $treeIndexToStart; |
65c3ec94 |
44 | } else { |
45 | // we aren't a child of this node at all |
46 | return -1; |
47 | } |
48 | } |
49 | |
8b096f0a |
50 | /** |
51 | * Will insert a new value into the tree, based on a given comparison value. |
52 | * |
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 |
56 | */ |
65c3ec94 |
57 | function addChildNodeToTree($comparisonValue, $value, &$tree) { |
58 | $parentNode = findParentForChild($comparisonValue, 0, $tree); |
59 | |
60 | // create a new subNode |
61 | $newNodeIndex = count($tree); |
62 | $tree[$newNodeIndex]['value'] = $value; |
63 | $tree[$newNodeIndex]['doIHaveChildren'] = false; |
64 | |
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; |
69 | } else { |
70 | $nextSubNode = count($tree[$parentNode]['subNodes']); |
71 | // make sure the parent knows it has children |
72 | $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex; |
73 | } |
74 | } |
75 | |
8b096f0a |
76 | /** |
77 | * Recursively walk the tree of trash mailboxes and delete all folders and messages |
78 | * |
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 |
82 | * @return void |
83 | */ |
65c3ec94 |
84 | function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) { |
85 | global $trash_folder; |
1519cc53 |
86 | walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder); |
87 | } |
88 | |
89 | /** |
90 | * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages |
91 | * |
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 |
96 | * @return void |
97 | */ |
98 | function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) { |
65c3ec94 |
99 | if ($tree[$index]['doIHaveChildren']) { |
100 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
8a549df2 |
101 | walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
65c3ec94 |
102 | } |
1519cc53 |
103 | if ($tree[$index]['value'] != $mailbox) { |
8a549df2 |
104 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
65c3ec94 |
105 | } else { |
1519cc53 |
106 | $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox); |
eaf1a312 |
107 | if ($mbx_response['EXISTS'] > 0) { |
a51932f2 |
108 | sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true); |
eaf1a312 |
109 | // CLOSE === EXPUNGE and UNSELECT |
110 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
294bf31a |
111 | } |
65c3ec94 |
112 | } |
113 | } else { |
1519cc53 |
114 | if ($tree[$index]['value'] != $mailbox) { |
8a549df2 |
115 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
65c3ec94 |
116 | } else { |
1519cc53 |
117 | $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox); |
eaf1a312 |
118 | if ($mbx_response['EXISTS'] > 0) { |
a51932f2 |
119 | sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true); |
eaf1a312 |
120 | // CLOSE === EXPUNGE and UNSELECT |
121 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
294bf31a |
122 | } |
65c3ec94 |
123 | } |
124 | } |
125 | } |
126 | |
b44ee3f0 |
127 | |
8b096f0a |
128 | /** |
129 | * Recursively delete a tree of mail folders. |
130 | * |
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 |
134 | * @return void |
135 | */ |
65c3ec94 |
136 | function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) { |
137 | if ($tree[$index]['doIHaveChildren']) { |
138 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
8a549df2 |
139 | walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
65c3ec94 |
140 | } |
141 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
142 | } else { |
143 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
144 | } |
145 | } |
146 | |
8b096f0a |
147 | /** |
148 | * Recursively walk a tree of folders to create them under the trash folder. |
149 | */ |
65c3ec94 |
150 | function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) { |
151 | global $trash_folder, $delimiter; |
152 | |
4bce71a5 |
153 | $position = strrpos($topFolderName, $delimiter); |
e521a833 |
154 | if ($position !== FALSE) { |
4bce71a5 |
155 | $position++; |
e521a833 |
156 | } |
65c3ec94 |
157 | $subFolderName = substr($tree[$index]['value'], $position); |
158 | |
159 | if ($tree[$index]['doIHaveChildren']) { |
0d18f614 |
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, ""); |
163 | |
62f7daa5 |
164 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
eaf1a312 |
165 | $messageCount = $mbx_response['EXISTS']; |
166 | if ($messageCount > 0) { |
0d18f614 |
167 | sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName); |
eaf1a312 |
168 | } |
169 | // after copy close the mailbox to get in unselected state |
170 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
65c3ec94 |
171 | for ($j = 0;$j < count($tree[$index]['subNodes']); $j++) |
525b7ae6 |
172 | walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName); |
65c3ec94 |
173 | } else { |
0d18f614 |
174 | if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName)) |
175 | sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ''); |
b44ee3f0 |
176 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
177 | $messageCount = $mbx_response['EXISTS']; |
eaf1a312 |
178 | if ($messageCount > 0) { |
0d18f614 |
179 | sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName); |
eaf1a312 |
180 | } |
181 | // after copy close the mailbox to get in unselected state |
182 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
65c3ec94 |
183 | } |
184 | } |
abddc974 |
185 | |
8b096f0a |
186 | /** |
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 |
190 | * @return void |
191 | */ |
65c3ec94 |
192 | function simpleWalkTreePre($index, $tree) { |
193 | if ($tree[$index]['doIHaveChildren']) { |
194 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
8a549df2 |
195 | simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree); |
65c3ec94 |
196 | } |
6fd95361 |
197 | echo $tree[$index]['value'] . '<br />'; |
65c3ec94 |
198 | } else { |
6fd95361 |
199 | echo $tree[$index]['value'] . '<br />'; |
65c3ec94 |
200 | } |
201 | } |
a51932f2 |
202 | |
203 | ?> |