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