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