59177427 |
1 | <?php |
245a6892 |
2 | |
3 | /* $Id$ */ |
4 | |
f435778e |
5 | if (defined('tree_php')) |
6 | return; |
7 | define('tree_php', true); |
a4351446 |
8 | |
f435778e |
9 | include('../functions/imap.php'); |
a4351446 |
10 | |
41fd4ed4 |
11 | // Recursive function to find the correct parent for a new node |
a4351446 |
12 | function findParentForChild($value, $treeIndexToStart, $tree) { |
8a549df2 |
13 | // is $value in $tree[$treeIndexToStart]['value'] |
14 | if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) { |
41fd4ed4 |
15 | // do I have children, if not then must be a childnode of the current node |
8a549df2 |
16 | if ($tree[$treeIndexToStart]['doIHaveChildren']) { |
41fd4ed4 |
17 | // loop through each subNode checking to see if we are a subNode of one of them |
8a549df2 |
18 | for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) { |
19 | $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree); |
8703e429 |
20 | if ($result > -1) |
a4351446 |
21 | return $result; |
22 | } |
41fd4ed4 |
23 | // if we aren't a child of one of the subNodes, must be a child of current node |
a4351446 |
24 | return $treeIndexToStart; |
25 | } else |
26 | return $treeIndexToStart; |
27 | } else { |
41fd4ed4 |
28 | // we aren't a child of this node at all |
a4351446 |
29 | return -1; |
30 | } |
31 | } |
32 | |
abddc974 |
33 | function addChildNodeToTree($comparisonValue, $value, &$tree) { |
34 | $parentNode = findParentForChild($comparisonValue, 0, $tree); |
a4351446 |
35 | |
36 | // create a new subNode |
abddc974 |
37 | $newNodeIndex = count($tree); |
8a549df2 |
38 | $tree[$newNodeIndex]['value'] = $value; |
39 | $tree[$newNodeIndex]['doIHaveChildren'] = false; |
a4351446 |
40 | |
8a549df2 |
41 | if ($tree[$parentNode]['doIHaveChildren'] == false) { |
a4351446 |
42 | // make sure the parent knows it has children |
8a549df2 |
43 | $tree[$parentNode]['subNodes'][0] = $newNodeIndex; |
44 | $tree[$parentNode]['doIHaveChildren'] = true; |
a4351446 |
45 | } else { |
8a549df2 |
46 | $nextSubNode = count($tree[$parentNode]['subNodes']); |
a4351446 |
47 | // make sure the parent knows it has children |
8a549df2 |
48 | $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex; |
a4351446 |
49 | } |
50 | } |
51 | |
294bf31a |
52 | function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) { |
53 | global $trash_folder; |
8a549df2 |
54 | if ($tree[$index]['doIHaveChildren']) { |
55 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
56 | walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
294bf31a |
57 | } |
8a549df2 |
58 | if ($tree[$index]['value'] != $trash_folder) { |
59 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
294bf31a |
60 | } else { |
61 | $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder); |
62 | if ($numMessages > 0) { |
63 | sqimap_mailbox_select($imap_stream, $trash_folder); |
8a549df2 |
64 | sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted'); |
8cf653ad |
65 | sqimap_mailbox_expunge($imap_stream, $trash_folder, true); |
294bf31a |
66 | } |
67 | } |
68 | } else { |
8a549df2 |
69 | if ($tree[$index]['value'] != $trash_folder) { |
70 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
294bf31a |
71 | } else { |
72 | $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder); |
73 | if ($numMessages > 0) { |
74 | sqimap_mailbox_select($imap_stream, $trash_folder); |
8a549df2 |
75 | sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted'); |
8cf653ad |
76 | sqimap_mailbox_expunge($imap_stream, $trash_folder, true); |
294bf31a |
77 | } |
78 | } |
79 | } |
80 | } |
81 | |
a4351446 |
82 | function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) { |
8a549df2 |
83 | if ($tree[$index]['doIHaveChildren']) { |
84 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
85 | walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
a4351446 |
86 | } |
8a549df2 |
87 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
a4351446 |
88 | } else { |
8a549df2 |
89 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
a4351446 |
90 | } |
91 | } |
92 | |
93 | function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $dm, $topFolderName) { |
94 | global $trash_folder; |
95 | |
96 | $position = strrpos($topFolderName, $dm) + 1; |
8a549df2 |
97 | $subFolderName = substr($tree[$index]['value'], $position); |
a4351446 |
98 | |
8a549df2 |
99 | if ($tree[$index]['doIHaveChildren']) { |
a4351446 |
100 | sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, ""); |
8a549df2 |
101 | sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
a4351446 |
102 | |
8a549df2 |
103 | $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']); |
a4351446 |
104 | if ($messageCount > 0) |
105 | sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName); |
106 | |
8a549df2 |
107 | for ($j = 0;$j < count($tree[$index]['subNodes']); $j++) |
108 | walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $dm, $topFolderName); |
a4351446 |
109 | } else { |
8a549df2 |
110 | sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, ''); |
111 | sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
a4351446 |
112 | |
8a549df2 |
113 | $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']); |
a4351446 |
114 | if ($messageCount > 0) |
115 | sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName); |
116 | } |
117 | } |
abddc974 |
118 | |
119 | function simpleWalkTreePre($index, $tree) { |
8a549df2 |
120 | if ($tree[$index]['doIHaveChildren']) { |
121 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
122 | simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree); |
abddc974 |
123 | } |
8a549df2 |
124 | echo $tree[$index]['value'] . '<br>'; |
abddc974 |
125 | } else { |
8a549df2 |
126 | echo $tree[$index]['value'] . '<br>'; |
abddc974 |
127 | } |
128 | } |
f435778e |
129 | ?> |