Modified sqimap_session_id to return something when there is no session ID
[squirrelmail.git] / functions / tree.php
1 <?php
2
3 /**
4 ** tree.php
5 **
6 ** Copyright (c) 1999-2001 The Squirrelmail Development 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('../functions/imap.php');
16
17 // Recursive function to find the correct parent for a new node
18 function findParentForChild($value, $treeIndexToStart, $tree) {
19 // is $value in $tree[$treeIndexToStart]['value']
20 if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
21 // do I have children, if not then must be a childnode of the current node
22 if ($tree[$treeIndexToStart]['doIHaveChildren']) {
23 // loop through each subNode checking to see if we are a subNode of one of them
24 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
25 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
26 if ($result > -1)
27 return $result;
28 }
29 // if we aren't a child of one of the subNodes, must be a child of current node
30 return $treeIndexToStart;
31 } else
32 return $treeIndexToStart;
33 } else {
34 // we aren't a child of this node at all
35 return -1;
36 }
37 }
38
39 function addChildNodeToTree($comparisonValue, $value, &$tree) {
40 $parentNode = findParentForChild($comparisonValue, 0, $tree);
41
42 // create a new subNode
43 $newNodeIndex = count($tree);
44 $tree[$newNodeIndex]['value'] = $value;
45 $tree[$newNodeIndex]['doIHaveChildren'] = false;
46
47 if ($tree[$parentNode]['doIHaveChildren'] == false) {
48 // make sure the parent knows it has children
49 $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
50 $tree[$parentNode]['doIHaveChildren'] = true;
51 } else {
52 $nextSubNode = count($tree[$parentNode]['subNodes']);
53 // make sure the parent knows it has children
54 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
55 }
56 }
57
58 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
59 global $trash_folder;
60 if ($tree[$index]['doIHaveChildren']) {
61 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
62 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
63 }
64 if ($tree[$index]['value'] != $trash_folder) {
65 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
66 } else {
67 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
68 if ($numMessages > 0) {
69 sqimap_mailbox_select($imap_stream, $trash_folder);
70 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
71 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
72 }
73 }
74 } else {
75 if ($tree[$index]['value'] != $trash_folder) {
76 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
77 } else {
78 $numMessages = sqimap_get_num_messages($imap_stream, $trash_folder);
79 if ($numMessages > 0) {
80 sqimap_mailbox_select($imap_stream, $trash_folder);
81 sqimap_messages_flag ($imap_stream, 1, $numMessages, 'Deleted');
82 sqimap_mailbox_expunge($imap_stream, $trash_folder, true);
83 }
84 }
85 }
86 }
87
88 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
89 if ($tree[$index]['doIHaveChildren']) {
90 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
91 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
92 }
93 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
94 } else {
95 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
96 }
97 }
98
99 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
100 global $trash_folder, $delimiter;
101
102 $position = strrpos($topFolderName, $delimiter) + 1;
103 $subFolderName = substr($tree[$index]['value'], $position);
104
105 if ($tree[$index]['doIHaveChildren']) {
106 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
107 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
108
109 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
110 if ($messageCount > 0)
111 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
112
113 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++)
114 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
115 } else {
116 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
117 sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
118
119 $messageCount = sqimap_get_num_messages($imap_stream, $tree[$index]['value']);
120 if ($messageCount > 0)
121 sqimap_messages_copy($imap_stream, 1, $messageCount, $trash_folder . $delimiter . $subFolderName);
122 }
123 }
124
125 function simpleWalkTreePre($index, $tree) {
126 if ($tree[$index]['doIHaveChildren']) {
127 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
128 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
129 }
130 echo $tree[$index]['value'] . '<br>';
131 } else {
132 echo $tree[$index]['value'] . '<br>';
133 }
134 }
135 ?>