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