5d9657943c8db0cc2e1955194da26d71f56ae00c
[squirrelmail.git] / functions / tree.php
1 <?php
2
3 /**
4 * tree.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
11 *
12 * $Id$
13 */
14
15 /*****************************************************************/
16 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
17 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
18 /*** + Base level indent should begin at left margin, as ***/
19 /*** the require_once below. ***/
20 /*** + All identation should consist of four space blocks ***/
21 /*** + Tab characters are evil. ***/
22 /*** + all comments should use "slash-star ... star-slash" ***/
23 /*** style -- no pound characters, no slash-slash style ***/
24 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
25 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
26 /*** + Please use ' instead of ", when possible. Note " ***/
27 /*** should always be used in _( ) function calls. ***/
28 /*** Thank you for your help making the SM code more readable. ***/
29 /*****************************************************************/
30
31 require_once('../functions/imap.php');
32
33 /**
34 * findParentForChild
35 *
36 * Recursive function to find the correct parent for a new node
37 */
38
39 function findParentForChild($value, $treeIndexToStart, $tree) {
40 // is $value in $tree[$treeIndexToStart]['value']
41 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
42 // do I have children, if not then must be a childnode of the current node
43 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
44 // loop through each subNode checking to see if we are a subNode of one of them
45 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
46 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
47 if ($result > -1)
48 return $result;
49 }
50 // if we aren't a child of one of the subNodes, must be a child of current node
51 return $treeIndexToStart;
52 } else
53 return $treeIndexToStart;
54 } else {
55 // we aren't a child of this node at all
56 return -1;
57 }
58 }
59
60 function addChildNodeToTree($comparisonValue, $value, &$tree) {
61 $parentNode = findParentForChild($comparisonValue, 0, $tree);
62
63 // create a new subNode
64 $newNodeIndex = count($tree);
65 $tree[$newNodeIndex]['value'] = $value;
66 $tree[$newNodeIndex]['doIHaveChildren'] = false;
67
68 if ($tree[$parentNode]['doIHaveChildren'] == false) {
69 // make sure the parent knows it has children
70 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
71 $tree[$parentNode]['doIHaveChildren'] = true;
72 } else {
73 $nextSubNode = count($tree[$parentNode]['subNodes']);
74 // make sure the parent knows it has children
75 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
76 }
77 }
78
79 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
80 global $trash_folder;
81 if ($tree[$index]['doIHaveChildren']) {
82 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
83 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
84 }
85 if ($tree[$index]['value'] != $trash_folder) {
86 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
87 } else {
88 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
89 if ($numMessages > 0) {
90 sqimap_mailbox_select($imap_stream, $trash_folder);
91 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
92 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
93 }
94 }
95 } else {
96 if ($tree[$index]['value'] != $trash_folder) {
97 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
98 } else {
99 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
100 if ($numMessages > 0) {
101 sqimap_mailbox_select($imap_stream, $trash_folder);
102 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
103 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
104 }
105 }
106 }
107 }
108
109 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
110 if ($tree[$index]['doIHaveChildren']) {
111 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
112 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
113 }
114 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
115 } else {
116 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
117 }
118 }
119
120 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
121 global $trash_folder, $delimiter;
122
123 $position = strrpos($topFolderName, $delimiter) + 1;
124 $subFolderName = substr($tree[$index]['value'], $position);
125
126 if ($tree[$index]['doIHaveChildren']) {
127 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
128 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
129
130 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
131 if ($messageCount > 0)
132 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
133
134 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
135 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
136 } else {
137 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
138 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
139
140 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
141 if ($messageCount > 0)
142 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
143 }
144 }
145
146 function simpleWalkTreePre($index, $tree) {
147 if ($tree[$index]['doIHaveChildren']) {
148 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
149 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
150 }
151 echo $tree[$index]['value'] . '<br>';
152 } else {
153 echo $tree[$index]['value'] . '<br>';
154 }
155 }
156 ?>