cda8c8e361d6b5f570ed362f697d797265b3d3e7
[squirrelmail.git] / functions / tree.php
1 <?php
2
3 /* $Id$ */
4
5 if (defined('tree_php'))
6 return;
7 define('tree_php', true);
8
9 include('../functions/imap.php');
10
11 // Recursive function to find the correct parent for a new node
12 function findParentForChild($value, $treeIndexToStart, $tree) {
13 // is $value in $tree[$treeIndexToStart]['value']
14 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
15 // do I have children, if not then must be a childnode of the current node
16 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
17 // loop through each subNode checking to see if we are a subNode of one of them
18 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
19 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
20 if ($result > -1)
21 return $result;
22 }
23 // if we aren't a child of one of the subNodes, must be a child of current node
24 return $treeIndexToStart;
25 } else
26 return $treeIndexToStart;
27 } else {
28 // we aren't a child of this node at all
29 return -1;
30 }
31 }
32
33 function addChildNodeToTree($comparisonValue, $value, &$tree) {
34 $parentNode = findParentForChild($comparisonValue, 0, $tree);
35
36 // create a new subNode
37 $newNodeIndex = count($tree);
38 $tree[$newNodeIndex]['value'] = $value;
39 $tree[$newNodeIndex]['doIHaveChildren'] = false;
40
41 if ($tree[$parentNode]['doIHaveChildren'] == false) {
42 // make sure the parent knows it has children
43 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
44 $tree[$parentNode]['doIHaveChildren'] = true;
45 } else {
46 $nextSubNode = count($tree[$parentNode]['subNodes']);
47 // make sure the parent knows it has children
48 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
49 }
50 }
51
52 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
53 global $trash_folder;
54 if ($tree[$index]['doIHaveChildren']) {
55 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
56 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
57 }
58 if ($tree[$index]['value'] != $trash_folder) {
59 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
60 } else {
61 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
62 if ($numMessages > 0) {
63 sqimap_mailbox_select($imap_stream, $trash_folder);
64 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
65 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
66 }
67 }
68 } else {
69 if ($tree[$index]['value'] != $trash_folder) {
70 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
71 } else {
72 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
73 if ($numMessages > 0) {
74 sqimap_mailbox_select($imap_stream, $trash_folder);
75 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
76 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
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 } else {
110 sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, '');
111 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
112
113 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
114 if ($messageCount > 0)
115 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $dm . $subFolderName);
116 }
117 }
118
119 function simpleWalkTreePre($index, $tree) {
120 if ($tree[$index]['doIHaveChildren']) {
121 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
122 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
123 }
124 echo $tree[$index]['value'] . '<br>';
125 } else {
126 echo $tree[$index]['value'] . '<br>';
127 }
128 }
129 ?>