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