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