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