Fixing undefined variables
[squirrelmail.git] / functions / tree.php
1 <?php
2
3 /**
4 * tree.php
5 *
6 * Copyright (c) 1999-2004 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 * @version $Id$
13 * @package squirrelmail
14 */
15
16 /** Clearly, this needs the IMAP functions.. */
17 require_once(SM_PATH . 'functions/imap.php');
18
19 /**
20 * Recursive function to find the correct parent for a new node.
21 *
22 * @param mixed value the value to find a parent for
23 * @param int treeIndexToStart where to start the search, usually the root node (0)
24 * @param array tree the tree to search
25 * @return int the index of the parent
26 */
27 function findParentForChild($value, $treeIndexToStart, $tree) {
28 // is $value in $tree[$treeIndexToStart]['value']
29 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
30 // do I have children, if not then must be a childnode of the current node
31 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
32 // loop through each subNode checking to see if we are a subNode of one of them
33 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
34 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
35 if ($result > -1)
36 return $result;
37 }
38 // if we aren't a child of one of the subNodes, must be a child of current node
39 return $treeIndexToStart;
40 } else
41 return $treeIndexToStart;
42 } else {
43 // we aren't a child of this node at all
44 return -1;
45 }
46 }
47
48 /**
49 * Will insert a new value into the tree, based on a given comparison value.
50 *
51 * @param mixed comparisonValue the value to determine where the new element should be placed.
52 * @param mixed value the new node to insert
53 * @param array tree the tree to insert the node in, by ref
54 */
55 function addChildNodeToTree($comparisonValue, $value, &$tree) {
56 $parentNode = findParentForChild($comparisonValue, 0, $tree);
57
58 // create a new subNode
59 $newNodeIndex = count($tree);
60 $tree[$newNodeIndex]['value'] = $value;
61 $tree[$newNodeIndex]['doIHaveChildren'] = false;
62
63 if ($tree[$parentNode]['doIHaveChildren'] == false) {
64 // make sure the parent knows it has children
65 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
66 $tree[$parentNode]['doIHaveChildren'] = true;
67 } else {
68 $nextSubNode = count($tree[$parentNode]['subNodes']);
69 // make sure the parent knows it has children
70 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
71 }
72 }
73
74 /**
75 * Recursively walk the tree of trash mailboxes and delete all folders and messages
76 *
77 * @param int index the place in the tree to start, usually 0
78 * @param stream imap_stream the IMAP connection to send commands to
79 * @param array tree the tree to walk
80 * @return void
81 */
82 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
83 global $trash_folder;
84 if ($tree[$index]['doIHaveChildren']) {
85 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
86 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
87 }
88 if ($tree[$index]['value'] != $trash_folder) {
89 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
90 } else {
91 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
92 if ($mbx_response['EXISTS'] > 0) {
93 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
94 // CLOSE === EXPUNGE and UNSELECT
95 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
96 }
97 }
98 } else {
99 if ($tree[$index]['value'] != $trash_folder) {
100 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
101 } else {
102 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
103 if ($mbx_response['EXISTS'] > 0) {
104 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
105 // CLOSE === EXPUNGE and UNSELECT
106 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
107 }
108 }
109 }
110 }
111
112
113 /**
114 * Recursively delete a tree of mail folders.
115 *
116 * @param int index the place in the tree to start, usually 0
117 * @param stream imap_stream the IMAP connection to send commands to
118 * @param array tree the tree to walk
119 * @return void
120 */
121 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
122 if ($tree[$index]['doIHaveChildren']) {
123 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
124 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
125 }
126 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
127 } else {
128 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
129 }
130 }
131
132 /**
133 * Recursively walk a tree of folders to create them under the trash folder.
134 */
135 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
136 global $trash_folder, $delimiter;
137
138 $position = strrpos($topFolderName, $delimiter);
139 if ($position !== FALSE) {
140 $position++;
141 }
142 $subFolderName = substr($tree[$index]['value'], $position);
143
144 if ($tree[$index]['doIHaveChildren']) {
145 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
146 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
147 $messageCount = $mbx_response['EXISTS'];
148 if ($messageCount > 0) {
149 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
150 }
151 // after copy close the mailbox to get in unselected state
152 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
153 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
154 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
155 } else {
156 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
157 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
158 $messageCount = $mbx_response['EXISTS'];
159 if ($messageCount > 0) {
160 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
161 }
162 // after copy close the mailbox to get in unselected state
163 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
164 }
165 }
166
167 /**
168 * Recursive function that outputs a tree In-Pre-Order.
169 * @param int index the node to start (usually 0)
170 * @param array tree the tree to walk
171 * @return void
172 */
173 function simpleWalkTreePre($index, $tree) {
174 if ($tree[$index]['doIHaveChildren']) {
175 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
176 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
177 }
178 echo $tree[$index]['value'] . '<br />';
179 } else {
180 echo $tree[$index]['value'] . '<br />';
181 }
182 }
183 ?>