tree.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * tree.php
  4. *
  5. * This file provides functions to walk trees of folders, for
  6. * instance to delete a whole tree.
  7. *
  8. * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /**
  14. * Recursive function to find the correct parent for a new node.
  15. *
  16. * @param mixed value the value to find a parent for
  17. * @param int treeIndexToStart where to start the search, usually the root node (0)
  18. * @param array tree the tree to search
  19. * @return int the index of the parent
  20. */
  21. function findParentForChild($value, $treeIndexToStart, $tree) {
  22. // is $value in $tree[$treeIndexToStart]['value']
  23. if ((isset($tree[$treeIndexToStart])) && (strstr($value, $tree[$treeIndexToStart]['value']))) {
  24. // do I have children, if not then must be a childnode of the current node
  25. if ($tree[$treeIndexToStart]['doIHaveChildren']) {
  26. // loop through each subNode checking to see if we are a subNode of one of them
  27. for ($i=0;$i< count($tree[$treeIndexToStart]['subNodes']);$i++) {
  28. $result = findParentForChild($value, $tree[$treeIndexToStart]['subNodes'][$i], $tree);
  29. if ($result > -1)
  30. return $result;
  31. }
  32. // if we aren't a child of one of the subNodes, must be a child of current node
  33. return $treeIndexToStart;
  34. } else
  35. return $treeIndexToStart;
  36. } else {
  37. // we aren't a child of this node at all
  38. return -1;
  39. }
  40. }
  41. /**
  42. * Will insert a new value into the tree, based on a given comparison value.
  43. *
  44. * @param mixed comparisonValue the value to determine where the new element should be placed.
  45. * @param mixed value the new node to insert
  46. * @param array tree the tree to insert the node in, by ref
  47. */
  48. function addChildNodeToTree($comparisonValue, $value, &$tree) {
  49. $parentNode = findParentForChild($comparisonValue, 0, $tree);
  50. // create a new subNode
  51. $newNodeIndex = count($tree);
  52. $tree[$newNodeIndex]['value'] = $value;
  53. $tree[$newNodeIndex]['doIHaveChildren'] = false;
  54. if ($tree[$parentNode]['doIHaveChildren'] == false) {
  55. // make sure the parent knows it has children
  56. $tree[$parentNode]['subNodes'][0] = $newNodeIndex;
  57. $tree[$parentNode]['doIHaveChildren'] = true;
  58. } else {
  59. $nextSubNode = count($tree[$parentNode]['subNodes']);
  60. // make sure the parent knows it has children
  61. $tree[$parentNode]['subNodes'][$nextSubNode] = $newNodeIndex;
  62. }
  63. }
  64. /**
  65. * Recursively walk the tree of trash mailboxes and delete all folders and messages
  66. *
  67. * @param int index the place in the tree to start, usually 0
  68. * @param stream imap_stream the IMAP connection to send commands to
  69. * @param array tree the tree to walk
  70. * @return void
  71. */
  72. function walkTreeInPreOrderEmptyTrash($index, $imap_stream, $tree) {
  73. global $trash_folder;
  74. walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $trash_folder);
  75. }
  76. /**
  77. * Recursively walk the tree of mailboxes in the given folder and delete all folders and messages
  78. *
  79. * @param int index the place in the tree to start, usually 0
  80. * @param stream imap_stream the IMAP connection to send commands to
  81. * @param array tree the tree to walk
  82. * @param mailbox the name of the root folder to empty
  83. * @return void
  84. */
  85. function walkTreeInPreOrderEmptyFolder($index, $imap_stream, $tree, $mailbox) {
  86. if ($tree[$index]['doIHaveChildren']) {
  87. for ($j = 0; $j < count($tree[$index]['subNodes']); $j++) {
  88. walkTreeInPreOrderEmptyTrash($tree[$index]['subNodes'][$j], $imap_stream, $tree);
  89. }
  90. if ($tree[$index]['value'] != $mailbox) {
  91. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  92. } else {
  93. $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
  94. if ($mbx_response['EXISTS'] > 0) {
  95. sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
  96. // CLOSE === EXPUNGE and UNSELECT
  97. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  98. }
  99. }
  100. } else {
  101. if ($tree[$index]['value'] != $mailbox) {
  102. sqimap_mailbox_delete($imap_stream, $tree[$index]['value']);
  103. } else {
  104. $mbx_response = sqimap_mailbox_select($imap_stream, $mailbox);
  105. if ($mbx_response['EXISTS'] > 0) {
  106. sqimap_toggle_flag($imap_stream, '1:*', '\\Deleted', true, true);
  107. // CLOSE === EXPUNGE and UNSELECT
  108. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  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. * Recursively walk a tree of folders to create them under the trash folder.
  133. */
  134. function walkTreeInPostOrderCreatingFoldersUnderTrash($index, $imap_stream, $tree, $topFolderName) {
  135. global $trash_folder, $delimiter;
  136. $position = strrpos($topFolderName, $delimiter);
  137. if ($position !== FALSE) {
  138. $position++;
  139. }
  140. $subFolderName = substr($tree[$index]['value'], $position);
  141. if ($tree[$index]['doIHaveChildren']) {
  142. // create new trash subfolder only if it does not exist.
  143. if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
  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_msgs_list_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. if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
  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_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
  161. }
  162. // after copy close the mailbox to get in unselected state
  163. sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);
  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. ?>