6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
13 * @package squirrelmail
16 /** Clearly, this needs the IMAP functions.. */
17 require_once(SM_PATH
. 'functions/imap.php');
20 * Recursive function to find the correct parent for a new node.
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
27 function 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']) {
32 // loop through each subNode checking to see if we are a subNode of one of them
33 for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++
) {
34 $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
38 // if we aren't a child of one of the subNodes, must be a child of current node
39 return $treeIndexToStart;
41 return $treeIndexToStart;
43 // we aren't a child of this node at all
49 * Will insert a new value into the tree, based on a given comparison value.
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
55 function addChildNodeToTree($comparisonValue, $value, &$tree) {
56 $parentNode = findParentForChild($comparisonValue, 0, $tree);
58 // create a new subNode
59 $newNodeIndex = count($tree);
60 $tree[$newNodeIndex]['value'] = $value;
61 $tree[$newNodeIndex]['doIHaveChildren'] = false;
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;
68 $nextSubNode = count($tree[$parentNode]['subNodes']);
69 // make sure the parent knows it has children
70 $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
75 * Recursively walk the tree of trash mailboxes and delete all folders and messages
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
82 function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
84 if ($tree[$index]['doIHaveChildren']) {
85 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
86 walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
88 if ($tree[$index]['value'] != $trash_folder) {
89 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
91 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
92 if ($mbx_response['EXISTS'] > 0) {
93 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
94 // CLOSE === EXPUNGE and UNSELECT
95 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
99 if ($tree[$index]['value'] != $trash_folder) {
100 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
102 $mbx_response = sqimap_mailbox_select($imap_stream, $trash_folder);
103 if ($mbx_response['EXISTS'] > 0) {
104 sqimap_messages_flag ($imap_stream, 1, '*', 'Deleted', true);
105 // CLOSE === EXPUNGE and UNSELECT
106 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
114 * Recursively delete a tree of mail folders.
116 * @param int index the place in the tree to start, usually 0
117 * @param stream imap_stream the IMAP connection to send commands to
118 * @param array tree the tree to walk
121 function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) {
122 if ($tree[$index]['doIHaveChildren']) {
123 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
124 walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree);
126 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
128 sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
133 * Recursively walk a tree of folders to create them under the trash folder.
135 function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
136 global $trash_folder, $delimiter;
138 $position = strrpos($topFolderName, $delimiter);
139 if ($position !== FALSE) {
142 $subFolderName = substr($tree[$index]['value'], $position);
144 if ($tree[$index]['doIHaveChildren']) {
145 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
146 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
147 $messageCount = $mbx_response['EXISTS'];
148 if ($messageCount > 0) {
149 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
151 // after copy close the mailbox to get in unselected state
152 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
153 for ($j = 0;$j < count($tree[$index]['subNodes']); $j++
)
154 walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName);
156 sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, '');
157 $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
158 $messageCount = $mbx_response['EXISTS'];
159 if ($messageCount > 0) {
160 sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
162 // after copy close the mailbox to get in unselected state
163 sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
168 * Recursive function that outputs a tree In-Pre-Order.
169 * @param int index the node to start (usually 0)
170 * @param array tree the tree to walk
173 function simpleWalkTreePre($index, $tree) {
174 if ($tree[$index]['doIHaveChildren']) {
175 for ($j = 0; $j < count($tree[$index]['subNodes']); $j++
) {
176 simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree);
178 echo $tree[$index]['value'] . '<br>';
180 echo $tree[$index]['value'] . '<br>';