sqimap_messages_flag is depreciated and no longer exists. Now emptying the trash...
[squirrelmail.git] / functions / tree.php
CommitLineData
59177427 1<?php
245a6892 2
35586184 3/**
4 * tree.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
1519cc53 9 * This file provides functions to walk trees of folders, for
10 * instance to delete a whole tree.
35586184 11 *
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
35586184 14 */
a4351446 15
d6c32258 16/** Clearly, this needs the IMAP functions.. */
b68edc75 17require_once(SM_PATH . 'functions/imap.php');
35586184 18
19/**
8b096f0a 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
35586184 26 */
65c3ec94 27function 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']) {
41fd4ed4 32 // loop through each subNode checking to see if we are a subNode of one of them
8a549df2 33 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
65c3ec94 34 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
35 if ($result > -1)
36 return $result;
a4351446 37 }
41fd4ed4 38 // if we aren't a child of one of the subNodes, must be a child of current node
a4351446 39 return $treeIndexToStart;
65c3ec94 40 } else
a4351446 41 return $treeIndexToStart;
65c3ec94 42 } else {
43 // we aren't a child of this node at all
44 return -1;
45 }
46}
47
8b096f0a 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 */
65c3ec94 55function 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
8b096f0a 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 */
65c3ec94 82function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
83 global $trash_folder;
1519cc53 84 walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
85}
86
87/**
88 * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
89 *
90 * @param int index the place in the tree to start, usually 0
91 * @param stream imap_stream the IMAP connection to send commands to
92 * @param array tree the tree to walk
93 * @param mailbox the name of the root folder to empty
94 * @return void
95 */
96function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) {
65c3ec94 97 if ($tree[$index]['doIHaveChildren']) {
98 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 99 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
65c3ec94 100 }
1519cc53 101 if ($tree[$index]['value'] != $mailbox) {
8a549df2 102 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
65c3ec94 103 } else {
1519cc53 104 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
eaf1a312 105 if ($mbx_response['EXISTS'] > 0) {
a51932f2 106 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
eaf1a312 107 // CLOSE === EXPUNGE and UNSELECT
108 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
294bf31a 109 }
65c3ec94 110 }
111 } else {
1519cc53 112 if ($tree[$index]['value'] != $mailbox) {
8a549df2 113 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
65c3ec94 114 } else {
1519cc53 115 $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
eaf1a312 116 if ($mbx_response['EXISTS'] > 0) {
a51932f2 117 sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
eaf1a312 118 // CLOSE === EXPUNGE and UNSELECT
119 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
294bf31a 120 }
65c3ec94 121 }
122 }
123}
124
b44ee3f0 125
8b096f0a 126/**
127 * Recursively delete a tree of mail folders.
128 *
129 * @param int index the place in the tree to start, usually 0
130 * @param stream imap_stream the IMAP connection to send commands to
131 * @param array tree the tree to walk
132 * @return void
133 */
65c3ec94 134function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
135 if ($tree[$index]['doIHaveChildren']) {
136 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 137 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
65c3ec94 138 }
139 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
140 } else {
141 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
142 }
143}
144
8b096f0a 145/**
146 * Recursively walk a tree of folders to create them under the trash folder.
147 */
65c3ec94 148function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
149 global $trash_folder, $delimiter;
150
4bce71a5 151 $position = strrpos($topFolderName, $delimiter);
e521a833 152 if ($position !== FALSE) {
4bce71a5 153 $position++;
e521a833 154 }
65c3ec94 155 $subFolderName = substr($tree[$index]['value'], $position);
156
157 if ($tree[$index]['doIHaveChildren']) {
158 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
62f7daa5 159 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
eaf1a312 160 $messageCount = $mbx_response['EXISTS'];
161 if ($messageCount > 0) {
b44ee3f0 162 sqimap_messages_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 {
169 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
b44ee3f0 170 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
171 $messageCount = $mbx_response['EXISTS'];
eaf1a312 172 if ($messageCount > 0) {
b44ee3f0 173 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
eaf1a312 174 }
175 // after copy close the mailbox to get in unselected state
176 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
65c3ec94 177 }
178}
abddc974 179
8b096f0a 180/**
181 * Recursive function that outputs a tree In-Pre-Order.
182 * @param int index the node to start (usually 0)
183 * @param array tree the tree to walk
184 * @return void
185 */
65c3ec94 186function simpleWalkTreePre($index, $tree) {
187 if ($tree[$index]['doIHaveChildren']) {
188 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 189 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
65c3ec94 190 }
6fd95361 191 echo $tree[$index]['value'] . '<br />';
65c3ec94 192 } else {
6fd95361 193 echo $tree[$index]['value'] . '<br />';
65c3ec94 194 }
195}
a51932f2 196
197?>