5 if (defined('tree_php'))
7 define('tree_php', true);
9 require_once('../functions/imap.php');
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);
23 // if we aren't a child of one of the subNodes, must be a child of current node
24 return $treeIndexToStart;
26 return $treeIndexToStart;
28 // we aren't a child of this node at all
33 function addChildNodeToTree($comparisonValue, $value, &$tree) {
34 $parentNode = findParentForChild($comparisonValue, 0, $tree);
36 // create a new subNode
37 $newNodeIndex = count($tree);
38 $tree[$newNodeIndex]['value'] = $value;
39 $tree[$newNodeIndex]['doIHaveChildren'] = false;
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;
46 $nextSubNode = count($tree[$parentNode]['subNodes']);
47 // make sure the parent knows it has children
48 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
52 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
54 if ($tree[$index]['doIHaveChildren']) {
55 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
56 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
58 if ($tree[$index]['value'] != $trash_folder) {
59 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
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);
69 if ($tree[$index]['value'] != $trash_folder) {
70 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
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);
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);
87 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
89 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
93 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $dm, $topFolderName) {
96 $position = strrpos($topFolderName, $dm) +
1;
97 $subFolderName = substr($tree[$index]['value'], $position);
99 if ($tree[$index]['doIHaveChildren']) {
100 sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, "");
101 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
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);
107 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++
)
108 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $dm, $topFolderName);
110 sqimap_mailbox_create($imap_stream, $trash_folder . $dm . $subFolderName, '');
111 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
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);
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);
124 echo $tree[$index]['value'] . '<br>';
126 echo $tree[$index]['value'] . '<br>';