CLOSE the selected mailbox (unselect) before we delete it.
[squirrelmail.git] / functions / tree.php
CommitLineData
59177427 1<?php
245a6892 2
35586184 3/**
4 * tree.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 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 * $Id$
13 */
a4351446 14
b68edc75 15require_once(SM_PATH . 'functions/imap.php');
35586184 16
17/**
18 * findParentForChild
19 *
20 * Recursive function to find the correct parent for a new node
21 */
7350889b 22
65c3ec94 23function findParentForChild($value, $treeIndexToStart, $tree) {
24 // is $value in $tree[$treeIndexToStart]['value']
25 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
26 // do I have children, if not then must be a childnode of the current node
27 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
41fd4ed4 28 // loop through each subNode checking to see if we are a subNode of one of them
8a549df2 29 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
65c3ec94 30 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
31 if ($result > -1)
32 return $result;
a4351446 33 }
41fd4ed4 34 // if we aren't a child of one of the subNodes, must be a child of current node
a4351446 35 return $treeIndexToStart;
65c3ec94 36 } else
a4351446 37 return $treeIndexToStart;
65c3ec94 38 } else {
39 // we aren't a child of this node at all
40 return -1;
41 }
42}
43
44function addChildNodeToTree($comparisonValue, $value, &$tree) {
45 $parentNode = findParentForChild($comparisonValue, 0, $tree);
46
47 // create a new subNode
48 $newNodeIndex = count($tree);
49 $tree[$newNodeIndex]['value'] = $value;
50 $tree[$newNodeIndex]['doIHaveChildren'] = false;
51
52 if ($tree[$parentNode]['doIHaveChildren'] == false) {
53 // make sure the parent knows it has children
54 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
55 $tree[$parentNode]['doIHaveChildren'] = true;
56 } else {
57 $nextSubNode = count($tree[$parentNode]['subNodes']);
58 // make sure the parent knows it has children
59 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
60 }
61}
62
63function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
64 global $trash_folder;
65 if ($tree[$index]['doIHaveChildren']) {
66 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 67 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
65c3ec94 68 }
69 if ($tree[$index]['value'] != $trash_folder) {
8a549df2 70 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
65c3ec94 71 } else {
b44ee3f0 72 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
eaf1a312 73 if ($mbx_response['EXISTS'] > 0) {
b44ee3f0 74 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
eaf1a312 75 // CLOSE === EXPUNGE and UNSELECT
76 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
294bf31a 77 }
65c3ec94 78 }
79 } else {
80 if ($tree[$index]['value'] != $trash_folder) {
8a549df2 81 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
65c3ec94 82 } else {
eaf1a312 83 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
84 if ($mbx_response['EXISTS'] > 0) {
85 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
86 // CLOSE === EXPUNGE and UNSELECT
87 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
294bf31a 88 }
65c3ec94 89 }
90 }
91}
92
b44ee3f0 93
65c3ec94 94function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
95 if ($tree[$index]['doIHaveChildren']) {
96 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 97 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
65c3ec94 98 }
99 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
100 } else {
101 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
102 }
103}
104
105function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
106 global $trash_folder, $delimiter;
107
108 $position = strrpos($topFolderName, $delimiter) + 1;
109 $subFolderName = substr($tree[$index]['value'], $position);
110
111 if ($tree[$index]['doIHaveChildren']) {
112 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
b44ee3f0 113 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
eaf1a312 114 $messageCount = $mbx_response['EXISTS'];
115 if ($messageCount > 0) {
b44ee3f0 116 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
eaf1a312 117 }
118 // after copy close the mailbox to get in unselected state
119 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
65c3ec94 120 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
525b7ae6 121 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
65c3ec94 122 } else {
123 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
b44ee3f0 124 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
125 $messageCount = $mbx_response['EXISTS'];
eaf1a312 126 if ($messageCount > 0) {
b44ee3f0 127 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
eaf1a312 128 }
129 // after copy close the mailbox to get in unselected state
130 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
65c3ec94 131 }
132}
abddc974 133
65c3ec94 134function simpleWalkTreePre($index, $tree) {
135 if ($tree[$index]['doIHaveChildren']) {
136 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
8a549df2 137 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
65c3ec94 138 }
139 echo $tree[$index]['value'] . '<br>';
140 } else {
141 echo $tree[$index]['value'] . '<br>';
142 }
143}
525b7ae6 144?>