| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * tree.php |
| 5 | * |
| 6 | * This file provides functions to walk trees of folders, for |
| 7 | * instance to delete a whole tree. |
| 8 | * |
| 9 | * @copyright © 1999-2006 The SquirrelMail Project Team |
| 10 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 11 | * @version $Id$ |
| 12 | * @package squirrelmail |
| 13 | */ |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * Recursive function to find the correct parent for a new node. |
| 18 | * |
| 19 | * @param mixed value the value to find a parent for |
| 20 | * @param int treeIndexToStart where to start the search, usually the root node (0) |
| 21 | * @param array tree the tree to search |
| 22 | * @return int the index of the parent |
| 23 | */ |
| 24 | function findParentForChild($value, $treeIndexToStart, $tree) { |
| 25 | // is $value in $tree[$treeIndexToStart]['value'] |
| 26 | if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) { |
| 27 | // do I have children, if not then must be a childnode of the current node |
| 28 | if ($tree[$treeIndexToStart]['doIHaveChildren']) { |
| 29 | // loop through each subNode checking to see if we are a subNode of one of them |
| 30 | for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) { |
| 31 | $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree); |
| 32 | if ($result > -1) |
| 33 | return $result; |
| 34 | } |
| 35 | // if we aren't a child of one of the subNodes, must be a child of current node |
| 36 | return $treeIndexToStart; |
| 37 | } else |
| 38 | return $treeIndexToStart; |
| 39 | } else { |
| 40 | // we aren't a child of this node at all |
| 41 | return -1; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Will insert a new value into the tree, based on a given comparison value. |
| 47 | * |
| 48 | * @param mixed comparisonValue the value to determine where the new element should be placed. |
| 49 | * @param mixed value the new node to insert |
| 50 | * @param array tree the tree to insert the node in, by ref |
| 51 | */ |
| 52 | function addChildNodeToTree($comparisonValue, $value, &$tree) { |
| 53 | $parentNode = findParentForChild($comparisonValue, 0, $tree); |
| 54 | |
| 55 | // create a new subNode |
| 56 | $newNodeIndex = count($tree); |
| 57 | $tree[$newNodeIndex]['value'] = $value; |
| 58 | $tree[$newNodeIndex]['doIHaveChildren'] = false; |
| 59 | |
| 60 | if ($tree[$parentNode]['doIHaveChildren'] == false) { |
| 61 | // make sure the parent knows it has children |
| 62 | $tree[$parentNode]['subNodes'][0] = $newNodeIndex; |
| 63 | $tree[$parentNode]['doIHaveChildren'] = true; |
| 64 | } else { |
| 65 | $nextSubNode = count($tree[$parentNode]['subNodes']); |
| 66 | // make sure the parent knows it has children |
| 67 | $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Recursively walk the tree of trash mailboxes and delete all folders and messages |
| 73 | * |
| 74 | * @param int index the place in the tree to start, usually 0 |
| 75 | * @param stream imap_stream the IMAP connection to send commands to |
| 76 | * @param array tree the tree to walk |
| 77 | * @return void |
| 78 | */ |
| 79 | function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) { |
| 80 | global $trash_folder; |
| 81 | walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages |
| 86 | * |
| 87 | * @param int index the place in the tree to start, usually 0 |
| 88 | * @param stream imap_stream the IMAP connection to send commands to |
| 89 | * @param array tree the tree to walk |
| 90 | * @param mailbox the name of the root folder to empty |
| 91 | * @return void |
| 92 | */ |
| 93 | function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) { |
| 94 | if ($tree[$index]['doIHaveChildren']) { |
| 95 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
| 96 | walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
| 97 | } |
| 98 | if ($tree[$index]['value'] != $mailbox) { |
| 99 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 100 | } else { |
| 101 | $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox); |
| 102 | if ($mbx_response['EXISTS'] > 0) { |
| 103 | sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true); |
| 104 | // CLOSE === EXPUNGE and UNSELECT |
| 105 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
| 106 | } |
| 107 | } |
| 108 | } else { |
| 109 | if ($tree[$index]['value'] != $mailbox) { |
| 110 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 111 | } else { |
| 112 | $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox); |
| 113 | if ($mbx_response['EXISTS'] > 0) { |
| 114 | sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true); |
| 115 | // CLOSE === EXPUNGE and UNSELECT |
| 116 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * Recursively delete a tree of mail folders. |
| 125 | * |
| 126 | * @param int index the place in the tree to start, usually 0 |
| 127 | * @param stream imap_stream the IMAP connection to send commands to |
| 128 | * @param array tree the tree to walk |
| 129 | * @return void |
| 130 | */ |
| 131 | function walkTreeInPreOrderDeleteFolders($index, $imap_stream, $tree) { |
| 132 | if ($tree[$index]['doIHaveChildren']) { |
| 133 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
| 134 | walkTreeInPreOrderDeleteFolders($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
| 135 | } |
| 136 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 137 | } else { |
| 138 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Recursively walk a tree of folders to create them under the trash folder. |
| 144 | */ |
| 145 | function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) { |
| 146 | global $trash_folder, $delimiter; |
| 147 | |
| 148 | $position = strrpos($topFolderName, $delimiter); |
| 149 | if ($position !== FALSE) { |
| 150 | $position++; |
| 151 | } |
| 152 | $subFolderName = substr($tree[$index]['value'], $position); |
| 153 | |
| 154 | if ($tree[$index]['doIHaveChildren']) { |
| 155 | // create new trash subfolder only if it does not exist. |
| 156 | if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName)) |
| 157 | sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ""); |
| 158 | |
| 159 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
| 160 | $messageCount = $mbx_response['EXISTS']; |
| 161 | if ($messageCount > 0) { |
| 162 | sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName); |
| 163 | } |
| 164 | // after copy close the mailbox to get in unselected state |
| 165 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
| 166 | for ($j = 0;$j < count($tree[$index]['subNodes']); $j++) |
| 167 | walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName); |
| 168 | } else { |
| 169 | if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName)) |
| 170 | sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ''); |
| 171 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
| 172 | $messageCount = $mbx_response['EXISTS']; |
| 173 | if ($messageCount > 0) { |
| 174 | sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName); |
| 175 | } |
| 176 | // after copy close the mailbox to get in unselected state |
| 177 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Recursive function that outputs a tree In-Pre-Order. |
| 183 | * @param int index the node to start (usually 0) |
| 184 | * @param array tree the tree to walk |
| 185 | * @return void |
| 186 | */ |
| 187 | function simpleWalkTreePre($index, $tree) { |
| 188 | if ($tree[$index]['doIHaveChildren']) { |
| 189 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
| 190 | simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree); |
| 191 | } |
| 192 | echo $tree[$index]['value'] . '<br />'; |
| 193 | } else { |
| 194 | echo $tree[$index]['value'] . '<br />'; |
| 195 | } |
| 196 | } |