| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * tree.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2004 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 | * @package squirrelmail |
| 14 | */ |
| 15 | |
| 16 | /** Clearly, this needs the IMAP functions.. */ |
| 17 | require_once(SM_PATH . 'functions/imap.php'); |
| 18 | |
| 19 | /** |
| 20 | * Recursive function to find the correct parent for a new node. |
| 21 | * |
| 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 |
| 26 | */ |
| 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); |
| 35 | if ($result > -1) |
| 36 | return $result; |
| 37 | } |
| 38 | // if we aren't a child of one of the subNodes, must be a child of current node |
| 39 | return $treeIndexToStart; |
| 40 | } else |
| 41 | return $treeIndexToStart; |
| 42 | } else { |
| 43 | // we aren't a child of this node at all |
| 44 | return -1; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Will insert a new value into the tree, based on a given comparison value. |
| 50 | * |
| 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 |
| 54 | */ |
| 55 | function addChildNodeToTree($comparisonValue, $value, &$tree) { |
| 56 | $parentNode = findParentForChild($comparisonValue, 0, $tree); |
| 57 | |
| 58 | // create a new subNode |
| 59 | $newNodeIndex = count($tree); |
| 60 | $tree[$newNodeIndex]['value'] = $value; |
| 61 | $tree[$newNodeIndex]['doIHaveChildren'] = false; |
| 62 | |
| 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; |
| 67 | } else { |
| 68 | $nextSubNode = count($tree[$parentNode]['subNodes']); |
| 69 | // make sure the parent knows it has children |
| 70 | $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Recursively walk the tree of trash mailboxes and delete all folders and messages |
| 76 | * |
| 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 |
| 80 | * @return void |
| 81 | */ |
| 82 | function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) { |
| 83 | global $trash_folder; |
| 84 | if ($tree[$index]['doIHaveChildren']) { |
| 85 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
| 86 | walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree); |
| 87 | } |
| 88 | if ($tree[$index]['value'] != $trash_folder) { |
| 89 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 90 | } else { |
| 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); |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | if ($tree[$index]['value'] != $trash_folder) { |
| 100 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 101 | } else { |
| 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); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Recursively delete a tree of mail folders. |
| 115 | * |
| 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 |
| 119 | * @return void |
| 120 | */ |
| 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); |
| 125 | } |
| 126 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 127 | } else { |
| 128 | sqimap_mailbox_delete($imap_stream, $tree[$index]['value']); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Recursively walk a tree of folders to create them under the trash folder. |
| 134 | */ |
| 135 | function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) { |
| 136 | global $trash_folder, $delimiter; |
| 137 | |
| 138 | $position = strrpos($topFolderName, $delimiter); |
| 139 | if ($position !== FALSE) |
| 140 | $position++; |
| 141 | $subFolderName = substr($tree[$index]['value'], $position); |
| 142 | |
| 143 | if ($tree[$index]['doIHaveChildren']) { |
| 144 | sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ""); |
| 145 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
| 146 | $messageCount = $mbx_response['EXISTS']; |
| 147 | if ($messageCount > 0) { |
| 148 | sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName); |
| 149 | } |
| 150 | // after copy close the mailbox to get in unselected state |
| 151 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
| 152 | for ($j = 0;$j < count($tree[$index]['subNodes']); $j++) |
| 153 | walkTreeInPostOrderCreatingFoldersUnderTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree, $topFolderName); |
| 154 | } else { |
| 155 | sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, ''); |
| 156 | $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']); |
| 157 | $messageCount = $mbx_response['EXISTS']; |
| 158 | if ($messageCount > 0) { |
| 159 | sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName); |
| 160 | } |
| 161 | // after copy close the mailbox to get in unselected state |
| 162 | sqimap_run_command($imap_stream,'CLOSE',false,$response,$message); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Recursive function that outputs a tree In-Pre-Order. |
| 168 | * @param int index the node to start (usually 0) |
| 169 | * @param array tree the tree to walk |
| 170 | * @return void |
| 171 | */ |
| 172 | function simpleWalkTreePre($index, $tree) { |
| 173 | if ($tree[$index]['doIHaveChildren']) { |
| 174 | for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) { |
| 175 | simpleWalkTreePre($tree[$index]['subNodes'][$j], $tree); |
| 176 | } |
| 177 | echo $tree[$index]['value'] . '<br>'; |
| 178 | } else { |
| 179 | echo $tree[$index]['value'] . '<br>'; |
| 180 | } |
| 181 | } |
| 182 | ?> |