Remove isWarning as it's been dropped from PEAR with cvs versions
[squirrelmail.git] / functions / tree.php
1 <?php
2
3 /**
4 * tree.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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 */
14
15 require_once(SM_PATH . 'functions/imap.php');
16
17 /**
18 * findParentForChild
19 *
20 * Recursive function to find the correct parent for a new node
21 */
22
23 function 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']) {
28 // loop through each subNode checking to see if we are a subNode of one of them
29 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
30 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
31 if ($result > -1)
32 return $result;
33 }
34 // if we aren't a child of one of the subNodes, must be a child of current node
35 return $treeIndexToStart;
36 } else
37 return $treeIndexToStart;
38 } else {
39 // we aren't a child of this node at all
40 return -1;
41 }
42 }
43
44 function 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
63 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
64 global $trash_folder;
65 if ($tree[$index]['doIHaveChildren']) {
66 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
67 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
68 }
69 if ($tree[$index]['value'] != $trash_folder) {
70 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
71 } else {
72 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
73 if ($mbx_response['EXISTS'] > 0) {
74 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
75 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
76 }
77 }
78 } else {
79 if ($tree[$index]['value'] != $trash_folder) {
80 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
81 } else {
82 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
83 if ($mbx_response['EXISTS'] > 0) {
84 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
85 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
86 }
87 }
88 }
89 }
90
91
92 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
93 if ($tree[$index]['doIHaveChildren']) {
94 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
95 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
96 }
97 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
98 } else {
99 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
100 }
101 }
102
103 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
104 global $trash_folder, $delimiter;
105
106 $position = strrpos($topFolderName, $delimiter) + 1;
107 $subFolderName = substr($tree[$index]['value'], $position);
108
109 if ($tree[$index]['doIHaveChildren']) {
110 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
111 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
112 $messageCount = $mbx_response['EXISTS'];
113 if ($messageCount > 0)
114 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
115 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
116 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
117 } else {
118 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
119 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
120 $messageCount = $mbx_response['EXISTS'];
121 if ($messageCount > 0)
122 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
123 }
124 }
125
126 function simpleWalkTreePre($index, $tree) {
127 if ($tree[$index]['doIHaveChildren']) {
128 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
129 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
130 }
131 echo $tree[$index]['value'] . '<br>';
132 } else {
133 echo $tree[$index]['value'] . '<br>';
134 }
135 }
136 ?>